/
test_bitset.ml
215 lines (187 loc) · 4.63 KB
/
test_bitset.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
open OUnit
module BS = BatBitSet
let test_of_array arr =
let t = BS.create (Array.length arr) in
let () =
(* Create the table *)
Array.iteri
(fun idx vl ->
try
if vl then
BS.set t idx
with e ->
assert_failure
(Printf.sprintf
"while setting bitset.(%d) got this exception: %s"
idx (Printexc.to_string e)))
arr
in
let () =
(* Check the table *)
Array.iteri
(fun idx vl ->
let res =
try
BS.mem t idx
with e ->
assert_failure
(Printf.sprintf
"while getting bitset.(%d) got this exception: %s"
idx (Printexc.to_string e))
in
assert_equal
~msg:(Printf.sprintf "at idx %d" idx)
~printer:string_of_bool
vl res)
arr
in
()
let assert_mem t lst =
List.iter
(fun (i, b) ->
assert_equal
~msg:(Printf.sprintf "at idx %d" i)
~printer:string_of_bool
b
(BS.mem t i))
lst
let lst1 = [1; 4; 25; 27]
let lst2 = [1; 5; 26; 250]
let biop op ?(rev=false) lst () =
let t1 = BS.of_list lst1 in
let t2 = BS.of_list lst2 in
let t1, t2 = if rev then t2, t1 else t1, t2 in
let tr = op t1 t2 in
assert_mem tr (List.map (fun i -> i, true) lst);
assert_equal
~msg:"number of element"
~printer:string_of_int
(List.length lst)
(BS.count tr)
module EInt =
struct
type t = int
let compare = ( - )
let pp_printer = Format.pp_print_int
let pp_print_sep = OUnitDiff.pp_comma_separator
end
module ListInt =
struct
include OUnitDiff.SetMake(EInt)
let assert_equal ?msg lst1 lst2 =
assert_equal ?msg (of_list lst1) (of_list lst2)
end
let tests = "BitSet" >::: [
"Check small array" >::
(fun () ->
test_of_array [|true; false; false; true; false; true;
true; false; false; false; true|]);
"Check intermediate array" >::
(fun () ->
test_of_array
(Array.init 100 (fun _ -> Random.bool ())));
"Check huge array" >::
(fun () ->
test_of_array
(Array.init 1000 (fun _ -> Random.bool ())));
"empty" >::
(fun () ->
let t = BS.empty () in
assert_mem t
[1, false;
2, false;
100, false]);
"create" >::
(fun () ->
let t = BS.create 10 in
assert_mem t
[1, false;
2, false;
9, false;
10, false;
100, false]);
"full" >::
(fun () ->
let t = BS.create_full 10 in
assert_mem t
[0, true;
1, true;
2, true;
9, true;
10, false;
100, false];
assert_equal
~msg:"count"
~printer:string_of_int
10
(BS.count t));
"copy" >::
(fun () ->
let t = BS.of_list lst1 in
let t' = BS.copy t in
assert_bool
"Copy should be equals"
(BS.equals t t'));
"union" >::
(biop BS.union [1; 4; 5; 25; 26; 27; 250]);
"union2" >::
(biop BS.union ~rev:true [1; 4; 5; 25; 26; 27; 250]);
"diff1" >::
(biop BS.diff [4; 25; 27]);
"diff2" >::
(biop BS.diff ~rev:true [5; 26; 250]);
"sym_diff" >::
(biop BS.sym_diff [4; 25; 27; 5; 26; 250]);
"sym_diff2" >::
(biop BS.sym_diff ~rev:true [4; 25; 27; 5; 26; 250]);
"inter" >::
(biop BS.inter [1]);
"next_set_bit" >::
(fun () ->
let bs = BS.of_list lst1 in
let string_of_int_opt =
function
| Some i -> string_of_int i
| None -> "<none>"
in
let last =
List.fold_left
(fun prv cur ->
assert_equal
~printer:string_of_int_opt
(Some cur)
(BS.next_set_bit bs (prv + 1));
cur)
(-1)
lst1
in
assert_equal
~printer:string_of_int_opt
None
(BS.next_set_bit bs (last + 1)));
"enum" >::
(fun () ->
let t1 = BS.of_list lst1 in
let t2 = BS.of_list lst2 in
ListInt.assert_equal
lst1
(BatList.of_enum (BS.enum t1));
ListInt.assert_equal
lst2
(BatList.of_enum (BS.enum t2)));
"toggle" >::
(fun () ->
let t = BS.empty () in
BS.toggle t 10;
assert_bool "idx 10 is set" (BS.mem t 10);
BS.toggle t 10;
assert_bool "idx 10 is not set" (not (BS.mem t 10)));
"compare" >::
(fun () ->
let t1 = BS.of_list lst1 in
let t2 = BS.of_list lst2 in
assert_bool "lst1 < lst2" (BS.compare t1 t2 < 0);
assert_bool "lst2 > lst1" (BS.compare t2 t1 > 0);
assert_bool "lst1 = lst1" (BS.compare t1 t1 = 0);
assert_bool "lst2 = lst2" (BS.compare t2 t2 = 0))
]