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 //===----------------------------------------------------------------------===// 9 10 #include <ostream> 11 #include "gtest/gtest.h" 12 #include "llvm/ADT/APInt.h" 13 #include "llvm/ADT/SmallString.h" 14 15 using namespace llvm; 16 17 namespace { 18 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_TRUE(Shl[0]); 24 EXPECT_FALSE(Shl[1]); 25 } 26 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()); 31 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()); 40 } 41 42 // XFAIL this test on FreeBSD where the system gcc-4.2.1 seems to miscompile it. 43 #if defined(__llvm__) || !defined(__FreeBSD__) 44 45 TEST(APIntTest, i33_Count) { 46 APInt i33minus2(33, static_cast<uint64_t>(-2), true); 47 EXPECT_EQ(0u, i33minus2.countLeadingZeros()); 48 EXPECT_EQ(32u, i33minus2.countLeadingOnes()); 49 EXPECT_EQ(33u, i33minus2.getActiveBits()); 50 EXPECT_EQ(1u, i33minus2.countTrailingZeros()); 51 EXPECT_EQ(32u, i33minus2.countPopulation()); 52 EXPECT_EQ(-2, i33minus2.getSExtValue()); 53 EXPECT_EQ(((uint64_t)-2)&((1ull<<33) -1), i33minus2.getZExtValue()); 54 } 55 56 #endif 57 58 TEST(APIntTest, i65_Count) { 59 APInt i65minus(65, 0, true); 60 i65minus.setBit(64); 61 EXPECT_EQ(0u, i65minus.countLeadingZeros()); 62 EXPECT_EQ(1u, i65minus.countLeadingOnes()); 63 EXPECT_EQ(65u, i65minus.getActiveBits()); 64 EXPECT_EQ(64u, i65minus.countTrailingZeros()); 65 EXPECT_EQ(1u, i65minus.countPopulation()); 66 } 67 68 TEST(APIntTest, i128_PositiveCount) { 69 APInt u128max = APInt::getAllOnesValue(128); 70 EXPECT_EQ(128u, u128max.countLeadingOnes()); 71 EXPECT_EQ(0u, u128max.countLeadingZeros()); 72 EXPECT_EQ(128u, u128max.getActiveBits()); 73 EXPECT_EQ(0u, u128max.countTrailingZeros()); 74 EXPECT_EQ(128u, u128max.countTrailingOnes()); 75 EXPECT_EQ(128u, u128max.countPopulation()); 76 77 APInt u64max(128, static_cast<uint64_t>(-1), false); 78 EXPECT_EQ(64u, u64max.countLeadingZeros()); 79 EXPECT_EQ(0u, u64max.countLeadingOnes()); 80 EXPECT_EQ(64u, u64max.getActiveBits()); 81 EXPECT_EQ(0u, u64max.countTrailingZeros()); 82 EXPECT_EQ(64u, u64max.countTrailingOnes()); 83 EXPECT_EQ(64u, u64max.countPopulation()); 84 EXPECT_EQ((uint64_t)~0ull, u64max.getZExtValue()); 85 86 APInt zero(128, 0, true); 87 EXPECT_EQ(128u, zero.countLeadingZeros()); 88 EXPECT_EQ(0u, zero.countLeadingOnes()); 89 EXPECT_EQ(0u, zero.getActiveBits()); 90 EXPECT_EQ(128u, zero.countTrailingZeros()); 91 EXPECT_EQ(0u, zero.countTrailingOnes()); 92 EXPECT_EQ(0u, zero.countPopulation()); 93 EXPECT_EQ(0u, zero.getSExtValue()); 94 EXPECT_EQ(0u, zero.getZExtValue()); 95 96 APInt one(128, 1, true); 97 EXPECT_EQ(127u, one.countLeadingZeros()); 98 EXPECT_EQ(0u, one.countLeadingOnes()); 99 EXPECT_EQ(1u, one.getActiveBits()); 100 EXPECT_EQ(0u, one.countTrailingZeros()); 101 EXPECT_EQ(1u, one.countTrailingOnes()); 102 EXPECT_EQ(1u, one.countPopulation()); 103 EXPECT_EQ(1, one.getSExtValue()); 104 EXPECT_EQ(1u, one.getZExtValue()); 105 } 106 107 TEST(APIntTest, i1) { 108 const APInt neg_two(1, static_cast<uint64_t>(-2), true); 109 const APInt neg_one(1, static_cast<uint64_t>(-1), true); 110 const APInt zero(1, 0); 111 const APInt one(1, 1); 112 const APInt two(1, 2); 113 114 EXPECT_EQ(0, neg_two.getSExtValue()); 115 EXPECT_EQ(-1, neg_one.getSExtValue()); 116 EXPECT_EQ(1u, neg_one.getZExtValue()); 117 EXPECT_EQ(0u, zero.getZExtValue()); 118 EXPECT_EQ(-1, one.getSExtValue()); 119 EXPECT_EQ(1u, one.getZExtValue()); 120 EXPECT_EQ(0u, two.getZExtValue()); 121 EXPECT_EQ(0, two.getSExtValue()); 122 123 // Basic equalities for 1-bit values. 124 EXPECT_EQ(zero, two); 125 EXPECT_EQ(zero, neg_two); 126 EXPECT_EQ(one, neg_one); 127 EXPECT_EQ(two, neg_two); 128 129 // Additions. 130 EXPECT_EQ(two, one + one); 131 EXPECT_EQ(zero, neg_one + one); 132 EXPECT_EQ(neg_two, neg_one + neg_one); 133 134 // Subtractions. 135 EXPECT_EQ(neg_two, neg_one - one); 136 EXPECT_EQ(two, one - neg_one); 137 EXPECT_EQ(zero, one - one); 138 139 // Shifts. 140 EXPECT_EQ(zero, one << one); 141 EXPECT_EQ(one, one << zero); 142 EXPECT_EQ(zero, one.shl(1)); 143 EXPECT_EQ(one, one.shl(0)); 144 EXPECT_EQ(zero, one.lshr(1)); 145 EXPECT_EQ(zero, one.ashr(1)); 146 147 // Multiplies. 148 EXPECT_EQ(neg_one, neg_one * one); 149 EXPECT_EQ(neg_one, one * neg_one); 150 EXPECT_EQ(one, neg_one * neg_one); 151 EXPECT_EQ(one, one * one); 152 153 // Divides. 154 EXPECT_EQ(neg_one, one.sdiv(neg_one)); 155 EXPECT_EQ(neg_one, neg_one.sdiv(one)); 156 EXPECT_EQ(one, neg_one.sdiv(neg_one)); 157 EXPECT_EQ(one, one.sdiv(one)); 158 159 EXPECT_EQ(neg_one, one.udiv(neg_one)); 160 EXPECT_EQ(neg_one, neg_one.udiv(one)); 161 EXPECT_EQ(one, neg_one.udiv(neg_one)); 162 EXPECT_EQ(one, one.udiv(one)); 163 164 // Remainders. 165 EXPECT_EQ(zero, neg_one.srem(one)); 166 EXPECT_EQ(zero, neg_one.urem(one)); 167 EXPECT_EQ(zero, one.srem(neg_one)); 168 } 169 170 TEST(APIntTest, fromString) { 171 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2)); 172 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2)); 173 EXPECT_EQ(APInt(32, 2), APInt(32, "10", 2)); 174 EXPECT_EQ(APInt(32, 3), APInt(32, "11", 2)); 175 EXPECT_EQ(APInt(32, 4), APInt(32, "100", 2)); 176 177 EXPECT_EQ(APInt(32, 0), APInt(32, "+0", 2)); 178 EXPECT_EQ(APInt(32, 1), APInt(32, "+1", 2)); 179 EXPECT_EQ(APInt(32, 2), APInt(32, "+10", 2)); 180 EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2)); 181 EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2)); 182 183 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 2)); 184 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 2)); 185 EXPECT_EQ(APInt(32, uint64_t(-2LL)), APInt(32, "-10", 2)); 186 EXPECT_EQ(APInt(32, uint64_t(-3LL)), APInt(32, "-11", 2)); 187 EXPECT_EQ(APInt(32, uint64_t(-4LL)), APInt(32, "-100", 2)); 188 189 190 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8)); 191 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8)); 192 EXPECT_EQ(APInt(32, 7), APInt(32, "7", 8)); 193 EXPECT_EQ(APInt(32, 8), APInt(32, "10", 8)); 194 EXPECT_EQ(APInt(32, 15), APInt(32, "17", 8)); 195 EXPECT_EQ(APInt(32, 16), APInt(32, "20", 8)); 196 197 EXPECT_EQ(APInt(32, +0), APInt(32, "+0", 8)); 198 EXPECT_EQ(APInt(32, +1), APInt(32, "+1", 8)); 199 EXPECT_EQ(APInt(32, +7), APInt(32, "+7", 8)); 200 EXPECT_EQ(APInt(32, +8), APInt(32, "+10", 8)); 201 EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8)); 202 EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8)); 203 204 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 8)); 205 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 8)); 206 EXPECT_EQ(APInt(32, uint64_t(-7LL)), APInt(32, "-7", 8)); 207 EXPECT_EQ(APInt(32, uint64_t(-8LL)), APInt(32, "-10", 8)); 208 EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-17", 8)); 209 EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-20", 8)); 210 211 212 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10)); 213 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10)); 214 EXPECT_EQ(APInt(32, 9), APInt(32, "9", 10)); 215 EXPECT_EQ(APInt(32, 10), APInt(32, "10", 10)); 216 EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10)); 217 EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10)); 218 219 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 10)); 220 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 10)); 221 EXPECT_EQ(APInt(32, uint64_t(-9LL)), APInt(32, "-9", 10)); 222 EXPECT_EQ(APInt(32, uint64_t(-10LL)), APInt(32, "-10", 10)); 223 EXPECT_EQ(APInt(32, uint64_t(-19LL)), APInt(32, "-19", 10)); 224 EXPECT_EQ(APInt(32, uint64_t(-20LL)), APInt(32, "-20", 10)); 225 226 227 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16)); 228 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16)); 229 EXPECT_EQ(APInt(32, 15), APInt(32, "F", 16)); 230 EXPECT_EQ(APInt(32, 16), APInt(32, "10", 16)); 231 EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16)); 232 EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16)); 233 234 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 16)); 235 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 16)); 236 EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-F", 16)); 237 EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-10", 16)); 238 EXPECT_EQ(APInt(32, uint64_t(-31LL)), APInt(32, "-1F", 16)); 239 EXPECT_EQ(APInt(32, uint64_t(-32LL)), APInt(32, "-20", 16)); 240 241 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 36)); 242 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 36)); 243 EXPECT_EQ(APInt(32, 35), APInt(32, "Z", 36)); 244 EXPECT_EQ(APInt(32, 36), APInt(32, "10", 36)); 245 EXPECT_EQ(APInt(32, 71), APInt(32, "1Z", 36)); 246 EXPECT_EQ(APInt(32, 72), APInt(32, "20", 36)); 247 248 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 36)); 249 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 36)); 250 EXPECT_EQ(APInt(32, uint64_t(-35LL)), APInt(32, "-Z", 36)); 251 EXPECT_EQ(APInt(32, uint64_t(-36LL)), APInt(32, "-10", 36)); 252 EXPECT_EQ(APInt(32, uint64_t(-71LL)), APInt(32, "-1Z", 36)); 253 EXPECT_EQ(APInt(32, uint64_t(-72LL)), APInt(32, "-20", 36)); 254 } 255 256 TEST(APIntTest, FromArray) { 257 EXPECT_EQ(APInt(32, uint64_t(1)), APInt(32, ArrayRef<uint64_t>(1))); 258 } 259 260 TEST(APIntTest, StringBitsNeeded2) { 261 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 2)); 262 EXPECT_EQ(1U, APInt::getBitsNeeded( "1", 2)); 263 EXPECT_EQ(2U, APInt::getBitsNeeded( "10", 2)); 264 EXPECT_EQ(2U, APInt::getBitsNeeded( "11", 2)); 265 EXPECT_EQ(3U, APInt::getBitsNeeded("100", 2)); 266 267 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 2)); 268 EXPECT_EQ(1U, APInt::getBitsNeeded( "+1", 2)); 269 EXPECT_EQ(2U, APInt::getBitsNeeded( "+10", 2)); 270 EXPECT_EQ(2U, APInt::getBitsNeeded( "+11", 2)); 271 EXPECT_EQ(3U, APInt::getBitsNeeded("+100", 2)); 272 273 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 2)); 274 EXPECT_EQ(2U, APInt::getBitsNeeded( "-1", 2)); 275 EXPECT_EQ(3U, APInt::getBitsNeeded( "-10", 2)); 276 EXPECT_EQ(3U, APInt::getBitsNeeded( "-11", 2)); 277 EXPECT_EQ(4U, APInt::getBitsNeeded("-100", 2)); 278 } 279 280 TEST(APIntTest, StringBitsNeeded8) { 281 EXPECT_EQ(3U, APInt::getBitsNeeded( "0", 8)); 282 EXPECT_EQ(3U, APInt::getBitsNeeded( "7", 8)); 283 EXPECT_EQ(6U, APInt::getBitsNeeded("10", 8)); 284 EXPECT_EQ(6U, APInt::getBitsNeeded("17", 8)); 285 EXPECT_EQ(6U, APInt::getBitsNeeded("20", 8)); 286 287 EXPECT_EQ(3U, APInt::getBitsNeeded( "+0", 8)); 288 EXPECT_EQ(3U, APInt::getBitsNeeded( "+7", 8)); 289 EXPECT_EQ(6U, APInt::getBitsNeeded("+10", 8)); 290 EXPECT_EQ(6U, APInt::getBitsNeeded("+17", 8)); 291 EXPECT_EQ(6U, APInt::getBitsNeeded("+20", 8)); 292 293 EXPECT_EQ(4U, APInt::getBitsNeeded( "-0", 8)); 294 EXPECT_EQ(4U, APInt::getBitsNeeded( "-7", 8)); 295 EXPECT_EQ(7U, APInt::getBitsNeeded("-10", 8)); 296 EXPECT_EQ(7U, APInt::getBitsNeeded("-17", 8)); 297 EXPECT_EQ(7U, APInt::getBitsNeeded("-20", 8)); 298 } 299 300 TEST(APIntTest, StringBitsNeeded10) { 301 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 10)); 302 EXPECT_EQ(2U, APInt::getBitsNeeded( "3", 10)); 303 EXPECT_EQ(4U, APInt::getBitsNeeded( "9", 10)); 304 EXPECT_EQ(4U, APInt::getBitsNeeded("10", 10)); 305 EXPECT_EQ(5U, APInt::getBitsNeeded("19", 10)); 306 EXPECT_EQ(5U, APInt::getBitsNeeded("20", 10)); 307 308 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 10)); 309 EXPECT_EQ(4U, APInt::getBitsNeeded( "+9", 10)); 310 EXPECT_EQ(4U, APInt::getBitsNeeded("+10", 10)); 311 EXPECT_EQ(5U, APInt::getBitsNeeded("+19", 10)); 312 EXPECT_EQ(5U, APInt::getBitsNeeded("+20", 10)); 313 314 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 10)); 315 EXPECT_EQ(5U, APInt::getBitsNeeded( "-9", 10)); 316 EXPECT_EQ(5U, APInt::getBitsNeeded("-10", 10)); 317 EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10)); 318 EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10)); 319 } 320 321 TEST(APIntTest, StringBitsNeeded16) { 322 EXPECT_EQ(4U, APInt::getBitsNeeded( "0", 16)); 323 EXPECT_EQ(4U, APInt::getBitsNeeded( "F", 16)); 324 EXPECT_EQ(8U, APInt::getBitsNeeded("10", 16)); 325 EXPECT_EQ(8U, APInt::getBitsNeeded("1F", 16)); 326 EXPECT_EQ(8U, APInt::getBitsNeeded("20", 16)); 327 328 EXPECT_EQ(4U, APInt::getBitsNeeded( "+0", 16)); 329 EXPECT_EQ(4U, APInt::getBitsNeeded( "+F", 16)); 330 EXPECT_EQ(8U, APInt::getBitsNeeded("+10", 16)); 331 EXPECT_EQ(8U, APInt::getBitsNeeded("+1F", 16)); 332 EXPECT_EQ(8U, APInt::getBitsNeeded("+20", 16)); 333 334 EXPECT_EQ(5U, APInt::getBitsNeeded( "-0", 16)); 335 EXPECT_EQ(5U, APInt::getBitsNeeded( "-F", 16)); 336 EXPECT_EQ(9U, APInt::getBitsNeeded("-10", 16)); 337 EXPECT_EQ(9U, APInt::getBitsNeeded("-1F", 16)); 338 EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16)); 339 } 340 341 TEST(APIntTest, toString) { 342 SmallString<16> S; 343 bool isSigned; 344 345 APInt(8, 0).toString(S, 2, true, true); 346 EXPECT_EQ(S.str().str(), "0b0"); 347 S.clear(); 348 APInt(8, 0).toString(S, 8, true, true); 349 EXPECT_EQ(S.str().str(), "00"); 350 S.clear(); 351 APInt(8, 0).toString(S, 10, true, true); 352 EXPECT_EQ(S.str().str(), "0"); 353 S.clear(); 354 APInt(8, 0).toString(S, 16, true, true); 355 EXPECT_EQ(S.str().str(), "0x0"); 356 S.clear(); 357 APInt(8, 0).toString(S, 36, true, true); 358 EXPECT_EQ(S.str().str(), "0"); 359 S.clear(); 360 361 isSigned = false; 362 APInt(8, 255, isSigned).toString(S, 2, isSigned, true); 363 EXPECT_EQ(S.str().str(), "0b11111111"); 364 S.clear(); 365 APInt(8, 255, isSigned).toString(S, 8, isSigned, true); 366 EXPECT_EQ(S.str().str(), "0377"); 367 S.clear(); 368 APInt(8, 255, isSigned).toString(S, 10, isSigned, true); 369 EXPECT_EQ(S.str().str(), "255"); 370 S.clear(); 371 APInt(8, 255, isSigned).toString(S, 16, isSigned, true); 372 EXPECT_EQ(S.str().str(), "0xFF"); 373 S.clear(); 374 APInt(8, 255, isSigned).toString(S, 36, isSigned, true); 375 EXPECT_EQ(S.str().str(), "73"); 376 S.clear(); 377 378 isSigned = true; 379 APInt(8, 255, isSigned).toString(S, 2, isSigned, true); 380 EXPECT_EQ(S.str().str(), "-0b1"); 381 S.clear(); 382 APInt(8, 255, isSigned).toString(S, 8, isSigned, true); 383 EXPECT_EQ(S.str().str(), "-01"); 384 S.clear(); 385 APInt(8, 255, isSigned).toString(S, 10, isSigned, true); 386 EXPECT_EQ(S.str().str(), "-1"); 387 S.clear(); 388 APInt(8, 255, isSigned).toString(S, 16, isSigned, true); 389 EXPECT_EQ(S.str().str(), "-0x1"); 390 S.clear(); 391 APInt(8, 255, isSigned).toString(S, 36, isSigned, true); 392 EXPECT_EQ(S.str().str(), "-1"); 393 S.clear(); 394 } 395 396 TEST(APIntTest, Log2) { 397 EXPECT_EQ(APInt(15, 7).logBase2(), 2U); 398 EXPECT_EQ(APInt(15, 7).ceilLogBase2(), 3U); 399 EXPECT_EQ(APInt(15, 7).exactLogBase2(), -1); 400 EXPECT_EQ(APInt(15, 8).logBase2(), 3U); 401 EXPECT_EQ(APInt(15, 8).ceilLogBase2(), 3U); 402 EXPECT_EQ(APInt(15, 8).exactLogBase2(), 3); 403 EXPECT_EQ(APInt(15, 9).logBase2(), 3U); 404 EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4U); 405 EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1); 406 } 407 408 TEST(APIntTest, magic) { 409 EXPECT_EQ(APInt(32, 3).magic().m, APInt(32, "55555556", 16)); 410 EXPECT_EQ(APInt(32, 3).magic().s, 0U); 411 EXPECT_EQ(APInt(32, 5).magic().m, APInt(32, "66666667", 16)); 412 EXPECT_EQ(APInt(32, 5).magic().s, 1U); 413 EXPECT_EQ(APInt(32, 7).magic().m, APInt(32, "92492493", 16)); 414 EXPECT_EQ(APInt(32, 7).magic().s, 2U); 415 } 416 417 TEST(APIntTest, magicu) { 418 EXPECT_EQ(APInt(32, 3).magicu().m, APInt(32, "AAAAAAAB", 16)); 419 EXPECT_EQ(APInt(32, 3).magicu().s, 1U); 420 EXPECT_EQ(APInt(32, 5).magicu().m, APInt(32, "CCCCCCCD", 16)); 421 EXPECT_EQ(APInt(32, 5).magicu().s, 2U); 422 EXPECT_EQ(APInt(32, 7).magicu().m, APInt(32, "24924925", 16)); 423 EXPECT_EQ(APInt(32, 7).magicu().s, 3U); 424 EXPECT_EQ(APInt(64, 25).magicu(1).m, APInt(64, "A3D70A3D70A3D70B", 16)); 425 EXPECT_EQ(APInt(64, 25).magicu(1).s, 4U); 426 } 427 428 #ifdef GTEST_HAS_DEATH_TEST 429 #ifndef NDEBUG 430 TEST(APIntTest, StringDeath) { 431 EXPECT_DEATH(APInt(0, "", 0), "Bitwidth too small"); 432 EXPECT_DEATH(APInt(32, "", 0), "Invalid string length"); 433 EXPECT_DEATH(APInt(32, "0", 0), "Radix should be 2, 8, 10, 16, or 36!"); 434 EXPECT_DEATH(APInt(32, "", 10), "Invalid string length"); 435 EXPECT_DEATH(APInt(32, "-", 10), "String is only a sign, needs a value."); 436 EXPECT_DEATH(APInt(1, "1234", 10), "Insufficient bit width"); 437 EXPECT_DEATH(APInt(32, "\0", 10), "Invalid string length"); 438 EXPECT_DEATH(APInt(32, StringRef("1\02", 3), 10), "Invalid character in digit string"); 439 EXPECT_DEATH(APInt(32, "1L", 10), "Invalid character in digit string"); 440 } 441 #endif 442 #endif 443 444 TEST(APIntTest, mul_clear) { 445 APInt ValA(65, -1ULL); 446 APInt ValB(65, 4); 447 APInt ValC(65, 0); 448 ValC = ValA * ValB; 449 ValA *= ValB; 450 EXPECT_EQ(ValA.toString(10, false), ValC.toString(10, false)); 451 } 452 453 } 454