forked from janestreet/base_quickcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_test.ml
152 lines (141 loc) · 4.58 KB
/
test_test.ml
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
open! Import
module type S = Test.S
module Config = Test.Config
let default_config = Test.default_config
let%expect_test ("default_config" [@tags "64-bits-only"]) =
Ref.set_temporarily sexp_style To_string_hum ~f:(fun () ->
print_s [%sexp (default_config : Config.t)]);
[%expect
{|
((seed (Deterministic "an arbitrary but deterministic string"))
(test_count 10000) (shrink_count 10000)
(sizes
(0 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 0 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 0 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 0 1 2 3 4 5 6 ...)))
|}]
;;
let%expect_test ("default_config" [@tags "32-bits-only"]) =
Ref.set_temporarily sexp_style To_string_hum ~f:(fun () ->
print_s [%sexp (default_config : Config.t)]);
[%expect
{|
((seed (Deterministic "an arbitrary but deterministic string"))
(test_count 1000) (shrink_count 10000)
(sizes
(0 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 0 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 0 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 0 1 2 3 4 5 6 ...)))
|}]
;;
let run = Test.run
let result = Test.result
let run_exn = Test.run_exn
let%expect_test "run_exn" =
let module M = struct
type t = bool option list [@@deriving sexp_of]
let quickcheck_generator = Generator.list (Generator.option Generator.bool)
let quickcheck_shrinker = Shrinker.list (Shrinker.option Shrinker.bool)
end
in
let module M_without_shrinker = struct
include M
let quickcheck_shrinker = Shrinker.atomic
end
in
(* success *)
let count = ref 0 in
require_does_not_raise (fun () -> Test.run_exn (module M) ~f:(fun _ -> Int.incr count));
require
(!count = Test.default_config.test_count)
~if_false_then_print_s:(lazy [%message (!count : int)]);
[%expect {| |}];
(* failure *)
let failure list = assert (List.is_sorted list ~compare:[%compare: bool option]) in
(* large sizes to demonstrate shrinking *)
let config =
{ Test.default_config with sizes = Sequence.cycle_list_exn [ 10; 20; 30 ] }
in
(* simple failure *)
require_does_raise ~hide_positions:true (fun () ->
Test.run_exn ~config ~f:failure (module M));
[%expect
{|
("Base_quickcheck.Test.run: test failed"
(input ((false) ()))
(error "Assert_failure test_test.ml:LINE:COL"))
|}];
(* failure without shrinking *)
require_does_raise ~hide_positions:true (fun () ->
Test.run_exn ~config ~f:failure (module M_without_shrinker));
[%expect
{|
("Base_quickcheck.Test.run: test failed"
(input (() () (false) (true) (false) ()))
(error "Assert_failure test_test.ml:LINE:COL"))
|}];
(* failure from examples *)
require_does_raise ~hide_positions:true (fun () ->
Test.run_exn
~config
~f:failure
~examples:[ [ Some true; Some true; None; Some true; Some true ] ]
(module M));
[%expect
{|
("Base_quickcheck.Test.run: test failed"
(input ((true) ()))
(error "Assert_failure test_test.ml:LINE:COL"))
|}];
(* failure from examples without shrinking *)
require_does_raise ~hide_positions:true (fun () ->
Test.run_exn
~config
~f:failure
(module M_without_shrinker)
~examples:[ [ Some true; Some true; None; Some true; Some true ] ]);
[%expect
{|
("Base_quickcheck.Test.run: test failed"
(input ((true) (true) () (true) (true)))
(error "Assert_failure test_test.ml:LINE:COL"))
|}]
;;
let with_sample = Test.with_sample
let with_sample_exn = Test.with_sample_exn
let%expect_test "with_sample_exn" =
let generator = Generator.list (Generator.option Generator.bool) in
with_sample_exn
generator
~config:{ Test.default_config with test_count = 20 }
~f:(fun sample ->
Sequence.iter sample ~f:(fun value ->
Core.print_s [%sexp (value : bool option list)]));
[%expect
{|
()
()
(() (true))
(() (true) (false))
()
((true))
((true))
((true))
()
(() (false) (true))
(() () (false) (true) () (true) () ())
((true) () () () (true) (false) ())
(() () (true) () () (false) (false))
(() () (true) (true) (false) () (true) ())
()
()
((true) (true) () (false) (false) () () () () (true) () () (true) (false)
(false) ())
((true) (false) (true))
((false) () () (false) () () () (false) (false) () (true) () () () ()
(false))
()
|}]
;;