• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:typearray
18 * @tc.desc:test TypeArray
19 * @tc.type: FUNC
20 * @tc.require: issueI5NO8G
21 */
22
23const typedArrayConstructors = [
24    Float64Array, Float32Array, Int32Array, Int16Array, Int8Array, Uint32Array, Uint16Array, Uint8Array,
25    Uint8ClampedArray
26];
27
28typedArrayConstructors.forEach(function(ctor, i) {
29    if (testTypeArray1(ctor) && testTypeArray2(ctor) &&
30        testTypeArray3(ctor) && testTypeArray4(ctor) &&
31        testTypeArrayWithSize(ctor, 10) && testTypeArrayWithSize(ctor, 50) &&
32        testTypeArrayIC(ctor)) {
33        print(ctor.name + " test success !!!")
34    } else {
35        print(ctor.name + " test fail !!!")
36    }
37});
38
39[
40    BigInt64Array,
41    BigUint64Array
42].forEach(function(ctor, i) {
43    if (testTypeArray5(ctor) ) {
44        print(ctor.name + " test success !!!")
45    } else {
46        print(ctor.name + " test fail !!!")
47    }
48});
49
50function testTypeArray1(ctor) {
51    let result = []
52    let obj = new ctor(5);
53    result.push(obj[0] == 0);
54    result.push(obj[1] == 0);
55    obj[0] = "123";
56    result.push(obj[0] == 123)
57    result.push(obj["a"] == undefined);
58    obj["a"] = "abc";
59    result.push(obj["a"] == "abc");
60    obj["-0"] = 12;
61    result.push(obj["-0"] == undefined)
62    for (let i = 0; i < result.length; i++) {
63        if (!result[i]) {
64            return false;
65        }
66    }
67    return true;
68}
69
70function testTypeArray2(ctor) {
71    let result = []
72    let obj = new ctor(5);
73    for (let i = 0; i < 5; i++) {
74        obj[i] = i;
75    }
76    let child = Object.create(obj);
77    for (let i = 0; i < 5; i++) {
78        result.push(child[i] == i);
79    }
80    for (let i = 0; i < result.length; i++) {
81        if (!result[i]) {
82            return false;
83        }
84    }
85    return true;
86}
87
88function testTypeArray3(ctor) {
89    let result = []
90    let obj = new ctor(5);
91    let parent = {5: 5, "a": "a"};
92    obj.__proto__ = parent;
93    result.push(obj[4] == 0);
94    result.push(obj[5] == undefined);
95    result.push(obj["a"] == "a");
96    for (let i = 0; i < result.length; i++) {
97        if (!result[i]) {
98            return false;
99        }
100    }
101    return true;
102}
103
104function testTypeArray4(ctor) {
105    let a1 = new Array(1024);
106    let a2 = new ctor(1024);
107    a2.set(a1);
108    for (let i = 0; i < a2.length; i++) {
109        if (a2[i]) {
110            return false;
111        }
112    }
113    return true;
114}
115
116function testTypeArray5(ctor) {
117    let result = []
118    let a1 = new ctor(10);
119    a1.set([21n, 2n, 3n], "2");
120    result.push(a1[2] == 21n);
121    result.push(a1[3] == 2n);
122    result.push(a1[4] == 3n);
123    for (let i = 0; i < result.length; i++) {
124        if (!result[i]) {
125            return false;
126        }
127    }
128    return true;
129}
130
131function testTypeArrayWithSize(ctor, size) {
132    let result = []
133    let test = new Array(size);
134    for (var i = 0; i < 10; i++) {
135        test[i] = i;
136    }
137    let obj = new ctor(test);
138    for (var i = 0; i < 10; i++) {
139        result.push(obj[i] == i);
140    }
141    for (let i = 0; i < result.length; i++) {
142        if (!result[i]) {
143            return false;
144        }
145    }
146    return true;
147}
148
149function testTypeArrayIC(ctor) {
150    let result = []
151    let obj = new ctor(100);
152    for (var i = 0; i < 100; i++) {
153        obj[i] = i;
154    }
155    for (var i = 0; i < 100; i++) {
156        result.push(obj[i] == i);
157    }
158    for (var i = 0; i < 100; i++) {
159        result.push(obj.at(i) == i);
160    }
161    for (let i = 0; i < result.length; i++) {
162        if (!result[i]) {
163            return false;
164        }
165    }
166    return true;
167}
168
169let a1 = new ArrayBuffer(1024*1024*8);
170let a2 = new Uint8Array(a1);
171let a3 = Uint8Array.from(a2);
172print(a3.length);
173
174const a4 = new Uint32Array(1024);
175const obj1 = {
176    "c": a4,
177    __proto__: a4
178}
179obj1[235] = 1024;
180print(obj1[235]);
181
182try {
183    const a5 = new Uint8ClampedArray(new ArrayBuffer(1283053413), 9007199254740991);
184    a5.copyWithin(-13602);
185} catch(e) {
186    print("test successful !!!");
187}
188
189try {
190    const a6 = new BigInt64Array(10);
191    Int16Array.apply(null, a6);
192} catch(e) {
193    print("test successful !!!");
194}
195
196const a7 = new BigInt64Array(4);
197function foo() {}
198const f = new foo();
199const protoOf = f.isPrototypeOf;
200print(protoOf.apply(protoOf, a7));
201const uint8 = new Uint8Array([1, 2, 3]);
202const reversedUint8 = uint8.toReversed();
203print(reversedUint8); // Uint8Array [3, 2, 1]
204print(uint8); // Uint8Array [1, 2, 3]
205
206try {
207    const a8 = new Int8Array(new ArrayBuffer(0x40004141, {"maxByteLength": 0x40004141}));
208    const a9 = new Float64Array(a8);
209} catch (e) {
210    print("test successful !!!");
211}
212
213try {
214    const a10 = [1, 2];
215    const a11 = new Uint8Array(a10);
216    const a12 = new Uint32Array(a11);
217    a12.set(a10, 0x1ffffffff);
218} catch (e) {
219    print("test successful !!!");
220}
221
222try {
223    const v17 = new Int16Array(5);
224    const v20 = new Int16Array(5);
225    v17.set(v20, 4294967295);
226} catch (error) {
227    print(error)
228}
229
230try {
231    const v17 = new Int16Array(5);
232    const v20 = new Int16Array(5);
233    v17.set(v20, 4294967296);
234} catch (error) {
235    print(error)
236}
237
238try {
239    new BigUint64Array(new ArrayBuffer(256), 256, 0x1fffffff)
240} catch (error) {
241    print(error)
242}
243
244let arr12 = new Uint8Array(256).fill(255);
245print(arr12[0] == 255);
246print(arr12[123] == 255);
247print(arr12[255] == 255);
248
249try {
250    new Uint8Array(2 ** 32 - 1);
251} catch (error) {
252    print(error.name);
253}
254
255const v21 = new SharedArrayBuffer(32);
256const v22 = new BigInt64Array(v21);
257Atomics.or(v22, Int16Array, false);
258print(v22);
259print(Atomics.wait(v22, false, true));
260
261var arr13 = { [0]: 1, [1]: 20, [2]: 300, [3]: 4000, length: 4};
262var proxy = new Proxy(arr13, {
263    get: function(target, name) {
264        if (name == Symbol.iterator) {
265            return undefined;
266        }
267        return target[name];
268    }
269})
270var arr14 = new Uint8Array(proxy);
271print(arr13.length == arr14.length)
272
273
274const v2 = ("4294967297")["replaceAll"]();
275const v4 = new Float32Array();
276const t11 = v4.__proto__;
277t11[v2] = v2;
278print(t11[v2]);
279
280const v3 = String.fromCharCode(564654156456456465465)
281const v5 = new Int16Array(true);
282print(v5["join"](v3));
283
284// Test case for filter()
285let arr;
286var calls = 0;
287function TestTypedArrayFilter(e) {
288    arr = new e([-5, 10, 20, 30, 40, 50, 60.6]);
289}
290
291function TestTypedArrayFilterFunc(element, index, array) {
292    return element >= 10;
293}
294
295TestTypedArrayFilter(Int8Array);
296print(arr.filter(TestTypedArrayFilterFunc));
297print(calls);
298
299Object.defineProperty(Int8Array.prototype, "constructor", {
300    get: function () {
301        calls++;
302    }
303});
304
305TestTypedArrayFilter(Int8Array);
306print(arr.filter(TestTypedArrayFilterFunc));
307print(calls);
308
309// test case for some()
310[
311    Float64Array,
312    Float32Array,
313    Int32Array,
314    Int16Array,
315    Int8Array,
316    Uint32Array,
317    Uint16Array,
318    Uint8Array,
319    Uint8ClampedArray
320].forEach(function (ctor, i) {
321if (testTypeArraySome(ctor)) {
322        print(ctor.name + " test success !!!")
323    } else {
324        print(ctor.name + " test fail !!!")
325    }
326});
327
328function testTypeArraySome(ctor) {
329    let obj = new ctor([-1, 0, 2, 5, 8]);
330    return obj.some((element, index, array) => element > 5);
331}
332
333// Test case for every()
334let arr1_every = new Int16Array([1, 2, 3, 4, 5, 6, 7, 8, 9]);
335function testEvery_true(ele) {
336    return ele > 0;
337}
338function testEvery_false(ele) {
339    return ele > 10;
340}
341print(arr1_every.every(testEvery_true));
342print(arr1_every.every(testEvery_false));
343
344let arr2_every = new Int16Array();
345print(arr2_every.every(testEvery_false));
346let arr3_every = [1, 2, 3, 4, 5, 6, 7, 8, 9];
347print(arr3_every.every(testEvery_true));
348
349// Test case for reduce()
350let arr1_reduce = new Int16Array([1, 2, 3, 4, 5, 6, 7, 8, 9]);
351function test_Reduce(a, b){
352    return a+b;
353}
354print(arr1_reduce.reduce(test_Reduce));
355print(arr1_reduce.reduce(test_Reduce, 10));
356let arr2_reduce = [1, 2, 3, 4, 5, 6, 7, 8, 9];
357print(arr2_reduce.reduce(test_Reduce));
358
359// Test case for reduceRight()
360let arr1_reduceRight = new Int16Array([1, 2, 3, 4, 5, 6, 7, 8, 9]);
361function test_ReduceRight(a, b){
362    return a+b;
363}
364print(arr1_reduceRight.reduceRight(test_ReduceRight));
365print(arr1_reduceRight.reduceRight(test_ReduceRight, 10));
366let arr2_reduceRight = [1, 2, 3, 4, 5, 6, 7, 8, 9];
367print(arr2_reduceRight.reduceRight(test_ReduceRight));
368
369// Test case for copyWithin()
370let arr1_copyWithin = new Int16Array([1, 2, 3, 4, 5, 6, 7, 8, 9]);
371print(arr1_copyWithin.copyWithin(-10, 1, 100));
372let arr2_copyWithin = new Int16Array([1, 2, 3, 4, 5, 6, 7, 8, 9]);
373print(arr2_copyWithin.copyWithin(-3, -100, -1));
374let arr3_copyWithin = new Int16Array([1, 2, 3, 4, 5, 6, 7, 8, 9]);
375print(arr3_copyWithin.copyWithin(4, 1, 10));
376let arr4_copyWithin = new Int16Array([1, 2, 3, 4, 5, 6, 7, 8, 9]);
377print(arr4_copyWithin.copyWithin(4, -2));
378let arr5_copyWithin = new Int16Array([1, 2, 3, 4, 5, 6, 7, 8, 9]);
379print(arr5_copyWithin.copyWithin(4));
380let arr6_copyWithin = [1, 2, 3, 4, 5, 6, 7, 8, 9];
381print(arr6_copyWithin.copyWithin(1));
382
383// Test case for findIndex()
384[
385    Float64Array,
386    Float32Array,
387    Int32Array,
388    Int16Array,
389    Int8Array,
390    Uint32Array,
391    Uint16Array,
392    Uint8Array,
393    Uint8ClampedArray
394].forEach(function(ctor, i) {
395    if (testTypeArrayFindIndex(ctor)) {
396        print(ctor.name + " test success !!!")
397    } else {
398        print(ctor.name + " test fail !!!")
399    }
400});
401
402function testFindIndex(element, Last, array) {
403    return element >= 60;
404}
405
406function testTypeArrayFindIndex(ctor) {
407    let obj = new ctor([5, 10, 20, 30, 40, 50, 60])
408    let result = obj.findIndex(testFindIndex);
409    return result != -1;
410}
411
412// Test case for includes()
413let arr1_includes = new Int16Array([1, 2, 3, 4, 5, 6, 7, 8, 9]);
414print(arr1_includes.includes(5, -100));
415print(arr1_includes.includes(55,-1));
416let arr2_includes = [1, 2, 3, 4, 5, 6, 7, 8, 9];
417print(arr2_includes.includes(5));
418
419// Test case for find()
420let arr1_find = new Int16Array([1, 2, 3, 4, 5, 6, 7, 8, 9]);
421function testFind_true(ele) {
422    return ele === 5;
423}
424function testFind_false(ele) {
425    return ele > 10;
426}
427print(arr1_find.find(testFind_true));
428print(arr1_find.find(testFind_false));
429
430let arr2_find = new Int16Array();
431print(arr2_find.find(testFind_false));
432let arr3_find = [1, 2, 3, 4, 5, 6, 7, 8, 9];
433print(arr3_find.find(testFind_true));
434
435// Test case for indexOf()
436[
437    Float64Array,
438    Float32Array,
439    Int32Array,
440    Int16Array,
441    Int8Array,
442    Uint32Array,
443    Uint16Array,
444    Uint8Array,
445    Uint8ClampedArray
446].forEach(function(ctor, i) {
447    if (testTypeArrayIndexOf(ctor)) {
448        print(ctor.name + " test success !!!")
449    } else {
450        print(ctor.name + " test fail !!!")
451    }
452});
453
454function testTypeArrayIndexOf(ctor) {
455    let obj = new ctor([5, 10, 20, 30, 40, 50, 60])
456    let result = obj.indexOf(60);
457    return result != -1;
458}
459
460// Test case for lastIndexOf()
461[
462    Float64Array,
463    Float32Array,
464    Int32Array,
465    Int16Array,
466    Int8Array,
467    Uint32Array,
468    Uint16Array,
469    Uint8Array,
470    Uint8ClampedArray
471].forEach(function(ctor, i) {
472    if (testTypeArrayLastIndexOf(ctor)) {
473        print(ctor.name + " test success !!!")
474    } else {
475        print(ctor.name + " test fail !!!")
476    }
477});
478
479function testTypeArrayLastIndexOf(ctor) {
480    let obj = new ctor([5, 10, 20, 30, 40, 50, 60]);
481    let result = obj.lastIndexOf(5)
482    return result != -1;
483}
484
485[
486    BigInt64Array,
487    BigUint64Array
488].forEach(function(ctor, i) {
489    if (testTypeArrayLastIndexOf1(ctor) ) {
490        print(ctor.name + " test success !!!")
491    } else {
492        print(ctor.name + " test fail !!!")
493    }
494});
495
496function testTypeArrayLastIndexOf1(ctor) {
497    let obj = new ctor([5n, 10n, 20n, 30n, 40n, 50n, 60n]);
498    let result = obj.lastIndexOf(5n)
499    return result != -1;
500}
501
502let lastIndexOfFailed = new Int16Array(5, 10, 20, 30, 40, 50, 60)
503let lastIndexOfFailedResult = lastIndexOfFailed.lastIndexOf('ABC')
504print(lastIndexOfFailedResult);
505
506// Test case for reverse()
507[
508    Float64Array,
509    Float32Array,
510    Int32Array,
511    Int16Array,
512    Int8Array,
513    Uint32Array,
514    Uint16Array,
515    Uint8Array,
516    Uint8ClampedArray
517].forEach(function(ctor, i) {
518    if (testTypeArrayReverse1(ctor)) {
519        print(ctor.name + " test success !!!")
520    } else {
521        print(ctor.name + " test fail !!!")
522    }
523});
524
525function testTypeArrayReverse1(ctor) {
526    let arr1 = new ctor([1, 2, 3, 4, 5]);
527    arr1.reverse();
528    let arr2 = new ctor([5, 4, 3, 2, 1]);
529    if (arr1.length !== arr2.length) return false;
530    for (let i = 0; i < arr1.length; i++) {
531        if (arr1[i] !== arr2[i]) return false;
532    }
533    return true;
534}
535
536[
537    BigInt64Array,
538    BigUint64Array
539].forEach(function(ctor, i) {
540    if (testTypeArrayReverse2(ctor) ) {
541        print(ctor.name + " test success !!!")
542    } else {
543        print(ctor.name + " test fail !!!")
544    }
545});
546
547function testTypeArrayReverse2(ctor) {
548    let arr1 = new ctor([1n, 2n, 3n, 4n, 5n]);
549    arr1.reverse();
550    let arr2 = new ctor([5n, 4n, 3n, 2n, 1n]);
551    if (arr1.length !== arr2.length) return false;
552    for (let i = 0; i < arr1.length; i++) {
553        if (arr1[i] !== arr2[i]) return false;
554    }
555    return true;
556}
557
558function fun1(accumulator, currentvalue) {
559    print(accumulator, currentvalue);
560    return accumulator + currentvalue;
561}
562let arr1 = new Uint32Array();
563let arr2 = new Uint32Array(3);
564for (let i = 0; i < 3; i++) {
565    arr2[i] = i + 1;
566}
567let res1 = arr1.reduceRight(fun1, 1, 1);
568print(res1);
569let res2 = arr2.reduceRight(fun1, 1, 1);
570print(res2);
571let res3 = arr1.reduceRight(fun1, 1);
572print(res3);
573let res4 = arr2.reduceRight(fun1, 1);
574print(res4);
575try {
576    let res5 = arr1.reduceRight(fun1);
577    print(res5);
578} catch (e) {
579    print(e.name);
580}
581let res6 = arr2.reduceRight(fun1);
582print(res6);
583let res7 = arr1.reduceRight(fun1, undefined);
584print(res7);
585let res8 = arr2.reduceRight(fun1, undefined);
586print(res8);
587let res9 = arr1.reduceRight(fun1, null);
588print(res9);
589let res10 = arr2.reduceRight(fun1, null);
590print(res10);
591
592for (let i = 0; i < 3; i++) {
593  arr2[i] = i + 1;
594}
595res1 = arr1.reduce(fun1, 1, 1);
596print(res1);
597res2 = arr2.reduce(fun1, 1, 1);
598print(res2);
599res3 = arr1.reduce(fun1, 1);
600print(res3);
601res4 = arr2.reduce(fun1, 1);
602print(res4);
603try {
604  let res5 = arr1.reduce(fun1);
605  print(res5);
606} catch (e) {
607  print(e.name);
608}
609res6 = arr2.reduce(fun1);
610print(res6);
611res7 = arr1.reduce(fun1, undefined);
612print(res7);
613res8 = arr2.reduce(fun1, undefined);
614print(res8);
615res9 = arr1.reduce(fun1, null);
616print(res9);
617res10 = arr2.reduce(fun1, null);
618print(res10);
619
620// Test case for findLastIndex()
621[
622    Float64Array,
623    Float32Array,
624    Int32Array,
625    Int16Array,
626    Int8Array,
627    Uint32Array,
628    Uint16Array,
629    Uint8Array,
630    Uint8ClampedArray
631].forEach(function(ctor, i) {
632    if (testTypeArrayfindLastIndex(ctor)) {
633        print(ctor.name + " test success !!!")
634    } else {
635        print(ctor.name + " test fail !!!")
636    }
637});
638
639function func(element, Last, array) {
640    return element >= 0;
641}
642
643function testTypeArrayfindLastIndex(ctor) {
644    let obj = new ctor([5, 10, 20, 30, 40, 50, 60])
645    let result = obj.findLastIndex(func);
646    return result != -1;
647}
648
649[
650    BigInt64Array,
651    BigUint64Array
652].forEach(function(ctor, i) {
653    if (testTypeArrayfindLastIndex1(ctor) ) {
654        print(ctor.name + " test success !!!")
655    } else {
656        print(ctor.name + " test fail !!!")
657    }
658});
659
660function testTypeArrayfindLastIndex1(ctor) {
661    let obj = new ctor([5n, 10n, 20n, 30n, 40n, 50n, 60n])
662    let result = obj.findLastIndex(func);
663    return result != -1;
664}
665
666// Test case for of()
667[
668    Float64Array,
669    Float32Array,
670    Int32Array,
671    Int16Array,
672    Int8Array,
673    Uint32Array,
674    Uint16Array,
675    Uint8Array,
676    Uint8ClampedArray
677].forEach(function(ctor, i) {
678    if (testTypeArrayOf1(ctor) && testTypeArrayOf2(ctor)) {
679        print(ctor.name + " test success !!!")
680    } else {
681        print(ctor.name + " test fail !!!")
682    }
683});
684
685function testTypeArrayOf1(ctor) {
686    let arr1 = ctor.of("1", 2, undefined);
687    let arr2 = new ctor([1, 2, undefined])
688    return typedArraysEqual(arr1, arr2);
689}
690
691function testTypeArrayOf2(ctor) {
692    var obj1 = {
693        valueOf() {
694          return 41;
695        }
696      };
697      var obj2 = {
698        valueOf() {
699          return 42;
700        }
701      };
702    let arr1 = ctor.of(obj1, obj2, obj1);
703    let arr2 = new ctor([41, 42, 41])
704    return typedArraysEqual(arr1, arr2);
705}
706
707[
708    Float64Array,
709    Float32Array,
710    Int32Array,
711    Int16Array,
712    Int8Array,
713    Uint32Array,
714    Uint16Array,
715    Uint8Array,
716    Uint8ClampedArray
717].forEach(function(ctor, i) {
718    if (testTypeArrayOf3(ctor)) {
719        print(ctor.name + " test success !!!")
720    } else {
721        print(ctor.name + " test fail !!!")
722    }
723});
724
725function testTypeArrayOf3(ctor) {
726    try {
727        const arr1 = new ctor();
728        arr1.proto = ctor;
729        Uint8Array.of("m");
730    } catch (e) {
731        return false;
732    }
733    return true;
734}
735
736function typedArraysEqual(typedArr1, typedArr2) {
737    if (typedArr1.length !== typedArr2.length) return false;
738    for (let i = 0; i < typedArr1.length; i++) {
739      if (typedArr1[i] !== typedArr2[i]) return false;
740    }
741    return true;
742}
743
744// typedArray.map()
745[
746    Float64Array,
747    Float32Array,
748    Int32Array,
749    Int16Array,
750    Int8Array,
751    Uint32Array,
752    Uint16Array,
753    Uint8Array,
754    Uint8ClampedArray
755].forEach(function(ctor, i) {
756    if (testTypeArray1(ctor)) {
757        print(ctor.name + " test success !!!")
758    } else {
759        print(ctor.name + " test fail !!!")
760    }
761});
762
763function testTypeArray1(ctor) {
764    let obj = new ctor([2]);
765    let result = obj.map(function (num) {
766        return num * 2;
767    });
768    return result[0] == 4;
769}
770
771// test case for toReversed()
772[
773    Float64Array,
774    Float32Array,
775    Int32Array,
776    Int16Array,
777    Int8Array,
778    Uint32Array,
779    Uint16Array,
780    Uint8Array,
781    Uint8ClampedArray
782].forEach(function(ctor, i) {
783    if (testTypeArrayToReversed1(ctor)) {
784        print(ctor.name + " test success !!!")
785    } else {
786        print(ctor.name + " test fail !!!")
787    }
788});
789
790function testTypeArrayToReversed1(ctor) {
791    let arr1 = new ctor([1, 2, 3, 4, 5]);
792    let arr2 = arr1.toReversed();
793    let arr3 = new ctor([5, 4, 3, 2, 1]);
794    if (arr2.length !== arr3.length) return false;
795    for (let i = 0; i < arr2.length; i++) {
796        if (arr2[i] !== arr3[i]) return false;
797    }
798    return true;
799}
800
801[
802    BigInt64Array,
803    BigUint64Array
804].forEach(function(ctor, i) {
805    if (testTypeArrayToReversed2(ctor) ) {
806        print(ctor.name + " test success !!!")
807    } else {
808        print(ctor.name + " test fail !!!")
809    }
810});
811
812function testTypeArrayToReversed2(ctor) {
813    let arr1 = new ctor([1n, 2n, 3n, 4n, 5n]);
814    let arr2 = arr1.toReversed();
815    let arr3 = new ctor([5n, 4n, 3n, 2n, 1n]);
816    if (arr2.length !== arr3.length) return false;
817    for (let i = 0; i < arr2.length; i++) {
818        if (arr2[i] !== arr3[i]) return false;
819    }
820    return true;
821}
822
823var arr_every = new Uint8Array([11, 22, 33, 44]);
824ArkTools.arrayBufferDetach(arr_every.buffer);
825try {
826    arr_every.every(() => true)
827} catch (e) {
828    print(e instanceof TypeError);
829}
830
831var arr_forEach = new Uint8Array([11, 22, 33, 44]);
832ArkTools.arrayBufferDetach(arr_forEach.buffer);
833try {
834    arr_forEach.forEach(() => true)
835} catch (e) {
836    print(e instanceof TypeError);
837}
838
839var typedArrayConstructorsSort = [
840    Uint8Array,
841    Int8Array,
842    Uint16Array,
843    Int16Array,
844    Uint32Array,
845    Int32Array,
846    Uint8ClampedArray,
847    Float32Array,
848    Float64Array
849];
850
851for (var constructor of typedArrayConstructorsSort) {
852    // For arrays of floats, certain handling of +-0/NaN
853    var b = new constructor([1, +0, -0, NaN, -0, NaN, +0, 3, 2])
854    b.sort();
855    print(prettyPrinted(b[0]), prettyPrinted(b[1]), prettyPrinted(b[2]),
856          prettyPrinted(b[3]), prettyPrinted(b[4]), prettyPrinted(b[5]),
857          prettyPrinted(b[6]), prettyPrinted(b[7]), prettyPrinted(b[8]))
858}
859
860function prettyPrinted(value) {
861    let visited = new Set();
862    function prettyPrint(value) {
863        try {
864            switch (typeof value) {
865                case "string":
866                    return JSONStringify(value);
867                case "bigint":
868                    return String(value) + "n";
869                case "number":
870                    if (value === 0 && (1 / value) < 0) return "-0";
871                // FALLTHROUGH.
872                case "boolean":
873                case "undefined":
874                case "function":
875                case "symbol":
876                    return String(value);
877                case "object":
878                    if (value === null) return "null";
879                    // Guard against re-visiting.
880                    if (visited.has(value)) return "<...>";
881                    visited.add(value);
882                    var objectClass = classOf(value);
883                    switch (objectClass) {
884                        case "Number":
885                        case "BigInt":
886                        case "String":
887                        case "Boolean":
888                        case "Date":
889                            return objectClass + "(" + prettyPrint(ValueOf(value)) + ")";
890                        case "RegExp":
891                            return RegExpPrototypeToString.call(value);
892                        case "Array":
893                            var mapped = ArrayPrototypeMap.call(
894                                value, (v, i, array) => {
895                                    if (v === undefined && !(i in array)) return "";
896                                    return prettyPrint(v, visited);
897                                });
898                            var joined = ArrayPrototypeJoin.call(mapped, ",");
899                            return "[" + joined + "]";
900                        case "Int8Array":
901                        case "Uint8Array":
902                        case "Uint8ClampedArray":
903                        case "Int16Array":
904                        case "Uint16Array":
905                        case "Int32Array":
906                        case "Uint32Array":
907                        case "Float32Array":
908                        case "Float64Array":
909                        case "BigInt64Array":
910                        case "BigUint64Array":
911                            var joined = ArrayPrototypeJoin.call(value, ",");
912                            return objectClass + "([" + joined + "])";
913                        case "Object":
914                            break;
915                        default:
916                            return objectClass + "(" + String(value) + ")";
917                    }
918                    // classOf() returned "Object".
919                    var name = value.constructor?.name ?? "Object";
920                    var pretty_properties = [];
921                    for (let [k, v] of Object.entries(value)) {
922                        ArrayPrototypePush.call(
923                            pretty_properties, `${k}:${prettyPrint(v, visited)}`);
924                    }
925                    var joined = ArrayPrototypeJoin.call(pretty_properties, ",");
926                    return `${name}({${joined}})`;
927                default:
928                    return "-- unknown value --";
929            }
930        } catch (e) {
931            // Guard against general exceptions (especially stack overflows).
932            return "<error>"
933        }
934    }
935    return prettyPrint(value);
936}
937
938[
939    Float64Array,
940    Float32Array,
941    Int32Array,
942    Int16Array,
943    Int8Array,
944    Uint32Array,
945    Uint16Array,
946    Uint8Array,
947    Uint8ClampedArray
948].forEach(function(ctor, i) {
949    class C extends ctor{
950
951    }
952    C.of();
953    print("Class extends "+ ctor.name + " test success!")
954});
955
956[
957    Float64Array,
958    Float32Array,
959    Int32Array,
960    Int16Array,
961    Int8Array,
962    Uint32Array,
963    Uint16Array,
964    Uint8Array,
965    Uint8ClampedArray
966].forEach(function (ctor, i) {
967    try {
968        let obj = {
969            __proto__: [1, 2, 3, 4],
970            length: 1n,
971        }
972        new ctor(obj);
973    } catch (e) {
974        print("Test New " + ctor.name + " with Bad_Obj Success!")
975    }
976});
977
978try {
979    new Uint8Array(5).map(function() {
980		ArkTools.ArrayBufferDetach(this.buffer);
981    })
982} catch(e) {
983    print(e instanceof TypeError);
984}
985
986var array_reverse = new Uint8Array([11, 22, 33, 44]);
987ArkTools.arrayBufferDetach(array_reverse.buffer);
988try {
989    array_reverse.reverse();
990} catch (e) {
991    print(e instanceof TypeError);
992}
993
994var arr_indexOf = new Uint8Array([11, 22, 33]);
995ArkTools.arrayBufferDetach(arr_indexOf.buffer);
996try {
997    arr_indexOf.indexOf(0);
998} catch(e) {
999    print(e instanceof TypeError);
1000}
1001
1002var arr_lastIndexOf = new Uint8Array([11, 22, 33]);
1003ArkTools.arrayBufferDetach(arr_lastIndexOf.buffer);
1004try {
1005    arr_lastIndexOf.lastIndexOf(0);
1006} catch(e) {
1007    print(e instanceof TypeError);
1008}
1009
1010var arr_map = new Uint8Array([11, 22, 33, 44]);
1011ArkTools.arrayBufferDetach(arr_map.buffer);
1012try {
1013    arr_map.map((v) => v)
1014} catch (e) {
1015    print(e instanceof TypeError)
1016}
1017
1018var arr_filter = new Uint8Array([11, 22]);
1019ArkTools.arrayBufferDetach(arr_filter.buffer);
1020try {
1021    arr_filter.filter(false)
1022} catch (e) {
1023    print(e instanceof TypeError)
1024}
1025
1026var arr_some = new Uint8Array([11, 22]);
1027ArkTools.arrayBufferDetach(arr_some.buffer);
1028try {
1029    arr_some.some(false)
1030} catch (e) {
1031    print(e instanceof TypeError)
1032}
1033
1034var arr_some1 = new Uint8Array([33, 44]);
1035Object.defineProperty(arr_some1, 'length', { value: 1 });
1036print(arr_some1.some(function(elt) { return elt == 44; }));
1037print(Array.prototype.some.call(arr_some1, function(elt) {
1038    return elt == 44;
1039}));
1040
1041var arr_fill = new Uint8Array([2, 2]);
1042Object.defineProperty(arr_fill, 'length', {value: 1});
1043arr_fill.fill(3);
1044Array.prototype.fill.call(arr_fill, 4);
1045print(4 == arr_fill[0]);
1046print(3 == arr_fill[1]);
1047ArkTools.arrayBufferDetach(arr_fill.buffer);
1048try {
1049    arr_fill.fill(0);
1050} catch (e) {
1051    print(e instanceof TypeError);
1052}
1053
1054var arr_fill1 = new Uint8ClampedArray([0, 0, 0]).fill(2.50000);
1055arr_fill1.forEach((b)=> {
1056    print(2 == b);
1057})
1058
1059function sum(a, b) { return a + b; }
1060var arr_shadow_length = new Uint8Array([11, 22]);
1061Object.defineProperty(arr_shadow_length, 'length', {value: 1});
1062print(Array.prototype.reduce.call(arr_shadow_length, sum, 0) == 11);
1063print(Array.prototype.reduceRight.call(arr_shadow_length, sum, 0) == 11);
1064print(Uint8Array.prototype.reduce.length == 1);
1065print(Uint8Array.prototype.reduceRight.length == 1);
1066
1067var arr_reduce = new Uint8Array([11, 22]);
1068ArkTools.arrayBufferDetach(arr_reduce.buffer);
1069try {
1070    arr_reduce.reduce(sum, 0);
1071} catch (e) {
1072    print(e instanceof TypeError);
1073}
1074
1075var arr_reduceRight = new Uint8Array([11, 22]);
1076ArkTools.arrayBufferDetach(arr_reduceRight.buffer);
1077try {
1078    arr_reduceRight.reduceRight(sum, 0);
1079} catch (e) {
1080    print(e instanceof TypeError);
1081}