apatrick@google.com | 0265b73 | 2009-11-19 08:08:39 +0900 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // This file contains the unit tests for the bit utilities. |
| 6 | |
| 7 | #include "base/bits.h" |
primiano | 47c6906 | 2015-07-25 05:13:32 +0900 | [diff] [blame] | 8 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 9 | #include <stddef.h> |
| 10 | |
primiano | 47c6906 | 2015-07-25 05:13:32 +0900 | [diff] [blame] | 11 | #include <limits> |
| 12 | |
apatrick@google.com | 0265b73 | 2009-11-19 08:08:39 +0900 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
| 15 | namespace base { |
| 16 | namespace bits { |
| 17 | |
| 18 | TEST(BitsTest, Log2Floor) { |
| 19 | EXPECT_EQ(-1, Log2Floor(0)); |
| 20 | EXPECT_EQ(0, Log2Floor(1)); |
| 21 | EXPECT_EQ(1, Log2Floor(2)); |
| 22 | EXPECT_EQ(1, Log2Floor(3)); |
| 23 | EXPECT_EQ(2, Log2Floor(4)); |
| 24 | for (int i = 3; i < 31; ++i) { |
| 25 | unsigned int value = 1U << i; |
| 26 | EXPECT_EQ(i, Log2Floor(value)); |
| 27 | EXPECT_EQ(i, Log2Floor(value + 1)); |
| 28 | EXPECT_EQ(i, Log2Floor(value + 2)); |
| 29 | EXPECT_EQ(i - 1, Log2Floor(value - 1)); |
| 30 | EXPECT_EQ(i - 1, Log2Floor(value - 2)); |
| 31 | } |
| 32 | EXPECT_EQ(31, Log2Floor(0xffffffffU)); |
| 33 | } |
| 34 | |
| 35 | TEST(BitsTest, Log2Ceiling) { |
| 36 | EXPECT_EQ(-1, Log2Ceiling(0)); |
| 37 | EXPECT_EQ(0, Log2Ceiling(1)); |
| 38 | EXPECT_EQ(1, Log2Ceiling(2)); |
| 39 | EXPECT_EQ(2, Log2Ceiling(3)); |
| 40 | EXPECT_EQ(2, Log2Ceiling(4)); |
| 41 | for (int i = 3; i < 31; ++i) { |
| 42 | unsigned int value = 1U << i; |
| 43 | EXPECT_EQ(i, Log2Ceiling(value)); |
| 44 | EXPECT_EQ(i + 1, Log2Ceiling(value + 1)); |
| 45 | EXPECT_EQ(i + 1, Log2Ceiling(value + 2)); |
| 46 | EXPECT_EQ(i, Log2Ceiling(value - 1)); |
| 47 | EXPECT_EQ(i, Log2Ceiling(value - 2)); |
| 48 | } |
| 49 | EXPECT_EQ(32, Log2Ceiling(0xffffffffU)); |
| 50 | } |
| 51 | |
primiano | 47c6906 | 2015-07-25 05:13:32 +0900 | [diff] [blame] | 52 | TEST(BitsTest, Align) { |
| 53 | const size_t kSizeTMax = std::numeric_limits<size_t>::max(); |
| 54 | EXPECT_EQ(0ul, Align(0, 4)); |
| 55 | EXPECT_EQ(4ul, Align(1, 4)); |
| 56 | EXPECT_EQ(4096ul, Align(1, 4096)); |
| 57 | EXPECT_EQ(4096ul, Align(4096, 4096)); |
| 58 | EXPECT_EQ(4096ul, Align(4095, 4096)); |
| 59 | EXPECT_EQ(8192ul, Align(4097, 4096)); |
| 60 | EXPECT_EQ(kSizeTMax - 31, Align(kSizeTMax - 62, 32)); |
| 61 | EXPECT_EQ(kSizeTMax / 2 + 1, Align(1, kSizeTMax / 2 + 1)); |
| 62 | } |
| 63 | |
Chris Palmer | ee842bf | 2016-11-19 06:21:23 +0900 | [diff] [blame] | 64 | TEST(BitsTest, CLZWorks) { |
| 65 | EXPECT_EQ(32u, CountLeadingZeroBits32(0u)); |
| 66 | EXPECT_EQ(31u, CountLeadingZeroBits32(1u)); |
| 67 | EXPECT_EQ(1u, CountLeadingZeroBits32(1u << 30)); |
| 68 | EXPECT_EQ(0u, CountLeadingZeroBits32(1u << 31)); |
| 69 | |
| 70 | #if defined(ARCH_CPU_64_BITS) |
| 71 | EXPECT_EQ(64u, CountLeadingZeroBitsSizeT(0ull)); |
| 72 | EXPECT_EQ(63u, CountLeadingZeroBitsSizeT(1ull)); |
| 73 | EXPECT_EQ(32u, CountLeadingZeroBitsSizeT(1ull << 31)); |
| 74 | EXPECT_EQ(1u, CountLeadingZeroBitsSizeT(1ull << 62)); |
| 75 | EXPECT_EQ(0u, CountLeadingZeroBitsSizeT(1ull << 63)); |
| 76 | #else |
| 77 | EXPECT_EQ(32u, CountLeadingZeroBitsSizeT(0u)); |
| 78 | EXPECT_EQ(31u, CountLeadingZeroBitsSizeT(1u)); |
| 79 | EXPECT_EQ(1u, CountLeadingZeroBitsSizeT(1u << 30)); |
| 80 | EXPECT_EQ(0u, CountLeadingZeroBitsSizeT(1u << 31)); |
| 81 | #endif |
| 82 | } |
| 83 | |
apatrick@google.com | 0265b73 | 2009-11-19 08:08:39 +0900 | [diff] [blame] | 84 | } // namespace bits |
| 85 | } // namespace base |