tests, base: Added GTests for base/intmath.cc
[gem5.git] / src / base / intmath.test.cc
1 /*
2 * Copyright (c) 2019 The Regents of the University of California
3 * All rights reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Bobby R. Bruce
29 Mahyar Samani
30 */
31
32 #include <gtest/gtest.h>
33
34 #include "base/intmath.hh"
35
36 TEST(IntmathTest, isPowerOf2)
37 {
38 EXPECT_TRUE(isPowerOf2(1));
39 EXPECT_TRUE(isPowerOf2(65536));
40 EXPECT_TRUE(isPowerOf2(131072));
41 EXPECT_TRUE(isPowerOf2(262144));
42 EXPECT_FALSE(isPowerOf2(0));
43 EXPECT_FALSE(isPowerOf2(2521));
44 EXPECT_FALSE(isPowerOf2(1679616));
45 }
46
47 TEST(IntmathTest, power)
48 {
49 EXPECT_EQ(65536, power(2, 16));
50 EXPECT_EQ(9765625, power(5, 10));
51 EXPECT_EQ(43046721, power(power(3, 4), 4));
52 }
53
54 TEST(IntmathTest, floorLog2)
55 {
56 EXPECT_EQ(0, floorLog2(1));
57 EXPECT_EQ(4, floorLog2(16));
58 EXPECT_EQ(4, floorLog2(31));
59 EXPECT_EQ(5, floorLog2(36));
60 EXPECT_EQ(8, floorLog2(436));
61 EXPECT_EQ(16, floorLog2(65537));
62 EXPECT_EQ(20, floorLog2(1783592));
63 EXPECT_EQ(41, floorLog2(2821109907456));
64 }
65
66 TEST(IntmathTest, ceilLog2)
67 {
68 EXPECT_EQ(0, ceilLog2(1));
69 EXPECT_EQ(4, ceilLog2(16));
70 EXPECT_EQ(5, ceilLog2(31));
71 EXPECT_EQ(6, ceilLog2(36));
72 EXPECT_EQ(9, ceilLog2(436));
73 EXPECT_EQ(17, ceilLog2(65537));
74 EXPECT_EQ(21, ceilLog2(1783592));
75 EXPECT_EQ(42, ceilLog2(2821109907456));
76 }
77
78
79 TEST(IntmathTest, divCeil)
80 {
81 EXPECT_EQ(5, divCeil(55, 13));
82 EXPECT_EQ(90, divCeil(7922, 89));
83 EXPECT_EQ(4, divCeil(4800, 1442));
84 EXPECT_EQ(4, divCeil(75, 24));
85 EXPECT_EQ(46, divCeil(451, 10));
86 }
87
88 TEST(IntmathTest, roundUp)
89 {
90 EXPECT_EQ(4104, roundUp(4101, 4));
91 EXPECT_EQ(4112, roundUp(4105, 8));
92 EXPECT_EQ(4112, roundUp(4101, 16));
93 EXPECT_EQ(8192, roundUp(7991, 256));
94 }
95
96 TEST(IntmathTest, roundDown)
97 {
98 EXPECT_EQ(4100, roundDown(4101, 4));
99 EXPECT_EQ(4104, roundDown(4105, 8));
100 EXPECT_EQ(4096, roundDown(4101, 16));
101 EXPECT_EQ(7936, roundDown(7991, 256));
102 }