Debian Codegraph @

rhino/testsrc/jstests/harmony/dataview-accessors.js

raw symbols SCIP
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 load("testsrc/assert.js");
29
30 // These tests are adapted from Khronos DataView tests
31
32 var intArray1 =
33 [0, 1, 2, 3, 100, 101, 102, 103, 128, 129, 130, 131, 252, 253, 254, 255];
34 var intArray2 =
35 [31, 32, 33, 0, 1, 2, 3, 100, 101, 102, 103, 128, 129, 130, 131, 252,
36 253, 254, 255];
37 var initialArray =
38 [204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204,
39 204, 204, 204];
40
41 var arayBuffer = null;
42 var view = null;
43 var viewStart = 0;
44 var viewLength = 0;
45
46 function getElementSize(func) {
47 switch (func) {
48 case "Int8":
49 case "Uint8":
50 return 1;
51 case "Int16":
52 case "Uint16":
53 return 2;
54 case "Int32":
55 case "Uint32":
56 case "Float32":
57 return 4;
58 case "Float64":
59 return 8;
60 default:
61 assertUnreachable(func);
62 }
63 }
64
65 function checkGet(func, index, expected, littleEndian) {
66 function doGet() {
67 if (littleEndian != undefined)
68 return view["get" + func](index, littleEndian);
69 else
70 return view["get" + func](index);
71 }
72 if (index >=0 && index + getElementSize(func) - 1 < view.byteLength)
73 assertSame(expected, doGet());
74 else
75 assertThrows(doGet, RangeError);
76 }
77
78 function checkSet(func, index, value, littleEndian) {
79 function doSet() {
80 if (littleEndian != undefined)
81 view["set" + func](index, value, littleEndian);
82 else
83 view["set" + func](index, value);
84 }
85 if (index >= 0 &&
86 index + getElementSize(func) - 1 < view.byteLength) {
87 assertSame(undefined, doSet());
88 checkGet(func, index, value, littleEndian);
89 } else {
90 assertThrows(doSet, RangeError);
91 }
92 }
93
94 function test(isTestingGet, func, index, value, littleEndian) {
95 if (isTestingGet)
96 checkGet(func, index, value, littleEndian);
97 else
98 checkSet(func, index, value, littleEndian);
99 }
100
101 function createDataView(
102 array, frontPaddingNum, littleEndian, start, length) {
103 if (!littleEndian)
104 array.reverse();
105 var paddingArray = new Array(frontPaddingNum);
106 arrayBuffer = (new Uint8Array(paddingArray.concat(array))).buffer;
107 view = new DataView(arrayBuffer, viewStart, viewLength);
108 if (!littleEndian)
109 array.reverse(); // restore the array.
110 }
111
112 function runIntegerTestCases(isTestingGet, array, start, length) {
113 createDataView(array, 0, true, start, length);
114
115 test(isTestingGet, "Int8", 0, 0);
116 test(isTestingGet, "Int8", undefined, 0);
117 test(isTestingGet, "Int8", 8, -128);
118 test(isTestingGet, "Int8", 15, -1);
119 test(isTestingGet, "Int8", 1e12, undefined);
120
121 test(isTestingGet, "Uint8", 0, 0);
122 test(isTestingGet, "Uint8", undefined, 0);
123 test(isTestingGet, "Uint8", 8, 128);
124 test(isTestingGet, "Uint8", 15, 255);
125 test(isTestingGet, "Uint8", 1e12, undefined);
126
127 // Little endian.
128 test(isTestingGet, "Int16", 0, 256, true);
129 test(isTestingGet, "Int16", undefined, 256, true);
130 test(isTestingGet, "Int16", 5, 26213, true);
131 test(isTestingGet, "Int16", 9, -32127, true);
132 test(isTestingGet, "Int16", 14, -2, true);
133 test(isTestingGet, "Int16", 1e12, undefined, true);
134
135 // Big endian.
136 test(isTestingGet, "Int16", 0, 1);
137 test(isTestingGet, "Int16", undefined, 1);
138 test(isTestingGet, "Int16", 5, 25958);
139 test(isTestingGet, "Int16", 9, -32382);
140 test(isTestingGet, "Int16", 14, -257);
141 test(isTestingGet, "Int16", 1e12, undefined);
142
143 // Little endian.
144 test(isTestingGet, "Uint16", 0, 256, true);
145 test(isTestingGet, "Uint16", undefined, 256, true);
146 test(isTestingGet, "Uint16", 5, 26213, true);
147 test(isTestingGet, "Uint16", 9, 33409, true);
148 test(isTestingGet, "Uint16", 14, 65534, true);
149 test(isTestingGet, "Uint16", 1e12, undefined, true);
150
151 // Big endian.
152 test(isTestingGet, "Uint16", 0, 1);
153 test(isTestingGet, "Uint16", undefined, 1);
154 test(isTestingGet, "Uint16", 5, 25958);
155 test(isTestingGet, "Uint16", 9, 33154);
156 test(isTestingGet, "Uint16", 14, 65279);
157 test(isTestingGet, "Uint16", 1e12, undefined);
158
159 // Little endian.
160 test(isTestingGet, "Int32", 0, 50462976, true);
161 test(isTestingGet, "Int32", undefined, 50462976, true);
162 test(isTestingGet, "Int32", 3, 1717920771, true);
163 test(isTestingGet, "Int32", 6, -2122291354, true);
164 test(isTestingGet, "Int32", 9, -58490239, true);
165 test(isTestingGet, "Int32", 12,-66052, true);
166 test(isTestingGet, "Int32", 1e12, undefined, true);
167
168 // Big endian.
169 test(isTestingGet, "Int32", 0, 66051);
170 test(isTestingGet, "Int32", undefined, 66051);
171 test(isTestingGet, "Int32", 3, 56911206);
172 test(isTestingGet, "Int32", 6, 1718059137);
173 test(isTestingGet, "Int32", 9, -2122152964);
174 test(isTestingGet, "Int32", 12, -50462977);
175 test(isTestingGet, "Int32", 1e12, undefined);
176
177 // Little endian.
178 test(isTestingGet, "Uint32", 0, 50462976, true);
179 test(isTestingGet, "Uint32", undefined, 50462976, true);
180 test(isTestingGet, "Uint32", 3, 1717920771, true);
181 test(isTestingGet, "Uint32", 6, 2172675942, true);
182 test(isTestingGet, "Uint32", 9, 4236477057, true);
183 test(isTestingGet, "Uint32", 12,4294901244, true);
184 test(isTestingGet, "Uint32", 1e12, undefined, true);
185
186 // Big endian.
187 test(isTestingGet, "Uint32", 0, 66051);
188 test(isTestingGet, "Uint32", undefined, 66051);
189 test(isTestingGet, "Uint32", 3, 56911206);
190 test(isTestingGet, "Uint32", 6, 1718059137);
191 test(isTestingGet, "Uint32", 9, 2172814332);
192 test(isTestingGet, "Uint32", 12, 4244504319);
193 test(isTestingGet, "Uint32", 1e12, undefined);
194 }
195
196 function testFloat(isTestingGet, func, array, start, expected) {
197 // Little endian.
198 createDataView(array, 0, true, start);
199 test(isTestingGet, func, 0, expected, true);
200 test(isTestingGet, func, undefined, expected, true);
201 createDataView(array, 3, true, start);
202 test(isTestingGet, func, 3, expected, true);
203 createDataView(array, 7, true, start);
204 test(isTestingGet, func, 7, expected, true);
205 createDataView(array, 10, true, start);
206 test(isTestingGet, func, 10, expected, true);
207 test(isTestingGet, func, 1e12, undefined, true);
208
209 // Big endian.
210 createDataView(array, 0, false);
211 test(isTestingGet, func, 0, expected, false);
212 test(isTestingGet, func, undefined, expected, false);
213 createDataView(array, 3, false);
214 test(isTestingGet, func, 3, expected, false);
215 createDataView(array, 7, false);
216 test(isTestingGet, func, 7, expected, false);
217 createDataView(array, 10, false);
218 test(isTestingGet, func, 10, expected, false);
219 test(isTestingGet, func, 1e12, undefined, false);
220 }
221
222 function runFloatTestCases(isTestingGet, start) {
223 testFloat(isTestingGet, "Float32",
224 isTestingGet ? [0, 0, 32, 65] : initialArray, start, 10);
225
226 testFloat(isTestingGet, "Float32",
227 isTestingGet ? [164, 112, 157, 63] : initialArray,
228 start, 1.2300000190734863);
229 testFloat(isTestingGet, "Float32",
230 isTestingGet ? [95, 53, 50, 199] : initialArray,
231 start, -45621.37109375);
232 testFloat(isTestingGet, "Float32",
233 isTestingGet ? [255, 255, 255, 127] : initialArray,
234 start, NaN);
235 testFloat(isTestingGet, "Float32",
236 isTestingGet ? [255, 255, 255, 255] : initialArray,
237 start, -NaN);
238
239 testFloat(isTestingGet, "Float64",
240 isTestingGet ? [0, 0, 0, 0, 0, 0, 36, 64] : initialArray,
241 start, 10);
242 testFloat(isTestingGet, "Float64",
243 isTestingGet ? [174, 71, 225, 122, 20, 174, 243, 63] : initialArray,
244 start, 1.23);
245 testFloat(isTestingGet, "Float64",
246 isTestingGet ? [181, 55, 248, 30, 242, 179, 87, 193] : initialArray,
247 start, -6213576.4839);
248 testFloat(isTestingGet, "Float64",
249 isTestingGet ? [255, 255, 255, 255, 255, 255, 255, 127] : initialArray,
250 start, NaN);
251 testFloat(isTestingGet, "Float64",
252 isTestingGet ? [255, 255, 255, 255, 255, 255, 255, 255] : initialArray,
253 start, -NaN);
254 }
255
256 function runNegativeIndexTests(isTestingGet) {
257 createDataView(intArray1, 0, true, 0, 16);
258
259 test(isTestingGet, "Int8", -1, 0);
260 test(isTestingGet, "Int8", -2, 0);
261
262 test(isTestingGet, "Uint8", -1, 0);
263 test(isTestingGet, "Uint8", -2, 0);
264
265 test(isTestingGet, "Int16", -1, 1);
266 test(isTestingGet, "Int16", -2, 1);
267 test(isTestingGet, "Int16", -3, 1);
268
269 test(isTestingGet, "Uint16", -1, 1);
270 test(isTestingGet, "Uint16", -2, 1);
271 test(isTestingGet, "Uint16", -3, 1);
272
273 test(isTestingGet, "Int32", -1, 66051);
274 test(isTestingGet, "Int32", -3, 66051);
275 test(isTestingGet, "Int32", -5, 66051);
276
277 test(isTestingGet, "Uint32", -1, 66051);
278 test(isTestingGet, "Uint32", -3, 66051);
279 test(isTestingGet, "Uint32", -5, 66051);
280
281 createDataView([0, 0, 0, 0, 0, 0, 0, 0], 0, true, 0, 8);
282
283 test(isTestingGet, "Float32", -1, 0);
284 test(isTestingGet, "Float32", -3, 0);
285 test(isTestingGet, "Float32", -5, 0);
286
287 test(isTestingGet, "Float64", -1, 0);
288 test(isTestingGet, "Float64", -5, 0);
289 test(isTestingGet, "Float64", -9, 0);
290 }
291
292
293 function TestGetters() {
294 runIntegerTestCases(true, intArray1, 0, 16);
295 runFloatTestCases(true, 0);
296
297 runIntegerTestCases(true, intArray2, 3, 2);
298 runFloatTestCases(true, 3);
299
300 runNegativeIndexTests(true);
301 }
302
303 function TestSetters() {
304 runIntegerTestCases(false, initialArray, 0, 16);
305 runFloatTestCases(false);
306
307 runIntegerTestCases(false, initialArray, 3, 2);
308 runFloatTestCases(false, 7);
309
310 runNegativeIndexTests(false);
311 }
312
313 TestGetters();
314 TestSetters();
315
316 function CheckOutOfRangeInt8(value, expected) {
317 var view = new DataView(new ArrayBuffer(100));
318 assertSame(undefined, view.setInt8(0, value));
319 assertSame(expected, view.getInt8(0));
320 assertSame(undefined, view.setInt8(0, value, true));
321 assertSame(expected, view.getInt8(0, true));
322 }
323
324 function CheckOutOfRangeUint8(value, expected) {
325 var view = new DataView(new ArrayBuffer(100));
326 assertSame(undefined, view.setUint8(0, value));
327 assertSame(expected, view.getUint8(0));
328 assertSame(undefined, view.setUint8(0, value, true));
329 assertSame(expected, view.getUint8(0, true));
330 }
331
332 function CheckOutOfRangeInt16(value, expected) {
333 var view = new DataView(new ArrayBuffer(100));
334 assertSame(undefined, view.setInt16(0, value));
335 assertSame(expected, view.getInt16(0));
336 assertSame(undefined, view.setInt16(0, value, true));
337 assertSame(expected, view.getInt16(0, true));
338 }
339
340 function CheckOutOfRangeUint16(value, expected) {
341 var view = new DataView(new ArrayBuffer(100));
342 assertSame(undefined, view.setUint16(0, value));
343 assertSame(expected, view.getUint16(0));
344 assertSame(undefined, view.setUint16(0, value, true));
345 assertSame(expected, view.getUint16(0, true));
346 }
347
348 function CheckOutOfRangeInt32(value, expected) {
349 var view = new DataView(new ArrayBuffer(100));
350 assertSame(undefined, view.setInt32(0, value));
351 assertSame(expected, view.getInt32(0));
352 assertSame(undefined, view.setInt32(0, value, true));
353 assertSame(expected, view.getInt32(0, true));
354 }
355
356 function CheckOutOfRangeUint32(value, expected) {
357 var view = new DataView(new ArrayBuffer(100));
358 assertSame(undefined, view.setUint32(0, value));
359 assertSame(expected, view.getUint32(0));
360 assertSame(undefined, view.setUint32(0, value, true));
361 assertSame(expected, view.getUint32(0, true));
362 }
363
364 function TestOutOfRange() {
365 CheckOutOfRangeInt8(0x80, -0x80);
366 CheckOutOfRangeInt8(0x1000, 0);
367 CheckOutOfRangeInt8(-0x81, 0x7F);
368
369 CheckOutOfRangeUint8(0x100, 0);
370 CheckOutOfRangeUint8(0x1000, 0);
371 CheckOutOfRangeUint8(-0x80, 0x80);
372 CheckOutOfRangeUint8(-1, 0xFF);
373 CheckOutOfRangeUint8(-0xFF, 1);
374
375 CheckOutOfRangeInt16(0x8000, -0x8000);
376 CheckOutOfRangeInt16(0x10000, 0);
377 CheckOutOfRangeInt16(-0x8001, 0x7FFF);
378
379 CheckOutOfRangeUint16(0x10000, 0);
380 CheckOutOfRangeUint16(0x100000, 0);
381 CheckOutOfRangeUint16(-0x8000, 0x8000);
382 CheckOutOfRangeUint16(-1, 0xFFFF);
383 CheckOutOfRangeUint16(-0xFFFF, 1);
384
385 CheckOutOfRangeInt32(0x80000000, -0x80000000);
386 CheckOutOfRangeInt32(0x100000000, 0);
387 CheckOutOfRangeInt32(-0x80000001, 0x7FFFFFFF);
388
389 CheckOutOfRangeUint32(0x100000000, 0);
390 CheckOutOfRangeUint32(0x1000000000, 0);
391 CheckOutOfRangeUint32(-0x80000000, 0x80000000);
392 CheckOutOfRangeUint32(-1, 0xFFFFFFFF);
393 CheckOutOfRangeUint32(-0xFFFFFFFF, 1);
394 }
395
396 TestOutOfRange();
397
398 function TestGeneralAccessors() {
399 var a = new DataView(new ArrayBuffer(256));
400 function CheckAccessor(name) {
401 var f = a[name];
402 assertThrows(function() { f(); }, TypeError);
403 f.call(a, 0, 0); // should not throw
404 assertThrows(function() { f.call({}, 0, 0); }, TypeError);
405 assertThrows(function() { f.call(a); }, TypeError);
406 if (name.indexOf("set") == 0) {
407 assertThrows(function() { f.call(a, 1); }, TypeError);
408 } else {
409 f.call(a, 1); // should not throw
410 }
411 }
412 CheckAccessor("getUint8");
413 CheckAccessor("setUint8");
414 CheckAccessor("getInt8");
415 CheckAccessor("setInt8");
416 CheckAccessor("getUint16");
417 CheckAccessor("setUint16");
418 CheckAccessor("getInt16");
419 CheckAccessor("setInt16");
420 CheckAccessor("getUint32");
421 CheckAccessor("setUint32");
422 CheckAccessor("getInt32");
423 CheckAccessor("setInt32");
424 CheckAccessor("getFloat32");
425 CheckAccessor("setFloat32");
426 CheckAccessor("getFloat64");
427 CheckAccessor("setFloat64");
428 }
429
430 TestGeneralAccessors();
431
432 function TestInsufficientArguments() {
433 var a = new DataView(new ArrayBuffer(256));
434
435 assertThrows(function() { a.getUint8(); }, TypeError);
436 assertThrows(function() { a.getInt8(); }, TypeError);
437 assertThrows(function() { a.getUint16(); }, TypeError);
438 assertThrows(function() { a.getInt16(); }, TypeError);
439 assertThrows(function() { a.getUint32(); }, TypeError);
440 assertThrows(function() { a.getInt32(); }, TypeError);
441 assertThrows(function() { a.getFloat32(); }, TypeError);
442 assertThrows(function() { a.getFloat64(); }, TypeError);
443
444 assertThrows(function() { a.setUint8(); }, TypeError);
445 assertThrows(function() { a.setInt8(); }, TypeError);
446 assertThrows(function() { a.setUint16(); }, TypeError);
447 assertThrows(function() { a.setInt16(); }, TypeError);
448 assertThrows(function() { a.setUint32(); }, TypeError);
449 assertThrows(function() { a.setInt32(); }, TypeError);
450 assertThrows(function() { a.setFloat32(); }, TypeError);
451 assertThrows(function() { a.setFloat64(); }, TypeError);
452
453 assertThrows(function() { a.setUint8(1) }, TypeError);
454 assertThrows(function() { a.setInt8(1) }, TypeError);
455 assertThrows(function() { a.setUint16(1) }, TypeError);
456 assertThrows(function() { a.setInt16(1) }, TypeError);
457 assertThrows(function() { a.setUint32(1) }, TypeError);
458 assertThrows(function() { a.setInt32(1) }, TypeError);
459 assertThrows(function() { a.setFloat32(1) }, TypeError);
460 assertThrows(function() { a.setFloat64(1) }, TypeError);
461 }
462
463 TestInsufficientArguments();
464
465 function TestOffsets(func, val) {
466 var a = new ArrayBuffer(32);
467 var view = new DataView(a);
468 var sf = view["set" + func];
469 sf.call(view, 0, val);
470
471 var v1 = new DataView(a, 16, 16);
472 var gf = v1["get" + func];
473 var result = gf.call(v1, 0);
474 assertEquals(0, result);
475
476 }
477
478 TestOffsets("Int8", 0xff);
479 TestOffsets("Int16", 0xffff);
480 TestOffsets("Int32", 0xffffffff);
481 TestOffsets("Int8", -1);
482 TestOffsets("Int16", -12);
483 TestOffsets("Int32", -1234);
484 TestOffsets("Uint8", 0xff);
485 TestOffsets("Uint16", 0xffff);
486 TestOffsets("Uint32", 0xffffffff);
487 TestOffsets("Float32", 3.14);
488 TestOffsets("Float64", 3.14159);
489
490 "success";