OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 // This file contains the unit tests for the bit utilities. | 5 // This file contains the unit tests for the bit utilities. |
6 | 6 |
7 #include "base/bits.h" | 7 #include "base/bits.h" |
8 | |
9 #include <limits> | |
10 | |
8 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
9 | 12 |
10 namespace base { | 13 namespace base { |
11 namespace bits { | 14 namespace bits { |
12 | 15 |
13 TEST(BitsTest, Log2Floor) { | 16 TEST(BitsTest, Log2Floor) { |
14 EXPECT_EQ(-1, Log2Floor(0)); | 17 EXPECT_EQ(-1, Log2Floor(0)); |
15 EXPECT_EQ(0, Log2Floor(1)); | 18 EXPECT_EQ(0, Log2Floor(1)); |
16 EXPECT_EQ(1, Log2Floor(2)); | 19 EXPECT_EQ(1, Log2Floor(2)); |
17 EXPECT_EQ(1, Log2Floor(3)); | 20 EXPECT_EQ(1, Log2Floor(3)); |
(...skipping 19 matching lines...) Expand all Loading... | |
37 unsigned int value = 1U << i; | 40 unsigned int value = 1U << i; |
38 EXPECT_EQ(i, Log2Ceiling(value)); | 41 EXPECT_EQ(i, Log2Ceiling(value)); |
39 EXPECT_EQ(i + 1, Log2Ceiling(value + 1)); | 42 EXPECT_EQ(i + 1, Log2Ceiling(value + 1)); |
40 EXPECT_EQ(i + 1, Log2Ceiling(value + 2)); | 43 EXPECT_EQ(i + 1, Log2Ceiling(value + 2)); |
41 EXPECT_EQ(i, Log2Ceiling(value - 1)); | 44 EXPECT_EQ(i, Log2Ceiling(value - 1)); |
42 EXPECT_EQ(i, Log2Ceiling(value - 2)); | 45 EXPECT_EQ(i, Log2Ceiling(value - 2)); |
43 } | 46 } |
44 EXPECT_EQ(32, Log2Ceiling(0xffffffffU)); | 47 EXPECT_EQ(32, Log2Ceiling(0xffffffffU)); |
45 } | 48 } |
46 | 49 |
50 TEST(BitsTest, Align) { | |
Lei Zhang
2015/07/24 18:59:55
Thanks!
| |
51 const size_t kSizeTMax = std::numeric_limits<size_t>::max(); | |
52 EXPECT_EQ(0ul, Align(0, 4)); | |
53 EXPECT_EQ(4ul, Align(1, 4)); | |
54 EXPECT_EQ(4096ul, Align(1, 4096)); | |
55 EXPECT_EQ(4096ul, Align(4096, 4096)); | |
56 EXPECT_EQ(4096ul, Align(4095, 4096)); | |
57 EXPECT_EQ(8192ul, Align(4097, 4096)); | |
58 EXPECT_EQ(kSizeTMax - 31, Align(kSizeTMax - 62, 32)); | |
59 EXPECT_EQ(kSizeTMax / 2 + 1, Align(1, kSizeTMax / 2 + 1)); | |
60 } | |
61 | |
47 } // namespace bits | 62 } // namespace bits |
48 } // namespace base | 63 } // namespace base |
OLD | NEW |