log2_test.cc (1722B)
1 /* 2 * Copyright (c) 2018, Alliance for Open Media. All rights reserved. 3 * 4 * This source code is subject to the terms of the BSD 2 Clause License and 5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License 6 * was not distributed with this source code in the LICENSE file, you can 7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open 8 * Media Patent License 1.0 was not distributed with this source code in the 9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent. 10 */ 11 12 #include <limits.h> 13 #include <math.h> 14 15 #include "aom_ports/bitops.h" 16 #include "gtest/gtest.h" 17 18 TEST(Log2Test, GetMsb) { 19 // Test small numbers exhaustively. 20 for (unsigned int n = 1; n < 10000; n++) { 21 EXPECT_EQ(get_msb(n), static_cast<int>(floor(log2(n)))); 22 } 23 24 // Test every power of 2 and the two adjacent numbers. 25 for (int exponent = 2; exponent < 32; exponent++) { 26 const unsigned int power_of_2 = 1U << exponent; 27 EXPECT_EQ(get_msb(power_of_2 - 1), exponent - 1); 28 EXPECT_EQ(get_msb(power_of_2), exponent); 29 EXPECT_EQ(get_msb(power_of_2 + 1), exponent); 30 } 31 } 32 33 TEST(Log2Test, AomCeilLog2) { 34 // Test small numbers exhaustively. 35 EXPECT_EQ(aom_ceil_log2(0), 0); 36 for (int n = 1; n < 10000; n++) { 37 EXPECT_EQ(aom_ceil_log2(n), static_cast<int>(ceil(log2(n)))); 38 } 39 40 // Test every power of 2 and the two adjacent numbers. 41 for (int exponent = 2; exponent < 31; exponent++) { 42 const int power_of_2 = 1 << exponent; 43 EXPECT_EQ(aom_ceil_log2(power_of_2 - 1), exponent); 44 EXPECT_EQ(aom_ceil_log2(power_of_2), exponent); 45 EXPECT_EQ(aom_ceil_log2(power_of_2 + 1), exponent + 1); 46 } 47 48 // INT_MAX = 2^31 - 1 49 EXPECT_EQ(aom_ceil_log2(INT_MAX), 31); 50 }