• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-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
16type num_str_str = [number, string, string];
17type num_str_str_with_array = [num_str_str, number[]];
18
19class A {
20    foo(): int {
21        return 1;
22    }
23}
24
25class B extends A {
26    override foo(): int  {
27        return 2;
28    }
29}
30
31class TupleProvider<X> {
32    memb: [X, X];
33    constructor(value: [X, X]) { this.memb = value;}
34    public publish(): [X, X] { return this.memb; }
35}
36
37
38function main(): void {
39    const tup_1: [string, number] = ["a", 12];
40
41    assertEQ(tup_1[0], "a")
42    assertEQ(tup_1[1], 12)
43
44    const tup_2: num_str_str[] = [
45        [13, "A", "D"],
46        [14, "B", "E"],
47        [25, "C", "F"],
48    ];
49
50    const tup_3: num_str_str_with_array[] = [
51        [tup_2[0], [250]],
52        [tup_2[1], [250, 260]],
53        [tup_2[0], [300, 300, 300]],
54    ];
55
56    const sum = tup_3[0][1][0] + tup_3[1][1][0] + tup_3[1][1][1] + tup_3[2][1][0] + tup_3[2][1][1] + tup_3[2][1][2];
57    assertEQ(sum, 1660);
58
59    tup_3[0][0][0]++;
60    assertEQ(tup_3[0][0][0], 14)
61    tup_3[0][0][0] = 4;
62    assertEQ(tup_3[0][0][0], 4)
63
64    let int_update: [int] = [42];
65    assertEQ(int_update[0], 42)
66    assertEQ(int_update[0]++, 42)
67    assertEQ(int_update[0], 43)
68    assertEQ(++int_update[0], 44)
69    int_update[0]++;
70    assertEQ(int_update[0], 45)
71
72    let tup_8: [number, string][];
73    tup_8 = [[1, "E"], [2, "F"], [3, "G"]];
74    assertTrue(tup_8[0][0] == 1 && tup_8[0][1] == "E")
75    assertTrue(tup_8[1][0] == 2 && tup_8[1][1] == "F")
76    assertTrue(tup_8[2][0] == 3 && tup_8[2][1] == "G")
77
78    let tup_10: [number, int, string, boolean, Object] = [1, 2, "I", false, new Object()];
79    let var_float: float = tup_10[1];
80    let var_float_2: float = 2.0f;
81    assertEQ(var_float, var_float_2);
82
83    let tup_11: [int, number, string, boolean, Object] = [6, 7, "J", true, 789];
84    tup_11[0] += new Short(2 as short);
85
86    assertEQ(tup_11[0], 8);
87    assertEQ(tup_11[4], (789 as Object));
88
89    let tup_13: [number, number] = [8, 9];
90    let tup_14: [number, number] = [10, 11];
91
92    tup_13 = tup_14;
93    assertEQ(tup_13[0], 10)
94    assertEQ(tup_13[1], 11)
95    tup_13[0] = 12;
96    tup_13[1] = Double.valueOf(13.0);
97    assertEQ(tup_13[0], 12)
98    assertEQ(tup_13[1], 13)
99
100    let a_b_tup: [A, A, B] = [new A(), new B(), new B()];
101    assertEQ(a_b_tup[0].foo(), 1)
102    assertEQ(a_b_tup[1].foo(), 2)
103    assertEQ(a_b_tup[2].foo(), 2)
104
105    let number_tup: [Double, Double] = [42.0, 43.0];
106    let tn: TupleProvider<number> = new TupleProvider<number>(number_tup);
107    let result: [Number, Number] = tn.publish();
108    assertEQ(result[0], 42.0)
109}
110