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 let array1 = new ctor(["one", "two", "three"]); 356 array1.reverse(); 357 print(array1); 358}); 359 360print("================Test Some================"); 361[ 362 SendableFloat64Array, 363 SendableFloat32Array, 364 SendableInt32Array, 365 SendableInt16Array, 366 SendableInt8Array, 367 SendableUint32Array, 368 SendableUint16Array, 369 SendableUint8Array, 370 SendableUint8ClampedArray, 371].forEach((ctor: Function) => { 372 const negatives = new ctor([-10, 20, -30, 40, -50]); 373 const positives = new ctor([10, 20, 30, 40, 50]); 374 print( 375 negatives.some((element: number) => { 376 return element < 0; 377 }), 378 ); 379 print( 380 positives.some((element: number) => { 381 return element < 0; 382 }), 383 ); 384}); 385 386print("================Test Every================"); 387[ 388 SendableFloat64Array, 389 SendableFloat32Array, 390 SendableInt32Array, 391 SendableInt16Array, 392 SendableInt8Array, 393 SendableUint32Array, 394 SendableUint16Array, 395 SendableUint8Array, 396 SendableUint8ClampedArray, 397].forEach((ctor: Function) => { 398 const negatives = new ctor([-10, -20, -30, -40, -50]); 399 print( 400 negatives.every((element: number) => { 401 return element < 0; 402 }), 403 ); 404}); 405 406print("================Test Slice================"); 407[ 408 SendableFloat64Array, 409 SendableFloat32Array, 410 SendableInt32Array, 411 SendableInt16Array, 412 SendableInt8Array, 413 SendableUint32Array, 414 SendableUint16Array, 415 SendableUint8Array, 416 SendableUint8ClampedArray, 417].forEach((ctor: Function) => { 418 let array = new ctor([10, 20, 30, 40, 50]); 419 let sliced = array.slice(); 420 print(sliced); // [10, 20, 30, 40, 50] 421 sliced = array.slice(1); 422 print(sliced); // [20, 30, 40, 50] 423 sliced = array.slice(0, 5); 424 print(sliced); // [10, 20, 30, 40, 50] 425 sliced = array.slice(2, 10); 426 print(sliced); // [30, 40, 50] 427 sliced = array.slice(-1, 1); 428 print(sliced.length); // 0 429 sliced = array.slice(2, -10); 430 print(sliced.length); // 0 431}); 432 433print("================Test IndexOf================"); 434[ 435 SendableFloat64Array, 436 SendableFloat32Array, 437 SendableInt32Array, 438 SendableInt16Array, 439 SendableInt8Array, 440 SendableUint32Array, 441 SendableUint16Array, 442 SendableUint8Array, 443 SendableUint8ClampedArray, 444].forEach((ctor: Function) => { 445 const array = new ctor([10, 20, 30, 40, 50]); 446 print(array.indexOf(50)); // 4 447 print(array.indexOf(20, 3)); // -1 448 print(array.indexOf(51)); // -1 449}); 450 451print("================Test LastIndexOf================"); 452[ 453 SendableFloat64Array, 454 SendableFloat32Array, 455 SendableInt32Array, 456 SendableInt16Array, 457 SendableInt8Array, 458 SendableUint32Array, 459 SendableUint16Array, 460 SendableUint8Array, 461 SendableUint8ClampedArray, 462].forEach((ctor: Function) => { 463 const array = new ctor([10, 20, 30, 40, 50]); 464 print(array.lastIndexOf(50)); // 4 465 print(array.lastIndexOf(30, 1)); // -1 466 print(array.lastIndexOf(51)); // -1 467}); 468 469 const array1 = new SendableFloat64Array([1.1, 20, 3.3, +51.2, -16.5]); 470 print(array1.lastIndexOf(51.2)); // 3 471 print(array1.lastIndexOf(3.3, 3)); // 2 472 print(array1.lastIndexOf(51.2, 1)); // -1 473 474 const array2 = new SendableFloat32Array([1.1, 20, 3.3, +51.2, -16.5]); 475 print(array2.lastIndexOf(51.2)); // 3 476 print(array2.lastIndexOf(3.3, 3)); // 2 477 print(array2.lastIndexOf(51.2, 3)); // -1 478 479 480print("================Test ReduceRight================"); 481[ 482 SendableFloat64Array, 483 SendableFloat32Array, 484 SendableInt32Array, 485 SendableInt16Array, 486 SendableInt8Array, 487 SendableUint32Array, 488 SendableUint16Array, 489 SendableUint8Array, 490 SendableUint8ClampedArray, 491].forEach((ctor: Function) => { 492 const array = new ctor([1, 2, 3, 4]); 493 print(array.reduceRight((acc: number, currValue: number) => acc + currValue)); // 10 494 print(array.reduceRight((acc: number, currValue: number) => acc + currValue, 10)); // 20 495 print(array.reduceRight<string>((acc: number, currValue: number) => "" + acc + " " + currValue, "10")); // 10, 4, 3, 2, 1 496 497 const array1 = new ctor([1, 2, 3, 4]); 498 print(array1.reduceRight((acc: number, currValue: number) => acc + currValue, undefined)); 499 500 const array2 = new ctor(); 501 print(array2.reduceRight((acc: number, currValue: number) => acc + currValue, 1)); 502 print(array2.reduceRight((acc: number, currValue: number) => acc + currValue, undefined)); 503 504 try { 505 print(array2.reduceRight((acc: number, currValue: number) => acc + currValue)); 506 } catch (err) { 507 print("reduceRight failed. err: " + err + ", code: " + err.code); 508 } 509 510 try { 511 print(array2.reduceRight(1, 1)); 512 } catch (err) { 513 print("reduceRight failed. err: " + err + ", code: " + err.code); 514 } 515}); 516 517print("================Test ToString================"); 518[ 519 SendableFloat64Array, 520 SendableFloat32Array, 521 SendableInt32Array, 522 SendableInt16Array, 523 SendableInt8Array, 524 SendableUint32Array, 525 SendableUint16Array, 526 SendableUint8Array, 527 SendableUint8ClampedArray, 528].forEach((ctor: Function) => { 529 const array = new ctor([10, 20, 30, 40, 50]); 530 print(array.toString()); 531}); 532 533print("================Test ToLocaleString================"); 534[ 535 SendableFloat64Array, 536 SendableFloat32Array, 537 SendableInt32Array, 538 SendableInt16Array, 539 SendableInt8Array, 540 SendableUint32Array, 541 SendableUint16Array, 542 SendableUint8Array, 543 SendableUint8ClampedArray, 544].forEach((ctor: Function) => { 545 const array = new ctor([10, 20, 30, 40, 50]); 546 print(array.toLocaleString()); 547}); 548[ 549 SendableInt32Array, 550 SendableInt16Array, 551 SendableInt8Array, 552 SendableUint32Array, 553 SendableUint16Array, 554 SendableUint8Array, 555 SendableUint8ClampedArray, 556].forEach((ctor: Function) => { 557 const array4 = new SendableInt32Array([1000, 2000, 3000, 4000, 5000]); 558 print(array4.toLocaleString('de-DE')); 559 print(array4.toLocaleString('fr-FR')); 560}); 561[ 562 SendableFloat64Array, 563 SendableFloat32Array, 564].forEach((ctor: Function) => { 565 const array5 = new SendableFloat32Array([123456.789, 2000.00]); 566 print(array5.toLocaleString('en-US', {style: 'currency', currency: 'USD'})); 567 print(array5.toLocaleString('de-DE', {style: 'currency', currency: 'USD'})); 568}); 569 570print("================Test Of================"); 571[ 572 SendableFloat64Array, 573 SendableFloat32Array, 574 SendableInt32Array, 575 SendableInt16Array, 576 SendableInt8Array, 577 SendableUint32Array, 578 SendableUint16Array, 579 SendableUint8Array, 580 SendableUint8ClampedArray, 581].forEach((ctor: Function) => { 582 let array = ctor.of(1, 2, 3); 583 print((array instanceof ctor) + ', array: [' + array + ']'); 584 585 let array1 = ctor.of("1", "2", "3", "4"); 586 print((array1 instanceof ctor) + ', array: [' + array1 + ']'); 587}); 588 589print("================Test FindIndex================"); 590[ 591 SendableFloat64Array, 592 SendableFloat32Array, 593 SendableInt32Array, 594 SendableInt16Array, 595 SendableInt8Array, 596 SendableUint32Array, 597 SendableUint16Array, 598 SendableUint8Array, 599 SendableUint8ClampedArray, 600].forEach((ctor: Function) => { 601 const array = new ctor([10, -20, 30, -40, 50]); 602 print( 603 array.findIndex((element: number) => { 604 return element < 0; 605 }), 606 ); 607}); 608 609print("================Test Find================"); 610[ 611 SendableFloat64Array, 612 SendableFloat32Array, 613 SendableInt32Array, 614 SendableInt16Array, 615 SendableInt8Array, 616 SendableUint32Array, 617 SendableUint16Array, 618 SendableUint8Array, 619 SendableUint8ClampedArray, 620].forEach((ctor: Function) => { 621 const array = new ctor([10, -20, 30, -40, 50]); 622 print( 623 array.find((element: number) => { 624 return element < 0; 625 }), 626 ); 627}); 628 629print("================Test Fill================"); 630[SendableFloat64Array, SendableInt32Array, SendableInt16Array, SendableInt8Array, SendableUint8ClampedArray].forEach( 631 (ctor: Function) => { 632 print(new ctor([1, 2, 3]).fill(4)); // [4, 4, 4] 633 print(new ctor([1, 2, 3]).fill(4, 1)); // [1, 4, 4] 634 print(new ctor([1, 2, 3]).fill(4, 1, 2)); // [1, 4, 3] 635 print(new ctor([1, 2, 3]).fill(4, 1, 1)); // [1, 2, 3] 636 print(new ctor([1, 2, 3]).fill(4, -3, -2)); // [4, 2, 3] 637 }, 638); 639 640print("================Test Filter================"); 641[ 642 SendableFloat64Array, 643 SendableFloat32Array, 644 SendableInt32Array, 645 SendableInt16Array, 646 SendableInt8Array, 647 SendableUint32Array, 648 SendableUint16Array, 649 SendableUint8Array, 650 SendableUint8ClampedArray, 651].forEach((ctor: Function) => { 652 const array = new ctor([-10, 20, -30, 40, -50]); 653 let filterd = array.filter((element: number) => { 654 return element < 0; 655 }); 656 print((filterd instanceof ctor) + ', filterd: [' + filterd + ']'); 657}); 658 659print("================Test CopyWith================"); 660[ 661 SendableFloat64Array, 662 SendableFloat32Array, 663 SendableInt32Array, 664 SendableInt16Array, 665 SendableInt8Array, 666 SendableUint32Array, 667 SendableUint16Array, 668 SendableUint8Array, 669 SendableUint8ClampedArray, 670].forEach((ctor: Function) => { 671 let array = new ctor([1, 2, 3, 4, 5, 6, 7, 8]); 672 let copied = array.copyWithin(3, 1, 3); 673 print((copied instanceof ctor) + ', copied: [' + copied + ']'); // [1, 2, 3, 2, 3, 6, 7, 8] 674 array = new ctor([1, 2, 3, 4, 5, 6, 7, 8]); 675 copied = array.copyWithin(3, 1); 676 print(copied); // [1, 2, 3, 2, 3, 4, 5, 6] 677}); 678 679print("================Test Map================"); 680[ 681 SendableFloat64Array, 682 SendableFloat32Array, 683 SendableInt32Array, 684 SendableInt16Array, 685 SendableInt8Array, 686 SendableUint32Array, 687 SendableUint16Array, 688 SendableUint8Array, 689 SendableUint8ClampedArray, 690].forEach((ctor: Function) => { 691 let array = new ctor([25, 36, 49]); 692 const mapped = array.map(Math.sqrt); 693 print((mapped instanceof ctor) + ', mapped: [' + mapped + ']'); // [5, 6, 7] 694}); 695 696print("================Test Keys/Values/Entries================"); 697[ 698 SendableFloat64Array, 699 SendableFloat32Array, 700 SendableInt32Array, 701 SendableInt16Array, 702 SendableInt8Array, 703 SendableUint32Array, 704 SendableUint16Array, 705 SendableUint8Array, 706 SendableUint8ClampedArray, 707].forEach((ctor: Function) => { 708 const array = new ctor([10, 20, 30]); 709 let keys = array.keys(); 710 let values = array.values(); 711 let entries = array.entries(); 712 for (let i = 0; i < array.length; i++) { 713 print('key: ' + keys.next().value + ', value: ' + values.next().value + ', entries: ' + entries.next().value); 714 } 715}); 716 717print("================Test ForEach================"); 718new SendableUint32Array([0, 1, 2, 3]).forEach((element: number, index: number, array: SendableUint32Array) => { 719 print(`array[${index}] = ${element}`); 720}); 721 722try { 723 new SendableUint32Array([0, 1, 2, 3]).forEach((element: number, index: number, array: SendableUint32Array) => { 724 print(array.slice(1, 3)); 725 }); 726} catch (e) { 727 print("ForEach: " + e + ", errCode: " + e.code); 728} 729 730try { 731 new SendableUint32Array([0, 1, 2, 3]).forEach((element: number, index: number, array: SendableUint32Array) => { 732 array.sort(); 733 }); 734} catch (e) { 735 print("ForEach: " + e + ", errCode: " + e.code); 736} 737 738print("================Test From================"); 739[SendableFloat64Array, SendableInt32Array, SendableInt16Array, SendableInt8Array].forEach((ctor: Function) => { 740 // From an iterable object (Set) 741 let array = ctor.from(new Set([1, 2, 3])); 742 print((array instanceof ctor) + ', array: [' + array + ']'); 743 // From a string 744 array = ctor.from('123'); 745 print(array); 746 // Use with arrow function and map 747 array = ctor.from([1, 2, 3], (x) => x + x); 748 print(array); 749 // Generate a sequence of numbers 750 Uint8Array.from({ length: 5 }, (v, k) => k); 751 print(array); 752}); 753 754print("================Test Set================"); 755let uint8Arr; 756let uint16Arr; 757let uint32Arr; 758// Sendable array 759uint8Arr = new SendableUint8Array(8); 760uint8Arr.set(new SendableArray(1, 2, 3, 4, 5), 3); 761print(uint8Arr); 762// Non-sendable array 763uint8Arr = new SendableUint8Array(8); 764uint8Arr.set([1, 2, 3], 3); 765print(uint8Arr); 766// Sendable same type 767uint8Arr = new SendableUint8Array(8); 768uint8Arr.set(new SendableUint8Array([1, 2, 3, 4, 5, 6]), 2); 769print(uint8Arr); 770// Non-sendable same type 771uint8Arr = new SendableUint8Array(8); 772uint8Arr.set(new Uint8Array([1, 2, 3, 4, 5, 6]), 2); 773print(uint8Arr); 774// Sendable diff type 775uint8Arr = new SendableUint8Array(8); 776uint32Arr = new SendableUint32Array([1024, 1, 2]) 777uint8Arr.set(uint32Arr, 3); 778print(uint8Arr); 779// Non-sendable diff type 780uint8Arr = new SendableUint8Array(8); 781uint32Arr = new Uint32Array([1024, 1, 2]) 782uint8Arr.set(uint32Arr, 3); 783print(uint8Arr); 784// Set self 785let uint8c = new Uint8ClampedArray(11); 786uint8c.set(uint8c); 787print(uint8c); 788uint8Arr = new Uint8Array(11); 789uint8Arr.fill(1); 790uint8Arr.set(uint8Arr); 791print(uint8Arr); 792uint16Arr = new Uint16Array(11); 793uint16Arr.fill(513); 794uint16Arr.set(uint16Arr); 795print(uint16Arr); 796uint32Arr = new Uint32Array(11); 797uint32Arr.fill(65536); 798uint32Arr.set(uint32Arr); 799print(uint32Arr); 800 801print("================Test Freeze================"); 802try { 803 let uint16 = new SendableUint16Array([1, 2, 3]); 804 Object.freeze(uint16); 805} catch (e) { 806 print("Object.freeze error: " + e); 807} 808try { 809 let uint16 = new SendableUint16Array(); 810 Object.freeze(uint16); 811 print("Object.freeze freeze empty array success"); 812} catch (e) { 813 print("Object.freeze error: " + e); 814} 815try { 816 Object.defineProperty(SendableBigInt64Array.prototype, 'constructor', { 817 get: function () { 818 calls++; 819 }, 820 }); 821} catch(e) { 822 print("Object.defineProperty error: " + e); 823} 824try { 825 let uint8 = new SendableUint8Array([1, 2, 3, 4]); 826 uint8.nonExistProp = "nonExistProp"; 827} catch(e) { 828 print("Add nonExistProp error: " + e); 829} 830try { 831 let uint8 = new SendableUint8Array([1, 2, 3, 4]); 832 uint8["nonExistProp"] = "nonExistProp"; 833} catch(e) { 834 print("Add nonExistProp error: " + e); 835} 836 837print("================Test Inheritance================"); 838class SubUint32Array extends SendableUint32Array { 839 constructor(params: any) { 840 'use sendable'; 841 super(params); 842 } 843} 844 845let subUint32Array = new SubUint32Array([1, 4, 3, 2, 5]); 846print("array: [" + subUint32Array + "]" + ", sorted: [" + subUint32Array.sort() + "]"); 847print("length: " + subUint32Array.length + ", byteLength: " + subUint32Array.byteLength); 848print("BYTES_PER_ELEMENT: " + subUint32Array.BYTES_PER_ELEMENT + ", " + SubUint32Array.BYTES_PER_ELEMENT); 849 850class SubSubUint32Array extends SubUint32Array { 851 constructor(params: any) { 852 "use sendable"; 853 super(params) 854 } 855} 856let subSubUint32Array = new SubSubUint32Array([1, 4, 3, 2, 5]); 857print("array: [" + subSubUint32Array + "]" + ", sorted: [" + subSubUint32Array.sort() + "]"); 858print("length: " + subSubUint32Array.length + ", byteLength: " + subSubUint32Array.byteLength); 859print("BYTES_PER_ELEMENT: " + subSubUint32Array.BYTES_PER_ELEMENT + ", " + SubSubUint32Array.BYTES_PER_ELEMENT); 860 861print("================Test IC================"); 862[ 863 SendableFloat64Array, 864 SendableFloat32Array, 865 SendableInt32Array, 866 SendableInt16Array, 867 SendableInt8Array, 868 SendableUint32Array, 869 SendableUint16Array, 870 SendableUint8Array, 871 SendableUint8ClampedArray, 872].forEach((ctor: Function) => { 873 if (testTypeArrayIC(ctor)) { 874 print(ctor.name + ' test IC success'); 875 } else { 876 print(ctor.name + ' test IC failed'); 877 } 878}); 879function testTypeArrayIC(ctor: Function) { 880 let result = [] 881 let obj = new ctor(100); 882 for (var i = 0; i < 100; i++) { 883 obj[i] = i; 884 } 885 for (var i = 0; i < 100; i++) { 886 result.push(obj[i] == i); 887 } 888 for (var i = 0; i < 100; i++) { 889 result.push(obj.at(i) == i); 890 } 891 for (let i = 0; i < result.length; i++) { 892 if (!result[i]) { 893 return false; 894 } 895 } 896 return true; 897} 898 899print("================Test ArrayBuffer================"); 900let sArrayBuffer : SendableArrayBuffer = new SendableArrayBuffer(16); 901print("SendableArrayBuffer length: " + sArrayBuffer.byteLength); 902let int32View: SendableInt32Array = new SendableInt32Array(sArrayBuffer); 903int32View[1] = 42; 904let sliced = new SendableInt32Array(sArrayBuffer.slice(4, 12)); 905print("SendableArrayBuffer sliced[0]: " + sliced[0]); 906 907print(SendableArrayBuffer[Symbol.species] == SendableArrayBuffer); 908print(SendableArrayBuffer.name == "SendableArrayBuffer"); 909print(sArrayBuffer[Symbol.toStringTag] == "SendableArrayBuffer"); 910 911let isview = SendableArrayBuffer.isView(new SendableInt32Array()); 912print('SendableArrayBuffer SendableInt32Array isView: ' + isview); 913isview = SendableArrayBuffer.isView(new Int8Array(10)); 914print("SendableArrayBuffer Int8Array isView: " + isview); 915 916class SubSendableArrayBuffer extends SendableArrayBuffer { 917 constructor(params: any) { 918 "use sendable"; 919 super(params); 920 } 921} 922let subSArrayBuffer = new SubSendableArrayBuffer(20); 923print("SubSendableArrayBuffer length: " + subSArrayBuffer.byteLength); 924let int32View1: SendableInt32Array = new SendableInt32Array(subSArrayBuffer); 925int32View1[2] = 5; 926let slicedSub = new SendableInt32Array(subSArrayBuffer.slice(8, 16)); 927print("SubSendableArrayBuffer slicedSub[0]: " + slicedSub[0]); 928let isviewSub = SubSendableArrayBuffer.isView(new SendableInt32Array()); 929print('SubSendableArrayBuffer SendableInt32Array isView: ' + isviewSub); 930isviewSub = SendableArrayBuffer.isView(new Int8Array(10)); 931print("SendableArrayBuffer Int8Array isView: " + isviewSub); 932 933function testReflectApply() { 934 try { 935 const v1 = new Uint8ClampedArray(Uint8ClampedArray, Uint8ClampedArray, Uint8ClampedArray); 936 const v5 = new SendableUint16Array(1727); 937 new SendableUint32Array(v5); 938 const v8 = [v1]; 939 Reflect.apply(v1.slice, v5, v8); 940 print("Reflect.apply slice success"); 941 } catch(e) { 942 print("Reflect.apply slice error: " + e); 943 } 944} 945 946testReflectApply() 947