1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/*
17 * Copyright (c) 2023 Huawei Device Co., Ltd.
18 * Licensed under the Apache License, Version 2.0 (the "License");
19 * you may not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 *     http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS,
26 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
29 */
30
31function TestSample()
32{
33    print("TestSample:");
34    let arr: number[] = new Array(1025).fill(0);
35    arr[1] = 1;
36    print(arr[1]);
37}
38
39function TestArrayWithElementsAndProperties()
40{
41    print("TestArrayWithElementsAndProperties:");
42    let arr: number[] = new Array(2048)
43    arr[1] = 2;
44    arr[3] = 4;
45    arr.x1 = 1;
46    arr.x2 = 2;
47    arr.x3 = 3;
48    arr.x4 = 4;
49    arr.x5 = 5;
50    arr.length = 2047;
51
52    arr.fill("000");
53    print(arr[1]);
54    print(arr[3]);
55    print(arr.x1);
56    print(arr.x2);
57    print(arr.x3);
58    print(arr.x4);
59    print(arr.x5);
60    print(arr.length);
61}
62
63function TestFullArrayWithElementsAndProperties()
64{
65    print("TestFullArrayWithElementsAndProperties:");
66    let arr: number[] = new Array(2048)
67    arr.x1 = 1;
68    arr.x2 = 2;
69    arr.x3 = 3;
70    arr.x4 = 4;
71    arr.x5 = 5;
72    for (let i: number = 0; i < 2048; i++) {
73        arr[i] = "apple"
74    }
75    arr.length = 2047;
76    arr.fill(0);
77    for (let i: number = 0; i < 5; i++) {
78        print(arr[i]);
79    }
80    for (let i: number = 2045; i < 2048; i++) {
81        print(arr[i]); 
82    }
83    print(arr.apple);
84    print(arr.x1);
85    print(arr.x2);
86    print(arr.x3);
87    print(arr.x4);
88    print(arr.x5);
89    print(arr.length);
90}
91
92function TestShouldNotOptimizeAsFastElements()
93{
94    print("TestShouldNotOptimizeAsFastElements:");
95    let arr: number[] = new Array(1025)
96    arr.x1 = 1;
97    arr.x2 = 2;
98    arr.x3 = 3;
99    arr.x4 = 4;
100    arr.x5 = 5;
101    for (let i: number = 0; i < 1025; i++) {
102        arr[i] = "apple"
103    }
104    arr.length = 0;
105    arr.fill(0);
106    for (let i: number = 0; i < 5; i++) {
107        print(arr[i]);
108    }
109    for (let i: number = 1020; i < 1025; i++) {
110        print(arr[i]); 
111    }
112    print(arr.apple);
113    print(arr.x1);
114    print(arr.x2);
115    print(arr.x3);
116    print(arr.x4);
117    print(arr.x5);
118    print(arr.length);
119}
120
121function TestStringArrayWithElementsAndProperties()
122{
123    print("TestStringArrayWithElementsAndProperties:");
124    let arr: string[] = new Array(1025)
125    arr[1] = "apple"
126    arr.apple = "b"
127
128    arr.fill("ark");
129    print(arr[0]);
130    print(arr[1]);
131    print(arr.apple);
132}
133
134function TestSpecialCase()
135{
136    print("TestSpecialCase:");
137    let arr: number[] = new Array(1025);
138    Object.defineProperty(arr, '0', {
139        value: 42,
140        writable: false,
141    });
142    arr.fill(1);
143    print(arr[0]);
144}
145
146TestSample();
147TestArrayWithElementsAndProperties();
148TestFullArrayWithElementsAndProperties();
149TestShouldNotOptimizeAsFastElements();
150TestStringArrayWithElementsAndProperties();
151try {
152    TestSpecialCase();
153} catch (e) {}