Home | History | Annotate | Download | only in ADT
      1 //===- llvm/unittest/ADT/APSIntTest.cpp - APSInt 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 "llvm/ADT/APSInt.h"
     11 #include "gtest/gtest.h"
     12 
     13 using namespace llvm;
     14 
     15 namespace {
     16 
     17 TEST(APSIntTest, MoveTest) {
     18   APSInt A(32, true);
     19   EXPECT_TRUE(A.isUnsigned());
     20 
     21   APSInt B(128, false);
     22   A = B;
     23   EXPECT_FALSE(A.isUnsigned());
     24 
     25   APSInt C(B);
     26   EXPECT_FALSE(C.isUnsigned());
     27 
     28   APInt Wide(256, 0);
     29   const uint64_t *Bits = Wide.getRawData();
     30   APSInt D(std::move(Wide));
     31   EXPECT_TRUE(D.isUnsigned());
     32   EXPECT_EQ(Bits, D.getRawData()); // Verify that "Wide" was really moved.
     33 
     34   A = APSInt(64, true);
     35   EXPECT_TRUE(A.isUnsigned());
     36 
     37   Wide = APInt(128, 1);
     38   Bits = Wide.getRawData();
     39   A = std::move(Wide);
     40   EXPECT_TRUE(A.isUnsigned());
     41   EXPECT_EQ(Bits, A.getRawData()); // Verify that "Wide" was really moved.
     42 }
     43 
     44 TEST(APSIntTest, get) {
     45   EXPECT_TRUE(APSInt::get(7).isSigned());
     46   EXPECT_EQ(64u, APSInt::get(7).getBitWidth());
     47   EXPECT_EQ(7u, APSInt::get(7).getZExtValue());
     48   EXPECT_EQ(7, APSInt::get(7).getSExtValue());
     49   EXPECT_TRUE(APSInt::get(-7).isSigned());
     50   EXPECT_EQ(64u, APSInt::get(-7).getBitWidth());
     51   EXPECT_EQ(-7, APSInt::get(-7).getSExtValue());
     52   EXPECT_EQ(UINT64_C(0) - 7, APSInt::get(-7).getZExtValue());
     53 }
     54 
     55 TEST(APSIntTest, getUnsigned) {
     56   EXPECT_TRUE(APSInt::getUnsigned(7).isUnsigned());
     57   EXPECT_EQ(64u, APSInt::getUnsigned(7).getBitWidth());
     58   EXPECT_EQ(7u, APSInt::getUnsigned(7).getZExtValue());
     59   EXPECT_EQ(7, APSInt::getUnsigned(7).getSExtValue());
     60   EXPECT_TRUE(APSInt::getUnsigned(-7).isUnsigned());
     61   EXPECT_EQ(64u, APSInt::getUnsigned(-7).getBitWidth());
     62   EXPECT_EQ(-7, APSInt::getUnsigned(-7).getSExtValue());
     63   EXPECT_EQ(UINT64_C(0) - 7, APSInt::getUnsigned(-7).getZExtValue());
     64 }
     65 
     66 TEST(APSIntTest, getExtValue) {
     67   EXPECT_TRUE(APSInt(APInt(3, 7), true).isUnsigned());
     68   EXPECT_TRUE(APSInt(APInt(3, 7), false).isSigned());
     69   EXPECT_TRUE(APSInt(APInt(4, 7), true).isUnsigned());
     70   EXPECT_TRUE(APSInt(APInt(4, 7), false).isSigned());
     71   EXPECT_TRUE(APSInt(APInt(4, -7), true).isUnsigned());
     72   EXPECT_TRUE(APSInt(APInt(4, -7), false).isSigned());
     73   EXPECT_EQ(7, APSInt(APInt(3, 7), true).getExtValue());
     74   EXPECT_EQ(-1, APSInt(APInt(3, 7), false).getExtValue());
     75   EXPECT_EQ(7, APSInt(APInt(4, 7), true).getExtValue());
     76   EXPECT_EQ(7, APSInt(APInt(4, 7), false).getExtValue());
     77   EXPECT_EQ(9, APSInt(APInt(4, -7), true).getExtValue());
     78   EXPECT_EQ(-7, APSInt(APInt(4, -7), false).getExtValue());
     79 }
     80 
     81 TEST(APSIntTest, compareValues) {
     82   auto U = [](uint64_t V) { return APSInt::getUnsigned(V); };
     83   auto S = [](int64_t V) { return APSInt::get(V); };
     84 
     85   // Bit-width matches and is-signed.
     86   EXPECT_TRUE(APSInt::compareValues(S(7), S(8)) < 0);
     87   EXPECT_TRUE(APSInt::compareValues(S(8), S(7)) > 0);
     88   EXPECT_TRUE(APSInt::compareValues(S(7), S(7)) == 0);
     89   EXPECT_TRUE(APSInt::compareValues(S(-7), S(8)) < 0);
     90   EXPECT_TRUE(APSInt::compareValues(S(8), S(-7)) > 0);
     91   EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7)) == 0);
     92   EXPECT_TRUE(APSInt::compareValues(S(-7), S(-8)) > 0);
     93   EXPECT_TRUE(APSInt::compareValues(S(-8), S(-7)) < 0);
     94   EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7)) == 0);
     95 
     96   // Bit-width matches and not is-signed.
     97   EXPECT_TRUE(APSInt::compareValues(U(7), U(8)) < 0);
     98   EXPECT_TRUE(APSInt::compareValues(U(8), U(7)) > 0);
     99   EXPECT_TRUE(APSInt::compareValues(U(7), U(7)) == 0);
    100 
    101   // Bit-width matches and mixed signs.
    102   EXPECT_TRUE(APSInt::compareValues(U(7), S(8)) < 0);
    103   EXPECT_TRUE(APSInt::compareValues(U(8), S(7)) > 0);
    104   EXPECT_TRUE(APSInt::compareValues(U(7), S(7)) == 0);
    105   EXPECT_TRUE(APSInt::compareValues(U(8), S(-7)) > 0);
    106 
    107   // Bit-width mismatch and is-signed.
    108   EXPECT_TRUE(APSInt::compareValues(S(7).trunc(32), S(8)) < 0);
    109   EXPECT_TRUE(APSInt::compareValues(S(8).trunc(32), S(7)) > 0);
    110   EXPECT_TRUE(APSInt::compareValues(S(7).trunc(32), S(7)) == 0);
    111   EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(8)) < 0);
    112   EXPECT_TRUE(APSInt::compareValues(S(8).trunc(32), S(-7)) > 0);
    113   EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(-7)) == 0);
    114   EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(-8)) > 0);
    115   EXPECT_TRUE(APSInt::compareValues(S(-8).trunc(32), S(-7)) < 0);
    116   EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(-7)) == 0);
    117   EXPECT_TRUE(APSInt::compareValues(S(7), S(8).trunc(32)) < 0);
    118   EXPECT_TRUE(APSInt::compareValues(S(8), S(7).trunc(32)) > 0);
    119   EXPECT_TRUE(APSInt::compareValues(S(7), S(7).trunc(32)) == 0);
    120   EXPECT_TRUE(APSInt::compareValues(S(-7), S(8).trunc(32)) < 0);
    121   EXPECT_TRUE(APSInt::compareValues(S(8), S(-7).trunc(32)) > 0);
    122   EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7).trunc(32)) == 0);
    123   EXPECT_TRUE(APSInt::compareValues(S(-7), S(-8).trunc(32)) > 0);
    124   EXPECT_TRUE(APSInt::compareValues(S(-8), S(-7).trunc(32)) < 0);
    125   EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7).trunc(32)) == 0);
    126 
    127   // Bit-width mismatch and not is-signed.
    128   EXPECT_TRUE(APSInt::compareValues(U(7), U(8).trunc(32)) < 0);
    129   EXPECT_TRUE(APSInt::compareValues(U(8), U(7).trunc(32)) > 0);
    130   EXPECT_TRUE(APSInt::compareValues(U(7), U(7).trunc(32)) == 0);
    131   EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), U(8)) < 0);
    132   EXPECT_TRUE(APSInt::compareValues(U(8).trunc(32), U(7)) > 0);
    133   EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), U(7)) == 0);
    134 
    135   // Bit-width mismatch and mixed signs.
    136   EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), S(8)) < 0);
    137   EXPECT_TRUE(APSInt::compareValues(U(8).trunc(32), S(7)) > 0);
    138   EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), S(7)) == 0);
    139   EXPECT_TRUE(APSInt::compareValues(U(8).trunc(32), S(-7)) > 0);
    140   EXPECT_TRUE(APSInt::compareValues(U(7), S(8).trunc(32)) < 0);
    141   EXPECT_TRUE(APSInt::compareValues(U(8), S(7).trunc(32)) > 0);
    142   EXPECT_TRUE(APSInt::compareValues(U(7), S(7).trunc(32)) == 0);
    143   EXPECT_TRUE(APSInt::compareValues(U(8), S(-7).trunc(32)) > 0);
    144 }
    145 
    146 TEST(APSIntTest, FromString) {
    147   EXPECT_EQ(APSInt("1").getExtValue(), 1);
    148   EXPECT_EQ(APSInt("-1").getExtValue(), -1);
    149   EXPECT_EQ(APSInt("0").getExtValue(), 0);
    150   EXPECT_EQ(APSInt("56789").getExtValue(), 56789);
    151   EXPECT_EQ(APSInt("-1234").getExtValue(), -1234);
    152 }
    153 
    154 #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
    155 
    156 TEST(APSIntTest, StringDeath) {
    157   EXPECT_DEATH(APSInt(""), "Invalid string length");
    158   EXPECT_DEATH(APSInt("1a"), "Invalid character in digit string");
    159 }
    160 
    161 #endif
    162 
    163 } // end anonymous namespace
    164