-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtest_zip.ml
71 lines (64 loc) · 1.51 KB
/
test_zip.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
open Zip
open QCheck
let g =
Gen.(
of_l
<$> sized
@@ fix (fun self n ->
match n with
| 0 -> map leaf nat
| n ->
frequency
[ ( 5
, branch
<$> ( 0 -- (n / 2) >>= fun n' ->
list_size (0 -- 10) (self n') ) )
; (1, map leaf nat)
]))
let arbitrary = make g
(* sum zipper using next *)
let sum_by_next z =
let rec aux z r =
match (node z, is_end z) with
| Leaf n, false -> aux (next z) (r + n)
| Leaf _, true -> r
| Branch _, false -> aux (next z) r
| Branch _, true -> r
in
aux z 0
(* sum zipper using prev *)
let sum_by_prev z =
let rec to_end z =
let z' = next z in
if is_end z' then
z
else
to_end z'
in
let rec aux z r =
let r' =
match node z with
| Leaf n -> r + n
| Branch _ -> r
in
match prev z with
| Some z' -> aux z' r'
| None -> r'
in
aux (to_end z) 0
let sum z =
let value = root z in
let rec aux value =
match value with
| Leaf n -> n
| Branch l -> List.fold_left (fun r e -> r + aux e) 0 l
in
aux value
let next_prev_sum_test =
Test.make ~name:"next_prev_sum_generative_test" ~count:1000 arbitrary
(fun z ->
let sum' = sum z in
sum_by_next z = sum' && sum_by_prev z = sum')
let () =
Alcotest.run "zip"
@@ [ ("next & prev", [ QCheck_alcotest.to_alcotest next_prev_sum_test ]) ]