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// These tests are adapted from Khronos DataView tests
29
30var intArray1 =
31  [0, 1, 2, 3, 100, 101, 102, 103, 128, 129, 130, 131, 252, 253, 254, 255];
32var intArray2 =
33  [31, 32, 33, 0, 1, 2, 3, 100, 101, 102, 103, 128, 129, 130, 131, 252,
34    253, 254, 255];
35var initialArray =
36  [204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204,
37    204, 204, 204];
38
39var arayBuffer = null;
40var view = null;
41var viewStart = 0;
42var viewLength = 0;
43
44function getElementSize(func) {
45  switch (func) {
46    case "Int8":
47    case "Uint8":
48      return 1;
49    case "Int16":
50    case "Uint16":
51      return 2;
52    case "Int32":
53    case "Uint32":
54    case "Float32":
55      return 4;
56    case "Float64":
57      return 8;
58    default:
59      assertUnreachable(func);
60  }
61}
62
63function checkGet(func, index, expected, littleEndian) {
64  function doGet() {
65    if (littleEndian != undefined)
66      return view["get" + func](index, littleEndian);
67    else
68      return view["get" + func](index);
69  }
70  if (index >=0 && index + getElementSize(func) - 1 < view.byteLength)
71      assertSame(expected, doGet());
72  else
73      assertThrows(doGet, RangeError);
74}
75
76function checkSet(func, index, value, littleEndian) {
77  function doSet() {
78    if (littleEndian != undefined)
79      view["set" + func](index, value, littleEndian);
80    else
81      view["set" + func](index, value);
82  }
83  if (index >= 0 &&
84      index + getElementSize(func) - 1 < view.byteLength) {
85    assertSame(undefined, doSet());
86    checkGet(func, index, value, littleEndian);
87  } else {
88    assertThrows(doSet, RangeError);
89  }
90}
91
92function test(isTestingGet, func, index, value, littleEndian) {
93  if (isTestingGet)
94    checkGet(func, index, value, littleEndian);
95  else
96    checkSet(func, index, value, littleEndian);
97}
98
99function createDataView(
100    array, frontPaddingNum, littleEndian, start, length) {
101  if (!littleEndian)
102    array.reverse();
103  var paddingArray = new Array(frontPaddingNum);
104  arrayBuffer = (new Uint8Array(paddingArray.concat(array))).buffer;
105  view = new DataView(arrayBuffer, viewStart, viewLength);
106  if (!littleEndian)
107    array.reverse(); // restore the array.
108}
109
110function runIntegerTestCases(isTestingGet, array, start, length) {
111  createDataView(array, 0, true, start, length);
112
113  test(isTestingGet, "Int8", 0, 0);
114  test(isTestingGet, "Int8", undefined, 0);
115  test(isTestingGet, "Int8", 8, -128);
116  test(isTestingGet, "Int8", 15, -1);
117
118  test(isTestingGet, "Uint8", 0, 0);
119  test(isTestingGet, "Uint8", undefined, 0);
120  test(isTestingGet, "Uint8", 8, 128);
121  test(isTestingGet, "Uint8", 15, 255);
122
123  // Little endian.
124  test(isTestingGet, "Int16", 0, 256, true);
125  test(isTestingGet, "Int16", undefined, 256, true);
126  test(isTestingGet, "Int16", 5, 26213, true);
127  test(isTestingGet, "Int16", 9, -32127, true);
128  test(isTestingGet, "Int16", 14, -2, true);
129
130  // Big endian.
131  test(isTestingGet, "Int16", 0, 1);
132  test(isTestingGet, "Int16", undefined, 1);
133  test(isTestingGet, "Int16", 5, 25958);
134  test(isTestingGet, "Int16", 9, -32382);
135  test(isTestingGet, "Int16", 14, -257);
136
137  // Little endian.
138  test(isTestingGet, "Uint16", 0, 256, true);
139  test(isTestingGet, "Uint16", undefined, 256, true);
140  test(isTestingGet, "Uint16", 5, 26213, true);
141  test(isTestingGet, "Uint16", 9, 33409, true);
142  test(isTestingGet, "Uint16", 14, 65534, true);
143
144  // Big endian.
145  test(isTestingGet, "Uint16", 0, 1);
146  test(isTestingGet, "Uint16", undefined, 1);
147  test(isTestingGet, "Uint16", 5, 25958);
148  test(isTestingGet, "Uint16", 9, 33154);
149  test(isTestingGet, "Uint16", 14, 65279);
150
151  // Little endian.
152  test(isTestingGet, "Int32", 0, 50462976, true);
153  test(isTestingGet, "Int32", undefined, 50462976, true);
154  test(isTestingGet, "Int32", 3, 1717920771, true);
155  test(isTestingGet, "Int32", 6, -2122291354, true);
156  test(isTestingGet, "Int32", 9, -58490239, true);
157  test(isTestingGet, "Int32", 12,-66052, true);
158
159  // Big endian.
160  test(isTestingGet, "Int32", 0, 66051);
161  test(isTestingGet, "Int32", undefined, 66051);
162  test(isTestingGet, "Int32", 3, 56911206);
163  test(isTestingGet, "Int32", 6, 1718059137);
164  test(isTestingGet, "Int32", 9, -2122152964);
165  test(isTestingGet, "Int32", 12, -50462977);
166
167  // Little endian.
168  test(isTestingGet, "Uint32", 0, 50462976, true);
169  test(isTestingGet, "Uint32", undefined, 50462976, true);
170  test(isTestingGet, "Uint32", 3, 1717920771, true);
171  test(isTestingGet, "Uint32", 6, 2172675942, true);
172  test(isTestingGet, "Uint32", 9, 4236477057, true);
173  test(isTestingGet, "Uint32", 12,4294901244, true);
174
175  // Big endian.
176  test(isTestingGet, "Uint32", 0, 66051);
177  test(isTestingGet, "Uint32", undefined, 66051);
178  test(isTestingGet, "Uint32", 3, 56911206);
179  test(isTestingGet, "Uint32", 6, 1718059137);
180  test(isTestingGet, "Uint32", 9, 2172814332);
181  test(isTestingGet, "Uint32", 12, 4244504319);
182}
183
184function testFloat(isTestingGet, func, array, start, expected) {
185  // Little endian.
186  createDataView(array, 0, true, start);
187  test(isTestingGet, func, 0, expected, true);
188  test(isTestingGet, func, undefined, expected, true);
189  createDataView(array, 3, true, start);
190  test(isTestingGet, func, 3, expected, true);
191  createDataView(array, 7, true, start);
192  test(isTestingGet, func, 7, expected, true);
193  createDataView(array, 10, true, start);
194  test(isTestingGet, func, 10, expected, true);
195
196  // Big endian.
197  createDataView(array, 0, false);
198  test(isTestingGet, func, 0, expected, false);
199  test(isTestingGet, func, undefined, expected, false);
200  createDataView(array, 3, false);
201  test(isTestingGet, func, 3, expected, false);
202  createDataView(array, 7, false);
203  test(isTestingGet, func, 7, expected, false);
204  createDataView(array, 10, false);
205  test(isTestingGet, func, 10, expected, false);
206}
207
208function runFloatTestCases(isTestingGet, start) {
209  testFloat(isTestingGet, "Float32",
210    isTestingGet ? [0, 0, 32, 65] : initialArray, start, 10);
211
212  testFloat(isTestingGet, "Float32",
213    isTestingGet ? [164, 112, 157, 63] : initialArray,
214      start, 1.2300000190734863);
215  testFloat(isTestingGet, "Float32",
216    isTestingGet ? [95, 53, 50, 199] : initialArray,
217    start, -45621.37109375);
218  testFloat(isTestingGet, "Float32",
219    isTestingGet ? [255, 255, 255, 127] : initialArray,
220    start, NaN);
221  testFloat(isTestingGet, "Float32",
222    isTestingGet ? [255, 255, 255, 255] : initialArray,
223    start, -NaN);
224
225  testFloat(isTestingGet, "Float64",
226    isTestingGet ? [0, 0, 0, 0, 0, 0, 36, 64] : initialArray,
227    start, 10);
228  testFloat(isTestingGet, "Float64",
229    isTestingGet ? [174, 71, 225, 122, 20, 174, 243, 63] : initialArray,
230    start, 1.23);
231  testFloat(isTestingGet, "Float64",
232    isTestingGet ? [181, 55, 248, 30, 242, 179, 87, 193] : initialArray,
233    start, -6213576.4839);
234  testFloat(isTestingGet, "Float64",
235    isTestingGet ? [255, 255, 255, 255, 255, 255, 255, 127] : initialArray,
236    start, NaN);
237  testFloat(isTestingGet, "Float64",
238    isTestingGet ? [255, 255, 255, 255, 255, 255, 255, 255] : initialArray,
239    start, -NaN);
240}
241
242function runNegativeIndexTests(isTestingGet) {
243  createDataView(intArray1, 0, true, 0, 16);
244
245  test(isTestingGet, "Int8", -1, 0);
246  test(isTestingGet, "Int8", -2, 0);
247
248  test(isTestingGet, "Uint8", -1, 0);
249  test(isTestingGet, "Uint8", -2, 0);
250
251  test(isTestingGet, "Int16", -1, 1);
252  test(isTestingGet, "Int16", -2, 1);
253  test(isTestingGet, "Int16", -3, 1);
254
255  test(isTestingGet, "Uint16", -1, 1);
256  test(isTestingGet, "Uint16", -2, 1);
257  test(isTestingGet, "Uint16", -3, 1);
258
259  test(isTestingGet, "Int32", -1, 66051);
260  test(isTestingGet, "Int32", -3, 66051);
261  test(isTestingGet, "Int32", -5, 66051);
262
263  test(isTestingGet, "Uint32", -1, 66051);
264  test(isTestingGet, "Uint32", -3, 66051);
265  test(isTestingGet, "Uint32", -5, 66051);
266
267  createDataView([0, 0, 0, 0, 0, 0, 0, 0], 0, true, 0, 8);
268
269  test(isTestingGet, "Float32", -1, 0);
270  test(isTestingGet, "Float32", -3, 0);
271  test(isTestingGet, "Float32", -5, 0);
272
273  test(isTestingGet, "Float64", -1, 0);
274  test(isTestingGet, "Float64", -5, 0);
275  test(isTestingGet, "Float64", -9, 0);
276}
277
278
279function TestGetters() {
280  runIntegerTestCases(true, intArray1, 0, 16);
281  runFloatTestCases(true, 0);
282
283  runIntegerTestCases(true, intArray2, 3, 2);
284  runFloatTestCases(true, 3);
285
286  runNegativeIndexTests(true);
287}
288
289function TestSetters() {
290  runIntegerTestCases(false, initialArray, 0, 16);
291  runFloatTestCases(false);
292
293  runIntegerTestCases(false, initialArray, 3, 2);
294  runFloatTestCases(false, 7);
295
296  runNegativeIndexTests(false);
297}
298
299TestGetters();
300TestSetters();
301
302function CheckOutOfRangeInt8(value, expected) {
303  var view = new DataView(new ArrayBuffer(100));
304  assertSame(undefined, view.setInt8(0, value));
305  assertSame(expected, view.getInt8(0));
306  assertSame(undefined, view.setInt8(0, value, true));
307  assertSame(expected, view.getInt8(0, true));
308}
309
310function CheckOutOfRangeUint8(value, expected) {
311  var view = new DataView(new ArrayBuffer(100));
312  assertSame(undefined, view.setUint8(0, value));
313  assertSame(expected, view.getUint8(0));
314  assertSame(undefined, view.setUint8(0, value, true));
315  assertSame(expected, view.getUint8(0, true));
316}
317
318function CheckOutOfRangeInt16(value, expected) {
319  var view = new DataView(new ArrayBuffer(100));
320  assertSame(undefined, view.setInt16(0, value));
321  assertSame(expected, view.getInt16(0));
322  assertSame(undefined, view.setInt16(0, value, true));
323  assertSame(expected, view.getInt16(0, true));
324}
325
326function CheckOutOfRangeUint16(value, expected) {
327  var view = new DataView(new ArrayBuffer(100));
328  assertSame(undefined, view.setUint16(0, value));
329  assertSame(expected, view.getUint16(0));
330  assertSame(undefined, view.setUint16(0, value, true));
331  assertSame(expected, view.getUint16(0, true));
332}
333
334function CheckOutOfRangeInt32(value, expected) {
335  var view = new DataView(new ArrayBuffer(100));
336  assertSame(undefined, view.setInt32(0, value));
337  assertSame(expected, view.getInt32(0));
338  assertSame(undefined, view.setInt32(0, value, true));
339  assertSame(expected, view.getInt32(0, true));
340}
341
342function CheckOutOfRangeUint32(value, expected) {
343  var view = new DataView(new ArrayBuffer(100));
344  assertSame(undefined, view.setUint32(0, value));
345  assertSame(expected, view.getUint32(0));
346  assertSame(undefined, view.setUint32(0, value, true));
347  assertSame(expected, view.getUint32(0, true));
348}
349
350function TestOutOfRange() {
351  CheckOutOfRangeInt8(0x80,   -0x80);
352  CheckOutOfRangeInt8(0x1000, 0);
353  CheckOutOfRangeInt8(-0x81,  0x7F);
354
355  CheckOutOfRangeUint8(0x100,  0);
356  CheckOutOfRangeUint8(0x1000, 0);
357  CheckOutOfRangeUint8(-0x80,  0x80);
358  CheckOutOfRangeUint8(-1,     0xFF);
359  CheckOutOfRangeUint8(-0xFF,  1);
360
361  CheckOutOfRangeInt16(0x8000,  -0x8000);
362  CheckOutOfRangeInt16(0x10000, 0);
363  CheckOutOfRangeInt16(-0x8001, 0x7FFF);
364
365  CheckOutOfRangeUint16(0x10000,  0);
366  CheckOutOfRangeUint16(0x100000, 0);
367  CheckOutOfRangeUint16(-0x8000,  0x8000);
368  CheckOutOfRangeUint16(-1,       0xFFFF);
369  CheckOutOfRangeUint16(-0xFFFF,  1);
370
371  CheckOutOfRangeInt32(0x80000000,  -0x80000000);
372  CheckOutOfRangeInt32(0x100000000, 0);
373  CheckOutOfRangeInt32(-0x80000001, 0x7FFFFFFF);
374
375  CheckOutOfRangeUint32(0x100000000,  0);
376  CheckOutOfRangeUint32(0x1000000000, 0);
377  CheckOutOfRangeUint32(-0x80000000,  0x80000000);
378  CheckOutOfRangeUint32(-1,           0xFFFFFFFF);
379  CheckOutOfRangeUint32(-0xFFFFFFFF,  1);
380}
381
382TestOutOfRange();
383
384function TestGeneralAccessors() {
385  var a = new DataView(new ArrayBuffer(256));
386  function CheckAccessor(name) {
387    var f = a[name];
388    assertThrows(function() { f(); }, TypeError);
389    f.call(a, 0, 0); // should not throw
390    assertThrows(function() { f.call({}, 0, 0); }, TypeError);
391    assertThrows(function() { f.call(a); }, TypeError);
392    if (name.indexOf("set") == 0) {
393      assertThrows(function() { f.call(a, 1); }, TypeError);
394    } else {
395      f.call(a, 1); // should not throw
396    }
397  }
398  CheckAccessor("getUint8");
399  CheckAccessor("setUint8");
400  CheckAccessor("getInt8");
401  CheckAccessor("setInt8");
402  CheckAccessor("getUint16");
403  CheckAccessor("setUint16");
404  CheckAccessor("getInt16");
405  CheckAccessor("setInt16");
406  CheckAccessor("getUint32");
407  CheckAccessor("setUint32");
408  CheckAccessor("getInt32");
409  CheckAccessor("setInt32");
410  CheckAccessor("getFloat32");
411  CheckAccessor("setFloat32");
412  CheckAccessor("getFloat64");
413  CheckAccessor("setFloat64");
414}
415
416TestGeneralAccessors();
417
418function TestInsufficientArguments() {
419  var a = new DataView(new ArrayBuffer(256));
420
421  assertThrows(function() { a.getUint8(); }, TypeError);
422  assertThrows(function() { a.getInt8(); }, TypeError);
423  assertThrows(function() { a.getUint16(); }, TypeError);
424  assertThrows(function() { a.getInt16(); }, TypeError);
425  assertThrows(function() { a.getUint32(); }, TypeError);
426  assertThrows(function() { a.getInt32(); }, TypeError);
427  assertThrows(function() { a.getFloat32(); }, TypeError);
428  assertThrows(function() { a.getFloat64(); }, TypeError);
429
430  assertThrows(function() { a.setUint8(); }, TypeError);
431  assertThrows(function() { a.setInt8(); }, TypeError);
432  assertThrows(function() { a.setUint16(); }, TypeError);
433  assertThrows(function() { a.setInt16(); }, TypeError);
434  assertThrows(function() { a.setUint32(); }, TypeError);
435  assertThrows(function() { a.setInt32(); }, TypeError);
436  assertThrows(function() { a.setFloat32(); }, TypeError);
437  assertThrows(function() { a.setFloat64(); }, TypeError);
438
439  assertThrows(function() { a.setUint8(1) }, TypeError);
440  assertThrows(function() { a.setInt8(1) }, TypeError);
441  assertThrows(function() { a.setUint16(1) }, TypeError);
442  assertThrows(function() { a.setInt16(1) }, TypeError);
443  assertThrows(function() { a.setUint32(1) }, TypeError);
444  assertThrows(function() { a.setInt32(1) }, TypeError);
445  assertThrows(function() { a.setFloat32(1) }, TypeError);
446  assertThrows(function() { a.setFloat64(1) }, TypeError);
447}
448
449TestInsufficientArguments();
450