Skip to content

Commit ecc624e

Browse files
committed
tests: ArrayBuffer subarray, add tests for uint 8-32 typed arrays
1 parent dc51bdc commit ecc624e

5 files changed

Lines changed: 1029 additions & 1 deletion

File tree

tests/unit/src/TestMain.hx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class TestMain extends Application {
88

99
var runner = new Runner();
1010
runner.addCase(new utils.ArrayBufferTest());
11+
runner.addCase(new utils.UInt8ArrayTest());
12+
runner.addCase(new utils.UInt16ArrayTest());
13+
runner.addCase(new utils.UInt32ArrayTest());
1114
runner.addCase(new utils.DataViewTest());
1215
Report.create(runner);
1316
runner.run();

tests/unit/src/utils/ArrayBufferTest.hx

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package utils;
22

3+
import lime.utils.UInt8Array;
34
import lime.utils.DataView;
45
import lime.utils.ArrayBuffer;
56
import utest.Assert;
@@ -12,6 +13,57 @@ class ArrayBufferTest extends Test {
1213

1314
public function testByteLength():Void {
1415
var buffer = new ArrayBuffer(4);
15-
Assert.equals(4, buffer.byteLength);
16+
Assert.equals(4, buffer.byteLength);
17+
}
18+
19+
public function testSubarray():Void {
20+
var buffer = new ArrayBuffer(4);
21+
var array = new UInt8Array(buffer);
22+
array[0] = 0xca;
23+
array[1] = 0xfe;
24+
array[2] = 0xba;
25+
array[3] = 0xbe;
26+
27+
var fourBuffer = buffer.slice(0, 4);
28+
Assert.equals(4, fourBuffer.byteLength);
29+
var fourArray = new UInt8Array(fourBuffer);
30+
Assert.equals(4, fourArray.byteLength);
31+
Assert.equals(0xca, fourArray[0]);
32+
Assert.equals(0xfe, fourArray[1]);
33+
Assert.equals(0xba, fourArray[2]);
34+
Assert.equals(0xbe, fourArray[3]);
35+
36+
var twoStartBuffer = buffer.slice(0, 2);
37+
Assert.equals(2, twoStartBuffer.byteLength);
38+
var twoStartArray = new UInt8Array(twoStartBuffer);
39+
Assert.equals(2, twoStartArray.byteLength);
40+
Assert.equals(0xca, twoStartArray[0]);
41+
Assert.equals(0xfe, twoStartArray[1]);
42+
43+
var twoEndBuffer = buffer.slice(2, 4);
44+
Assert.equals(2, twoEndBuffer.byteLength);
45+
var twoEndArray = new UInt8Array(twoEndBuffer);
46+
Assert.equals(2, twoEndArray.byteLength);
47+
Assert.equals(0xba, twoEndArray[0]);
48+
Assert.equals(0xbe, twoEndArray[1]);
49+
50+
var endBeforeStartBuffer = buffer.slice(2, 1);
51+
Assert.equals(0, endBeforeStartBuffer.byteLength);
52+
var endBeforeStartArray = new UInt8Array(endBeforeStartBuffer);
53+
Assert.equals(0, endBeforeStartArray.byteLength);
54+
55+
var endEqualsStartBuffer = buffer.slice(2, 2);
56+
Assert.equals(0, endEqualsStartBuffer.byteLength);
57+
var endEqualsStartArray = new UInt8Array(endEqualsStartBuffer);
58+
Assert.equals(0, endEqualsStartArray.byteLength);
59+
60+
var beyondLengthBuffer = buffer.slice(0, 400);
61+
Assert.equals(4, beyondLengthBuffer.byteLength);
62+
var beyondLengthArray = new UInt8Array(beyondLengthBuffer);
63+
Assert.equals(4, beyondLengthArray.byteLength);
64+
Assert.equals(0xca, beyondLengthArray[0]);
65+
Assert.equals(0xfe, beyondLengthArray[1]);
66+
Assert.equals(0xba, beyondLengthArray[2]);
67+
Assert.equals(0xbe, beyondLengthArray[3]);
1668
}
1769
}
Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
package utils;
2+
3+
import lime.utils.UInt16Array;
4+
import lime.utils.UInt32Array;
5+
import lime.utils.UInt8Array;
6+
import lime.utils.ArrayBuffer;
7+
import utest.Assert;
8+
import utest.Test;
9+
10+
class UInt16ArrayTest extends Test {
11+
public function new() {
12+
super();
13+
}
14+
15+
public function testByteLength():Void {
16+
var buffer = new ArrayBuffer(8);
17+
var array = new UInt16Array(buffer);
18+
Assert.equals(4, array.length);
19+
Assert.equals(8, array.byteLength);
20+
}
21+
22+
public function testValues():Void {
23+
var buffer = new ArrayBuffer(8);
24+
var array = new UInt16Array(buffer);
25+
Assert.equals(0x0, array[0]);
26+
Assert.equals(0x0, array[1]);
27+
Assert.equals(0x0, array[2]);
28+
Assert.equals(0x0, array[3]);
29+
array[0] = 0xcafe;
30+
array[1] = 0xbabe;
31+
array[2] = 0xdeca;
32+
array[3] = 0xfbad;
33+
Assert.equals(0xcafe, array[0]);
34+
Assert.equals(0xbabe, array[1]);
35+
Assert.equals(0xdeca, array[2]);
36+
Assert.equals(0xfbad, array[3]);
37+
}
38+
39+
public function testSubarray():Void {
40+
var buffer = new ArrayBuffer(8);
41+
var array = new UInt16Array(buffer);
42+
array[0] = 0xcafe;
43+
array[1] = 0xbabe;
44+
array[2] = 0xdeca;
45+
array[3] = 0xfbad;
46+
47+
var four = array.subarray(0, 4);
48+
Assert.equals(4, four.length);
49+
Assert.equals(8, four.byteLength);
50+
Assert.equals(0xcafe, four[0]);
51+
Assert.equals(0xbabe, four[1]);
52+
Assert.equals(0xdeca, four[2]);
53+
Assert.equals(0xfbad, four[3]);
54+
55+
var twoStart = array.subarray(0, 2);
56+
Assert.equals(2, twoStart.length);
57+
Assert.equals(4, twoStart.byteLength);
58+
Assert.equals(0xcafe, twoStart[0]);
59+
Assert.equals(0xbabe, twoStart[1]);
60+
61+
var twoEnd = array.subarray(2, 4);
62+
Assert.equals(2, twoEnd.length);
63+
Assert.equals(4, twoEnd.byteLength);
64+
Assert.equals(0xdeca, twoEnd[0]);
65+
Assert.equals(0xfbad, twoEnd[1]);
66+
67+
var endBeforeStart = array.subarray(2, 1);
68+
Assert.equals(0, endBeforeStart.length);
69+
Assert.equals(0, endBeforeStart.byteLength);
70+
71+
var endEqualsStart = array.subarray(2, 2);
72+
Assert.equals(0, endEqualsStart.length);
73+
Assert.equals(0, endEqualsStart.byteLength);
74+
75+
var beyondLength = array.subarray(0, 400);
76+
Assert.equals(4, beyondLength.length);
77+
Assert.equals(8, beyondLength.byteLength);
78+
Assert.equals(0xcafe, beyondLength[0]);
79+
Assert.equals(0xbabe, beyondLength[1]);
80+
Assert.equals(0xdeca, beyondLength[2]);
81+
Assert.equals(0xfbad, beyondLength[3]);
82+
}
83+
84+
public function testSetArrayOfInts():Void {
85+
var array1:Array<Int> = [0xcafe, 0xbabe, 0xdeca, 0xfbad];
86+
87+
var buffer2 = new ArrayBuffer(16);
88+
var array2 = new UInt16Array(buffer2);
89+
90+
array2.set(array1);
91+
Assert.equals(0xcafe, array2[0]);
92+
Assert.equals(0xbabe, array2[1]);
93+
Assert.equals(0xdeca, array2[2]);
94+
Assert.equals(0xfbad, array2[3]);
95+
Assert.equals(0x0, array2[4]);
96+
Assert.equals(0x0, array2[5]);
97+
Assert.equals(0x0, array2[6]);
98+
Assert.equals(0x0, array2[7]);
99+
100+
for (i in 0...array2.length) {
101+
array2[i] = 0x0;
102+
}
103+
104+
array2.set(array1, 1);
105+
Assert.equals(0x0, array2[0]);
106+
Assert.equals(0xcafe, array2[1]);
107+
Assert.equals(0xbabe, array2[2]);
108+
Assert.equals(0xdeca, array2[3]);
109+
Assert.equals(0xfbad, array2[4]);
110+
Assert.equals(0x0, array2[5]);
111+
Assert.equals(0x0, array2[6]);
112+
Assert.equals(0x0, array2[7]);
113+
114+
for (i in 0...array2.length) {
115+
array2[i] = 0x0;
116+
}
117+
118+
Assert.raises(function():Void {
119+
array2.set(array1, 6);
120+
});
121+
Assert.equals(0x0, array2[0]);
122+
Assert.equals(0x0, array2[1]);
123+
Assert.equals(0x0, array2[2]);
124+
Assert.equals(0x0, array2[3]);
125+
Assert.equals(0x0, array2[4]);
126+
Assert.equals(0x0, array2[5]);
127+
Assert.equals(0x0, array2[6]);
128+
Assert.equals(0x0, array2[7]);
129+
}
130+
131+
public function testSetUInt8Array():Void {
132+
var buffer1 = new ArrayBuffer(4);
133+
var array1 = new UInt8Array(buffer1);
134+
array1[0] = 0xca;
135+
array1[1] = 0xfe;
136+
array1[2] = 0xba;
137+
array1[3] = 0xbe;
138+
139+
var buffer2 = new ArrayBuffer(16);
140+
var array2 = new UInt16Array(buffer2);
141+
142+
array2.set(array1);
143+
Assert.equals(0xca, array2[0]);
144+
Assert.equals(0xfe, array2[1]);
145+
Assert.equals(0xba, array2[2]);
146+
Assert.equals(0xbe, array2[3]);
147+
Assert.equals(0x0, array2[4]);
148+
Assert.equals(0x0, array2[5]);
149+
Assert.equals(0x0, array2[6]);
150+
Assert.equals(0x0, array2[7]);
151+
152+
// reset the array to all zeros
153+
for (i in 0...array2.length) {
154+
array2[i] = 0x0;
155+
}
156+
157+
array2.set(array1, 1);
158+
Assert.equals(0x0, array2[0]);
159+
Assert.equals(0xca, array2[1]);
160+
Assert.equals(0xfe, array2[2]);
161+
Assert.equals(0xba, array2[3]);
162+
Assert.equals(0xbe, array2[4]);
163+
Assert.equals(0x0, array2[5]);
164+
Assert.equals(0x0, array2[6]);
165+
Assert.equals(0x0, array2[7]);
166+
167+
for (i in 0...array2.length) {
168+
array2[i] = 0x0;
169+
}
170+
171+
// if the array can't fit, it throws
172+
Assert.raises(function():Void {
173+
array2.set(array1, 6);
174+
});
175+
// in that case, none of the values should have been updated
176+
Assert.equals(0x0, array2[0]);
177+
Assert.equals(0x0, array2[1]);
178+
Assert.equals(0x0, array2[2]);
179+
Assert.equals(0x0, array2[3]);
180+
Assert.equals(0x0, array2[4]);
181+
Assert.equals(0x0, array2[5]);
182+
Assert.equals(0x0, array2[6]);
183+
Assert.equals(0x0, array2[7]);
184+
}
185+
186+
public function testSetUInt16Array():Void {
187+
var buffer1 = new ArrayBuffer(8);
188+
var array1 = new UInt16Array(buffer1);
189+
array1[0] = 0xcafe;
190+
array1[1] = 0xbabe;
191+
array1[2] = 0xdeca;
192+
array1[3] = 0xfbad;
193+
194+
var buffer2 = new ArrayBuffer(16);
195+
var array2 = new UInt16Array(buffer2);
196+
197+
array2.set(array1);
198+
Assert.equals(0xcafe, array2[0]);
199+
Assert.equals(0xbabe, array2[1]);
200+
Assert.equals(0xdeca, array2[2]);
201+
Assert.equals(0xfbad, array2[3]);
202+
Assert.equals(0x0, array2[4]);
203+
Assert.equals(0x0, array2[5]);
204+
Assert.equals(0x0, array2[6]);
205+
Assert.equals(0x0, array2[7]);
206+
207+
// reset the array to all zeros
208+
for (i in 0...array2.length) {
209+
array2[i] = 0x0;
210+
}
211+
212+
array2.set(array1, 1);
213+
Assert.equals(0x0, array2[0]);
214+
Assert.equals(0xcafe, array2[1]);
215+
Assert.equals(0xbabe, array2[2]);
216+
Assert.equals(0xdeca, array2[3]);
217+
Assert.equals(0xfbad, array2[4]);
218+
Assert.equals(0x0, array2[5]);
219+
Assert.equals(0x0, array2[6]);
220+
Assert.equals(0x0, array2[7]);
221+
222+
for (i in 0...array2.length) {
223+
array2[i] = 0x0;
224+
}
225+
226+
// if the array can't fit, it throws
227+
Assert.raises(function():Void {
228+
array2.set(array1, 6);
229+
});
230+
// in that case, none of the values should have been updated
231+
Assert.equals(0x0, array2[0]);
232+
Assert.equals(0x0, array2[1]);
233+
Assert.equals(0x0, array2[2]);
234+
Assert.equals(0x0, array2[3]);
235+
Assert.equals(0x0, array2[4]);
236+
Assert.equals(0x0, array2[5]);
237+
Assert.equals(0x0, array2[6]);
238+
Assert.equals(0x0, array2[7]);
239+
}
240+
241+
public function testSetUInt32Array():Void {
242+
var buffer1 = new ArrayBuffer(16);
243+
var array1 = new UInt32Array(buffer1);
244+
array1[0] = 0xcafebabe;
245+
array1[1] = 0xdecafbad;
246+
array1[2] = 0xffffffff;
247+
array1[3] = 0xdeadbeef;
248+
249+
var buffer2 = new ArrayBuffer(16);
250+
var array2 = new UInt16Array(buffer2);
251+
252+
array2.set(array1);
253+
Assert.equals(0xbabe, array2[0]);
254+
Assert.equals(0xfbad, array2[1]);
255+
Assert.equals(0xffff, array2[2]);
256+
Assert.equals(0xbeef, array2[3]);
257+
Assert.equals(0x0, array2[4]);
258+
Assert.equals(0x0, array2[5]);
259+
Assert.equals(0x0, array2[6]);
260+
Assert.equals(0x0, array2[7]);
261+
262+
// reset the array to all zeros
263+
for (i in 0...array2.length) {
264+
array2[i] = 0x0;
265+
}
266+
267+
array2.set(array1, 1);
268+
Assert.equals(0x0, array2[0]);
269+
Assert.equals(0xbabe, array2[1]);
270+
Assert.equals(0xfbad, array2[2]);
271+
Assert.equals(0xffff, array2[3]);
272+
Assert.equals(0xbeef, array2[4]);
273+
Assert.equals(0x0, array2[5]);
274+
Assert.equals(0x0, array2[6]);
275+
Assert.equals(0x0, array2[7]);
276+
277+
for (i in 0...array2.length) {
278+
array2[i] = 0x0;
279+
}
280+
281+
// if the array can't fit, it throws
282+
Assert.raises(function():Void {
283+
array2.set(array1, 6);
284+
});
285+
// in that case, none of the values should have been updated
286+
Assert.equals(0x0, array2[0]);
287+
Assert.equals(0x0, array2[1]);
288+
Assert.equals(0x0, array2[2]);
289+
Assert.equals(0x0, array2[3]);
290+
Assert.equals(0x0, array2[4]);
291+
Assert.equals(0x0, array2[5]);
292+
Assert.equals(0x0, array2[6]);
293+
Assert.equals(0x0, array2[7]);
294+
}
295+
}

0 commit comments

Comments
 (0)