Skip to content

Commit 55b59b5

Browse files
committed
move more tests into testlib
1 parent 3b2cd78 commit 55b59b5

File tree

4 files changed

+107
-122
lines changed

4 files changed

+107
-122
lines changed

src/core/tests/dune

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,33 @@
1-
2-
3-
(executable
4-
(name check_labelled_mods)
5-
(modules check_labelled_mods)
6-
(flags :standard -warn-error -a -w -3-33-35-27-39-50 -nolabels)
7-
(libraries containers))
8-
91
(executable
10-
(name test_hash)
11-
(modules test_hash)
12-
(flags :standard -warn-error -a+8)
13-
(libraries containers iter))
2+
(name check_labelled_mods)
3+
(modules check_labelled_mods)
4+
(flags :standard -warn-error -a -w -3-33-35-27-39-50 -nolabels)
5+
(libraries containers))
146

157
(executable
16-
(name test_random)
17-
(flags :standard -warn-error -a+8)
18-
(modules test_random)
19-
(libraries containers))
8+
(name test_hash)
9+
(modules test_hash)
10+
(flags :standard -warn-error -a+8)
11+
(libraries containers iter))
2012

2113
(executable
22-
(name test_csexp)
23-
(flags :standard -warn-error -a+8)
24-
(modules test_csexp)
25-
(libraries containers csexp qcheck-core qcheck))
14+
(name test_random)
15+
(flags :standard -warn-error -a+8)
16+
(modules test_random)
17+
(libraries containers))
2618

2719
(rule
28-
(alias runtest)
29-
(locks /ctest)
30-
(package containers)
31-
(action (run ./test_random.exe)))
32-
33-
(rule
34-
(alias runtest)
35-
(locks /ctest)
36-
(package containers)
37-
(action (run ./test_csexp.exe)))
20+
(alias runtest)
21+
(locks /ctest)
22+
(package containers)
23+
(action
24+
(run ./test_random.exe)))
3825

3926
; what matters is that it compiles
27+
4028
(rule
41-
(alias runtest)
42-
(locks /ctest)
43-
(package containers)
44-
(action (run ./check_labelled_mods.exe)))
29+
(alias runtest)
30+
(locks /ctest)
31+
(package containers)
32+
(action
33+
(run ./check_labelled_mods.exe)))

src/core/tests/test_csexp.ml

Lines changed: 0 additions & 87 deletions
This file was deleted.

tests/core/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
(flags :standard -strict-sequence -warn-error -a+8 -open CCShims_)
44
(modes native)
55
(libraries containers containers.bencode containers.unix threads
6-
containers_testlib iter gen uutf))
6+
containers_testlib iter gen uutf csexp))

tests/core/t_sexp.ml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,86 @@ t @@ fun () ->
7878
print_endline @@ Printexc.to_string e ^ "\n" ^ st;
7979
assert false);
8080
true;;
81+
82+
module CS = CCCanonical_sexp
83+
84+
85+
module Csexp_arg = struct
86+
open Csexp
87+
type t = Csexp.t
88+
let atom s = Atom s
89+
let list l = List l
90+
let match_ s ~atom ~list = match s with
91+
| Atom s -> atom s
92+
| List l -> list l
93+
type loc = unit
94+
let make_loc = None
95+
let atom_with_loc ~loc:() = atom
96+
let list_with_loc ~loc:() = list
97+
end
98+
99+
module CS0 = CS.Make(Csexp_arg)
100+
module Sexp0 = CCSexp.Make(Csexp_arg)
101+
102+
let gen_csexp (str:string Q.Gen.t) : CS0.t Q.Gen.t =
103+
let open Q.Gen in
104+
let open Csexp in
105+
begin fix @@ fun self depth ->
106+
let mklist n =
107+
list_size (0 -- n) (self (depth+1)) >|= fun l -> List l
108+
in
109+
frequency @@ List.flatten [
110+
[(3, str
111+
>|= fun s -> Atom s)];
112+
(match depth with
113+
| 0 -> [4,mklist 25]
114+
| 1 -> [2,mklist 7]
115+
| 2 -> [1,mklist 2]
116+
| _ -> []);
117+
]
118+
end 0
119+
120+
let rec shrink_csexp (s:Csexp.t) : Csexp.t Q.Iter.t =
121+
let open Csexp in
122+
let open Q.Iter in
123+
match s with
124+
| Atom s -> Q.Shrink.string s >|= fun s -> Atom s
125+
| List l -> Q.Shrink.list ~shrink:shrink_csexp l >|= fun l -> List l
126+
127+
let arb_csexp_pp =
128+
let genstr = Q.Gen.(string_size ~gen:Q.Gen.printable (0--15)) in
129+
Q.make ~print:Sexp0.to_string
130+
~shrink:shrink_csexp (gen_csexp genstr)
131+
132+
let arb_csexp_arb =
133+
(* binary-ready *)
134+
let genchar = Q.Gen.(0 -- 255 >|=Char.chr) in
135+
let genstr = Q.Gen.(string_size ~gen:genchar (0--15)) in
136+
Q.make
137+
~print:Sexp0.to_string
138+
~shrink:shrink_csexp (gen_csexp genstr)
139+
140+
module Make(X : sig val arb : Csexp.t Q.arbitrary end)() = struct
141+
open X;;
142+
143+
q ~count:2_000 arb @@ fun sexp ->
144+
let s = CS0.to_string sexp in
145+
match Csexp.parse_string s with
146+
| Ok sexp' -> sexp = sexp'
147+
| Error (_,msg) -> Q.Test.fail_report msg;;
148+
149+
q ~count:2_000 arb @@ fun sexp ->
150+
let s = Csexp.to_string sexp in
151+
match CS0.parse_string s with
152+
| Ok sexp' -> sexp = sexp'
153+
| Error msg -> Q.Test.fail_report msg;;
154+
155+
let init () = ()
156+
end
157+
158+
let () =
159+
let module M1 = Make(struct let arb=arb_csexp_pp end)() in
160+
let module M2 = Make(struct let arb=arb_csexp_arb end)() in
161+
M1.init();
162+
M2.init();
163+
()

0 commit comments

Comments
 (0)