Debian Codegraph @

test/record_field_control.ml

raw symbols SCIP
1 open Core
2 open Ppx_hash_lib.Std
3 open Hash.Builtin
4
5 (* Tests for record-field attributes [@hash.ignore] *)
6
7 let check_hash_differently ~hash ~sexp_of_t a b =
8 if hash a = hash b
9 then failwithf !"fail: expect values to hash differently: %{sexp:t} -- %{sexp:t}" a b ()
10 ;;
11
12 let check_hash_same ~hash ~sexp_of_t a b =
13 if not (hash a = hash b)
14 then failwithf !"fail: expect values to hash the same: %{sexp:t} -- %{sexp:t}" a b ()
15 ;;
16
17 module No_string_field = struct
18 type t = { i : int } [@@deriving hash, sexp_of]
19
20 let v1 = { i = 42 }
21 end
22
23 module Immutable = struct
24 type t =
25 { s : string
26 ; i : int
27 }
28 [@@deriving hash, sexp_of]
29
30 let v1 = { s = "hey"; i = 42 }
31 let v2 = { s = "ho"; i = 42 }
32 let%test_unit _ = check_hash_differently ~hash ~sexp_of_t v1 v2
33 end
34
35 module Immutable_hash_dot_ignore = struct
36 type t =
37 { s : string [@hash.ignore]
38 ; i : int
39 }
40 [@@deriving hash, sexp_of]
41
42 let v1 = { s = "hey"; i = 42 }
43 let v2 = { s = "ho"; i = 42 }
44 let%test_unit _ = check_hash_same ~hash ~sexp_of_t v1 v2
45 let%test_unit _ = [%test_eq: int] (hash v1) No_string_field.(hash v1)
46 end
47
48 module Mutable_hash_dot_ignore = struct
49 type t =
50 { mutable s : string [@hash.ignore]
51 ; i : int
52 }
53 [@@deriving hash, sexp_of]
54
55 let v1 = { s = "hey"; i = 42 }
56 let v2 = { s = "ho"; i = 42 }
57 let%test_unit _ = check_hash_same ~hash ~sexp_of_t v1 v2
58 let%test_unit _ = [%test_eq: int] (hash v1) No_string_field.(hash v1)
59 end
60
61 module Immutable_compare_ignore = struct
62 type t =
63 { s : string [@compare.ignore]
64 ; i : int
65 }
66 [@@deriving hash, sexp_of]
67
68 let v1 = { s = "hey"; i = 42 }
69 let v2 = { s = "ho"; i = 42 }
70 let%test_unit _ = check_hash_same ~hash ~sexp_of_t v1 v2
71 let%test_unit _ = [%test_eq: int] (hash v1) No_string_field.(hash v1)
72 end