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:sendablearray
18 * @tc.desc:test sendablearray
19 * @tc.type: FUNC
20 * @tc.require: issueI8QUU0
21 */
22
23// @ts-nocheck
24declare function print(str: any): string;
25
26class SuperClass {
27    public num: number = 0;
28    constructor(num: number) {
29        "use sendable"
30        this.num = num;
31    }
32}
33
34class SubClass extends SuperClass {
35    public strProp: string = "";
36    constructor(num: number) {
37        "use sendable"
38        super(num);
39        this.strProp = "" + num;
40    }
41}
42
43class SubSharedClass extends SendableArray {
44  constructor() {
45    'use sendable';
46    super();
47  }
48}
49
50class SuperUnSharedClass {
51    public num: number = 0;
52    constructor(num: number) {
53        this.num = num;
54    }
55}
56
57SendableArray.from<string>(['a', 'r', 'k']);
58
59function at() {
60    print("Start Test at")
61    const array1 = new SendableArray<number>(5, 12, 8, 130, 44);
62    let index = 2;
63    print(`An index of ${index} returns ${array1.at(index)}`); // An index of 2 returns 8
64
65    index = -2;
66    print(`An index of ${index} returns ${array1.at(index)}`); // An index of -2 returns 130
67
68    index = 200;
69    print(`An index of ${index} returns ${array1.at(index)}`); // An index of 200 returns undefined
70
71    print(`An index of null returns ${array1.at(null)}`); // An index of null returns 5
72
73    print(`An index of undefined returns ${array1.at(undefined)}`); // An index of undefined returns 5
74
75    print(`An index of undefined returns ${array1.at(true)}`); // An index of true returns 12
76
77    print(`An index of undefined returns ${array1.at(false)}`); // An index of false returns 5
78
79    index = 2871622679;
80    print(`An index of 2871622679 returns ${array1.at(index)}`); // An index of 2871622679 returns undefined
81}
82
83function entries() {
84    print("Start Test entries")
85    const array1 = new SendableArray<string>('a', 'b', 'c');
86    const iterator = array1.entries();
87    for (const [key, value] of iterator) {
88        print("" + key + "," + value); // 0 a, 1 b, 2 c
89    }
90}
91
92function keys() {
93    print("Start Test keys")
94    const array1 = new SendableArray<string>('a', 'b', 'c');
95    const iterator = array1.keys();
96    for (const key of iterator) {
97        print("" + key); // 0, 1, 2
98    }
99}
100
101function values() {
102    print("Start Test values")
103    const array1 = new SendableArray<string>('a', 'b', 'c');
104    const iterator = array1.values();
105    for (const value of iterator) {
106        print("" + value); // a, b, c
107    }
108}
109
110function find() {
111    print("Start Test find")
112    const array1 = new SendableArray<number>(5, 12, 8, 130, 44);
113
114    const found = array1.find((element: number) => element > 10);
115    print("" + found); // 12
116
117    const array2 = new SendableArray<SuperClass>(
118      new SubClass(5),
119      new SubClass(32),
120      new SubClass(8),
121      new SubClass(130),
122      new SubClass(44),
123    );
124    const result: SubClass | undefined = array2.find<SubClass>(
125      (value: SuperClass, index: number, obj: SendableArray<SuperClass>) => value instanceof SubClass,
126    );
127    print((new SubClass(5)).strProp); // 5
128}
129
130function includes() {
131    print("Start Test includes")
132    const array1 = new SendableArray<number>(1, 2, 3);
133    print("" + array1.includes(2)); // true
134
135    const pets = new SendableArray<string>('cat', 'dog', 'bat');
136    print("" + pets.includes('cat')); // true
137
138    print("" + pets.includes('at')); // false
139}
140
141function index() {
142    print("Start Test index")
143    const array1 = new SendableArray<number>(5, 12, 8, 130, 44);
144    const isLargeNumber = (element: number) => element > 13;
145    print("" + array1.findIndex(isLargeNumber)); // 3
146
147}
148
149function fill() {
150    print("Start Test fill")
151    const array1 = new SendableArray<number>(1, 2, 3, 4);
152    array1.fill(0, 2, 4);
153    print(array1); // [1, 2, 0, 0]
154
155    array1.fill(5, 1);
156    print(array1); // [1, 5, 5, 5]
157
158    array1.fill(6);
159    print(array1) // [6, 6, 6, 6]
160}
161
162// remove
163function pop() {
164    print("Start Test pop")
165    const sharedArray = new SendableArray<number>(5, 12, 8, 130, 44);
166    print("poped: " + sharedArray.pop());
167}
168
169// update
170function randomUpdate() {
171    print("Start Test randomUpdate")
172    const sharedArray = new SendableArray<number>(5, 12, 8, 130, 44);
173    sharedArray[1] = 30
174    print(sharedArray[1]);
175    try {
176        sharedArray[null] = 30
177    } catch (err) {
178        print("add element by index access failed. err: " + err + ", code: " + err.code);
179    }
180
181    try {
182        sharedArray[undefined] = 30
183    } catch (err) {
184        print("add element by index access failed. err: " + err + ", code: " + err.code);
185    }
186
187    try {
188        sharedArray[2871622679] = 30
189    } catch (err) {
190        print("add element by index access failed. err: " + err + ", code: " + err.code);
191    }
192}
193
194//  get
195function randomGet() {
196    print("Start Test randomGet")
197    const sharedArray = new SendableArray<number>(5, 12, 8, 130, 44);
198    sharedArray.at(0)
199    print(sharedArray);
200}
201
202// add
203function randomAdd() {
204    print("Start Test randomAdd")
205    const sharedArray = new SendableArray<number>(5, 12, 8);
206    try {
207        sharedArray[4000] = 7;
208    } catch (err) {
209        print("add element by index access failed. err: " + err + ", code: " + err.code);
210    }
211}
212
213function create(): void {
214    print("Start Test create")
215    let arkTSTest: SendableArray<number> = new SendableArray<number>(5);
216    let arkTSTest1: SendableArray<number> = new SendableArray<number>(1, 3, 5);
217}
218
219function from(): void {
220    print("Start Test from")
221    print(SendableArray.from<string>(['A', 'B', 'C']));
222    try {
223        print(SendableArray.from<string>(['E', , 'M', 'P', 'T', 'Y']));
224    } catch (err) {
225        print("Create from empty element list failed. err: " + err + ", code: " + err.code);
226    }
227    const source = new SendableArray<undefined>(undefined, undefined, 1);
228    try {
229        print('Create from sendable undefined element list success. arr: ' + SendableArray.from<string>(source));
230    } catch (err) {
231        print("Create from sendable undefined element list failed. err: " + err + ", code: " + err.code);
232    }
233    // trigger string cache
234    SendableArray.from("hello");
235    print(SendableArray.from("hello"));
236}
237
238function fromTemplate(): void {
239  print('Start Test fromTemplate');
240  let artTSTest1: SendableArray<string> = SendableArray.from<Number, string>([1, 2, 3], (x: number) => '' + x);
241  print('artTSTest1: ' + artTSTest1);
242  let arkTSTest2: SendableArray<string> = SendableArray.from<Number, string>([1, 2, 3], (item: number) => '' + item); // ["1", "Key", "3"]
243  print('arkTSTest2: ' + arkTSTest2);
244}
245
246function length(): void {
247    print("Start Test length")
248    let array: SendableArray<number> = new SendableArray<number>(1, 3, 5);
249    print("Array length: " + array.length);
250    array.length = 50;
251    print("Array length after changed: " + array.length);
252}
253
254function push(): void {
255    print("Start Test push")
256    let array: SendableArray<number> = new SendableArray<number>(1, 3, 5);
257    array.push(2, 4, 6);
258    print("Elements pushed: " + array);
259}
260
261function concat(): void {
262    print("Start Test concat")
263    let array: SendableArray<number> = new SendableArray<number>(1, 3, 5);
264    let arkTSToAppend: SendableArray<number> = new SendableArray<number>(2, 4, 6);
265    let arkTSToAppend1: SendableArray<number> = new SendableArray<number>(100, 101, 102);
266
267    print(array.concat(arkTSToAppend)); // [1, 3, 5, 2, 4, 6]
268    print(array.concat(arkTSToAppend, arkTSToAppend1));
269    print(array.concat(200));
270    print(array.concat(201, 202));
271    let arr: SendableArray<number> = array.concat(null);
272    print(arr);
273    print(arr[3]);
274    print(arr.length);
275    let arr1: SendableArray<number> = array.concat(undefined);
276    print(arr1);
277    print(arr1[3]);
278    print(arr1.length);
279}
280
281function join(): void {
282    print("Start Test join")
283    const elements = new SendableArray<string>('Fire', 'Air', 'Water');
284    print(elements.join());
285    print(elements.join(''));
286    print(elements.join('-'));
287    print(elements.join(null));
288    print(elements.join(undefined));
289}
290
291function shift() {
292    print("Start Test shift")
293    const array1 = new SendableArray<number>(2, 4, 6);
294    print(array1.shift());
295    print(array1.length);
296
297    const emptyArray = new SendableArray<number>();
298    print(emptyArray.shift());
299}
300
301function unshift() {
302    print("Start Test unshift")
303    const array = SendableArray.from<number>([1, 2, 3]);
304    print(array.unshift(4, 5));
305    print(array.length);
306}
307
308function slice() {
309    print("Start Test slice")
310    const animals = new SendableArray<string>('ant', 'bison', 'camel', 'duck', 'elephant');
311    print(animals.slice());
312    print(animals.slice(2));
313    print(animals.slice(2, 4));
314    try {
315        let a1 = animals.slice(1.5, 4);
316        print("slice(1.5, 4) element success");
317        print(a1);
318    } catch (err) {
319        print("slice element failed. err: " + err + ", code: " + err.code);
320    }
321
322    try {
323        let a2 = animals.slice(8, 4);
324        print("slice(8, 4) element success");
325    } catch (err) {
326        print("slice element failed. err: " + err + ", code: " + err.code);
327    }
328
329    try {
330        let a3 = animals.slice(8, 100);
331        print("slice(8, 100) element success");
332    } catch (err) {
333        print("slice element failed. err: " + err + ", code: " + err.code);
334    }
335
336    try {
337        print(animals.slice(null));
338    } catch (err) {
339        print("slice element failed. err: " + err + ", code: " + err.code);
340    }
341
342    try {
343        print(animals.slice(undefined));
344    } catch (err) {
345        print("slice element failed. err: " + err + ", code: " + err.code);
346    }
347}
348
349function sort() {
350    print("Start Test sort")
351    const months = new SendableArray<string>('March', 'Jan', 'Feb', 'Dec');
352    print(months.sort());
353
354    const array1 = [1, 30, 4, 21, 10000];
355    print(array1.sort());
356
357    array1.sort((a: number, b: number) => a - b);
358}
359
360function indexOf() {
361    print("Start Test indexOf")
362    const beasts = new SendableArray<string>('ant', 'bison', 'camel', 'duck', 'bison');
363    print(beasts.indexOf('bison')); // Expected: 1
364    print(beasts.indexOf('bison', 2)) // Expected: 4
365    print(beasts.indexOf('giraffe')) // Expected: -1
366}
367
368function forEach() {
369  print('Start Test forEach');
370  const array = new SendableArray<string>('a', 'b', 'c');
371  array.forEach((element: string) => print(element)); // a <br/> b <br/>  c
372
373  array.forEach((element: string, index: number, array: SendableArray<string>) =>
374    print(`a[${index}] = ${element}, ${array[index]}`),
375  );
376}
377
378function map() {
379    print("Start Test map")
380    const array = new SendableArray<number>(1, 4, 9, 16);
381    print(array.map<string>((x: number) => x + x));
382}
383
384function filter() {
385    print("Start Test filter")
386    const words = new SendableArray<string>('spray', 'elite', 'exuberant', 'destruction', 'present');
387    print(words.filter((word: string) => word.length > 6))
388    const array2 = new SendableArray<SuperClass>(
389      new SubClass(5),
390      new SuperClass(12),
391      new SubClass(8),
392      new SuperClass(130),
393      new SubClass(44),
394    );
395    const result = array2.filter<SubClass>((value: SuperClass, index: number, obj: Array<SuperClass>) => value instanceof SubClass);
396    result.forEach((element: SubClass) => print(element.num)); // 5, 8, 44
397}
398
399function reduce() {
400    print("Start Test reduce")
401    const array = new SendableArray<number>(1, 2, 3, 4);
402    print(array.reduce((acc: number, currValue: number) => acc + currValue)); // 10
403
404    print(array.reduce((acc: number, currValue: number) => acc + currValue, 10)); // 20
405
406    print(array.reduce<string>((acc: number, currValue: number) => "" + acc + " " + currValue, "10")); // 10, 1, 2, 3, 4
407}
408
409function f0() {
410    const o1 = {
411    };
412
413    return o1;
414}
415
416const v2 = f0();
417class C3 {
418    constructor(a5,a6) {
419        const v9 = new SendableArray();
420        v9.splice(0,0, v2);
421    }
422}
423
424function splice() {
425    print("Start Test splice")
426    const array = new SendableArray<string>('Jan', 'March', 'April', 'June');
427    array.splice(1, 0, 'Feb', 'Oct');
428    print(array); // "Jan", "Feb", "Oct", "March", "April", "June"
429    const removeArray = array.splice(4, 2, 'May');
430    print(array); // "Jan", "Feb", "Oct", "March", "May"
431    print(removeArray); // "April", "June"
432    const removeArray1 = array.splice(2, 3);
433    print(array); // "Jan", "Feb"
434    print(removeArray1); // "Oct", "March", "May"
435
436    const array2 = new SendableArray<SubClass>(
437        new SubClass(5),
438        new SubClass(32),
439        new SubClass(8),
440        new SubClass(130),
441        new SubClass(44),
442    );
443
444    try {
445        array2.splice(0, 0, new SuperUnSharedClass(48));
446        print("Add one element by splice api.");
447    } catch (err) {
448        print("Add one element by splice api failed. err: " + err + ", code: " + err.code);
449    }
450
451    try {
452        new C3();
453        print("Add one element by splice api.");
454    } catch (err) {
455        print("Add one element by splice api failed. err: " + err + ", code: " + err.code);
456    }
457}
458
459function staticCreate() {
460    print("Start Test staticCreate")
461    const array = SendableArray.create<number>(10, 5);
462    print(array);
463    try {
464        const array = SendableArray.create<number>(5);
465        print("Create with without initialValue success.");
466    } catch (err) {
467        print("Create with without initialValue failed. err: " + err + ", code: " + err.code);
468    }
469    try {
470        const array = SendableArray.create<number>(-1, 5);
471        print("Create with negative length success.");
472    } catch (err) {
473        print("Create with negative length failed. err: " + err + ", code: " + err.code);
474    }
475    try {
476        const array = SendableArray.create<number>(13107200, 1); // 13107200: 12.5MB
477        print("Create huge sendableArrayWith initialValue success.");
478    } catch (err) {
479        print("Create huge sendableArrayWith initialValue failed. err: " + err + ", code: " + err.code);
480    }
481    try {
482        const array = SendableArray.create<number>(0x100000000, 5);
483        print("Create with exceed max length success.");
484    } catch (err) {
485        print("Create with exceed max length failed. err: " + err + ", code: " + err.code);
486    }
487}
488
489function readonlyLength() {
490    print("Start Test readonlyLength")
491    const array = SendableArray.create<number>(10, 5);
492    print(array.length);
493    array.length = 0;
494    print(array.length);
495}
496
497function shrinkTo() {
498    print("Start Test shrinkTo")
499    const array = new SendableArray<number>(5, 5, 5, 5, 5, 5, 5, 5, 5, 5);
500    print(array.length);
501    array.shrinkTo(array.length);
502    print("Shrink to array.length: " + array);
503    array.shrinkTo(array.length + 1);
504    print("Shrink to array.length + 1: " + array);
505    try {
506        array.shrinkTo(-1);
507        print("Shrink to -1 success");
508    } catch (err) {
509        print("Shrink to -1 fail. err: " + err + ", code: " + err.code);
510    }
511    try {
512        array.shrinkTo(0x100000000);
513        print("Shrink to invalid 0x100000000 success");
514    } catch (err) {
515        print("Shrink to invalid 0x100000000 fail. err: " + err + ", code: " + err.code);
516    }    
517    array.shrinkTo(1);
518    print(array.length);
519    print(array);
520
521}
522
523function extendTo() {
524    print("Start Test growTo")
525    const array = SendableArray.create<number>(5, 5);
526    print(array.length);
527    array.extendTo(array.length, 0);
528    print("ExtendTo to array.length: " + array);
529    array.extendTo(array.length - 1, 0);
530    print("ExtendTo to array.length - 1: " + array);
531    array.extendTo(0, 0);
532    print("ExtendTo to 0: " + array);
533    try {
534        array.extendTo(-1, 0);
535        print("ExtendTo to -1 success.");
536    } catch (err) {
537        print("ExtendTo to -1 fail. err: " + err + ", code: " + err.code);
538    }
539    try {
540        array.extendTo(0x100000000, 0);
541        print("ExtendTo to invalid 0x100000000 success.");
542    } catch (err) {
543        print("ExtendTo to invalid 0x100000000 fail. err: " + err + ", code: " + err.code);
544    }
545    try {
546        array.extendTo(8);
547        print("ExtendTo to 8 without initValue success.");
548    } catch (err) {
549        print("ExtendTo to 8 without initValue fail. err: " + err + ", code: " + err.code);
550    }
551    array.extendTo(8, 11);
552    print(array.length);
553    print(array);
554}
555
556function indexAccess() {
557    print("Start Test indexAccess")
558    const array = new SendableArray<number>(1, 3, 5, 7);
559    print("element1: " + array[1]);
560    array[1] = 10
561    print("element1 assigned to 10: " + array[1]);
562    try {
563        array[10]
564        print("Index access read out of range success.");
565    } catch (err) {
566        print("Index access read out of range failed. err: " + err + ", code: " + err.code);
567    }
568    try {
569        array[100] = 10
570        print("Index access write out of range success.");
571    } catch (err) {
572        print("Index access write out of range failed. err: " + err + ", code: " + err.code);
573    }
574    try {
575        array.forEach((key: number, _: number, array: SendableArray) => {
576          array[key + array.length];
577        });
578    } catch (err) {
579        print("read element while iterate array fail. err: " + err + ", errCode: " + err.code);
580    }
581    try {
582        array.forEach((key: number, _: number, array: SendableArray) => {
583          array[key + array.length] = 100;
584        });
585    } catch (err) {
586        print("write element while iterate array fail. err: " + err + ", errCode: " + err.code);
587    }
588}
589
590function indexStringAccess() {
591    print("Start Test indexStringAccess")
592    const array = new SendableArray<number>(1, 3, 5, 7);
593    print("String index element1: " + array["" + 1]);
594    array["" + 1] = 10
595    print("String index element1 assigned to 10: " + array["" + 1]);
596    try {
597        array["" + 10]
598        print("String Index access read out of range success.");
599    } catch (err) {
600        print("String Index access read out of range failed. err: " + err + ", code: " + err.code);
601    }
602    try {
603        array["" + 100] = 10
604        print("String Index access write out of range success.");
605    } catch (err) {
606        print("String Index access write out of range failed. err: " + err + ", code: " + err.code);
607    }
608    try {
609        array.forEach((key: number, _: number, array: SendableArray) => {
610          array['' + key + array.length];
611        });
612    } catch (err) {
613        print("String index read element while iterate array fail. err: " + err + ", errCode: " + err.code);
614    }
615    try {
616        array.forEach((key: number, _: number, array: SendableArray) => {
617          array['' + key + array.length] = 100;
618        });
619    } catch (err) {
620        print("String index write element while iterate array fail. err: " + err + ", errCode: " + err.code);
621    }
622}
623
624function testForIC(index: number) {
625    const array = new SendableArray<number>(1, 3, 5, 7);
626    try {
627        const element = array[index < 80 ? 1 : 10];
628        if (index == 1) {
629            print("[IC] Index access read in range success. array: " + element);
630        }
631    } catch (err) {
632        if (index == 99) {
633            print("[IC] Index access read out of range failed. err: " + err + ", code: " + err.code);
634        }
635    }
636    try {
637        array[index < 80 ? 1 : 100] = 10
638        if (index == 1) {
639            print("[IC] Index access write in range success.");
640        }
641    } catch (err) {
642        if (index == 99) {
643            print("[IC] Index access write out of range failed. err: " + err + ", code: " + err.code);
644        }
645    }
646    try {
647        array.length = index < 80 ? 1 : 100;
648        if (index == 1) {
649            print("[IC] assign readonly length no error.");
650        }
651    } catch (err) {
652        if (index == 99) {
653            print("[IC] assign readonly length fail. err: " + err + ", code: " + err.code);
654        }
655    }
656}
657
658function testStringForIC(index: number) {
659    const array = new SendableArray<number>(1, 3, 5, 7);
660    try {
661        const element = array["" + index < 80 ? 1 : 10];
662        if (index == 1) {
663            print("[IC] String Index access read in range success. array: " + element);
664        }
665    } catch (err) {
666        if (index == 99) {
667            print("[IC] String Index access read out of range failed. err: " + err + ", code: " + err.code);
668        }
669    }
670    try {
671        array["" + (index < 80 ? 1 : 100)] = 10
672        if (index == 1) {
673            print("[IC] String Index access write in range success.");
674        }
675    } catch (err) {
676        if (index == 99) {
677            print("[IC] String Index access write out of range failed. err: " + err + ", code: " + err.code);
678        }
679    }
680}
681
682function frozenTest(array: SendableArray) {
683  try {
684    array.notExistProp = 1;
685  } catch (err) {
686    print('Add prop to array failed. err: ' + err);
687  }
688  try {
689    Object.defineProperty(array, 'defineNotExistProp', { value: 321, writable: false });
690  } catch (err) {
691    print('defineNotExistProp to array failed. err: ' + err);
692  }
693  try {
694    array.at = 1;
695  } catch (err) {
696    print('Update function [at] failed. err: ' + err);
697  }
698  try {
699    Object.defineProperty(array, 'at', { value: 321, writable: false });
700  } catch (err) {
701    print('Update function [at] by defineProperty failed. err: ' + err);
702  }
703  array.push(111);
704}
705
706function arrayFrozenTest() {
707    print("Start Test arrayFrozenTest")
708    let arr1 = new SendableArray<string>('ARK');
709    print("arrayFrozenTest [new] single string. arr: " + arr1);
710    frozenTest(arr1);
711    arr1 = new SendableArray<string>('A', 'R', 'K');
712    print("arrayFrozenTest [new]. arr: " + arr1);
713    frozenTest(arr1);
714    arr1 = SendableArray.from<string>(['A', 'R', 'K']);
715    print("arrayFrozenTest static [from]. arr: " + arr1);
716    frozenTest(arr1);
717    arr1 = SendableArray.create<string>(3, 'A');
718    print("arrayFrozenTest static [create]. arr: " + arr1);
719    frozenTest(arr1);
720}
721
722function sharedArrayFrozenTest() {
723    print("Start Test sharedArrayFrozenTest")
724    let arr1 = new SubSharedClass();
725    arr1.push("A");
726    arr1.push("R");
727    arr1.push("K");
728    print("sharedArrayFrozenTest [new]. arr: " + arr1);
729    frozenTest(arr1);
730}
731
732function increaseArray() {
733    print("Start Test extendSharedTest")
734    let sub = new SubSharedClass();
735    for (let idx: number = 0; idx < 1200; idx++) {
736        sub.push(idx + 10);
737    }
738    print("Push: " + sub);
739}
740
741function arrayFromSet(){
742  print('Start Test arrayFromSet');
743  const set = new Set(['foo', 'bar', 'baz', 'foo']);
744  const sharedSet = new SendableSet(['foo', 'bar', 'baz', 'foo']);
745  print('Create from normal set: ' + SendableArray.from(set));
746  print('Create from shared set: ' + SendableArray.from(set));
747}
748
749function arrayFromNormalMap() {
750    print("Start Test arrayFromNormalMap")
751    const map = new Map([
752        [1, 2],
753        [2, 4],
754        [4, 8],
755      ]);
756      Array.from(map);
757      // [[1, 2], [2, 4], [4, 8]]
758      
759      const mapper = new Map([
760        ["1", "a"],
761        ["2", "b"],
762      ]);
763      Array.from(mapper.values());
764      // ['a', 'b'];
765      
766      Array.from(mapper.keys());
767      // ['1', '2'];
768}
769
770function arrayFromSendableMap() {
771  print('Start test arrayFromSendableMap');
772  const map = new SendableMap([
773    [1, 2],
774    [2, 4],
775    [4, 8],
776  ]);
777  try {
778    print('create from sharedMap: ' + SendableArray.from(map));
779  } catch (err) {
780    print('create from sharedMap with non-sendable array failed. err: ' + err + ', code: ' + err.code);
781  }
782  // [[1, 2], [2, 4], [4, 8]]
783
784  const mapper = new SendableMap([SendableArray.from(['1', 'a']), SendableArray.from(['2', 'b'])]);
785  print('create from sharedMapper.values(): ' + SendableArray.from(mapper.values()));
786  // ['a', 'b'];
787
788  print('create from sharedMapper.values(): ' + SendableArray.from(mapper.keys()));
789  // ['1', '2'];
790}
791
792function arrayFromNotArray() {
793    print("Start test arrayFromNotArray")
794    function NotArray(len: number) {
795        print("NotArray called with length", len);
796    }
797
798    try {
799        print('Create array from notArray: ' + SendableArray.from.call(NotArray, new Set(['foo', 'bar', 'baz'])));
800    } catch (err) {
801        print("Create array from notArray failed. err: " + err + ", code: " + err.code);
802    }
803}
804
805function derivedSlice() {
806    print("Start Test derivedSlice")
807    let animals = new SubSharedClass();
808    animals.push('ant');
809    animals.push('bison');
810    animals.push('camel');
811    animals.push('duck');
812    animals.push('elephant');
813    print("instanceOf slice result: " + (animals.slice() instanceof SubSharedClass));
814}
815
816function derivedSort() {
817    print("Start Test derivedSort")
818    let months = new SubSharedClass();
819    months.push('March')
820    months.push('Jan')
821    months.push('Feb')
822    months.push('Dec')
823
824    const sortedMonth = months.sort();
825    print("instanceOf derived sort result: " + (sortedMonth instanceof SubSharedClass));
826}
827
828function derivedForEach() {
829  print('Start Test derivedForEach');
830  let array = new SubSharedClass();
831  array.push('March');
832  array.push('Jan');
833  array.push('Feb');
834  array.push('Dec');
835  array.forEach((element: string, index: number, array: SendableArray<string>) =>
836    print(`a[${index}] = ${element}, ${array instanceof SubSharedClass}`),
837  );
838}
839
840function derivedMap() {
841    print("Start derivedMap")
842    let array = new SubSharedClass();
843    array.push(1);
844    array.push(4);
845    array.push(9);
846    array.push(16);
847    print("instanceOf derived map result: " + (array.map<string>((x: number) => x + x + "") instanceof SubSharedClass));
848}
849
850function derivedFill() {
851    print("Start Test derivedFill")
852    let array = new SubSharedClass();
853    array.push(1);
854    array.push(2);
855    array.push(3);
856    array.push(4);
857    const filledArray = array.fill(0, 2, 4);
858    print(array); // [1, 2, 0, 0]
859    print("instanceOf derived fill result: " + (filledArray instanceof SubSharedClass));
860}
861
862function readOutOfRange() {
863    print("Start Test array read out of range")
864    const array = new SendableArray<number>(1, 3, 5, 7);
865    print("array[0]: " + array[0]);
866    try {
867        let value = array[9];
868        print("read out of range success " + value);
869    } catch (err) {
870        print("read out of range failed. err: " + err + ", code: " + err.code);
871    }
872
873    try {
874        let value = array['0'];
875        print("read out of range success " + value);
876    } catch (err) {
877        print("read out of range failed. err: " + err + ", code: " + err.code);
878    }
879
880    try {
881        let value = array[0.0];
882        print("read out of range success " + value);
883    } catch (err) {
884        print("read out of range failed. err: " + err + ", code: " + err.code);
885    }
886
887    try {
888        let value = array[1.5]
889        print("read out of range success " + value);
890    } catch (err) {
891        print("read out of range failed. err: " + err + ", code: " + err.code);
892    }
893
894    try {
895        let value = array[undefined]
896        print("read out of range success " + value);
897    } catch (err) {
898        print("read out of range failed. err: " + err + ", code: " + err.code);
899    }
900
901    try {
902        let value = array[null]
903        print("read out of range success " + value);
904    } catch (err) {
905        print("read out of range failed. err: " + err + ", code: " + err.code);
906    }
907
908    try {
909        let value = array[Symbol.toStringTag]
910        print("read out of range success " + value);
911    } catch (err) {
912        print("read out of range failed. err: " + err + ", code: " + err.code);
913    }
914
915    try {
916        let value = array[false]
917        print("read out of range success " + value);
918    } catch (err) {
919        print("read out of range failed. err: " + err + ", code: " + err.code);
920    }
921
922    try {
923        let value = array[true]
924        print("read out of range success " + value);
925    } catch (err) {
926        print("read out of range failed. err: " + err + ", code: " + err.code);
927    }
928}
929
930function forOf() {
931    print("Start Test array for of")
932    const array = new SendableArray<number>(1, 3, 5, 7);
933    for(const num of array){
934        print(num);
935    }
936}
937
938function sharedArrayConstructorTest(){
939    let from_arr  = [1,2,3];
940    let s_arr = new SendableArray<number>(...from_arr); // output [1,2,3]
941    print(("SendableArray ...from_arr: " + s_arr));
942    let s_arr1 = new SendableArray<number>(0, ...from_arr); // output [1,2,3]
943    print(("SendableArray ...from_arr1: " + s_arr1));
944    try {
945        print("Create from SendableArray with non-sendable array error: " + new SendableArray(from_arr));
946    } catch (err) {
947        print("Create from SendableArray with non-sendable array error failed. err: " + err + ", code: " + err.code);
948    }
949}
950
951function fromArrayConstructorTest(): void {
952    print("Start Test fromArrayConstructorTest")
953    const array1 = new SendableArray<string>('a', 'b', 'c');
954    const iterator = array1.values();
955    print(SendableArray.from<string>(iterator));
956}
957
958function DefinePropertyTest() {
959    print("Start Test DefinePropertyTest")
960    let array = new SendableArray<string>('ARK');
961    try {
962        Object.defineProperty(array, '0', {writable: true, configurable: true, enumerable: true, value: "321"});
963        print('defineProperty to array success');
964    } catch (err) {
965        print('defineProperty to array failed. err: ' + err);
966    }
967
968    try {
969        Object.defineProperty(array, '1200', {writable: true, configurable: true, enumerable: true, value: "321"});
970        print('defineProperty to array success');
971    } catch (err) {
972        print('defineProperty to array failed. err: ' + err);
973    }
974
975    try {
976        Object.defineProperty(array, 0, {writable: true, configurable: true, enumerable: true, value: "321"});
977        print('defineProperty to array success');
978    } catch (err) {
979        print('defineProperty to array failed. err: ' + err);
980    }
981
982    try {
983        Object.defineProperty(array, 1200, {writable: true, configurable: true, enumerable: true, value: "321"});
984        print('defineProperty to array success');
985    } catch (err) {
986        print('defineProperty to array failed. err: ' + err);
987    }
988
989    try {
990        Object.defineProperty(array, 2871622679, {writable: true, configurable: true, enumerable: true, value: "321"});
991        print('defineProperty to array success');
992    } catch (err) {
993        print('defineProperty to array failed. err: ' + err);
994    }
995    try {
996        Object.defineProperty(array, 0.0, {writable: true, configurable: true, enumerable: true, value: "321"});
997        print('defineProperty to array success ' + array[0.0]);
998    } catch (err) {
999        print("defineProperty to array failed. err: " + err + ", code: " + err.code);
1000    }
1001
1002    try {
1003        Object.defineProperty(array, 1.5, {writable: true, configurable: true, enumerable: true, value: "321"});
1004        print('defineProperty to array success ' + array[1.5]);
1005    } catch (err) {
1006        print("defineProperty to array failed. err: " + err + ", code: " + err.code);
1007    }
1008
1009    try {
1010        Object.defineProperty(array, undefined, {writable: true, configurable: true, enumerable: true, value: "321"});
1011        print("defineProperty to array success " + array[undefined]);
1012    } catch (err) {
1013        print("defineProperty to array failed. err: " + err + ", code: " + err.code);
1014    }
1015
1016    try {
1017        Object.defineProperty(array, null, {writable: true, configurable: true, enumerable: true, value: "321"});
1018        print("defineProperty to array success " + array[null]);
1019    } catch (err) {
1020        print("defineProperty to array failed. err: " + err + ", code: " + err.code);
1021    }
1022
1023    try {
1024        Object.defineProperty(array, Symbol.toStringTag, {writable: true, configurable: true, enumerable: true, value: "321"});
1025        print("defineProperty to array success " + array[Symbol.toStringTag]);
1026    } catch (err) {
1027        print("defineProperty to array failed. err: " + err + ", code: " + err.code);
1028    }
1029
1030    try {
1031        Object.defineProperty(array, true, {writable: true, configurable: true, enumerable: true, value: "321"});
1032        print("defineProperty to array success " + array[null]);
1033    } catch (err) {
1034        print("defineProperty to array failed. err: " + err + ", code: " + err.code);
1035    }
1036
1037    try {
1038        Object.defineProperty(array, false, {writable: true, configurable: true, enumerable: true, value: "321"});
1039        print("defineProperty to array success " + array[Symbol.toStringTag]);
1040    } catch (err) {
1041        print("defineProperty to array failed. err: " + err + ", code: " + err.code);
1042    }
1043}
1044
1045function isEven(num) {
1046    return num % 2 === 0;
1047}
1048
1049function SomeTest(): void {
1050    print("Start Test SomeTest")
1051    const numbers = new SendableArray<number>(1, 2, 3, 4, 5);
1052
1053    const hasEvenNumber = numbers.some(isEven); // 1: Whether there are even numbers in the array
1054    print(hasEvenNumber); // should be true
1055
1056    const hasNegativeNumber = numbers.some(num => num < 0); // 2:  Whether there are negative numbers in the array
1057    print(hasNegativeNumber); // should be false
1058}
1059
1060function EveryTest(): void {
1061    print("Start Test EveryTest")
1062    const numbers = new SendableArray<number>(1, 2, 3, 4, 5);
1063
1064    const allPositive = numbers.every((num) => num > 0); // Check that all elements in the array are greater than 0
1065    print(allPositive); // should be true
1066
1067    const allEven = numbers.every((num) => num % 2 === 0); // Check if all the elements in the array are even
1068    print(allEven); // should be false
1069}
1070
1071function isArrayTest() {
1072  // print true
1073  print(SendableArray.isArray(new SendableArray()));
1074  print(SendableArray.isArray(new SendableArray('a', 'b', 'c', 'd')));
1075  print(SendableArray.isArray(new SendableArray(3)));
1076  print(SendableArray.isArray(SendableArray.prototype));
1077
1078  // print false
1079  print(SendableArray.isArray([]));
1080  print(SendableArray.isArray([1]));
1081  print(SendableArray.isArray());
1082  print(SendableArray.isArray({}));
1083  print(SendableArray.isArray(null));
1084  print(SendableArray.isArray(undefined));
1085  print(SendableArray.isArray(17));
1086  print(SendableArray.isArray('SendableArray'));
1087  print(SendableArray.isArray(true));
1088  print(SendableArray.isArray(false));
1089  print(SendableArray.isArray(new SendableUint8Array(32)));
1090}
1091
1092function lastIndexOfTest() {
1093  let arr = SendableArray.from([1, 2, 3, 4, 2, 5]);
1094  print(arr.lastIndexOf(2));
1095
1096  print(arr.lastIndexOf(1));
1097  print(arr.lastIndexOf(5));
1098  print(arr.lastIndexOf(6));
1099
1100  let emptyArr = SendableArray.from([]);
1101  print(emptyArr.lastIndexOf(1));
1102
1103  let arrWithNaN = SendableArray.from([1, 2, NaN, 4, NaN]);
1104  print(arrWithNaN.lastIndexOf(NaN));
1105
1106  let arrWithUndefined = SendableArray.from([1, 2, undefined, 4]);
1107  print(arrWithUndefined.lastIndexOf(undefined));
1108}
1109
1110at()
1111
1112entries()
1113
1114keys()
1115
1116values()
1117
1118find();
1119
1120includes();
1121
1122index();
1123
1124fill();
1125
1126pop();
1127
1128randomUpdate();
1129
1130randomGet();
1131
1132randomAdd();
1133create();
1134from();
1135fromTemplate();
1136length();
1137push();
1138concat();
1139join()
1140shift()
1141unshift()
1142slice()
1143sort()
1144indexOf()
1145forEach()
1146map()
1147filter()
1148reduce()
1149splice()
1150staticCreate()
1151readonlyLength()
1152shrinkTo()
1153extendTo()
1154indexAccess()
1155indexStringAccess()
1156print("Start Test testForIC")
1157for (let index: number = 0; index < 100; index++) {
1158    testForIC(index)
1159}
1160
1161print("Start Test testStringForIC")
1162for (let index: number = 0; index < 100; index++) {
1163    testStringForIC(index)
1164}
1165
1166arrayFrozenTest()
1167sharedArrayFrozenTest()
1168arrayFromSet()
1169arrayFromNormalMap()
1170arrayFromSendableMap();
1171arrayFromNotArray();
1172
1173derivedSlice();
1174derivedSort();
1175derivedForEach();
1176derivedMap()
1177derivedFill()
1178readOutOfRange()
1179forOf();
1180sharedArrayConstructorTest()
1181fromArrayConstructorTest()
1182DefinePropertyTest()
1183
1184SomeTest()
1185EveryTest()
1186isArrayTest();
1187lastIndexOfTest();
1188