diff options
Diffstat (limited to 'unittests/ADT')
-rw-r--r-- | unittests/ADT/APFloatTest.cpp | 49 | ||||
-rw-r--r-- | unittests/ADT/SmallVectorTest.cpp | 48 | ||||
-rw-r--r-- | unittests/ADT/StringMapTest.cpp | 6 |
3 files changed, 79 insertions, 24 deletions
diff --git a/unittests/ADT/APFloatTest.cpp b/unittests/ADT/APFloatTest.cpp index 5f05b86..08ac2a0 100644 --- a/unittests/ADT/APFloatTest.cpp +++ b/unittests/ADT/APFloatTest.cpp @@ -12,6 +12,7 @@ #include "llvm/Support/raw_ostream.h" #include "gtest/gtest.h" #include "llvm/ADT/APFloat.h" +#include "llvm/ADT/APSInt.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" @@ -344,6 +345,54 @@ TEST(APFloatTest, toString) { ASSERT_EQ("8.731834E+2", convertToString(873.1834, 0, 0)); } +TEST(APFloatTest, toInteger) { + bool isExact = false; + APSInt result(5, /*isUnsigned=*/true); + + EXPECT_EQ(APFloat::opOK, + APFloat(APFloat::IEEEdouble, "10") + .convertToInteger(result, APFloat::rmTowardZero, &isExact)); + EXPECT_TRUE(isExact); + EXPECT_EQ(APSInt(APInt(5, 10), true), result); + + EXPECT_EQ(APFloat::opInvalidOp, + APFloat(APFloat::IEEEdouble, "-10") + .convertToInteger(result, APFloat::rmTowardZero, &isExact)); + EXPECT_FALSE(isExact); + EXPECT_EQ(APSInt::getMinValue(5, true), result); + + EXPECT_EQ(APFloat::opInvalidOp, + APFloat(APFloat::IEEEdouble, "32") + .convertToInteger(result, APFloat::rmTowardZero, &isExact)); + EXPECT_FALSE(isExact); + EXPECT_EQ(APSInt::getMaxValue(5, true), result); + + EXPECT_EQ(APFloat::opInexact, + APFloat(APFloat::IEEEdouble, "7.9") + .convertToInteger(result, APFloat::rmTowardZero, &isExact)); + EXPECT_FALSE(isExact); + EXPECT_EQ(APSInt(APInt(5, 7), true), result); + + result.setIsUnsigned(false); + EXPECT_EQ(APFloat::opOK, + APFloat(APFloat::IEEEdouble, "-10") + .convertToInteger(result, APFloat::rmTowardZero, &isExact)); + EXPECT_TRUE(isExact); + EXPECT_EQ(APSInt(APInt(5, -10, true), false), result); + + EXPECT_EQ(APFloat::opInvalidOp, + APFloat(APFloat::IEEEdouble, "-17") + .convertToInteger(result, APFloat::rmTowardZero, &isExact)); + EXPECT_FALSE(isExact); + EXPECT_EQ(APSInt::getMinValue(5, false), result); + + EXPECT_EQ(APFloat::opInvalidOp, + APFloat(APFloat::IEEEdouble, "16") + .convertToInteger(result, APFloat::rmTowardZero, &isExact)); + EXPECT_FALSE(isExact); + EXPECT_EQ(APSInt::getMaxValue(5, false), result); +} + static APInt nanbits(const fltSemantics &Sem, bool SNaN, bool Negative, uint64_t fill) { APInt apfill(64, fill); diff --git a/unittests/ADT/SmallVectorTest.cpp b/unittests/ADT/SmallVectorTest.cpp index f4da54d..0d3535d 100644 --- a/unittests/ADT/SmallVectorTest.cpp +++ b/unittests/ADT/SmallVectorTest.cpp @@ -35,26 +35,26 @@ public: Constructable() : value(0) { ++numConstructorCalls; } - + Constructable(int val) : value(val) { ++numConstructorCalls; } - + Constructable(const Constructable & src) { value = src.value; ++numConstructorCalls; } - + ~Constructable() { ++numDestructorCalls; } - + Constructable & operator=(const Constructable & src) { value = src.value; ++numAssignmentCalls; return *this; } - + int getValue() const { return abs(value); } @@ -64,7 +64,7 @@ public: numDestructorCalls = 0; numAssignmentCalls = 0; } - + static int getNumConstructorCalls() { return numConstructorCalls; } @@ -91,10 +91,10 @@ int Constructable::numAssignmentCalls; class SmallVectorTest : public testing::Test { protected: typedef SmallVector<Constructable, 4> VectorType; - + VectorType theVector; VectorType otherVector; - + void SetUp() { Constructable::reset(); } @@ -111,7 +111,7 @@ protected: // Assert that theVector contains the specified values, in order. void assertValuesInOrder(VectorType & v, size_t size, ...) { EXPECT_EQ(size, v.size()); - + va_list ap; va_start(ap, size); for (size_t i = 0; i < size; ++i) { @@ -121,7 +121,7 @@ protected: va_end(ap); } - + // Generate a sequence of values to initialize the vector. void makeSequence(VectorType & v, int start, int end) { for (int i = start; i <= end; ++i) { @@ -155,18 +155,24 @@ TEST_F(SmallVectorTest, PushPopTest) { theVector.push_back(Constructable(2)); assertValuesInOrder(theVector, 2u, 1, 2); + // Insert at beginning + theVector.insert(theVector.begin(), theVector[1]); + assertValuesInOrder(theVector, 3u, 2, 1, 2); + // Pop one element theVector.pop_back(); - assertValuesInOrder(theVector, 1u, 1); + assertValuesInOrder(theVector, 2u, 2, 1); - // Pop another element + // Pop remaining elements + theVector.pop_back(); theVector.pop_back(); assertEmpty(theVector); - + // Check number of constructor calls. Should be 2 for each list element, - // one for the argument to push_back, and one for the list element itself. - EXPECT_EQ(4, Constructable::getNumConstructorCalls()); - EXPECT_EQ(4, Constructable::getNumDestructorCalls()); + // one for the argument to push_back, one for the argument to insert, + // and one for the list element itself. + EXPECT_EQ(5, Constructable::getNumConstructorCalls()); + EXPECT_EQ(5, Constructable::getNumDestructorCalls()); } // Clear test. @@ -198,7 +204,7 @@ TEST_F(SmallVectorTest, ResizeGrowTest) { SCOPED_TRACE("ResizeGrowTest"); theVector.resize(2); - + // The extra constructor/destructor calls come from the temporary object used // to initialize the contents of the resized array (via copy construction). EXPECT_EQ(3, Constructable::getNumConstructorCalls()); @@ -226,10 +232,10 @@ TEST_F(SmallVectorTest, OverflowTest) { for (int i = 0; i < 10; ++i) { EXPECT_EQ(i+1, theVector[i].getValue()); } - + // Now resize back to fixed size. theVector.resize(1); - + assertValuesInOrder(theVector, 1u, 1); } @@ -364,13 +370,13 @@ TEST_F(SmallVectorTest, ComparisonTest) { makeSequence(theVector, 1, 3); makeSequence(otherVector, 1, 3); - + EXPECT_TRUE(theVector == otherVector); EXPECT_FALSE(theVector != otherVector); otherVector.clear(); makeSequence(otherVector, 2, 4); - + EXPECT_FALSE(theVector == otherVector); EXPECT_TRUE(theVector != otherVector); } diff --git a/unittests/ADT/StringMapTest.cpp b/unittests/ADT/StringMapTest.cpp index ea91348..2ae5820 100644 --- a/unittests/ADT/StringMapTest.cpp +++ b/unittests/ADT/StringMapTest.cpp @@ -51,7 +51,7 @@ protected: // Iterator tests StringMap<uint32_t>::iterator it = testMap.begin(); - EXPECT_STREQ(testKey, it->first()); + EXPECT_STREQ(testKey, it->first().data()); EXPECT_EQ(testValue, it->second); ++it; EXPECT_TRUE(it == testMap.end()); @@ -157,7 +157,7 @@ TEST_F(StringMapTest, IterationTest) { it != testMap.end(); ++it) { std::stringstream ss; ss << "key_" << it->second; - ASSERT_STREQ(ss.str().c_str(), it->first()); + ASSERT_STREQ(ss.str().c_str(), it->first().data()); visited[it->second] = true; } @@ -189,7 +189,7 @@ TEST_F(StringMapTest, StringMapEntryTest) { StringMap<uint32_t>::value_type* entry = StringMap<uint32_t>::value_type::Create( testKeyFirst, testKeyFirst + testKeyLength, 1u); - EXPECT_STREQ(testKey, entry->first()); + EXPECT_STREQ(testKey, entry->first().data()); EXPECT_EQ(1u, entry->second); free(entry); } |