update tests

This commit is contained in:
Simon Cruanes 2026-03-14 03:34:48 +00:00
parent e52574c5fb
commit d12a809658

View file

@ -1,99 +1,111 @@
include (val Containers_testlib.make ~__FILE__ ())
module H = Containers_xxhash
(* Gold tests: hash_string with XXH64 *)
(* Gold tests: hash_string with XXH64 (mix_string + finalize) *)
;;
t @@ fun () ->
assert_equal ~printer:Int64.to_string (-1205034819632174695L)
(H.hash_string ~seed:0L "");
assert_equal ~printer:Int64.to_string (-3049013831022690741L)
(H.hash_string ~seed:1L "");
assert_equal ~printer:Int64.to_string (-7444071767201028348L)
(H.hash_string ~seed:42L "");
assert_equal ~printer:Int64.to_string 2994696410035606400L
(H.hash_string ~seed:(-1L) "");
assert_equal ~printer:Int64.to_string (-3292477735350538661L)
(H.hash_string ~seed:0L "a");
assert_equal ~printer:Int64.to_string (-2395144786285869370L)
(H.hash_string ~seed:1L "a");
assert_equal ~printer:Int64.to_string (-8582455328737087284L)
(H.hash_string ~seed:42L "a");
assert_equal ~printer:Int64.to_string 6972758980737027682L
(H.hash_string ~seed:(-1L) "a");
assert_equal ~printer:Int64.to_string 2794345569481354659L
(H.hash_string ~seed:0L "hello");
assert_equal ~printer:Int64.to_string 2584346877953614258L
(H.hash_string ~seed:1L "hello");
assert_equal ~printer:Int64.to_string (-4367754540140381902L)
(H.hash_string ~seed:42L "hello");
assert_equal ~printer:Int64.to_string 125878816811915416L
(H.hash_string ~seed:(-1L) "hello");
assert_equal ~printer:Int64.to_string (-4510281645523327586L)
(H.hash_string ~seed:0L "hello, world!");
assert_equal ~printer:Int64.to_string (-504920995464555508L)
(H.hash_string ~seed:1L "hello, world!");
assert_equal ~printer:Int64.to_string 6256815367265528243L
(H.hash_string ~seed:42L "hello, world!");
assert_equal ~printer:Int64.to_string 3946915247760971950L
(H.hash_string ~seed:(-1L) "hello, world!");
assert_equal ~printer:Int64.to_string 1513236774081638803L
(H.hash_string ~seed:0L "the quick brown fox");
assert_equal ~printer:Int64.to_string 4806561598883688116L
(H.hash_string ~seed:1L "the quick brown fox");
assert_equal ~printer:Int64.to_string 6882318601984224800L
(H.hash_string ~seed:42L "the quick brown fox");
assert_equal ~printer:Int64.to_string 3091774062649670660L
(H.hash_string ~seed:(-1L) "the quick brown fox");
assert_equal ~printer:Int64.to_string (-8037231448521241007L)
(H.hash_string ~seed:H.seed "");
assert_equal ~printer:Int64.to_string 7619381941762342490L
(H.hash_string ~seed:H.seed "a");
assert_equal ~printer:Int64.to_string 8482916093137399771L
(H.hash_string ~seed:H.seed "hello");
assert_equal ~printer:Int64.to_string (-3052030864281505429L)
(H.hash_string ~seed:H.seed "hello, world!");
assert_equal ~printer:Int64.to_string 2707297459162763210L
(H.hash_string ~seed:H.seed "the quick brown fox");
true
;;
(* Gold tests: hash_string with non-default seed (seed from mix_int) *)
t @@ fun () ->
(* seed after mixing 1 into seed=0: hash_string uses that as XXH64 seed *)
let seed1 = H.mix_int H.seed 1 in
(* these values computed from: finalize(mix_string(mix_int(0,1), s)) *)
assert_equal ~printer:Int64.to_string
(H.hash_string ~seed:seed1 "")
(H.hash_string ~seed:seed1 "");
(* just test determinism with custom seed *)
assert_equal ~printer:Int64.to_string
(H.hash_string ~seed:seed1 "hello")
(H.hash_string ~seed:seed1 "hello");
(* different seeds produce different hashes for same string *)
assert (
not
(Int64.equal
(H.hash_string ~seed:H.seed "hello")
(H.hash_string ~seed:seed1 "hello")));
true
;;
(* Gold tests: hash_string default seed=0 *)
t @@ fun () ->
assert_equal ~printer:Int64.to_string (-1205034819632174695L) (H.hash_string "");
assert_equal ~printer:Int64.to_string (-3292477735350538661L)
(H.hash_string "a");
assert_equal ~printer:Int64.to_string 2794345569481354659L
assert_equal ~printer:Int64.to_string (-8037231448521241007L) (H.hash_string "");
assert_equal ~printer:Int64.to_string 7619381941762342490L (H.hash_string "a");
assert_equal ~printer:Int64.to_string 8482916093137399771L
(H.hash_string "hello");
true
;;
(* Gold tests: hash_int64 *)
t @@ fun () ->
assert_equal ~printer:Int64.to_string 3803688792395291579L (H.hash_int64 0L 0L);
assert_equal ~printer:Int64.to_string (-6977822845260490347L)
(H.hash_int64 1L 0L);
assert_equal ~printer:Int64.to_string (-5379971487550586029L)
(H.hash_int64 42L 0L);
assert_equal ~printer:Int64.to_string (-8804195676797548855L)
(H.hash_int64 (-1L) 0L);
assert_equal ~printer:Int64.to_string (-7296932117151183542L)
(H.hash_int64 1234567890123456789L 0L);
assert_equal ~printer:Int64.to_string (-5605595894618674504L) (H.hash_int64 0L);
assert_equal ~printer:Int64.to_string 7046788939542163588L (H.hash_int64 1L);
assert_equal ~printer:Int64.to_string 2627184251037003377L (H.hash_int64 42L);
assert_equal ~printer:Int64.to_string (-8629399683307595115L)
(H.hash_int64 (-1L));
assert_equal ~printer:Int64.to_string 8147024165990365903L
(H.hash_int64 1234567890123456789L);
true
;;
(* Gold tests: mix64 *)
(* Gold tests: hash_int *)
t @@ fun () ->
assert_equal ~printer:Int64.to_string 2506089352882295055L (H.mix64 0L 1L);
assert_equal ~printer:Int64.to_string (-7001672635703045582L) (H.mix64 1L 42L);
assert_equal ~printer:Int64.to_string (-6036538516425062073L)
(H.mix64 42L (-1L));
assert_equal ~printer:Int64.to_string 3785813419010030675L
(H.mix64 (-1L) 1234567890123456789L);
assert_equal ~printer:Int64.to_string (-7296932117151183542L)
(H.mix64 1234567890123456789L 0L);
assert_equal ~printer:Int64.to_string (-5605595894618674504L) (H.hash_int 0);
assert_equal ~printer:Int64.to_string 7046788939542163588L (H.hash_int 1);
assert_equal ~printer:Int64.to_string 2627184251037003377L (H.hash_int 42);
assert_equal ~printer:Int64.to_string (-8629399683307595115L) (H.hash_int (-1));
assert_equal ~printer:Int64.to_string (-3317520227865190253L)
(H.hash_int 1234567890);
true
;;
(* Gold tests: finalize64 *)
(* Gold tests: hash_int32 *)
t @@ fun () ->
assert_equal ~printer:Int64.to_string 3803688792395291579L (H.finalize64 0L);
assert_equal ~printer:Int64.to_string (-6977822845260490347L) (H.finalize64 1L);
assert_equal ~printer:Int64.to_string (-5379971487550586029L) (H.finalize64 42L);
assert_equal ~printer:Int64.to_string (-8804195676797548855L)
(H.finalize64 (-1L));
assert_equal ~printer:Int64.to_string (-7296932117151183542L)
(H.finalize64 1234567890123456789L);
assert_equal ~printer:Int64.to_string (-5605595894618674504L) (H.hash_int32 0l);
assert_equal ~printer:Int64.to_string 7046788939542163588L (H.hash_int32 1l);
assert_equal ~printer:Int64.to_string 2627184251037003377L (H.hash_int32 42l);
assert_equal ~printer:Int64.to_string (-8629399683307595115L)
(H.hash_int32 (-1l));
assert_equal ~printer:Int64.to_string (-3317520227865190253L)
(H.hash_int32 1234567890l);
true
;;
(* Gold tests: hash_bool *)
t @@ fun () ->
assert_equal ~printer:Int64.to_string (-5605595894618674504L)
(H.hash_bool false);
assert_equal ~printer:Int64.to_string 7046788939542163588L (H.hash_bool true);
true
;;
(* Gold tests: hash_char *)
t @@ fun () ->
assert_equal ~printer:Int64.to_string (-1595464024050301112L) (H.hash_char 'a');
assert_equal ~printer:Int64.to_string (-2980224328396984668L) (H.hash_char 'z');
assert_equal ~printer:Int64.to_string 7387411195422956975L (H.hash_char '0');
true
;;
(* Gold tests: finalize(seed) = finalize(0L) = XXH64(&0, 8, 0) *)
t @@ fun () ->
assert_equal ~printer:Int64.to_string 3803688792395291579L (H.finalize H.seed);
(* finalize is deterministic *)
assert_equal ~printer:Int64.to_string
(H.finalize (H.mix_int64 H.seed 42L))
(H.finalize (H.mix_int64 H.seed 42L));
true
;;
@ -103,20 +115,18 @@ Int64.equal (H.hash_string s) (H.hash_string s)
;;
q ~count:10_000 Q.int64 @@ fun v ->
Int64.equal (H.hash_int64 v 0L) (H.hash_int64 v 0L)
Int64.equal (H.hash_int64 v) (H.hash_int64 v)
;;
q ~count:10_000 Q.int @@ fun v -> Int.equal (H.hash_int v 0) (H.hash_int v 0);;
q ~count:10_000 Q.int @@ fun v -> Int64.equal (H.hash_int v) (H.hash_int v);;
q ~count:10_000 Q.bool @@ fun b -> Int64.equal (H.hash_bool b) (H.hash_bool b);;
q ~count:10_000 Q.char @@ fun c -> Int64.equal (H.hash_char c) (H.hash_char c);;
q ~count:10_000 Q.int64 @@ fun h ->
Int64.equal (H.finalize64 h) (H.finalize64 h)
;;
(* mix64 is not commutative for most pairs *)
(* mix_int64 is not commutative for most pairs *)
q ~count:10_000 (Q.pair Q.int64 Q.int64) @@ fun (a, b) ->
Q.assume (not (Int64.equal a b));
let ab = H.mix64 a b in
let ba = H.mix64 b a in
let ab = H.finalize (H.mix_int64 (H.mix_int64 H.seed a) b) in
let ba = H.finalize (H.mix_int64 (H.mix_int64 H.seed b) a) in
not (Int64.equal ab ba)
;;
@ -125,7 +135,7 @@ t @@ fun () ->
for len = 0 to 99 do
for _ = 1 to 1000 do
let s = String.make len 'x' in
let h = H.hash_string s in
let h = H.mix_string H.seed s |> H.finalize in
if len > 0 then
if Int64.equal h 0L then
failwith