1/* 2 * Copyright (c) 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 16function check1() { 17 let sum = 0; 18 let a : number[] = [1,2,3] 19 let idx : number | string; 20 for (idx of a) { 21 let b = idx 22 if (b instanceof Number) { 23 sum += b; 24 } else { 25 sum += 0; 26 } 27 } 28 29 assert(sum == 6); 30} 31 32function check2() { 33 let str = ""; 34 let a2 : string[] = ["ddd"] 35 let idx2 : string | boolean; 36 for (idx2 of a2) { 37 let c2 = idx2 38 if (c2 instanceof string) { 39 str += c2; 40 } 41 } 42 43 assert(str == "ddd"); 44} 45 46function check3() { 47 let str = ""; 48 let a3 : (int|string)[] = ["1,2,3"] 49 let idx3 : int|String; 50 for (idx3 of a3) { 51 let c3 = idx3 52 if (idx3 instanceof string) { 53 str += c3; 54 } 55 } 56 57 assert(str == "1,2,3"); 58} 59 60function check4() { 61 let sum = 0; 62 let a4 : number[] = [888, 999]; 63 for (let idx : number | null of a4) { 64 let b4 = idx; 65 if (b4 instanceof Number) { 66 sum += b4; 67 } 68 } 69 70 assert(sum == 1887); 71} 72 73function check5() { 74 let sum = 0; 75 let a5 : number[] = [11, 22]; 76 for (let idx5 : Number of a5) { 77 let b5 = idx5 78 sum += b5; 79 } 80 81 assert(sum == 33); 82} 83 84function check6() { 85 let sum = 0; 86 let a6 : number[] = [500, 600]; 87 for (let idx6 : Double | null of a6) { 88 let b6 = idx6 89 if (b6 instanceof Number) { 90 sum += b6; 91 } 92 } 93 94 assert(sum == 1100); 95} 96 97function check7() { 98 let str = ""; 99 let a7 : string = "ffff" 100 for (let idx7 : char of a7) { 101 let b7 = idx7 102 str += b7; 103 } 104 105 assert(str == "ffff"); 106} 107function main() { 108 check1(); 109 check2(); 110 check3(); 111 check4(); 112 check5(); 113 check6(); 114 check7(); 115} 116