1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/* 17 * @tc.name:sendabletypedarray 18 * @tc.desc:test sendabletypedarray 19 * @tc.type: FUNC 20 * @tc.require: issue#I9EBOZ 21 */ 22 23// @ts-nocheck 24declare function print(str: any): string; 25 26print("================Test Basic properties================"); 27[ 28 SendableInt8Array, 29 SendableInt16Array, 30 SendableInt32Array, 31 SendableFloat32Array, 32 SendableFloat64Array, 33 SendableBigInt64Array, 34 SendableUint8Array, 35 SendableUint16Array, 36 SendableUint8ClampedArray, 37 SendableUint32Array, 38 SendableBigUint64Array, 39].forEach((ctor: Function) => { 40 let array; 41 if (ctor == SendableBigInt64Array || ctor == SendableBigUint64Array) { 42 array = new ctor([1n, 2n, 3n, 4n]); 43 } else { 44 array = new ctor([1, 2, 3, 4]); 45 } 46 print('length: ' + array.length + ', byteLength: ' + array.byteLength); 47 print('BYTES_PER_ELEMENT: ' + ctor.BYTES_PER_ELEMENT + ', ' + array.BYTES_PER_ELEMENT); 48}); 49 50print("================Test Constructor================"); 51// Without New 52try { 53 let uint32 = SendableUint32Array([1, 2, 3, 4, 5]); 54} catch (e) { 55 print(e); 56} 57 58// From a length 59let uint32 = new SendableUint32Array(2); 60uint32[0] = 100; 61print(uint32); // [100, 0] 62 63// From an non-sendable array 64uint32 = new SendableUint32Array([11, 22]); 65print(uint32); // [11, 22] 66uint32 = new SendableUint32Array(new Array(11, 22)); 67print(uint32); // [11, 22] 68 69// From an sendable array 70uint32 = new SendableUint32Array(new SendableArray(11, 22, 33)); 71print(uint32); // [11, 22, 33] 72 73// From another non-sendable TypedArray 74let uint8 = new SendableUint8Array(new Uint8Array([11, 22, 33])); 75print(uint8); // [11, 22, 33] 76uint8 = new SendableUint8Array(new Uint32Array([1024, 11, 22])); 77print(uint8); // [0, 22, 33] 78 79// From another sendable TypedArray 80uint8 = new SendableUint8Array(new SendableUint8Array([11, 22, 33])); 81print(uint8); // [11, 22, 33] 82uint8 = new SendableUint8Array(new SendableUint32Array([1024, 11, 22])); 83print(uint8); // [0, 11, 22] 84 85// From an non-sendable ArrayBuffer 86try { 87 const buffer = new ArrayBuffer(64); 88 const float64 = new SendableFloat64Array(buffer, 8, 4); 89} catch (e) { 90 print('From an non-sendable ArrayBuffer: ' + e + ', errCode: ' + e.code); 91} 92 93// From an sendable ArrayBuffer 94try { 95 const buffer = new SendableArrayBuffer(64); 96 const float64 = new SendableFloat64Array(buffer, 8, 4); 97 float64.fill(0.1); 98 print(float64); 99} catch (e) { 100 print("error: " + e); 101} 102try { 103 let arrBuf = new SendableArrayBuffer(16); 104 let sliced = new SendableInt16Array(arrBuf.slice(4, 10)); 105 print(sliced); 106} catch (e) { 107 print(e); 108} 109try { 110 let arrBuf = new SendableArrayBuffer(16); 111 let sliced = new SendableInt16Array(arrBuf.slice(4, 7)); 112 print(sliced); 113} catch (e) { 114 print(e); 115} 116try { 117 let arrBuf = new SendableArrayBuffer(16); 118 let sliced = new SendableInt32Array(arrBuf.slice(4, 11)); 119 print(sliced); 120} catch (e) { 121 print(e); 122} 123try { 124 let arrBuf = new SendableArrayBuffer(16); 125 let sliced = new SendableFloat64Array(arrBuf.slice(4, 11)); 126 print(sliced); 127} catch (e) { 128 print(e); 129} 130// From an arrayLike 131class SendableObject { 132 constructor() { 133 'use sendable'; 134 } 135}; 136let sObj = new SendableObject(); 137let obj = new Object(); 138uint32 = new SendableUint32Array({ [0]: sObj, [1]: obj, [2]: 300, [3]: 4000, length: 4 }); 139print("From an arrayLike: " + uint32.length + ", array: [" + uint32 + "]"); 140 141// From an iterable 142const iterable = (function* () { 143 yield* [1, 2, 3]; 144})(); 145let float64FromIterable = new SendableFloat64Array(iterable); 146print("From an iterable: " + float64FromIterable.length + ", array: [" + float64FromIterable + "]"); 147print("================Test At================"); 148[ 149 SendableFloat64Array, 150 SendableFloat32Array, 151 SendableInt32Array, 152 SendableInt16Array, 153 SendableInt8Array, 154 SendableUint32Array, 155 SendableUint16Array, 156 SendableUint8Array, 157 SendableUint8ClampedArray, 158].forEach((ctor: Function) => { 159 if (testTypedArrayAt1(ctor)) { 160 print(ctor.name + ' test success'); 161 } else { 162 print(ctor.name + ' test failed'); 163 } 164}); 165 166[SendableBigInt64Array, SendableBigUint64Array].forEach((ctor: Function) => { 167 if (testTypedArrayAt2(ctor)) { 168 print(ctor.name + ' test success'); 169 } else { 170 print(ctor.name + ' test failed'); 171 } 172}); 173 174function testTypedArrayAt1(ctor) { 175 let result = [] 176 let obj = new ctor(5); 177 obj[0] = 10; 178 obj[1] = 11; 179 obj[2] = 12; 180 obj[3] = 13; 181 obj[4] = 14; 182 result.push(obj.at(-1) == 14); 183 result.push(obj.at(1.5) == 11); 184 result.push(obj.at(-3) == 12); 185 result.push(obj.at("3") == 13) 186 result.push(obj.at(-6) == undefined); 187 result.push(obj.at(5) == undefined); 188 result.push(obj.at({}) == 10); 189 for (let i = 0; i < result.length; i++) { 190 if (!result[i]) { 191 return false; 192 } 193 } 194 return true; 195} 196 197function testTypedArrayAt2(ctor) { 198 let result = [] 199 let obj = new ctor(5); 200 obj[0] = 10n; 201 obj[1] = 11n; 202 obj[2] = 12n; 203 obj[3] = 13n; 204 obj[4] = 9017199254740995n; 205 result.push(obj.at(-1) == 9017199254740995n); 206 result.push(obj.at(1.5) == 11n); 207 result.push(obj.at(-3) == 12n); 208 result.push(obj.at("3") == 13n) 209 result.push(obj.at(-6) == undefined); 210 result.push(obj.at(5) == undefined); 211 result.push(obj.at({}) == 10n); 212 for (let i = 0; i < result.length; i++) { 213 if (!result[i]) { 214 return false; 215 } 216 } 217 return true; 218} 219 220let uintc8 = new SendableUint8ClampedArray(2); 221uintc8[0] = 42; 222uintc8[1] = 1337; 223print(uintc8[0]); // 42 224print(uintc8[1]); // 255 (clamped) 225 226print("================Test Join================"); 227let constructors = [ 228 [SendableUint8Array, [0, 1]], 229 [SendableInt8Array, [0, 1]], 230 [SendableUint16Array, [0, 1]], 231 [SendableInt16Array, [0, 1]], 232 [SendableUint32Array, [0, 1]], 233 [SendableInt32Array, [0, 1]], 234 [SendableFloat32Array, [0, 1]], 235 [SendableFloat64Array, [0, 1]], 236 [SendableUint8ClampedArray, [0, 1]], 237 [SendableBigInt64Array, [0n, 1n]], 238 [SendableBigUint64Array, [0n, 1n]], 239]; 240 241let typedArray; 242const separator = { 243 toString() { 244 ArkTools.arrayBufferDetach(typedArray.buffer); 245 return '*'; 246 } 247}; 248 249constructors.forEach(([constructor, arr]) => { 250 typedArray = new constructor(arr); 251 print(typedArray.join() == '0,1'); 252 print(typedArray.join('-') == '0-1'); 253 print(typedArray.join(separator) == '*'); // detach array buffer 254}); 255 256try { 257 let typedArray2 = new SendableInt8Array(65537); 258 typedArray2.join(typedArray2); 259} catch(e) { 260 print(e); 261} 262 263print("================Test Subarray================"); 264[ 265 SendableFloat64Array, 266 SendableFloat32Array, 267 SendableInt32Array, 268 SendableInt16Array, 269 SendableInt8Array, 270 SendableUint32Array, 271 SendableUint16Array, 272 SendableUint8Array, 273 SendableUint8ClampedArray, 274].forEach((ctor: Function) => { 275 let obj = new ctor([10, 11, 12, 13, 14]); 276 let result = obj.subarray(); 277 print(result); 278 result = obj.subarray(1); 279 print(result); 280 result = obj.subarray(0, 5); 281 print(result); 282 result = obj.subarray(2, 10); 283 print(result); 284 result = obj.subarray(-1, 1); 285 print(result.length); 286 result = obj.subarray(2, -10); 287 print(result.length); 288}); 289 290print("================Test Includes================"); 291[ 292 [SendableUint8Array, [1, 2, 3]], 293 [SendableInt8Array, [1, 2, 3]], 294 [SendableUint16Array, [1, 2, 3]], 295 [SendableInt16Array, [1, 2, 3]], 296 [SendableUint32Array, [1, 2, 3]], 297 [SendableInt32Array, [1, 2, 3]], 298 [SendableFloat32Array, [1, 2, 3]], 299 [SendableFloat64Array, [1, 2, 3]], 300 [SendableUint8ClampedArray, [1, 2, 3]], 301 [SendableBigInt64Array, [1n, 2n, 3n]], 302 [SendableBigUint64Array, [1n, 2n, 3n]], 303].forEach(([constructor, arr]) => { 304 typedArray = new constructor(arr); 305 if (typedArray instanceof SendableBigInt64Array || typedArray instanceof SendableBigUint64Array) { 306 print(typedArray.includes(2n)); 307 print(typedArray.includes(4n)); 308 print(typedArray.includes(3n, 3)); 309 } else { 310 print(typedArray.includes(2)); 311 print(typedArray.includes(4)); 312 print(typedArray.includes(3, 3)); 313 } 314}); 315 316print("================Test Sort================"); 317[ 318 SendableFloat64Array, 319 SendableFloat32Array, 320 SendableInt32Array, 321 SendableInt16Array, 322 SendableInt8Array, 323 SendableUint32Array, 324 SendableUint16Array, 325 SendableUint8Array, 326 SendableUint8ClampedArray, 327].forEach((ctor: Function) => { 328 let array = new ctor([1, 21, 11, 31]); 329 array.sort(); 330 print(array); // [1, 11, 21, 31] 331 332 array = new ctor([1, 21, 11, 31]); 333 array.sort((a: number, b: number) => { 334 return b - a; 335 }); 336 print(array); // [31, 21, 11, 1] 337}); 338 339print("================Test Reverse================"); 340[ 341 SendableFloat64Array, 342 SendableFloat32Array, 343 SendableInt32Array, 344 SendableInt16Array, 345 SendableInt8Array, 346 SendableUint32Array, 347 SendableUint16Array, 348 SendableUint8Array, 349 SendableUint8ClampedArray, 350].forEach((ctor: Function) => { 351 let array = new ctor([1, 21, 11, 31]); 352 array.reverse(); 353 print(array); 354}); 355 356print("================Test Some================"); 357[ 358 SendableFloat64Array, 359 SendableFloat32Array, 360 SendableInt32Array, 361 SendableInt16Array, 362 SendableInt8Array, 363 SendableUint32Array, 364 SendableUint16Array, 365 SendableUint8Array, 366 SendableUint8ClampedArray, 367].forEach((ctor: Function) => { 368 const negatives = new ctor([-10, 20, -30, 40, -50]); 369 const positives = new ctor([10, 20, 30, 40, 50]); 370 print( 371 negatives.some((element: number) => { 372 return element < 0; 373 }), 374 ); 375 print( 376 positives.some((element: number) => { 377 return element < 0; 378 }), 379 ); 380}); 381 382print("================Test Every================"); 383[ 384 SendableFloat64Array, 385 SendableFloat32Array, 386 SendableInt32Array, 387 SendableInt16Array, 388 SendableInt8Array, 389 SendableUint32Array, 390 SendableUint16Array, 391 SendableUint8Array, 392 SendableUint8ClampedArray, 393].forEach((ctor: Function) => { 394 const negatives = new ctor([-10, -20, -30, -40, -50]); 395 print( 396 negatives.every((element: number) => { 397 return element < 0; 398 }), 399 ); 400}); 401 402print("================Test Slice================"); 403[ 404 SendableFloat64Array, 405 SendableFloat32Array, 406 SendableInt32Array, 407 SendableInt16Array, 408 SendableInt8Array, 409 SendableUint32Array, 410 SendableUint16Array, 411 SendableUint8Array, 412 SendableUint8ClampedArray, 413].forEach((ctor: Function) => { 414 let array = new ctor([10, 20, 30, 40, 50]); 415 let sliced = array.slice(); 416 print(sliced); // [10, 20, 30, 40, 50] 417 sliced = array.slice(1); 418 print(sliced); // [20, 30, 40, 50] 419 sliced = array.slice(0, 5); 420 print(sliced); // [10, 20, 30, 40, 50] 421 sliced = array.slice(2, 10); 422 print(sliced); // [30, 40, 50] 423 sliced = array.slice(-1, 1); 424 print(sliced.length); // 0 425 sliced = array.slice(2, -10); 426 print(sliced.length); // 0 427}); 428 429print("================Test IndexOf================"); 430[ 431 SendableFloat64Array, 432 SendableFloat32Array, 433 SendableInt32Array, 434 SendableInt16Array, 435 SendableInt8Array, 436 SendableUint32Array, 437 SendableUint16Array, 438 SendableUint8Array, 439 SendableUint8ClampedArray, 440].forEach((ctor: Function) => { 441 const array = new ctor([10, 20, 30, 40, 50]); 442 print(array.indexOf(50)); // 4 443 print(array.indexOf(20, 3)); // -1 444 print(array.indexOf(51)); // -1 445}); 446 447print("================Test FindIndex================"); 448[ 449 SendableFloat64Array, 450 SendableFloat32Array, 451 SendableInt32Array, 452 SendableInt16Array, 453 SendableInt8Array, 454 SendableUint32Array, 455 SendableUint16Array, 456 SendableUint8Array, 457 SendableUint8ClampedArray, 458].forEach((ctor: Function) => { 459 const array = new ctor([10, -20, 30, -40, 50]); 460 print( 461 array.findIndex((element: number) => { 462 return element < 0; 463 }), 464 ); 465}); 466 467print("================Test Find================"); 468[ 469 SendableFloat64Array, 470 SendableFloat32Array, 471 SendableInt32Array, 472 SendableInt16Array, 473 SendableInt8Array, 474 SendableUint32Array, 475 SendableUint16Array, 476 SendableUint8Array, 477 SendableUint8ClampedArray, 478].forEach((ctor: Function) => { 479 const array = new ctor([10, -20, 30, -40, 50]); 480 print( 481 array.find((element: number) => { 482 return element < 0; 483 }), 484 ); 485}); 486 487print("================Test Fill================"); 488[SendableFloat64Array, SendableInt32Array, SendableInt16Array, SendableInt8Array, SendableUint8ClampedArray].forEach( 489 (ctor: Function) => { 490 print(new ctor([1, 2, 3]).fill(4)); // [4, 4, 4] 491 print(new ctor([1, 2, 3]).fill(4, 1)); // [1, 4, 4] 492 print(new ctor([1, 2, 3]).fill(4, 1, 2)); // [1, 4, 3] 493 print(new ctor([1, 2, 3]).fill(4, 1, 1)); // [1, 2, 3] 494 print(new ctor([1, 2, 3]).fill(4, -3, -2)); // [4, 2, 3] 495 }, 496); 497 498print("================Test Filter================"); 499[ 500 SendableFloat64Array, 501 SendableFloat32Array, 502 SendableInt32Array, 503 SendableInt16Array, 504 SendableInt8Array, 505 SendableUint32Array, 506 SendableUint16Array, 507 SendableUint8Array, 508 SendableUint8ClampedArray, 509].forEach((ctor: Function) => { 510 const array = new ctor([-10, 20, -30, 40, -50]); 511 let filterd = array.filter((element: number) => { 512 return element < 0; 513 }); 514 print((filterd instanceof ctor) + ', filterd: [' + filterd + ']'); 515}); 516 517print("================Test CopyWith================"); 518[ 519 SendableFloat64Array, 520 SendableFloat32Array, 521 SendableInt32Array, 522 SendableInt16Array, 523 SendableInt8Array, 524 SendableUint32Array, 525 SendableUint16Array, 526 SendableUint8Array, 527 SendableUint8ClampedArray, 528].forEach((ctor: Function) => { 529 let array = new ctor([1, 2, 3, 4, 5, 6, 7, 8]); 530 let copied = array.copyWithin(3, 1, 3); 531 print((copied instanceof ctor) + ', copied: [' + copied + ']'); // [1, 2, 3, 2, 3, 6, 7, 8] 532 array = new ctor([1, 2, 3, 4, 5, 6, 7, 8]); 533 copied = array.copyWithin(3, 1); 534 print(copied); // [1, 2, 3, 2, 3, 4, 5, 6] 535}); 536 537print("================Test Map================"); 538[ 539 SendableFloat64Array, 540 SendableFloat32Array, 541 SendableInt32Array, 542 SendableInt16Array, 543 SendableInt8Array, 544 SendableUint32Array, 545 SendableUint16Array, 546 SendableUint8Array, 547 SendableUint8ClampedArray, 548].forEach((ctor: Function) => { 549 let array = new ctor([25, 36, 49]); 550 const mapped = array.map(Math.sqrt); 551 print((mapped instanceof ctor) + ', mapped: [' + mapped + ']'); // [5, 6, 7] 552}); 553 554print("================Test Keys/Values/Entries================"); 555[ 556 SendableFloat64Array, 557 SendableFloat32Array, 558 SendableInt32Array, 559 SendableInt16Array, 560 SendableInt8Array, 561 SendableUint32Array, 562 SendableUint16Array, 563 SendableUint8Array, 564 SendableUint8ClampedArray, 565].forEach((ctor: Function) => { 566 const array = new ctor([10, 20, 30]); 567 let keys = array.keys(); 568 let values = array.values(); 569 let entries = array.entries(); 570 for (let i = 0; i < array.length; i++) { 571 print('key: ' + keys.next().value + ', value: ' + values.next().value + ', entries: ' + entries.next().value); 572 } 573}); 574 575print("================Test ForEach================"); 576new SendableUint32Array([0, 1, 2, 3]).forEach((element: number, index: number, array: SendableUint32Array) => { 577 print(`array[${index}] = ${element}`); 578}); 579 580try { 581 new SendableUint32Array([0, 1, 2, 3]).forEach((element: number, index: number, array: SendableUint32Array) => { 582 print(array.slice(1, 3)); 583 }); 584} catch (e) { 585 print("ForEach: " + e + ", errCode: " + e.code); 586} 587 588try { 589 new SendableUint32Array([0, 1, 2, 3]).forEach((element: number, index: number, array: SendableUint32Array) => { 590 array.sort(); 591 }); 592} catch (e) { 593 print("ForEach: " + e + ", errCode: " + e.code); 594} 595 596print("================Test From================"); 597[SendableFloat64Array, SendableInt32Array, SendableInt16Array, SendableInt8Array].forEach((ctor: Function) => { 598 // From an iterable object (Set) 599 let array = ctor.from(new Set([1, 2, 3])); 600 print((array instanceof ctor) + ', array: [' + array + ']'); 601 // From a string 602 array = ctor.from('123'); 603 print(array); 604 // Use with arrow function and map 605 array = ctor.from([1, 2, 3], (x) => x + x); 606 print(array); 607 // Generate a sequence of numbers 608 Uint8Array.from({ length: 5 }, (v, k) => k); 609 print(array); 610}); 611 612print("================Test Set================"); 613let uint8Arr; 614let uint16Arr; 615let uint32Arr; 616// Sendable array 617uint8Arr = new SendableUint8Array(8); 618uint8Arr.set(new SendableArray(1, 2, 3, 4, 5), 3); 619print(uint8Arr); 620// Non-sendable array 621uint8Arr = new SendableUint8Array(8); 622uint8Arr.set([1, 2, 3], 3); 623print(uint8Arr); 624// Sendable same type 625uint8Arr = new SendableUint8Array(8); 626uint8Arr.set(new SendableUint8Array([1, 2, 3, 4, 5, 6]), 2); 627print(uint8Arr); 628// Non-sendable same type 629uint8Arr = new SendableUint8Array(8); 630uint8Arr.set(new Uint8Array([1, 2, 3, 4, 5, 6]), 2); 631print(uint8Arr); 632// Sendable diff type 633uint8Arr = new SendableUint8Array(8); 634uint32Arr = new SendableUint32Array([1024, 1, 2]) 635uint8Arr.set(uint32Arr, 3); 636print(uint8Arr); 637// Non-sendable diff type 638uint8Arr = new SendableUint8Array(8); 639uint32Arr = new Uint32Array([1024, 1, 2]) 640uint8Arr.set(uint32Arr, 3); 641print(uint8Arr); 642// Set self 643let uint8c = new Uint8ClampedArray(11); 644uint8c.set(uint8c); 645print(uint8c); 646uint8Arr = new Uint8Array(11); 647uint8Arr.fill(1); 648uint8Arr.set(uint8Arr); 649print(uint8Arr); 650uint16Arr = new Uint16Array(11); 651uint16Arr.fill(513); 652uint16Arr.set(uint16Arr); 653print(uint16Arr); 654uint32Arr = new Uint32Array(11); 655uint32Arr.fill(65536); 656uint32Arr.set(uint32Arr); 657print(uint32Arr); 658 659print("================Test Freeze================"); 660try { 661 let uint16 = new SendableUint16Array([1, 2, 3]); 662 Object.freeze(uint16); 663} catch (e) { 664 print("Object.freeze error: " + e); 665} 666try { 667 let uint16 = new SendableUint16Array(); 668 Object.freeze(uint16); 669 print("Object.freeze freeze empty array success"); 670} catch (e) { 671 print("Object.freeze error: " + e); 672} 673try { 674 Object.defineProperty(SendableBigInt64Array.prototype, 'constructor', { 675 get: function () { 676 calls++; 677 }, 678 }); 679} catch(e) { 680 print("Object.defineProperty error: " + e); 681} 682try { 683 let uint8 = new SendableUint8Array([1, 2, 3, 4]); 684 uint8.nonExistProp = "nonExistProp"; 685} catch(e) { 686 print("Add nonExistProp error: " + e); 687} 688try { 689 let uint8 = new SendableUint8Array([1, 2, 3, 4]); 690 uint8["nonExistProp"] = "nonExistProp"; 691} catch(e) { 692 print("Add nonExistProp error: " + e); 693} 694 695print("================Test Inheritance================"); 696class SubUint32Array extends SendableUint32Array { 697 constructor(params: any) { 698 'use sendable'; 699 super(params); 700 } 701} 702 703let subUint32Array = new SubUint32Array([1, 4, 3, 2, 5]); 704print("array: [" + subUint32Array + "]" + ", sorted: [" + subUint32Array.sort() + "]"); 705print("length: " + subUint32Array.length + ", byteLength: " + subUint32Array.byteLength); 706print("BYTES_PER_ELEMENT: " + subUint32Array.BYTES_PER_ELEMENT + ", " + SubUint32Array.BYTES_PER_ELEMENT); 707 708class SubSubUint32Array extends SubUint32Array { 709 constructor(params: any) { 710 "use sendable"; 711 super(params) 712 } 713} 714let subSubUint32Array = new SubSubUint32Array([1, 4, 3, 2, 5]); 715print("array: [" + subSubUint32Array + "]" + ", sorted: [" + subSubUint32Array.sort() + "]"); 716print("length: " + subSubUint32Array.length + ", byteLength: " + subSubUint32Array.byteLength); 717print("BYTES_PER_ELEMENT: " + subSubUint32Array.BYTES_PER_ELEMENT + ", " + SubSubUint32Array.BYTES_PER_ELEMENT); 718 719print("================Test IC================"); 720[ 721 SendableFloat64Array, 722 SendableFloat32Array, 723 SendableInt32Array, 724 SendableInt16Array, 725 SendableInt8Array, 726 SendableUint32Array, 727 SendableUint16Array, 728 SendableUint8Array, 729 SendableUint8ClampedArray, 730].forEach((ctor: Function) => { 731 if (testTypeArrayIC(ctor)) { 732 print(ctor.name + ' test IC success'); 733 } else { 734 print(ctor.name + ' test IC failed'); 735 } 736}); 737function testTypeArrayIC(ctor: Function) { 738 let result = [] 739 let obj = new ctor(100); 740 for (var i = 0; i < 100; i++) { 741 obj[i] = i; 742 } 743 for (var i = 0; i < 100; i++) { 744 result.push(obj[i] == i); 745 } 746 for (var i = 0; i < 100; i++) { 747 result.push(obj.at(i) == i); 748 } 749 for (let i = 0; i < result.length; i++) { 750 if (!result[i]) { 751 return false; 752 } 753 } 754 return true; 755} 756 757print("================Test ArrayBuffer================"); 758let sArrayBuffer : SendableArrayBuffer = new SendableArrayBuffer(16); 759print("SendableArrayBuffer length: " + sArrayBuffer.byteLength); 760let int32View: SendableInt32Array = new SendableInt32Array(sArrayBuffer); 761int32View[1] = 42; 762let sliced = new SendableInt32Array(sArrayBuffer.slice(4, 12)); 763print("SendableArrayBuffer sliced[0]: " + sliced[0]); 764 765print(SendableArrayBuffer[Symbol.species] == SendableArrayBuffer); 766print(SendableArrayBuffer.name == "SendableArrayBuffer"); 767print(sArrayBuffer[Symbol.toStringTag] == "SendableArrayBuffer"); 768 769let isview = SendableArrayBuffer.isView(new SendableInt32Array()); 770print('SendableArrayBuffer SendableInt32Array isView: ' + isview); 771isview = SendableArrayBuffer.isView(new Int8Array(10)); 772print("SendableArrayBuffer Int8Array isView: " + isview); 773 774class SubSendableArrayBuffer extends SendableArrayBuffer { 775 constructor(params: any) { 776 "use sendable"; 777 super(params); 778 } 779} 780let subSArrayBuffer = new SubSendableArrayBuffer(20); 781print("SubSendableArrayBuffer length: " + subSArrayBuffer.byteLength); 782let int32View1: SendableInt32Array = new SendableInt32Array(subSArrayBuffer); 783int32View1[2] = 5; 784let slicedSub = new SendableInt32Array(subSArrayBuffer.slice(8, 16)); 785print("SubSendableArrayBuffer slicedSub[0]: " + slicedSub[0]); 786let isviewSub = SubSendableArrayBuffer.isView(new SendableInt32Array()); 787print('SubSendableArrayBuffer SendableInt32Array isView: ' + isviewSub); 788isviewSub = SendableArrayBuffer.isView(new Int8Array(10)); 789print("SendableArrayBuffer Int8Array isView: " + isviewSub); 790 791function testReflectApply() { 792 try { 793 const v1 = new Uint8ClampedArray(Uint8ClampedArray, Uint8ClampedArray, Uint8ClampedArray); 794 const v5 = new SendableUint16Array(1727); 795 new SendableUint32Array(v5); 796 const v8 = [v1]; 797 Reflect.apply(v1.slice, v5, v8); 798 print("Reflect.apply slice success"); 799 } catch(e) { 800 print("Reflect.apply slice error: " + e); 801 } 802} 803 804testReflectApply() 805