Storing a set of PATypeHolders is a bad idea because their sort order will
[llvm.git] / unittests / ADT / APIntTest.cpp
blob5de7d08b8993278e2554c63ca8c93fabb6f2231d
1 //===- llvm/unittest/ADT/APInt.cpp - APInt unit tests ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include <ostream>
11 #include "gtest/gtest.h"
12 #include "llvm/ADT/APInt.h"
13 #include "llvm/ADT/SmallString.h"
15 using namespace llvm;
17 namespace {
19 // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
20 TEST(APIntTest, ShiftLeftByZero) {
21 APInt One = APInt::getNullValue(65) + 1;
22 APInt Shl = One.shl(0);
23 EXPECT_EQ(true, Shl[0]);
24 EXPECT_EQ(false, Shl[1]);
27 TEST(APIntTest, i128_NegativeCount) {
28 APInt Minus3(128, static_cast<uint64_t>(-3), true);
29 EXPECT_EQ(126u, Minus3.countLeadingOnes());
30 EXPECT_EQ(-3, Minus3.getSExtValue());
32 APInt Minus1(128, static_cast<uint64_t>(-1), true);
33 EXPECT_EQ(0u, Minus1.countLeadingZeros());
34 EXPECT_EQ(128u, Minus1.countLeadingOnes());
35 EXPECT_EQ(128u, Minus1.getActiveBits());
36 EXPECT_EQ(0u, Minus1.countTrailingZeros());
37 EXPECT_EQ(128u, Minus1.countTrailingOnes());
38 EXPECT_EQ(128u, Minus1.countPopulation());
39 EXPECT_EQ(-1, Minus1.getSExtValue());
42 TEST(APIntTest, i33_Count) {
43 APInt i33minus2(33, static_cast<uint64_t>(-2), true);
44 EXPECT_EQ(0u, i33minus2.countLeadingZeros());
45 EXPECT_EQ(32u, i33minus2.countLeadingOnes());
46 EXPECT_EQ(33u, i33minus2.getActiveBits());
47 EXPECT_EQ(1u, i33minus2.countTrailingZeros());
48 EXPECT_EQ(32u, i33minus2.countPopulation());
49 EXPECT_EQ(-2, i33minus2.getSExtValue());
50 EXPECT_EQ(((uint64_t)-2)&((1ull<<33) -1), i33minus2.getZExtValue());
53 TEST(APIntTest, i65_Count) {
54 APInt i65minus(65, 0, true);
55 i65minus.set(64);
56 EXPECT_EQ(0u, i65minus.countLeadingZeros());
57 EXPECT_EQ(1u, i65minus.countLeadingOnes());
58 EXPECT_EQ(65u, i65minus.getActiveBits());
59 EXPECT_EQ(64u, i65minus.countTrailingZeros());
60 EXPECT_EQ(1u, i65minus.countPopulation());
63 TEST(APIntTest, i128_PositiveCount) {
64 APInt u128max = APInt::getAllOnesValue(128);
65 EXPECT_EQ(128u, u128max.countLeadingOnes());
66 EXPECT_EQ(0u, u128max.countLeadingZeros());
67 EXPECT_EQ(128u, u128max.getActiveBits());
68 EXPECT_EQ(0u, u128max.countTrailingZeros());
69 EXPECT_EQ(128u, u128max.countTrailingOnes());
70 EXPECT_EQ(128u, u128max.countPopulation());
72 APInt u64max(128, static_cast<uint64_t>(-1), false);
73 EXPECT_EQ(64u, u64max.countLeadingZeros());
74 EXPECT_EQ(0u, u64max.countLeadingOnes());
75 EXPECT_EQ(64u, u64max.getActiveBits());
76 EXPECT_EQ(0u, u64max.countTrailingZeros());
77 EXPECT_EQ(64u, u64max.countTrailingOnes());
78 EXPECT_EQ(64u, u64max.countPopulation());
79 EXPECT_EQ((uint64_t)~0ull, u64max.getZExtValue());
81 APInt zero(128, 0, true);
82 EXPECT_EQ(128u, zero.countLeadingZeros());
83 EXPECT_EQ(0u, zero.countLeadingOnes());
84 EXPECT_EQ(0u, zero.getActiveBits());
85 EXPECT_EQ(128u, zero.countTrailingZeros());
86 EXPECT_EQ(0u, zero.countTrailingOnes());
87 EXPECT_EQ(0u, zero.countPopulation());
88 EXPECT_EQ(0u, zero.getSExtValue());
89 EXPECT_EQ(0u, zero.getZExtValue());
91 APInt one(128, 1, true);
92 EXPECT_EQ(127u, one.countLeadingZeros());
93 EXPECT_EQ(0u, one.countLeadingOnes());
94 EXPECT_EQ(1u, one.getActiveBits());
95 EXPECT_EQ(0u, one.countTrailingZeros());
96 EXPECT_EQ(1u, one.countTrailingOnes());
97 EXPECT_EQ(1u, one.countPopulation());
98 EXPECT_EQ(1, one.getSExtValue());
99 EXPECT_EQ(1u, one.getZExtValue());
102 TEST(APIntTest, i1) {
103 const APInt neg_two(1, static_cast<uint64_t>(-2), true);
104 const APInt neg_one(1, static_cast<uint64_t>(-1), true);
105 const APInt zero(1, 0);
106 const APInt one(1, 1);
107 const APInt two(1, 2);
109 EXPECT_EQ(0, neg_two.getSExtValue());
110 EXPECT_EQ(-1, neg_one.getSExtValue());
111 EXPECT_EQ(1u, neg_one.getZExtValue());
112 EXPECT_EQ(0u, zero.getZExtValue());
113 EXPECT_EQ(-1, one.getSExtValue());
114 EXPECT_EQ(1u, one.getZExtValue());
115 EXPECT_EQ(0u, two.getZExtValue());
116 EXPECT_EQ(0, two.getSExtValue());
118 // Basic equalities for 1-bit values.
119 EXPECT_EQ(zero, two);
120 EXPECT_EQ(zero, neg_two);
121 EXPECT_EQ(one, neg_one);
122 EXPECT_EQ(two, neg_two);
124 // Additions.
125 EXPECT_EQ(two, one + one);
126 EXPECT_EQ(zero, neg_one + one);
127 EXPECT_EQ(neg_two, neg_one + neg_one);
129 // Subtractions.
130 EXPECT_EQ(neg_two, neg_one - one);
131 EXPECT_EQ(two, one - neg_one);
132 EXPECT_EQ(zero, one - one);
134 // Shifts.
135 EXPECT_EQ(zero, one << one);
136 EXPECT_EQ(one, one << zero);
137 EXPECT_EQ(zero, one.shl(1));
138 EXPECT_EQ(one, one.shl(0));
139 EXPECT_EQ(zero, one.lshr(1));
140 EXPECT_EQ(zero, one.ashr(1));
142 // Multiplies.
143 EXPECT_EQ(neg_one, neg_one * one);
144 EXPECT_EQ(neg_one, one * neg_one);
145 EXPECT_EQ(one, neg_one * neg_one);
146 EXPECT_EQ(one, one * one);
148 // Divides.
149 EXPECT_EQ(neg_one, one.sdiv(neg_one));
150 EXPECT_EQ(neg_one, neg_one.sdiv(one));
151 EXPECT_EQ(one, neg_one.sdiv(neg_one));
152 EXPECT_EQ(one, one.sdiv(one));
154 EXPECT_EQ(neg_one, one.udiv(neg_one));
155 EXPECT_EQ(neg_one, neg_one.udiv(one));
156 EXPECT_EQ(one, neg_one.udiv(neg_one));
157 EXPECT_EQ(one, one.udiv(one));
159 // Remainders.
160 EXPECT_EQ(zero, neg_one.srem(one));
161 EXPECT_EQ(zero, neg_one.urem(one));
162 EXPECT_EQ(zero, one.srem(neg_one));
165 TEST(APIntTest, fromString) {
166 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2));
167 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2));
168 EXPECT_EQ(APInt(32, 2), APInt(32, "10", 2));
169 EXPECT_EQ(APInt(32, 3), APInt(32, "11", 2));
170 EXPECT_EQ(APInt(32, 4), APInt(32, "100", 2));
172 EXPECT_EQ(APInt(32, 0), APInt(32, "+0", 2));
173 EXPECT_EQ(APInt(32, 1), APInt(32, "+1", 2));
174 EXPECT_EQ(APInt(32, 2), APInt(32, "+10", 2));
175 EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2));
176 EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2));
178 EXPECT_EQ(APInt(32, -0), APInt(32, "-0", 2));
179 EXPECT_EQ(APInt(32, -1), APInt(32, "-1", 2));
180 EXPECT_EQ(APInt(32, -2), APInt(32, "-10", 2));
181 EXPECT_EQ(APInt(32, -3), APInt(32, "-11", 2));
182 EXPECT_EQ(APInt(32, -4), APInt(32, "-100", 2));
185 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8));
186 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8));
187 EXPECT_EQ(APInt(32, 7), APInt(32, "7", 8));
188 EXPECT_EQ(APInt(32, 8), APInt(32, "10", 8));
189 EXPECT_EQ(APInt(32, 15), APInt(32, "17", 8));
190 EXPECT_EQ(APInt(32, 16), APInt(32, "20", 8));
192 EXPECT_EQ(APInt(32, +0), APInt(32, "+0", 8));
193 EXPECT_EQ(APInt(32, +1), APInt(32, "+1", 8));
194 EXPECT_EQ(APInt(32, +7), APInt(32, "+7", 8));
195 EXPECT_EQ(APInt(32, +8), APInt(32, "+10", 8));
196 EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8));
197 EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8));
199 EXPECT_EQ(APInt(32, -0), APInt(32, "-0", 8));
200 EXPECT_EQ(APInt(32, -1), APInt(32, "-1", 8));
201 EXPECT_EQ(APInt(32, -7), APInt(32, "-7", 8));
202 EXPECT_EQ(APInt(32, -8), APInt(32, "-10", 8));
203 EXPECT_EQ(APInt(32, -15), APInt(32, "-17", 8));
204 EXPECT_EQ(APInt(32, -16), APInt(32, "-20", 8));
207 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10));
208 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10));
209 EXPECT_EQ(APInt(32, 9), APInt(32, "9", 10));
210 EXPECT_EQ(APInt(32, 10), APInt(32, "10", 10));
211 EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10));
212 EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10));
214 EXPECT_EQ(APInt(32, -0), APInt(32, "-0", 10));
215 EXPECT_EQ(APInt(32, -1), APInt(32, "-1", 10));
216 EXPECT_EQ(APInt(32, -9), APInt(32, "-9", 10));
217 EXPECT_EQ(APInt(32, -10), APInt(32, "-10", 10));
218 EXPECT_EQ(APInt(32, -19), APInt(32, "-19", 10));
219 EXPECT_EQ(APInt(32, -20), APInt(32, "-20", 10));
222 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16));
223 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16));
224 EXPECT_EQ(APInt(32, 15), APInt(32, "F", 16));
225 EXPECT_EQ(APInt(32, 16), APInt(32, "10", 16));
226 EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16));
227 EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16));
229 EXPECT_EQ(APInt(32, -0), APInt(32, "-0", 16));
230 EXPECT_EQ(APInt(32, -1), APInt(32, "-1", 16));
231 EXPECT_EQ(APInt(32, -15), APInt(32, "-F", 16));
232 EXPECT_EQ(APInt(32, -16), APInt(32, "-10", 16));
233 EXPECT_EQ(APInt(32, -31), APInt(32, "-1F", 16));
234 EXPECT_EQ(APInt(32, -32), APInt(32, "-20", 16));
237 TEST(APIntTest, StringBitsNeeded2) {
238 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 2));
239 EXPECT_EQ(1U, APInt::getBitsNeeded( "1", 2));
240 EXPECT_EQ(2U, APInt::getBitsNeeded( "10", 2));
241 EXPECT_EQ(2U, APInt::getBitsNeeded( "11", 2));
242 EXPECT_EQ(3U, APInt::getBitsNeeded("100", 2));
244 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 2));
245 EXPECT_EQ(1U, APInt::getBitsNeeded( "+1", 2));
246 EXPECT_EQ(2U, APInt::getBitsNeeded( "+10", 2));
247 EXPECT_EQ(2U, APInt::getBitsNeeded( "+11", 2));
248 EXPECT_EQ(3U, APInt::getBitsNeeded("+100", 2));
250 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 2));
251 EXPECT_EQ(2U, APInt::getBitsNeeded( "-1", 2));
252 EXPECT_EQ(3U, APInt::getBitsNeeded( "-10", 2));
253 EXPECT_EQ(3U, APInt::getBitsNeeded( "-11", 2));
254 EXPECT_EQ(4U, APInt::getBitsNeeded("-100", 2));
257 TEST(APIntTest, StringBitsNeeded8) {
258 EXPECT_EQ(3U, APInt::getBitsNeeded( "0", 8));
259 EXPECT_EQ(3U, APInt::getBitsNeeded( "7", 8));
260 EXPECT_EQ(6U, APInt::getBitsNeeded("10", 8));
261 EXPECT_EQ(6U, APInt::getBitsNeeded("17", 8));
262 EXPECT_EQ(6U, APInt::getBitsNeeded("20", 8));
264 EXPECT_EQ(3U, APInt::getBitsNeeded( "+0", 8));
265 EXPECT_EQ(3U, APInt::getBitsNeeded( "+7", 8));
266 EXPECT_EQ(6U, APInt::getBitsNeeded("+10", 8));
267 EXPECT_EQ(6U, APInt::getBitsNeeded("+17", 8));
268 EXPECT_EQ(6U, APInt::getBitsNeeded("+20", 8));
270 EXPECT_EQ(4U, APInt::getBitsNeeded( "-0", 8));
271 EXPECT_EQ(4U, APInt::getBitsNeeded( "-7", 8));
272 EXPECT_EQ(7U, APInt::getBitsNeeded("-10", 8));
273 EXPECT_EQ(7U, APInt::getBitsNeeded("-17", 8));
274 EXPECT_EQ(7U, APInt::getBitsNeeded("-20", 8));
277 TEST(APIntTest, StringBitsNeeded10) {
278 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 10));
279 EXPECT_EQ(2U, APInt::getBitsNeeded( "3", 10));
280 EXPECT_EQ(4U, APInt::getBitsNeeded( "9", 10));
281 EXPECT_EQ(4U, APInt::getBitsNeeded("10", 10));
282 EXPECT_EQ(5U, APInt::getBitsNeeded("19", 10));
283 EXPECT_EQ(5U, APInt::getBitsNeeded("20", 10));
285 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 10));
286 EXPECT_EQ(4U, APInt::getBitsNeeded( "+9", 10));
287 EXPECT_EQ(4U, APInt::getBitsNeeded("+10", 10));
288 EXPECT_EQ(5U, APInt::getBitsNeeded("+19", 10));
289 EXPECT_EQ(5U, APInt::getBitsNeeded("+20", 10));
291 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 10));
292 EXPECT_EQ(5U, APInt::getBitsNeeded( "-9", 10));
293 EXPECT_EQ(5U, APInt::getBitsNeeded("-10", 10));
294 EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10));
295 EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10));
298 TEST(APIntTest, StringBitsNeeded16) {
299 EXPECT_EQ(4U, APInt::getBitsNeeded( "0", 16));
300 EXPECT_EQ(4U, APInt::getBitsNeeded( "F", 16));
301 EXPECT_EQ(8U, APInt::getBitsNeeded("10", 16));
302 EXPECT_EQ(8U, APInt::getBitsNeeded("1F", 16));
303 EXPECT_EQ(8U, APInt::getBitsNeeded("20", 16));
305 EXPECT_EQ(4U, APInt::getBitsNeeded( "+0", 16));
306 EXPECT_EQ(4U, APInt::getBitsNeeded( "+F", 16));
307 EXPECT_EQ(8U, APInt::getBitsNeeded("+10", 16));
308 EXPECT_EQ(8U, APInt::getBitsNeeded("+1F", 16));
309 EXPECT_EQ(8U, APInt::getBitsNeeded("+20", 16));
311 EXPECT_EQ(5U, APInt::getBitsNeeded( "-0", 16));
312 EXPECT_EQ(5U, APInt::getBitsNeeded( "-F", 16));
313 EXPECT_EQ(9U, APInt::getBitsNeeded("-10", 16));
314 EXPECT_EQ(9U, APInt::getBitsNeeded("-1F", 16));
315 EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16));
319 #ifdef GTEST_HAS_DEATH_TEST
320 TEST(APIntTest, StringDeath) {
321 EXPECT_DEATH(APInt(0, "", 0), "Bitwidth too small");
322 EXPECT_DEATH(APInt(32, "", 0), "Invalid string length");
323 EXPECT_DEATH(APInt(32, "0", 0), "Radix should be 2, 8, 10, or 16!");
324 EXPECT_DEATH(APInt(32, "", 10), "Invalid string length");
325 EXPECT_DEATH(APInt(32, "-", 10), "String is only a sign, needs a value.");
326 EXPECT_DEATH(APInt(1, "1234", 10), "Insufficient bit width");
327 EXPECT_DEATH(APInt(32, "\0", 10), "Invalid string length");
328 EXPECT_DEATH(APInt(32, StringRef("1\02", 3), 10), "Invalid character in digit string");
329 EXPECT_DEATH(APInt(32, "1L", 10), "Invalid character in digit string");
331 #endif