@@ -124,6 +124,20 @@ let test : type a b c. (a, b, c) Setup.t -> b array -> unit =
124
124
if not (kind_field_is_correct setup a3) then print_endline " corrupted `kind`" ;
125
125
()
126
126
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
+
127
141
let % expect_test " float32" =
128
142
test Setup. Float32 [| Float. neg_infinity; - 1. ; 0. ; 1. ; Float. infinity |];
129
143
[% expect {|| }]
@@ -137,7 +151,17 @@ let%expect_test "int8" =
137
151
[% expect {|| }]
138
152
139
153
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" ;
141
165
[% expect {|| }]
142
166
143
167
let % expect_test " int16" =
0 commit comments