ruby: Refactor some Event subclasses to lambdas
[gem5.git] / src / unittest / bituniontest.cc
1 /*
2 * Copyright 2014 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30 #include <cassert>
31 #include <iostream>
32
33 #include "base/bitunion.hh"
34 #include "base/cprintf.hh"
35 #include "unittest/unittest.hh"
36
37 using namespace std;
38 using UnitTest::setCase;
39
40 namespace {
41
42 BitUnion64(SixtyFour)
43 Bitfield<39, 32> byte5;
44 Bitfield<2> bit2;
45 BitfieldRO<39, 32> byte5RO;
46 BitfieldWO<39, 32> byte5WO;
47 SubBitUnion(byte6, 47, 40)
48 Bitfield<43, 42> bits43To42;
49 Bitfield<41> bit41;
50 SignedBitfield<41> bit41Signed;
51 EndSubBitUnion(byte6)
52 SignedBitfield<47, 40> byte6Signed;
53 SignedBitfieldRO<47, 40> byte6SignedRO;
54 SignedBitfieldWO<47, 40> byte6SignedWO;
55 EndBitUnion(SixtyFour)
56
57 BitUnion64(EmptySixtyFour)
58 EndBitUnion(EmptySixtyFour)
59
60 BitUnion32(EmptyThirtyTwo)
61 EndBitUnion(EmptyThirtyTwo)
62
63 BitUnion16(EmptySixteen)
64 EndBitUnion(EmptySixteen)
65
66 BitUnion8(EmptyEight)
67 EndBitUnion(EmptyEight)
68
69 struct ContainingStruct
70 {
71 BitUnion64(Contained)
72 Bitfield<63, 60> topNibble;
73 EndBitUnion(Contained)
74
75 Contained contained;
76 };
77
78 uint64_t
79 containingFunc(uint64_t init_val, uint64_t fieldVal)
80 {
81 BitUnion32(Contained)
82 Bitfield<16, 15> field;
83 EndBitUnion(Contained)
84
85 Contained contained = init_val;
86 contained.field = fieldVal;
87 return contained;
88 }
89
90 } // anonymous namespace
91
92 // Declare these as global so g++ doesn't ignore them. Initialize them in
93 // various ways.
94 EmptySixtyFour emptySixtyFour = 0;
95 EmptyThirtyTwo emptyThirtyTwo;
96 EmptySixteen emptySixteen;
97 EmptyEight emptyEight(0);
98
99 int
100 main()
101 {
102 SixtyFour sixtyFour = 0;
103
104 setCase("normal bitfield");
105 EXPECT_EQ(sixtyFour.byte5, 0);
106 sixtyFour.byte5 = 0xff;
107 EXPECT_EQ(sixtyFour, 0xff00000000);
108 sixtyFour.byte5 = 0xfff;
109 EXPECT_EQ(sixtyFour, 0xff00000000);
110 EXPECT_EQ(sixtyFour.byte5, 0xff);
111 sixtyFour = 0;
112
113 setCase("single bitfield");
114 EXPECT_EQ(sixtyFour.bit2, 0);
115 sixtyFour.bit2 = 0x1;
116 EXPECT_EQ(sixtyFour, 0x4);
117 EXPECT_EQ(sixtyFour.bit2, 0x1);
118 sixtyFour = 0;
119
120 setCase("read only bitfield");
121 EXPECT_EQ(sixtyFour.byte5RO, 0);
122 sixtyFour.byte5 = 0xff;
123 EXPECT_EQ(sixtyFour.byte5RO, 0xff);
124 sixtyFour = 0;
125
126 setCase("write only bitfield");
127 sixtyFour.byte5WO = 0xff;
128 EXPECT_EQ(sixtyFour, 0xff00000000);
129 sixtyFour = 0;
130
131 setCase("sub bitunions and their bitfields");
132 EXPECT_EQ(sixtyFour.byte6.bit41, 0);
133 sixtyFour.byte6 = 0x2;
134 EXPECT_EQ(sixtyFour.byte6.bit41, 1);
135 sixtyFour.byte6.bits43To42 = 0x3;
136 EXPECT_EQ(sixtyFour.byte6, 0xe);
137 sixtyFour.byte6 = 0xff;
138 sixtyFour.byte6.bit41 = 0;
139 EXPECT_EQ(sixtyFour, 0xfd0000000000);
140 sixtyFour = 0;
141
142 setCase("normal, read only, and write only signed bitfields");
143 sixtyFour.byte6 = 0xff;
144 EXPECT_EQ(sixtyFour.byte6Signed, -1);
145 EXPECT_EQ(sixtyFour.byte6SignedRO, -1);
146 sixtyFour.byte6SignedWO = 0;
147 EXPECT_EQ(sixtyFour.byte6Signed, 0);
148 EXPECT_EQ(sixtyFour.byte6SignedRO, 0);
149 EXPECT_EQ(sixtyFour.byte6, 0);
150 sixtyFour = 0;
151
152 setCase("bitunion declared inside a struct");
153 ContainingStruct containing;
154 containing.contained = 0;
155 containing.contained.topNibble = 0xd;
156 EXPECT_EQ(containing.contained, 0xd000000000000000);
157
158 setCase("bitunion declared inside a function");
159 EXPECT_EQ(containingFunc(0xfffff, 0), 0xe7fff);
160
161 setCase("assigning bitfields to other bitfields");
162 SixtyFour otherSixtyFour = 0;
163 sixtyFour.bit2 = 1;
164 otherSixtyFour.byte6.bit41 = sixtyFour.bit2;
165 EXPECT_EQ(otherSixtyFour, 0x20000000000);
166 otherSixtyFour.bit2 = sixtyFour.bit2;
167 EXPECT_EQ(otherSixtyFour, 0x20000000004);
168
169 setCase("bitunion operators");
170 sixtyFour = 0;
171 otherSixtyFour = 0x4;
172 sixtyFour = otherSixtyFour;
173 EXPECT_EQ(sixtyFour, 0x4);
174 sixtyFour = 0;
175 EXPECT_TRUE(sixtyFour < otherSixtyFour);
176 EXPECT_TRUE(otherSixtyFour > sixtyFour);
177 EXPECT_TRUE(sixtyFour != otherSixtyFour);
178 sixtyFour = otherSixtyFour;
179 EXPECT_TRUE(sixtyFour == otherSixtyFour);
180
181 return UnitTest::printResults();
182 }