1 /* 2 * Copyright (c) 2021-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 "utils/math_helpers.h" 17 18 #include <cmath> 19 #include <gtest/gtest.h> 20 21 namespace panda::helpers::math::test { 22 23 HWTEST(MathHelpers, GetIntLog2, testing::ext::TestSize.Level0) 24 { 25 uint64_t val = 0; 26 for (uint32_t i = 1; i < 32; i++) { 27 val = 1U << i; 28 EXPECT_EQ(GetIntLog2(val), i); 29 EXPECT_EQ(GetIntLog2(val), log2(static_cast<double>(val))); 30 } 31 32 for (int i = 1; i < 32; i++) { 33 val = (1U << i) + (i == 31 ? -1 : 1); 34 #ifndef NDEBUG 35 EXPECT_DEATH_IF_SUPPORTED(GetIntLog2(val), ""); 36 #endif 37 } 38 } 39 40 HWTEST(MathHelpers, IsPowerOfTwo, testing::ext::TestSize.Level0) 41 { 42 EXPECT_TRUE(IsPowerOfTwo(1)); 43 EXPECT_TRUE(IsPowerOfTwo(2)); 44 EXPECT_TRUE(IsPowerOfTwo(4)); 45 EXPECT_TRUE(IsPowerOfTwo(64)); 46 EXPECT_TRUE(IsPowerOfTwo(1024)); 47 EXPECT_TRUE(IsPowerOfTwo(2048)); 48 EXPECT_FALSE(IsPowerOfTwo(3)); 49 EXPECT_FALSE(IsPowerOfTwo(63)); 50 EXPECT_FALSE(IsPowerOfTwo(65)); 51 EXPECT_FALSE(IsPowerOfTwo(100)); 52 } 53 54 HWTEST(MathHelpers, GetPowerOfTwoValue32, testing::ext::TestSize.Level0) 55 { 56 for (int i = 0; i <= 1; i++) { 57 EXPECT_EQ(GetPowerOfTwoValue32(i), 1U); 58 } 59 for (int i = 2; i <= 2; i++) { 60 EXPECT_EQ(GetPowerOfTwoValue32(i), 2U); 61 } 62 for (int i = 9; i <= 16; i++) { 63 EXPECT_EQ(GetPowerOfTwoValue32(i), 16U); 64 } 65 for (int i = 33; i <= 64; i++) { 66 EXPECT_EQ(GetPowerOfTwoValue32(i), 64U); 67 } 68 for (int i = 1025; i <= 2048; i++) { 69 EXPECT_EQ(GetPowerOfTwoValue32(i), 2048U); 70 } 71 } 72 73 HWTEST(MathHelpers, MinAndMaxTest, testing::ext::TestSize.Level0) 74 { 75 ASSERT_EQ(0.0, panda::helpers::math::min<float>(0.0, 0.0)); 76 ASSERT_EQ(1.0, panda::helpers::math::min<float>(1.0, 2.0)); 77 78 ASSERT_EQ(0.0, panda::helpers::math::max<float>(0.0, 0.0)); 79 ASSERT_EQ(2.0, panda::helpers::math::max<float>(2.0, 1.0)); 80 } 81 82 } // namespace panda::helpers::math::test 83