Skip to content

Commit 5de3dbe

Browse files
committed
Oxcaml: add tests (indexed by unboxed int)
1 parent 9c1e499 commit 5de3dbe

File tree

4 files changed

+4135
-0
lines changed

4 files changed

+4135
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
disable
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
(* TEST
2+
flambda2;
3+
include stdlib_upstream_compatible;
4+
{
5+
native;
6+
}{
7+
bytecode;
8+
}{
9+
flags = "-extension layouts_alpha";
10+
native;
11+
}{
12+
flags = "-extension layouts_alpha";
13+
bytecode;
14+
}{
15+
flags = "-extension layouts_beta";
16+
native;
17+
}{
18+
flags = "-extension layouts_beta";
19+
bytecode;
20+
}
21+
*)
22+
23+
module By_int64_u = struct
24+
module I = Stdlib_upstream_compatible.Int64_u
25+
module A = struct
26+
external get : 'a array -> int64# -> 'a =
27+
"%array_safe_get_indexed_by_int64#"
28+
external set : 'a array -> int64# -> 'a -> unit =
29+
"%array_safe_set_indexed_by_int64#"
30+
external unsafe_get : 'a array -> int64# -> 'a =
31+
"%array_unsafe_get_indexed_by_int64#"
32+
external unsafe_set : 'a array -> int64# -> 'a -> unit =
33+
"%array_unsafe_set_indexed_by_int64#"
34+
end
35+
end
36+
37+
module By_int32_u = struct
38+
module I = Stdlib_upstream_compatible.Int32_u
39+
module A = struct
40+
external get : 'a array -> int32# -> 'a =
41+
"%array_safe_get_indexed_by_int32#"
42+
external set : 'a array -> int32# -> 'a -> unit =
43+
"%array_safe_set_indexed_by_int32#"
44+
external unsafe_get : 'a array -> int32# -> 'a =
45+
"%array_unsafe_get_indexed_by_int32#"
46+
external unsafe_set : 'a array -> int32# -> 'a -> unit =
47+
"%array_unsafe_set_indexed_by_int32#"
48+
end
49+
end
50+
51+
module By_nativeint_u = struct
52+
module I = Stdlib_upstream_compatible.Nativeint_u
53+
54+
module A = struct
55+
external get : 'a array -> nativeint# -> 'a =
56+
"%array_safe_get_indexed_by_nativeint#"
57+
external set : 'a array -> nativeint# -> 'a -> unit =
58+
"%array_safe_set_indexed_by_nativeint#"
59+
external unsafe_get : 'a array -> nativeint# -> 'a =
60+
"%array_unsafe_get_indexed_by_nativeint#"
61+
external unsafe_set : 'a array -> nativeint# -> 'a -> unit =
62+
"%array_unsafe_set_indexed_by_nativeint#"
63+
end
64+
end
65+
66+
let check_eq arr g =
67+
for i = 0 to Array.length arr - 1 do
68+
assert (g arr i = arr.(i))
69+
done
70+
71+
let check_inval f =
72+
try let _ = f () in assert false with
73+
| Invalid_argument _ -> ()
74+
75+
let pp = Format.printf
76+
77+
let test_get (g: 'a. 'a array -> int -> 'a) =
78+
check_eq [| 1; 2; 3; 4; 5; 6; 7|] g;
79+
check_eq [| "a"; "b"; "c"; "d"|] g;
80+
check_eq [| 1.; 2.; 3.; 4.; 5.|] g;
81+
()
82+
83+
let test_set (g: 'a. 'a array -> int -> 'a -> unit) =
84+
let fill arr v =
85+
for i = 0 to Array.length arr - 1 do
86+
g arr i v; assert(Array.get arr i = v)
87+
done
88+
in
89+
let check_all_eq arr v = assert (Array.for_all (fun x -> x = v) arr) in
90+
let arr = [| 1; 2; 3; 4; 5; 6; 7|] in
91+
fill arr 0; check_all_eq arr 0;
92+
let arr = [| "a"; "b"; "c"; "d"|] in
93+
fill arr "aaa"; check_all_eq arr "aaa";
94+
let arr = [| 1.; 2.; 3.; 4.; 5.|] in
95+
fill arr 0.; check_all_eq arr 0.;
96+
()
97+
98+
let test_int64_u () =
99+
let open By_int64_u in
100+
101+
test_get (fun arr i -> A.get arr (I.of_int i));
102+
test_get (fun arr i -> A.unsafe_get arr (I.of_int i));
103+
104+
test_set (fun arr i -> A.set arr (I.of_int i));
105+
test_set (fun arr i -> A.unsafe_set arr (I.of_int i));
106+
107+
(* This is
108+
0b1000000000000000000000000000000000000000000000000000000000000001
109+
in binary and should be out of bound. *)
110+
check_inval (fun () -> A.get [| 1; 2; 3|] (-#9223372036854775807L));
111+
check_inval (fun () -> A.set [| 1; 2; 3|] (-#9223372036854775807L) 0);
112+
(* no promises when using unsafe_get. int truncation happens. *)
113+
let arr = [| 1; 2; 3|] in
114+
assert (A.unsafe_get arr (-#9223372036854775807L) = 2);
115+
A.unsafe_set arr (-#9223372036854775807L) 11111;
116+
assert (A.unsafe_get arr #1L = 11111);
117+
check_inval (fun () -> A.get [| 1; 2; 3|] (-#1L));
118+
check_inval (fun () -> A.set [| 1; 2; 3|] (-#1L) 1);
119+
()
120+
121+
let test_int32_u () =
122+
let open By_int32_u in
123+
124+
test_get (fun arr i -> A.get arr (I.of_int i));
125+
test_get (fun arr i -> A.unsafe_get arr (I.of_int i));
126+
127+
test_set (fun arr i -> A.set arr (I.of_int i));
128+
test_set (fun arr i -> A.unsafe_set arr (I.of_int i));
129+
130+
check_inval (fun () -> A.get [| 1; 2; 3|] (-#2147483647l));
131+
check_inval (fun () -> A.set [| 1; 2; 3|] (-#2147483647l) 0);
132+
check_inval (fun () -> A.get [| 1; 2; 3|] (-#1l));
133+
check_inval (fun () -> A.set [| 1; 2; 3|] (-#1l) 1);
134+
()
135+
136+
let test_nativeint_u () =
137+
let open By_nativeint_u in
138+
139+
test_get (fun arr i -> A.get arr (I.of_int i));
140+
test_get (fun arr i -> A.unsafe_get arr (I.of_int i));
141+
142+
test_set (fun arr i -> A.set arr (I.of_int i));
143+
test_set (fun arr i -> A.unsafe_set arr (I.of_int i));
144+
145+
check_inval (fun () -> A.get [| 1; 2; 3|] (-#0x7fffffffn));
146+
check_inval (fun () -> A.set [| 1; 2; 3|] (-#0x7fffffffn) 0);
147+
check_inval (fun () -> A.get [| 1; 2; 3|] (-#1n));
148+
check_inval (fun () -> A.set [| 1; 2; 3|] (-#1n) 1);
149+
()
150+
151+
let () =
152+
test_int64_u ();
153+
test_int32_u ();
154+
test_nativeint_u ();
155+
()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(env
2+
(_
3+
(flags
4+
(:standard
5+
(-w -32)))))
6+
7+
(tests
8+
(names array_indexing stringlike_indexing)
9+
(build_if %{oxcaml_supported})
10+
(libraries stdlib_stable stdlib_upstream_compatible)
11+
(modes js wasm))

0 commit comments

Comments
 (0)