forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathounit_vec_test.ml
153 lines (148 loc) · 4.96 KB
/
ounit_vec_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
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
let ((>::),
(>:::)) = OUnit.((>::),(>:::))
(* open Ext_json *)
let v = Vec_int.init 10 (fun i -> i);;
let (=~) x y = OUnit.assert_equal ~cmp:(Vec_int.equal (fun (x: int) y -> x=y)) x y
let (=~~) x y
=
OUnit.assert_equal ~cmp:(Vec_int.equal (fun (x: int) y -> x=y))
x (Vec_int.of_array y)
let suites =
__FILE__
>:::
[
(* idea
[%loc "inplace filter" ] --> __LOC__ ^ "inplace filter"
or "inplace filter" [@bs.loc]
*)
"inplace_filter " ^ __LOC__ >:: begin fun _ ->
v =~~ [|0; 1; 2; 3; 4; 5; 6; 7; 8; 9|];
ignore @@ Vec_int.push v 32;
let capacity = Vec_int.capacity v in
v =~~ [|0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 32|];
Vec_int.inplace_filter (fun x -> x mod 2 = 0) v ;
v =~~ [|0; 2; 4; 6; 8; 32|];
Vec_int.inplace_filter (fun x -> x mod 3 = 0) v ;
v =~~ [|0;6|];
Vec_int.inplace_filter (fun x -> x mod 3 <> 0) v ;
v =~~ [||];
OUnit.assert_equal (Vec_int.capacity v ) capacity ;
Vec_int.compact v ;
OUnit.assert_equal (Vec_int.capacity v ) 0
end
;
"inplace_filter_from " ^ __LOC__ >:: begin fun _ ->
let v = Vec_int.of_array (Array.init 10 (fun i -> i)) in
v =~~ [|0; 1; 2; 3; 4; 5; 6; 7; 8; 9|];
Vec_int.push v 96 ;
Vec_int.inplace_filter_from 2 (fun x -> x mod 2 = 0) v ;
v =~~ [|0; 1; 2; 4; 6; 8; 96|];
Vec_int.inplace_filter_from 2 (fun x -> x mod 3 = 0) v ;
v =~~ [|0; 1; 6; 96|];
Vec_int.inplace_filter (fun x -> x mod 3 <> 0) v ;
v =~~ [|1|];
Vec_int.compact v ;
OUnit.assert_equal (Vec_int.capacity v ) 1
end
;
"map " ^ __LOC__ >:: begin fun _ ->
let v = Vec_int.of_array (Array.init 1000 (fun i -> i )) in
Vec_int.map succ v =~~ (Array.init 1000 succ) ;
OUnit.assert_bool __LOC__ (Vec_int.exists (fun x -> x >= 999) v );
OUnit.assert_bool __LOC__ (not (Vec_int.exists (fun x -> x > 1000) v ));
OUnit.assert_equal (Vec_int.last v ) 999
end ;
__LOC__ >:: begin fun _ ->
let count = 1000 in
let init_array = (Array.init count (fun i -> i)) in
let u = Vec_int.of_array init_array in
let v = Vec_int.inplace_filter_with (fun x -> x mod 2 = 0) ~cb_no:(fun a b -> Set_int.add b a)Set_int.empty u in
let (even,odd) = init_array |> Array.to_list |> List.partition (fun x -> x mod 2 = 0) in
OUnit.assert_equal
(Set_int.elements v) odd ;
u =~~ Array.of_list even
end ;
"filter" ^ __LOC__ >:: begin fun _ ->
let v = Vec_int.of_array [|1;2;3;4;5;6|] in
v |> Vec_int.filter (fun x -> x mod 3 = 0) |> (fun x -> x =~~ [|3;6|]);
v =~~ [|1;2;3;4;5;6|];
Vec_int.pop v ;
v =~~ [|1;2;3;4;5|];
let count = ref 0 in
let len = Vec_int.length v in
while not (Vec_int.is_empty v ) do
Vec_int.pop v ;
incr count
done;
OUnit.assert_equal len !count
end
;
__LOC__ >:: begin fun _ ->
let count = 100 in
let v = Vec_int.of_array (Array.init count (fun i -> i)) in
OUnit.assert_bool __LOC__
(try Vec_int.delete v count; false with _ -> true );
for i = count - 1 downto 10 do
Vec_int.delete v i ;
done ;
v =~~ [|0;1;2;3;4;5;6;7;8;9|]
end;
"sub" ^ __LOC__ >:: begin fun _ ->
let v = Vec_int.make 5 in
OUnit.assert_bool __LOC__
(try ignore @@ Vec_int.sub v 0 2 ; false with Invalid_argument _ -> true);
Vec_int.push v 1;
OUnit.assert_bool __LOC__
(try ignore @@ Vec_int.sub v 0 2 ; false with Invalid_argument _ -> true);
Vec_int.push v 2;
( Vec_int.sub v 0 2 =~~ [|1;2|])
end;
"reserve" ^ __LOC__ >:: begin fun _ ->
let v = Vec_int.empty () in
Vec_int.reserve v 1000 ;
for i = 0 to 900 do
Vec_int.push v i
done ;
OUnit.assert_equal (Vec_int.length v) 901 ;
OUnit.assert_equal (Vec_int.capacity v) 1000
end ;
"capacity" ^ __LOC__ >:: begin fun _ ->
let v = Vec_int.of_array [|3|] in
Vec_int.reserve v 10 ;
v =~~ [|3 |];
Vec_int.push v 1 ;
Vec_int.push v 2 ;
Vec_int.push v 5;
v=~~ [|3;1;2;5|];
OUnit.assert_equal (Vec_int.capacity v ) 10 ;
for i = 0 to 5 do
Vec_int.push v i
done;
v=~~ [|3;1;2;5;0;1;2;3;4;5|];
Vec_int.push v 100;
v=~~[|3;1;2;5;0;1;2;3;4;5;100|];
OUnit.assert_equal (Vec_int.capacity v ) 20
end
;
__LOC__ >:: begin fun _ ->
let empty = Vec_int.empty () in
Vec_int.push empty 3;
empty =~~ [|3|];
end
;
__LOC__ >:: begin fun _ ->
let lst = [1;2;3;4] in
let v = Vec_int.of_list lst in
OUnit.assert_equal
(Vec_int.map_into_list (fun x -> x + 1) v)
(Ext_list.map lst (fun x -> x + 1) )
end;
__LOC__ >:: begin fun _ ->
let v = Vec_int.make 4 in
Vec_int.push v 1 ;
Vec_int.push v 2;
Vec_int.reverse_in_place v;
v =~~ [|2;1|]
end
;
]