-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ml
91 lines (81 loc) · 2.52 KB
/
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
module I = struct type t = int let compare = Stdlib.compare end
module B = Bag.Make(I)
let print = B.print Format.pp_print_int
let () =
let a = B.add 1 ~mult:1 (B.add 2 ~mult:2 (B.add 3 ~mult:3 B.empty)) in
let b = B.add 1 ~mult:4 (B.add 2 ~mult:5 (B.add 3 ~mult:6 B.empty)) in
assert (B.cardinal a = 6);
assert (B.cardinal (B.sum a b) = 21);
assert (B.cardinal (B.union a b) = 15);
assert (B.is_empty (B.diff a b));
assert (B.cardinal (B.diff b a) = 9);
assert (B.equal (B.inter a b) a);
assert (B.included a b);
assert (not (B.included b a));
assert (not (B.disjoint b a));
assert (B.elements a = [1,1; 2,2; 3,3]);
assert (B.min_elt a = (1,1));
assert (B.max_elt a = (3,3));
let f _ = 1 in
assert (B.cardinal (B.map f a) = 3);
assert (B.cardinal (B.map f b) = 3);
let e = B.filter (fun x _ -> x mod 2 = 0) a in
assert (B.min_elt e = (2,2));
assert (B.max_elt e = (2,2));
assert (B.choose e = (2,2));
assert (B.cardinal e = 2);
let o =
B.filter (fun x _ -> x mod 2 = 1) b in
assert (B.min_elt o = (1,4));
assert (B.max_elt o = (3,6));
assert (B.cardinal o = 10);
()
let test n =
let b1 = ref B.empty in
let b2 = ref B.empty in
for x = 0 to n do
b1 := B.add x ~mult:2 !b1;
assert (B.cardinal !b1 = 2*(x+1));
b2 := B.add (n-x) !b2;
assert (B.cardinal !b2 = 2*x+1);
b2 := B.add (n-x) !b2;
if x < n/2 then assert (B.disjoint !b1 !b2)
done;
assert (B.mem n !b1);
assert (B.occ n !b1 = 2);
assert (B.cardinal !b1 = 2*(n+1));
assert (B.cardinal !b2 = 2*(n+1));
assert (B.equal !b1 !b2);
assert (B.for_all (fun x _ -> x <= n) !b1);
assert (not (B.for_all (fun x _ -> x < n) !b1));
assert (B.exists (fun x m -> x = 0 && m = 2) !b2);
for x = 0 to n do
b1 := B.remove_all x !b1;
b2 := B.remove (n-x) ~mult:2 !b2;
done;
assert (B.is_empty !b1);
assert (B.is_empty !b2);
()
let () =
for n = 0 to 10 do test (10 * n) done
(* division *)
let () = Random.init 42
let test n =
let b1 = ref B.empty in
let b2 = ref B.empty in
for i = 0 to n-1 do
b1 := B.add ~mult:(Random.int 10) i !b1;
b2 := B.add ~mult:(Random.int 3) i !b2
done;
let q, r = B.div !b1 !b2 in
(* Format.printf "b1 = %a / b2 = %a@." print !b1 print !b2;
* Format.printf "q = %d / r = %a@." q print r; *)
assert (B.equal !b1 (B.sum (B.mul !b2 q) r));
let q, r = B.divi !b1 1 in
assert (B.equal q !b1 && B.is_empty r);
for k = 2 to 5 do
let q, r = B.divi !b1 k in
assert (B.equal !b1 (B.sum (B.mul q k) r))
done
let () =
for n = 0 to 10 do test n done