2 * Copyright (c) 2019 The Regents of the University of California
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.
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.
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.
38 #include <gtest/gtest.h>
42 #include "base/types.hh"
45 * The following test the Cycles class. Cycles is a wrapper for uint64_t.
46 * It overloads most commonly used operators.
48 TEST(CyclesTest
, NoCycles
)
54 TEST(CyclesTest
, PrefixIncrement
)
57 EXPECT_EQ(1, ++cycles
);
58 EXPECT_EQ(2, ++cycles
);
63 TEST(CyclesTest
, PrefixDecrement
)
66 EXPECT_EQ(9, --cycles
);
67 EXPECT_EQ(8, --cycles
);
71 TEST(CyclesTest
, InPlaceAddition
)
76 EXPECT_EQ(17, cycles
);
79 TEST(CyclesTest
, GreaterThanLessThan
)
83 EXPECT_TRUE(two_cycles
> one_cycle
);
84 EXPECT_TRUE(one_cycle
< two_cycles
);
87 TEST(CyclesTest
, AddCycles
)
91 Cycles added
= cycles_1
+ cycles_2
;
95 TEST(CyclesTest
, SubtractCycles
)
99 Cycles subtracted
= cycles_1
- cycles_2
;
100 EXPECT_EQ(24, subtracted
);
103 TEST(CyclesTest
, ShiftRight
)
105 Cycles
cycles(1ULL << 40);
106 Cycles cycles_shifted
= cycles
>> 5;
107 EXPECT_EQ((1ULL << 35), cycles_shifted
);
110 TEST(CyclesTest
, ShiftLeft
)
112 Cycles
cycles(1ULL << 40);
113 Cycles cycles_shifted
= cycles
<< 20;
114 EXPECT_EQ((1ULL << 60), cycles_shifted
);
117 TEST(CyclesTest
, OutStream
)
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());
126 * MicroPCRomBit is a constant. This simple test verifies it has not changed.
127 * The following MicroPC tests rely heavily on this constant.
129 TEST(MicroPCTest
, CheckMicroPCRomBit
)
131 EXPECT_EQ((1 << 15), MicroPCRomBit
);
134 TEST(MicroPCTest
, RomMicroPCTest
)
136 EXPECT_EQ(MicroPCRomBit
+ (1 << 8), romMicroPC(MicroPCRomBit
+ (1 << 8)));
139 TEST(MicroPCTest
, NormalMicroPCTest
)
141 EXPECT_EQ((1 << 8), normalMicroPC((1 << 8) + MicroPCRomBit
));
144 TEST(MicroPCTest
, IsRomMicroPCTest
)
146 EXPECT_TRUE(isRomMicroPC(MicroPCRomBit
+ (1 << 8)));
149 TEST(MicroPCTest
, IsNotRomMicroPCTest
)
151 EXPECT_FALSE(isRomMicroPC((1 << 8)));
155 * Both the "floatToBits32" and "floatToBits64" functions use the standard
156 * union approach to carry out type punning. These checks are simple regression
159 TEST(TypesTest
, FloatToBits32
)
161 EXPECT_EQ(0x3e828f5c, floatToBits32(0.255));
164 TEST(TypesTest
, floatToBits64
)
166 EXPECT_EQ(0x3fd067dfe32a0664, floatToBits64(0.25634));
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.
174 TEST(TypesTest
, floatsToBitsDoubleInput
)
176 double val
= 0.84023;
177 EXPECT_EQ(floatToBits64(val
), floatToBits(val
));
180 TEST(TypesTest
, floatsToBitsFloatInput
)
183 EXPECT_EQ(floatToBits32(val
), floatToBits(val
));