deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / test / mjsunit / harmony / typedarrays.js
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 // Flags: --harmony-tostring
29
30 // ArrayBuffer
31
32 function TestByteLength(param, expectedByteLength) {
33   var ab = new ArrayBuffer(param);
34   assertSame(expectedByteLength, ab.byteLength);
35 }
36
37 function TestArrayBufferCreation() {
38   TestByteLength(1, 1);
39   TestByteLength(256, 256);
40   TestByteLength(2.567, 2);
41
42   TestByteLength("abc", 0);
43
44   TestByteLength(0, 0);
45
46   assertThrows(function() { new ArrayBuffer(-10); }, RangeError);
47   assertThrows(function() { new ArrayBuffer(-2.567); }, RangeError);
48
49 /* TODO[dslomov]: Reenable the test
50   assertThrows(function() {
51     var ab1 = new ArrayBuffer(0xFFFFFFFFFFFF)
52   }, RangeError);
53 */
54
55   var ab = new ArrayBuffer();
56   assertSame(0, ab.byteLength);
57   assertEquals("[object ArrayBuffer]",
58       Object.prototype.toString.call(ab));
59 }
60
61 TestArrayBufferCreation();
62
63 function TestByteLengthNotWritable() {
64   var ab = new ArrayBuffer(1024);
65   assertSame(1024, ab.byteLength);
66
67   assertThrows(function() { "use strict"; ab.byteLength = 42; }, TypeError);
68 }
69
70 TestByteLengthNotWritable();
71
72 function TestSlice(expectedResultLen, initialLen, start, end) {
73   var ab = new ArrayBuffer(initialLen);
74   var a1 = new Uint8Array(ab);
75   for (var i = 0; i < a1.length; i++) {
76     a1[i] = 0xCA;
77   }
78   var slice = ab.slice(start, end);
79   assertSame(expectedResultLen, slice.byteLength);
80   var a2 = new Uint8Array(slice);
81   for (var i = 0; i < a2.length; i++) {
82     assertSame(0xCA, a2[i]);
83   }
84 }
85
86 function TestArrayBufferSlice() {
87   var ab = new ArrayBuffer(1024);
88   var ab1 = ab.slice(512, 1024);
89   assertSame(512, ab1.byteLength);
90
91   TestSlice(512, 1024, 512, 1024);
92   TestSlice(512, 1024, 512);
93
94   TestSlice(0, 0, 1, 20);
95   TestSlice(100, 100, 0, 100);
96   TestSlice(100, 100, 0, 1000);
97
98   TestSlice(0, 100, 5, 1);
99
100   TestSlice(1, 100, -11, -10);
101   TestSlice(9, 100, -10, 99);
102   TestSlice(0, 100, -10, 80);
103   TestSlice(10, 100, 80, -10);
104
105   TestSlice(10, 100, 90, "100");
106   TestSlice(10, 100, "90", "100");
107
108   TestSlice(0,  100, 90, "abc");
109   TestSlice(10, 100, "abc", 10);
110
111   TestSlice(10, 100, 0.96, 10.96);
112   TestSlice(10, 100, 0.96, 10.01);
113   TestSlice(10, 100, 0.01, 10.01);
114   TestSlice(10, 100, 0.01, 10.96);
115
116   TestSlice(10, 100, 90);
117   TestSlice(10, 100, -10);
118 }
119
120 TestArrayBufferSlice();
121
122 // Typed arrays
123
124 function TestTypedArray(constr, elementSize, typicalElement) {
125   assertSame(elementSize, constr.BYTES_PER_ELEMENT);
126
127   var ab = new ArrayBuffer(256*elementSize);
128
129   var a0 = new constr(30);
130   assertEquals("[object " + constr.name + "]",
131       Object.prototype.toString.call(a0));
132
133   assertTrue(ArrayBuffer.isView(a0));
134   assertSame(elementSize, a0.BYTES_PER_ELEMENT);
135   assertSame(30, a0.length);
136   assertSame(30*elementSize, a0.byteLength);
137   assertSame(0, a0.byteOffset);
138   assertSame(30*elementSize, a0.buffer.byteLength);
139
140   var aLen0 = new constr(0);
141   assertSame(elementSize, aLen0.BYTES_PER_ELEMENT);
142   assertSame(0, aLen0.length);
143   assertSame(0, aLen0.byteLength);
144   assertSame(0, aLen0.byteOffset);
145   assertSame(0, aLen0.buffer.byteLength);
146
147   var aOverBufferLen0 = new constr(ab, 128*elementSize, 0);
148   assertSame(ab, aOverBufferLen0.buffer);
149   assertSame(elementSize, aOverBufferLen0.BYTES_PER_ELEMENT);
150   assertSame(0, aOverBufferLen0.length);
151   assertSame(0, aOverBufferLen0.byteLength);
152   assertSame(128*elementSize, aOverBufferLen0.byteOffset);
153
154   var a1 = new constr(ab, 128*elementSize, 128);
155   assertSame(ab, a1.buffer);
156   assertSame(elementSize, a1.BYTES_PER_ELEMENT);
157   assertSame(128, a1.length);
158   assertSame(128*elementSize, a1.byteLength);
159   assertSame(128*elementSize, a1.byteOffset);
160
161
162   var a2 = new constr(ab, 64*elementSize, 128);
163   assertSame(ab, a2.buffer);
164   assertSame(elementSize, a2.BYTES_PER_ELEMENT);
165   assertSame(128, a2.length);
166   assertSame(128*elementSize, a2.byteLength);
167   assertSame(64*elementSize, a2.byteOffset);
168
169   var a3 = new constr(ab, 192*elementSize);
170   assertSame(ab, a3.buffer);
171   assertSame(64, a3.length);
172   assertSame(64*elementSize, a3.byteLength);
173   assertSame(192*elementSize, a3.byteOffset);
174
175   var a4 = new constr(ab);
176   assertSame(ab, a4.buffer);
177   assertSame(256, a4.length);
178   assertSame(256*elementSize, a4.byteLength);
179   assertSame(0, a4.byteOffset);
180
181
182   var i;
183   for (i = 0; i < 128; i++) {
184     a1[i] = typicalElement;
185   }
186
187   for (i = 0; i < 128; i++) {
188     assertSame(typicalElement, a1[i]);
189   }
190
191   for (i = 0; i < 64; i++) {
192     assertSame(0, a2[i]);
193   }
194
195   for (i = 64; i < 128; i++) {
196     assertSame(typicalElement, a2[i]);
197   }
198
199   for (i = 0; i < 64; i++) {
200     assertSame(typicalElement, a3[i]);
201   }
202
203   for (i = 0; i < 128; i++) {
204     assertSame(0, a4[i]);
205   }
206
207   for (i = 128; i < 256; i++) {
208     assertSame(typicalElement, a4[i]);
209   }
210
211   var aAtTheEnd = new constr(ab, 256*elementSize);
212   assertSame(elementSize, aAtTheEnd.BYTES_PER_ELEMENT);
213   assertSame(0, aAtTheEnd.length);
214   assertSame(0, aAtTheEnd.byteLength);
215   assertSame(256*elementSize, aAtTheEnd.byteOffset);
216
217   assertThrows(function () { new constr(ab, 257*elementSize); }, RangeError);
218   assertThrows(
219       function () { new constr(ab, 128*elementSize, 192); },
220       RangeError);
221
222   if (elementSize !== 1) {
223     assertThrows(function() { new constr(ab, 128*elementSize - 1, 10); },
224                  RangeError);
225     var unalignedArrayBuffer = new ArrayBuffer(10*elementSize + 1);
226     var goodArray = new constr(unalignedArrayBuffer, 0, 10);
227     assertSame(10, goodArray.length);
228     assertSame(10*elementSize, goodArray.byteLength);
229     assertThrows(function() { new constr(unalignedArrayBuffer)}, RangeError);
230     assertThrows(function() { new constr(unalignedArrayBuffer, 5*elementSize)},
231                  RangeError);
232   }
233
234   var aFromString = new constr("30");
235   assertSame(elementSize, aFromString.BYTES_PER_ELEMENT);
236   assertSame(30, aFromString.length);
237   assertSame(30*elementSize, aFromString.byteLength);
238   assertSame(0, aFromString.byteOffset);
239   assertSame(30*elementSize, aFromString.buffer.byteLength);
240
241   var jsArray = [];
242   for (i = 0; i < 30; i++) {
243     jsArray.push(typicalElement);
244   }
245   var aFromArray = new constr(jsArray);
246   assertSame(elementSize, aFromArray.BYTES_PER_ELEMENT);
247   assertSame(30, aFromArray.length);
248   assertSame(30*elementSize, aFromArray.byteLength);
249   assertSame(0, aFromArray.byteOffset);
250   assertSame(30*elementSize, aFromArray.buffer.byteLength);
251   for (i = 0; i < 30; i++) {
252     assertSame(typicalElement, aFromArray[i]);
253   }
254
255   var abLen0 = new ArrayBuffer(0);
256   var aOverAbLen0 = new constr(abLen0);
257   assertSame(abLen0, aOverAbLen0.buffer);
258   assertSame(elementSize, aOverAbLen0.BYTES_PER_ELEMENT);
259   assertSame(0, aOverAbLen0.length);
260   assertSame(0, aOverAbLen0.byteLength);
261   assertSame(0, aOverAbLen0.byteOffset);
262
263   var aNoParam = new constr();
264   assertSame(elementSize, aNoParam.BYTES_PER_ELEMENT);
265   assertSame(0, aNoParam.length);
266   assertSame(0, aNoParam.byteLength);
267   assertSame(0, aNoParam.byteOffset);
268
269   var a = new constr(ab, 64*elementSize, 128);
270   assertEquals("[object " + constr.name + "]",
271       Object.prototype.toString.call(a));
272   var desc = Object.getOwnPropertyDescriptor(
273       constr.prototype, Symbol.toStringTag);
274   assertTrue(desc.configurable);
275   assertFalse(desc.enumerable);
276   assertFalse(!!desc.writable);
277   assertFalse(!!desc.set);
278   assertEquals("function", typeof desc.get);
279 }
280
281 TestTypedArray(Uint8Array, 1, 0xFF);
282 TestTypedArray(Int8Array, 1, -0x7F);
283 TestTypedArray(Uint16Array, 2, 0xFFFF);
284 TestTypedArray(Int16Array, 2, -0x7FFF);
285 TestTypedArray(Uint32Array, 4, 0xFFFFFFFF);
286 TestTypedArray(Int32Array, 4, -0x7FFFFFFF);
287 TestTypedArray(Float32Array, 4, 0.5);
288 TestTypedArray(Float64Array, 8, 0.5);
289 TestTypedArray(Uint8ClampedArray, 1, 0xFF);
290
291 function SubarrayTestCase(constructor, item, expectedResultLen, expectedStartIndex,
292                           initialLen, start, end) {
293   var a = new constructor(initialLen);
294   var s = a.subarray(start, end);
295   assertSame(constructor, s.constructor);
296   assertSame(expectedResultLen, s.length);
297   if (s.length > 0) {
298     s[0] = item;
299     assertSame(item, a[expectedStartIndex]);
300   }
301 }
302
303 function TestSubArray(constructor, item) {
304   SubarrayTestCase(constructor, item, 512, 512, 1024, 512, 1024);
305   SubarrayTestCase(constructor, item, 512, 512, 1024, 512);
306
307   SubarrayTestCase(constructor, item, 0, undefined, 0, 1, 20);
308   SubarrayTestCase(constructor, item, 100, 0,       100, 0, 100);
309   SubarrayTestCase(constructor, item, 100, 0,       100,  0, 1000);
310   SubarrayTestCase(constructor, item, 0, undefined, 100, 5, 1);
311
312   SubarrayTestCase(constructor, item, 1, 89,        100, -11, -10);
313   SubarrayTestCase(constructor, item, 9, 90,        100, -10, 99);
314   SubarrayTestCase(constructor, item, 0, undefined, 100, -10, 80);
315   SubarrayTestCase(constructor, item, 10,80,        100, 80, -10);
316
317   SubarrayTestCase(constructor, item, 10,90,        100, 90, "100");
318   SubarrayTestCase(constructor, item, 10,90,        100, "90", "100");
319
320   SubarrayTestCase(constructor, item, 0, undefined, 100, 90, "abc");
321   SubarrayTestCase(constructor, item, 10,0,         100, "abc", 10);
322
323   SubarrayTestCase(constructor, item, 10,0,         100, 0.96, 10.96);
324   SubarrayTestCase(constructor, item, 10,0,         100, 0.96, 10.01);
325   SubarrayTestCase(constructor, item, 10,0,         100, 0.01, 10.01);
326   SubarrayTestCase(constructor, item, 10,0,         100, 0.01, 10.96);
327
328
329   SubarrayTestCase(constructor, item, 10,90,        100, 90);
330   SubarrayTestCase(constructor, item, 10,90,        100, -10);
331
332   var method = constructor.prototype.subarray;
333   method.call(new constructor(100), 0, 100);
334   var o = {};
335   assertThrows(function() { method.call(o, 0, 100); }, TypeError);
336 }
337
338 TestSubArray(Uint8Array, 0xFF);
339 TestSubArray(Int8Array, -0x7F);
340 TestSubArray(Uint16Array, 0xFFFF);
341 TestSubArray(Int16Array, -0x7FFF);
342 TestSubArray(Uint32Array, 0xFFFFFFFF);
343 TestSubArray(Int32Array, -0x7FFFFFFF);
344 TestSubArray(Float32Array, 0.5);
345 TestSubArray(Float64Array, 0.5);
346 TestSubArray(Uint8ClampedArray, 0xFF);
347
348 function TestTypedArrayOutOfRange(constructor, value, result) {
349   var a = new constructor(1);
350   a[0] = value;
351   assertSame(result, a[0]);
352 }
353
354 TestTypedArrayOutOfRange(Uint8Array, 0x1FA, 0xFA);
355 TestTypedArrayOutOfRange(Uint8Array, -1, 0xFF);
356
357 TestTypedArrayOutOfRange(Int8Array, 0x1FA, 0x7A - 0x80);
358
359 TestTypedArrayOutOfRange(Uint16Array, 0x1FFFA, 0xFFFA);
360 TestTypedArrayOutOfRange(Uint16Array, -1, 0xFFFF);
361 TestTypedArrayOutOfRange(Int16Array, 0x1FFFA, 0x7FFA - 0x8000);
362
363 TestTypedArrayOutOfRange(Uint32Array, 0x1FFFFFFFA, 0xFFFFFFFA);
364 TestTypedArrayOutOfRange(Uint32Array, -1, 0xFFFFFFFF);
365 TestTypedArrayOutOfRange(Int32Array, 0x1FFFFFFFA, 0x7FFFFFFA - 0x80000000);
366
367 TestTypedArrayOutOfRange(Uint8ClampedArray, 0x1FA, 0xFF);
368 TestTypedArrayOutOfRange(Uint8ClampedArray, -1, 0);
369
370 var typedArrayConstructors = [
371   Uint8Array,
372   Int8Array,
373   Uint16Array,
374   Int16Array,
375   Uint32Array,
376   Int32Array,
377   Uint8ClampedArray,
378   Float32Array,
379   Float64Array];
380
381 function TestPropertyTypeChecks(constructor) {
382   function CheckProperty(name) {
383     var d = Object.getOwnPropertyDescriptor(constructor.prototype, name);
384     var o = {};
385     assertThrows(function() {d.get.call(o);}, TypeError);
386     for (var i = 0; i < typedArrayConstructors.length; i++) {
387       var ctor = typedArrayConstructors[i];
388       var a = new ctor(10);
389       if (ctor === constructor) {
390         d.get.call(a); // shouldn't throw
391       } else {
392         assertThrows(function() {d.get.call(a);}, TypeError);
393       }
394     }
395   }
396
397   CheckProperty("buffer");
398   CheckProperty("byteOffset");
399   CheckProperty("byteLength");
400   CheckProperty("length");
401 }
402
403 for(i = 0; i < typedArrayConstructors.length; i++) {
404   TestPropertyTypeChecks(typedArrayConstructors[i]);
405 }
406
407
408 function TestTypedArraySet() {
409   // Test array.set in different combinations.
410
411   function assertArrayPrefix(expected, array) {
412     for (var i = 0; i < expected.length; ++i) {
413       assertEquals(expected[i], array[i]);
414     }
415   }
416
417   var a11 = new Int16Array([1, 2, 3, 4, 0, -1])
418   var a12 = new Uint16Array(15)
419   a12.set(a11, 3)
420   assertArrayPrefix([0, 0, 0, 1, 2, 3, 4, 0, 0xffff, 0, 0], a12)
421   assertThrows(function(){ a11.set(a12) })
422
423   var a21 = [1, undefined, 10, NaN, 0, -1, {valueOf: function() {return 3}}]
424   var a22 = new Int32Array(12)
425   a22.set(a21, 2)
426   assertArrayPrefix([0, 0, 1, 0, 10, 0, 0, -1, 3, 0], a22)
427
428   var a31 = new Float32Array([2, 4, 6, 8, 11, NaN, 1/0, -3])
429   var a32 = a31.subarray(2, 6)
430   a31.set(a32, 4)
431   assertArrayPrefix([2, 4, 6, 8, 6, 8, 11, NaN], a31)
432   assertArrayPrefix([6, 8, 6, 8], a32)
433
434   var a4 = new Uint8ClampedArray([3,2,5,6])
435   a4.set(a4)
436   assertArrayPrefix([3, 2, 5, 6], a4)
437
438   // Cases with overlapping backing store but different element sizes.
439   var b = new ArrayBuffer(4)
440   var a5 = new Int16Array(b)
441   var a50 = new Int8Array(b)
442   var a51 = new Int8Array(b, 0, 2)
443   var a52 = new Int8Array(b, 1, 2)
444   var a53 = new Int8Array(b, 2, 2)
445
446   a5.set([0x5050, 0x0a0a])
447   assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50)
448   assertArrayPrefix([0x50, 0x50], a51)
449   assertArrayPrefix([0x50, 0x0a], a52)
450   assertArrayPrefix([0x0a, 0x0a], a53)
451
452   a50.set([0x50, 0x50, 0x0a, 0x0a])
453   a51.set(a5)
454   assertArrayPrefix([0x50, 0x0a, 0x0a, 0x0a], a50)
455
456   a50.set([0x50, 0x50, 0x0a, 0x0a])
457   a52.set(a5)
458   assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50)
459
460   a50.set([0x50, 0x50, 0x0a, 0x0a])
461   a53.set(a5)
462   assertArrayPrefix([0x50, 0x50, 0x50, 0x0a], a50)
463
464   a50.set([0x50, 0x51, 0x0a, 0x0b])
465   a5.set(a51)
466   assertArrayPrefix([0x0050, 0x0051], a5)
467
468   a50.set([0x50, 0x51, 0x0a, 0x0b])
469   a5.set(a52)
470   assertArrayPrefix([0x0051, 0x000a], a5)
471
472   a50.set([0x50, 0x51, 0x0a, 0x0b])
473   a5.set(a53)
474   assertArrayPrefix([0x000a, 0x000b], a5)
475
476   // Mixed types of same size.
477   var a61 = new Float32Array([1.2, 12.3])
478   var a62 = new Int32Array(2)
479   a62.set(a61)
480   assertArrayPrefix([1, 12], a62)
481   a61.set(a62)
482   assertArrayPrefix([1, 12], a61)
483
484   // Invalid source
485   var a = new Uint16Array(50);
486   var expected = [];
487   for (i = 0; i < 50; i++) {
488     a[i] = i;
489     expected.push(i);
490   }
491   a.set({});
492   assertArrayPrefix(expected, a);
493   assertThrows(function() { a.set.call({}) }, TypeError);
494   assertThrows(function() { a.set.call([]) }, TypeError);
495
496   assertThrows(function() { a.set(0); }, TypeError);
497   assertThrows(function() { a.set(0, 1); }, TypeError);
498 }
499
500 TestTypedArraySet();
501
502 function TestTypedArraysWithIllegalIndices() {
503   var a = new Int32Array(100);
504
505   a[-10] = 10;
506   assertEquals(undefined, a[-10]);
507   a["-10"] = 10;
508   assertEquals(undefined, a["-10"]);
509
510   var s = "    -10";
511   a[s] = 10;
512   assertEquals(10, a[s]);
513   var s1 = "    -10   ";
514   a[s] = 10;
515   assertEquals(10, a[s]);
516
517   a["-1e2"] = 10;
518   assertEquals(10, a["-1e2"]);
519   assertEquals(undefined, a[-1e2]);
520
521   a["-0"] = 256;
522   var s2 = "     -0";
523   a[s2] = 255;
524   assertEquals(undefined, a["-0"]);
525   assertEquals(255, a[s2]);
526   assertEquals(0, a[-0]);
527
528   /* Chromium bug: 424619
529    * a[-Infinity] = 50;
530    * assertEquals(undefined, a[-Infinity]);
531    */
532   a[1.5] = 10;
533   assertEquals(10, a[1.5]);
534   var nan = Math.sqrt(-1);
535   a[nan] = 5;
536   assertEquals(5, a[nan]);
537
538   var x = 0;
539   var y = -0;
540   assertEquals(Infinity, 1/x);
541   assertEquals(-Infinity, 1/y);
542   a[x] = 5;
543   a[y] = 27;
544   assertEquals(27, a[x]);
545   assertEquals(27, a[y]);
546 }
547
548 TestTypedArraysWithIllegalIndices();
549
550 function TestTypedArraysWithIllegalIndicesStrict() {
551   'use strict';
552   var a = new Int32Array(100);
553
554   a[-10] = 10;
555   assertEquals(undefined, a[-10]);
556   a["-10"] = 10;
557   assertEquals(undefined, a["-10"]);
558
559   var s = "    -10";
560   a[s] = 10;
561   assertEquals(10, a[s]);
562   var s1 = "    -10   ";
563   a[s] = 10;
564   assertEquals(10, a[s]);
565
566   a["-1e2"] = 10;
567   assertEquals(10, a["-1e2"]);
568   assertEquals(undefined, a[-1e2]);
569
570   a["-0"] = 256;
571   var s2 = "     -0";
572   a[s2] = 255;
573   assertEquals(undefined, a["-0"]);
574   assertEquals(255, a[s2]);
575   assertEquals(0, a[-0]);
576
577   /* Chromium bug: 424619
578    * a[-Infinity] = 50;
579    * assertEquals(undefined, a[-Infinity]);
580    */
581   a[1.5] = 10;
582   assertEquals(10, a[1.5]);
583   var nan = Math.sqrt(-1);
584   a[nan] = 5;
585   assertEquals(5, a[nan]);
586
587   var x = 0;
588   var y = -0;
589   assertEquals(Infinity, 1/x);
590   assertEquals(-Infinity, 1/y);
591   a[x] = 5;
592   a[y] = 27;
593   assertEquals(27, a[x]);
594   assertEquals(27, a[y]);
595 }
596
597 TestTypedArraysWithIllegalIndicesStrict();
598
599 // DataView
600 function TestDataViewConstructor() {
601   var ab = new ArrayBuffer(256);
602
603   var d1 = new DataView(ab, 1, 255);
604   assertTrue(ArrayBuffer.isView(d1));
605   assertSame(ab, d1.buffer);
606   assertSame(1, d1.byteOffset);
607   assertSame(255, d1.byteLength);
608
609   var d2 = new DataView(ab, 2);
610   assertSame(ab, d2.buffer);
611   assertSame(2, d2.byteOffset);
612   assertSame(254, d2.byteLength);
613
614   var d3 = new DataView(ab);
615   assertSame(ab, d3.buffer);
616   assertSame(0, d3.byteOffset);
617   assertSame(256, d3.byteLength);
618
619   var d3a = new DataView(ab, 1, 0);
620   assertSame(ab, d3a.buffer);
621   assertSame(1, d3a.byteOffset);
622   assertSame(0, d3a.byteLength);
623
624   var d3b = new DataView(ab, 256, 0);
625   assertSame(ab, d3b.buffer);
626   assertSame(256, d3b.byteOffset);
627   assertSame(0, d3b.byteLength);
628
629   var d3c = new DataView(ab, 256);
630   assertSame(ab, d3c.buffer);
631   assertSame(256, d3c.byteOffset);
632   assertSame(0, d3c.byteLength);
633
634   var d4 = new DataView(ab, 1, 3.1415926);
635   assertSame(ab, d4.buffer);
636   assertSame(1, d4.byteOffset);
637   assertSame(3, d4.byteLength);
638
639
640   // error cases
641   assertThrows(function() { new DataView(ab, -1); }, RangeError);
642   assertThrows(function() { new DataView(ab, 1, -1); }, RangeError);
643   assertThrows(function() { new DataView(); }, TypeError);
644   assertThrows(function() { new DataView([]); }, TypeError);
645   assertThrows(function() { new DataView(ab, 257); }, RangeError);
646   assertThrows(function() { new DataView(ab, 1, 1024); }, RangeError);
647 }
648
649 TestDataViewConstructor();
650
651 function TestDataViewPropertyTypeChecks() {
652   var a = new DataView(new ArrayBuffer(10));
653   function CheckProperty(name) {
654     var d = Object.getOwnPropertyDescriptor(DataView.prototype, name);
655     var o = {}
656     assertThrows(function() {d.get.call(o);}, TypeError);
657     d.get.call(a); // shouldn't throw
658   }
659
660   CheckProperty("buffer");
661   CheckProperty("byteOffset");
662   CheckProperty("byteLength");
663 }
664
665
666 TestDataViewPropertyTypeChecks();
667
668
669 function TestDataViewToStringTag() {
670   var a = new DataView(new ArrayBuffer(10));
671   assertEquals("[object DataView]", Object.prototype.toString.call(a));
672   var desc = Object.getOwnPropertyDescriptor(
673       DataView.prototype, Symbol.toStringTag);
674   assertTrue(desc.configurable);
675   assertFalse(desc.enumerable);
676   assertFalse(desc.writable);
677   assertEquals("DataView", desc.value);
678 }
679
680
681 // General tests for properties
682
683 // Test property attribute [[Enumerable]]
684 function TestEnumerable(func, obj) {
685   function props(x) {
686     var array = [];
687     for (var p in x) array.push(p);
688     return array.sort();
689   }
690   assertArrayEquals([], props(func));
691   assertArrayEquals([], props(func.prototype));
692   if (obj)
693     assertArrayEquals([], props(obj));
694 }
695 TestEnumerable(ArrayBuffer, new ArrayBuffer());
696 for(i = 0; i < typedArrayConstructors.length; i++) {
697   TestEnumerable(typedArrayConstructors[i]);
698 }
699 TestEnumerable(DataView, new DataView(new ArrayBuffer()));
700
701 // Test arbitrary properties on ArrayBuffer
702 function TestArbitrary(m) {
703   function TestProperty(map, property, value) {
704     map[property] = value;
705     assertEquals(value, map[property]);
706   }
707   for (var i = 0; i < 20; i++) {
708     TestProperty(m, 'key' + i, 'val' + i);
709     TestProperty(m, 'foo' + i, 'bar' + i);
710   }
711 }
712 TestArbitrary(new ArrayBuffer(256));
713 for(i = 0; i < typedArrayConstructors.length; i++) {
714   TestArbitrary(new typedArrayConstructors[i](10));
715 }
716 TestArbitrary(new DataView(new ArrayBuffer(256)));
717
718
719 // Test direct constructor call
720 assertThrows(function() { ArrayBuffer(); }, TypeError);
721 assertThrows(function() { DataView(new ArrayBuffer()); }, TypeError);