OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 Copyright (C) 2011 Apple Computer, Inc. All rights reserved. |
| 4 |
| 5 Redistribution and use in source and binary forms, with or without |
| 6 modification, are permitted provided that the following conditions are |
| 7 met: |
| 8 |
| 9 * Redistributions of source code must retain the above copyright |
| 10 notice, this list of conditions and the following disclaimer. |
| 11 * Redistributions in binary form must reproduce the above |
| 12 copyright notice, this list of conditions and the following disclaimer |
| 13 in the documentation and/or other materials provided with the |
| 14 distribution. |
| 15 * Neither the name of Google Inc. nor the names of its |
| 16 contributors may be used to endorse or promote products derived from |
| 17 this software without specific prior written permission. |
| 18 |
| 19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 --> |
| 31 <!DOCTYPE html> |
| 32 <html> |
| 33 <head> |
| 34 <meta charset="utf-8"> |
| 35 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
| 36 <script src="../../resources/js-test-pre.js"></script> |
| 37 <script src="../resources/webgl-test.js"></script> |
| 38 </head> |
| 39 <body> |
| 40 <div id="description"></div> |
| 41 <div id="console"></div> |
| 42 |
| 43 <script> |
| 44 |
| 45 description("Verifies the functionality of the new array-like objects in the Typ
edArray spec"); |
| 46 |
| 47 var currentlyRunning = ''; |
| 48 var allPassed = true; |
| 49 function running(str) { |
| 50 currentlyRunning = str; |
| 51 } |
| 52 |
| 53 function output(str) { |
| 54 debug(str); |
| 55 } |
| 56 |
| 57 function pass() { |
| 58 testPassed(currentlyRunning); |
| 59 } |
| 60 |
| 61 function fail(str) { |
| 62 allPassed = false; |
| 63 var exc; |
| 64 if (str) |
| 65 exc = currentlyRunning + ': ' + str; |
| 66 else |
| 67 exc = currentlyRunning; |
| 68 testFailed(exc); |
| 69 } |
| 70 |
| 71 function assertEq(prefix, expected, val) { |
| 72 if (expected != val) { |
| 73 var str = prefix + ': expected ' + expected + ', got ' + val; |
| 74 throw str; |
| 75 } |
| 76 } |
| 77 |
| 78 function assert(prefix, expected) { |
| 79 if (!expected) { |
| 80 var str = prefix + ': expected value / true'; |
| 81 throw str; |
| 82 } |
| 83 } |
| 84 |
| 85 function printSummary() { |
| 86 if (allPassed) { |
| 87 debug("Test passed."); |
| 88 } else { |
| 89 debug("TEST FAILED"); |
| 90 } |
| 91 } |
| 92 |
| 93 var byteLength; |
| 94 var subBuffer; |
| 95 function testSlice() { |
| 96 function test(subBuf, starts, size) { |
| 97 byteLength = size; |
| 98 subBuffer = eval(subBuf); |
| 99 var subArray = new Int8Array(subBuffer); |
| 100 assertEq(subBuf, subBuffer.byteLength, byteLength); |
| 101 for (var i = 0; i < size; ++i) |
| 102 assertEq('Element ' + i, starts + i, subArray[i]); |
| 103 } |
| 104 |
| 105 try { |
| 106 running('testSlice'); |
| 107 var buffer = new ArrayBuffer(32); |
| 108 var array = new Int8Array(buffer); |
| 109 for (var i = 0; i < 32; ++i) |
| 110 array[i] = i; |
| 111 |
| 112 test("buffer.slice(0)", 0, 32); |
| 113 test("buffer.slice(16)", 16, 16); |
| 114 test("buffer.slice(24)", 24, 8); |
| 115 test("buffer.slice(32)", 32, 0); |
| 116 test("buffer.slice(40)", 32, 0); |
| 117 test("buffer.slice(80)", 32, 0); |
| 118 |
| 119 test("buffer.slice(-8)", 24, 8); |
| 120 test("buffer.slice(-16)", 16, 16); |
| 121 test("buffer.slice(-24)", 8, 24); |
| 122 test("buffer.slice(-32)", 0, 32); |
| 123 test("buffer.slice(-40)", 0, 32); |
| 124 test("buffer.slice(-80)", 0, 32); |
| 125 |
| 126 test("buffer.slice(0, 32)", 0, 32); |
| 127 test("buffer.slice(0, 16)", 0, 16); |
| 128 test("buffer.slice(8, 24)", 8, 16); |
| 129 test("buffer.slice(16, 32)", 16, 16); |
| 130 test("buffer.slice(24, 16)", 24, 0); |
| 131 |
| 132 test("buffer.slice(16, -8)", 16, 8); |
| 133 test("buffer.slice(-20, 30)", 12, 18); |
| 134 |
| 135 test("buffer.slice(-8, -20)", 24, 0); |
| 136 test("buffer.slice(-20, -8)", 12, 12); |
| 137 test("buffer.slice(-40, 16)", 0, 16); |
| 138 test("buffer.slice(-40, 40)", 0, 32); |
| 139 pass(); |
| 140 } catch (e) { |
| 141 fail(e); |
| 142 } |
| 143 } |
| 144 |
| 145 // |
| 146 // Tests for unsigned array variants |
| 147 // |
| 148 |
| 149 function testSetAndGet10To1(type, name) { |
| 150 running('test ' + name + ' SetAndGet10To1'); |
| 151 try { |
| 152 var array = new type(10); |
| 153 for (var i = 0; i < 10; i++) { |
| 154 array[i] = 10 - i; |
| 155 } |
| 156 for (var i = 0; i < 10; i++) { |
| 157 assertEq('Element ' + i, 10 - i, array[i]); |
| 158 } |
| 159 pass(); |
| 160 } catch (e) { |
| 161 fail(e); |
| 162 } |
| 163 } |
| 164 |
| 165 function testConstructWithArrayOfUnsignedValues(type, name) { |
| 166 running('test ' + name + ' ConstructWithArrayOfUnsignedValues'); |
| 167 try { |
| 168 var array = new type([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); |
| 169 assertEq('Array length', 10, array.length); |
| 170 for (var i = 0; i < 10; i++) { |
| 171 assertEq('Element ' + i, 10 - i, array[i]); |
| 172 } |
| 173 pass(); |
| 174 } catch (e) { |
| 175 fail(e); |
| 176 } |
| 177 } |
| 178 |
| 179 function testConstructWithTypedArrayOfUnsignedValues(type, name) { |
| 180 running('test ' + name + ' ConstructWithTypedArrayOfUnsignedValues'); |
| 181 try { |
| 182 var tmp = new type([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); |
| 183 var array = new type(tmp); |
| 184 assertEq('Array length', 10, array.length); |
| 185 for (var i = 0; i < 10; i++) { |
| 186 assertEq('Element ' + i, 10 - i, array[i]); |
| 187 } |
| 188 pass(); |
| 189 } catch (e) { |
| 190 fail(e); |
| 191 } |
| 192 } |
| 193 |
| 194 // |
| 195 // Tests for signed array variants |
| 196 // |
| 197 |
| 198 function testSetAndGetPos10ToNeg10(type, name) { |
| 199 running('test ' + name + ' SetAndGetPos10ToNeg10'); |
| 200 try { |
| 201 var array = new type(21); |
| 202 for (var i = 0; i < 21; i++) { |
| 203 array[i] = 10 - i; |
| 204 } |
| 205 for (var i = 0; i < 21; i++) { |
| 206 assertEq('Element ' + i, 10 - i, array[i]); |
| 207 } |
| 208 pass(); |
| 209 } catch (e) { |
| 210 fail(e); |
| 211 } |
| 212 } |
| 213 |
| 214 function testConstructWithArrayOfSignedValues(type, name) { |
| 215 running('test ' + name + ' ConstructWithArrayOfSignedValues'); |
| 216 try { |
| 217 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]); |
| 218 assertEq('Array length', 21, array.length); |
| 219 for (var i = 0; i < 21; i++) { |
| 220 assertEq('Element ' + i, 10 - i, array[i]); |
| 221 } |
| 222 pass(); |
| 223 } catch (e) { |
| 224 fail(e); |
| 225 } |
| 226 } |
| 227 |
| 228 function testConstructWithTypedArrayOfSignedValues(type, name) { |
| 229 running('test ' + name + ' ConstructWithTypedArrayOfSignedValues'); |
| 230 try { |
| 231 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]); |
| 232 var array = new type(tmp); |
| 233 assertEq('Array length', 21, array.length); |
| 234 for (var i = 0; i < 21; i++) { |
| 235 assertEq('Element ' + i, 10 - i, array[i]); |
| 236 } |
| 237 pass(); |
| 238 } catch (e) { |
| 239 fail(e); |
| 240 } |
| 241 } |
| 242 |
| 243 // |
| 244 // Test cases for integral types. |
| 245 // Some JavaScript engines need separate copies of this code in order |
| 246 // to exercise all of their optimized code paths. |
| 247 // |
| 248 |
| 249 function testIntegralArrayTruncationBehavior(type, name, unsigned) { |
| 250 running('test integral array truncation behavior for ' + name); |
| 251 |
| 252 var sourceData; |
| 253 var expectedResults; |
| 254 |
| 255 if (unsigned) { |
| 256 sourceData = [0.6, 10.6]; |
| 257 if (type === Uint8ClampedArray) { |
| 258 expectedResults = [1, 11]; |
| 259 } else { |
| 260 expectedResults = [0, 10]; |
| 261 } |
| 262 } else { |
| 263 sourceData = [0.6, 10.6, -0.6, -10.6]; |
| 264 expectedResults = [0, 10, 0, -10]; |
| 265 } |
| 266 |
| 267 var numIterations = 10; |
| 268 var array = new type(numIterations); |
| 269 |
| 270 // The code block in each of the case statements below is identical, but some |
| 271 // JavaScript engines need separate copies in order to exercise all of |
| 272 // their optimized code paths. |
| 273 |
| 274 try { |
| 275 switch (type) { |
| 276 case Int8Array: |
| 277 for (var ii = 0; ii < sourceData.length; ++ii) { |
| 278 for (var jj = 0; jj < numIterations; ++jj) { |
| 279 array[jj] = sourceData[ii]; |
| 280 assertEq('Storing ' + sourceData[ii], expectedResults[ii], array[jj]); |
| 281 } |
| 282 } |
| 283 break; |
| 284 case Int16Array: |
| 285 for (var ii = 0; ii < sourceData.length; ++ii) { |
| 286 for (var jj = 0; jj < numIterations; ++jj) { |
| 287 array[jj] = sourceData[ii]; |
| 288 assertEq('Storing ' + sourceData[ii], expectedResults[ii], array[jj]); |
| 289 } |
| 290 } |
| 291 break; |
| 292 case Int32Array: |
| 293 for (var ii = 0; ii < sourceData.length; ++ii) { |
| 294 for (var jj = 0; jj < numIterations; ++jj) { |
| 295 array[jj] = sourceData[ii]; |
| 296 assertEq('Storing ' + sourceData[ii], expectedResults[ii], array[jj]); |
| 297 } |
| 298 } |
| 299 break; |
| 300 case Uint8Array: |
| 301 for (var ii = 0; ii < sourceData.length; ++ii) { |
| 302 for (var jj = 0; jj < numIterations; ++jj) { |
| 303 array[jj] = sourceData[ii]; |
| 304 assertEq('Storing ' + sourceData[ii], expectedResults[ii], array[jj]); |
| 305 } |
| 306 } |
| 307 break; |
| 308 case Uint8ClampedArray: |
| 309 for (var ii = 0; ii < sourceData.length; ++ii) { |
| 310 for (var jj = 0; jj < numIterations; ++jj) { |
| 311 array[jj] = sourceData[ii]; |
| 312 assertEq('Storing ' + sourceData[ii], expectedResults[ii], array[jj]); |
| 313 } |
| 314 } |
| 315 break; |
| 316 case Uint16Array: |
| 317 for (var ii = 0; ii < sourceData.length; ++ii) { |
| 318 for (var jj = 0; jj < numIterations; ++jj) { |
| 319 array[jj] = sourceData[ii]; |
| 320 assertEq('Storing ' + sourceData[ii], expectedResults[ii], array[jj]); |
| 321 } |
| 322 } |
| 323 break; |
| 324 case Uint32Array: |
| 325 for (var ii = 0; ii < sourceData.length; ++ii) { |
| 326 for (var jj = 0; jj < numIterations; ++jj) { |
| 327 array[jj] = sourceData[ii]; |
| 328 assertEq('Storing ' + sourceData[ii], expectedResults[ii], array[jj]); |
| 329 } |
| 330 } |
| 331 break; |
| 332 default: |
| 333 fail("Unhandled type"); |
| 334 break; |
| 335 } |
| 336 |
| 337 pass(); |
| 338 } catch (e) { |
| 339 fail(e); |
| 340 } |
| 341 } |
| 342 |
| 343 |
| 344 // |
| 345 // Test cases for both signed and unsigned types |
| 346 // |
| 347 |
| 348 function testGetWithOutOfRangeIndices(type, name) { |
| 349 debug('Testing ' + name + ' GetWithOutOfRangeIndices'); |
| 350 // See below for declaration of this global variable |
| 351 array = new type([2, 3]); |
| 352 shouldBeUndefined("array[2]"); |
| 353 shouldBeUndefined("array[-1]"); |
| 354 shouldBeUndefined("array[0x20000000]"); |
| 355 } |
| 356 |
| 357 function testOffsetsAndSizes(type, name, elementSizeInBytes) { |
| 358 running('test ' + name + ' OffsetsAndSizes'); |
| 359 try { |
| 360 var len = 10; |
| 361 assertEq('type.BYTES_PER_ELEMENT', elementSizeInBytes, type.BYTES_PER_ELEMEN
T); |
| 362 var array = new type(len); |
| 363 assert('array.buffer', array.buffer); |
| 364 assertEq('array.byteOffset', 0, array.byteOffset); |
| 365 assertEq('array.length', len, array.length); |
| 366 assertEq('array.byteLength', len * elementSizeInBytes, array.byteLength); |
| 367 array = new type(array.buffer, elementSizeInBytes, len - 1); |
| 368 assert('array.buffer', array.buffer); |
| 369 assertEq('array.byteOffset', elementSizeInBytes, array.byteOffset); |
| 370 assertEq('array.length', len - 1, array.length); |
| 371 assertEq('array.byteLength', (len - 1) * elementSizeInBytes, array.byteLengt
h); |
| 372 pass(); |
| 373 } catch (e) { |
| 374 fail(e); |
| 375 } |
| 376 } |
| 377 |
| 378 function testSetFromTypedArray(type, name) { |
| 379 running('test ' + name + ' SetFromTypedArray'); |
| 380 try { |
| 381 var array = new type(10); |
| 382 var array2 = new type(5); |
| 383 for (var i = 0; i < 10; i++) { |
| 384 assertEq('Element ' + i, 0, array[i]); |
| 385 } |
| 386 for (var i = 0; i < array2.length; i++) { |
| 387 array2[i] = i; |
| 388 } |
| 389 array.set(array2); |
| 390 for (var i = 0; i < array2.length; i++) { |
| 391 assertEq('Element ' + i, i, array[i]); |
| 392 } |
| 393 array.set(array2, 5); |
| 394 for (var i = 0; i < array2.length; i++) { |
| 395 assertEq('Element ' + i, i, array[5 + i]); |
| 396 } |
| 397 pass(); |
| 398 } catch (e) { |
| 399 fail(e); |
| 400 } |
| 401 } |
| 402 |
| 403 function negativeTestSetFromTypedArray(type, name) { |
| 404 running('negativeTest ' + name + ' SetFromTypedArray'); |
| 405 try { |
| 406 var array = new type(5); |
| 407 var array2 = new type(6); |
| 408 for (var i = 0; i < 5; i++) { |
| 409 assertEq('Element ' + i, 0, array[i]); |
| 410 } |
| 411 for (var i = 0; i < array2.length; i++) { |
| 412 array2[i] = i; |
| 413 } |
| 414 try { |
| 415 array.set(array2); |
| 416 fail('Expected exception from array.set(array2)'); |
| 417 return; |
| 418 } catch (e) { |
| 419 } |
| 420 try { |
| 421 array2.set(array, 2); |
| 422 fail('Expected exception from array2.set(array, 2)'); |
| 423 return; |
| 424 } catch (e) { |
| 425 } |
| 426 pass(); |
| 427 } catch (e) { |
| 428 fail(e); |
| 429 } |
| 430 } |
| 431 |
| 432 function testSetFromArray(type, name) { |
| 433 running('test ' + name + ' SetFromArray'); |
| 434 try { |
| 435 var array = new type(10); |
| 436 var array2 = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; |
| 437 for (var i = 0; i < 10; i++) { |
| 438 assertEq('Element ' + i, 0, array[i]); |
| 439 } |
| 440 array.set(array2, 0); |
| 441 for (var i = 0; i < array2.length; i++) { |
| 442 assertEq('Element ' + i, 10 - i, array[i]); |
| 443 } |
| 444 pass(); |
| 445 } catch (e) { |
| 446 fail(e); |
| 447 } |
| 448 } |
| 449 |
| 450 function negativeTestSetFromArray(type, name) { |
| 451 running('negativeTest ' + name + ' SetFromArray'); |
| 452 try { |
| 453 var array = new type([2, 3]); |
| 454 try { |
| 455 array.set([4, 5], 1); |
| 456 fail(); |
| 457 return; |
| 458 } catch (e) { |
| 459 } |
| 460 try { |
| 461 array.set([4, 5, 6]); |
| 462 fail(); |
| 463 return; |
| 464 } catch (e) { |
| 465 } |
| 466 pass(); |
| 467 } catch (e) { |
| 468 fail(e); |
| 469 } |
| 470 } |
| 471 |
| 472 function testSubarray(type, name) { |
| 473 running('test ' + name + ' Subarray'); |
| 474 try { |
| 475 var array = new type([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
| 476 var subarray = array.subarray(0, 5); |
| 477 assertEq('subarray.length', 5, subarray.length); |
| 478 for (var i = 0; i < 5; i++) { |
| 479 assertEq('Element ' + i, i, subarray[i]); |
| 480 } |
| 481 subarray = array.subarray(4, 10); |
| 482 assertEq('subarray.length', 6, subarray.length); |
| 483 for (var i = 0; i < 6; i++) { |
| 484 assertEq('Element ' + i, 4 + i, subarray[i]); |
| 485 } |
| 486 pass(); |
| 487 } catch (e) { |
| 488 fail(e); |
| 489 } |
| 490 } |
| 491 |
| 492 function negativeTestSubarray(type, name) { |
| 493 running('negativeTest ' + name + ' Subarray'); |
| 494 try { |
| 495 var array = new type([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
| 496 subarray = array.subarray(5, 11); |
| 497 if (subarray.length != 5) { |
| 498 fail(); |
| 499 return; |
| 500 } |
| 501 subarray = array.subarray(10, 10); |
| 502 if (subarray.length != 0) { |
| 503 fail(); |
| 504 return; |
| 505 } |
| 506 pass(); |
| 507 } catch (e) { |
| 508 fail(e); |
| 509 } |
| 510 } |
| 511 |
| 512 function testSetBoundaryConditions(type, name, testValues, expectedValues) { |
| 513 running('test ' + name + ' SetBoundaryConditions'); |
| 514 try { |
| 515 var array = new type(1); |
| 516 assertEq('Array length', 1, array.length); |
| 517 for (var ii = 0; ii < testValues.length; ++ii) { |
| 518 for (var jj = 0; jj < 10; ++jj) { |
| 519 array[0] = testValues[ii]; |
| 520 assertEq('Element 0', expectedValues[ii], array[0]); |
| 521 } |
| 522 } |
| 523 pass(); |
| 524 } catch (e) { |
| 525 fail(e); |
| 526 } |
| 527 } |
| 528 |
| 529 function testConstructionBoundaryConditions(type, name, testValues, expectedValu
es) { |
| 530 running('test ' + name + ' ConstructionBoundaryConditions'); |
| 531 try { |
| 532 var array = new type(testValues); |
| 533 assertEq('Array length', testValues.length, array.length); |
| 534 for (var ii = 0; ii < testValues.length; ++ii) { |
| 535 assertEq('Element ' + ii, expectedValues[ii], array[ii]); |
| 536 } |
| 537 pass(); |
| 538 } catch (e) { |
| 539 fail(e); |
| 540 } |
| 541 } |
| 542 |
| 543 function testConstructionWithNullBuffer(type, name) { |
| 544 var array; |
| 545 try { |
| 546 array = new type(null); |
| 547 testFailed("Construction of " + name + " with null buffer should throw e
xception"); |
| 548 } catch (e) { |
| 549 testPassed("Construction of " + name + " with null buffer threw exceptio
n"); |
| 550 } |
| 551 try { |
| 552 array = new type(null, 0, 0); |
| 553 testFailed("Construction of " + name + " with (null buffer, 0) should th
row exception"); |
| 554 } catch (e) { |
| 555 testPassed("Construction of " + name + " with (null buffer, 0) threw exc
eption"); |
| 556 } |
| 557 try { |
| 558 array = new type(null, 0, 0); |
| 559 testFailed("Construction of " + name + " with (null buffer, 0, 0) should
throw exception"); |
| 560 } catch (e) { |
| 561 testPassed("Construction of " + name + " with (null buffer, 0, 0) threw
exception"); |
| 562 } |
| 563 } |
| 564 |
| 565 function shouldThrowIndexSizeErr(func, text) { |
| 566 var errorText = text + " should throw an exception"; |
| 567 try { |
| 568 func(); |
| 569 testFailed(errorText); |
| 570 } catch (e) { |
| 571 testPassed(text + " threw an exception"); |
| 572 } |
| 573 } |
| 574 |
| 575 function testConstructionWithOutOfRangeValues(type, name) { |
| 576 shouldThrowIndexSizeErr(function() { |
| 577 var buffer = new ArrayBuffer(4); |
| 578 var array = new type(buffer, 4, 0x3FFFFFFF); |
| 579 }, "Construction of " + name + " with out-of-range number of elements"); |
| 580 shouldThrowIndexSizeErr(function() { |
| 581 var buffer = new ArrayBuffer(4); |
| 582 var array = new type(buffer, 8); |
| 583 }, "Construction of " + name + " with out-of-range offset"); |
| 584 } |
| 585 |
| 586 function testConstructionWithNegativeOutOfRangeValues(type, name) { |
| 587 try { |
| 588 var buffer = new ArrayBuffer(-1); |
| 589 testFailed("Construction of ArrayBuffer with negative size should throw
exception"); |
| 590 } catch (e) { |
| 591 testPassed("Construction of ArrayBuffer with negative size threw excepti
on"); |
| 592 } |
| 593 try { |
| 594 var array = new type(-1); |
| 595 testFailed("Construction of " + name + " with negative size should throw
exception"); |
| 596 } catch (e) { |
| 597 testPassed("Construction of " + name + " with negative size threw except
ion"); |
| 598 } |
| 599 shouldThrowIndexSizeErr(function() { |
| 600 var buffer = new ArrayBuffer(4); |
| 601 var array = new type(buffer, 4, -2147483648); |
| 602 }, "Construction of " + name + " with negative out-of-range values"); |
| 603 } |
| 604 |
| 605 function testConstructionWithUnalignedOffset(type, name, elementSizeInBytes) { |
| 606 if (elementSizeInBytes > 1) { |
| 607 shouldThrowIndexSizeErr(function() { |
| 608 var buffer = new ArrayBuffer(32); |
| 609 var array = new type(buffer, 1, elementSizeInBytes); |
| 610 }, "Construction of " + name + " with unaligned offset"); |
| 611 } |
| 612 } |
| 613 |
| 614 function testConstructionWithUnalignedLength(type, name, elementSizeInBytes) { |
| 615 if (elementSizeInBytes > 1) { |
| 616 shouldThrowIndexSizeErr(function() { |
| 617 var buffer = new ArrayBuffer(elementSizeInBytes + 1); |
| 618 var array = new type(buffer, 0); |
| 619 }, "Construction of " + name + " with unaligned length"); |
| 620 } |
| 621 } |
| 622 |
| 623 function testConstructionOfHugeArray(type, name, sz) { |
| 624 if (sz == 1) |
| 625 return; |
| 626 try { |
| 627 // Construction of huge arrays must fail because byteLength is |
| 628 // an unsigned long |
| 629 array = new type(3000000000); |
| 630 testFailed("Construction of huge " + name + " should throw exception"); |
| 631 } catch (e) { |
| 632 testPassed("Construction of huge " + name + " threw exception"); |
| 633 } |
| 634 } |
| 635 |
| 636 function testConstructionWithBothArrayBufferAndLength(type, name, elementSizeInB
ytes) { |
| 637 var bufByteLength = 1000 * elementSizeInBytes; |
| 638 var buf = new ArrayBuffer(bufByteLength); |
| 639 var array1 = new type(buf); |
| 640 var array2 = new type(bufByteLength / elementSizeInBytes); |
| 641 if (array1.length == array2.length) { |
| 642 testPassed("Array lengths matched with explicit and implicit creation of
ArrayBuffer"); |
| 643 } else { |
| 644 testFailed("Array lengths DID NOT MATCH with explicit and implicit creat
ion of ArrayBuffer"); |
| 645 } |
| 646 } |
| 647 |
| 648 function testConstructionWithSubPortionOfArrayBuffer(type, name, elementSizeInBy
tes) { |
| 649 if (elementSizeInBytes > 1) { |
| 650 // Test construction with a valid sub-portion of an array buffer |
| 651 // (whose size is not an integral multiple of the element size). |
| 652 var size = 4 * elementSizeInBytes + (elementSizeInBytes / 2); |
| 653 var buf = new ArrayBuffer(size); |
| 654 try { |
| 655 var array = new type(buf, 0, 2); |
| 656 testPassed("new " + name + "(new ArrayBuffer(" + size + "), 0, 2) su
cceeded"); |
| 657 } catch (e) { |
| 658 testFailed("new " + name + "(new ArrayBuffer(" + size + "), 0, 2) fa
iled: " + e); |
| 659 } |
| 660 } |
| 661 } |
| 662 |
| 663 // These need to be global for shouldBe to see them |
| 664 var array; |
| 665 var typeSize; |
| 666 |
| 667 function testSubarrayWithOutOfRangeValues(type, name, sz) { |
| 668 debug("Testing subarray of " + name); |
| 669 try { |
| 670 var buffer = new ArrayBuffer(32); |
| 671 array = new type(buffer); |
| 672 typeSize = sz; |
| 673 shouldBe("array.length", "32 / typeSize"); |
| 674 try { |
| 675 shouldBe("array.subarray(4, 0x3FFFFFFF).length", "(32 / typeSize) -
4"); |
| 676 shouldBe("array.subarray(4, -2147483648).length", "0"); |
| 677 // Test subarray() against overflows. |
| 678 array = array.subarray(2); |
| 679 if (sz > 1) { |
| 680 // Full byte offset is +1 larger than the maximum unsigned long
int. |
| 681 // Make sure subarray() still handles it correctly. Otherwise o
verflow would happen and |
| 682 // offset would be 0, and array.length array.length would incorr
ectly be 1. |
| 683 var start = 4294967296 / sz - 2; |
| 684 array = array.subarray(start, start + 1); |
| 685 shouldBe("array.length", "0"); |
| 686 } |
| 687 } catch (e) { |
| 688 testFailed("Subarray of " + name + " threw exception"); |
| 689 } |
| 690 } catch (e) { |
| 691 testFailed("Exception: " + e); |
| 692 } |
| 693 } |
| 694 |
| 695 function testSubarrayWithDefaultValues(type, name, sz) { |
| 696 debug("Testing subarray with default inputs of " + name); |
| 697 try { |
| 698 var buffer = new ArrayBuffer(32); |
| 699 array = new type(buffer); |
| 700 typeSize = sz; |
| 701 shouldBe("array.length", "32 / typeSize"); |
| 702 try { |
| 703 shouldBe("array.subarray(0).length", "(32 / typeSize)"); |
| 704 shouldBe("array.subarray(2).length", "(32 / typeSize) - 2"); |
| 705 shouldBe("array.subarray(-2).length", "2"); |
| 706 shouldBe("array.subarray(-2147483648).length", "(32 / typeSize)"); |
| 707 } catch (e) { |
| 708 testFailed("Subarray of " + name + " threw exception"); |
| 709 } |
| 710 } catch (e) { |
| 711 testFailed("Exception: " + e); |
| 712 } |
| 713 } |
| 714 |
| 715 function setWithInvalidOffset(type, name, length, |
| 716 sourceType, sourceName, sourceLength, |
| 717 offset, offsetDescription) { |
| 718 var webglArray = new type(length); |
| 719 var sourceArray = new sourceType(sourceLength); |
| 720 for (var i = 0; i < sourceLength; i++) |
| 721 sourceArray[i] = 42 + i; |
| 722 try { |
| 723 webglArray.set(sourceArray, offset); |
| 724 testFailed("Setting " + name + " from " + sourceName + " with " + |
| 725 offsetDescription + " offset was not caught"); |
| 726 } catch (e) { |
| 727 testPassed("Setting " + name + " from " + sourceName + " with " + |
| 728 offsetDescription + " offset was caught"); |
| 729 } |
| 730 } |
| 731 |
| 732 function setWithValidOffset(type, name, length, |
| 733 sourceType, sourceName, sourceLength, |
| 734 offset, offsetDescription) { |
| 735 running("Setting " + name + " from " + sourceName + " with " + |
| 736 offsetDescription + " offset"); |
| 737 var webglArray = new type(length); |
| 738 var sourceArray = new sourceType(sourceLength); |
| 739 for (var i = 0; i < sourceLength; i++) |
| 740 sourceArray[i] = 42 + i; |
| 741 try { |
| 742 webglArray.set(sourceArray, offset); |
| 743 offset = Math.floor(offset); |
| 744 for (var i = 0; i < sourceLength; i++) { |
| 745 assertEq("Element " + i + offset, sourceArray[i], webglArray[i + off
set]); |
| 746 } |
| 747 pass(); |
| 748 } catch (e) { |
| 749 fail(e); |
| 750 } |
| 751 } |
| 752 |
| 753 |
| 754 function testSettingFromArrayWithOutOfRangeOffset(type, name) { |
| 755 setWithInvalidOffset(type, name, 32, Array, "array", 16, |
| 756 0x7FFFFFF8, "out-of-range"); |
| 757 } |
| 758 |
| 759 function testSettingFromTypedArrayWithOutOfRangeOffset(type, name) { |
| 760 setWithInvalidOffset(type, name, 32, type, name, 16, |
| 761 0x7FFFFFF8, "out-of-range"); |
| 762 } |
| 763 |
| 764 function testSettingFromArrayWithNegativeOffset(type, name) { |
| 765 setWithInvalidOffset(type, name, 32, Array, "array", 16, |
| 766 -1, "negative"); |
| 767 } |
| 768 |
| 769 function testSettingFromTypedArrayWithNegativeOffset(type, name) { |
| 770 setWithInvalidOffset(type, name, 32, type, name, 16, |
| 771 -1, "negative"); |
| 772 } |
| 773 |
| 774 function testSettingFromArrayWithMinusZeroOffset(type, name) { |
| 775 setWithValidOffset(type, name, 32, Array, "array", 16, |
| 776 -0, "-0"); |
| 777 } |
| 778 |
| 779 function testSettingFromTypedArrayWithMinusZeroOffset(type, name) { |
| 780 setWithValidOffset(type, name, 32, type, name, 16, |
| 781 -0, "-0"); |
| 782 } |
| 783 |
| 784 function testSettingFromArrayWithBoundaryOffset(type, name) { |
| 785 setWithValidOffset(type, name, 32, Array, "array", 16, |
| 786 16, "boundary"); |
| 787 } |
| 788 |
| 789 function testSettingFromTypedArrayWithBoundaryOffset(type, name) { |
| 790 setWithValidOffset(type, name, 32, type, name, 16, |
| 791 16, "boundary"); |
| 792 } |
| 793 |
| 794 function testSettingFromArrayWithNonIntegerOffset(type, name) { |
| 795 setWithValidOffset(type, name, 32, Array, "array", 16, |
| 796 16.999, "non-integer"); |
| 797 } |
| 798 |
| 799 function testSettingFromTypedArrayWithNonIntegerOffset(type, name) { |
| 800 setWithValidOffset(type, name, 32, type, name, 16, |
| 801 16.999, "non-integer"); |
| 802 } |
| 803 |
| 804 function testSettingFromFakeArrayWithOutOfRangeLength(type, name) { |
| 805 var webglArray = new type(32); |
| 806 var array = {}; |
| 807 array.length = 0x80000000; |
| 808 try { |
| 809 webglArray.set(array, 8); |
| 810 testFailed("Setting " + name + " from fake array with invalid length was
not caught"); |
| 811 } catch (e) { |
| 812 testPassed("Setting " + name + " from fake array with invalid length was
caught"); |
| 813 } |
| 814 } |
| 815 |
| 816 |
| 817 function negativeTestGetAndSetMethods(type, name) { |
| 818 array = new type([2, 3]); |
| 819 shouldBeUndefined("array.get"); |
| 820 var exceptionThrown = false; |
| 821 // We deliberately check for an exception here rather than using |
| 822 // shouldThrow here because the precise contents of the syntax |
| 823 // error are not specified. |
| 824 try { |
| 825 webGLArray.set(0, 1); |
| 826 } catch (e) { |
| 827 exceptionThrown = true; |
| 828 } |
| 829 var output = "array.set(0, 1) "; |
| 830 if (exceptionThrown) { |
| 831 testPassed(output + "threw exception."); |
| 832 } else { |
| 833 testFailed(output + "did not throw exception."); |
| 834 } |
| 835 } |
| 836 |
| 837 function testNaNConversion(type, name) { |
| 838 running('test storing NaN in ' + name); |
| 839 |
| 840 var array = new type([1, 1]); |
| 841 var results = []; |
| 842 |
| 843 // The code block in each of the case statements below is identical, but some |
| 844 // JavaScript engines need separate copies in order to exercise all of |
| 845 // their optimized code paths. |
| 846 try { |
| 847 switch (type) { |
| 848 case Float32Array: |
| 849 for (var i = 0; i < array.length; ++i) { |
| 850 array[i] = NaN; |
| 851 results[i] = array[i]; |
| 852 } |
| 853 break; |
| 854 case Float64Array: |
| 855 for (var i = 0; i < array.length; ++i) { |
| 856 array[i] = NaN; |
| 857 results[i] = array[i]; |
| 858 } |
| 859 break; |
| 860 case Int8Array: |
| 861 for (var i = 0; i < array.length; ++i) { |
| 862 array[i] = NaN; |
| 863 results[i] = array[i]; |
| 864 } |
| 865 break; |
| 866 case Int16Array: |
| 867 for (var i = 0; i < array.length; ++i) { |
| 868 array[i] = NaN; |
| 869 results[i] = array[i]; |
| 870 } |
| 871 break; |
| 872 case Int32Array: |
| 873 for (var i = 0; i < array.length; ++i) { |
| 874 array[i] = NaN; |
| 875 results[i] = array[i]; |
| 876 } |
| 877 break; |
| 878 case Uint8Array: |
| 879 for (var i = 0; i < array.length; ++i) { |
| 880 array[i] = NaN; |
| 881 results[i] = array[i]; |
| 882 } |
| 883 break; |
| 884 case Uint8ClampedArray: |
| 885 for (var i = 0; i < array.length; ++i) { |
| 886 array[i] = NaN; |
| 887 results[i] = array[i]; |
| 888 } |
| 889 break; |
| 890 case Uint16Array: |
| 891 for (var i = 0; i < array.length; ++i) { |
| 892 array[i] = NaN; |
| 893 results[i] = array[i]; |
| 894 } |
| 895 break; |
| 896 case Uint32Array: |
| 897 for (var i = 0; i < array.length; ++i) { |
| 898 array[i] = NaN; |
| 899 results[i] = array[i]; |
| 900 } |
| 901 break; |
| 902 default: |
| 903 fail("Unhandled type"); |
| 904 break; |
| 905 } |
| 906 |
| 907 // Some types preserve NaN values; all other types convert NaN to zero. |
| 908 if (type === Float32Array || type === Float64Array) { |
| 909 assert('initial NaN preserved', isNaN(new type([NaN])[0])); |
| 910 for (var i = 0; i < array.length; ++i) |
| 911 assert('NaN preserved via setter', isNaN(results[i])); |
| 912 } else { |
| 913 assertEq('initial NaN converted to zero', 0, new type([NaN])[0]); |
| 914 for (var i = 0; i < array.length; ++i) |
| 915 assertEq('NaN converted to zero by setter', 0, results[i]); |
| 916 } |
| 917 |
| 918 pass(); |
| 919 } catch (e) { |
| 920 fail(e); |
| 921 } |
| 922 } |
| 923 |
| 924 // |
| 925 // Test driver |
| 926 // |
| 927 |
| 928 function runTests() { |
| 929 allPassed = true; |
| 930 |
| 931 testSlice(); |
| 932 |
| 933 // The "name" attribute is a concession to browsers which don't |
| 934 // implement the "name" property on function objects |
| 935 var testCases = |
| 936 [ {name: "Float32Array", |
| 937 unsigned: false, |
| 938 integral: false, |
| 939 elementSizeInBytes: 4, |
| 940 testValues: [ -500.5, 500.5 ], |
| 941 expectedValues: [ -500.5, 500.5 ] |
| 942 }, |
| 943 {name: "Float64Array", |
| 944 unsigned: false, |
| 945 integral: false, |
| 946 elementSizeInBytes: 8, |
| 947 testValues: [ -500.5, 500.5 ], |
| 948 expectedValues: [ -500.5, 500.5 ] |
| 949 }, |
| 950 {name: "Int8Array", |
| 951 unsigned: false, |
| 952 integral: true, |
| 953 elementSizeInBytes: 1, |
| 954 testValues: [ -128, 127, -129, 128 ], |
| 955 expectedValues: [ -128, 127, 127, -128 ] |
| 956 }, |
| 957 {name: "Int16Array", |
| 958 unsigned: false, |
| 959 integral: true, |
| 960 elementSizeInBytes: 2, |
| 961 testValues: [ -32768, 32767, -32769, 32768 ], |
| 962 expectedValues: [ -32768, 32767, 32767, -32768 ] |
| 963 }, |
| 964 {name: "Int32Array", |
| 965 unsigned: false, |
| 966 integral: true, |
| 967 elementSizeInBytes: 4, |
| 968 testValues: [ -2147483648, 2147483647, -2147483649, 2147483648 ], |
| 969 expectedValues: [ -2147483648, 2147483647, 2147483647, -2147483648 ] |
| 970 }, |
| 971 {name: "Uint8Array", |
| 972 unsigned: true, |
| 973 integral: true, |
| 974 elementSizeInBytes: 1, |
| 975 testValues: [ 0, 255, -1, 256 ], |
| 976 expectedValues: [ 0, 255, 255, 0 ] |
| 977 }, |
| 978 {name: "Uint8ClampedArray", |
| 979 unsigned: true, |
| 980 integral: true, |
| 981 elementSizeInBytes: 1, |
| 982 testValues: [ 0, 255, -1, 256 ], |
| 983 expectedValues: [ 0, 255, 0, 255 ] |
| 984 }, |
| 985 {name: "Uint16Array", |
| 986 unsigned: true, |
| 987 integral: true, |
| 988 elementSizeInBytes: 2, |
| 989 testValues: [ 0, 65535, -1, 65536 ], |
| 990 expectedValues: [ 0, 65535, 65535, 0 ] |
| 991 }, |
| 992 {name: "Uint32Array", |
| 993 unsigned: true, |
| 994 integral: true, |
| 995 elementSizeInBytes: 4, |
| 996 testValues: [ 0, 4294967295, -1, 4294967296 ], |
| 997 expectedValues: [ 0, 4294967295, 4294967295, 0 ] |
| 998 } |
| 999 ]; |
| 1000 for (var i = 0; i < testCases.length; i++) { |
| 1001 var testCase = testCases[i]; |
| 1002 running(testCase.name); |
| 1003 if (!(testCase.name in window)) { |
| 1004 fail("does not exist"); |
| 1005 continue; |
| 1006 } |
| 1007 var type = window[testCase.name]; |
| 1008 var name = testCase.name; |
| 1009 if (testCase.unsigned) { |
| 1010 testSetAndGet10To1(type, name); |
| 1011 testConstructWithArrayOfUnsignedValues(type, name); |
| 1012 testConstructWithTypedArrayOfUnsignedValues(type, name); |
| 1013 } else { |
| 1014 testSetAndGetPos10ToNeg10(type, name); |
| 1015 testConstructWithArrayOfSignedValues(type, name); |
| 1016 testConstructWithTypedArrayOfSignedValues(type, name); |
| 1017 } |
| 1018 if (testCase.integral) { |
| 1019 testIntegralArrayTruncationBehavior(type, name, testCase.unsigned); |
| 1020 } |
| 1021 testGetWithOutOfRangeIndices(type, name); |
| 1022 testOffsetsAndSizes(type, name, testCase.elementSizeInBytes); |
| 1023 testSetFromTypedArray(type, name); |
| 1024 negativeTestSetFromTypedArray(type, name); |
| 1025 testSetFromArray(type, name); |
| 1026 negativeTestSetFromArray(type, name); |
| 1027 testSubarray(type, name); |
| 1028 negativeTestSubarray(type, name); |
| 1029 testSetBoundaryConditions(type, |
| 1030 name, |
| 1031 testCase.testValues, |
| 1032 testCase.expectedValues); |
| 1033 testConstructionBoundaryConditions(type, |
| 1034 name, |
| 1035 testCase.testValues, |
| 1036 testCase.expectedValues); |
| 1037 testConstructionWithNullBuffer(type, name); |
| 1038 testConstructionWithOutOfRangeValues(type, name); |
| 1039 testConstructionWithNegativeOutOfRangeValues(type, name); |
| 1040 testConstructionWithUnalignedOffset(type, name, testCase.elementSizeInBytes)
; |
| 1041 testConstructionWithUnalignedLength(type, name, testCase.elementSizeInBytes)
; |
| 1042 testConstructionOfHugeArray(type, name, testCase.elementSizeInBytes); |
| 1043 testConstructionWithBothArrayBufferAndLength(type, name, testCase.elementSiz
eInBytes); |
| 1044 testConstructionWithSubPortionOfArrayBuffer(type, name, testCase.elementSize
InBytes); |
| 1045 testSubarrayWithOutOfRangeValues(type, name, testCase.elementSizeInBytes); |
| 1046 testSubarrayWithDefaultValues(type, name, testCase.elementSizeInBytes); |
| 1047 testSettingFromArrayWithOutOfRangeOffset(type, name); |
| 1048 testSettingFromTypedArrayWithOutOfRangeOffset(type, name); |
| 1049 testSettingFromArrayWithNegativeOffset(type, name); |
| 1050 testSettingFromTypedArrayWithNegativeOffset(type, name); |
| 1051 testSettingFromArrayWithMinusZeroOffset(type, name); |
| 1052 testSettingFromTypedArrayWithMinusZeroOffset(type, name); |
| 1053 testSettingFromArrayWithBoundaryOffset(type, name); |
| 1054 testSettingFromTypedArrayWithBoundaryOffset(type, name); |
| 1055 testSettingFromArrayWithNonIntegerOffset(type, name); |
| 1056 testSettingFromTypedArrayWithNonIntegerOffset(type, name); |
| 1057 testSettingFromFakeArrayWithOutOfRangeLength(type, name); |
| 1058 negativeTestGetAndSetMethods(type, name); |
| 1059 testNaNConversion(type, name); |
| 1060 } |
| 1061 |
| 1062 printSummary(); |
| 1063 } |
| 1064 |
| 1065 runTests(); |
| 1066 successfullyParsed = true; |
| 1067 |
| 1068 </script> |
| 1069 <script src="../../resources/js-test-post.js"></script> |
| 1070 |
| 1071 </body> |
| 1072 </html> |
OLD | NEW |