File: random_state.ml

package info (click to toggle)
ppx-inline-test 0.17.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 364 kB
  • sloc: ml: 1,367; makefile: 15; javascript: 3; ansic: 2; sh: 2
file content (52 lines) | stat: -rw-r--r-- 1,081 bytes parent folder | download | duplicates (2)
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
47
48
49
50
51
52
let test list =
  ListLabels.iter list ~f:(fun expect ->
    let actual = Random.int 1000 in
    if actual <> expect then failwith (Printf.sprintf "%d <> %d" actual expect))
;;

(* Random state is repeatable: *)
let in_fresh_inline_test = [ 35; 358; 338 ]
let%test_unit _ = test in_fresh_inline_test
let%test_unit _ = test in_fresh_inline_test
let%test_unit _ = test in_fresh_inline_test

(* Random state can be overridden: *)
let after_random_init_0 = [ 518; 504; 87 ]

let%test_unit _ =
  Random.init 0;
  test after_random_init_0
;;

let%test_unit _ =
  Random.init 0;
  test after_random_init_0
;;

let%test_unit _ =
  Random.init 0;
  test after_random_init_0
;;

(* Tests inside a functor restore the existing random state after they run: *)
module Make () = struct
  let%test_unit _ = ()
end

let%test_unit _ =
  Random.init 0;
  let module _ = Make () in
  test after_random_init_0
;;

let%test_unit _ =
  Random.init 0;
  let module _ = Make () in
  test after_random_init_0
;;

let%test_unit _ =
  Random.init 0;
  let module _ = Make () in
  test after_random_init_0
;;