5 <script src=
"../../../resources/js-test.js"></script>
6 <script src=
"resources/webgl-test.js"></script>
7 <script src=
"resources/typed-array-test-cases.js"></script>
10 <div id=
"description"></div>
11 <div id=
"console"></div>
14 description("Verifies the functionality of the new array-like objects in the TypedArray spec");
16 var currentlyRunning
= '';
18 function running(str
) {
19 currentlyRunning
= str
;
22 function output(str
) {
27 testPassed(currentlyRunning
);
34 exc
= currentlyRunning
+ ': ' + str
;
36 exc
= currentlyRunning
;
40 function assertEq(prefix
, expected
, val
) {
41 if (expected
!= val
) {
42 var str
= prefix
+ ': expected ' + expected
+ ', got ' + val
;
47 function assert(prefix
, expected
) {
49 var str
= prefix
+ ': expected value / true';
54 function printSummary() {
56 debug("Test passed.");
66 function testSlice() {
67 function test(subBuf
, starts
, size
) {
69 subBuffer
= eval(subBuf
);
70 subArray
= new Int8Array(subBuffer
);
71 assertEq(subBuf
, subBuffer
.byteLength
, byteLength
);
72 for (var i
= 0; i
< size
; ++i
)
73 assertEq('Element ' + i
, starts
+ i
, subArray
[i
]);
78 buffer
= new ArrayBuffer(32);
79 var array
= new Int8Array(buffer
);
80 for (var i
= 0; i
< 32; ++i
)
83 test("buffer.slice(0)", 0, 32);
84 test("buffer.slice(16)", 16, 16);
85 test("buffer.slice(24)", 24, 8);
86 test("buffer.slice(32)", 32, 0);
87 test("buffer.slice(40)", 32, 0);
88 test("buffer.slice(80)", 32, 0);
90 test("buffer.slice(-8)", 24, 8);
91 test("buffer.slice(-16)", 16, 16);
92 test("buffer.slice(-24)", 8, 24);
93 test("buffer.slice(-32)", 0, 32);
94 test("buffer.slice(-40)", 0, 32);
95 test("buffer.slice(-80)", 0, 32);
97 test("buffer.slice(0, 32)", 0, 32);
98 test("buffer.slice(0, 16)", 0, 16);
99 test("buffer.slice(8, 24)", 8, 16);
100 test("buffer.slice(16, 32)", 16, 16);
101 test("buffer.slice(24, 16)", 24, 0);
103 test("buffer.slice(16, -8)", 16, 8);
104 test("buffer.slice(-20, 30)", 12, 18);
106 test("buffer.slice(-8, -20)", 24, 0);
107 test("buffer.slice(-20, -8)", 12, 12);
108 test("buffer.slice(-40, 16)", 0, 16);
109 test("buffer.slice(-40, 40)", 0, 32);
116 function testInheritanceHierarchy() {
117 debug('test inheritance hierarchy of typed array views');
120 var foo
= ArrayBufferView
;
121 testFailed('ArrayBufferView has [NoInterfaceObject] extended attribute and should not be defined');
123 testPassed('ArrayBufferView has [NoInterfaceObject] extended attribute and was (correctly) not defined');
126 // There is currently only one kind of view that inherits from another
127 // Uint8ClampedArray inherited from Uint8Array in earlier versions
128 // of the typed array specification. Since this is no longer the
129 // case, assert the new behavior.
130 shouldBe('new Uint8ClampedArray(1) instanceof Uint8Array', 'false');
134 // Tests for unsigned array variants
137 function testSetAndGet10To1(type
, name
) {
138 running('test ' + name
+ ' SetAndGet10To1');
140 var array
= new type(10);
141 for (var i
= 0; i
< 10; i
++) {
144 for (var i
= 0; i
< 10; i
++) {
145 assertEq('Element ' + i
, 10 - i
, array
[i
]);
153 function testConstructWithArrayOfUnsignedValues(type
, name
) {
154 running('test ' + name
+ ' ConstructWithArrayOfUnsignedValues');
156 var array
= new type([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
157 assertEq('Array length', 10, array
.length
);
158 for (var i
= 0; i
< 10; i
++) {
159 assertEq('Element ' + i
, 10 - i
, array
[i
]);
167 function testConstructWithTypedArrayOfUnsignedValues(type
, name
) {
168 running('test ' + name
+ ' ConstructWithTypedArrayOfUnsignedValues');
170 var tmp
= new type([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
171 var array
= new type(tmp
);
172 assertEq('Array length', 10, array
.length
);
173 for (var i
= 0; i
< 10; i
++) {
174 assertEq('Element ' + i
, 10 - i
, array
[i
]);
183 // Tests for signed array variants
186 function testSetAndGetPos10ToNeg10(type
, name
) {
187 running('test ' + name
+ ' SetAndGetPos10ToNeg10');
189 var array
= new type(21);
190 for (var i
= 0; i
< 21; i
++) {
193 for (var i
= 0; i
< 21; i
++) {
194 assertEq('Element ' + i
, 10 - i
, array
[i
]);
202 function testConstructWithArrayOfSignedValues(type
, name
) {
203 running('test ' + name
+ ' ConstructWithArrayOfSignedValues');
205 var array
= new type([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]);
206 assertEq('Array length', 21, array
.length
);
207 for (var i
= 0; i
< 21; i
++) {
208 assertEq('Element ' + i
, 10 - i
, array
[i
]);
216 function testConstructWithTypedArrayOfSignedValues(type
, name
) {
217 running('test ' + name
+ ' ConstructWithTypedArrayOfSignedValues');
219 var tmp
= new type([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]);
220 var array
= new type(tmp
);
221 assertEq('Array length', 21, array
.length
);
222 for (var i
= 0; i
< 21; i
++) {
223 assertEq('Element ' + i
, 10 - i
, array
[i
]);
232 // Test cases for integral types.
233 // Some JavaScript engines need separate copies of this code in order
234 // to exercise all of their optimized code paths.
237 function testIntegralArrayTruncationBehavior(type
, name
, unsigned
) {
238 running('test integral array truncation behavior for ' + name
);
244 sourceData
= [0.6, 10.6, 0.2, 10.2, 10.5, 11.5];
245 if (type
=== Uint8ClampedArray
) {
246 expectedResults
= [1, 11, 0, 10, 10, 12];
248 expectedResults
= [0, 10, 0, 10, 10, 11];
251 sourceData
= [0.6, 10.6, -0.6, -10.6];
252 expectedResults
= [0, 10, 0, -10];
255 var numIterations
= 10;
256 var array
= new type(numIterations
);
258 // The code block in each of the case statements below is identical, but some
259 // JavaScript engines need separate copies in order to exercise all of
260 // their optimized code paths.
265 for (var ii
= 0; ii
< sourceData
.length
; ++ii
) {
266 for (var jj
= 0; jj
< numIterations
; ++jj
) {
267 array
[jj
] = sourceData
[ii
];
268 assertEq('Storing ' + sourceData
[ii
], expectedResults
[ii
], array
[jj
]);
273 for (var ii
= 0; ii
< sourceData
.length
; ++ii
) {
274 for (var jj
= 0; jj
< numIterations
; ++jj
) {
275 array
[jj
] = sourceData
[ii
];
276 assertEq('Storing ' + sourceData
[ii
], expectedResults
[ii
], array
[jj
]);
281 for (var ii
= 0; ii
< sourceData
.length
; ++ii
) {
282 for (var jj
= 0; jj
< numIterations
; ++jj
) {
283 array
[jj
] = sourceData
[ii
];
284 assertEq('Storing ' + sourceData
[ii
], expectedResults
[ii
], array
[jj
]);
289 for (var ii
= 0; ii
< sourceData
.length
; ++ii
) {
290 for (var jj
= 0; jj
< numIterations
; ++jj
) {
291 array
[jj
] = sourceData
[ii
];
292 assertEq('Storing ' + sourceData
[ii
], expectedResults
[ii
], array
[jj
]);
296 case Uint8ClampedArray
:
297 for (var ii
= 0; ii
< sourceData
.length
; ++ii
) {
298 for (var jj
= 0; jj
< numIterations
; ++jj
) {
299 array
[jj
] = sourceData
[ii
];
300 assertEq('Storing ' + sourceData
[ii
], expectedResults
[ii
], array
[jj
]);
305 for (var ii
= 0; ii
< sourceData
.length
; ++ii
) {
306 for (var jj
= 0; jj
< numIterations
; ++jj
) {
307 array
[jj
] = sourceData
[ii
];
308 assertEq('Storing ' + sourceData
[ii
], expectedResults
[ii
], array
[jj
]);
313 for (var ii
= 0; ii
< sourceData
.length
; ++ii
) {
314 for (var jj
= 0; jj
< numIterations
; ++jj
) {
315 array
[jj
] = sourceData
[ii
];
316 assertEq('Storing ' + sourceData
[ii
], expectedResults
[ii
], array
[jj
]);
321 fail("Unhandled type");
333 // Test cases for both signed and unsigned types
336 function testGetWithOutOfRangeIndices(type
, name
) {
337 debug('Testing ' + name
+ ' GetWithOutOfRangeIndices');
338 // See below for declaration of this global variable
339 array
= new type([2, 3]);
340 shouldBeUndefined("array[2]");
341 shouldBeUndefined("array[-1]");
342 shouldBeUndefined("array[0x20000000]");
345 function testOffsetsAndSizes(type
, name
, elementSizeInBytes
) {
346 running('test ' + name
+ ' OffsetsAndSizes');
349 assertEq('type.BYTES_PER_ELEMENT', elementSizeInBytes
, type
.BYTES_PER_ELEMENT
);
350 var array
= new type(len
);
351 assert('array.buffer', array
.buffer
);
352 assertEq('array.byteOffset', 0, array
.byteOffset
);
353 assertEq('array.length', len
, array
.length
);
354 assertEq('array.byteLength', len
* elementSizeInBytes
, array
.byteLength
);
355 array
= new type(array
.buffer
, elementSizeInBytes
, len
- 1);
356 assert('array.buffer', array
.buffer
);
357 assertEq('array.byteOffset', elementSizeInBytes
, array
.byteOffset
);
358 assertEq('array.length', len
- 1, array
.length
);
359 assertEq('array.byteLength', (len
- 1) * elementSizeInBytes
, array
.byteLength
);
366 function testSetFromTypedArray(type
, name
) {
367 running('test ' + name
+ ' SetFromTypedArray');
369 var array
= new type(10);
370 var array2
= new type(5);
371 for (var i
= 0; i
< 10; i
++) {
372 assertEq('Element ' + i
, 0, array
[i
]);
374 for (var i
= 0; i
< array2
.length
; i
++) {
378 for (var i
= 0; i
< array2
.length
; i
++) {
379 assertEq('Element ' + i
, i
, array
[i
]);
381 array
.set(array2
, 5);
382 for (var i
= 0; i
< array2
.length
; i
++) {
383 assertEq('Element ' + i
, i
, array
[5 + i
]);
391 function negativeTestSetFromTypedArray(type
, name
) {
392 running('negativeTest ' + name
+ ' SetFromTypedArray');
394 var array
= new type(5);
395 var array2
= new type(6);
396 for (var i
= 0; i
< 5; i
++) {
397 assertEq('Element ' + i
, 0, array
[i
]);
399 for (var i
= 0; i
< array2
.length
; i
++) {
404 fail('Expected exception from array.set(array2)');
409 array2
.set(array
, 2);
410 fail('Expected exception from array2.set(array, 2)');
420 function testSetFromArray(type
, name
) {
421 running('test ' + name
+ ' SetFromArray');
423 var array
= new type(10);
424 var array2
= [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
425 for (var i
= 0; i
< 10; i
++) {
426 assertEq('Element ' + i
, 0, array
[i
]);
428 array
.set(array2
, 0);
429 for (var i
= 0; i
< array2
.length
; i
++) {
430 assertEq('Element ' + i
, 10 - i
, array
[i
]);
438 function negativeTestSetFromArray(type
, name
) {
439 running('negativeTest ' + name
+ ' SetFromArray');
441 var array
= new type([2, 3]);
443 array
.set([4, 5], 1);
449 array
.set([4, 5, 6]);
461 function testSubarray(type
, name
) {
462 running('test ' + name
+ ' Subarray');
464 var array
= new type([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
465 var subarray
= array
.subarray(0, 5);
466 assertEq('subarray.length', 5, subarray
.length
);
467 for (var i
= 0; i
< 5; i
++) {
468 assertEq('Element ' + i
, i
, subarray
[i
]);
470 subarray
= array
.subarray(4, 10);
471 assertEq('subarray.length', 6, subarray
.length
);
472 for (var i
= 0; i
< 6; i
++) {
473 assertEq('Element ' + i
, 4 + i
, subarray
[i
]);
481 function testSubarrayOffsetAndLengthClamping(type
, name
) {
482 running('test ' + name
+ ' Subarray offset and length clamping');
484 var array
= new type([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
485 var subarray1
= array
.subarray(0, 5);
486 var subarray2
= subarray1
.subarray(-2, 10);
487 assertEq('subarray2.length', 2, subarray2
.length
);
488 assertEq('Element ' + 0, 3, subarray2
[0]);
489 assertEq('Element ' + 1, 4, subarray2
[1]);
496 function negativeTestSubarray(type
, name
) {
497 running('negativeTest ' + name
+ ' Subarray');
499 var array
= new type([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
500 subarray
= array
.subarray(5, 11);
501 if (subarray
.length
!= 5) {
505 subarray
= array
.subarray(10, 10);
506 if (subarray
.length
!= 0) {
516 function testSetBoundaryConditions(type
, name
, testValues
, expectedValues
) {
517 running('test ' + name
+ ' SetBoundaryConditions');
519 var array
= new type(1);
520 assertEq('Array length', 1, array
.length
);
521 for (var ii
= 0; ii
< testValues
.length
; ++ii
) {
522 for (var jj
= 0; jj
< 10; ++jj
) {
523 array
[0] = testValues
[ii
];
524 assertEq('Element 0', expectedValues
[ii
], array
[0]);
533 function testConstructionBoundaryConditions(type
, name
, testValues
, expectedValues
) {
534 running('test ' + name
+ ' ConstructionBoundaryConditions');
536 var array
= new type(testValues
);
537 assertEq('Array length', testValues
.length
, array
.length
);
538 for (var ii
= 0; ii
< testValues
.length
; ++ii
) {
539 assertEq('Element ' + ii
, expectedValues
[ii
], array
[ii
]);
547 function testConstructionWithNullBuffer(type
, name
) {
550 array
= new type(null);
551 testFailed("Construction of " + name
+ " with null buffer should throw exception");
553 testPassed("Construction of " + name
+ " with null buffer threw exception");
556 array
= new type(null, 0, 0);
557 testFailed("Construction of " + name
+ " with (null buffer, 0) should throw exception");
559 testPassed("Construction of " + name
+ " with (null buffer, 0) threw exception");
562 array
= new type(null, 0, 0);
563 testFailed("Construction of " + name
+ " with (null buffer, 0, 0) should throw exception");
565 testPassed("Construction of " + name
+ " with (null buffer, 0, 0) threw exception");
569 function testConstructionWithExceptionThrowingObject(type
, name
) {
571 Object
.defineProperty(o
, "length", { get: function() { throw "bail;" }});
573 var array
= new type(o
);
576 testPassed("Construction of " + name
+ " with exception-throwing array-like object didn't crash unexpectedly");
579 function shouldThrowIndexSizeErr(func
, text
) {
580 var errorText
= text
+ " should throw an exception";
583 testFailed(errorText
);
585 testPassed(text
+ " threw an exception");
589 function shouldThrowTypeError(func
, text
) {
594 if (e
instanceof TypeError
) {
599 testPassed(text
+ " threw TypeError");
601 testFailed(text
+ " should throw TypeError");
605 function testConstructionWithOutOfRangeValues(type
, name
) {
606 shouldThrowIndexSizeErr(function() {
607 var buffer
= new ArrayBuffer(4);
608 var array
= new type(buffer
, 4, 0x3FFFFFFF);
609 }, "Construction of " + name
+ " with out-of-range number of elements");
610 shouldThrowIndexSizeErr(function() {
611 var buffer
= new ArrayBuffer(4);
612 var array
= new type(buffer
, 8);
613 }, "Construction of " + name
+ " with out-of-range offset");
616 function testConstructionWithNegativeOutOfRangeValues(type
, name
) {
618 var buffer
= new ArrayBuffer(-1);
619 testFailed("Construction of ArrayBuffer with negative size should throw exception");
621 testPassed("Construction of ArrayBuffer with negative size threw exception");
624 var array
= new type(-1);
625 testFailed("Construction of " + name
+ " with negative size should throw exception");
627 testPassed("Construction of " + name
+ " with negative size threw exception");
629 shouldThrowIndexSizeErr(function() {
630 var buffer
= new ArrayBuffer(4);
631 var array
= new type(buffer
, 4, -2147483648);
632 }, "Construction of " + name
+ " with negative out-of-range values");
635 function testConstructionWithUnalignedOffset(type
, name
, elementSizeInBytes
) {
636 if (elementSizeInBytes
> 1) {
637 shouldThrowIndexSizeErr(function() {
638 var buffer
= new ArrayBuffer(32);
639 var array
= new type(buffer
, 1, elementSizeInBytes
);
640 }, "Construction of " + name
+ " with unaligned offset");
644 function testConstructionWithUnalignedLength(type
, name
, elementSizeInBytes
) {
645 if (elementSizeInBytes
> 1) {
646 shouldThrowIndexSizeErr(function() {
647 var buffer
= new ArrayBuffer(elementSizeInBytes
+ 1);
648 var array
= new type(buffer
, 0);
649 }, "Construction of " + name
+ " with unaligned length");
653 function testConstructionWithBothArrayBufferAndLength(type
, name
, elementSizeInBytes
) {
654 var bufByteLength
= 1000 * elementSizeInBytes
;
655 var buf
= new ArrayBuffer(bufByteLength
);
656 var array1
= new type(buf
);
657 var array2
= new type(bufByteLength
/ elementSizeInBytes
);
658 if (array1
.length
== array2
.length
) {
659 testPassed("Array lengths matched with explicit and implicit creation of ArrayBuffer");
661 testFailed("Array lengths DID NOT MATCH with explicit and implicit creation of ArrayBuffer");
665 function testConstructionWithSubPortionOfArrayBuffer(type
, name
, elementSizeInBytes
) {
666 if (elementSizeInBytes
> 1) {
667 // Test construction with a valid sub-portion of an array buffer
668 // (whose size is not an integral multiple of the element size).
669 var size
= 4 * elementSizeInBytes
+ (elementSizeInBytes
/ 2);
670 var buf
= new ArrayBuffer(size
);
672 var array
= new type(buf
, 0, 2);
673 testPassed("new " + name
+ "(new ArrayBuffer(" + size
+ "), 0, 2) succeeded");
675 testFailed("new " + name
+ "(new ArrayBuffer(" + size
+ "), 0, 2) failed: " + e
);
680 // These need to be global for shouldBe to see them
684 function testSubarrayWithOutOfRangeValues(type
, name
, sz
) {
685 debug("Testing subarray of " + name
);
687 var buffer
= new ArrayBuffer(32);
688 array
= new type(buffer
);
690 shouldBe("array.length", "32 / typeSize");
692 shouldBe("array.subarray(4, 0x3FFFFFFF).length", "(32 / typeSize) - 4");
693 shouldBe("array.subarray(4, -2147483648).length", "0");
694 // Test subarray() against overflows.
695 array
= array
.subarray(2);
697 // Full byte offset is +1 larger than the maximum unsigned long int.
698 // Make sure subarray() still handles it correctly. Otherwise overflow would happen and
699 // offset would be 0, and array.length array.length would incorrectly be 1.
700 var start
= 4294967296 / sz
- 2;
701 array
= array
.subarray(start
, start
+ 1);
702 shouldBe("array.length", "0");
705 testFailed("Subarray of " + name
+ " threw exception");
708 testFailed("Exception: " + e
);
712 function testSubarrayWithDefaultValues(type
, name
, sz
) {
713 debug("Testing subarray with default inputs of " + name
);
715 var buffer
= new ArrayBuffer(32);
716 array
= new type(buffer
);
718 shouldBe("array.length", "32 / typeSize");
720 shouldBe("array.subarray(0).length", "(32 / typeSize)");
721 shouldBe("array.subarray(2).length", "(32 / typeSize) - 2");
722 shouldBe("array.subarray(-2).length", "2");
723 shouldBe("array.subarray(-2147483648).length", "(32 / typeSize)");
725 testFailed("Subarray of " + name
+ " threw exception");
728 testFailed("Exception: " + e
);
732 function setWithInvalidOffset(type
, name
, length
,
733 sourceType
, sourceName
, sourceLength
,
734 offset
, offsetDescription
) {
735 var webglArray
= new type(length
);
736 var sourceArray
= new sourceType(sourceLength
);
737 for (var i
= 0; i
< sourceLength
; i
++)
738 sourceArray
[i
] = 42 + i
;
740 webglArray
.set(sourceArray
, offset
);
741 testFailed("Setting " + name
+ " from " + sourceName
+ " with " +
742 offsetDescription
+ " offset was not caught");
744 testPassed("Setting " + name
+ " from " + sourceName
+ " with " +
745 offsetDescription
+ " offset was caught");
749 function setWithValidOffset(type
, name
, length
,
750 sourceType
, sourceName
, sourceLength
,
751 offset
, offsetDescription
) {
752 running("Setting " + name
+ " from " + sourceName
+ " with " +
753 offsetDescription
+ " offset");
754 var webglArray
= new type(length
);
755 var sourceArray
= new sourceType(sourceLength
);
756 for (var i
= 0; i
< sourceLength
; i
++)
757 sourceArray
[i
] = 42 + i
;
759 webglArray
.set(sourceArray
, offset
);
760 offset
= Math
.floor(offset
);
761 for (var i
= 0; i
< sourceLength
; i
++) {
762 assertEq("Element " + i
+ offset
, sourceArray
[i
], webglArray
[i
+ offset
]);
771 function testSettingFromArrayWithOutOfRangeOffset(type
, name
) {
772 setWithInvalidOffset(type
, name
, 32, Array
, "array", 16,
773 0x7FFFFFF8, "out-of-range");
776 function testSettingFromTypedArrayWithOutOfRangeOffset(type
, name
) {
777 setWithInvalidOffset(type
, name
, 32, type
, name
, 16,
778 0x7FFFFFF8, "out-of-range");
781 function testSettingFromArrayWithNegativeOffset(type
, name
) {
782 setWithInvalidOffset(type
, name
, 32, Array
, "array", 16,
786 function testSettingFromTypedArrayWithNegativeOffset(type
, name
) {
787 setWithInvalidOffset(type
, name
, 32, type
, name
, 16,
791 function testSettingFromArrayWithMinusZeroOffset(type
, name
) {
792 setWithValidOffset(type
, name
, 32, Array
, "array", 16,
796 function testSettingFromTypedArrayWithMinusZeroOffset(type
, name
) {
797 setWithValidOffset(type
, name
, 32, type
, name
, 16,
801 function testSettingFromArrayWithBoundaryOffset(type
, name
) {
802 setWithValidOffset(type
, name
, 32, Array
, "array", 16,
806 function testSettingFromTypedArrayWithBoundaryOffset(type
, name
) {
807 setWithValidOffset(type
, name
, 32, type
, name
, 16,
811 function testSettingFromArrayWithNonIntegerOffset(type
, name
) {
812 setWithValidOffset(type
, name
, 32, Array
, "array", 16,
813 16.999, "non-integer");
816 function testSettingFromTypedArrayWithNonIntegerOffset(type
, name
) {
817 setWithValidOffset(type
, name
, 32, type
, name
, 16,
818 16.999, "non-integer");
821 function testSettingFromFakeArrayWithOutOfRangeLength(type
, name
) {
822 var webglArray
= new type(32);
824 array
.length
= 0x80000000;
826 webglArray
.set(array
, 8);
827 testFailed("Setting " + name
+ " from fake array with invalid length was not caught");
829 testPassed("Setting " + name
+ " from fake array with invalid length was caught");
834 function negativeTestGetAndSetMethods(type
, name
) {
835 array
= new type([2, 3]);
836 shouldBeUndefined("array.get");
837 var exceptionThrown
= false;
838 // We deliberately check for an exception here rather than using
839 // shouldThrow here because the precise contents of the syntax
840 // error are not specified.
842 webGLArray
.set(0, 1);
844 exceptionThrown
= true;
846 var output
= "array.set(0, 1) ";
847 if (exceptionThrown
) {
848 testPassed(output
+ "threw exception.");
850 testFailed(output
+ "did not throw exception.");
854 function testNaNConversion(type
, name
) {
855 running('test storing NaN in ' + name
);
857 var array
= new type([1, 1]);
860 // The code block in each of the case statements below is identical, but some
861 // JavaScript engines need separate copies in order to exercise all of
862 // their optimized code paths.
866 for (var i
= 0; i
< array
.length
; ++i
) {
868 results
[i
] = array
[i
];
872 for (var i
= 0; i
< array
.length
; ++i
) {
874 results
[i
] = array
[i
];
878 for (var i
= 0; i
< array
.length
; ++i
) {
880 results
[i
] = array
[i
];
884 for (var i
= 0; i
< array
.length
; ++i
) {
886 results
[i
] = array
[i
];
890 for (var i
= 0; i
< array
.length
; ++i
) {
892 results
[i
] = array
[i
];
896 for (var i
= 0; i
< array
.length
; ++i
) {
898 results
[i
] = array
[i
];
901 case Uint8ClampedArray
:
902 for (var i
= 0; i
< array
.length
; ++i
) {
904 results
[i
] = array
[i
];
908 for (var i
= 0; i
< array
.length
; ++i
) {
910 results
[i
] = array
[i
];
914 for (var i
= 0; i
< array
.length
; ++i
) {
916 results
[i
] = array
[i
];
920 fail("Unhandled type");
924 // Some types preserve NaN values; all other types convert NaN to zero.
925 if (type
=== Float32Array
|| type
=== Float64Array
) {
926 assert('initial NaN preserved', isNaN(new type([NaN
])[0]));
927 for (var i
= 0; i
< array
.length
; ++i
)
928 assert('NaN preserved via setter', isNaN(results
[i
]));
930 assertEq('initial NaN converted to zero', 0, new type([NaN
])[0]);
931 for (var i
= 0; i
< array
.length
; ++i
)
932 assertEq('NaN converted to zero by setter', 0, results
[i
]);
945 function runTests() {
949 testInheritanceHierarchy();
951 for (var i
= 0; i
< testCases
.length
; i
++) {
952 var testCase
= testCases
[i
];
953 running(testCase
.name
);
954 if (!(testCase
.name
in window
)) {
955 fail("does not exist");
958 var type
= window
[testCase
.name
];
959 var name
= testCase
.name
;
960 if (testCase
.unsigned
) {
961 testSetAndGet10To1(type
, name
);
962 testConstructWithArrayOfUnsignedValues(type
, name
);
963 testConstructWithTypedArrayOfUnsignedValues(type
, name
);
965 testSetAndGetPos10ToNeg10(type
, name
);
966 testConstructWithArrayOfSignedValues(type
, name
);
967 testConstructWithTypedArrayOfSignedValues(type
, name
);
969 if (testCase
.integral
) {
970 testIntegralArrayTruncationBehavior(type
, name
, testCase
.unsigned
);
972 testGetWithOutOfRangeIndices(type
, name
);
973 testOffsetsAndSizes(type
, name
, testCase
.elementSizeInBytes
);
974 testSetFromTypedArray(type
, name
);
975 negativeTestSetFromTypedArray(type
, name
);
976 testSetFromArray(type
, name
);
977 negativeTestSetFromArray(type
, name
);
978 testSubarray(type
, name
);
979 testSubarrayOffsetAndLengthClamping(type
, name
);
980 negativeTestSubarray(type
, name
);
981 testSetBoundaryConditions(type
,
984 testCase
.expectedValues
);
985 testConstructionBoundaryConditions(type
,
988 testCase
.expectedValues
);
989 testConstructionWithNullBuffer(type
, name
);
990 testConstructionWithExceptionThrowingObject(type
, name
);
991 testConstructionWithOutOfRangeValues(type
, name
);
992 testConstructionWithNegativeOutOfRangeValues(type
, name
);
993 testConstructionWithUnalignedOffset(type
, name
, testCase
.elementSizeInBytes
);
994 testConstructionWithUnalignedLength(type
, name
, testCase
.elementSizeInBytes
);
995 testConstructionWithBothArrayBufferAndLength(type
, name
, testCase
.elementSizeInBytes
);
996 testConstructionWithSubPortionOfArrayBuffer(type
, name
, testCase
.elementSizeInBytes
);
997 testSubarrayWithOutOfRangeValues(type
, name
, testCase
.elementSizeInBytes
);
998 testSubarrayWithDefaultValues(type
, name
, testCase
.elementSizeInBytes
);
999 testSettingFromArrayWithOutOfRangeOffset(type
, name
);
1000 testSettingFromTypedArrayWithOutOfRangeOffset(type
, name
);
1001 testSettingFromArrayWithNegativeOffset(type
, name
);
1002 testSettingFromTypedArrayWithNegativeOffset(type
, name
);
1003 testSettingFromArrayWithMinusZeroOffset(type
, name
);
1004 testSettingFromTypedArrayWithMinusZeroOffset(type
, name
);
1005 testSettingFromArrayWithBoundaryOffset(type
, name
);
1006 testSettingFromTypedArrayWithBoundaryOffset(type
, name
);
1007 testSettingFromArrayWithNonIntegerOffset(type
, name
);
1008 testSettingFromTypedArrayWithNonIntegerOffset(type
, name
);
1009 testSettingFromFakeArrayWithOutOfRangeLength(type
, name
);
1010 negativeTestGetAndSetMethods(type
, name
);
1011 testNaNConversion(type
, name
);
1018 var successfullyParsed
= true;