blob: 270b8ef7d3b472557c0a1840e57a6f46c3d52b5d [file] [log] [blame]
apatrick@google.com0265b732009-11-19 08:08:39 +09001// 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"
primiano47c69062015-07-25 05:13:32 +09008
avia6a6a682015-12-27 07:15:14 +09009#include <stddef.h>
10
primiano47c69062015-07-25 05:13:32 +090011#include <limits>
12
apatrick@google.com0265b732009-11-19 08:08:39 +090013#include "testing/gtest/include/gtest/gtest.h"
14
15namespace base {
16namespace bits {
17
18TEST(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
35TEST(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
primiano47c69062015-07-25 05:13:32 +090052TEST(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 Palmeree842bf2016-11-19 06:21:23 +090064TEST(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.com0265b732009-11-19 08:08:39 +090084} // namespace bits
85} // namespace base