misc: Merge branch 'release-staging-v20.1.0.0' into develop
[gem5.git] / src / base / types.test.cc
1 /*
2 * Copyright (c) 2019 The Regents of the University of California
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include <gtest/gtest.h>
39
40 #include <sstream>
41
42 #include "base/types.hh"
43
44 /*
45 * The following test the Cycles class. Cycles is a wrapper for uint64_t.
46 * It overloads most commonly used operators.
47 */
48 TEST(CyclesTest, NoCycles)
49 {
50 Cycles cycles;
51 EXPECT_EQ(0, cycles);
52 }
53
54 TEST(CyclesTest, PrefixIncrement)
55 {
56 Cycles cycles(0);
57 EXPECT_EQ(1, ++cycles);
58 EXPECT_EQ(2, ++cycles);
59 EXPECT_EQ(2, cycles);
60 }
61
62
63 TEST(CyclesTest, PrefixDecrement)
64 {
65 Cycles cycles(10);
66 EXPECT_EQ(9, --cycles);
67 EXPECT_EQ(8, --cycles);
68 EXPECT_EQ(8, cycles);
69 }
70
71 TEST(CyclesTest, InPlaceAddition)
72 {
73 Cycles cycles(12);
74 Cycles to_add(5);
75 cycles += to_add;
76 EXPECT_EQ(17, cycles);
77 }
78
79 TEST(CyclesTest, GreaterThanLessThan)
80 {
81 Cycles one_cycle(1);
82 Cycles two_cycles(2);
83 EXPECT_TRUE(two_cycles > one_cycle);
84 EXPECT_TRUE(one_cycle < two_cycles);
85 }
86
87 TEST(CyclesTest, AddCycles)
88 {
89 Cycles cycles_1(10);
90 Cycles cycles_2(15);
91 Cycles added = cycles_1 + cycles_2;
92 EXPECT_EQ(25, added);
93 }
94
95 TEST(CyclesTest, SubtractCycles)
96 {
97 Cycles cycles_1(25);
98 Cycles cycles_2(1);
99 Cycles subtracted = cycles_1 - cycles_2;
100 EXPECT_EQ(24, subtracted);
101 }
102
103 TEST(CyclesTest, ShiftRight)
104 {
105 Cycles cycles(1ULL << 40);
106 Cycles cycles_shifted = cycles >> 5;
107 EXPECT_EQ((1ULL << 35), cycles_shifted);
108 }
109
110 TEST(CyclesTest, ShiftLeft)
111 {
112 Cycles cycles(1ULL << 40);
113 Cycles cycles_shifted = cycles << 20;
114 EXPECT_EQ((1ULL << 60), cycles_shifted);
115 }
116
117 TEST(CyclesTest, OutStream)
118 {
119 Cycles cycles(56);
120 std::ostringstream ss;
121 ss << "The number of cycles is: " << cycles << std::endl;
122 EXPECT_EQ("The number of cycles is: 56\n", ss.str());
123 }
124
125 /*
126 * MicroPCRomBit is a constant. This simple test verifies it has not changed.
127 * The following MicroPC tests rely heavily on this constant.
128 */
129 TEST(MicroPCTest, CheckMicroPCRomBit)
130 {
131 EXPECT_EQ((1 << 15), MicroPCRomBit);
132 }
133
134 TEST(MicroPCTest, RomMicroPCTest)
135 {
136 EXPECT_EQ(MicroPCRomBit + (1 << 8), romMicroPC(MicroPCRomBit + (1 << 8)));
137 }
138
139 TEST(MicroPCTest, NormalMicroPCTest)
140 {
141 EXPECT_EQ((1 << 8), normalMicroPC((1 << 8) + MicroPCRomBit));
142 }
143
144 TEST(MicroPCTest, IsRomMicroPCTest)
145 {
146 EXPECT_TRUE(isRomMicroPC(MicroPCRomBit + (1 << 8)));
147 }
148
149 TEST(MicroPCTest, IsNotRomMicroPCTest)
150 {
151 EXPECT_FALSE(isRomMicroPC((1 << 8)));
152 }
153
154 /*
155 * Both the "floatToBits32" and "floatToBits64" functions use the standard
156 * union approach to carry out type punning. These checks are simple regression
157 * tests.
158 */
159 TEST(TypesTest, FloatToBits32)
160 {
161 EXPECT_EQ(0x3e828f5c, floatToBits32(0.255));
162 }
163
164 TEST(TypesTest, floatToBits64)
165 {
166 EXPECT_EQ(0x3fd067dfe32a0664, floatToBits64(0.25634));
167 }
168
169 /*
170 * "floatToBits(double val)" and "floatToBits(float val)" are simple overloads
171 * for "floatToBits64" and "floatToBits32" respectively. Ergo, these tests
172 * check this is the case.
173 */
174 TEST(TypesTest, floatsToBitsDoubleInput)
175 {
176 double val = 0.84023;
177 EXPECT_EQ(floatToBits64(val), floatToBits(val));
178 }
179
180 TEST(TypesTest, floatsToBitsFloatInput)
181 {
182 float val = 0.04567;
183 EXPECT_EQ(floatToBits32(val), floatToBits(val));
184 }