ERL_NIF_TERM _hh_from_binary(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { ErlNifBinary source; if (!enif_inspect_binary(env, argv[0], &source)) { return enif_make_badarg(env); } hdr_histogram_t* target = NULL; int success = hdr_decode(source.data, source.size, &target); if (success != 0) { return make_error(env, "bad_hdr_binary"); } ErlNifResourceType* ctx_type = get_hh_ctx_type(env); hh_ctx_t* ctx = (hh_ctx_t*)enif_alloc_resource(ctx_type, sizeof(hh_ctx_t)); ctx->data = (hdr_histogram_t*)target; ctx->highest_trackable_value = target->highest_trackable_value; ctx->significant_figures = target->significant_figures; ERL_NIF_TERM result = enif_make_resource(env, ctx); enif_release_resource(ctx); return enif_make_tuple2(env, ATOM_OK, result); }
static char* test_encode_and_decode() { load_histograms(); size_t raw_histogram_size = hdr_get_memory_size(cor_histogram); uint8_t* buffer = (uint8_t*) malloc(hdr_get_memory_size(cor_histogram)); size_t encode_result = hdr_encode(cor_histogram, buffer, raw_histogram_size); mu_assert("Did not encode", encode_result != 0); mu_assert("Incorrect size", encode_result <= raw_histogram_size); struct hdr_histogram* loaded_histogram = NULL; hdr_decode(buffer, raw_histogram_size, &loaded_histogram); int compare_result = memcmp(cor_histogram, loaded_histogram, raw_histogram_size); if (compare_result != 0) { uint8_t* a = (uint8_t*) cor_histogram; uint8_t* b = (uint8_t*) loaded_histogram; for (int i = 0; i < raw_histogram_size; i++) { if (a[i] != b[i]) { printf("Mismatch at %d: %x - %x\n", i, a[i] & 0xFF, b[i] & 0xFF); } } } mu_assert("Comparison did not match", compare_result == 0); return 0; }