Merge pull request #7 from GameAnalytics/carray_tests

Test hyper_carray backend
diff --git a/c_src/hyper_carray.c b/c_src/hyper_carray.c
index 554badd..7d78554 100644
--- a/c_src/hyper_carray.c
+++ b/c_src/hyper_carray.c
@@ -133,7 +133,7 @@
 void dtor(ErlNifEnv * env, void *obj);
 
 /*
- * Given a list of at least 2 hyper_carrays [A,B,...], merge into a single new
+ * Given a list of at least 1 hyper_carrays [A,B,...], merge into a single new
  * hyper_carray N. Where the i-ths item N[i] is max(A[i], B[i], ...).
  * A, B, and so on are assumed to be _different_ hyper_carrays.
  */
@@ -148,7 +148,7 @@
 	    || !enif_get_list_cell(env, argv[0], &head, &tail))
 		return enif_make_badarg(env);
 
-	if (narrays < 2)
+	if (narrays < 1)
 		return enif_make_badarg(env);
 
 	void *tmp = NULL;
@@ -249,11 +249,11 @@
 	struct hyper_carray *restrict arr = NULL;
 	HYPER_CARRAY_OR_BADARG(argv[0], arr);
 
-	size_t nbytes = HYPER_CARRAY_SIZE + arr->size;
+	size_t nbytes = arr->size;
 
 	ERL_NIF_TERM bin;
 	unsigned char *buf = enif_make_new_binary(env, nbytes, &bin);
-	memcpy(buf, arr, nbytes);
+	memcpy(buf, arr->items, nbytes);
 
 	return bin;
 }
@@ -273,7 +273,7 @@
 
 	struct hyper_carray *restrict arr = NULL;
 	carray_alloc(precision, &arr);
-	memcpy(arr, bin.data, HYPER_CARRAY_SIZE + arr->size);
+	memcpy(arr->items, bin.data, arr->size);
 
 	ERL_NIF_TERM erl_res = enif_make_resource(env, arr);
 	enif_release_resource(arr);
diff --git a/src/hyper.erl b/src/hyper.erl
index 863eb6a..9fabba6 100644
--- a/src/hyper.erl
+++ b/src/hyper.erl
@@ -305,7 +305,7 @@
     Ps      = [15],
     Cards   = [1, 100, 500, 1000, 2500, 5000, 10000,
                15000, 25000, 50000, 100000, 1000000],
-    Mods    = [hyper_gb, hyper_array, hyper_bisect, hyper_binary],
+    Mods    = [hyper_gb, hyper_array, hyper_bisect, hyper_binary, hyper_carray],
     Repeats = 10,
 
     Time = fun (F, Args) ->
diff --git a/test/hyper_test.erl b/test/hyper_test.erl
index e0f9624..dfa1157 100644
--- a/test/hyper_test.erl
+++ b/test/hyper_test.erl
@@ -30,6 +30,7 @@
       ?_test(small_big_union_t()),
       ?_test(intersect_card_t()),
       ?_test(bad_serialization_t()),
+      {"Union property with hyper_carry", RunProp(prop_union(hyper_carray))},
       {"Union property with hyper_binary", RunProp(prop_union(hyper_binary))},
       {"Union property with hyper_array", RunProp(prop_union(hyper_array))},
       {"Union property with hyper_bisect", RunProp(prop_union(hyper_bisect))},
@@ -85,11 +86,13 @@
     Array  = hyper:compact(hyper:insert_many(Values, hyper:new(P, hyper_array))),
     Bisect = hyper:compact(hyper:insert_many(Values, hyper:new(P, hyper_bisect))),
     Binary = hyper:compact(hyper:insert_many(Values, hyper:new(P, hyper_binary))),
+    Carray = hyper:compact(hyper:insert_many(Values, hyper:new(P, hyper_carray))),
 
     {hyper_gb    , GbRegisters}     = Gb#hyper.registers,
     {hyper_array , ArrayRegisters}  = Array#hyper.registers,
     {hyper_bisect, BisectRegisters} = Bisect#hyper.registers,
     {hyper_binary, BinaryRegisters} = Binary#hyper.registers,
+    {hyper_carray, CarrayRegisters} = Carray#hyper.registers,
 
     ExpectedRegisters = lists:foldl(
                           fun (Value, Registers) ->
@@ -120,21 +123,25 @@
     ?assertEqual(ExpectedBytes, hyper_array:encode_registers(ArrayRegisters)),
     ?assertEqual(ExpectedBytes, hyper_bisect:encode_registers(BisectRegisters)),
     ?assertEqual(ExpectedBytes, hyper_binary:encode_registers(BinaryRegisters)),
+    ?assertEqual(ExpectedBytes, hyper_carray:encode_registers(CarrayRegisters)),
 
     ?assertEqual(hyper:card(Gb),
                  hyper:card(hyper:from_json(hyper:to_json(Array), hyper_gb))),
     ?assertEqual(Array, hyper:from_json(hyper:to_json(Array), hyper_array)),
-    ?assertEqual(Array, hyper:from_json(hyper:to_json(Bisect), hyper_array)),
     ?assertEqual(Bisect, hyper:from_json(hyper:to_json(Array), hyper_bisect)),
+    ?assertEqual(Binary, hyper:from_json(hyper:to_json(Binary), hyper_binary)),
+    ?assertEqual(Carray, hyper:from_json(hyper:to_json(Carray), hyper_carray)),
 
 
     ?assertEqual(hyper:to_json(Gb), hyper:to_json(Array)),
     ?assertEqual(hyper:to_json(Gb), hyper:to_json(Bisect)),
     ?assertEqual(hyper:to_json(Gb), hyper:to_json(Binary)),
+    ?assertEqual(hyper:to_json(Gb), hyper:to_json(Carray)),
 
     ?assertEqual(hyper:card(Gb), hyper:card(Array)),
     ?assertEqual(hyper:card(Gb), hyper:card(Bisect)),
-    ?assertEqual(hyper:card(Gb), hyper:card(Binary)).
+    ?assertEqual(hyper:card(Gb), hyper:card(Binary)),
+    ?assertEqual(hyper:card(Gb), hyper:card(Carray)).
 
 
 
@@ -186,7 +193,7 @@
 
 
 error_range_t() ->
-    Mods = [hyper_gb, hyper_array, hyper_bisect, hyper_binary],
+    Mods = backends(),
     Run = fun (Cardinality, P, Mod) ->
                   lists:foldl(fun (V, H) ->
                                       hyper:insert(V, H)
@@ -367,7 +374,7 @@
 %%
 
 backends() ->
-    [hyper_gb, hyper_array, hyper_bisect, hyper_binary].
+    [hyper_gb, hyper_array, hyper_bisect, hyper_binary, hyper_carray].
 
 
 gen_values() ->