gem5  v21.1.0.2
types.test.cc
Go to the documentation of this file.
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 using namespace gem5;
45 
46 /*
47  * The following test the Cycles class. Cycles is a wrapper for uint64_t.
48  * It overloads most commonly used operators.
49  */
50 TEST(CyclesTest, NoCycles)
51 {
52  Cycles cycles;
53  EXPECT_EQ(0, cycles);
54 }
55 
56 TEST(CyclesTest, PrefixIncrement)
57 {
58  Cycles cycles(0);
59  EXPECT_EQ(1, ++cycles);
60  EXPECT_EQ(2, ++cycles);
61  EXPECT_EQ(2, cycles);
62 }
63 
64 
65 TEST(CyclesTest, PrefixDecrement)
66 {
67  Cycles cycles(10);
68  EXPECT_EQ(9, --cycles);
69  EXPECT_EQ(8, --cycles);
70  EXPECT_EQ(8, cycles);
71 }
72 
73 TEST(CyclesTest, InPlaceAddition)
74 {
75  Cycles cycles(12);
76  Cycles to_add(5);
77  cycles += to_add;
78  EXPECT_EQ(17, cycles);
79 }
80 
81 TEST(CyclesTest, GreaterThanLessThan)
82 {
83  Cycles one_cycle(1);
84  Cycles two_cycles(2);
85  EXPECT_TRUE(two_cycles > one_cycle);
86  EXPECT_TRUE(one_cycle < two_cycles);
87 }
88 
89 TEST(CyclesTest, AddCycles)
90 {
91  Cycles cycles_1(10);
92  Cycles cycles_2(15);
93  Cycles added = cycles_1 + cycles_2;
94  EXPECT_EQ(25, added);
95 }
96 
97 TEST(CyclesTest, SubtractCycles)
98 {
99  Cycles cycles_1(25);
100  Cycles cycles_2(1);
101  Cycles subtracted = cycles_1 - cycles_2;
102  EXPECT_EQ(24, subtracted);
103 }
104 
105 TEST(CyclesTest, ShiftRight)
106 {
107  Cycles cycles(1ULL << 40);
108  Cycles cycles_shifted = cycles >> 5;
109  EXPECT_EQ((1ULL << 35), cycles_shifted);
110 }
111 
112 TEST(CyclesTest, ShiftLeft)
113 {
114  Cycles cycles(1ULL << 40);
115  Cycles cycles_shifted = cycles << 20;
116  EXPECT_EQ((1ULL << 60), cycles_shifted);
117 }
118 
119 TEST(CyclesTest, OutStream)
120 {
121  Cycles cycles(56);
122  std::ostringstream ss;
123  ss << "The number of cycles is: " << cycles << std::endl;
124  EXPECT_EQ("The number of cycles is: 56\n", ss.str());
125 }
126 
127 /*
128  * MicroPCRomBit is a constant. This simple test verifies it has not changed.
129  * The following MicroPC tests rely heavily on this constant.
130  */
131 TEST(MicroPCTest, CheckMicroPCRomBit)
132 {
133  EXPECT_EQ((1 << 15), MicroPCRomBit);
134 }
135 
136 TEST(MicroPCTest, RomMicroPCTest)
137 {
138  EXPECT_EQ(MicroPCRomBit + (1 << 8), romMicroPC(MicroPCRomBit + (1 << 8)));
139 }
140 
141 TEST(MicroPCTest, NormalMicroPCTest)
142 {
143  EXPECT_EQ((1 << 8), normalMicroPC((1 << 8) + MicroPCRomBit));
144 }
145 
146 TEST(MicroPCTest, IsRomMicroPCTest)
147 {
148  EXPECT_TRUE(isRomMicroPC(MicroPCRomBit + (1 << 8)));
149 }
150 
151 TEST(MicroPCTest, IsNotRomMicroPCTest)
152 {
153  EXPECT_FALSE(isRomMicroPC((1 << 8)));
154 }
155 
156 /*
157  * Both the "floatToBits32" and "floatToBits64" functions use the standard
158  * union approach to carry out type punning. These checks are simple regression
159  * tests.
160  */
161 TEST(TypesTest, FloatToBits32)
162 {
163  EXPECT_EQ(0x3e828f5c, floatToBits32(0.255));
164 }
165 
166 TEST(TypesTest, floatToBits64)
167 {
168  EXPECT_EQ(0x3fd067dfe32a0664, floatToBits64(0.25634));
169 }
170 
171 /*
172  * "floatToBits(double val)" and "floatToBits(float val)" are simple overloads
173  * for "floatToBits64" and "floatToBits32" respectively. Ergo, these tests
174  * check this is the case.
175  */
176 TEST(TypesTest, floatsToBitsDoubleInput)
177 {
178  double val = 0.84023;
179  EXPECT_EQ(floatToBits64(val), floatToBits(val));
180 }
181 
182 TEST(TypesTest, floatsToBitsFloatInput)
183 {
184  float val = 0.04567;
185  EXPECT_EQ(floatToBits32(val), floatToBits(val));
186 }
gem5::floatToBits32
static uint32_t floatToBits32(float val)
Definition: types.hh:186
gem5::normalMicroPC
static MicroPC normalMicroPC(MicroPC upc)
Definition: types.hh:160
gem5::isRomMicroPC
static bool isRomMicroPC(MicroPC upc)
Definition: types.hh:166
gem5::X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:775
TEST
TEST(CyclesTest, NoCycles)
Definition: types.test.cc:50
gem5::Cycles
Cycles is a wrapper class for representing cycle counts, i.e.
Definition: types.hh:78
gem5::floatToBits
static uint64_t floatToBits(double val)
Definition: types.hh:209
ss
std::stringstream ss
Definition: trace.test.cc:45
gem5::floatToBits64
static uint64_t floatToBits64(double val)
Definition: types.hh:198
types.hh
gem5::romMicroPC
static MicroPC romMicroPC(MicroPC upc)
Definition: types.hh:154
gem5::MicroPCRomBit
static const MicroPC MicroPCRomBit
Definition: types.hh:151
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40

Generated on Tue Sep 21 2021 12:25:00 for gem5 by doxygen 1.8.17