1/* 2 * Copyright (c) 2022 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:container 18 * @tc.desc:test container 19 * @tc.type: FUNC 20 * @tc.require: 21 */ 22var PlainArray = undefined; 23if (globalThis["ArkPrivate"] != undefined) { 24 PlainArray = ArkPrivate.Load(ArkPrivate.PlainArray); 25 26 let map = new Map(); 27 let plainArray = new PlainArray(); 28 let proxy = new Proxy(plainArray, {}); 29 let testArray = ["0", "1", "2", "3", "4", "5"] 30 let res = true 31 let ret = proxy.add(0, "0") 32 proxy.add(1, "1") 33 proxy.add(2, "2") 34 proxy.add(3, "3") 35 proxy.add(4, "4") 36 proxy.add(5, "5") 37 38 for(let i = 0; i < testArray.length; i++) { 39 if (proxy[i] !== testArray[i]) { 40 res = false 41 } 42 } 43 map.set("test plainarray add:", res) 44 map.set("test plainarray 'add' ret:", ret === undefined) 45 map.set("test plainarray length:", proxy.length === 6) 46 map.set("test plainarray has:", proxy.has(2)) 47 map.set("test plainarray getIndexOfValue:", proxy.getIndexOfValue("1") === 1) 48 map.set("test plainarray getIndexOfKey:", proxy.getIndexOfKey(5) === 5) 49 map.set("test plainarray getKeyAt:", proxy.getKeyAt(1) === 1) 50 map.set("test plainarray getValueAt:", proxy.getValueAt(2) === "2") 51 52 let newPlainArray = proxy.clone() 53 res = true 54 for(let i = 0; i < testArray.length; i++) { 55 if (newPlainArray[i] !== testArray[i]) { 56 res = false 57 } 58 } 59 map.set("test plainarray clone:", res) 60 61 const removeRes = proxy.remove(3) 62 testArray.splice(3, 1) 63 map.set("test plainarray removeRes:", removeRes) 64 65 proxy.removeAt(2) 66 testArray.splice(2, 1) 67 res = true 68 for(let i = 0; i < testArray.length; i++) { 69 if (proxy.get(i) !== testArray[i]) { 70 res = false 71 } 72 } 73 74 newPlainArray = proxy.clone() 75 res = proxy.removeRangeFrom(1, 2) 76 testArray.splice(1, 2) 77 if (res > 0) { 78 res = newPlainArray.removeRangeFrom(0, 100) 79 if (res > 0) { 80 res = newPlainArray.isEmpty() 81 } 82 } 83 map.set("test plainarray removeRangeFrom:", res) 84 85 res = true 86 proxy.forEach((i, d) => { 87 }) 88 89 map.set("test plainarray forEach:", res) 90 91 res = true 92 let testArray3 = [0, 5] 93 let j = 0 94 for (const data of proxy) { 95 } 96 map.set("test plainarray for of:", res) 97 98 res = true 99 let itr = proxy[Symbol.iterator](); 100 let tmp = undefined; 101 let testArray1 = [] 102 do { 103 tmp = itr.next().value; 104 testArray1.push(tmp); 105 } while (tmp != undefined); 106 map.set("test plainarray Symbol.iterator:", res) 107 108 let arr2 = new PlainArray(); 109 let proxy1 = new Proxy(arr2, {}); 110 proxy1.add(0, "0") 111 proxy1.add(1, "1") 112 proxy1.add(2, "2") 113 proxy1.add(3, "3") 114 proxy1.add(4, "4") 115 proxy1.add(5, "5") 116 proxy1.setValueAt(2, "123") 117 map.set("test plainarray setValueAt and get:", proxy1.get(2) === "123") 118 ret = proxy1.clear() 119 map.set("test plainarray clear:", proxy1.length === 0) 120 map.set("test plainarray 'clear' ret:", ret === undefined) 121 map.set("test plainarray isEmpty:", proxy1.isEmpty()) 122 proxy1.add(0, "0") 123 proxy1.add(1, "1") 124 proxy1.add(2, "2") 125 proxy1.add(3, "3") 126 proxy1.add(4, "4") 127 proxy1.add(5, "5") 128 map.set("test plainarray toString:", proxy1.toString() == "0:0,1:1,2:2,3:3,4:4,5:5"); 129 let empty_pa = new PlainArray(); 130 try { 131 empty_pa.getValueAt(0); 132 } catch(err) { 133 res = (err =="BusinessError: Container is empty") 134 map.set("test getValueAt exception when arraylist is empty:", res) 135 } 136 try { 137 empty_pa.removeRangeFrom(0, 1); 138 } catch(err) { 139 res = (err =="BusinessError: Container is empty") 140 map.set("test removeRangeFrom exception when arraylist is empty:", res) 141 } 142 try { 143 empty_pa.setValueAt(0); 144 } catch(err) { 145 res = (err =="BusinessError: Container is empty") 146 map.set("test setValueAt exception when arraylist is empty:", res) 147 } 148 149 try { 150 let myPlainArray = new PlainArray(); 151 myPlainArray.add(1); 152 myPlainArray[2147483648]; 153 } catch(err) { 154 let overFlowTest = (err == "BusinessError: The type of \"index\" must be small integer."); 155 map.set("test PlainArray[i] overFlowTest:", overFlowTest); 156 } 157 158 class Tmp { 159 age = 0; 160 constructor(age) { 161 this.age = age; 162 } 163 } 164 165 { 166 let plainArray = new PlainArray(); 167 plainArray.add(1, "squirrel"); 168 plainArray.add(2, "sparrow"); 169 let result = plainArray.get(1); 170 map.set("test PlainArray get0001:", result == "squirrel"); 171 } 172 173 { 174 let plainArray = new PlainArray(); 175 plainArray.add(1, ""); 176 plainArray.add(2, "sparrow"); 177 let result = plainArray.get(1); 178 map.set("test PlainArray get0002:", result == ''); 179 } 180 181 { 182 try { 183 let plainArray = new PlainArray(); 184 plainArray.add(null, "zhang san"); 185 plainArray.get(null); 186 } catch (err) { 187 const code = err.code; 188 let result = (code == 401); 189 map.set("test PlainArray get0003:", result == true); 190 } 191 } 192 193 { 194 try { 195 let plainArray = new PlainArray(); 196 plainArray.add(undefined, "zhang san"); 197 plainArray.get(undefined); 198 } catch (err) { 199 const code = err.code; 200 let result = (code == 401); 201 map.set("test PlainArray get0004:", result == true); 202 } 203 } 204 205 { 206 let plainArray = new PlainArray(); 207 plainArray.add(0, "zhang san"); 208 let str = plainArray.get(0); 209 let result = (str == "zhang san"); 210 map.set("test PlainArray get0005:", result == true); 211 plainArray.add(-1, "zhang san"); 212 str = plainArray.get(-1); 213 result = (str == "zhang san"); 214 map.set("test PlainArray get0005:", result == true); 215 216 let num = 2147483647; 217 plainArray.add(num, "li si"); 218 str = plainArray.get(num); 219 result = (str == "li si"); 220 map.set("test PlainArray get0005:", result == true); 221 let num1 = -2147483648; 222 plainArray.add(num1, "li si"); 223 str = plainArray.get(num1); 224 result = (str == "li si"); 225 map.set("test PlainArray get0005:", result == true); 226 } 227 228 { 229 let plainArray = new PlainArray(); 230 plainArray.add(1, "squirrel"); 231 plainArray.add(2, ""); 232 let result = plainArray.getIndexOfKey(2); 233 map.set("test PlainArray getIndexOfKey0001:", result == 1); 234 } 235 236 { 237 let plainArray = new PlainArray(); 238 plainArray.add(1, new Tmp(10)); 239 plainArray.add(2, new Tmp(12)); 240 let result = plainArray.getIndexOfKey(2); 241 map.set("test PlainArray getIndexOfKey0002:", result == 1); 242 } 243 244 { 245 let plainArray = new PlainArray(); 246 plainArray.add(1, "squirrel"); 247 plainArray.add(2, null); 248 plainArray.add(3, undefined); 249 let result = plainArray.getIndexOfKey(2); 250 map.set("test PlainArray getIndexOfKey0003:", result == 1); 251 let result1 = plainArray.getIndexOfKey(3); 252 map.set("test PlainArray getIndexOfKey0003:", result1 == 2); 253 } 254 255 { 256 let plainArray = new PlainArray(); 257 plainArray.add(1, "squirrel"); 258 plainArray.add(2, "sparrow"); 259 let result = plainArray.getIndexOfKey(2); 260 map.set("test PlainArray getIndexOfKey0004:", result == 1); 261 } 262 263 { 264 let plainArray = new PlainArray(); 265 plainArray.add(1, "squirrel"); 266 plainArray.add(2, ""); 267 let result = plainArray.getIndexOfValue(""); 268 map.set("test PlainArray getIndexOfValue0001:", result == 1); 269 } 270 271 { 272 let plainArray = new PlainArray(); 273 let param1 = new Tmp(10); 274 plainArray.add(1, param1); 275 let param2 = new Tmp(10); 276 plainArray.add(2, param2); 277 let result = plainArray.getIndexOfValue(param2); 278 map.set("test PlainArray getIndexOfValue0002:", result == 1); 279 } 280 281 { 282 let plainArray = new PlainArray(); 283 plainArray.add(1, "squirrel"); 284 plainArray.add(2, null); 285 plainArray.add(3, undefined); 286 let result = plainArray.getIndexOfValue(null); 287 map.set("test PlainArray getIndexOfValue0003:", result == 1); 288 let result1 = plainArray.getIndexOfValue(undefined); 289 map.set("test PlainArray getIndexOfValue0003:", result1 == 2); 290 } 291 292 { 293 let plainArray = new PlainArray(); 294 plainArray.add(1, "squirrel"); 295 plainArray.add(2, "sparrow"); 296 let result = plainArray.getIndexOfValue("sparrow"); 297 map.set("test PlainArray getIndexOfValue0004:", result == 1); 298 } 299 300 { 301 let plainArray = new PlainArray(); 302 plainArray.add(1, "squirrel"); 303 plainArray.add(2, "sparrow"); 304 let result = plainArray.getKeyAt(3); 305 map.set("test PlainArray getKeyAt0001:", result == undefined); 306 } 307 308 { 309 let plainArray = new PlainArray(); 310 plainArray.add(1, "squirrel"); 311 plainArray.add(2, "sparrow"); 312 let result = plainArray.getKeyAt(1); 313 map.set("test PlainArray getKeyAt0002:", result == 2); 314 } 315 316 { 317 try { 318 let plainArray = new PlainArray(); 319 plainArray.add(1, "squirrel"); 320 plainArray.add(2, "sparrow"); 321 plainArray.getKeyAt(null); 322 } catch (err) { 323 const code = err.code; 324 let result = (code == 401); 325 map.set("test PlainArray getKeyAt0003:", result == true); 326 } 327 } 328 329 { 330 try { 331 let plainArray = new PlainArray(); 332 plainArray.add(1, "squirrel"); 333 plainArray.add(2, "sparrow"); 334 plainArray.getKeyAt(undefined); 335 } catch (err) { 336 const code = err.code; 337 let result = (code == 401); 338 map.set("test PlainArray getKeyAt0004:", result == true); 339 } 340 } 341 342 { 343 let plainArray = new PlainArray(); 344 plainArray.add(1, "squirrel"); 345 plainArray.add(2, "sparrow"); 346 let result = plainArray.getKeyAt(1); 347 let index = plainArray.getIndexOfKey(result); 348 map.set("test PlainArray getKeyAt0005:", index == 1); 349 } 350 351 { 352 try { 353 let plainArray = new PlainArray(); 354 plainArray.add(1, "squirrel"); 355 plainArray.add(2, "sparrow"); 356 plainArray.getValueAt(2); 357 } catch (err) { 358 const code = err.code; 359 let result = (code == 10200001); 360 map.set("test PlainArray getValueAt0001:", result == true); 361 } 362 } 363 364 { 365 let plainArray = new PlainArray(); 366 plainArray.add(1, "squirrel"); 367 plainArray.add(2, "sparrow"); 368 let result = plainArray.getValueAt(1); 369 map.set("test PlainArray getValueAt0002:", result == "sparrow"); 370 } 371 372 { 373 try { 374 let plainArray = new PlainArray(); 375 plainArray.add(1, "squirrel"); 376 plainArray.add(2, "sparrow"); 377 plainArray.getValueAt(null); 378 } catch (err) { 379 const code = err.code; 380 let result = (code == 401); 381 map.set("test PlainArray getValueAt0003:", result == true); 382 } 383 } 384 385 { 386 try { 387 let plainArray = new PlainArray(); 388 plainArray.add(1, "squirrel"); 389 plainArray.add(2, "sparrow"); 390 plainArray.getValueAt(undefined); 391 } catch (err) { 392 const code = err.code; 393 let result = (code == 401); 394 map.set("test PlainArray getValueAt0004:", result == true); 395 } 396 } 397 398 { 399 let plainArray = new PlainArray(); 400 plainArray.add(1, "squirrel"); 401 plainArray.add(2, "sparrow"); 402 let result = plainArray.getValueAt(1); 403 let index = plainArray.getIndexOfValue(result); 404 map.set("test PlainArray getValueAt0005:", index == 1); 405 } 406 407 { 408 let plainArray = new PlainArray(); 409 plainArray.add(1, "squirrel"); 410 plainArray.add(2, "sparrow"); 411 let newPlainArray = plainArray.clone(); 412 let result = (newPlainArray.length == plainArray.length); 413 map.set("test PlainArray clone0001", result == true); 414 } 415 416 { 417 let plainArray = new PlainArray(); 418 plainArray.add(1, 1); 419 plainArray.add(2, 2); 420 let newPlainArray = plainArray.clone(); 421 let result = (newPlainArray.length == plainArray.length); 422 map.set("test PlainArray clone0002", result == true); 423 } 424 425 { 426 let plainArray = new PlainArray(); 427 plainArray.add(1, new Tmp(10)); 428 plainArray.add(2, new Tmp(12)); 429 let newPlainArray = plainArray.clone(); 430 let result = (newPlainArray.length == plainArray.length); 431 map.set("test PlainArray clone0003", result == true); 432 } 433 434 { 435 let plainArray = new PlainArray(); 436 plainArray.add(1, new Tmp(10)); 437 plainArray.add(2, new Tmp(12)); 438 let newPlainArray = plainArray.clone(); 439 newPlainArray.add(3, new Tmp(13)); 440 let result = plainArray.length; 441 map.set("test PlainArray clone0004", result == 2); 442 } 443 444 { 445 let plainArray = new PlainArray(); 446 let param = "a".repeat(5120); 447 plainArray.add(1, param); 448 let newPlainArray = plainArray.clone(); 449 let result = (newPlainArray.length == plainArray.length); 450 map.set("test PlainArray clone0005", result == true); 451 } 452 453 { 454 let plainArray = new PlainArray(); 455 plainArray.add(1, null); 456 plainArray.add(2, undefined); 457 let newPlainArray = plainArray.clone(); 458 let result = (newPlainArray.length == plainArray.length); 459 map.set("test PlainArray clone0006", result == true); 460 } 461 462 { 463 let plainArray = new PlainArray(); 464 let param = "a".repeat(5120); 465 plainArray.add(1, param); 466 let result = plainArray.length; 467 map.set("test PlainArray add0001", result == 1); 468 } 469 470 { 471 let plainArray = new PlainArray(); 472 plainArray.add(1, "squirrel"); 473 let result = plainArray.length; 474 map.set("test PlainArray add0002", result == 1); 475 } 476 477 { 478 let plainArray = new PlainArray(); 479 plainArray.add(1, null); 480 plainArray.add(2, undefined); 481 let result = plainArray.length; 482 map.set("test PlainArray add0003", result == 2); 483 } 484 485 { 486 let plainArray = new PlainArray(); 487 plainArray.add(1, "squirrel"); 488 plainArray.add(2, "sparrow"); 489 let result = plainArray.remove(2); 490 map.set("test PlainArray remove0001", result == "sparrow"); 491 } 492 493 { 494 try { 495 let plainArray = new PlainArray(); 496 plainArray.remove(null); 497 } catch (err) { 498 const code = err.code; 499 let result = (code == 401); 500 map.set("test PlainArray remove0002", result == true); 501 } 502 } 503 504 { 505 try { 506 let plainArray = new PlainArray(); 507 plainArray.remove(undefined); 508 } catch (err) { 509 const code = err.code; 510 let result = (code == 401); 511 map.set("test PlainArray remove0003", result == true); 512 } 513 } 514 515 { 516 let plainArray = new PlainArray(); 517 plainArray.add(1, "zhang san"); 518 plainArray.add(2, "zhang san1"); 519 let result = plainArray.remove(3); 520 map.set("test PlainArray remove0004", result == undefined); 521 } 522 523 { 524 let plainArray = new PlainArray(); 525 plainArray.add(1, "zhang san"); 526 plainArray.add(2, "zhang san1"); 527 let num = 2147483647; 528 plainArray.add(num, "li si"); 529 let result = plainArray.remove(num); 530 map.set("test PlainArray remove0005", result == "li si"); 531 let num1 = -2147483648; 532 plainArray.add(num1, "li si"); 533 result = plainArray.remove(num1); 534 map.set("test PlainArray remove0005", result == "li si"); 535 } 536 537 { 538 let plainArray = new PlainArray(); 539 plainArray.add(1, "squirrel"); 540 plainArray.add(2, "sparrow"); 541 let result = plainArray.removeAt(1); 542 map.set("test PlainArray removeAt0001", result == "sparrow"); 543 } 544 545 { 546 let plainArray = new PlainArray(); 547 plainArray.add(1, "squirrel"); 548 plainArray.add(2, "sparrow"); 549 let result = plainArray.removeAt(-1); 550 map.set("test PlainArray removeAt0002", result == undefined); 551 } 552 553 { 554 let plainArray = new PlainArray(); 555 plainArray.add(1, "squirrel"); 556 plainArray.add(2, "sparrow"); 557 let result = plainArray.removeAt(2); 558 map.set("test PlainArray removeAt0003", result == undefined); 559 } 560 561 { 562 let plainArray = new PlainArray(); 563 let result = plainArray.removeAt(0); 564 map.set("test PlainArray removeAt0004", result == undefined); 565 } 566 567 { 568 try { 569 let plainArray = new PlainArray(); 570 plainArray.add(1, "squirrel"); 571 plainArray.add(2, "sparrow"); 572 plainArray.removeRangeFrom(1, 0); 573 } catch (err) { 574 const code = err.code; 575 let result = (code == 10200001) 576 map.set("test PlainArray removeRangeFrom0001", result == true); 577 } 578 } 579 580 { 581 let plainArray = new PlainArray(); 582 plainArray.add(1, "squirrel"); 583 plainArray.add(2, "sparrow"); 584 let result = plainArray.removeRangeFrom(1, 3); 585 map.set("test PlainArray removeRangeFrom0002", result == 1); 586 } 587 588 { 589 let plainArray = new PlainArray(); 590 plainArray.add(1, "squirrel"); 591 plainArray.add(2, "sparrow"); 592 let result = plainArray.removeRangeFrom(1, Math.floor(7.5)); 593 map.set("test PlainArray removeRangeFrom0003", result == 1); 594 } 595 596 { 597 try { 598 let plainArray = new PlainArray(); 599 let result = plainArray.removeRangeFrom(0, 0); 600 } catch (err) { 601 const code = err.code; 602 let result = (code == 10200001); 603 map.set("test PlainArray removeRangeFrom0004", result == true); 604 } 605 } 606 607 { 608 let plainArray = new PlainArray(); 609 plainArray.add(1, "squirrel"); 610 plainArray.add(2, "sparrow"); 611 plainArray.setValueAt(1, 3546); 612 let result = plainArray.getValueAt(1); 613 map.set("test PlainArray setValueAt0001", result == 3546); 614 } 615 616 { 617 let plainArray = new PlainArray(); 618 plainArray.add(1, "zhang san"); 619 plainArray.add(2, "zhang san1"); 620 let num = 2147483647; 621 plainArray.add(num, "li si"); 622 let result = plainArray.remove(num); 623 map.set("test PlainArray remove0005", result == "li si"); 624 let num1 = -2147483648; 625 plainArray.add(num1, "li si"); 626 result = plainArray.remove(num1); 627 map.set("test PlainArray remove0005", result == "li si"); 628 } 629 630 { 631 let plainArray = new PlainArray(); 632 plainArray.add(1, "squirrel"); 633 plainArray.add(2, "sparrow"); 634 let result = plainArray.removeAt(1); 635 map.set("test PlainArray removeAt0001", result == "sparrow"); 636 } 637 638 { 639 let plainArray = new PlainArray(); 640 plainArray.add(1, "squirrel"); 641 plainArray.add(2, "sparrow"); 642 let result = plainArray.removeAt(-1); 643 map.set("test PlainArray removeAt0002", result == undefined); 644 } 645 646 { 647 let plainArray = new PlainArray(); 648 plainArray.add(1, "squirrel"); 649 plainArray.add(2, "sparrow"); 650 let result = plainArray.removeAt(2); 651 map.set("test PlainArray removeAt0003", result == undefined); 652 } 653 654 { 655 let plainArray = new PlainArray(); 656 let result = plainArray.removeAt(0); 657 map.set("test PlainArray removeAt0004", result == undefined); 658 } 659 660 { 661 try { 662 let plainArray = new PlainArray(); 663 plainArray.add(1, "squirrel"); 664 plainArray.add(2, "sparrow"); 665 plainArray.removeRangeFrom(1, 0); 666 } catch (err) { 667 const code = err.code; 668 let result = (code == 10200001) 669 map.set("test PlainArray removeRangeFrom0001", result == true); 670 } 671 } 672 673 { 674 let plainArray = new PlainArray(); 675 plainArray.add(1, "squirrel"); 676 plainArray.add(2, "sparrow"); 677 let result = plainArray.removeRangeFrom(1, 3); 678 map.set("test PlainArray removeRangeFrom0002", result == 1); 679 } 680 681 { 682 let plainArray = new PlainArray(); 683 plainArray.add(1, "squirrel"); 684 plainArray.add(2, "sparrow"); 685 let result = plainArray.removeRangeFrom(1, Math.floor(7.5)); 686 map.set("test PlainArray removeRangeFrom0003", result == 1); 687 } 688 689 { 690 try { 691 let plainArray = new PlainArray(); 692 let result = plainArray.removeRangeFrom(0, 0); 693 } catch (err) { 694 const code = err.code; 695 let result = (code == 10200001); 696 map.set("test PlainArray removeRangeFrom0004", result == true); 697 } 698 } 699 700 { 701 let plainArray = new PlainArray(); 702 plainArray.add(1, "squirrel"); 703 plainArray.add(2, "sparrow"); 704 plainArray.setValueAt(1, 3546); 705 let result = plainArray.getValueAt(1); 706 map.set("test PlainArray setValueAt0001", result == 3546); 707 } 708 709 { 710 let plainArray = new PlainArray(); 711 plainArray.add(1, "zhang san"); 712 plainArray.add(2, "zhang san1"); 713 let num = 2147483647; 714 plainArray.add(num, "li si"); 715 let result = plainArray.remove(num); 716 map.set("test PlainArray remove0005", result == "li si"); 717 let num1 = -2147483648; 718 plainArray.add(num1, "li si"); 719 result = plainArray.remove(num1); 720 map.set("test PlainArray remove0005", result == "li si"); 721 } 722 723 { 724 let plainArray = new PlainArray(); 725 plainArray.add(1, "squirrel"); 726 plainArray.add(2, "sparrow"); 727 let result = plainArray.removeAt(1); 728 map.set("test PlainArray removeAt0001", result == "sparrow"); 729 } 730 731 { 732 let plainArray = new PlainArray(); 733 plainArray.add(1, "squirrel"); 734 plainArray.add(2, "sparrow"); 735 let result = plainArray.removeAt(-1); 736 map.set("test PlainArray removeAt0002", result == undefined); 737 } 738 739 { 740 let plainArray = new PlainArray(); 741 plainArray.add(1, "squirrel"); 742 plainArray.add(2, "sparrow"); 743 let result = plainArray.removeAt(2); 744 map.set("test PlainArray removeAt0003", result == undefined); 745 } 746 747 { 748 let plainArray = new PlainArray(); 749 let result = plainArray.removeAt(0); 750 map.set("test PlainArray removeAt0004", result == undefined); 751 } 752 753 { 754 try { 755 let plainArray = new PlainArray(); 756 plainArray.add(1, "squirrel"); 757 plainArray.add(2, "sparrow"); 758 plainArray.removeRangeFrom(1, 0); 759 } catch (err) { 760 const code = err.code; 761 let result = (code == 10200001) 762 map.set("test PlainArray removeRangeFrom0001", result == true); 763 } 764 } 765 766 { 767 let plainArray = new PlainArray(); 768 plainArray.add(1, "squirrel"); 769 plainArray.add(2, "sparrow"); 770 let result = plainArray.removeRangeFrom(1, 3); 771 map.set("test PlainArray removeRangeFrom0002", result == 1); 772 } 773 774 { 775 let plainArray = new PlainArray(); 776 plainArray.add(1, "squirrel"); 777 plainArray.add(2, "sparrow"); 778 let result = plainArray.removeRangeFrom(1, Math.floor(7.5)); 779 map.set("test PlainArray removeRangeFrom0003", result == 1); 780 } 781 782 { 783 try { 784 let plainArray = new PlainArray(); 785 let result = plainArray.removeRangeFrom(0, 0); 786 } catch (err) { 787 const code = err.code; 788 let result = (code == 10200001); 789 map.set("test PlainArray removeRangeFrom0004", result == true); 790 } 791 } 792 793 { 794 let plainArray = new PlainArray(); 795 plainArray.add(1, "squirrel"); 796 plainArray.add(2, "sparrow"); 797 plainArray.setValueAt(1, 3546); 798 let result = plainArray.getValueAt(1); 799 map.set("test PlainArray setValueAt0001", result == 3546); 800 } 801 802 { 803 let plainArray = new PlainArray(); 804 for (let i = 0; i < 1024; i++) { 805 plainArray.add(i, i); 806 } 807 plainArray.toString(); 808 let result = plainArray.length; 809 map.set("test PlainArray toString0006", result == 1024); 810 } 811 812 { 813 let plainArray = new PlainArray(); 814 plainArray.add(1, "squirrel"); 815 plainArray.add(2, "sparrow"); 816 plainArray.clear(); 817 let result = plainArray.length; 818 map.set("test PlainArray clear0001", result == 0); 819 } 820 821 { 822 let plainArray = new PlainArray(); 823 plainArray.add(1, "squirrel"); 824 plainArray.clear(); 825 plainArray.add(2, "sparrow"); 826 let result = plainArray.length; 827 map.set("test PlainArray clear0002", result == 1); 828 } 829 830 { 831 let plainArray = new PlainArray(); 832 plainArray.clear(); 833 let result = plainArray.length; 834 map.set("test PlainArray clear0003", result == 0); 835 } 836 837 { 838 let plainArray = new PlainArray(); 839 plainArray.add(1, "squirrel"); 840 plainArray.add(2, "sparrow"); 841 let myFlag = true; 842 plainArray.forEach((value, index) => { 843 if (plainArray.get(index) != value) { 844 myFlag = false; 845 } 846 }); 847 map.set("test PlainArray forEach0001", myFlag); 848 } 849 850 { 851 let plainArray = new PlainArray(); 852 plainArray.add(1, "squirrel"); 853 plainArray.add(2, "sparrow"); 854 plainArray.add(3, "sparrow"); 855 let myFlag = true; 856 plainArray.forEach((value, index) => { 857 if (plainArray.get(index) != value) { 858 myFlag = false; 859 } 860 }); 861 map.set("test PlainArray forEach0002", myFlag); 862 } 863 864 { 865 let plainArray = new PlainArray(); 866 let myFlag = true; 867 plainArray.forEach((value, index) => { 868 if (plainArray.get(index) != value) { 869 myFlag = false; 870 } 871 }); 872 map.set("test PlainArray forEach0003", myFlag); 873 } 874 875 { 876 let plainArray = new PlainArray(); 877 plainArray.add(0, undefined); 878 plainArray.add(1, null); 879 let myFlag = true; 880 plainArray.forEach((value, index) => { 881 if (plainArray.get(index) != value) { 882 myFlag = false; 883 } 884 }); 885 map.set("test PlainArray forEach0004", myFlag); 886 } 887 888 { 889 let plainArray = new PlainArray(); 890 plainArray.add(0, undefined); 891 plainArray.add(1, null); 892 let myFlag = true; 893 plainArray.forEach(() => { 894 if (plainArray.length != 2) { 895 myFlag = false; 896 } 897 }); 898 map.set("test PlainArray forEach0005", myFlag); 899 } 900 901 { 902 let plainArray = new PlainArray(); 903 plainArray.add(1, "squirrel"); 904 plainArray.add(2, "sparrow"); 905 plainArray.add(3, "sparrow"); 906 let myFlag = true; 907 plainArray.forEach((value, index) => { 908 if (plainArray.get(index) != value) { 909 myFlag = false; 910 } 911 }); 912 map.set("test PlainArray forEach0002", myFlag); 913 } 914 915 { 916 let plainArray = new PlainArray(); 917 let myFlag = true; 918 plainArray.forEach((value, index) => { 919 if (plainArray.get(index) != value) { 920 myFlag = false; 921 } 922 }); 923 map.set("test PlainArray forEach0003", myFlag); 924 } 925 926 { 927 let plainArray = new PlainArray(); 928 plainArray.add(0, undefined); 929 plainArray.add(1, null); 930 let myFlag = true; 931 plainArray.forEach((value, index) => { 932 if (plainArray.get(index) != value) { 933 myFlag = false; 934 } 935 }); 936 map.set("test PlainArray forEach0004", myFlag); 937 } 938 939 { 940 let plainArray = new PlainArray(); 941 plainArray.add(0, undefined); 942 plainArray.add(1, null); 943 let myFlag = true; 944 plainArray.forEach(() => { 945 if (plainArray.length != 2) { 946 myFlag = false; 947 } 948 }); 949 map.set("test PlainArray forEach0005", myFlag); 950 } 951 952 { 953 let plainArray = new PlainArray(); 954 plainArray.add(1, "squirrel"); 955 plainArray.add(2, "sparrow"); 956 plainArray.add(3, "sparrow"); 957 let myFlag = true; 958 plainArray.forEach((value, index) => { 959 if (plainArray.get(index) != value) { 960 myFlag = false; 961 } 962 }); 963 map.set("test PlainArray forEach0002", myFlag); 964 } 965 966 { 967 let plainArray = new PlainArray(); 968 let myFlag = true; 969 plainArray.forEach((value, index) => { 970 if (plainArray.get(index) != value) { 971 myFlag = false; 972 } 973 }); 974 map.set("test PlainArray forEach0003", myFlag); 975 } 976 977 { 978 let plainArray = new PlainArray(); 979 plainArray.add(0, undefined); 980 plainArray.add(1, null); 981 let myFlag = true; 982 plainArray.forEach((value, index) => { 983 if (plainArray.get(index) != value) { 984 myFlag = false; 985 } 986 }); 987 map.set("test PlainArray forEach0004", myFlag); 988 } 989 990 { 991 let plainArray = new PlainArray(); 992 plainArray.add(0, undefined); 993 plainArray.add(1, null); 994 let myFlag = true; 995 plainArray.forEach(() => { 996 if (plainArray.length != 2) { 997 myFlag = false; 998 } 999 }); 1000 map.set("test PlainArray forEach0005", myFlag); 1001 } 1002 1003 res = undefined; 1004 function elements(value, key, map) { 1005 if (!value) { 1006 if (!res) { 1007 res = []; 1008 } 1009 res.push(key); 1010 } 1011 } 1012 map.forEach(elements); 1013 1014 let de = new PlainArray(); 1015 try { 1016 de.forEach(123); 1017 } catch(err) { 1018 if (err.name != "BusinessError") { 1019 print("PlainArray forEach throw error fail"); 1020 } 1021 } 1022 1023 // Math.floor as index input should not throw exception. 1024 let myPa = new PlainArray(); 1025 myPa.add(0, "a"); 1026 myPa.add(Math.floor(1.5), "b"); 1027 myPa.has(Math.floor(1.5)); 1028 myPa.get(Math.floor(1.5)); 1029 myPa.getValueAt(Math.floor(1.5)); 1030 myPa.setValueAt(Math.floor(1.5), "c"); 1031 myPa.getKeyAt(Math.floor(1.5)); 1032 myPa.getIndexOfKey(Math.floor(1.5)); 1033 myPa.removeAt(Math.floor(1.5)); 1034 1035 if (!res) { 1036 print("Test PlainArray success!!!"); 1037 } else { 1038 print("Test PlainArray fail: " + res); 1039 } 1040 const v6 = new PlainArray() 1041 function f2(a3) { 1042 return a3 1043 } 1044 const o5 = { 1045 "get" : f2, 1046 } 1047 const v7 = new Proxy(v6, o5) 1048 try { 1049 v7[1073741823] 1050 } catch (error) { 1051 print(error) 1052 } 1053} 1054export let plainarrayRes = "Test PlainArray done"; 1055