forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_test.ml
68 lines (56 loc) · 1.66 KB
/
array_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
open Mt
module type ARRAY = module type of Array
module Make (Array : ARRAY) = struct
let is_sorted x =
let len = Array.length x in
let rec aux i =
if i >= len - 1 then true
else
if x.(i) < x.(i+1) then
aux (i + 1)
else
false in
aux 0
let array_suites = Mt.[
"init", (fun _ ->
Eq (Array.init 5 (fun x -> x ), [|0;1;2;3;4|]));
"toList",(fun _ ->
let aux (xs : (int array * int list ) list) =
List.fold_left (fun acc (x, y ) -> (Array.to_list x, y)::acc )
[] xs
in
let a,b = List.split @@ aux [ [||], [] ] in
Eq (a,b)
);
"concat", (fun _ ->
Eq([|0;1;2;3;4;5|] ,
Array.concat [[|0;1;2|]; [|3;4|]; [||]; [|5|]]
));
"make", (fun _ ->
Eq ((Array.make 100 'a'),
(Array.init 100 (fun _ -> 'a'))
));
"sub", (fun _ ->
Eq ((Array.sub [|0;1;2;3;4|] 2 2), [|2;3|]));
"blit", (fun _ ->
let u = [|100;0;0|] in
let v = Array.init 3 (fun x -> x * 2 ) in
let () = Array.blit v 1 u 1 2 in
Eq (([|0;2;4|], [|100;2;4|]) , (v,u)));
"make", (fun _ ->
Eq (Array.make 2 1, [|1;1|]));
"sort", (fun _ ->
let u = [|3;0;1|] in
Array.sort (fun (x:int) y -> Pervasives.compare x y) u;
Eq([|0;1;3|]= u, true) (* seems [assert.deepEqual] does not do the right job..*)
);
"sort_large", (fun _ ->
(** random test is hard to reproduce *)
let v = Array.init 4 (fun i -> i mod 17) in
Array.sort (fun (x:int) y -> compare x y) v ;
Eq(true, is_sorted v ))
]
end
;; from_pair_suites __FILE__
(let module Array_test = Make(Array)in
Array_test.array_suites)