Debian Codegraph @

test-suite/ltac2/array_lib.v

raw
1 Require Import Ltac2.Ltac2.
2 Import Ltac2.Message.
3 Import Ltac2.Array.
4 Require Ltac2.List.
5 Require Ltac2.Int.
6
7 (* Array/List comparison functions which throw an exception on unequal *)
8
9 Ltac2 Type exn ::= [ Regression_Test_Failure ].
10
11 Ltac2 check_eq_int a l :=
12 List.iter2
13 (fun a b => match Int.equal a b with true => () | false => Control.throw Regression_Test_Failure end)
14 (to_list a) l.
15
16 Ltac2 check_eq_bool a l :=
17 List.iter2
18 (fun a b => match Bool.equal a b with true => () | false => Control.throw Regression_Test_Failure end)
19 (to_list a) l.
20
21 Ltac2 check_eq_int_matrix m ll :=
22 List.iter2 (fun a b => check_eq_int a b) (to_list m) ll.
23
24 Ltac2 check_eq_bool_matrix m ll :=
25 List.iter2 (fun a b => check_eq_bool a b) (to_list m) ll.
26
27 (* The below printing functions are mostly for debugging below test cases *)
28
29 Ltac2 print2 m1 m2 := print (Message.concat m1 m2).
30 Ltac2 print3 m1 m2 m3 := print2 m1 (Message.concat m2 m3).
31
32 Ltac2 print_int_array a :=
33 iteri (fun i x => print3 (of_int i) (of_string "=") (of_int x)) a.
34
35 Ltac2 of_bool b := match b with true=>of_string "true" | false=>of_string "false" end.
36
37 Ltac2 print_bool_array a :=
38 iteri (fun i x => print3 (of_int i) (of_string "=") (of_bool x)) a.
39
40 Ltac2 print_int_list a :=
41 List.iteri (fun i x => print3 (of_int i) (of_string "=") (of_int x)) a.
42
43 Goal True.
44 (* Test failure *)
45 Fail check_eq_int ((init 3 (fun i => (Int.add i 10)))) [10;11;13].
46
47 (* test empty with int *)
48 check_eq_int empty [].
49 check_eq_int (append empty (init 3 (fun i => (Int.add i 10)))) [10;11;12].
50 check_eq_int (append (init 3 (fun i => (Int.add i 10))) empty) [10;11;12].
51
52 (* test empty with bool *)
53 check_eq_bool empty [].
54 check_eq_bool (append empty (init 3 (fun i => (Int.ge i 2)))) [false;false;true].
55 check_eq_bool (append (init 3 (fun i => (Int.ge i 2))) empty) [false;false;true].
56
57 (* test init with int *)
58 check_eq_int (init 0 (fun i => (Int.add i 10))) [].
59 check_eq_int (init 4 (fun i => (Int.add i 10))) [10;11;12;13].
60
61 (* test init with bool *)
62 check_eq_bool (init 0 (fun i => (Int.ge i 2))) [].
63 check_eq_bool (init 4 (fun i => (Int.ge i 2))) [false;false;true;true].
64
65 (* test make_matrix, set, get *)
66 let a := make_matrix 4 3 1 in
67 Array.set (Array.get a 1) 2 0;
68 check_eq_int_matrix a [[1;1;1];[1;1;0];[1;1;1];[1;1;1]].
69
70 let a := make_matrix 3 4 false in
71 Array.set (Array.get a 2) 1 true;
72 check_eq_bool_matrix a [[false;false;false;false];[false;false;false;false];[false;true;false;false]].
73
74 (* test copy *)
75 let a := init 4 (fun i => (Int.add i 10)) in
76 let b := copy a in
77 check_eq_int b [10;11;12;13].
78
79 (* test append *)
80 let a := init 3 (fun i => (Int.add i 10)) in
81 let b := init 4 (fun i => (Int.add i 20)) in
82 check_eq_int (append a b) [10;11;12;20;21;22;23].
83
84 (* test concat *)
85 let a := init 3 (fun i => (Int.add i 10)) in
86 let b := init 4 (fun i => (Int.add i 20)) in
87 let c := init 5 (fun i => (Int.add i 30)) in
88 check_eq_int (concat (a::b::c::[])) [10;11;12;20;21;22;23;30;31;32;33;34].
89
90 (* test sub *)
91 let a := init 10 (fun i => (Int.add i 10)) in
92 let b := (sub a 3 0) in
93 let c := (append b (init 3 (fun i => (Int.add i 10)))) in
94 check_eq_int b [];
95 check_eq_int c [10;11;12].
96
97 let a := init 10 (fun i => (Int.add i 10)) in
98 let b := (sub a 3 4) in
99 check_eq_int b [13;14;15;16].
100
101 (* test fill *)
102 let a := init 10 (fun i => (Int.add i 10)) in
103 fill a 3 4 0;
104 check_eq_int a [10;11;12;0;0;0;0;17;18;19].
105
106 (* test blit *)
107 let a := init 10 (fun i => (Int.add i 10)) in
108 let b := init 10 (fun i => (Int.add i 20)) in
109 blit a 5 b 3 4;
110 check_eq_int b [20;21;22;15;16;17;18;27;28;29].
111
112 (* test iter *)
113 let a := init 4 (fun i => (Int.add i 3)) in
114 let b := init 10 (fun i => 10) in
115 iter (fun x => Array.set b x x) a;
116 check_eq_int b [10;10;10;3;4;5;6;10;10;10].
117
118 (* test iter2 *)
119 let a := init 4 (fun i => (Int.add i 2)) in
120 let b := init 4 (fun i => (Int.add i 4)) in
121 let c := init 8 (fun i => 10) in
122 iter2 (fun x y => Array.set c x y) a b;
123 check_eq_int c [10;10;4;5;6;7;10;10].
124
125 (* test map *)
126 let a := init 4 (fun i => (Int.add i 10)) in
127 check_eq_bool (map (fun i => (Int.ge i 12)) a) [false;false;true;true].
128
129 (* test map2 *)
130 let a := init 4 (fun i => (Int.add 10 i)) in
131 let b := init 4 (fun i => (Int.sub 13 i)) in
132 check_eq_bool (map2 (fun x y => (Int.ge x y)) a b) [false;false;true;true].
133
134 (* test iteri *)
135 let a := init 4 (fun i => (Int.add i 10)) in
136 let m := make_matrix 4 2 100 in
137 iteri (fun i x => Array.set (Array.get m i) 0 i; Array.set (Array.get m i) 1 x) a;
138 check_eq_int_matrix m [[0;10];[1;11];[2;12];[3;13]].
139
140 (* test mapi *)
141 let a := init 4 (fun i => (Int.sub 3 i)) in
142 check_eq_bool (mapi (fun i x => (Int.ge i x)) a) [false;false;true;true].
143
144 (* to_list is already tested in check_eq_... *)
145
146 (* test of_list *)
147 check_eq_int (of_list ([0;1;2;3])) [0;1;2;3].
148
149 (* test fold_left *)
150 let a := init 4 (fun i => (Int.add 10 i)) in
151 check_eq_int (of_list (fold_left (fun a b => b::a) [] a)) [13;12;11;10].
152
153 (* test fold_right *)
154 let a := init 4 (fun i => (Int.add 10 i)) in
155 check_eq_int (of_list (fold_right (fun a b => a::b) a [])) [10;11;12;13].
156
157 (* test exist *)
158 let a := init 4 (fun i => (Int.add 10 i)) in
159 let l := [
160 exist (fun x => Int.equal x 10) a;
161 exist (fun x => Int.equal x 13) a;
162 exist (fun x => Int.equal x 14) a] in
163 check_eq_bool (of_list l) [true;true;false].
164
165 (* test for_all *)
166 let a := init 4 (fun i => (Int.add 10 i)) in
167 let l := [
168 for_all (fun x => Int.lt x 14) a;
169 for_all (fun x => Int.lt x 13) a] in
170 check_eq_bool (of_list l) [true;false].
171
172 (* test mem *)
173 let a := init 4 (fun i => (Int.add 10 i)) in
174 let l := [
175 mem Int.equal 10 a;
176 mem Int.equal 13 a;
177 mem Int.equal 14 a] in
178 check_eq_bool (of_list l) [true;true;false].
179
180 exact I.
181 Qed.