1/* 2 * Copyright (c) 2023-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 16type num_str_str = [number, string, string]; 17type num_str_str_with_spread = [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 assert(tup_1[0] == "a"); 42 assert(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_spread[] = [ 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] + tup_3[1][1] + tup_3[1][2] + tup_3[2][1] + tup_3[2][2] + tup_3[2][3]; 57 assert(sum == 1660); 58 59 // Runs correctly, but fails on verifier 60 // It sees the type of `tup_3[0][0]` as an `Object`, what it really is an Array type. 61 62 // tup_3[0][0][0]++; 63 // assert(tup_3[0][0][0] == 14); 64 // tup_3[0][0][0] = 4; 65 // assert(tup_3[0][0][0] == 4); 66 67 let int_update: [int] = [42]; 68 assert(int_update[0] == 42); 69 assert(int_update[0]++ == 42); 70 assert(int_update[0] == 43); 71 assert(++int_update[0] == 44); 72 int_update[0]++; 73 assert(int_update[0] == 45); 74 75 let spread_tup: [Number, ...Number[]] = [6 as double,7 as double,8 as double, 9 as double, 10 as double]; 76 let num_arr: Number[] = [1 as double,2 as double,3 as double,4 as double,5 as double, 6 as double]; 77 78 spread_tup = num_arr; 79 80 let test_arr: Number[] = [1 as double,2 as double,3 as double,4 as double,5 as double, 6 as double]; 81 for(let idx = 0; idx < spread_tup.length; idx++){ 82 assert(spread_tup[idx] == test_arr[idx]); 83 } 84 85 let tup_4: [number, number] = [11,2]; 86 let prim_num_arr: number[] = [3,4]; 87 88 prim_num_arr = tup_4; 89 90 let test_arr_2: number[] = [11, 2]; 91 assert(prim_num_arr[0] == test_arr_2[0]); 92 assert(prim_num_arr[1] == test_arr_2[1]); 93 94 95 let tup_8: [number, string][]; 96 tup_8 = [[1, "E"], [2, "F"], [3, "G"]]; 97 assert(tup_8[0][0] == 1 && tup_8[0][1] == "E"); 98 assert(tup_8[1][0] == 2 && tup_8[1][1] == "F"); 99 assert(tup_8[2][0] == 3 && tup_8[2][1] == "G"); 100 101 // #15570 - test ArrayExpr assignability with individual element types 102 // let tup_10: [number, int, string, boolean, Object] = [1, 2, "I", false, new Object()]; 103 // let var_float: float = tup_10[1]; 104 // let var_float_2: float = 2.0; 105 // assert(var_float == var_float_2); 106 // let tup_11: [int, number, string, boolean, Object] = [6, 7, "J", true, 789]; 107 // NOTE: Bug in op_assignment lowering (removes const from property) 108 // tup_11[0] += new Short(2 as short); 109 // assert(tup_11[0] == 8); 110 // assert(tup_11[4] == (789 as Object)); 111 112 let tup_12: [number, ...number[]] = [1, 2, 3, 4]; 113 try { 114 tup_12[4]; 115 } catch (e: ArrayIndexOutOfBoundsError) { 116 } catch (f) { 117 assert(false); 118 } 119 120 let tup_13: [number, number] = [8, 9]; 121 let tup_14: [number, number] = [10, 11]; 122 123 tup_13 = tup_14; 124 assert(tup_13[0] == 10); 125 assert(tup_13[1] == 11); 126 tup_13[0] = 12; 127 tup_13[1] = Double.valueOf(13.0); 128 assert(tup_13[0] == 12); 129 assert(tup_13[1] == 13); 130 131 let a_b_tup: [A, A, B] = [new A(), new B(), new B()]; 132 assert(a_b_tup[0].foo() == 1); 133 assert(a_b_tup[1].foo() == 2); 134 assert(a_b_tup[2].foo() == 2); 135 136 let number_tup: [Double, Double] = [42.0, 43.0]; 137 let tn: TupleProvider<number> = new TupleProvider<number>(number_tup); 138 let result: [Number, Number] = tn.publish(); 139 assert result[0] == 42.0; 140} 141