File: import.ml

package info (click to toggle)
janest-base 0.17.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,632 kB
  • sloc: ml: 48,653; ansic: 281; javascript: 126; makefile: 14
file content (46 lines) | stat: -rw-r--r-- 1,152 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
include Base
include Stdio
include Base_for_tests
include Base_test_helpers
include Base_quickcheck.Export
include Expect_test_helpers_base

let () = Int_conversions.sexp_of_int_style := `Underscores
let is_none = Option.is_none
let is_some = Option.is_some
let ok_exn = Or_error.ok_exn
let stage = Staged.stage
let unstage = Staged.unstage

module type Hash = sig
  type t [@@deriving hash, sexp_of]
end

let check_hash_coherence (type t) here (module T : Hash with type t = t) ts =
  List.iter ts ~f:(fun t ->
    let hash1 = T.hash t in
    let hash2 = [%hash: T.t] t in
    require
      here
      (hash1 = hash2)
      ~cr:CR_soon
      ~if_false_then_print_s:
        (lazy [%message "" ~value:(t : T.t) (hash1 : int) (hash2 : int)]))
;;

module type Int_hash = sig
  include Hash

  val of_int_exn : int -> t
  val min_value : t
  val max_value : t
end

let check_int_hash_coherence (type t) here (module I : Int_hash with type t = t) =
  check_hash_coherence
    here
    (module I)
    [ I.min_value; I.of_int_exn 0; I.of_int_exn 37; I.max_value ]
;;

let test_conversion ~to_string f x = printf "%s --> %s\n" (to_string x) (to_string (f x))