• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 Google LLC
2 //
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 //     https://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 #include "src/base/math_utils.h"
16 
17 #include <gtest/gtest.h>
18 
19 namespace astc_codec {
20 namespace base {
21 
TEST(MathUtils,Log2Floor)22 TEST(MathUtils, Log2Floor) {
23   EXPECT_EQ(-1, Log2Floor(0));
24 
25   for (int i = 0; i < 32; i++) {
26     uint32_t n = 1U << i;
27     EXPECT_EQ(i, Log2Floor(n));
28     if (n > 2) {
29       EXPECT_EQ(i - 1, Log2Floor(n - 1));
30       EXPECT_EQ(i,     Log2Floor(n + 1));
31     }
32   }
33 }
34 
TEST(MathUtils,CountOnes)35 TEST(MathUtils, CountOnes) {
36   EXPECT_EQ(0, CountOnes(0));
37   EXPECT_EQ(1, CountOnes(1));
38   EXPECT_EQ(32, CountOnes(static_cast<uint32_t>(~0U)));
39   EXPECT_EQ(1, CountOnes(0x8000000));
40 
41   for (int i = 0; i < 32; i++) {
42     EXPECT_EQ(1, CountOnes(1U << i));
43     EXPECT_EQ(31, CountOnes(static_cast<uint32_t>(~0U) ^ (1U << i)));
44   }
45 }
46 
TEST(MathUtils,ReverseBits)47 TEST(MathUtils, ReverseBits) {
48   EXPECT_EQ(ReverseBits(0u), 0u);
49   EXPECT_EQ(ReverseBits(1u), 1u << 31);
50   EXPECT_EQ(ReverseBits(0xffffffff), 0xffffffff);
51   EXPECT_EQ(ReverseBits(0x00000001), 0x80000000);
52   EXPECT_EQ(ReverseBits(0x80000000), 0x00000001);
53   EXPECT_EQ(ReverseBits(0xaaaaaaaa), 0x55555555);
54   EXPECT_EQ(ReverseBits(0x55555555), 0xaaaaaaaa);
55   EXPECT_EQ(ReverseBits(0x7d5d7f53), 0xcafebabe);
56   EXPECT_EQ(ReverseBits(0xcafebabe), 0x7d5d7f53);
57 }
58 
TEST(MathUtils,GetBits)59 TEST(MathUtils, GetBits) {
60   EXPECT_EQ(GetBits(0u, 0, 1), 0u);
61   EXPECT_EQ(GetBits(0u, 0, 32), 0u);
62   EXPECT_EQ(GetBits(0x00000001u, 0, 1), 0x00000001);
63   EXPECT_EQ(GetBits(0x00000001u, 0, 32), 0x00000001);
64   EXPECT_EQ(GetBits(0x00000001u, 1, 31), 0x00000000);
65   EXPECT_EQ(GetBits(0x00000001u, 31, 1), 0x00000000);
66 
67   EXPECT_DEBUG_DEATH(GetBits(0x00000000u, 1, 32), "");
68   EXPECT_DEBUG_DEATH(GetBits(0x00000000u, 32, 0), "");
69   EXPECT_DEBUG_DEATH(GetBits(0x00000000u, 32, 1), "");
70 
71   EXPECT_EQ(GetBits(0XFFFFFFFFu, 0, 4), 0x0000000F);
72   EXPECT_EQ(GetBits(0XFFFFFFFFu, 16, 16), 0xFFFF);
73   EXPECT_EQ(GetBits(0x80000000u, 31, 1), 1);
74   EXPECT_EQ(GetBits(0xCAFEBABEu, 24, 8), 0xCA);
75 }
76 
77 }  // namespace base
78 }  // namespace astc_codec
79