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/bit_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 BitHelperTest : 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(BitHelperTest,CountLeadingZeros_CountTrailingZeros)50 HWTEST_F_L0(BitHelperTest, CountLeadingZeros_CountTrailingZeros)
51 {
52 uint8_t uint8MaxValue = std::numeric_limits<uint8_t>::max();
53 uint8_t uint8MinValue = std::numeric_limits<uint8_t>::min();
54 EXPECT_EQ(CountLeadingZeros<uint8_t>(uint8MaxValue), 0U);
55 EXPECT_EQ(CountLeadingZeros<uint8_t>(uint8MinValue), 8U);
56 EXPECT_EQ(CountTrailingZeros<uint8_t>(uint8MaxValue), 0U);
57 EXPECT_EQ(CountTrailingZeros<uint8_t>(uint8MinValue), 8U);
58
59 uint16_t uint16MaxValue = std::numeric_limits<uint16_t>::max();
60 uint16_t uint16MinValue = std::numeric_limits<uint16_t>::min();
61 EXPECT_EQ(CountLeadingZeros<uint16_t>(uint16MaxValue), 0U);
62 EXPECT_EQ(CountLeadingZeros<uint16_t>(uint16MinValue), 16U);
63 EXPECT_EQ(CountTrailingZeros<uint16_t>(uint16MaxValue), 0U);
64 EXPECT_EQ(CountTrailingZeros<uint16_t>(uint16MinValue), 16U);
65
66 uint32_t uint32MaxValue = std::numeric_limits<uint32_t>::max();
67 uint32_t uint32MinValue = std::numeric_limits<uint32_t>::min();
68 EXPECT_EQ(CountLeadingZeros<uint32_t>(uint32MaxValue), 0U);
69 EXPECT_EQ(CountLeadingZeros<uint32_t>(uint32MinValue), 32U);
70 EXPECT_EQ(CountTrailingZeros<uint32_t>(uint32MaxValue), 0U);
71 EXPECT_EQ(CountTrailingZeros<uint32_t>(uint32MinValue), 32U);
72
73 uint64_t uint64MaxValue = std::numeric_limits<uint64_t>::max();
74 uint64_t uint64MinValue = std::numeric_limits<uint64_t>::min();
75 EXPECT_EQ(CountLeadingZeros<uint64_t>(uint64MaxValue), 0U);
76 EXPECT_EQ(CountLeadingZeros<uint64_t>(uint64MinValue), 64U);
77 EXPECT_EQ(CountTrailingZeros<uint64_t>(uint64MaxValue), 0U);
78 EXPECT_EQ(CountTrailingZeros<uint64_t>(uint64MinValue), 64U);
79 }
80
HWTEST_F_L0(BitHelperTest,CountLeadingZeros32_CountLeadingOnes32)81 HWTEST_F_L0(BitHelperTest, CountLeadingZeros32_CountLeadingOnes32)
82 {
83 uint32_t uint32MaxValue = std::numeric_limits<uint32_t>::max();
84 uint32_t uint32CommonValue1 = std::numeric_limits<uint32_t>::max() >> 1;
85 uint32_t uint32CommonValue2 = std::numeric_limits<uint32_t>::max() >> 31; // 31 : right shift digit
86 uint32_t uint32MinValue = std::numeric_limits<uint32_t>::min();
87 EXPECT_EQ(CountLeadingZeros32(uint32MaxValue), 0U);
88 EXPECT_EQ(CountLeadingZeros32(uint32CommonValue1), 1U);
89 EXPECT_EQ(CountLeadingZeros32(uint32CommonValue2), 31U);
90 EXPECT_EQ(CountLeadingZeros32(uint32MinValue), 32U);
91 EXPECT_EQ(CountLeadingOnes32(uint32MaxValue), 32U);
92 EXPECT_EQ(CountLeadingOnes32(uint32CommonValue1), 0U);
93 EXPECT_EQ(CountLeadingOnes32(uint32CommonValue2), 0U);
94 EXPECT_EQ(CountLeadingOnes32(uint32MinValue), 0U);
95 }
96
HWTEST_F_L0(BitHelperTest,CountLeadingZeros64_CountLeadingOnes64)97 HWTEST_F_L0(BitHelperTest, CountLeadingZeros64_CountLeadingOnes64)
98 {
99 uint64_t uint64MaxValue = std::numeric_limits<uint64_t>::max();
100 uint64_t uint64CommonValue1 = std::numeric_limits<uint64_t>::max() >> 1;
101 uint64_t uint64CommonValue2 = std::numeric_limits<uint64_t>::max() >> 63; // 63 : right shift digit
102 uint64_t uint64MinValue = std::numeric_limits<uint64_t>::min();
103 EXPECT_EQ(CountLeadingZeros64(uint64MaxValue), 0U);
104 EXPECT_EQ(CountLeadingZeros64(uint64CommonValue1), 1U);
105 EXPECT_EQ(CountLeadingZeros64(uint64CommonValue2), 63U);
106 EXPECT_EQ(CountLeadingZeros64(uint64MinValue), 64U);
107 EXPECT_EQ(CountLeadingOnes64(uint64MaxValue), 64U);
108 EXPECT_EQ(CountLeadingOnes64(uint64CommonValue1), 0U);
109 EXPECT_EQ(CountLeadingOnes64(uint64CommonValue2), 0U);
110 EXPECT_EQ(CountLeadingOnes64(uint64MinValue), 0U);
111 }
112 } // namespace panda::test
113