• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024-2025 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
16function compMultiArray<T>(arrayMulti1: T[][], arrayMulti2: T[][]) {
17    let i : int = 0
18    for (let element1 of arrayMulti1) {
19        compArray<T>(element1, arrayMulti2[i])
20        i++
21    }
22}
23
24function compArray<T>(array1: T[], array2: T[]) {
25    let i : int = 0
26    for (let element1 of array1) {
27        assertEQ(element1, array2[i])
28        i++
29    }
30}
31
32class A {
33    t : int = 10
34    private constructor() {}
35    static createInstance(x : int) : A {
36        let a : A = new A();
37        a.t = x
38        return a
39    }
40}
41
42function main() {
43    let intArray : int[] = [77, 88, 99]
44
45    // Array variable declaration with multi spread
46    let expectedVarDecl : Int[] = [1, 2, 77, 88, 99, 4, 6, 77, 88, 99]
47    let arrayVarDecl : Int[] = [1, 2, ...intArray, 4, 6, ...intArray]
48    compArray<Int>(arrayVarDecl, expectedVarDecl)
49
50    // Array assignment expression with multi spread
51    let expectedAssignment : Int[] = [100, 200, 77, 88, 99, 400, 600, 77, 88, 99]
52    let arrayAssignment : Int[]
53    arrayAssignment = [100, 200, ...intArray, 400, 600, ...intArray]
54    compArray<Int>(arrayAssignment, expectedAssignment)
55
56    // Multi dimensional array
57    let expectedArray : Int[][] = [[7, 8], [10, 77, 88, 99]]
58    let arrayMulti : Int[][] = [[7, 8], [10, ...intArray]]
59    compMultiArray<Int>(arrayMulti, expectedArray)
60
61    // Union type array
62    type unionType = (int|string|null)
63    let unionArray : unionType[] = ["first", "second"]
64    let expectedUnion : unionType[] = [100, 200, "first", "second", 500, 600, "first", "second"]
65    let arrayUnion: unionType[] = [100, 200, ...unionArray, 500, 600, ...unionArray]
66    compArray<unionType>(arrayUnion, expectedUnion)
67
68    // Object array
69    let obj : Object = new Object();
70    let objectArray : Object[] = [obj, obj]
71    let expectedObject : Object[] = [obj, obj, obj]
72    let arrayObject : Object[] = [obj, ...objectArray]
73    compArray<Object>(arrayObject, expectedObject)
74
75    //tuple
76    type tupleType = [string, int, boolean]
77    let tuple : tupleType = ["first", 2, true]
78    let expectedTuple = ["first", 2, true, 100, 200, "first", 2, true]
79    let tuple2 = [...tuple, 100, 200, ...tuple]
80    assertEQ(tuple2.length, 8)
81    assertEQ(tuple2[0], "first")
82    assertEQ(tuple2[1], 2)
83    assertEQ(tuple2[2], true)
84    assertEQ(tuple2[5], "first")
85    assertEQ(tuple2[6], 2)
86    assertEQ(tuple2[7], true)
87
88    let a : A[] = [A.createInstance(10), A.createInstance(11)]
89    let b = [...a]
90    assertEQ(b.length, 2)
91    b[0].t = 10
92    b[1].t = 11
93}
94