14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_cideclare interface ArkTools {
174514f5e3Sopenharmony_ci    isAOTCompiled(args: any): boolean;
184514f5e3Sopenharmony_ci}
194514f5e3Sopenharmony_cideclare function print(arg:any):string;
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_cifunction printZero(x: any) {
224514f5e3Sopenharmony_ci    if (Object.is(x, -0)) {
234514f5e3Sopenharmony_ci        print("-0");
244514f5e3Sopenharmony_ci    } else {
254514f5e3Sopenharmony_ci        print(x);
264514f5e3Sopenharmony_ci    }
274514f5e3Sopenharmony_ci}
284514f5e3Sopenharmony_cifunction replace(a : number)
294514f5e3Sopenharmony_ci{
304514f5e3Sopenharmony_ci    return a;
314514f5e3Sopenharmony_ci}
324514f5e3Sopenharmony_ci
334514f5e3Sopenharmony_ci// Use try to prevent inlining to main
344514f5e3Sopenharmony_cifunction printMax(x: any, y: any) {
354514f5e3Sopenharmony_ci    try {
364514f5e3Sopenharmony_ci        print(Math.max(x, y));
374514f5e3Sopenharmony_ci    } finally {
384514f5e3Sopenharmony_ci    }
394514f5e3Sopenharmony_ci}
404514f5e3Sopenharmony_ci
414514f5e3Sopenharmony_cifunction printMax1(x: any) {
424514f5e3Sopenharmony_ci    try {
434514f5e3Sopenharmony_ci        print(Math.max(x));
444514f5e3Sopenharmony_ci    } finally {
454514f5e3Sopenharmony_ci    }
464514f5e3Sopenharmony_ci}
474514f5e3Sopenharmony_ci
484514f5e3Sopenharmony_cifunction printMax3(x: any, y: any, z: any) {
494514f5e3Sopenharmony_ci    try {
504514f5e3Sopenharmony_ci        print(Math.max(x, y, z));
514514f5e3Sopenharmony_ci    } finally {
524514f5e3Sopenharmony_ci    }
534514f5e3Sopenharmony_ci}
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_cilet doubleObj = {
564514f5e3Sopenharmony_ci    valueOf: () => { return 2.7; }
574514f5e3Sopenharmony_ci}
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_cilet nanObj = {
604514f5e3Sopenharmony_ci    valueOf: () => { return "something"; }
614514f5e3Sopenharmony_ci}
624514f5e3Sopenharmony_ci
634514f5e3Sopenharmony_cilet obj = {
644514f5e3Sopenharmony_ci    valueOf: () => {
654514f5e3Sopenharmony_ci        print("obj.valueOf")
664514f5e3Sopenharmony_ci        return -23;
674514f5e3Sopenharmony_ci    }
684514f5e3Sopenharmony_ci};
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_ci// Check without params
714514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
724514f5e3Sopenharmony_ciprint(Math.max()); //: -Infinity
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ci// Check with single param
754514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
764514f5e3Sopenharmony_ciprint(Math.max(0)); //: 0
774514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
784514f5e3Sopenharmony_ciprint(Math.max(567)); //: 567
794514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
804514f5e3Sopenharmony_ciprint(Math.max(-1e80)); //: -1e+80
814514f5e3Sopenharmony_ci
824514f5e3Sopenharmony_ci// Check with special float params
834514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
844514f5e3Sopenharmony_ciprint(Math.max(Infinity)); //: Infinity
854514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
864514f5e3Sopenharmony_ciprint(Math.max(-Infinity)); //: -Infinity
874514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
884514f5e3Sopenharmony_ciprint(Math.max(NaN)); //: NaN
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_ci// Check with 2 int params
914514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
924514f5e3Sopenharmony_ciprint(Math.max(3, 300)); //: 300
934514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
944514f5e3Sopenharmony_ciprint(Math.max(300, 3)); //: 300
954514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
964514f5e3Sopenharmony_ciprint(Math.max(3, -2)); //: 3
974514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
984514f5e3Sopenharmony_ciprint(Math.max(-22, -2)); //: -2
994514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1004514f5e3Sopenharmony_ciprint(Math.max(-1, -1)); //: -1
1014514f5e3Sopenharmony_ci
1024514f5e3Sopenharmony_ci// Check with 2 float params
1034514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1044514f5e3Sopenharmony_ciprint(Math.max(3.2, 300.7)); //: 300.7
1054514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1064514f5e3Sopenharmony_ciprint(Math.max(300.7, 3.2)); //: 300.7
1074514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1084514f5e3Sopenharmony_ciprint(Math.max(3e9, -2e10)); //: 3000000000
1094514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1104514f5e3Sopenharmony_ciprint(Math.max(-22.1, -2e-10)); //: -2e-10
1114514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1124514f5e3Sopenharmony_ciprint(Math.max(-1.8, -1.8)); //: -1.8
1134514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1144514f5e3Sopenharmony_ciprint(Math.max(Infinity, -1.8)); //: Infinity
1154514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1164514f5e3Sopenharmony_ciprint(Math.max(-1.8, Infinity)); //: Infinity
1174514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1184514f5e3Sopenharmony_ciprint(Math.max(-Infinity, -1.8)); //: -1.8
1194514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1204514f5e3Sopenharmony_ciprint(Math.max(-1.8, -Infinity)); //: -1.8
1214514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1224514f5e3Sopenharmony_ciprint(Math.max(-1.8, NaN)); //: NaN
1234514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1244514f5e3Sopenharmony_ciprint(Math.max(NaN, -1.8)); //: NaN
1254514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1264514f5e3Sopenharmony_ciprint(Math.max(-Infinity, NaN)); //: NaN
1274514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1284514f5e3Sopenharmony_ciprint(Math.max(Infinity, NaN)); //: NaN
1294514f5e3Sopenharmony_ci
1304514f5e3Sopenharmony_ci// Check 0 and -0
1314514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1324514f5e3Sopenharmony_ci//aot: [trace] aot inline function name: #*#printZero@builtinMathMax caller function name: func_main_0@builtinMathMax
1334514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathMax
1344514f5e3Sopenharmony_ciprintZero(Math.max(0, -0));  //: 0
1354514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1364514f5e3Sopenharmony_ci//aot: [trace] aot inline function name: #*#printZero@builtinMathMax caller function name: func_main_0@builtinMathMax
1374514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathMax
1384514f5e3Sopenharmony_ciprintZero(Math.max(-0, 0));  //: 0
1394514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1404514f5e3Sopenharmony_ci//aot: [trace] aot inline function name: #*#printZero@builtinMathMax caller function name: func_main_0@builtinMathMax
1414514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathMax
1424514f5e3Sopenharmony_ciprintZero(Math.max(0, 0));   //: 0
1434514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1444514f5e3Sopenharmony_ci//aot: [trace] aot inline function name: #*#printZero@builtinMathMax caller function name: func_main_0@builtinMathMax
1454514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathMax
1464514f5e3Sopenharmony_ciprintZero(Math.max(-0, -0)); //: -0
1474514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1484514f5e3Sopenharmony_ci//aot: [trace] aot inline function name: #*#printZero@builtinMathMax caller function name: func_main_0@builtinMathMax
1494514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathMax
1504514f5e3Sopenharmony_ciprintZero(Math.max(-5, -0)); //: -0
1514514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1524514f5e3Sopenharmony_ci//aot: [trace] aot inline function name: #*#printZero@builtinMathMax caller function name: func_main_0@builtinMathMax
1534514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathMax
1544514f5e3Sopenharmony_ciprintZero(Math.max(-5, 0)); //: 0
1554514f5e3Sopenharmony_ci
1564514f5e3Sopenharmony_ci// Check with int and float param
1574514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1584514f5e3Sopenharmony_ciprint(Math.max(1.5, 3)); //: 3
1594514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1604514f5e3Sopenharmony_ciprint(Math.max(3, 1.5)); //: 3
1614514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1624514f5e3Sopenharmony_ciprint(Math.max(2.5, 1)); //: 2.5
1634514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1644514f5e3Sopenharmony_ciprint(Math.max(1, 2.5)); //: 2.5
1654514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1664514f5e3Sopenharmony_ciprint(Math.max(1, -Infinity)); //: 1
1674514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1684514f5e3Sopenharmony_ciprint(Math.max(5, NaN)); //: NaN
1694514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1704514f5e3Sopenharmony_ciprint(Math.max(NaN, 5)); //: NaN
1714514f5e3Sopenharmony_ci
1724514f5e3Sopenharmony_ci
1734514f5e3Sopenharmony_ci// Check with 3 params
1744514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1754514f5e3Sopenharmony_ciprint(Math.max(2, 4, 6)); //: 6
1764514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1774514f5e3Sopenharmony_ciprint(Math.max(4, 6, 2)); //: 6
1784514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1794514f5e3Sopenharmony_ciprint(Math.max(6, 2, 4)); //: 6
1804514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1814514f5e3Sopenharmony_ciprint(Math.max(-1, 1, 2.5)); //: 2.5
1824514f5e3Sopenharmony_ci
1834514f5e3Sopenharmony_ci// Check with 4 params
1844514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1854514f5e3Sopenharmony_ciprint(Math.max(-4, 0, 2, -Infinity)); //: 2
1864514f5e3Sopenharmony_ci
1874514f5e3Sopenharmony_ci// Check with 5 params
1884514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:func_main_0@builtinMathMax
1894514f5e3Sopenharmony_ciprint(Math.max(1, 2, 1.5, 2, 8)); //: 8
1904514f5e3Sopenharmony_ci
1914514f5e3Sopenharmony_ci// Replace standard builtin
1924514f5e3Sopenharmony_cilet trueMax = Math.max
1934514f5e3Sopenharmony_ciMath.max = replace
1944514f5e3Sopenharmony_ciprint(Math.max(-1.001, -90)); //: -1.001
1954514f5e3Sopenharmony_ciMath.max = trueMax
1964514f5e3Sopenharmony_ci
1974514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:#*#printMax@builtinMathMax
1984514f5e3Sopenharmony_ciprintMax(-12, -100); //: -12
1994514f5e3Sopenharmony_ci// Call standard builtin with non-number param
2004514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:#*#printMax@builtinMathMax
2014514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2
2024514f5e3Sopenharmony_ciprintMax("abc", -100); //: NaN
2034514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:#*#printMax@builtinMathMax
2044514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2
2054514f5e3Sopenharmony_ciprintMax("-12", -100); //: -12
2064514f5e3Sopenharmony_ci
2074514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printMax)) {
2084514f5e3Sopenharmony_ci    // Replace standard builtin after call to standard builtin was profiled
2094514f5e3Sopenharmony_ci    Math.max = replace
2104514f5e3Sopenharmony_ci}
2114514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
2124514f5e3Sopenharmony_ciprintMax(5, 12); //pgo: 12
2134514f5e3Sopenharmony_ci                  //aot: 5
2144514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
2154514f5e3Sopenharmony_ciprintMax("abc", 2); //pgo: NaN
2164514f5e3Sopenharmony_ci                    //aot: abc
2174514f5e3Sopenharmony_ciMath.max = trueMax
2184514f5e3Sopenharmony_ci
2194514f5e3Sopenharmony_ci// Check IR correctness inside try-block
2204514f5e3Sopenharmony_citry {
2214514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Math.max, caller function name:#*#printMax@builtinMathMax
2224514f5e3Sopenharmony_ci    printMax(19, 20); //: 20
2234514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Math.max, caller function name:#*#printMax@builtinMathMax
2244514f5e3Sopenharmony_ci    printMax(19, 12); //: 19
2254514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Math.max, caller function name:#*#printMax@builtinMathMax
2264514f5e3Sopenharmony_ci    //aot: [trace] Check Type: NotNumber2
2274514f5e3Sopenharmony_ci    printMax("abc", 5); //: NaN
2284514f5e3Sopenharmony_ci} catch (e) {
2294514f5e3Sopenharmony_ci}
2304514f5e3Sopenharmony_ci
2314514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:#*#printMax1@builtinMathMax
2324514f5e3Sopenharmony_ci//aot: [trace] Check Type: InconsistentType1
2334514f5e3Sopenharmony_ci//: obj.valueOf
2344514f5e3Sopenharmony_ciprintMax1(obj); //: -23
2354514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:#*#printMax1@builtinMathMax
2364514f5e3Sopenharmony_ci//aot: [trace] Check Type: InconsistentType1
2374514f5e3Sopenharmony_ciprintMax1(doubleObj); //: 2.7
2384514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:#*#printMax@builtinMathMax
2394514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2
2404514f5e3Sopenharmony_ciprintMax(doubleObj, doubleObj); //: 2.7
2414514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:#*#printMax@builtinMathMax
2424514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2
2434514f5e3Sopenharmony_ci//: obj.valueOf
2444514f5e3Sopenharmony_ciprintMax(nanObj, obj); //: NaN
2454514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:#*#printMax@builtinMathMax
2464514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2
2474514f5e3Sopenharmony_ci//: obj.valueOf
2484514f5e3Sopenharmony_ciprintMax(2, obj); //: 2
2494514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:#*#printMax@builtinMathMax
2504514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2
2514514f5e3Sopenharmony_ci//: obj.valueOf
2524514f5e3Sopenharmony_ciprintMax(-100, obj); //: -23
2534514f5e3Sopenharmony_ci
2544514f5e3Sopenharmony_ci// call obj.valueOf twice
2554514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.max, caller function name:#*#printMax3@builtinMathMax
2564514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2
2574514f5e3Sopenharmony_ci//: obj.valueOf
2584514f5e3Sopenharmony_ci//: obj.valueOf
2594514f5e3Sopenharmony_ciprintMax3(NaN, obj, obj); //: NaN
260