Add getAsUnsignedInteger test case that checks that known bad values are rejected
[oota-llvm.git] / unittests / VMCore / ConstantsTest.cpp
index c40dc7e47155aaa599ad5a6fe3fe321c7cc7e5d5..623ea0d10290b9c5c930e19cb290ada8155250c7 100644 (file)
@@ -9,16 +9,18 @@
 
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
+#include "llvm/LLVMContext.h"
 #include "gtest/gtest.h"
 
 namespace llvm {
 namespace {
 
 TEST(ConstantsTest, Integer_i1) {
-  const IntegerType* Int1 = IntegerType::get(1);
+  IntegerType* Int1 = IntegerType::get(getGlobalContext(), 1);
   Constant* One = ConstantInt::get(Int1, 1, true);
   Constant* Zero = ConstantInt::get(Int1, 0);
   Constant* NegOne = ConstantInt::get(Int1, static_cast<uint64_t>(-1), true);
+  EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1));
   Constant* Undef = UndefValue::get(Int1);
 
   // Input:  @b = constant i1 add(i1 1 , i1 1)
@@ -94,5 +96,27 @@ TEST(ConstantsTest, Integer_i1) {
   EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne));
 }
 
+TEST(ConstantsTest, IntSigns) {
+  IntegerType* Int8Ty = Type::getInt8Ty(getGlobalContext());
+  EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue());
+  EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue());
+  EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue());
+  EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue());
+  EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue());
+  EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue());
+
+  // Overflow is handled by truncation.
+  EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue());
+}
+
+TEST(ConstantsTest, FP128Test) {
+  Type *FP128Ty = Type::getFP128Ty(getGlobalContext());
+
+  IntegerType *Int128Ty = Type::getIntNTy(getGlobalContext(), 128);
+  Constant *Zero128 = Constant::getNullValue(Int128Ty);
+  Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty);
+  EXPECT_TRUE(isa<ConstantFP>(X));
+}
+
 }  // end anonymous namespace
 }  // end namespace llvm