Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: test/mjsunit/harmony/typedarrays.js

Issue 1215863003: Include Harmony Array/TypedArray methods unconditionally (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: remove the flag and move the tests Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/mjsunit/harmony/typedarray-tostring.js ('k') | test/mjsunit/harmony/typedarrays-every.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 // Test that the constructor can be called with an iterable
281 function* gen() { for (var i = 0; i < 10; i++) yield i; }
282 var genArr = new constr(gen());
283 assertEquals(10, genArr.length);
284 assertEquals(0, genArr[0]);
285 assertEquals(9, genArr[9]);
286 // Arrays can be converted to TypedArrays
287 genArr = new constr([1, 2, 3]);
288 assertEquals(3, genArr.length);
289 assertEquals(1, genArr[0]);
290 assertEquals(3, genArr[2]);
291 // Redefining Array.prototype[Symbol.iterator] still works
292 var arrayIterator = Array.prototype[Symbol.iterator];
293 Array.prototype[Symbol.iterator] = gen;
294 genArr = new constr([1, 2, 3]);
295 assertEquals(10, genArr.length);
296 assertEquals(0, genArr[0]);
297 assertEquals(9, genArr[9]);
298 Array.prototype[Symbol.iterator] = arrayIterator;
299 // Other array-like things can be made into a TypedArray
300 var myObject = { 0: 5, 1: 6, length: 2 };
301 genArr = new constr(myObject);
302 assertEquals(2, genArr.length);
303 assertEquals(5, genArr[0]);
304 assertEquals(6, genArr[1]);
305 // Iterator takes precedence over array-like, and the property
306 // is read only once.
307 var iteratorReadCount = 0;
308 Object.defineProperty(myObject, Symbol.iterator, {
309 get: function() { iteratorReadCount++; return gen; }
310 });
311 genArr = new constr(myObject);
312 assertEquals(10, genArr.length);
313 assertEquals(0, genArr[0]);
314 assertEquals(9, genArr[9]);
315 assertEquals(1, iteratorReadCount);
316 }
317
318 TestTypedArray(Uint8Array, 1, 0xFF);
319 TestTypedArray(Int8Array, 1, -0x7F);
320 TestTypedArray(Uint16Array, 2, 0xFFFF);
321 TestTypedArray(Int16Array, 2, -0x7FFF);
322 TestTypedArray(Uint32Array, 4, 0xFFFFFFFF);
323 TestTypedArray(Int32Array, 4, -0x7FFFFFFF);
324 TestTypedArray(Float32Array, 4, 0.5);
325 TestTypedArray(Float64Array, 8, 0.5);
326 TestTypedArray(Uint8ClampedArray, 1, 0xFF);
327
328 function SubarrayTestCase(constructor, item, expectedResultLen, expectedStartInd ex,
329 initialLen, start, end) {
330 var a = new constructor(initialLen);
331 var s = a.subarray(start, end);
332 assertSame(constructor, s.constructor);
333 assertSame(expectedResultLen, s.length);
334 if (s.length > 0) {
335 s[0] = item;
336 assertSame(item, a[expectedStartIndex]);
337 }
338 }
339
340 function TestSubArray(constructor, item) {
341 SubarrayTestCase(constructor, item, 512, 512, 1024, 512, 1024);
342 SubarrayTestCase(constructor, item, 512, 512, 1024, 512);
343
344 SubarrayTestCase(constructor, item, 0, undefined, 0, 1, 20);
345 SubarrayTestCase(constructor, item, 100, 0, 100, 0, 100);
346 SubarrayTestCase(constructor, item, 100, 0, 100, 0, 1000);
347 SubarrayTestCase(constructor, item, 0, undefined, 100, 5, 1);
348
349 SubarrayTestCase(constructor, item, 1, 89, 100, -11, -10);
350 SubarrayTestCase(constructor, item, 9, 90, 100, -10, 99);
351 SubarrayTestCase(constructor, item, 0, undefined, 100, -10, 80);
352 SubarrayTestCase(constructor, item, 10,80, 100, 80, -10);
353
354 SubarrayTestCase(constructor, item, 10,90, 100, 90, "100");
355 SubarrayTestCase(constructor, item, 10,90, 100, "90", "100");
356
357 SubarrayTestCase(constructor, item, 0, undefined, 100, 90, "abc");
358 SubarrayTestCase(constructor, item, 10,0, 100, "abc", 10);
359
360 SubarrayTestCase(constructor, item, 10,0, 100, 0.96, 10.96);
361 SubarrayTestCase(constructor, item, 10,0, 100, 0.96, 10.01);
362 SubarrayTestCase(constructor, item, 10,0, 100, 0.01, 10.01);
363 SubarrayTestCase(constructor, item, 10,0, 100, 0.01, 10.96);
364
365
366 SubarrayTestCase(constructor, item, 10,90, 100, 90);
367 SubarrayTestCase(constructor, item, 10,90, 100, -10);
368
369 var method = constructor.prototype.subarray;
370 method.call(new constructor(100), 0, 100);
371 var o = {};
372 assertThrows(function() { method.call(o, 0, 100); }, TypeError);
373 }
374
375 TestSubArray(Uint8Array, 0xFF);
376 TestSubArray(Int8Array, -0x7F);
377 TestSubArray(Uint16Array, 0xFFFF);
378 TestSubArray(Int16Array, -0x7FFF);
379 TestSubArray(Uint32Array, 0xFFFFFFFF);
380 TestSubArray(Int32Array, -0x7FFFFFFF);
381 TestSubArray(Float32Array, 0.5);
382 TestSubArray(Float64Array, 0.5);
383 TestSubArray(Uint8ClampedArray, 0xFF);
384
385 function TestTypedArrayOutOfRange(constructor, value, result) {
386 var a = new constructor(1);
387 a[0] = value;
388 assertSame(result, a[0]);
389 }
390
391 TestTypedArrayOutOfRange(Uint8Array, 0x1FA, 0xFA);
392 TestTypedArrayOutOfRange(Uint8Array, -1, 0xFF);
393
394 TestTypedArrayOutOfRange(Int8Array, 0x1FA, 0x7A - 0x80);
395
396 TestTypedArrayOutOfRange(Uint16Array, 0x1FFFA, 0xFFFA);
397 TestTypedArrayOutOfRange(Uint16Array, -1, 0xFFFF);
398 TestTypedArrayOutOfRange(Int16Array, 0x1FFFA, 0x7FFA - 0x8000);
399
400 TestTypedArrayOutOfRange(Uint32Array, 0x1FFFFFFFA, 0xFFFFFFFA);
401 TestTypedArrayOutOfRange(Uint32Array, -1, 0xFFFFFFFF);
402 TestTypedArrayOutOfRange(Int32Array, 0x1FFFFFFFA, 0x7FFFFFFA - 0x80000000);
403
404 TestTypedArrayOutOfRange(Uint8ClampedArray, 0x1FA, 0xFF);
405 TestTypedArrayOutOfRange(Uint8ClampedArray, -1, 0);
406
407 var typedArrayConstructors = [
408 Uint8Array,
409 Int8Array,
410 Uint16Array,
411 Int16Array,
412 Uint32Array,
413 Int32Array,
414 Uint8ClampedArray,
415 Float32Array,
416 Float64Array];
417
418 function TestPropertyTypeChecks(constructor) {
419 function CheckProperty(name) {
420 var d = Object.getOwnPropertyDescriptor(constructor.prototype, name);
421 var o = {};
422 assertThrows(function() {d.get.call(o);}, TypeError);
423 for (var i = 0; i < typedArrayConstructors.length; i++) {
424 var ctor = typedArrayConstructors[i];
425 var a = new ctor(10);
426 if (ctor === constructor) {
427 d.get.call(a); // shouldn't throw
428 } else {
429 assertThrows(function() {d.get.call(a);}, TypeError);
430 }
431 }
432 }
433
434 CheckProperty("buffer");
435 CheckProperty("byteOffset");
436 CheckProperty("byteLength");
437 CheckProperty("length");
438 }
439
440 for(i = 0; i < typedArrayConstructors.length; i++) {
441 TestPropertyTypeChecks(typedArrayConstructors[i]);
442 }
443
444
445 function TestTypedArraySet() {
446 // Test array.set in different combinations.
447
448 function assertArrayPrefix(expected, array) {
449 for (var i = 0; i < expected.length; ++i) {
450 assertEquals(expected[i], array[i]);
451 }
452 }
453
454 var a11 = new Int16Array([1, 2, 3, 4, 0, -1])
455 var a12 = new Uint16Array(15)
456 a12.set(a11, 3)
457 assertArrayPrefix([0, 0, 0, 1, 2, 3, 4, 0, 0xffff, 0, 0], a12)
458 assertThrows(function(){ a11.set(a12) })
459
460 var a21 = [1, undefined, 10, NaN, 0, -1, {valueOf: function() {return 3}}]
461 var a22 = new Int32Array(12)
462 a22.set(a21, 2)
463 assertArrayPrefix([0, 0, 1, 0, 10, 0, 0, -1, 3, 0], a22)
464
465 var a31 = new Float32Array([2, 4, 6, 8, 11, NaN, 1/0, -3])
466 var a32 = a31.subarray(2, 6)
467 a31.set(a32, 4)
468 assertArrayPrefix([2, 4, 6, 8, 6, 8, 11, NaN], a31)
469 assertArrayPrefix([6, 8, 6, 8], a32)
470
471 var a4 = new Uint8ClampedArray([3,2,5,6])
472 a4.set(a4)
473 assertArrayPrefix([3, 2, 5, 6], a4)
474
475 // Cases with overlapping backing store but different element sizes.
476 var b = new ArrayBuffer(4)
477 var a5 = new Int16Array(b)
478 var a50 = new Int8Array(b)
479 var a51 = new Int8Array(b, 0, 2)
480 var a52 = new Int8Array(b, 1, 2)
481 var a53 = new Int8Array(b, 2, 2)
482
483 a5.set([0x5050, 0x0a0a])
484 assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50)
485 assertArrayPrefix([0x50, 0x50], a51)
486 assertArrayPrefix([0x50, 0x0a], a52)
487 assertArrayPrefix([0x0a, 0x0a], a53)
488
489 a50.set([0x50, 0x50, 0x0a, 0x0a])
490 a51.set(a5)
491 assertArrayPrefix([0x50, 0x0a, 0x0a, 0x0a], a50)
492
493 a50.set([0x50, 0x50, 0x0a, 0x0a])
494 a52.set(a5)
495 assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50)
496
497 a50.set([0x50, 0x50, 0x0a, 0x0a])
498 a53.set(a5)
499 assertArrayPrefix([0x50, 0x50, 0x50, 0x0a], a50)
500
501 a50.set([0x50, 0x51, 0x0a, 0x0b])
502 a5.set(a51)
503 assertArrayPrefix([0x0050, 0x0051], a5)
504
505 a50.set([0x50, 0x51, 0x0a, 0x0b])
506 a5.set(a52)
507 assertArrayPrefix([0x0051, 0x000a], a5)
508
509 a50.set([0x50, 0x51, 0x0a, 0x0b])
510 a5.set(a53)
511 assertArrayPrefix([0x000a, 0x000b], a5)
512
513 // Mixed types of same size.
514 var a61 = new Float32Array([1.2, 12.3])
515 var a62 = new Int32Array(2)
516 a62.set(a61)
517 assertArrayPrefix([1, 12], a62)
518 a61.set(a62)
519 assertArrayPrefix([1, 12], a61)
520
521 // Invalid source
522 var a = new Uint16Array(50);
523 var expected = [];
524 for (i = 0; i < 50; i++) {
525 a[i] = i;
526 expected.push(i);
527 }
528 a.set({});
529 assertArrayPrefix(expected, a);
530 assertThrows(function() { a.set.call({}) }, TypeError);
531 assertThrows(function() { a.set.call([]) }, TypeError);
532
533 assertThrows(function() { a.set(0); }, TypeError);
534 assertThrows(function() { a.set(0, 1); }, TypeError);
535 }
536
537 TestTypedArraySet();
538
539 function TestTypedArraysWithIllegalIndices() {
540 var a = new Int32Array(100);
541
542 a[-10] = 10;
543 assertEquals(undefined, a[-10]);
544 a["-10"] = 10;
545 assertEquals(undefined, a["-10"]);
546
547 var s = " -10";
548 a[s] = 10;
549 assertEquals(10, a[s]);
550 var s1 = " -10 ";
551 a[s] = 10;
552 assertEquals(10, a[s]);
553
554 a["-1e2"] = 10;
555 assertEquals(10, a["-1e2"]);
556 assertEquals(undefined, a[-1e2]);
557
558 a["-0"] = 256;
559 var s2 = " -0";
560 a[s2] = 255;
561 assertEquals(undefined, a["-0"]);
562 assertEquals(255, a[s2]);
563 assertEquals(0, a[-0]);
564
565 /* Chromium bug: 424619
566 * a[-Infinity] = 50;
567 * assertEquals(undefined, a[-Infinity]);
568 */
569 a[1.5] = 10;
570 assertEquals(undefined, a[1.5]);
571 var nan = Math.sqrt(-1);
572 a[nan] = 5;
573 assertEquals(undefined, a[nan]);
574
575 var x = 0;
576 var y = -0;
577 assertEquals(Infinity, 1/x);
578 assertEquals(-Infinity, 1/y);
579 a[x] = 5;
580 a[y] = 27;
581 assertEquals(27, a[x]);
582 assertEquals(27, a[y]);
583 }
584
585 TestTypedArraysWithIllegalIndices();
586
587 function TestTypedArraysWithIllegalIndicesStrict() {
588 'use strict';
589 var a = new Int32Array(100);
590
591 a[-10] = 10;
592 assertEquals(undefined, a[-10]);
593 a["-10"] = 10;
594 assertEquals(undefined, a["-10"]);
595
596 var s = " -10";
597 a[s] = 10;
598 assertEquals(10, a[s]);
599 var s1 = " -10 ";
600 a[s] = 10;
601 assertEquals(10, a[s]);
602
603 a["-1e2"] = 10;
604 assertEquals(10, a["-1e2"]);
605 assertEquals(undefined, a[-1e2]);
606
607 a["-0"] = 256;
608 var s2 = " -0";
609 a[s2] = 255;
610 assertEquals(undefined, a["-0"]);
611 assertEquals(255, a[s2]);
612 assertEquals(0, a[-0]);
613
614 /* Chromium bug: 424619
615 * a[-Infinity] = 50;
616 * assertEquals(undefined, a[-Infinity]);
617 */
618 a[1.5] = 10;
619 assertEquals(undefined, a[1.5]);
620 var nan = Math.sqrt(-1);
621 a[nan] = 5;
622 assertEquals(undefined, a[nan]);
623
624 var x = 0;
625 var y = -0;
626 assertEquals(Infinity, 1/x);
627 assertEquals(-Infinity, 1/y);
628 a[x] = 5;
629 a[y] = 27;
630 assertEquals(27, a[x]);
631 assertEquals(27, a[y]);
632 }
633
634 TestTypedArraysWithIllegalIndicesStrict();
635
636 // DataView
637 function TestDataViewConstructor() {
638 var ab = new ArrayBuffer(256);
639
640 var d1 = new DataView(ab, 1, 255);
641 assertTrue(ArrayBuffer.isView(d1));
642 assertSame(ab, d1.buffer);
643 assertSame(1, d1.byteOffset);
644 assertSame(255, d1.byteLength);
645
646 var d2 = new DataView(ab, 2);
647 assertSame(ab, d2.buffer);
648 assertSame(2, d2.byteOffset);
649 assertSame(254, d2.byteLength);
650
651 var d3 = new DataView(ab);
652 assertSame(ab, d3.buffer);
653 assertSame(0, d3.byteOffset);
654 assertSame(256, d3.byteLength);
655
656 var d3a = new DataView(ab, 1, 0);
657 assertSame(ab, d3a.buffer);
658 assertSame(1, d3a.byteOffset);
659 assertSame(0, d3a.byteLength);
660
661 var d3b = new DataView(ab, 256, 0);
662 assertSame(ab, d3b.buffer);
663 assertSame(256, d3b.byteOffset);
664 assertSame(0, d3b.byteLength);
665
666 var d3c = new DataView(ab, 256);
667 assertSame(ab, d3c.buffer);
668 assertSame(256, d3c.byteOffset);
669 assertSame(0, d3c.byteLength);
670
671 var d4 = new DataView(ab, 1, 3.1415926);
672 assertSame(ab, d4.buffer);
673 assertSame(1, d4.byteOffset);
674 assertSame(3, d4.byteLength);
675
676
677 // error cases
678 assertThrows(function() { new DataView(ab, -1); }, RangeError);
679 assertThrows(function() { new DataView(ab, 1, -1); }, RangeError);
680 assertThrows(function() { new DataView(); }, TypeError);
681 assertThrows(function() { new DataView([]); }, TypeError);
682 assertThrows(function() { new DataView(ab, 257); }, RangeError);
683 assertThrows(function() { new DataView(ab, 1, 1024); }, RangeError);
684 }
685
686 TestDataViewConstructor();
687
688 function TestDataViewPropertyTypeChecks() {
689 var a = new DataView(new ArrayBuffer(10));
690 function CheckProperty(name) {
691 var d = Object.getOwnPropertyDescriptor(DataView.prototype, name);
692 var o = {}
693 assertThrows(function() {d.get.call(o);}, TypeError);
694 d.get.call(a); // shouldn't throw
695 }
696
697 CheckProperty("buffer");
698 CheckProperty("byteOffset");
699 CheckProperty("byteLength");
700 }
701
702
703 TestDataViewPropertyTypeChecks();
704
705
706 function TestDataViewToStringTag() {
707 var a = new DataView(new ArrayBuffer(10));
708 assertEquals("[object DataView]", Object.prototype.toString.call(a));
709 var desc = Object.getOwnPropertyDescriptor(
710 DataView.prototype, Symbol.toStringTag);
711 assertTrue(desc.configurable);
712 assertFalse(desc.enumerable);
713 assertFalse(desc.writable);
714 assertEquals("DataView", desc.value);
715 }
716
717
718 // General tests for properties
719
720 // Test property attribute [[Enumerable]]
721 function TestEnumerable(func, obj) {
722 function props(x) {
723 var array = [];
724 for (var p in x) array.push(p);
725 return array.sort();
726 }
727 assertArrayEquals([], props(func));
728 assertArrayEquals([], props(func.prototype));
729 if (obj)
730 assertArrayEquals([], props(obj));
731 }
732 TestEnumerable(ArrayBuffer, new ArrayBuffer());
733 for(i = 0; i < typedArrayConstructors.length; i++) {
734 TestEnumerable(typedArrayConstructors[i]);
735 }
736 TestEnumerable(DataView, new DataView(new ArrayBuffer()));
737
738 // Test arbitrary properties on ArrayBuffer
739 function TestArbitrary(m) {
740 function TestProperty(map, property, value) {
741 map[property] = value;
742 assertEquals(value, map[property]);
743 }
744 for (var i = 0; i < 20; i++) {
745 TestProperty(m, 'key' + i, 'val' + i);
746 TestProperty(m, 'foo' + i, 'bar' + i);
747 }
748 }
749 TestArbitrary(new ArrayBuffer(256));
750 for(i = 0; i < typedArrayConstructors.length; i++) {
751 TestArbitrary(new typedArrayConstructors[i](10));
752 }
753 TestArbitrary(new DataView(new ArrayBuffer(256)));
754
755
756 // Test direct constructor call
757 assertThrows(function() { ArrayBuffer(); }, TypeError);
758 assertThrows(function() { DataView(new ArrayBuffer()); }, TypeError);
OLDNEW
« no previous file with comments | « test/mjsunit/harmony/typedarray-tostring.js ('k') | test/mjsunit/harmony/typedarrays-every.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698