blob: a4d6e7927a2b257dc7fcec23757a4e2ac5c6d9db [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Emily Bernierd0a1eb72015-03-24 16:35:39 -040028// Flags: --harmony-tostring
29
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030// ArrayBuffer
31
32function TestByteLength(param, expectedByteLength) {
33 var ab = new ArrayBuffer(param);
34 assertSame(expectedByteLength, ab.byteLength);
35}
36
37function 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);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040057 assertEquals("[object ArrayBuffer]",
58 Object.prototype.toString.call(ab));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059}
60
61TestArrayBufferCreation();
62
63function TestByteLengthNotWritable() {
64 var ab = new ArrayBuffer(1024);
65 assertSame(1024, ab.byteLength);
66
67 assertThrows(function() { "use strict"; ab.byteLength = 42; }, TypeError);
68}
69
70TestByteLengthNotWritable();
71
72function 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
86function 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
120TestArrayBufferSlice();
121
122// Typed arrays
123
124function 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);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400130 assertEquals("[object " + constr.name + "]",
131 Object.prototype.toString.call(a0));
132
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133 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);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400268
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);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279}
280
281TestTypedArray(Uint8Array, 1, 0xFF);
282TestTypedArray(Int8Array, 1, -0x7F);
283TestTypedArray(Uint16Array, 2, 0xFFFF);
284TestTypedArray(Int16Array, 2, -0x7FFF);
285TestTypedArray(Uint32Array, 4, 0xFFFFFFFF);
286TestTypedArray(Int32Array, 4, -0x7FFFFFFF);
287TestTypedArray(Float32Array, 4, 0.5);
288TestTypedArray(Float64Array, 8, 0.5);
289TestTypedArray(Uint8ClampedArray, 1, 0xFF);
290
291function 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
303function 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
338TestSubArray(Uint8Array, 0xFF);
339TestSubArray(Int8Array, -0x7F);
340TestSubArray(Uint16Array, 0xFFFF);
341TestSubArray(Int16Array, -0x7FFF);
342TestSubArray(Uint32Array, 0xFFFFFFFF);
343TestSubArray(Int32Array, -0x7FFFFFFF);
344TestSubArray(Float32Array, 0.5);
345TestSubArray(Float64Array, 0.5);
346TestSubArray(Uint8ClampedArray, 0xFF);
347
348function TestTypedArrayOutOfRange(constructor, value, result) {
349 var a = new constructor(1);
350 a[0] = value;
351 assertSame(result, a[0]);
352}
353
354TestTypedArrayOutOfRange(Uint8Array, 0x1FA, 0xFA);
355TestTypedArrayOutOfRange(Uint8Array, -1, 0xFF);
356
357TestTypedArrayOutOfRange(Int8Array, 0x1FA, 0x7A - 0x80);
358
359TestTypedArrayOutOfRange(Uint16Array, 0x1FFFA, 0xFFFA);
360TestTypedArrayOutOfRange(Uint16Array, -1, 0xFFFF);
361TestTypedArrayOutOfRange(Int16Array, 0x1FFFA, 0x7FFA - 0x8000);
362
363TestTypedArrayOutOfRange(Uint32Array, 0x1FFFFFFFA, 0xFFFFFFFA);
364TestTypedArrayOutOfRange(Uint32Array, -1, 0xFFFFFFFF);
365TestTypedArrayOutOfRange(Int32Array, 0x1FFFFFFFA, 0x7FFFFFFA - 0x80000000);
366
367TestTypedArrayOutOfRange(Uint8ClampedArray, 0x1FA, 0xFF);
368TestTypedArrayOutOfRange(Uint8ClampedArray, -1, 0);
369
370var typedArrayConstructors = [
371 Uint8Array,
372 Int8Array,
373 Uint16Array,
374 Int16Array,
375 Uint32Array,
376 Int32Array,
377 Uint8ClampedArray,
378 Float32Array,
379 Float64Array];
380
381function TestPropertyTypeChecks(constructor) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000382 function CheckProperty(name) {
383 var d = Object.getOwnPropertyDescriptor(constructor.prototype, name);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400384 var o = {};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000385 assertThrows(function() {d.get.call(o);}, TypeError);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400386 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 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000394 }
395 }
396
397 CheckProperty("buffer");
398 CheckProperty("byteOffset");
399 CheckProperty("byteLength");
400 CheckProperty("length");
401}
402
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400403for(i = 0; i < typedArrayConstructors.length; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404 TestPropertyTypeChecks(typedArrayConstructors[i]);
405}
406
407
408function 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
500TestTypedArraySet();
501
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400502function 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(undefined, 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
548TestTypedArraysWithIllegalIndices();
549
550function 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(undefined, 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
597TestTypedArraysWithIllegalIndicesStrict();
598
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000599// DataView
600function 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
649TestDataViewConstructor();
650
651function 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
666TestDataViewPropertyTypeChecks();
667
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400668
669function 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000681// General tests for properties
682
683// Test property attribute [[Enumerable]]
684function 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}
695TestEnumerable(ArrayBuffer, new ArrayBuffer());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400696for(i = 0; i < typedArrayConstructors.length; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000697 TestEnumerable(typedArrayConstructors[i]);
698}
699TestEnumerable(DataView, new DataView(new ArrayBuffer()));
700
701// Test arbitrary properties on ArrayBuffer
702function 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++) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400708 TestProperty(m, 'key' + i, 'val' + i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000709 TestProperty(m, 'foo' + i, 'bar' + i);
710 }
711}
712TestArbitrary(new ArrayBuffer(256));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400713for(i = 0; i < typedArrayConstructors.length; i++) {
714 TestArbitrary(new typedArrayConstructors[i](10));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000715}
716TestArbitrary(new DataView(new ArrayBuffer(256)));
717
718
719// Test direct constructor call
720assertThrows(function() { ArrayBuffer(); }, TypeError);
721assertThrows(function() { DataView(new ArrayBuffer()); }, TypeError);