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" 8 #include "testing/gtest/include/gtest/gtest.h" 9 10 namespace base { 11 namespace bits { 12 13 TEST(BitsTest, Log2Floor) { 14 EXPECT_EQ(-1, Log2Floor(0)); 15 EXPECT_EQ(0, Log2Floor(1)); 16 EXPECT_EQ(1, Log2Floor(2)); 17 EXPECT_EQ(1, Log2Floor(3)); 18 EXPECT_EQ(2, Log2Floor(4)); 19 for (int i = 3; i < 31; ++i) { 20 unsigned int value = 1U << i; 21 EXPECT_EQ(i, Log2Floor(value)); 22 EXPECT_EQ(i, Log2Floor(value + 1)); 23 EXPECT_EQ(i, Log2Floor(value + 2)); 24 EXPECT_EQ(i - 1, Log2Floor(value - 1)); 25 EXPECT_EQ(i - 1, Log2Floor(value - 2)); 26 } 27 EXPECT_EQ(31, Log2Floor(0xffffffffU)); 28 } 29 30 TEST(BitsTest, Log2Ceiling) { 31 EXPECT_EQ(-1, Log2Ceiling(0)); 32 EXPECT_EQ(0, Log2Ceiling(1)); 33 EXPECT_EQ(1, Log2Ceiling(2)); 34 EXPECT_EQ(2, Log2Ceiling(3)); 35 EXPECT_EQ(2, Log2Ceiling(4)); 36 for (int i = 3; i < 31; ++i) { 37 unsigned int value = 1U << i; 38 EXPECT_EQ(i, Log2Ceiling(value)); 39 EXPECT_EQ(i + 1, Log2Ceiling(value + 1)); 40 EXPECT_EQ(i + 1, Log2Ceiling(value + 2)); 41 EXPECT_EQ(i, Log2Ceiling(value - 1)); 42 EXPECT_EQ(i, Log2Ceiling(value - 2)); 43 } 44 EXPECT_EQ(32, Log2Ceiling(0xffffffffU)); 45 } 46 47 } // namespace bits 48 } // namespace base 49