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 */ 15declare function print(arg:any):void; 16let F : number[] = [0, 1, 2] 17let G : number[] = [0, 1, 2] 18 19function tryLoopOpt(f: number[], v : number): void { 20 let idx = 1 - 1; 21 let ret = f[idx]; 22 for (let i = 0; i < f.length; ++i) { 23 f[i] += 1; 24 f[i] += v; 25 } 26 print(ret); 27 print(f[idx]); 28 print(f[idx + 1]); 29} 30 31tryLoopOpt(F, <number><Object>'a'); 32 33function tryMergeOpt(g: number[], v: number): void { 34 let idx = 1 - 1; 35 let ret = g[idx]; 36 if (g[idx] < 10) { 37 g[idx] -= 10; 38 } else { 39 v++; 40 } 41 print(ret + v); 42 print(g[idx]); 43} 44 45tryMergeOpt(G, <number><Object>'b'); 46 47function binarySearch(array: number[], target: number): number { 48 let low : number = 0; 49 let high : number = array.length - 1; 50 51 while (low <= high) { 52 let mid: number = (low + high) >>> 1; 53 if (array[mid] == target) { 54 return mid; 55 } else if (array[mid] < target) { 56 low = mid + 1; 57 } else if (array[mid] > target) { 58 high = mid - 1; 59 } 60 } 61 return -1; 62} 63 64let array: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 65let ret = binarySearch(array, 3); 66print(ret); 67 68function foo(arr : number[]) { 69 let t = 0 + 0; 70 let sum = arr[t]; 71 for (let i = 0; i < 2; ++i) { 72 sum += arr[t]; 73 arr[t] = 999; 74 } 75 return sum; 76} 77print(foo(array)); 78 79class Obj { 80 a:number; 81 b:number; 82 constructor() { 83 this.a = 1; 84 this.b = 4; 85 } 86} 87function LoadPropertyOpt(obj : Obj) : number { 88 let x1 = obj.a; // 1 89 let y1 = obj.b; // 4 90 obj.a = 16; 91 let x2 = obj.a; 92 let y2 = obj.b; 93 return x1 + y1 + x2 + y2; 94} 95let obj : Obj = new Obj(); 96print(LoadPropertyOpt(obj)); 97 98let x = 1; 99const size = (x === undefined) ? 1 : 2; 100const cleanupFunc = (x === undefined) ? 1 : 2; 101print(size); 102print(cleanupFunc); 103 104function foo1() : void { 105 print(0); 106} 107function foo2(i : number) : void { 108 let a = {x: null, y:233, z:466}; 109 let b = {y: 5}; 110 a.x = b; 111 if (i > 10) { 112 a.x.y = i; 113 } 114 foo1() 115 return a.x.y; 116} 117function foo3(i : number) : void { 118 let a = {x: null, y:233, z:466}; 119 let b = {y: 5}; 120 a.x = b; 121 if (i > 10) { 122 a.x.y = i; 123 } 124 foo1() 125 return a.x; 126} 127print(foo2(1)) 128print(foo2(5)) 129print(foo2(10)) 130print(foo2(100)) 131print(foo2(11)) 132print(foo3(1)) 133print(foo3(11)) 134