Skip to content

Commit 56860f1

Browse files
committed
Test new module Typed_array.Bytes
1 parent a9c138d commit 56860f1

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

lib/tests/test_typed_array.ml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,20 @@ let test : type a b c. (a, b, c) Setup.t -> b array -> unit =
124124
if not (kind_field_is_correct setup a3) then print_endline "corrupted `kind`";
125125
()
126126

127+
(* Byte-wise equality *)
128+
let typed_arrays_equal ta1 ta2 =
129+
let byte_len1 = ta1##.byteLength
130+
and byte_len2 = ta2##.byteLength in
131+
if not (Int.equal byte_len1 byte_len2) then false
132+
else
133+
let view1 = new%js dataView (ta1##.buffer) in
134+
let view2 = new%js dataView (ta2##.buffer) in
135+
let rec cmp i =
136+
if i >= byte_len1 then true
137+
else Int.equal (view1##getUint8 i) (view2##getUint8 i) && cmp (i + 1)
138+
in
139+
cmp 0
140+
127141
let%expect_test "float32" =
128142
test Setup.Float32 [| Float.neg_infinity; -1.; 0.; 1.; Float.infinity |];
129143
[%expect {||}]
@@ -137,7 +151,17 @@ let%expect_test "int8" =
137151
[%expect {||}]
138152

139153
let%expect_test "uint8" =
140-
test Setup.Uint8 [| 0; 255 |];
154+
let a = [| 0; 255 |] in
155+
test Setup.Uint8 a;
156+
let ta = from_genarray (type_of_setup Setup.Uint8) (ba_of_array Setup.Uint8 a) in
157+
let bytes = Typed_array.Bytes.of_uint8Array ta in
158+
let ta' = Typed_array.Bytes.to_uint8Array bytes in
159+
if not (typed_arrays_equal ta ta') then
160+
print_endline "round-trip from uint8Array to bytes and back not equal";
161+
let buffer = ta##.buffer in
162+
let bytes'' = Typed_array.Bytes.of_arrayBuffer buffer in
163+
if not (Stdlib.Bytes.equal bytes'' bytes)
164+
then print_endline "bytes from arrayBuffer not equal to bytes from of_uint8Array";
141165
[%expect {||}]
142166

143167
let%expect_test "int16" =

0 commit comments

Comments
 (0)