tests, base: Added GTests for base/intmath.cc
authorBobby R. Bruce <bbruce@ucdavis.edu>
Tue, 22 Oct 2019 21:35:29 +0000 (14:35 -0700)
committerMahyar Samani <msamani@ucdavis.edu>
Wed, 20 Nov 2019 00:52:01 +0000 (00:52 +0000)
Testing intmath.hh and intmath.cc. Here is the
list of the functions that are tested.
intmath.isPowerOf2, intmath.power, intmath.floorLog2,
intmath.ceilLog2, intmath.divCeil, intmath.roundUp,
intmath.roundDown. Other functions are not tested,
because they are not currently used and are dead code.

Change-Id: I150ac1b5cead93c6698a8c9e9cec80bd87ef181a
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/22081
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Mahyar Samani <msamani@ucdavis.edu>
Reviewed-by: Bobby R. Bruce <bbruce@ucdavis.edu>
Maintainer: Bobby R. Bruce <bbruce@ucdavis.edu>
Maintainer: Jason Lowe-Power <jason@lowepower.com>

src/base/SConscript
src/base/intmath.hh
src/base/intmath.test.cc [new file with mode: 0644]

index 1c2f80baf967c15cb9e31c2187dee384dc4a2cc5..a011463848c0e7189f52921e64b98670f350d79e 100644 (file)
@@ -58,6 +58,7 @@ Source('hostinfo.cc')
 Source('inet.cc')
 Source('inifile.cc')
 GTest('inifile.test', 'inifile.test.cc', 'inifile.cc', 'str.cc')
+GTest('intmath.test', 'intmath.test.cc')
 Source('logging.cc')
 Source('match.cc')
 GTest('match.test', 'match.test.cc', 'match.cc', 'str.cc')
index 449a49ca84b8387fa544f3be37d88fff1e9bff92..711260fc61b5de5f57be00a6f286ed2050544bd5 100644 (file)
@@ -134,6 +134,7 @@ template <class T>
 inline int
 ceilLog2(const T& n)
 {
+    assert(n > 0);
     if (n == 1)
         return 0;
 
diff --git a/src/base/intmath.test.cc b/src/base/intmath.test.cc
new file mode 100644 (file)
index 0000000..cd05186
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2019 The Regents of the University of California
+ * All rights reserved
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Bobby R. Bruce
+            Mahyar Samani
+ */
+
+#include <gtest/gtest.h>
+
+#include "base/intmath.hh"
+
+TEST(IntmathTest, isPowerOf2)
+{
+    EXPECT_TRUE(isPowerOf2(1));
+    EXPECT_TRUE(isPowerOf2(65536));
+    EXPECT_TRUE(isPowerOf2(131072));
+    EXPECT_TRUE(isPowerOf2(262144));
+    EXPECT_FALSE(isPowerOf2(0));
+    EXPECT_FALSE(isPowerOf2(2521));
+    EXPECT_FALSE(isPowerOf2(1679616));
+}
+
+TEST(IntmathTest, power)
+{
+    EXPECT_EQ(65536, power(2, 16));
+    EXPECT_EQ(9765625, power(5, 10));
+    EXPECT_EQ(43046721, power(power(3, 4), 4));
+}
+
+TEST(IntmathTest, floorLog2)
+{
+    EXPECT_EQ(0, floorLog2(1));
+    EXPECT_EQ(4, floorLog2(16));
+    EXPECT_EQ(4, floorLog2(31));
+    EXPECT_EQ(5, floorLog2(36));
+    EXPECT_EQ(8, floorLog2(436));
+    EXPECT_EQ(16, floorLog2(65537));
+    EXPECT_EQ(20, floorLog2(1783592));
+    EXPECT_EQ(41, floorLog2(2821109907456));
+}
+
+TEST(IntmathTest, ceilLog2)
+{
+    EXPECT_EQ(0, ceilLog2(1));
+    EXPECT_EQ(4, ceilLog2(16));
+    EXPECT_EQ(5, ceilLog2(31));
+    EXPECT_EQ(6, ceilLog2(36));
+    EXPECT_EQ(9, ceilLog2(436));
+    EXPECT_EQ(17, ceilLog2(65537));
+    EXPECT_EQ(21, ceilLog2(1783592));
+    EXPECT_EQ(42, ceilLog2(2821109907456));
+}
+
+
+TEST(IntmathTest, divCeil)
+{
+    EXPECT_EQ(5, divCeil(55, 13));
+    EXPECT_EQ(90, divCeil(7922, 89));
+    EXPECT_EQ(4, divCeil(4800, 1442));
+    EXPECT_EQ(4, divCeil(75, 24));
+    EXPECT_EQ(46, divCeil(451, 10));
+}
+
+TEST(IntmathTest, roundUp)
+{
+    EXPECT_EQ(4104, roundUp(4101, 4));
+    EXPECT_EQ(4112, roundUp(4105, 8));
+    EXPECT_EQ(4112, roundUp(4101, 16));
+    EXPECT_EQ(8192, roundUp(7991, 256));
+}
+
+TEST(IntmathTest, roundDown)
+{
+    EXPECT_EQ(4100, roundDown(4101, 4));
+    EXPECT_EQ(4104, roundDown(4105, 8));
+    EXPECT_EQ(4096, roundDown(4101, 16));
+    EXPECT_EQ(7936, roundDown(7991, 256));
+}