1 /*
2 * Copyright (c) 2022 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
16 #include "ecmascript/base/math_helper.h"
17 #include "ecmascript/tests/test_helper.h"
18
19 using namespace panda::ecmascript;
20 using namespace panda::ecmascript::base;
21
22 namespace panda::test {
23 class MathHelperTest : public testing::Test {
24 public:
SetUpTestCase()25 static void SetUpTestCase()
26 {
27 GTEST_LOG_(INFO) << "SetUpTestCase";
28 }
29
TearDownTestCase()30 static void TearDownTestCase()
31 {
32 GTEST_LOG_(INFO) << "TearDownCase";
33 }
34
SetUp()35 void SetUp() override
36 {
37 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
38 }
39
TearDown()40 void TearDown() override
41 {
42 TestHelper::DestroyEcmaVMWithScope(instance, scope);
43 }
44
45 EcmaVM *instance {nullptr};
46 EcmaHandleScope *scope {nullptr};
47 JSThread *thread {nullptr};
48 };
49
HWTEST_F_L0(MathHelperTest,GetIntLog2_001)50 HWTEST_F_L0(MathHelperTest, GetIntLog2_001)
51 {
52 const uint32_t commonInput = static_cast<uint32_t>(0b111111111) << 13; // 13 : left shift digit
53 const uint32_t maxInput = std::numeric_limits<uint32_t>::max();
54 EXPECT_EQ(MathHelper::GetIntLog2(commonInput), 13U);
55 EXPECT_EQ(MathHelper::GetIntLog2(maxInput), 0U);
56 }
57
HWTEST_F_L0(MathHelperTest,GetIntLog2_002)58 HWTEST_F_L0(MathHelperTest, GetIntLog2_002)
59 {
60 const uint64_t commonInput = static_cast<uint64_t>(0b111111111) << 53; // 53 : left shift digit
61 const uint64_t maxInput = std::numeric_limits<uint64_t>::max();
62 EXPECT_EQ(MathHelper::GetIntLog2(commonInput), 53U);
63 EXPECT_EQ(MathHelper::GetIntLog2(maxInput), 0U);
64 }
65
HWTEST_F_L0(MathHelperTest,Asinh)66 HWTEST_F_L0(MathHelperTest, Asinh)
67 {
68 EXPECT_EQ(MathHelper::Asinh(1), 0.88137358701954302523260932497979);
69 EXPECT_EQ(MathHelper::Asinh(+0), +0.0);
70 EXPECT_EQ(MathHelper::Asinh(-0), -0.0);
71 EXPECT_EQ(MathHelper::Asinh(-1), -0.88137358701954302523260932497979);
72
73 double nanResult = MathHelper::Asinh(std::numeric_limits<double>::signaling_NaN());
74 EXPECT_TRUE(std::isnan(nanResult));
75 }
76
HWTEST_F_L0(MathHelperTest,Atanh)77 HWTEST_F_L0(MathHelperTest, Atanh)
78 {
79 EXPECT_EQ(MathHelper::Atanh(0), 0);
80 EXPECT_EQ(MathHelper::Atanh(0.5), std::atanh(0.5));
81
82 double infResult = MathHelper::Atanh(-1); // limit value
83 EXPECT_TRUE(std::isinf(infResult));
84
85 double nanResult = MathHelper::Atanh(2); // out of input range
86 EXPECT_TRUE(std::isnan(nanResult));
87 }
88 } // namespace panda::test
89