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 function print(arg : any) : string;
174514f5e3Sopenharmony_cideclare interface ArkTools {
184514f5e3Sopenharmony_ci    isAOTCompiled(args : any) : boolean;
194514f5e3Sopenharmony_ci}
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_cifunction replace(a : number)
224514f5e3Sopenharmony_ci{
234514f5e3Sopenharmony_ci    return a;
244514f5e3Sopenharmony_ci}
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_cifunction doTrunc(x : any) : number {
274514f5e3Sopenharmony_ci    return Math.trunc(x);
284514f5e3Sopenharmony_ci}
294514f5e3Sopenharmony_ci
304514f5e3Sopenharmony_cifunction printTrunc(x : any) {
314514f5e3Sopenharmony_ci    try {
324514f5e3Sopenharmony_ci        print(doTrunc(x));
334514f5e3Sopenharmony_ci    } finally {
344514f5e3Sopenharmony_ci    }
354514f5e3Sopenharmony_ci}
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_ci// Check without params
384514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
394514f5e3Sopenharmony_ciprint(Math.trunc()); //: NaN
404514f5e3Sopenharmony_ci
414514f5e3Sopenharmony_ci// Check with special float params
424514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
434514f5e3Sopenharmony_ciprint(Math.trunc(NaN)); //: NaN
444514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
454514f5e3Sopenharmony_ciprint(Math.trunc(Infinity)); //: Infinity
464514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
474514f5e3Sopenharmony_ciprint(Math.trunc(-Infinity)); //: -Infinity
484514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
494514f5e3Sopenharmony_ciprint(Math.trunc(+0)); //: 0
504514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
514514f5e3Sopenharmony_ciprint("1/x: " + 1 / Math.trunc(-0)); //: 1/x: -Infinity
524514f5e3Sopenharmony_ci
534514f5e3Sopenharmony_ci// Check with single integer param
544514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
554514f5e3Sopenharmony_ciprint(Math.trunc(1)); //: 1
564514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
574514f5e3Sopenharmony_ciprint(Math.trunc(-12)); //: -12
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_ci// Check with single float param
604514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
614514f5e3Sopenharmony_ciprint(Math.trunc(1.15613251)); //: 1
624514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
634514f5e3Sopenharmony_ciprint(Math.trunc(2.5)); //: 2
644514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
654514f5e3Sopenharmony_ciprint(Math.trunc(3.84556546)); //: 3
664514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
674514f5e3Sopenharmony_ciprint(Math.trunc(-1.124212)); //: -1
684514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
694514f5e3Sopenharmony_ciprint("1/x: " + 1 / Math.trunc(-8.5e-80)); //: 1/x: -Infinity
704514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
714514f5e3Sopenharmony_ciprint(Math.trunc(-4.5)); //: -4
724514f5e3Sopenharmony_ci
734514f5e3Sopenharmony_ci// Check with 2 params
744514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
754514f5e3Sopenharmony_ciprint(Math.trunc(2.4, 10.5)); //: 2
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_ci// Check with 3 params
784514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
794514f5e3Sopenharmony_ciprint(Math.trunc(3.123, 10, 1e-39)); //: 3
804514f5e3Sopenharmony_ci
814514f5e3Sopenharmony_ci// Check with 4 params
824514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
834514f5e3Sopenharmony_ciprint(Math.trunc(4.89, 10.5, 0, 11)); //: 4
844514f5e3Sopenharmony_ci
854514f5e3Sopenharmony_ci// Replace standart builtin
864514f5e3Sopenharmony_cilet true_trunc = Math.trunc
874514f5e3Sopenharmony_ciMath.trunc = replace
884514f5e3Sopenharmony_ciprint(Math.trunc(111.09)); //: 111.09
894514f5e3Sopenharmony_ciMath.trunc = true_trunc
904514f5e3Sopenharmony_ci
914514f5e3Sopenharmony_ci// Call standart builtin with non-number param
924514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:#*#doTrunc@builtinMathTrunc
934514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2
944514f5e3Sopenharmony_ciprintTrunc("abc"); //: NaN
954514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:#*#doTrunc@builtinMathTrunc
964514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2
974514f5e3Sopenharmony_ciprintTrunc("2.45"); //: 2
984514f5e3Sopenharmony_ci
994514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printTrunc)) {
1004514f5e3Sopenharmony_ci    // Replace standard builtin after call to standard builtin was profiled
1014514f5e3Sopenharmony_ci    Math.trunc = replace
1024514f5e3Sopenharmony_ci}
1034514f5e3Sopenharmony_ciprintTrunc(-12.1); //pgo: -12
1044514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1054514f5e3Sopenharmony_ci//aot: -12.1
1064514f5e3Sopenharmony_ci
1074514f5e3Sopenharmony_ciprintTrunc("abc"); //pgo: NaN
1084514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1094514f5e3Sopenharmony_ci//aot: abc
1104514f5e3Sopenharmony_ci
1114514f5e3Sopenharmony_ciMath.trunc = true_trunc
1124514f5e3Sopenharmony_ci
1134514f5e3Sopenharmony_ci// Checl IR correctness inside try-block
1144514f5e3Sopenharmony_citry {
1154514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Math.trunc, caller function name:#*#doTrunc@builtinMathTrunc
1164514f5e3Sopenharmony_ci    printTrunc(-48.12); //: -48
1174514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Math.trunc, caller function name:#*#doTrunc@builtinMathTrunc
1184514f5e3Sopenharmony_ci    //aot: [trace] Check Type: NotNumber2
1194514f5e3Sopenharmony_ci    printTrunc("abc"); //: NaN
1204514f5e3Sopenharmony_ci} catch (e) {
1214514f5e3Sopenharmony_ci}
1224514f5e3Sopenharmony_ci
1234514f5e3Sopenharmony_cilet obj = {
1244514f5e3Sopenharmony_ci    valueOf: () => { return -35.121; }
1254514f5e3Sopenharmony_ci};
1264514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.trunc, caller function name:func_main_0@builtinMathTrunc
1274514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2
1284514f5e3Sopenharmony_ciprint(Math.trunc(obj)); //: -35
1294514f5e3Sopenharmony_ci
1304514f5e3Sopenharmony_cifunction Throwing() {
1314514f5e3Sopenharmony_ci    this.value = -14.12;
1324514f5e3Sopenharmony_ci}
1334514f5e3Sopenharmony_ciThrowing.prototype.valueOf = function() {
1344514f5e3Sopenharmony_ci    if (this.value > 0) {
1354514f5e3Sopenharmony_ci        throw new Error("already positive");
1364514f5e3Sopenharmony_ci    }
1374514f5e3Sopenharmony_ci    return this.value;
1384514f5e3Sopenharmony_ci}
1394514f5e3Sopenharmony_cilet throwingObj = new Throwing();
1404514f5e3Sopenharmony_ci
1414514f5e3Sopenharmony_citry {
1424514f5e3Sopenharmony_ci    print(Math.trunc(throwingObj)); //: -14
1434514f5e3Sopenharmony_ci    throwingObj.value = 10;
1444514f5e3Sopenharmony_ci    print(Math.trunc(throwingObj)); //: Error: already positive
1454514f5e3Sopenharmony_ci} catch(e) {
1464514f5e3Sopenharmony_ci    print(e);
1474514f5e3Sopenharmony_ci} finally {
1484514f5e3Sopenharmony_ci    print(Math.trunc(obj)); //: -35
1494514f5e3Sopenharmony_ci}
150