-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy patht_array.ml
314 lines (267 loc) · 7.32 KB
/
t_array.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
open CCArray
module T = (val Containers_testlib.make ~__FILE__ ())
include T;;
t @@ fun () ->
let st = Random.State.make [||] in
let a = 0 -- 10000 in
let b = Array.copy a in
shuffle_with st a;
a <> b
;;
eq (Some 1) (get_safe [| 1; 2; 3 |] 0);;
eq (Some 2) (get_safe [| 1; 2; 3 |] 1);;
eq (Some 3) (get_safe [| 1; 2; 3 |] 2);;
eq None (get_safe [| 1; 2; 3 |] 4);;
eq None (get_safe [| 1; 2; 3 |] max_int);;
eq None (get_safe [| 1; 2; 3 |] ~-1);;
eq None (get_safe [| 1; 2; 3 |] ~-42);;
q
Q.(array int)
(fun a ->
let b = map (( + ) 1) a in
map_inplace (( + ) 1) a;
b = a)
;;
t @@ fun () ->
fold_while
(fun acc b ->
if b then
acc + 1, `Continue
else
acc, `Stop)
0
(Array.of_list [ true; true; false; true ])
= 2
;;
eq
(6, [| "1"; "2"; "3" |])
(fold_map (fun acc x -> acc + x, string_of_int x) 0 [| 1; 2; 3 |])
;;
q
Q.(array int)
(fun a ->
fold_map (fun acc x -> x :: acc, x) [] a = (List.rev @@ Array.to_list a, a))
;;
eq
~printer:Q.Print.(array int)
[| 0; 1; 3; 6 |]
(scan_left ( + ) 0 [| 1; 2; 3 |])
;;
eq ~printer:Q.Print.(array int) [| 0 |] (scan_left ( + ) 0 [||]);;
t @@ fun () ->
reverse_in_place [||];
true
;;
t @@ fun () ->
reverse_in_place [| 1 |];
true
;;
t @@ fun () ->
let a = [| 1; 2; 3; 4; 5 |] in
reverse_in_place a;
a = [| 5; 4; 3; 2; 1 |]
;;
t @@ fun () ->
let a = [| 1; 2; 3; 4; 5; 6 |] in
reverse_in_place a;
a = [| 6; 5; 4; 3; 2; 1 |]
;;
eq ~cmp:( = ) ~printer:Q.Print.(array int) [||] (sorted Stdlib.compare [||]);;
eq ~cmp:( = )
~printer:Q.Print.(array int)
[| 0; 1; 2; 3; 4 |]
(sorted Stdlib.compare [| 3; 2; 1; 4; 0 |])
;;
q
Q.(array int)
(fun a ->
let b = Array.copy a in
Array.sort Stdlib.compare b;
b = sorted Stdlib.compare a)
;;
eq ~cmp:( = )
~printer:Q.Print.(array int)
[||]
(sort_indices Stdlib.compare [||])
;;
eq ~cmp:( = )
~printer:Q.Print.(array int)
[| 4; 2; 1; 0; 3 |]
(sort_indices Stdlib.compare [| "d"; "c"; "b"; "e"; "a" |])
;;
q
Q.(array_of_size Gen.(0 -- 30) printable_string)
(fun a ->
let b = sort_indices String.compare a in
sorted String.compare a = Array.map (Array.get a) b)
;;
eq ~cmp:( = )
~printer:Q.Print.(array int)
[||]
(sort_ranking Stdlib.compare [||])
;;
eq ~cmp:( = )
~printer:Q.Print.(array int)
[| 3; 2; 1; 4; 0 |]
(sort_ranking Stdlib.compare [| "d"; "c"; "b"; "e"; "a" |])
;;
q
Q.(array_of_size Gen.(0 -- 50) printable_string)
(fun a ->
let b = sort_ranking String.compare a in
let a_sorted = sorted String.compare a in
a = Array.map (Array.get a_sorted) b)
;;
q Q.(array small_int) (fun a -> rev (rev a) = a);;
t @@ fun () -> rev [| 1; 2; 3 |] = [| 3; 2; 1 |];;
t @@ fun () -> rev [| 1; 2 |] = [| 2; 1 |];;
t @@ fun () -> rev [||] = [||];;
q Q.(array small_int) (fun a -> mem 1 a = Array.mem 1 a);;
eq (Some 3) (max Stdlib.compare [| 1; 2; 3 |]);;
eq (Some 4) (max Stdlib.compare [| 4; -1; 2; 3 |]);;
eq None (max Stdlib.compare [||]);;
eq (Some 2) (argmax Stdlib.compare [| 1; 2; 3 |]);;
eq (Some 0) (argmax Stdlib.compare [| 4; -1; 2; 3 |]);;
eq None (argmax Stdlib.compare [||]);;
eq (Some 1) (min Stdlib.compare [| 1; 2; 3 |]);;
eq (Some ~-1) (min Stdlib.compare [| 4; -1; 2; 3 |]);;
eq None (min Stdlib.compare [||]);;
eq (Some 0) (argmin Stdlib.compare [| 1; 2; 3 |]);;
eq (Some 1) (argmin Stdlib.compare [| 4; -1; 2; 3 |]);;
eq None (argmin Stdlib.compare [||]);;
t @@ fun () ->
filter_map
(fun x ->
if x mod 2 = 0 then
Some (string_of_int x)
else
None)
[| 1; 2; 3; 4 |]
= [| "2"; "4" |]
;;
t @@ fun () ->
filter_map
(fun x ->
if x mod 2 = 0 then
Some (string_of_int x)
else
None)
[| 1; 2; 3; 4; 5; 6 |]
= [| "2"; "4"; "6" |]
;;
t @@ fun () ->
let a = [| 1; 3; 5 |] in
let a' = flat_map (fun x -> [| x; x + 1 |]) a in
a' = [| 1; 2; 3; 4; 5; 6 |]
;;
eq ~cmp:( = )
~printer:Q.Print.(array int)
[| 11; 12; 21; 22 |]
(sorted CCInt.compare @@ monoid_product ( + ) [| 10; 20 |] [| 1; 2 |])
;;
eq ~cmp:( = )
~printer:Q.Print.(array int)
[| 11; 12; 13; 14 |]
(sorted CCInt.compare @@ monoid_product ( + ) [| 10 |] [| 1; 2; 3; 4 |])
;;
t @@ fun () -> lookup ~cmp:CCInt.compare 2 [| 0; 1; 2; 3; 4; 5 |] = Some 2;;
t @@ fun () -> lookup ~cmp:CCInt.compare 4 [| 0; 1; 2; 3; 4; 5 |] = Some 4;;
t @@ fun () -> lookup ~cmp:CCInt.compare 0 [| 1; 2; 3; 4; 5 |] = None;;
t @@ fun () -> lookup ~cmp:CCInt.compare 6 [| 1; 2; 3; 4; 5 |] = None;;
t @@ fun () -> lookup ~cmp:CCInt.compare 3 [||] = None;;
t @@ fun () -> lookup ~cmp:CCInt.compare 1 [| 1 |] = Some 0;;
t @@ fun () -> lookup ~cmp:CCInt.compare 2 [| 1 |] = None;;
t @@ fun () -> bsearch ~cmp:CCInt.compare 3 [| 1; 2; 2; 3; 4; 10 |] = `At 3;;
t @@ fun () ->
bsearch ~cmp:CCInt.compare 5 [| 1; 2; 2; 3; 4; 10 |] = `Just_after 4
;;
t @@ fun () -> bsearch ~cmp:CCInt.compare 1 [| 1; 2; 5; 5; 11; 12 |] = `At 0;;
t @@ fun () -> bsearch ~cmp:CCInt.compare 12 [| 1; 2; 5; 5; 11; 12 |] = `At 5;;
t @@ fun () -> bsearch ~cmp:CCInt.compare 10 [| 1; 2; 2; 3; 4; 9 |] = `All_lower
;;
t @@ fun () -> bsearch ~cmp:CCInt.compare 0 [| 1; 2; 2; 3; 4; 9 |] = `All_bigger
;;
t @@ fun () -> bsearch ~cmp:CCInt.compare 3 [||] = `Empty;;
t @@ fun () -> 1 -- 4 |> Array.to_list = [ 1; 2; 3; 4 ];;
t @@ fun () -> 4 -- 1 |> Array.to_list = [ 4; 3; 2; 1 ];;
t @@ fun () -> 0 -- 0 |> Array.to_list = [ 0 ];;
q
Q.(pair small_int small_int)
(fun (a, b) -> a -- b |> Array.to_list = CCList.(a -- b))
;;
q
Q.(pair small_int small_int)
(fun (a, b) -> a --^ b |> Array.to_list = CCList.(a --^ b))
;;
q
Q.(pair (array small_int) (array small_int))
(fun (a, b) -> equal ( = ) a b = equal ( = ) b a)
;;
t @@ fun () -> equal ( = ) [| 1 |] [| 1 |];;
t @@ fun () -> compare CCOrd.poly [| 1; 2; 3 |] [| 1; 2; 3 |] = 0;;
t @@ fun () -> compare CCOrd.poly [| 1; 2; 3 |] [| 2; 2; 3 |] < 0;;
t @@ fun () -> compare CCOrd.poly [| 1; 2 |] [| 1; 2; 3 |] < 0;;
t @@ fun () -> compare CCOrd.poly [| 1; 2; 3 |] [| 1; 2 |] > 0;;
t @@ fun () ->
let a = [| 1; 2; 3 |] in
swap a 0 1;
a = [| 2; 1; 3 |]
;;
t @@ fun () ->
let a = [| 1; 2; 3 |] in
swap a 0 2;
a = [| 3; 2; 1 |]
;;
q
Q.(array_of_size Gen.(0 -- 100) small_int)
(fun a ->
let b = Array.copy a in
for i = 0 to Array.length a - 1 do
for j = i + 1 to Array.length a - 1 do
swap a i j
done
done;
for i = 0 to Array.length a - 1 do
for j = i + 1 to Array.length a - 1 do
swap a i j
done
done;
a = b)
;;
eq
~printer:(fun s -> s)
(to_string string_of_int [| 1; 2; 3; 4; 5; 6 |])
"1, 2, 3, 4, 5, 6"
;;
eq ~printer:(fun s -> s) (to_string string_of_int [||]) "";;
eq
~printer:(fun s -> s)
(to_string ~sep:" " string_of_int [| 1; 2; 3; 4; 5; 6 |])
"1 2 3 4 5 6"
;;
eq ~printer:(fun s -> s) (to_string string_of_int [| 1 |]) "1";;
eq [] (to_seq [||] |> CCList.of_seq);;
eq [ 1; 2; 3 ] (to_seq [| 1; 2; 3 |] |> CCList.of_seq);;
eq CCList.(1 -- 1000) (to_seq (1 -- 1000) |> CCList.of_seq)
module IA = struct
let get = Array.get
let set = Array.set
let length = Array.length
type elt = int
type t = int array
end
let gen_arr = Q.Gen.(array_size (1 -- 100) small_int)
let arr_arbitrary =
Q.make
~print:Q.Print.(array int)
~small:Array.length
~shrink:Q.Shrink.(array ?shrink:None)
gen_arr
;;
q ~count:300 arr_arbitrary (fun a ->
let a1 = Array.copy a and a2 = Array.copy a in
Array.sort CCInt.compare a1;
sort_generic (module IA) ~cmp:CCInt.compare a2;
a1 = a2)
;;
q Q.(array int) (fun a -> of_iter (to_iter a) = a)