| 1 |
open Core |
| 2 |
|
| 3 |
module Tests (Hash : Base.Hash.S with type hash_value = int) = struct |
| 4 |
module Ppx_hash_lib = struct |
| 5 |
module Std = struct |
| 6 |
module Hash = Base.Hash.F (Hash) |
| 7 |
end |
| 8 |
end |
| 9 |
|
| 10 |
open Ppx_hash_lib.Std.Hash.Builtin |
| 11 |
|
| 12 |
let hash = `dont_use |
| 13 |
|
| 14 |
module State = struct |
| 15 |
module T = struct |
| 16 |
type t = Hash.state |
| 17 |
|
| 18 |
let compare = Hash.For_tests.compare_state |
| 19 |
let sexp_of_t s : Sexp.t = Atom (Hash.For_tests.state_to_string s) |
| 20 |
let t_of_sexp _ = assert false |
| 21 |
end |
| 22 |
|
| 23 |
include T |
| 24 |
include Comparable.Make (T) |
| 25 |
end |
| 26 |
|
| 27 |
let should_have_no_collisions list sexp_of_t hash = |
| 28 |
let m = |
| 29 |
State.Map.of_alist_multi |
| 30 |
(List.map list ~f:(fun v -> hash (Hash.reset (Hash.alloc ())) v, v)) |
| 31 |
in |
| 32 |
Map.iteri m ~f:(fun ~key:hash ~data:values -> |
| 33 |
match values with |
| 34 |
| [] -> assert false |
| 35 |
| [ _ ] -> () |
| 36 |
| _ :: _ :: _ -> |
| 37 |
failwiths ~here:[%here] "collision" (hash, values) [%sexp_of: State.t * t list]) |
| 38 |
;; |
| 39 |
|
| 40 |
module Ints = struct |
| 41 |
let should_have_no_collisions l s f = |
| 42 |
should_have_no_collisions l s (fun s x -> hash_fold_int s (f x)) |
| 43 |
;; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
let ints = [ 0; 1; 2; 3; 100; 500; -1; -2 ] |
| 48 |
let ( % ) a b = [%hash: int * int] (a, b) |
| 49 |
let zero = 0 |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
let%test_unit "simple combine2 collisions" = |
| 54 |
should_have_no_collisions |
| 55 |
(List.cartesian_product ints ints) |
| 56 |
[%sexp_of: int * int] |
| 57 |
(fun (a, b) -> a % b) |
| 58 |
;; |
| 59 |
|
| 60 |
let%test_unit "more complicated combine2 collisions" = |
| 61 |
should_have_no_collisions |
| 62 |
(List.cartesian_product ints ints) |
| 63 |
[%sexp_of: int * int] |
| 64 |
(fun (a, b) -> a % (b % zero)) |
| 65 |
;; |
| 66 |
|
| 67 |
let%test_unit "yet more complicated combine2s collisions" = |
| 68 |
should_have_no_collisions |
| 69 |
(List.cartesian_product ints ints) |
| 70 |
[%sexp_of: int * int] |
| 71 |
(fun (a, b) -> zero % a % (b % zero)) |
| 72 |
;; |
| 73 |
end |
| 74 |
|
| 75 |
let hash_int x = |
| 76 |
let h = Hash.alloc () in |
| 77 |
let h = Hash.reset h in |
| 78 |
Hash.fold_int h x |
| 79 |
;; |
| 80 |
|
| 81 |
let init () = |
| 82 |
let h = Hash.alloc () in |
| 83 |
Hash.reset h |
| 84 |
;; |
| 85 |
|
| 86 |
let hash_string x = |
| 87 |
let h = Hash.alloc () in |
| 88 |
let h = Hash.reset h in |
| 89 |
Hash.fold_string h x |
| 90 |
;; |
| 91 |
|
| 92 |
let ( = ) x y = Hash.For_tests.compare_state x y = 0 |
| 93 |
let assert_different hash_t a b = assert (not (hash_t a = hash_t b)) |
| 94 |
let%test_unit _ = assert_different hash_int 0 (1 lsl 32) |
| 95 |
|
| 96 |
let%test_unit _ = |
| 97 |
let a1 = String.make 7 'a' in |
| 98 |
let b = String.make 7 'b' in |
| 99 |
let a2 = String.make 7 'a' in |
| 100 |
let c = Some 5 in |
| 101 |
let a3 = String.make 7 'a' in |
| 102 |
let d = Obj.new_block Obj.abstract_tag 1 in |
| 103 |
assert (hash_string a1 = hash_string a2); |
| 104 |
assert (hash_string a1 = hash_string a3); |
| 105 |
assert (not (phys_same a1 b)); |
| 106 |
assert (not (phys_same a1 c)); |
| 107 |
assert (not (phys_same a1 d)) |
| 108 |
;; |
| 109 |
|
| 110 |
let%test_unit _ = assert_different hash_string "\200\200\200\200" "\200a\200\200" |
| 111 |
let%test_unit _ = assert_different hash_string "\200\200\200" "\200a\200" |
| 112 |
|
| 113 |
let%test_unit "int collisions" = |
| 114 |
Map.to_alist |
| 115 |
(Int.Map.of_alist_multi |
| 116 |
(List.init 100_000 ~f:(fun i -> |
| 117 |
Hash.get_hash_value (hash_int i) land ((1 lsl 17) - 1), i))) |
| 118 |
|> List.iter ~f:(fun (_, vs) -> |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
[%test_pred: int] (fun x -> x <= 10) (List.length vs)) |
| 128 |
;; |
| 129 |
|
| 130 |
let%test_unit "list collisions" = |
| 131 |
should_have_no_collisions |
| 132 |
[ []; [ [] ]; [ [ [] ] ]; [ [ [ "hello" ] ] ]; [ []; [] ] ] |
| 133 |
[%sexp_of: string list list list] |
| 134 |
[%hash_fold: string list list list] |
| 135 |
;; |
| 136 |
|
| 137 |
type 'a array_frozen = 'a array |
| 138 |
|
| 139 |
let sexp_of_array_frozen = sexp_of_array |
| 140 |
|
| 141 |
let%test_unit "array collisions" = |
| 142 |
should_have_no_collisions |
| 143 |
[ [||]; [| [||] |]; [| [| [||] |] |]; [| [| [| "hello" |] |] |]; [| [||]; [||] |] ] |
| 144 |
[%sexp_of: string array_frozen array_frozen array_frozen] |
| 145 |
[%hash_fold: string array_frozen array_frozen array_frozen] |
| 146 |
;; |
| 147 |
|
| 148 |
let%test_unit "string collisions" = |
| 149 |
should_have_no_collisions |
| 150 |
[ "", [ 16 lsl 56; 0 ]; String.make 8 '\000' ^ "\002" ^ String.make 7 '\000', [] ] |
| 151 |
[%sexp_of: string * int list] |
| 152 |
[%hash_fold: string * int list] |
| 153 |
;; |
| 154 |
end |
| 155 |
|
| 156 |
module I = Tests (Base.Hash) |
| 157 |
module S = Tests (Siphash_lib.Siphash) |
| 158 |
module P = Tests (Perfect_hash) |