OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/base/bits.h" | 5 #include "src/base/bits.h" |
6 #include "src/base/macros.h" | 6 #include "src/base/macros.h" |
7 #include "testing/gtest-support.h" | 7 #include "testing/gtest-support.h" |
8 | 8 |
| 9 #ifdef DEBUG |
| 10 #define DISABLE_IN_RELEASE(Name) Name |
| 11 #else |
| 12 #define DISABLE_IN_RELEASE(Name) DISABLED_##Name |
| 13 #endif |
| 14 |
9 namespace v8 { | 15 namespace v8 { |
10 namespace base { | 16 namespace base { |
11 namespace bits { | 17 namespace bits { |
12 | 18 |
13 TEST(BitsTest, CountPopulation32) { | 19 TEST(Bits, CountPopulation32) { |
14 EXPECT_EQ(0u, CountPopulation32(0)); | 20 EXPECT_EQ(0u, CountPopulation32(0)); |
15 EXPECT_EQ(1u, CountPopulation32(1)); | 21 EXPECT_EQ(1u, CountPopulation32(1)); |
16 EXPECT_EQ(8u, CountPopulation32(0x11111111)); | 22 EXPECT_EQ(8u, CountPopulation32(0x11111111)); |
17 EXPECT_EQ(16u, CountPopulation32(0xf0f0f0f0)); | 23 EXPECT_EQ(16u, CountPopulation32(0xf0f0f0f0)); |
18 EXPECT_EQ(24u, CountPopulation32(0xfff0f0ff)); | 24 EXPECT_EQ(24u, CountPopulation32(0xfff0f0ff)); |
19 EXPECT_EQ(32u, CountPopulation32(0xffffffff)); | 25 EXPECT_EQ(32u, CountPopulation32(0xffffffff)); |
20 } | 26 } |
21 | 27 |
22 | 28 |
23 TEST(BitsTest, CountLeadingZeros32) { | 29 TEST(Bits, CountLeadingZeros32) { |
24 EXPECT_EQ(32u, CountLeadingZeros32(0)); | 30 EXPECT_EQ(32u, CountLeadingZeros32(0)); |
25 EXPECT_EQ(31u, CountLeadingZeros32(1)); | 31 EXPECT_EQ(31u, CountLeadingZeros32(1)); |
26 TRACED_FORRANGE(uint32_t, shift, 0, 31) { | 32 TRACED_FORRANGE(uint32_t, shift, 0, 31) { |
27 EXPECT_EQ(31u - shift, CountLeadingZeros32(1u << shift)); | 33 EXPECT_EQ(31u - shift, CountLeadingZeros32(1u << shift)); |
28 } | 34 } |
29 EXPECT_EQ(4u, CountLeadingZeros32(0x0f0f0f0f)); | 35 EXPECT_EQ(4u, CountLeadingZeros32(0x0f0f0f0f)); |
30 } | 36 } |
31 | 37 |
32 | 38 |
33 TEST(BitsTest, CountTrailingZeros32) { | 39 TEST(Bits, CountTrailingZeros32) { |
34 EXPECT_EQ(32u, CountTrailingZeros32(0)); | 40 EXPECT_EQ(32u, CountTrailingZeros32(0)); |
35 EXPECT_EQ(31u, CountTrailingZeros32(0x80000000)); | 41 EXPECT_EQ(31u, CountTrailingZeros32(0x80000000)); |
36 TRACED_FORRANGE(uint32_t, shift, 0, 31) { | 42 TRACED_FORRANGE(uint32_t, shift, 0, 31) { |
37 EXPECT_EQ(shift, CountTrailingZeros32(1u << shift)); | 43 EXPECT_EQ(shift, CountTrailingZeros32(1u << shift)); |
38 } | 44 } |
39 EXPECT_EQ(4u, CountTrailingZeros32(0xf0f0f0f0)); | 45 EXPECT_EQ(4u, CountTrailingZeros32(0xf0f0f0f0)); |
40 } | 46 } |
41 | 47 |
42 | 48 |
43 TEST(BitsTest, RotateRight32) { | 49 TEST(Bits, IsPowerOfTwo32) { |
| 50 EXPECT_FALSE(IsPowerOfTwo32(0U)); |
| 51 TRACED_FORRANGE(uint32_t, shift, 0, 31) { |
| 52 EXPECT_TRUE(IsPowerOfTwo32(1U << shift)); |
| 53 EXPECT_FALSE(IsPowerOfTwo32((1U << shift) + 5U)); |
| 54 EXPECT_FALSE(IsPowerOfTwo32(~(1U << shift))); |
| 55 } |
| 56 TRACED_FORRANGE(uint32_t, shift, 2, 31) { |
| 57 EXPECT_FALSE(IsPowerOfTwo32((1U << shift) - 1U)); |
| 58 } |
| 59 EXPECT_FALSE(IsPowerOfTwo32(0xffffffff)); |
| 60 } |
| 61 |
| 62 |
| 63 TEST(Bits, IsPowerOfTwo64) { |
| 64 EXPECT_FALSE(IsPowerOfTwo64(0U)); |
| 65 TRACED_FORRANGE(uint32_t, shift, 0, 63) { |
| 66 EXPECT_TRUE(IsPowerOfTwo64(V8_UINT64_C(1) << shift)); |
| 67 EXPECT_FALSE(IsPowerOfTwo64((V8_UINT64_C(1) << shift) + 5U)); |
| 68 EXPECT_FALSE(IsPowerOfTwo64(~(V8_UINT64_C(1) << shift))); |
| 69 } |
| 70 TRACED_FORRANGE(uint32_t, shift, 2, 63) { |
| 71 EXPECT_FALSE(IsPowerOfTwo64((V8_UINT64_C(1) << shift) - 1U)); |
| 72 } |
| 73 EXPECT_FALSE(IsPowerOfTwo64(V8_UINT64_C(0xffffffffffffffff))); |
| 74 } |
| 75 |
| 76 |
| 77 TEST(Bits, RoundUpToPowerOfTwo32) { |
| 78 TRACED_FORRANGE(uint32_t, shift, 0, 31) { |
| 79 EXPECT_EQ(1u << shift, RoundUpToPowerOfTwo32(1u << shift)); |
| 80 } |
| 81 EXPECT_EQ(0u, RoundUpToPowerOfTwo32(0)); |
| 82 EXPECT_EQ(4u, RoundUpToPowerOfTwo32(3)); |
| 83 EXPECT_EQ(0x80000000u, RoundUpToPowerOfTwo32(0x7fffffffu)); |
| 84 } |
| 85 |
| 86 |
| 87 TEST(BitsDeathTest, DISABLE_IN_RELEASE(RoundUpToPowerOfTwo32)) { |
| 88 ASSERT_DEATH({ RoundUpToPowerOfTwo32(0x80000001u); }, "0x80000000"); |
| 89 } |
| 90 |
| 91 |
| 92 TEST(Bits, RoundDownToPowerOfTwo32) { |
| 93 TRACED_FORRANGE(uint32_t, shift, 0, 31) { |
| 94 EXPECT_EQ(1u << shift, RoundDownToPowerOfTwo32(1u << shift)); |
| 95 } |
| 96 EXPECT_EQ(0u, RoundDownToPowerOfTwo32(0)); |
| 97 EXPECT_EQ(4u, RoundDownToPowerOfTwo32(5)); |
| 98 EXPECT_EQ(0x80000000u, RoundDownToPowerOfTwo32(0x80000001u)); |
| 99 } |
| 100 |
| 101 |
| 102 TEST(Bits, RotateRight32) { |
44 TRACED_FORRANGE(uint32_t, shift, 0, 31) { | 103 TRACED_FORRANGE(uint32_t, shift, 0, 31) { |
45 EXPECT_EQ(0u, RotateRight32(0u, shift)); | 104 EXPECT_EQ(0u, RotateRight32(0u, shift)); |
46 } | 105 } |
47 EXPECT_EQ(1u, RotateRight32(1, 0)); | 106 EXPECT_EQ(1u, RotateRight32(1, 0)); |
48 EXPECT_EQ(1u, RotateRight32(2, 1)); | 107 EXPECT_EQ(1u, RotateRight32(2, 1)); |
49 EXPECT_EQ(0x80000000u, RotateRight32(1, 1)); | 108 EXPECT_EQ(0x80000000u, RotateRight32(1, 1)); |
50 } | 109 } |
51 | 110 |
52 | 111 |
53 TEST(BitsTest, RotateRight64) { | 112 TEST(Bits, RotateRight64) { |
54 TRACED_FORRANGE(uint64_t, shift, 0, 63) { | 113 TRACED_FORRANGE(uint64_t, shift, 0, 63) { |
55 EXPECT_EQ(0u, RotateRight64(0u, shift)); | 114 EXPECT_EQ(0u, RotateRight64(0u, shift)); |
56 } | 115 } |
57 EXPECT_EQ(1u, RotateRight64(1, 0)); | 116 EXPECT_EQ(1u, RotateRight64(1, 0)); |
58 EXPECT_EQ(1u, RotateRight64(2, 1)); | 117 EXPECT_EQ(1u, RotateRight64(2, 1)); |
59 EXPECT_EQ(V8_UINT64_C(0x8000000000000000), RotateRight64(1, 1)); | 118 EXPECT_EQ(V8_UINT64_C(0x8000000000000000), RotateRight64(1, 1)); |
60 } | 119 } |
61 | 120 |
62 } // namespace bits | 121 } // namespace bits |
63 } // namespace base | 122 } // namespace base |
64 } // namespace v8 | 123 } // namespace v8 |
OLD | NEW |