diff options
Diffstat (limited to 'unittests/ADT/APIntTest.cpp')
-rw-r--r-- | unittests/ADT/APIntTest.cpp | 183 |
1 files changed, 176 insertions, 7 deletions
diff --git a/unittests/ADT/APIntTest.cpp b/unittests/ADT/APIntTest.cpp index 648faf1..0b13aa4 100644 --- a/unittests/ADT/APIntTest.cpp +++ b/unittests/ADT/APIntTest.cpp @@ -8,7 +8,6 @@ //===----------------------------------------------------------------------===// #include <ostream> -#include "llvm/Support/raw_ostream.h" #include "gtest/gtest.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/SmallString.h" @@ -164,12 +163,182 @@ TEST(APIntTest, i1) { } TEST(APIntTest, fromString) { - EXPECT_EQ(APInt(1, 0), APInt(1, "0", 1, 10)); - EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 10)); - EXPECT_EQ(APInt(1, 1), APInt(1, "-1", 2, 10)); - EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 2)); - EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 8)); - EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 16)); + EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2)); + EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2)); + EXPECT_EQ(APInt(32, 2), APInt(32, "10", 2)); + EXPECT_EQ(APInt(32, 3), APInt(32, "11", 2)); + EXPECT_EQ(APInt(32, 4), APInt(32, "100", 2)); + + EXPECT_EQ(APInt(32, 0), APInt(32, "+0", 2)); + EXPECT_EQ(APInt(32, 1), APInt(32, "+1", 2)); + EXPECT_EQ(APInt(32, 2), APInt(32, "+10", 2)); + EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2)); + EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2)); + + EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 2)); + EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 2)); + EXPECT_EQ(APInt(32, uint64_t(-2LL)), APInt(32, "-10", 2)); + EXPECT_EQ(APInt(32, uint64_t(-3LL)), APInt(32, "-11", 2)); + EXPECT_EQ(APInt(32, uint64_t(-4LL)), APInt(32, "-100", 2)); + + + EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8)); + EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8)); + EXPECT_EQ(APInt(32, 7), APInt(32, "7", 8)); + EXPECT_EQ(APInt(32, 8), APInt(32, "10", 8)); + EXPECT_EQ(APInt(32, 15), APInt(32, "17", 8)); + EXPECT_EQ(APInt(32, 16), APInt(32, "20", 8)); + + EXPECT_EQ(APInt(32, +0), APInt(32, "+0", 8)); + EXPECT_EQ(APInt(32, +1), APInt(32, "+1", 8)); + EXPECT_EQ(APInt(32, +7), APInt(32, "+7", 8)); + EXPECT_EQ(APInt(32, +8), APInt(32, "+10", 8)); + EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8)); + EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8)); + + EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 8)); + EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 8)); + EXPECT_EQ(APInt(32, uint64_t(-7LL)), APInt(32, "-7", 8)); + EXPECT_EQ(APInt(32, uint64_t(-8LL)), APInt(32, "-10", 8)); + EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-17", 8)); + EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-20", 8)); + + + EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10)); + EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10)); + EXPECT_EQ(APInt(32, 9), APInt(32, "9", 10)); + EXPECT_EQ(APInt(32, 10), APInt(32, "10", 10)); + EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10)); + EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10)); + + EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 10)); + EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 10)); + EXPECT_EQ(APInt(32, uint64_t(-9LL)), APInt(32, "-9", 10)); + EXPECT_EQ(APInt(32, uint64_t(-10LL)), APInt(32, "-10", 10)); + EXPECT_EQ(APInt(32, uint64_t(-19LL)), APInt(32, "-19", 10)); + EXPECT_EQ(APInt(32, uint64_t(-20LL)), APInt(32, "-20", 10)); + + + EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16)); + EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16)); + EXPECT_EQ(APInt(32, 15), APInt(32, "F", 16)); + EXPECT_EQ(APInt(32, 16), APInt(32, "10", 16)); + EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16)); + EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16)); + + EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 16)); + EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 16)); + EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-F", 16)); + EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-10", 16)); + EXPECT_EQ(APInt(32, uint64_t(-31LL)), APInt(32, "-1F", 16)); + EXPECT_EQ(APInt(32, uint64_t(-32LL)), APInt(32, "-20", 16)); +} + +TEST(APIntTest, StringBitsNeeded2) { + EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 2)); + EXPECT_EQ(1U, APInt::getBitsNeeded( "1", 2)); + EXPECT_EQ(2U, APInt::getBitsNeeded( "10", 2)); + EXPECT_EQ(2U, APInt::getBitsNeeded( "11", 2)); + EXPECT_EQ(3U, APInt::getBitsNeeded("100", 2)); + + EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 2)); + EXPECT_EQ(1U, APInt::getBitsNeeded( "+1", 2)); + EXPECT_EQ(2U, APInt::getBitsNeeded( "+10", 2)); + EXPECT_EQ(2U, APInt::getBitsNeeded( "+11", 2)); + EXPECT_EQ(3U, APInt::getBitsNeeded("+100", 2)); + + EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 2)); + EXPECT_EQ(2U, APInt::getBitsNeeded( "-1", 2)); + EXPECT_EQ(3U, APInt::getBitsNeeded( "-10", 2)); + EXPECT_EQ(3U, APInt::getBitsNeeded( "-11", 2)); + EXPECT_EQ(4U, APInt::getBitsNeeded("-100", 2)); +} + +TEST(APIntTest, StringBitsNeeded8) { + EXPECT_EQ(3U, APInt::getBitsNeeded( "0", 8)); + EXPECT_EQ(3U, APInt::getBitsNeeded( "7", 8)); + EXPECT_EQ(6U, APInt::getBitsNeeded("10", 8)); + EXPECT_EQ(6U, APInt::getBitsNeeded("17", 8)); + EXPECT_EQ(6U, APInt::getBitsNeeded("20", 8)); + + EXPECT_EQ(3U, APInt::getBitsNeeded( "+0", 8)); + EXPECT_EQ(3U, APInt::getBitsNeeded( "+7", 8)); + EXPECT_EQ(6U, APInt::getBitsNeeded("+10", 8)); + EXPECT_EQ(6U, APInt::getBitsNeeded("+17", 8)); + EXPECT_EQ(6U, APInt::getBitsNeeded("+20", 8)); + + EXPECT_EQ(4U, APInt::getBitsNeeded( "-0", 8)); + EXPECT_EQ(4U, APInt::getBitsNeeded( "-7", 8)); + EXPECT_EQ(7U, APInt::getBitsNeeded("-10", 8)); + EXPECT_EQ(7U, APInt::getBitsNeeded("-17", 8)); + EXPECT_EQ(7U, APInt::getBitsNeeded("-20", 8)); +} + +TEST(APIntTest, StringBitsNeeded10) { + EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 10)); + EXPECT_EQ(2U, APInt::getBitsNeeded( "3", 10)); + EXPECT_EQ(4U, APInt::getBitsNeeded( "9", 10)); + EXPECT_EQ(4U, APInt::getBitsNeeded("10", 10)); + EXPECT_EQ(5U, APInt::getBitsNeeded("19", 10)); + EXPECT_EQ(5U, APInt::getBitsNeeded("20", 10)); + + EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 10)); + EXPECT_EQ(4U, APInt::getBitsNeeded( "+9", 10)); + EXPECT_EQ(4U, APInt::getBitsNeeded("+10", 10)); + EXPECT_EQ(5U, APInt::getBitsNeeded("+19", 10)); + EXPECT_EQ(5U, APInt::getBitsNeeded("+20", 10)); + + EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 10)); + EXPECT_EQ(5U, APInt::getBitsNeeded( "-9", 10)); + EXPECT_EQ(5U, APInt::getBitsNeeded("-10", 10)); + EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10)); + EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10)); +} + +TEST(APIntTest, StringBitsNeeded16) { + EXPECT_EQ(4U, APInt::getBitsNeeded( "0", 16)); + EXPECT_EQ(4U, APInt::getBitsNeeded( "F", 16)); + EXPECT_EQ(8U, APInt::getBitsNeeded("10", 16)); + EXPECT_EQ(8U, APInt::getBitsNeeded("1F", 16)); + EXPECT_EQ(8U, APInt::getBitsNeeded("20", 16)); + + EXPECT_EQ(4U, APInt::getBitsNeeded( "+0", 16)); + EXPECT_EQ(4U, APInt::getBitsNeeded( "+F", 16)); + EXPECT_EQ(8U, APInt::getBitsNeeded("+10", 16)); + EXPECT_EQ(8U, APInt::getBitsNeeded("+1F", 16)); + EXPECT_EQ(8U, APInt::getBitsNeeded("+20", 16)); + + EXPECT_EQ(5U, APInt::getBitsNeeded( "-0", 16)); + EXPECT_EQ(5U, APInt::getBitsNeeded( "-F", 16)); + EXPECT_EQ(9U, APInt::getBitsNeeded("-10", 16)); + EXPECT_EQ(9U, APInt::getBitsNeeded("-1F", 16)); + EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16)); +} + +TEST(APIntTest, Log2) { + EXPECT_EQ(APInt(15, 7).logBase2(), 2U); + EXPECT_EQ(APInt(15, 7).ceilLogBase2(), 3U); + EXPECT_EQ(APInt(15, 7).exactLogBase2(), -1); + EXPECT_EQ(APInt(15, 8).logBase2(), 3U); + EXPECT_EQ(APInt(15, 8).ceilLogBase2(), 3U); + EXPECT_EQ(APInt(15, 8).exactLogBase2(), 3); + EXPECT_EQ(APInt(15, 9).logBase2(), 3U); + EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4U); + EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1); +} + +#ifdef GTEST_HAS_DEATH_TEST +TEST(APIntTest, StringDeath) { + EXPECT_DEATH(APInt(0, "", 0), "Bitwidth too small"); + EXPECT_DEATH(APInt(32, "", 0), "Invalid string length"); + EXPECT_DEATH(APInt(32, "0", 0), "Radix should be 2, 8, 10, or 16!"); + EXPECT_DEATH(APInt(32, "", 10), "Invalid string length"); + EXPECT_DEATH(APInt(32, "-", 10), "String is only a sign, needs a value."); + EXPECT_DEATH(APInt(1, "1234", 10), "Insufficient bit width"); + EXPECT_DEATH(APInt(32, "\0", 10), "Invalid string length"); + EXPECT_DEATH(APInt(32, StringRef("1\02", 3), 10), "Invalid character in digit string"); + EXPECT_DEATH(APInt(32, "1L", 10), "Invalid character in digit string"); } +#endif } |