1/* 2 * Copyright (c) 2023 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 16let NONE = 0; 17let HOLE = 1; 18let PACKEDINT = 2; 19let HOLEINT = 3; 20let PACKEDNUMBER = 6; 21let HOLENUMBER = 7; 22let PACKEDSTRING = 8; 23let HOLESTRING = 9; 24let PACKEDOBJECT = 16; 25let HOLEOBJECT = 17; 26let PACKEDTAGGED = 30; 27let GENERIC = 31; 28let DICTIONARY = 31; 29 30// ElementdKind == INT 31// Test on ldobjbyvalue 32function test1() { 33 let a = [1, 2, 34]; 34 if (a[0] == 1 && a[1] == 2 && a[2] == 34 && ArkTools.getElementsKind(a) == PACKEDINT) { 35 print("test1 - success"); 36 } else { 37 print("test1 - failed"); 38 } 39} 40 41// ElementdKind == INT 42// Test on ldobjbyvalue and stobjbyvalue 43function test2() { 44 let a = [1, 2, 34]; 45 a[1] = 10; 46 if (a[0] == 1 && a[1] == 10 && a[2] == 34 && ArkTools.getElementsKind(a) == PACKEDINT) { 47 print("test2 - success"); 48 } else { 49 print("test2 - failed"); 50 } 51} 52 53// ElementdKind == INT 54// Test on ldobjbyvalue 55// With Deopt 56function test3() { 57 let a = [1, 2, 34]; 58 print(a[10]); // deopt point 59 if (a[0] == 1 && a[1] == 2 && a[2] == 34 && ArkTools.getElementsKind(a) == PACKEDINT) { 60 print("test3 - success"); 61 } else { 62 print("test3 - failed"); 63 } 64} 65 66// ElementdKind == INT 67// Test on ldobjbyvalue and stobjbyvalue 68// With Deopt 69function test4() { 70 let a = [1, 2, 34]; 71 print(a[10]); // deopt point 72 a[1] = 10; 73 if (a[0] == 1 && a[1] == 10 && a[2] == 34 && ArkTools.getElementsKind(a) == PACKEDINT) { 74 print("test4 - success"); 75 } else { 76 print("test4 - failed"); 77 } 78} 79 80// ElementdKind == DOUBLE 81// Test on ldobjbyvalue 82function test5() { 83 let a = [1.5, 3.5, 6.7]; 84 if (a[0] == 1.5 && a[1] == 3.5 && a[2] == 6.7 && ArkTools.getElementsKind(a) == PACKEDNUMBER) { 85 print("test5 - success"); 86 } else { 87 print("test5 - failed"); 88 } 89} 90 91// ElementdKind == DOUBLE 92// Test on ldobjbyvalue and stobjbyvalue 93function test6() { 94 let a = [1.5, 3.5, 6.7]; 95 a[1] = 8.8; 96 if (a[0] == 1.5 && a[1] == 8.8 && a[2] == 6.7 && ArkTools.getElementsKind(a) == PACKEDNUMBER) { 97 print("test6 - success"); 98 } else { 99 print("test6 - failed"); 100 } 101} 102 103// ElementdKind == DOUBLE 104// Test on ldobjbyvalue 105// With Deopt 106function test7() { 107 let a = [1.5, 3.5, 6.7]; 108 print(a[10]); // deopt point 109 if (a[0] == 1.5 && a[1] == 3.5 && a[2] == 6.7 && ArkTools.getElementsKind(a) == PACKEDNUMBER) { 110 print("test7 - success"); 111 } else { 112 print("test7 - failed"); 113 } 114} 115 116// ElementdKind == INT 117// Test on ldobjbyvalue and stobjbyvalue 118// With Deopt 119function test8() { 120 let a = [1.5, 3.5, 6.7]; 121 print(a[10]); // deopt point 122 a[1] = 8.8; 123 if (a[0] == 1.5 && a[1] == 8.8 && a[2] == 6.7 && ArkTools.getElementsKind(a) == PACKEDNUMBER) { 124 print("test8 - success"); 125 } else { 126 print("test8 - failed"); 127 } 128} 129 130// ElementdKind == NUMBER 131// Test on ldobjbyvalue 132function test9() { 133 let a = [1.5, 3, 6.7]; 134 if (a[0] == 1.5 && a[1] == 3 && a[2] == 6.7 && ArkTools.getElementsKind(a) == PACKEDNUMBER) { 135 print("test9 - success"); 136 } else { 137 print("test9 - failed"); 138 } 139} 140 141// ElementdKind == Number 142// Test on ldobjbyvalue stobjbyvalue 143function test10() { 144 let a = [1.5, 3, 6.7]; 145 a[0] = 2; 146 if (a[0] == 2 && a[1] == 3 && a[2] == 6.7 && ArkTools.getElementsKind(a) == PACKEDNUMBER) { 147 print("test10 - success"); 148 } else { 149 print("test10 - failed"); 150 } 151} 152 153// ElementdKind == Number 154// Test on ldobjbyvalue 155// With deopt 156function test11() { 157 let a = [1.5, 3, 6.7]; 158 print(a[10]); 159 if (a[0] == 1.5 && a[1] == 3 && a[2] == 6.7 && ArkTools.getElementsKind(a) == PACKEDNUMBER) { 160 print("test11 - success"); 161 } else { 162 print("test11 - failed"); 163 } 164} 165 166// ElementdKind == Number 167// Test on ldobjbyvalue stobjbyvalue 168// With deopt 169function test12() { 170 let a = [1.5, 3, 6.7]; 171 print(a[10]); 172 a[0] = 2; 173 if (a[0] == 2 && a[1] == 3 && a[2] == 6.7 && ArkTools.getElementsKind(a) == PACKEDNUMBER) { 174 print("test12 - success"); 175 } else { 176 print("test12 - failed"); 177 } 178} 179 180// ElementdKind == String 181// Test on ldobjbyvalue 182function test13() { 183 let a = ["o", "p", "q"]; 184 if (a[0] == "o" && a[1] == "p" && a[2] == "q" && ArkTools.getElementsKind(a) == PACKEDSTRING) { 185 print("test13 - success"); 186 } else { 187 print("test13 - failed"); 188 } 189} 190 191// ElementdKind == String 192// Test on ldobjbyvalue stobjbyvalue 193function test14() { 194 let a = ["o", "p", "q"]; 195 a[0] = "x"; 196 if (a[0] == "x" && a[1] == "p" && a[2] == "q" && ArkTools.getElementsKind(a) == PACKEDSTRING) { 197 print("test14 - success"); 198 } else { 199 print("test14 - failed"); 200 } 201} 202 203// ElementdKind == Number 204// Test on ldobjbyvalue 205// With deopt 206function test15() { 207 let a = ["o", "p", "q"]; 208 print(a[10]); 209 if (a[0] == "o" && a[1] == "p" && a[2] == "q" && ArkTools.getElementsKind(a) == PACKEDSTRING) { 210 print("test15 - success"); 211 } else { 212 print("test15 - failed"); 213 } 214} 215 216// ElementdKind == Number 217// Test on ldobjbyvalue stobjbyvalue 218// With deopt 219function test16() { 220 let a = ["o", "p", "q"]; 221 print(a[10]); 222 a[0] = "x"; 223 if (a[0] == "x" && a[1] == "p" && a[2] == "q" && ArkTools.getElementsKind(a) == PACKEDSTRING) { 224 print("test16 - success"); 225 } else { 226 print("test16 - failed"); 227 } 228} 229 230// ElementdKind == HOLE INT 231// Test on ldobjbyvalue 232function test17() { 233 let a = [1, , 5]; 234 if (a[0] == 1 && a[1] == undefined && a[2] == 5 && ArkTools.getElementsKind(a) == HOLEINT) { 235 print("test17 - success"); 236 } else { 237 print("test17 - failed"); 238 } 239} 240 241// ElementdKind == HOLE DOUBLE 242// Test on ldobjbyvalue 243function test18() { 244 let a = [1.5, , 5.6]; 245 if (a[0] == 1.5 && a[1] == undefined && a[2] == 5.6 && ArkTools.getElementsKind(a) == HOLENUMBER) { 246 print("test18 - success"); 247 } else { 248 print("test18 - failed"); 249 } 250} 251 252// ElementdKind == HOLE INT 253// Test on ldobjbyvalue 254// With deopt 255function test19() { 256 let a = [1, , 5]; 257 print(a[10]); 258 if (a[0] == 1 && a[1] == undefined && a[2] == 5 && ArkTools.getElementsKind(a) == HOLEINT) { 259 print("test19 - success"); 260 } else { 261 print("test19 - failed"); 262 } 263} 264 265// ElementdKind == HOLE DOUBLE 266// Test on ldobjbyvalue 267// With deopt 268function test20() { 269 let a = [1.5, , 5.6]; 270 print(a[10]); 271 if (a[0] == 1.5 && a[1] == undefined && a[2] == 5.6 && ArkTools.getElementsKind(a) == HOLENUMBER) { 272 print("test20 - success"); 273 } else { 274 print("test20 - failed"); 275 } 276} 277 278// ElementdKind == HOLE INT 279// Test on ldobjbyvalue stobjbyvalue 280function test21() { 281 let a = [1, , 5]; 282 a[0] = 2; 283 if (a[0] == 2 && a[1] == undefined && a[2] == 5 && ArkTools.getElementsKind(a) == HOLEINT) { 284 print("test21 - success"); 285 } else { 286 print("test21 - failed"); 287 } 288} 289 290// ElementdKind == HOLE DOUBLE 291// Test on ldobjbyvalue stobjbyvalue 292function test22() { 293 let a = [1.5, , 5.6]; 294 a[0] = 2.2; 295 if (a[0] == 2.2 && a[1] == undefined && a[2] == 5.6 && ArkTools.getElementsKind(a) == HOLENUMBER) { 296 print("test22 - success"); 297 } else { 298 print("test22 - failed"); 299 } 300} 301 302// ElementdKind == HOLE INT 303// Test on ldobjbyvalue stobjbyvalue 304// With deopt 305function test23() { 306 let a = [1, , 5]; 307 print(a[10]); 308 a[0] = 2; 309 if (a[0] == 2 && a[1] == undefined && a[2] == 5 && ArkTools.getElementsKind(a) == HOLEINT) { 310 print("test23 - success"); 311 } else { 312 print("test23 - failed"); 313 } 314} 315 316// ElementdKind == HOLE DOUBLE 317// Test on ldobjbyvalue stobjbyvalue 318// With deopt 319function test24() { 320 let a = [1.5, , 5.6]; 321 print(a[10]); 322 a[0] = 2.2; 323 if (a[0] == 2.2 && a[1] == undefined && a[2] == 5.6 && ArkTools.getElementsKind(a) == HOLENUMBER) { 324 print("test24 - success"); 325 } else { 326 print("test24 - failed"); 327 } 328} 329 330// ElementdKind == HOLE INT 331// Test on store value at "hole" place 332function test25() { 333 let a = [1, , 5]; 334 if (a[1] != undefined) { 335 print("test25 - failed: a[1] should be undefined"); 336 return; 337 } 338 a[1] = 2; 339 if (a[1] != 2) { 340 print("test25 - failed: store value to hole spot failed"); 341 } 342 if (a[0] == 1 && a[1] == 2 && a[2] == 5 && ArkTools.getElementsKind(a) == HOLEINT) { 343 print("test25 - success"); 344 } else { 345 print("test25 - failed"); 346 } 347} 348 349// ElementdKind == HOLE DOUBLE 350// Test on ldobjbyvalue stobjbyvalue 351function test26() { 352 let a = [1.5, , 5.6]; 353 if (a[1] != undefined) { 354 print("test26 - failed: a[1] should be undefined"); 355 return; 356 } 357 a[1] = 2.2; 358 if (a[1] != 2.2) { 359 print("test26 - failed: store value to hole spot failed"); 360 } 361 if (a[0] == 1.5 && a[1] == 2.2 && a[2] == 5.6 && ArkTools.getElementsKind(a) == HOLENUMBER) { 362 print("test26 - success"); 363 } else { 364 print("test26 - failed"); 365 } 366} 367 368// ElementdKind == HOLE INT 369// Test on store value at "hole" place 370// With deopt 371function test27() { 372 let a = [1, , 5]; 373 if (a[1] != undefined) { 374 print("test27 - failed: a[1] should be undefined"); 375 return; 376 } 377 a[1] = 2; 378 print(a[10]); 379 if (a[1] != 2) { 380 print("test27 - failed: store value to hole spot failed"); 381 } 382 if (a[0] == 1 && a[1] == 2 && a[2] == 5 && ArkTools.getElementsKind(a) == HOLEINT) { 383 print("test27 - success"); 384 } else { 385 print("test27 - failed"); 386 } 387} 388 389// ElementdKind == HOLE DOUBLE 390// Test on ldobjbyvalue stobjbyvalue 391// With deopt 392function test28() { 393 let a = [1.5, , 5.6]; 394 if (a[1] != undefined) { 395 print("test28 - failed: a[1] should be undefined"); 396 return; 397 } 398 a[1] = 2.2; 399 print(a[10]); 400 if (a[1] != 2.2) { 401 print("test28 - failed: store value to hole spot failed"); 402 } 403 if (a[0] == 1.5 && a[1] == 2.2 && a[2] == 5.6 && ArkTools.getElementsKind(a) == HOLENUMBER) { 404 print("test28 - success"); 405 } else { 406 print("test28 - failed"); 407 } 408} 409 410// ElementdKind == HOLE NUMBER 411// Test on ldobjbyvalue 412function test29() { 413 let a = [1, , 5.1]; 414 if (a[0] == 1 && a[1] == undefined && a[2] == 5.1 && ArkTools.getElementsKind(a) == HOLENUMBER) { 415 print("test29 - success"); 416 } else { 417 print("test29 - failed"); 418 } 419} 420 421// ElementdKind == HOLE Tagged 422// Test on ldobjbyvalue 423function test30() { 424 let a = ["o", , "q"]; 425 if (a[0] == "o" && a[1] == undefined && a[2] == "q" && ArkTools.getElementsKind(a) == HOLESTRING) { 426 print("test30 - success"); 427 } else { 428 print("test30 - failed"); 429 } 430} 431 432// ElementdKind == HOLE NUMBER 433// Test on ldobjbyvalue 434// With deopt 435function test31() { 436 let a = [1, , 5.1]; 437 print(a[10]); 438 if (a[0] == 1 && a[1] == undefined && a[2] == 5.1 && ArkTools.getElementsKind(a) == HOLENUMBER) { 439 print("test31 - success"); 440 } else { 441 print("test31 - failed"); 442 } 443} 444 445// ElementdKind == HOLE Tagged 446// Test on ldobjbyvalue 447// With deopt 448function test32() { 449 let a = ["o", , "q"]; 450 print(a[10]); 451 if (a[0] == "o" && a[1] == undefined && a[2] == "q" && ArkTools.getElementsKind(a) == HOLESTRING) { 452 print("test32 - success"); 453 } else { 454 print("test32 - failed"); 455 } 456} 457 458// ElementdKind == HOLE NUMBER 459// Test on ldobjbyvalue stobjbyvalue 460function test33() { 461 let a = [1, , 5.1]; 462 a[0] = 2; 463 if (a[0] == 2 && a[1] == undefined && a[2] == 5.1 && ArkTools.getElementsKind(a) == HOLENUMBER) { 464 print("test33 - success"); 465 } else { 466 print("test33 - failed"); 467 } 468} 469 470// ElementdKind == HOLE Tagged 471// Test on ldobjbyvalue stobjbyvalue 472function test34() { 473 let a = ["o", , "q"]; 474 a[0] = "x"; 475 if (a[0] == "x" && a[1] == undefined && a[2] == "q" && ArkTools.getElementsKind(a) == HOLESTRING) { 476 print("test34 - success"); 477 } else { 478 print("test34 - failed"); 479 } 480} 481 482// ElementdKind == HOLE NUMBER 483// Test on ldobjbyvalue stobjbyvalue 484// With deopt 485function test35() { 486 let a = [1, , 5.1]; 487 print(a[10]); 488 a[0] = 2; 489 if (a[0] == 2 && a[1] == undefined && a[2] == 5.1 && ArkTools.getElementsKind(a) == HOLENUMBER) { 490 print("test35 - success"); 491 } else { 492 print("test35 - failed"); 493 } 494} 495 496// ElementdKind == HOLE Tagged 497// Test on ldobjbyvalue stobjbyvalue 498// With deopt 499function test36() { 500 let a = ["o", , "q"]; 501 print(a[10]); 502 a[0] = "x"; 503 if (a[0] == "x" && a[1] == undefined && a[2] == "q" && ArkTools.getElementsKind(a) == HOLESTRING) { 504 print("test36 - success"); 505 } else { 506 print("test36 - failed"); 507 } 508} 509 510// ElementdKind == HOLE NUMBER 511// Test on store value at "hole" place 512function test37() { 513 let a = [1, , 5.6]; 514 if (a[1] != undefined) { 515 print("test37 - failed: a[1] should be undefined"); 516 return; 517 } 518 a[1] = 2; 519 if (a[1] != 2) { 520 print("test37 - failed: store value to hole spot failed"); 521 } 522 if (a[0] == 1 && a[1] == 2 && a[2] == 5.6 && ArkTools.getElementsKind(a) == HOLENUMBER) { 523 print("test37 - success"); 524 } else { 525 print("test37 - failed"); 526 } 527} 528 529// ElementdKind == HOLE Tagged 530// Test on store value at "hole" place 531function test38() { 532 let a = ["o", , "q"]; 533 if (a[1] != undefined) { 534 print("test38 - failed: a[1] should be undefined"); 535 return; 536 } 537 a[1] = "p"; 538 if (a[1] != "p") { 539 print("test38 - failed: store value to hole spot failed"); 540 } 541 if (a[0] == "o" && a[1] == "p" && a[2] == "q" && ArkTools.getElementsKind(a) == HOLESTRING) { 542 print("test38 - success"); 543 } else { 544 print("test38 - failed"); 545 } 546} 547 548// ElementdKind == HOLE NUMBER 549// Test on store value at "hole" place 550// With deopt 551function test39() { 552 let a = [1, , 5.1]; 553 if (a[1] != undefined) { 554 print("test39 - failed: a[1] should be undefined"); 555 return; 556 } 557 a[1] = 2; 558 print(a[10]); 559 if (a[1] != 2) { 560 print("test39 - failed: store value to hole spot failed"); 561 } 562 if (a[0] == 1 && a[1] == 2 && a[2] == 5.1 && ArkTools.getElementsKind(a) == HOLENUMBER) { 563 print("test39 - success"); 564 } else { 565 print("test39 - failed"); 566 } 567} 568 569// ElementdKind == HOLE DOUBLE 570// Test on store value at "hole" place 571// With deopt 572function test40() { 573 let a = ["o", , "q"]; 574 if (a[1] != undefined) { 575 print("test40 - failed: a[1] should be undefined"); 576 return; 577 } 578 a[1] = "p"; 579 print(a[10]); 580 if (a[1] != "p") { 581 print("test40 - failed: store value to hole spot failed"); 582 } 583 if (a[0] == "o" && a[1] == "p" && a[2] == "q" && ArkTools.getElementsKind(a) == HOLESTRING) { 584 print("test40 - success"); 585 } else { 586 print("test40 - failed"); 587 } 588} 589 590// Transition Int -> Number 591// Load Stable Array Check deopt 592function test41() { 593 let a = [1, 2, 3]; 594 if (a[0] != 1 || a[1] != 2 || a[2] != 3) { 595 print("test41 - failed"); 596 } 597 a[1] = 2.5; 598 if (a[0] == 1 && a[1] == 2.5 && a[2] == 3 && ArkTools.getElementsKind(a) == PACKEDNUMBER) { 599 print("test41 - success"); 600 } else { 601 print("test41 - failed"); 602 } 603} 604 605// Transition Int -> Number 606function test42() { 607 let a = [1, 2, 3]; 608 a[1] = 2.5; 609 if (a[0] == 1 && a[1] == 2.5 && a[2] == 3 && ArkTools.getElementsKind(a) == PACKEDNUMBER) { 610 print("test42 - success"); 611 } else { 612 print("test42 - failed"); 613 } 614} 615 616// Transition Int -> Number 617// Store Stable Array Check deopt 618function test43() { 619 let a = [1, 2, 3]; 620 a[0] = 4; 621 a[1] = 2.5; 622 if (a[0] == 4 && a[1] == 2.5 && a[2] == 3 && ArkTools.getElementsKind(a) == PACKEDNUMBER) { 623 print("test43 - success"); 624 } else { 625 print("test43 - failed"); 626 } 627} 628 629// Transition Int -> TaggedObject 630// Load Stable Array Check deopt 631function test44() { 632 let a = [1, 2, 3]; 633 if (a[0] != 1 || a[1] != 2 || a[2] != 3) { 634 print("test44 - failed"); 635 } 636 a[1] = "1"; 637 if (a[0] == 1 && a[1] == "1" && a[2] == 3 && ArkTools.getElementsKind(a) == PACKEDTAGGED) { 638 print("test44 - success"); 639 } else { 640 print("test44 - failed"); 641 } 642} 643 644// Transition Number -> Number 645function test45() { 646 let a = [1, 2.2, 3]; 647 if (a[0] != 1 || a[1] != 2.2 || a[2] != 3) { 648 print("test45 - failed"); 649 } 650 a[1] = 2; 651 if (a[0] == 1 && a[1] == 2 && a[2] == 3 && ArkTools.getElementsKind(a) == PACKEDNUMBER) { 652 print("test45 - success"); 653 } else { 654 print("test45 - failed"); 655 } 656} 657 658// Transition Number -> TaggedObject 659function test46() { 660 let a = [1, 2.2, 3]; 661 if (a[0] != 1 || a[1] != 2.2 || a[2] != 3) { 662 print("test46 - failed"); 663 } 664 a[1] = "2"; 665 if (a[0] == 1 && a[1] == "2" && a[2] == 3 && ArkTools.getElementsKind(a) == PACKEDTAGGED) { 666 print("test46 - success"); 667 } else { 668 print("test46 - failed"); 669 } 670} 671 672// Transition Int -> TaggedObject 673function test47() { 674 let a = [1, 2, 3]; 675 if (a[0] != 1 || a[1] != 2 || a[2] != 3) { 676 print("test47 - failed"); 677 } 678 a[1] = "2"; 679 if (a[0] == 1 && a[1] == "2" && a[2] == 3 && ArkTools.getElementsKind(a) == PACKEDTAGGED) { 680 print("test47 - success"); 681 } else { 682 print("test47 - failed"); 683 } 684} 685 686// Transition TaggedObject -> TaggedObject 687// Load and Store has type TaggedObject 688// Because it uses receiver type in pgo 689function test48() { 690 let a = [1, "2"]; 691 if (a[0] != 1 || a[1] != "2") { 692 print("test48 - failed"); 693 } 694 a[1] = 2; 695 if (a[0] == 1 && a[1] == 2 && ArkTools.getElementsKind(a) == PACKEDTAGGED) { 696 print("test48 - success"); 697 } else { 698 print("test48 - failed"); 699 } 700} 701 702// Transition TaggedObject -> TaggedObject 703function test49() { 704 let a = [1, "2"]; 705 if (a[0] != 1 || a[1] != "2") { 706 print("test49 - failed"); 707 } 708 a[0] = "1"; 709 if (a[0] == "1" && a[1] == "2" && ArkTools.getElementsKind(a) == PACKEDTAGGED) { 710 print("test49 - success"); 711 } else { 712 print("test49 - failed"); 713 } 714} 715 716// Transition Hole_Int -> Hole_Tagged 717// Has deopt at load 718function test50() { 719 let a = [1, , 3] 720 if (a[0] != 1 || a[1] != undefined || a[2] != 3) { 721 print("test50 - failed"); 722 } 723 a[0] = "1"; 724 if (a[0] == "1" && a[1] == undefined && a[2] == 3 && ArkTools.getElementsKind(a) == GENERIC) { 725 print("test50 - success"); 726 } else { 727 print("test50 - failed"); 728 } 729} 730 731// Transition Hole_Int -> Hole_Tagged 732// No Deopt 733function test51() { 734 let a = [1, , 3] 735 a[0] = "1"; 736 if (a[0] == "1" && a[1] == undefined && a[2] == 3 && ArkTools.getElementsKind(a) == GENERIC) { 737 print("test51 - success"); 738 } else { 739 print("test51 - failed"); 740 } 741} 742 743// Transition Hole_Int -> Hole_Number 744// Has deopt at load 745function test52() { 746 let a = [1, , 3] 747 if (a[0] != 1 || a[1] != undefined || a[2] != 3) { 748 print("test52 - failed"); 749 } 750 a[0] = 2.2; 751 if (a[0] == 2.2 && a[1] == undefined && a[2] == 3 && ArkTools.getElementsKind(a) == HOLENUMBER) { 752 print("test52 - success"); 753 } else { 754 print("test52 - failed"); 755 } 756} 757 758// Transition Hole_Int -> Hole_Number 759// No Deopt 760function test53() { 761 let a = [1, , 3] 762 a[0] = 2.2; 763 if (a[0] == 2.2 && a[1] == undefined && a[2] == 3 && ArkTools.getElementsKind(a) == HOLENUMBER) { 764 print("test53 - success"); 765 } else { 766 print("test53 - failed"); 767 } 768} 769 770// Transition Hole_Double -> Hole_Tagged 771// Has deopt at load 772function test54() { 773 let a = [1.1, , 3.3] 774 if (a[0] != 1.1 || a[1] != undefined || a[2] != 3.3) { 775 print("test54 - failed"); 776 } 777 a[0] = "1"; 778 if (a[0] == "1" && a[1] == undefined && a[2] == 3.3 && ArkTools.getElementsKind(a) == GENERIC) { 779 print("test54 - success"); 780 } else { 781 print("test54 - failed"); 782 } 783} 784 785// Transition Hole_Double -> Hole_Tagged 786// No Deopt 787function test55() { 788 let a = [1.1, , 3.3] 789 a[0] = "1"; 790 if (a[0] == "1" && a[1] == undefined && a[2] == 3.3 && ArkTools.getElementsKind(a) == GENERIC) { 791 print("test55 - success"); 792 } else { 793 print("test55 - failed"); 794 } 795} 796 797// Transition Hole_Number -> Hole_Tagged 798// Has deopt at load 799function test56() { 800 let a = [1, , 3.3] 801 if (a[0] != 1 || a[1] != undefined || a[2] != 3.3) { 802 print("test56 - failed"); 803 } 804 a[0] = "1"; 805 if (a[0] == "1" && a[1] == undefined && a[2] == 3.3 && ArkTools.getElementsKind(a) == GENERIC) { 806 print("test56 - success"); 807 } else { 808 print("test56 - failed"); 809 } 810} 811 812// Transition Hole_Number -> Hole_Tagged 813// No Deopt 814function test57() { 815 let a = [1, , 3.3] 816 a[0] = "1"; 817 if (a[0] == "1" && a[1] == undefined && a[2] == 3.3 && ArkTools.getElementsKind(a) == GENERIC) { 818 print("test57 - success"); 819 } else { 820 print("test57 - failed"); 821 } 822} 823 824// Transition Hole_Int -> Hole_Number 825// Has deopt at load 826// Store at hole place 827function test58() { 828 let a = [1, , 3] 829 if (a[0] != 1 || a[1] != undefined || a[2] != 3) { 830 print("test58 - failed"); 831 } 832 a[1] = 2.2; 833 if (a[0] == 1 && a[1] == 2.2 && a[2] == 3 && ArkTools.getElementsKind(a) == HOLENUMBER) { 834 print("test58 - success"); 835 } else { 836 print("test58 - failed"); 837 } 838} 839 840// Transition Hole_Int -> Hole_Number 841// No Deopt 842// Store at hole place 843function test59() { 844 let a = [1, , 3] 845 a[1] = 2.2; 846 if (a[0] == 1 && a[1] == 2.2 && a[2] == 3 && ArkTools.getElementsKind(a) == HOLENUMBER) { 847 print("test59 - success"); 848 } else { 849 print("test59 - failed"); 850 } 851} 852 853// Transition Hole_Int -> Hole_Tagged 854// Has deopt at load 855// Store at hole place 856function test60() { 857 let a = [1, , 3] 858 if (a[0] != 1 || a[1] != undefined || a[2] != 3) { 859 print("test60 - failed"); 860 } 861 a[1] = "2"; 862 if (a[0] == 1 && a[1] == "2" && a[2] == 3 && ArkTools.getElementsKind(a) == GENERIC) { 863 print("test60 - success"); 864 } else { 865 print("test60 - failed"); 866 } 867} 868 869// Transition Hole_Int -> Hole_Tagged 870// No Deopt 871// Store at hole place 872function test61() { 873 let a = [1, , 3] 874 a[1] = "2"; 875 if (a[0] == 1 && a[1] == "2" && a[2] == 3 && ArkTools.getElementsKind(a) == GENERIC) { 876 print("test61 - success"); 877 } else { 878 print("test61 - failed"); 879 } 880} 881 882// Transition Hole_Double -> Hole_Number 883// Has deopt at load 884// Store at hole place 885function test62() { 886 let a = [1.1, , 3.3] 887 if (a[0] != 1.1 || a[1] != undefined || a[2] != 3.3) { 888 print("test62 - failed"); 889 } 890 a[1] = 2; 891 if (a[0] == 1.1 && a[1] == 2 && a[2] == 3.3 && ArkTools.getElementsKind(a) == HOLENUMBER) { 892 print("test62 - success"); 893 } else { 894 print("test62 - failed"); 895 } 896} 897 898// Transition Hole_Double -> Hole_Number 899// No Deopt 900// Store at hole place 901function test63() { 902 let a = [1.1, , 3.3] 903 a[1] = 2; 904 if (a[0] == 1.1 && a[1] == 2 && a[2] == 3.3 && ArkTools.getElementsKind(a) == HOLENUMBER) { 905 print("test63 - success"); 906 } else { 907 print("test63 - failed"); 908 } 909} 910 911// Transition Hole_Double -> Hole_Tagged 912// Has deopt at load 913// Store at hole place 914function test64() { 915 let a = [1.1, , 3.3] 916 if (a[0] != 1.1 || a[1] != undefined || a[2] != 3.3) { 917 print("test64 - failed"); 918 } 919 a[1] = "2"; 920 if (a[0] == 1.1 && a[1] == "2" && a[2] == 3.3 && ArkTools.getElementsKind(a) == GENERIC) { 921 print("test64 - success"); 922 } else { 923 print("test64 - failed"); 924 } 925} 926 927// Transition Hole_Double -> Hole_Tagged 928// No Deopt 929// Store at hole place 930function test65() { 931 let a = [1.1, , 3.3] 932 a[1] = "2"; 933 if (a[0] == 1.1 && a[1] == "2" && a[2] == 3.3 && ArkTools.getElementsKind(a) == GENERIC) { 934 print("test65 - success"); 935 } else { 936 print("test65 - failed"); 937 } 938} 939 940// Transition Hole_Number -> Hole_Tagged 941// Has deopt at load 942// Store at hole place 943function test66() { 944 let a = [1, , 3.3] 945 if (a[0] != 1 || a[1] != undefined || a[2] != 3.3) { 946 print("test66 - failed"); 947 } 948 a[1] = "2"; 949 if (a[0] == 1 && a[1] == "2" && a[2] == 3.3 && ArkTools.getElementsKind(a) == GENERIC) { 950 print("test66 - success"); 951 } else { 952 print("test66 - failed"); 953 } 954} 955 956// Transition Hole_Number -> Hole_Tagged 957// No Deopt 958// Store at hole place 959function test67() { 960 let a = [1, , 3.3] 961 a[1] = "2"; 962 if (a[0] == 1 && a[1] == "2" && a[2] == 3.3 && ArkTools.getElementsKind(a) == GENERIC) { 963 print("test67 - success"); 964 } else { 965 print("test67 - failed"); 966 } 967} 968 969// Transition Hole_Tagged -> Hole_Tagged 970// no deopt at load 971// Store at hole place 972function test68() { 973 let a = [1, , "3"]; 974 if (a[0] != 1 || a[1] != undefined || a[2] != "3") { 975 print("test68 - failed"); 976 } 977 a[0] = "1"; 978 a[1] = "2"; 979 if (a[0] == "1" && a[1] == "2" && a[2] == "3" && ArkTools.getElementsKind(a) == GENERIC) { 980 print("test68 - success"); 981 } else { 982 print("test68 - failed"); 983 } 984} 985 986// Transition Hole_Tagged -> Hole_Tagged 987// No Deopt 988// Store at hole place 989function test69() { 990 let a = [1, , "3"]; 991 a[0] = "1"; 992 a[1] = "2"; 993 if (a[0] == "1" && a[1] == "2" && a[2] == "3" && ArkTools.getElementsKind(a) == GENERIC) { 994 print("test69 - success"); 995 } else { 996 print("test69 - failed"); 997 } 998} 999 1000// Compatibility test 1001// Packed Int Array 1002function test70() { 1003 let a = [1, 2, 3]; 1004 let result = "Output: " + a; 1005 let expected = "Output: 1,2,3"; 1006 if (result == expected) { 1007 print("test70 - success"); 1008 } else { 1009 print("test70 - failed"); 1010 } 1011} 1012 1013// Compatibility test 1014// Packed Number Array 1015function test71() { 1016 let a = [1.1, 2.2, 3.3]; 1017 let result = "Output: " + a; 1018 let expected = "Output: 1.1,2.2,3.3"; 1019 if (result == expected) { 1020 print("test71 - success"); 1021 } else { 1022 print("test71 - failed"); 1023 } 1024} 1025 1026// Compatibility test 1027// Packed Int and Number Array 1028function test72() { 1029 let a = [1, 2.2, 3.3]; 1030 let result = "Output: " + a; 1031 let expected = "Output: 1,2.2,3.3"; 1032 if (result == expected) { 1033 print("test72 - success"); 1034 } else { 1035 print("test72 - failed"); 1036 } 1037} 1038 1039// Compatibility test 1040// Holely Int Array 1041function test73() { 1042 let a = [1, , 3]; 1043 let result = "Output: " + a; 1044 let expected = "Output: 1,,3"; 1045 if (result == expected) { 1046 print("test73 - success"); 1047 } else { 1048 print("test73 - failed"); 1049 } 1050} 1051 1052// Compatibility test 1053// Holely Number Array 1054function test74() { 1055 let a = [1.1, , 3.3]; 1056 let result = "Output: " + a; 1057 let expected = "Output: 1.1,,3.3"; 1058 if (result == expected) { 1059 print("test74 - success"); 1060 } else { 1061 print("test74 - failed"); 1062 } 1063} 1064 1065// Compatibility test 1066// Holely Int and Number Array 1067function test75() { 1068 let a = [1, , 3.3]; 1069 let result = "Output: " + a; 1070 let expected = "Output: 1,,3.3"; 1071 if (result == expected) { 1072 print("test75 - success"); 1073 } else { 1074 print("test75 - failed"); 1075 } 1076} 1077 1078// Compatibility test 1079// Add Transition Int -> Number 1080function test76() { 1081 let a = [1, 2, 3]; 1082 a[1] = 2.2; 1083 let result = "Output: " + a; 1084 let expected = "Output: 1,2.2,3"; 1085 if (result == expected) { 1086 print("test76 - success"); 1087 } else { 1088 print("test76 - failed"); 1089 } 1090} 1091 1092// Compatibility test 1093// Add Transition Int -> Tagged 1094function test77() { 1095 let a = [1, 2, 3]; 1096 a[1] = "2"; 1097 let result = "Output: " + a; 1098 let expected = "Output: 1,2,3"; 1099 if (result == expected) { 1100 print("test77 - success"); 1101 } else { 1102 print("test77 - failed"); 1103 } 1104} 1105 1106// Compatibility test 1107// Add Transition Number -> Tagged 1108function test78() { 1109 let a = [1.1, 2.2, 3.3]; 1110 a[1] = "2.2"; 1111 let result = "Output: " + a; 1112 let expected = "Output: 1.1,2.2,3.3"; 1113 if (result == expected) { 1114 print("test78 - success"); 1115 } else { 1116 print("test78 - failed"); 1117 } 1118} 1119 1120// Compatibility test 1121// Add Transition Int and Number -> Tagged 1122function test79() { 1123 let a = [1, 2.2, 3.3]; 1124 a[1] = "2"; 1125 let result = "Output: " + a; 1126 let expected = "Output: 1,2,3.3"; 1127 if (result == expected) { 1128 print("test79 - success"); 1129 } else { 1130 print("test79 - failed"); 1131 } 1132} 1133 1134// Compatibility test 1135// Add Holely Transition HoleNumber -> Tagged 1136function test80() { 1137 let a = [1, , 3]; 1138 a[1] = 2; 1139 let result = "Output: " + a; 1140 let expected = "Output: 1,2,3"; 1141 if (result == expected) { 1142 print("test80 - success"); 1143 } else { 1144 print("test80 - failed"); 1145 } 1146} 1147 1148// Compatibility test 1149// Add Holely Transition HoleIntNumber -> Tagged 1150function test81() { 1151 let a = [1, , 3.3]; 1152 a[1] = 2.2; 1153 let result = "Output: " + a; 1154 let expected = "Output: 1,2.2,3.3"; 1155 if (result == expected) { 1156 print("test81 - success"); 1157 } else { 1158 print("test81 - failed"); 1159 } 1160} 1161 1162// Compatibility test 1163// Packed Tagged Array 1164function test82() { 1165 let a = ["1", "2", "3"]; 1166 let result = "Output: " + a; 1167 let expected = "Output: 1,2,3"; 1168 if (result == expected) { 1169 print("test82 - success"); 1170 } else { 1171 print("test82 - failed"); 1172 } 1173} 1174 1175// Compatibility test 1176// Add Holely Transition HoleTagged -> Tagged 1177function test83() { 1178 let a = ["1", , "3"]; 1179 a[1] = "2"; 1180 let result = "Output: " + a; 1181 let expected = "Output: 1,2,3"; 1182 if (result == expected) { 1183 print("test83 - success"); 1184 } else { 1185 print("test83 - failed"); 1186 } 1187} 1188 1189// BuiltinArray Array.constructor 1190function test84() { 1191 let a = new Array(5); 1192 if (ArkTools.getElementsKind(a) == HOLE) { 1193 print("test84 - success"); 1194 } else { 1195 print("test84 - failed"); 1196 } 1197} 1198 1199function test85() { 1200 let a = new Array(1, 2, 3); 1201 if (ArkTools.getElementsKind(a) == HOLEINT) { 1202 print("test85 - success"); 1203 } else { 1204 print("test85 - failed"); 1205 } 1206} 1207 1208function test86() { 1209 let a = new Array(1, 1.5, 3); 1210 if (ArkTools.getElementsKind(a) == HOLENUMBER) { 1211 print("test86 - success"); 1212 } else { 1213 print("test86 - failed"); 1214 } 1215} 1216 1217function test87() { 1218 let a = new Array("1", 1.5, 3); 1219 if (ArkTools.getElementsKind(a) == GENERIC) { 1220 print("test87 - success"); 1221 } else { 1222 print("test87 - failed"); 1223 } 1224} 1225 1226function test88() { 1227 let a = new Array("1", "2", "3"); 1228 if (ArkTools.getElementsKind(a) == HOLESTRING) { 1229 print("test88 - success"); 1230 } else { 1231 print("test88 - failed"); 1232 } 1233} 1234 1235function test89() { 1236 let a = Array.of(1, 2, 3); 1237 if (a.length == 3 && ArkTools.getElementsKind(a) == HOLEINT) { 1238 print("test89 - success"); 1239 } else { 1240 print("test89 - failed"); 1241 } 1242} 1243 1244function test90() { 1245 let a = Array.of(3); 1246 if (a.length == 1 && ArkTools.getElementsKind(a) == HOLEINT) { 1247 print("test90 - success"); 1248 } else { 1249 print("test90 - failed"); 1250 } 1251} 1252 1253// Question? 1254// Array is constructed from callback 1255function test91() { 1256 let result = 0; 1257 function NotArray(len) { 1258 result = len; 1259 } 1260 1261 let a = Array.of.call(NotArray, 1, 2, 3); 1262 if (result == 3 && a.length == 3 && ArkTools.getElementsKind(a) == GENERIC) { 1263 print("test91 - success"); 1264 } else { 1265 print("test91 - failed"); 1266 } 1267} 1268 1269// Array.concat 1270function test92() { 1271 const num1 = [1, 2, 3]; 1272 const num2 = [4, 5, 6]; 1273 1274 const a = num1.concat(num2); 1275 if (a.length == 6 && ArkTools.getElementsKind(a) == HOLEINT) { 1276 print("test92 - success"); 1277 } else { 1278 print("test92 - failed"); 1279 } 1280} 1281 1282function test93() { 1283 const num1 = [1, 2, 3]; 1284 const num2 = [4.4, 5.5, 6.6]; 1285 1286 const a = num1.concat(num2); 1287 if (a.length == 6 && ArkTools.getElementsKind(a) == HOLENUMBER) { 1288 print("test93 - success"); 1289 } else { 1290 print("test93 - failed"); 1291 } 1292} 1293 1294function test94() { 1295 let a = [1, , 3].concat([4, 5]); // [1, empty, 3, 4, 5] 1296 if (a.length == 5 && ArkTools.getElementsKind(a) == HOLEINT) { 1297 print("test94 - success"); 1298 } else { 1299 print("test94 - failed"); 1300 } 1301} 1302 1303function test95() { 1304 let a = [1, , 3].concat([4, 5]); // [1, empty, 3, 4, 5] 1305 if (a.length == 5 && ArkTools.getElementsKind(a) == HOLEINT) { 1306 print("test95 - success"); 1307 } else { 1308 print("test95 - failed"); 1309 } 1310} 1311 1312function test96() { 1313 const obj1 = { 0: 1, 1: 2, 2: 3, length: 3 }; 1314 const obj2 = { 0: 1, 1: 2, 2: 3, length: 3, [Symbol.isConcatSpreadable]: true }; 1315 let a = [0].concat(obj1, obj2); 1316 if (a.length == 5 && ArkTools.getElementsKind(a) == PACKEDTAGGED) { 1317 print("test96 - success"); 1318 } else { 1319 print("test96 - failed"); 1320 } 1321} 1322 1323// Array.copyWithin 1324function test97() { 1325 let a = [1, 2, 3, 4, 5].copyWithin(0, 3); // [4, 5, 3, 4, 5] 1326 if (a.length == 5 && ArkTools.getElementsKind(a) == PACKEDINT) { 1327 print("test97 - success"); 1328 } else { 1329 print("test97 - failed"); 1330 } 1331} 1332 1333function test98() { 1334 let a = [1, , 3].copyWithin(2, 1, 2); // [1, empty, empty] 1335 if (a.length == 3 && ArkTools.getElementsKind(a) == DICTIONARY) { 1336 print("test98 - success"); 1337 } else { 1338 print("test98 - failed"); 1339 } 1340} 1341 1342// Array.entries 1343function test99() { 1344 const a = ["a", "b", "c"]; 1345 let i = 0; 1346 let flag = true; 1347 for (const [index, element] of a.entries()) { 1348 if (element != a[i]) { 1349 flag = false; 1350 } 1351 i++; 1352 } 1353 if (flag) { 1354 print("test99 - success"); 1355 } else { 1356 print("test99 - failed"); 1357 } 1358} 1359 1360// Array.every 1361function test100() { 1362 const isBelowThreshold = (currentValue) => currentValue < 40; 1363 const array1 = [1, 30, 39, 29, 10, 13]; 1364 // Expected output: true 1365 if (array1.every(isBelowThreshold)) { 1366 print("test100 - success"); 1367 } else { 1368 print("test100 - failed"); 1369 } 1370} 1371 1372// Array.fill 1373function test101() { 1374 const array1 = [1, 2, 3, 4]; 1375 // Fill with 0 from position 2 until position 4 1376 array1.fill(0, 2, 4); 1377 // Expected output: Array [1, 2, 0, 0] 1378 if (array1.length == 4 && ArkTools.getElementsKind(array1) == PACKEDINT){ 1379 print("test101 - success"); 1380 } else { 1381 print("test101 - failed"); 1382 } 1383} 1384 1385function test102() { 1386 const array1 = [1, 2, 3, 4]; 1387 // Fill with 5 from position 1 1388 array1.fill(5.5, 1); 1389 // Expected output: Array [1, 5, 5, 5] 1390 if (array1.length == 4 && ArkTools.getElementsKind(array1) == PACKEDNUMBER){ 1391 print("test102 - success"); 1392 } else { 1393 print("test102 - failed"); 1394 } 1395} 1396 1397function test103() { 1398 const array1 = [1, 2, 3, 4]; 1399 array1.fill("6"); 1400 // Expected output: Array [6, 6, 6, 6] 1401 if (array1.length == 4 && ArkTools.getElementsKind(array1) == PACKEDTAGGED){ 1402 print("test103 - success"); 1403 } else { 1404 print("test103 - failed"); 1405 } 1406} 1407 1408// Array.filter 1409function test104() { 1410 const words = ['spray', 'elite', 'exuberant', 'destruction', 'present']; 1411 const result = words.filter((word) => word.length > 6); 1412 // Expected output: Array ["exuberant", "destruction", "present"] 1413 if (result.length == 3 && ArkTools.getElementsKind(result) == HOLESTRING){ 1414 print("test104 - success"); 1415 } else { 1416 print("test104 - failed"); 1417 } 1418} 1419 1420function test105() { 1421 const arrayLike = { 1422 length: 3, 1423 0: "a", 1424 1: "b", 1425 2: "c", 1426 3: "a", // ignored by filter() since length is 3 1427 }; 1428 let result = Array.prototype.filter.call(arrayLike, (x) => x <= "b"); 1429 // [ 'a', 'b' ] 1430 if (result.length == 2 && ArkTools.getElementsKind(result) == PACKEDSTRING){ 1431 print("test105 - success"); 1432 } else { 1433 print("test105 - failed"); 1434 } 1435} 1436 1437function test106() { 1438 let result = [1, , undefined].filter((x) => x === undefined); // [undefined] 1439 if (result.length == 1 && ArkTools.getElementsKind(result) == GENERIC){ 1440 print("test106 - success"); 1441 } else { 1442 print("test106 - failed"); 1443 } 1444} 1445 1446// Array.find 1447function test107() { 1448 const array1 = [5, 12, 8, 130, 44]; 1449 const found = array1.find((element) => element > 10); 1450 // Expected output: 12 1451 if (found == 12) { 1452 print("test107 - success"); 1453 } else { 1454 print("test107 - failed"); 1455 } 1456} 1457 1458// Array.findIndex 1459function test108() { 1460 const array1 = [5, 12, 8, 130, 44]; 1461 const isLargeNumber = (element) => element > 13; 1462 let result = array1.findIndex(isLargeNumber); 1463 // Expected output: 3 1464 if (result == 3) { 1465 print("test108 - success"); 1466 } else { 1467 print("test108 - failed"); 1468 } 1469} 1470 1471// Array.findLast 1472function test109() { 1473 const array1 = [5, 12, 50, 130, 44]; 1474 const found = array1.findLast((element) => element > 45); 1475 // Expected output: 130 1476 if (found == 130) { 1477 print("test109 - success"); 1478 } else { 1479 print("test109 - failed"); 1480 } 1481} 1482 1483// Array.findLastIndex 1484function test110() { 1485 const array1 = [5, 12, 50, 130, 44]; 1486 const isLargeNumber = (element) => element > 45; 1487 let result = array1.findLastIndex(isLargeNumber); 1488 // Expected output: 3 1489 // Index of element with value: 130 1490 if (result == 3) { 1491 print("test110 - success"); 1492 } else { 1493 print("test110 - failed"); 1494 } 1495} 1496 1497// Array.flat 1498function test111() { 1499 const arr1 = [0, 1, 2, [3, 4]]; 1500 let result = arr1.flat(); 1501 // expected output: Array [0, 1, 2, 3, 4] 1502 1503 if (result.length == 5 && ArkTools.getElementsKind(result) == PACKEDINT) { 1504 print("test111 - success"); 1505 } else { 1506 print("test111 - failed"); 1507 } 1508} 1509 1510function test112() { 1511 const arr2 = [0, 1, [2, [3, [4, 5]]]]; 1512 let result = arr2.flat(); 1513 // expected output: Array [0, 1, 2, Array [3, Array [4, 5]]] 1514 1515 if (result.length == 4 && ArkTools.getElementsKind(result) == PACKEDTAGGED) { 1516 print("test112 - success"); 1517 } else { 1518 print("test112 - failed"); 1519 } 1520} 1521 1522function test113() { 1523 const arr2 = [0, 1, [2, [3, [4, 5]]]]; 1524 let result = arr2.flat(2); 1525 // expected output: Array [0, 1, 2, 3, Array [4, 5]] 1526 1527 if (result.length == 5 && ArkTools.getElementsKind(result) == PACKEDTAGGED) { 1528 print("test113 - success"); 1529 } else { 1530 print("test113 - failed"); 1531 } 1532} 1533 1534function test114() { 1535 const arr2 = [0, 1, [2, [3, [4, 5]]]]; 1536 let result = arr2.flat(Infinity); 1537 // expected output: Array [0, 1, 2, 3, 4, 5] 1538 1539 if (result.length == 6 && ArkTools.getElementsKind(result) == PACKEDINT) { 1540 print("test114 - success"); 1541 } else { 1542 print("test114 - failed"); 1543 } 1544} 1545 1546// Array.flatMap 1547function test115() { 1548 const arr1 = [1, 2, 1]; 1549 const result = arr1.flatMap((num) => (num === 2 ? [2, 2] : 1)); 1550 // Expected output: Array [1, 2, 2, 1] 1551 if (result.length == 4 && ArkTools.getElementsKind(result) == PACKEDINT) { 1552 print("test115 - success"); 1553 } else { 1554 print("test115 - failed"); 1555 } 1556} 1557 1558function test116() { 1559 const arr1 = [1, 2, 1]; 1560 const result = arr1.flatMap((num) => (num === 2 ? 2.2 : 1)); 1561 // Expected output: Array [1, 2, 2, 1] 1562 if (result.length == 3 && ArkTools.getElementsKind(result) == PACKEDNUMBER) { 1563 print("test116 - success"); 1564 } else { 1565 print("test116 - failed"); 1566 } 1567} 1568 1569// Array.forEach 1570function test117() { 1571 const array1 = [1, 2, 3]; 1572 let result = 0; 1573 array1.forEach((element) => { result += element; }); 1574 if (result == 6) { 1575 print("test117 - success"); 1576 } else { 1577 print("test117 - failed"); 1578 } 1579} 1580 1581// Array.includes 1582function test118() { 1583 const array1 = [1, 2, 3]; 1584 let result = true; 1585 // Expected output: true 1586 if (!array1.includes(2) && ArkTools.getElementsKind(array1) == PACKEDINT) { 1587 result = false; 1588 } 1589 1590 const pets = ['cat', 'dog', 'bat']; 1591 // Expected output: true 1592 if (!pets.includes('cat')) { 1593 result = false; 1594 } 1595 // Expected output: false 1596 if (pets.includes('at')) { 1597 result = false; 1598 } 1599 1600 if (result) { 1601 print("test118 - success"); 1602 } else { 1603 print("test118 - failed"); 1604 } 1605} 1606 1607// Array.Indexof 1608function test119() { 1609 const beasts = ['ant', 'bison', 'camel', 'duck', 'bison']; 1610 let result = true; 1611 let index1 = beasts.indexOf('bison'); 1612 // Expected output: 1 1613 if (index1 != 1) { 1614 result = false; 1615 } 1616 1617 // Start from index 2 1618 let index2 = beasts.indexOf('bison', 2); 1619 // Expected output: 4 1620 if (index2 != 4) { 1621 result = false; 1622 } 1623 1624 let index3 = beasts.indexOf('giraffe'); 1625 // Expected output: -1 1626 if (index3 != -1) { 1627 result = false; 1628 } 1629 if (result) { 1630 print("test119 - success"); 1631 } else { 1632 print("test119 - failed"); 1633 } 1634} 1635 1636// Array.LastIndexof 1637function test120() { 1638 const animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo']; 1639 let result = true; 1640 let index1 = animals.lastIndexOf('Dodo'); 1641 // Expected output: 3 1642 if (index1 != 3) { 1643 result = false; 1644 } 1645 1646 let index2 = animals.lastIndexOf('Tiger'); 1647 // Expected output: 1 1648 if (index2 != 1) { 1649 result = false; 1650 } 1651 if (result) { 1652 print("test120 - success"); 1653 } else { 1654 print("test120 - failed"); 1655 } 1656} 1657 1658// Array.join 1659function test121() { 1660 const elements = ['Fire', 'Air', 'Water']; 1661 let result = true; 1662 let joined1 = elements.join(); 1663 // Expected output: "Fire,Air,Water" 1664 if (joined1 != "Fire,Air,Water") { 1665 result = false; 1666 } 1667 1668 let joined2 = elements.join(''); 1669 // Expected output: "FireAirWater" 1670 if (joined2 != "FireAirWater") { 1671 result = false; 1672 } 1673 1674 let joined3 = elements.join('-'); 1675 // Expected output: "Fire-Air-Water" 1676 if (joined3 != "Fire-Air-Water") { 1677 result = false; 1678 } 1679 if (result) { 1680 print("test121 - success"); 1681 } else { 1682 print("test121 - failed"); 1683 } 1684} 1685 1686// Array.map 1687function test122() { 1688 const array1 = [1, 4, 9, 16]; 1689 1690 // Pass a function to map 1691 const map1 = array1.map((x) => x * 2); 1692 // Expected output: Array [2, 8, 18, 32] 1693 1694 if (map1.length == 4 && ArkTools.getElementsKind(map1) == HOLEINT) { 1695 print("test122 - success"); 1696 } else { 1697 print("test122 - failed"); 1698 } 1699} 1700 1701function test123() { 1702 const array1 = [1, 4, 9, 16]; 1703 1704 // Pass a function to map 1705 const map1 = array1.map((x) => x * 1.1); 1706 // Expected output: Array [2, 8, 18, 32] 1707 1708 if (map1.length == 4 && ArkTools.getElementsKind(map1) == HOLENUMBER) { 1709 print("test123 - success"); 1710 } else { 1711 print("test123 - failed"); 1712 } 1713} 1714 1715// Array.pop 1716function test124() { 1717 const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato']; 1718 1719 let popResult = plants.pop(); 1720 // Expected output: "tomato" 1721 if (popResult == "tomato" && ArkTools.getElementsKind(plants) == PACKEDSTRING) { 1722 print("test124 - success"); 1723 } else { 1724 print("test124 - failed"); 1725 } 1726} 1727 1728function test125() { 1729 const a = [1, 2, 3, 4, 5.5]; 1730 let popResult = a.pop(); 1731 // Expected output: "tomato" 1732 if (popResult == 5.5 && ArkTools.getElementsKind(a) == PACKEDNUMBER) { 1733 print("test125 - success"); 1734 } else { 1735 print("test125 - failed"); 1736 } 1737} 1738 1739// Array.toSort 1740function test126() { 1741 let a = [6, 5, 4, 3, 2, 1]; 1742 let b = a.toSorted(); 1743 if (b.length == 6 && ArkTools.getElementsKind(b) == HOLEINT) { 1744 print("test126 - success"); 1745 } else { 1746 print("test126 - failed, expected: " + HOLEINT + " , but get: " + ArkTools.getElementsKind(b)); 1747 } 1748} 1749 1750// Array.unshift 1751function test127() { 1752 let a = [0, 1, 2]; 1753 a.unshift(-1); 1754 if (a.length == 4 && ArkTools.getElementsKind(a) == PACKEDINT) { 1755 print("test127 - success"); 1756 } else { 1757 print("test127 - failed, expected: " + PACKEDINT + " , but get: " + ArkTools.getElementsKind(a)); 1758 } 1759} 1760 1761function test128() { 1762 let a = [0, 1, 2]; 1763 a.unshift(-2, -1); 1764 if (a.length == 5 && ArkTools.getElementsKind(a) == PACKEDINT) { 1765 print("test128 - success"); 1766 } else { 1767 print("test128 - failed, expected: " + PACKEDINT + " , but get: " + ArkTools.getElementsKind(a)); 1768 } 1769} 1770 1771function test129() { 1772 // Array.prototype.toSpliced(start, deleteCount, item1, item2, ..., itemN) 1773 let a = [0, 1, 2, 3, 4, 5, 6]; 1774 let b = a.toSpliced(2, 2, 3, 3); 1775 if (b.length == 7 && ArkTools.getElementsKind(b) == PACKEDINT) { 1776 print("test129 - success"); 1777 } else { 1778 print("test129 - failed, expected: " + PACKEDINT + " , but get: " + ArkTools.getElementsKind(b)); 1779 } 1780} 1781 1782function test130() { 1783 // Array.prototype.toSpliced(start, deleteCount, item1, item2, ..., itemN) 1784 let a = [0, 1, 2, , 4, 5, 6]; 1785 let b = a.toSpliced(2, 1, 3); 1786 if (b.length == 7 && ArkTools.getElementsKind(b) == PACKEDTAGGED) { 1787 print("test130 - success"); 1788 } else { 1789 print("test130 - failed, expected: " + PACKEDTAGGED + " , but get: " + ArkTools.getElementsKind(b)); 1790 } 1791} 1792 1793function test131() { 1794 // Array.prototype.toSpliced(start, deleteCount, item1, item2, ..., itemN) 1795 let a = [0, 1, 2, 3, 4, 5, 6]; 1796 let b = a.toSpliced(2, 1, 3.1); 1797 if (b.length == 7 && ArkTools.getElementsKind(b) == PACKEDNUMBER) { 1798 print("test131 - success"); 1799 } else { 1800 print("test131 - failed, expected: " + PACKEDNUMBER + " , but get: " + ArkTools.getElementsKind(b)); 1801 } 1802} 1803 1804function test132() { 1805 // Array.prototype.toReversed() 1806 let a = [0, 1, 2, 4, 5, 6]; 1807 let b = a.toReversed(); 1808 if (b.length == 6 && ArkTools.getElementsKind(b) == PACKEDINT) { 1809 print("test132 - success"); 1810 } else { 1811 print("test132 - failed, expected: " + PACKEDINT + " , but get: " + ArkTools.getElementsKind(b)); 1812 } 1813} 1814 1815function test133() { 1816 // Array.prototype.toReversed() 1817 let a = [0, 1, 2, , 4, 5, 6]; 1818 let b = a.toReversed(); 1819 if (b.length == 7 && ArkTools.getElementsKind(b) == PACKEDTAGGED) { 1820 print("test133 - success"); 1821 } else { 1822 print("test133 - failed, expected: " + PACKEDTAGGED + " , but get: " + ArkTools.getElementsKind(b)); 1823 } 1824} 1825 1826function test134() { 1827 // Array.prototype.at(index) 1828 let a = [0, 1, 2, ,4, 5, 6]; 1829 if (a.length == 7 && a.at(2) == 2 && a.at(3) == undefined) { 1830 print("test134 - success"); 1831 } else { 1832 print("test134 - failed, a.At(2): " + a[2] + " , but get: a.At(3): " + a[3]); 1833 } 1834} 1835 1836function test135() { 1837 // Array.prototype.with(index, value) 1838 let a = [0, 1, 2, ,4, 5, 6]; 1839 let b = a.with(3, 6); 1840 if (b.length == 7 && b[3] == 6 && ArkTools.getElementsKind(b) == PACKEDINT) { 1841 print("test135 - success"); 1842 } else { 1843 print("test135 - failed, expected: " + PACKEDINT + " , but get: " + ArkTools.getElementsKind(b)); 1844 } 1845} 1846 1847function test136() { 1848 let a = [1, 2, 3, 4, 5]; 1849 a.push(5.5); 1850 if (a.length == 6 && a[5] == 5.5 && ArkTools.getElementsKind(a) == PACKEDNUMBER) { 1851 print("test136 - success"); 1852 } else { 1853 print("test136 - failed"); 1854 } 1855} 1856 1857function test137() { 1858 let a = [1, 2, 3, 4, 5]; 1859 a.push(undefined); 1860 if (a.length == 6 && a[5] == undefined && ArkTools.getElementsKind(a) == PACKEDTAGGED) { 1861 print("test137 - success"); 1862 } else { 1863 print("test137 - failed"); 1864 } 1865} 1866 1867function test138() { 1868 let a = [1, 2, , 4, 5]; 1869 1870 const initialValue = 0; 1871 const sumWithInitial = a.reduce( 1872 (accumulator, currentValue) => accumulator + currentValue, 1873 initialValue, 1874 ); 1875 1876 if (sumWithInitial == 12) { 1877 print("test138 - success"); 1878 } else { 1879 print("test138 - failed"); 1880 } 1881} 1882 1883function test139() { 1884 let a = [1, 2, , 4, 5]; 1885 1886 const initialValue = 0; 1887 const sumWithInitial = a.reduceRight( 1888 (accumulator, currentValue) => accumulator + currentValue, 1889 initialValue, 1890 ); 1891 1892 if (sumWithInitial == 12) { 1893 print("test139 - success"); 1894 } else { 1895 print("test139 - failed"); 1896 } 1897} 1898 1899function test140() { 1900 let a = [1, 2, 3, 4, 5]; 1901 a.reverse(); 1902 if (a.length == 5 && ArkTools.getElementsKind(a) == PACKEDINT) { 1903 print("test140 - success"); 1904 } else { 1905 print("test140 - failed"); 1906 } 1907} 1908 1909function test141() { 1910 let a = [1, 2, , 4, 5]; 1911 a.reverse(); 1912 if (a.length == 5 && ArkTools.getElementsKind(a) == HOLEINT) { 1913 print("test141 - success"); 1914 } else { 1915 print("test141 - failed"); 1916 } 1917} 1918 1919function test142() { 1920 // Array.prototype.shift() 1921 let a = [0.1, 1, 2, 3, 4, 5, 6]; 1922 let result = a.shift(); 1923 if (a.length == 6 && result == 0.1 && ArkTools.getElementsKind(a) == PACKEDNUMBER) { 1924 print("test142 - success"); 1925 } else { 1926 print("test142 - failed"); 1927 } 1928} 1929 1930function test143() { 1931 // Array.prototype.shift() 1932 let a = [0, 1, 2, , 4, 5, 6]; 1933 let result = a.shift(); 1934 if (a.length == 6 && result == 0 && ArkTools.getElementsKind(a) == HOLEINT) { 1935 print("test143 - success"); 1936 } else { 1937 print("test143 - failed"); 1938 } 1939} 1940 1941function test144() { 1942 // Array.prototype.slice() 1943 let a = [0, 1, 2, , 4, 5, 6]; 1944 let b = a.slice(); 1945 if (b.length == 7 && ArkTools.getElementsKind(b) == HOLEINT) { 1946 print("test144 - success"); 1947 } else { 1948 print("test144 - failed"); 1949 } 1950} 1951 1952function test145() { 1953 let a = [1, , 4, 21, 100000]; 1954 a.sort(); 1955 if (a[0] == 1 && a[1] == 100000 && a.length == 5 && ArkTools.getElementsKind(a) == HOLEINT) { 1956 print("test145 - success"); 1957 } else { 1958 print("test145 - failed"); 1959 } 1960} 1961 1962function test146() { 1963 // Array.prototype.splice() 1964 let a = [0, 1, 2, 3, 4, 5, 6]; 1965 a.splice(2, 2, 3, 3); 1966 if (a.length == 7 && ArkTools.getElementsKind(a) == PACKEDINT) { 1967 print("test146 - success"); 1968 } else { 1969 print("test146 - failed"); 1970 } 1971} 1972 1973function test147() { 1974 // Array.prototype.splice() 1975 let a = [0, 1, 2, , 4, 5, 6]; 1976 a.splice(2, 1, 3); 1977 if (a.length == 7 && ArkTools.getElementsKind(a) == HOLEINT) { 1978 print("test147 - success"); 1979 } else { 1980 print("test147 - failed"); 1981 } 1982} 1983 1984//fast Array.reverse 1985function test148() { 1986 let a = []; 1987 let b = a.reverse(); 1988 let c = [8, 6, 4, 2]; 1989 let d = [2, 4, 6, 8].reverse(); 1990 let e = [1,2,,4]; 1991 let f = e.reverse(); 1992 if (a == b && c[1] == d[1] && f[0] == 4 && 1993 f[1] == undefined) { 1994 print("test148 - success"); 1995 } else { 1996 print("test148 - failed"); 1997 } 1998} 1999 2000//fast Array.reverse 2001function test149() { 2002 let a = [0.4, 0.6, 0.8]; 2003 let b = a.reverse(); 2004 let c = ["str2", "str3", "str4"]; 2005 let d = c.reverse(); 2006 let proto = { 0: "foo", 19: "bar" }; 2007 let obj = { length: 20, 5: "foobar", __proto__: proto }; 2008 Array.prototype.reverse.call(obj); 2009 2010 if (b[0] == 0.8 && c[0] == "str4" && obj[0] == "bar" && 2011 obj[19] == "foo") { 2012 print("test149 - success"); 2013 } else { 2014 print("test149 - failed"); 2015 } 2016} 2017 2018// Int 2019test1(); 2020test2(); 2021test3(); 2022test4(); 2023 2024// Double 2025test5(); 2026test6(); 2027test7(); 2028test8(); 2029 2030// Number 2031test9(); 2032test10(); 2033test11(); 2034test12(); 2035 2036// String 2037test13(); 2038test14(); 2039test15(); 2040test16(); 2041 2042// Hole related tests 2043// Double and Int 2044test17(); 2045test18(); 2046test19(); 2047test20(); 2048test21(); 2049test22(); 2050test23(); 2051test24(); 2052test25(); 2053test26(); 2054test27(); 2055test28(); 2056// Number and Tagged 2057test29(); 2058test30(); 2059test31(); 2060test32(); 2061test33(); 2062test34(); 2063test35(); 2064test36(); 2065test37(); 2066test38(); 2067test39(); 2068test40(); 2069 2070// Transition - Packed 2071test41(); 2072test42(); 2073test43(); 2074test44(); 2075test45(); 2076test46(); 2077test47(); 2078test48(); 2079test49(); 2080 2081// Transition - Holey 2082test50(); 2083test51(); 2084test52(); 2085test53(); 2086test54(); 2087test55(); 2088test56(); 2089test57(); 2090test58(); 2091test59(); 2092test60(); 2093test61(); 2094test62(); 2095test63(); 2096test64(); 2097test65(); 2098test66(); 2099test67(); 2100test68(); 2101test69(); 2102 2103// Compatibility test 2104test70(); 2105test71(); 2106test72(); 2107test73(); 2108test74(); 2109test75(); 2110test76(); 2111test77(); 2112test78(); 2113test79(); 2114test80(); 2115test81(); 2116test82(); 2117test83(); 2118 2119// Builtins Test 2120// Array.Constructor 2121test84(); 2122test85(); 2123test86(); 2124test87(); 2125test88(); 2126 2127// Array.of 2128test89(); 2129test90(); 2130test91(); 2131 2132// Array.concat 2133test92(); 2134test93(); 2135test94(); 2136test95(); 2137test96(); 2138 2139// Array.copywithin 2140test97(); 2141test98(); 2142 2143// Array.entries 2144test99(); 2145 2146// Array.every 2147test100(); 2148 2149// Array.fill 2150test101(); 2151test102(); 2152test103(); 2153 2154// Array.filter 2155test104(); 2156test105(); 2157test106(); 2158 2159// Array.find 2160// Array.findIndex 2161// Array.findLast 2162// Array.findLastIndex 2163test107(); 2164test108(); 2165test109(); 2166test110(); 2167 2168// Array.flat 2169test111(); 2170test112(); 2171test113(); 2172test114(); 2173 2174// Array.flatMap 2175test115(); 2176test116(); 2177 2178// Array.ForEach 2179test117(); 2180 2181// Array.Includes 2182test118(); 2183 2184// Array.indexof 2185test119(); 2186 2187// Array.LastIndexof 2188test120(); 2189 2190// Array.join 2191test121(); 2192 2193// Array.map 2194test122(); 2195test123(); 2196 2197// Array.pop 2198test124(); 2199test125(); 2200 2201// Array.toSort 2202test126(); 2203 2204// Array.unshift 2205test127(); 2206test128(); 2207 2208// Array.toSplice 2209test129(); 2210test130(); 2211test131(); 2212 2213// Array.reverse 2214test132(); 2215test133(); 2216 2217// Array.at 2218test134(); 2219 2220// Array.with 2221test135(); 2222 2223// Array.push 2224test136(); 2225test137(); 2226 2227// Array.reduce 2228// Array.reduceRight 2229test138(); 2230test139(); 2231 2232// Array.reverse 2233test140(); 2234test141(); 2235 2236// Array.shift 2237test142(); 2238test143(); 2239 2240// Array.slice 2241test144(); // gc bug 2242 2243// Array.sort 2244test145(); 2245 2246// Array.splice 2247test146(); 2248test147(); 2249 2250test148(); 2251test149(); 2252 2253// Test COW ElementsKind 2254class Index { 2255 currentArrays = [ 2256 [0, 0, 0, 0], 2257 [0, 0, 0, 0], 2258 [0, 0, 0, 0], 2259 [0, 0, 0, 0] 2260 ] 2261 2262 changeCurretArrays() { 2263 let newArrays = [ 2264 [0, 0, 0, 0], 2265 [0, 0, 0, 0], 2266 [0, 0, 0, 0], 2267 [0, 0, 0, 0] 2268 ] 2269 2270 for (let j = 0; j < 4; j++) { 2271 for (let i = 0; i < 4; i++) { 2272 newArrays[j][i] = this.currentArrays[j][i] + 1; 2273 } 2274 } 2275 return newArrays; 2276 } 2277 2278 computeScore(array) { 2279 let total = 0; 2280 for (let j = 0; j < 4; j++) { 2281 for (let i = 0; i < 4; i++) { 2282 total += array[j][i]; 2283 } 2284 } 2285 return total; 2286 } 2287 2288 run() { 2289 let newArray = this.changeCurretArrays(); 2290 print(this.computeScore(newArray)); 2291 print(this.computeScore(this.currentArrays)); 2292 this.currentArrays = newArray; 2293 } 2294} 2295 2296let index = new Index; 2297for (let i = 0; i < 3; i++) { 2298 index.run(); 2299} 2300 2301function testLength() { 2302 let a = [0]; 2303 print(ArkTools.getElementsKind(a)); 2304 a.length = 3; 2305 print(ArkTools.getElementsKind(a)); 2306} 2307 2308testLength(); 2309 2310