summaryrefslogtreecommitdiff
path: root/src/base
diff options
context:
space:
mode:
authorGabe Black <gabeblack@google.com>2017-11-27 19:04:15 -0800
committerGabe Black <gabeblack@google.com>2017-11-28 11:00:32 +0000
commitd92e96732966ee48db3f8283367c0a1ffd84c1a7 (patch)
tree3ff6390986b66a79fb6804f8911f4944db69cb34 /src/base
parent60e165116b7785bc612d8ef5c9d22af7fe84e92a (diff)
downloadgem5-d92e96732966ee48db3f8283367c0a1ffd84c1a7.tar.xz
tests: Move the bituniontest to be alongside the bitunion header.
Change-Id: I7c1a49c41672a1108fcf67c5505b0441f90588ef Reviewed-on: https://gem5-review.googlesource.com/6142 Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com> Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>
Diffstat (limited to 'src/base')
-rw-r--r--src/base/SConscript2
-rw-r--r--src/base/bituniontest.cc194
2 files changed, 196 insertions, 0 deletions
diff --git a/src/base/SConscript b/src/base/SConscript
index 7aa93f504..13c1ffff5 100644
--- a/src/base/SConscript
+++ b/src/base/SConscript
@@ -76,6 +76,8 @@ Source('loader/symtab.cc')
Source('stats/text.cc')
+GTest('bituniontest', 'bituniontest.cc')
+
DebugFlag('Annotate', "State machine annotation debugging")
DebugFlag('AnnotateQ', "State machine annotation queue debugging")
DebugFlag('AnnotateVerbose', "Dump all state machine annotation details")
diff --git a/src/base/bituniontest.cc b/src/base/bituniontest.cc
new file mode 100644
index 000000000..1f24e7e66
--- /dev/null
+++ b/src/base/bituniontest.cc
@@ -0,0 +1,194 @@
+/*
+ * Copyright 2014 Google, Inc.
+ *
+ * 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: Gabe Black
+ */
+
+#include <gtest/gtest.h>
+
+#include <cassert>
+#include <iostream>
+
+#include "base/bitunion.hh"
+#include "base/cprintf.hh"
+
+using namespace std;
+
+namespace {
+
+BitUnion64(SixtyFour)
+ Bitfield<39, 32> byte5;
+ Bitfield<2> bit2;
+ BitfieldRO<39, 32> byte5RO;
+ BitfieldWO<39, 32> byte5WO;
+ SubBitUnion(byte6, 47, 40)
+ Bitfield<43, 42> bits43To42;
+ Bitfield<41> bit41;
+ SignedBitfield<41> bit41Signed;
+ EndSubBitUnion(byte6)
+ SignedBitfield<47, 40> byte6Signed;
+ SignedBitfieldRO<47, 40> byte6SignedRO;
+ SignedBitfieldWO<47, 40> byte6SignedWO;
+EndBitUnion(SixtyFour)
+
+BitUnion64(EmptySixtyFour)
+EndBitUnion(EmptySixtyFour)
+
+BitUnion32(EmptyThirtyTwo)
+EndBitUnion(EmptyThirtyTwo)
+
+BitUnion16(EmptySixteen)
+EndBitUnion(EmptySixteen)
+
+BitUnion8(EmptyEight)
+EndBitUnion(EmptyEight)
+
+struct ContainingStruct
+{
+ BitUnion64(Contained)
+ Bitfield<63, 60> topNibble;
+ EndBitUnion(Contained)
+
+ Contained contained;
+};
+
+uint64_t
+containingFunc(uint64_t init_val, uint64_t fieldVal)
+{
+ BitUnion32(Contained)
+ Bitfield<16, 15> field;
+ EndBitUnion(Contained)
+
+ Contained contained = init_val;
+ contained.field = fieldVal;
+ return contained;
+}
+
+} // anonymous namespace
+
+// Declare these as global so g++ doesn't ignore them. Initialize them in
+// various ways.
+EmptySixtyFour emptySixtyFour = 0;
+EmptyThirtyTwo emptyThirtyTwo;
+EmptySixteen emptySixteen;
+EmptyEight emptyEight(0);
+
+class BitUnionData : public testing::Test {
+ protected:
+ SixtyFour sixtyFour;
+
+ void SetUp() override { sixtyFour = 0; }
+};
+
+TEST_F(BitUnionData, NormalBitfield)
+{
+ EXPECT_EQ(sixtyFour.byte5, 0);
+ sixtyFour.byte5 = 0xff;
+ EXPECT_EQ(sixtyFour, 0xff00000000);
+ sixtyFour.byte5 = 0xfff;
+ EXPECT_EQ(sixtyFour, 0xff00000000);
+ EXPECT_EQ(sixtyFour.byte5, 0xff);
+}
+
+TEST_F(BitUnionData, SingleBitfield)
+{
+ EXPECT_EQ(sixtyFour.bit2, 0);
+ sixtyFour.bit2 = 0x1;
+ EXPECT_EQ(sixtyFour, 0x4);
+ EXPECT_EQ(sixtyFour.bit2, 0x1);
+}
+
+TEST_F(BitUnionData, ReadOnlyBitfield)
+{
+ EXPECT_EQ(sixtyFour.byte5RO, 0);
+ sixtyFour.byte5 = 0xff;
+ EXPECT_EQ(sixtyFour.byte5RO, 0xff);
+}
+
+TEST_F(BitUnionData, WriteOnlyBitfield)
+{
+ sixtyFour.byte5WO = 0xff;
+ EXPECT_EQ(sixtyFour, 0xff00000000);
+}
+
+TEST_F(BitUnionData, SubBitUnions)
+{
+ EXPECT_EQ(sixtyFour.byte6.bit41, 0);
+ sixtyFour.byte6 = 0x2;
+ EXPECT_EQ(sixtyFour.byte6.bit41, 1);
+ sixtyFour.byte6.bits43To42 = 0x3;
+ EXPECT_EQ(sixtyFour.byte6, 0xe);
+ sixtyFour.byte6 = 0xff;
+ sixtyFour.byte6.bit41 = 0;
+ EXPECT_EQ(sixtyFour, 0xfd0000000000);
+}
+
+TEST_F(BitUnionData, SignedBitfields)
+{
+ sixtyFour.byte6 = 0xff;
+ EXPECT_EQ(sixtyFour.byte6Signed, -1);
+ EXPECT_EQ(sixtyFour.byte6SignedRO, -1);
+ sixtyFour.byte6SignedWO = 0;
+ EXPECT_EQ(sixtyFour.byte6Signed, 0);
+ EXPECT_EQ(sixtyFour.byte6SignedRO, 0);
+ EXPECT_EQ(sixtyFour.byte6, 0);
+}
+
+TEST_F(BitUnionData, InsideStruct)
+{
+ ContainingStruct containing;
+ containing.contained = 0;
+ containing.contained.topNibble = 0xd;
+ EXPECT_EQ(containing.contained, 0xd000000000000000);
+}
+
+TEST_F(BitUnionData, InsideFunction)
+{
+ EXPECT_EQ(containingFunc(0xfffff, 0), 0xe7fff);
+}
+
+TEST_F(BitUnionData, BitfieldToBitfieldAssignment)
+{
+ SixtyFour otherSixtyFour = 0;
+ sixtyFour.bit2 = 1;
+ otherSixtyFour.byte6.bit41 = sixtyFour.bit2;
+ EXPECT_EQ(otherSixtyFour, 0x20000000000);
+ otherSixtyFour.bit2 = sixtyFour.bit2;
+ EXPECT_EQ(otherSixtyFour, 0x20000000004);
+}
+
+TEST_F(BitUnionData, Operators)
+{
+ SixtyFour otherSixtyFour = 0x4;
+ sixtyFour = otherSixtyFour;
+ EXPECT_EQ(sixtyFour, 0x4);
+ sixtyFour = 0;
+ EXPECT_TRUE(sixtyFour < otherSixtyFour);
+ EXPECT_TRUE(otherSixtyFour > sixtyFour);
+ EXPECT_TRUE(sixtyFour != otherSixtyFour);
+ sixtyFour = otherSixtyFour;
+ EXPECT_TRUE(sixtyFour == otherSixtyFour);
+}