diff options
Diffstat (limited to 'unittests/ADT')
-rw-r--r-- | unittests/ADT/APIntTest.cpp | 188 | ||||
-rw-r--r-- | unittests/ADT/DenseMapTest.cpp | 167 | ||||
-rw-r--r-- | unittests/ADT/DenseSetTest.cpp | 30 | ||||
-rw-r--r-- | unittests/ADT/ImmutableSetTest.cpp | 201 | ||||
-rw-r--r-- | unittests/ADT/Makefile | 15 | ||||
-rw-r--r-- | unittests/ADT/SmallVectorTest.cpp | 383 | ||||
-rw-r--r-- | unittests/ADT/StringMapTest.cpp | 202 | ||||
-rw-r--r-- | unittests/ADT/TripleTest.cpp | 137 |
8 files changed, 1323 insertions, 0 deletions
diff --git a/unittests/ADT/APIntTest.cpp b/unittests/ADT/APIntTest.cpp new file mode 100644 index 0000000..8ff7d1d --- /dev/null +++ b/unittests/ADT/APIntTest.cpp @@ -0,0 +1,188 @@ +//===- llvm/unittest/ADT/APInt.cpp - APInt unit tests ---------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include <ostream> +#include "llvm/Support/raw_ostream.h" +#include "gtest/gtest.h" +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/SmallString.h" + +using namespace llvm; + +namespace { + +// Make the Google Test failure output equivalent to APInt::dump() +std::ostream& operator<<(std::ostream &OS, const llvm::APInt& I) { + llvm::raw_os_ostream raw_os(OS); + + SmallString<40> S, U; + I.toStringUnsigned(U); + I.toStringSigned(S); + raw_os << "APInt(" << I.getBitWidth()<< "b, " + << U.c_str() << "u " << S.c_str() << "s)"; + raw_os.flush(); + return OS; +} + +// Test that APInt shift left works when bitwidth > 64 and shiftamt == 0 +TEST(APIntTest, ShiftLeftByZero) { + APInt One = APInt::getNullValue(65) + 1; + APInt Shl = One.shl(0); + EXPECT_EQ(true, Shl[0]); + EXPECT_EQ(false, Shl[1]); +} + +TEST(APIntTest, i128_NegativeCount) { + APInt Minus3(128, static_cast<uint64_t>(-3), true); + EXPECT_EQ(126u, Minus3.countLeadingOnes()); + EXPECT_EQ(-3, Minus3.getSExtValue()); + + APInt Minus1(128, static_cast<uint64_t>(-1), true); + EXPECT_EQ(0u, Minus1.countLeadingZeros()); + EXPECT_EQ(128u, Minus1.countLeadingOnes()); + EXPECT_EQ(128u, Minus1.getActiveBits()); + EXPECT_EQ(0u, Minus1.countTrailingZeros()); + EXPECT_EQ(128u, Minus1.countTrailingOnes()); + EXPECT_EQ(128u, Minus1.countPopulation()); + EXPECT_EQ(-1, Minus1.getSExtValue()); +} + +TEST(APIntTest, i33_Count) { + APInt i33minus2(33, static_cast<uint64_t>(-2), true); + EXPECT_EQ(0u, i33minus2.countLeadingZeros()); + EXPECT_EQ(32u, i33minus2.countLeadingOnes()); + EXPECT_EQ(33u, i33minus2.getActiveBits()); + EXPECT_EQ(1u, i33minus2.countTrailingZeros()); + EXPECT_EQ(32u, i33minus2.countPopulation()); + EXPECT_EQ(-2, i33minus2.getSExtValue()); + EXPECT_EQ(((uint64_t)-2)&((1ull<<33) -1), i33minus2.getZExtValue()); +} + +TEST(APIntTest, i65_Count) { + APInt i65minus(65, 0, true); + i65minus.set(64); + EXPECT_EQ(0u, i65minus.countLeadingZeros()); + EXPECT_EQ(1u, i65minus.countLeadingOnes()); + EXPECT_EQ(65u, i65minus.getActiveBits()); + EXPECT_EQ(64u, i65minus.countTrailingZeros()); + EXPECT_EQ(1u, i65minus.countPopulation()); +} + +TEST(APIntTest, i128_PositiveCount) { + APInt u128max = APInt::getAllOnesValue(128); + EXPECT_EQ(128u, u128max.countLeadingOnes()); + EXPECT_EQ(0u, u128max.countLeadingZeros()); + EXPECT_EQ(128u, u128max.getActiveBits()); + EXPECT_EQ(0u, u128max.countTrailingZeros()); + EXPECT_EQ(128u, u128max.countTrailingOnes()); + EXPECT_EQ(128u, u128max.countPopulation()); + + APInt u64max(128, static_cast<uint64_t>(-1), false); + EXPECT_EQ(64u, u64max.countLeadingZeros()); + EXPECT_EQ(0u, u64max.countLeadingOnes()); + EXPECT_EQ(64u, u64max.getActiveBits()); + EXPECT_EQ(0u, u64max.countTrailingZeros()); + EXPECT_EQ(64u, u64max.countTrailingOnes()); + EXPECT_EQ(64u, u64max.countPopulation()); + EXPECT_EQ((uint64_t)~0ull, u64max.getZExtValue()); + + APInt zero(128, 0, true); + EXPECT_EQ(128u, zero.countLeadingZeros()); + EXPECT_EQ(0u, zero.countLeadingOnes()); + EXPECT_EQ(0u, zero.getActiveBits()); + EXPECT_EQ(128u, zero.countTrailingZeros()); + EXPECT_EQ(0u, zero.countTrailingOnes()); + EXPECT_EQ(0u, zero.countPopulation()); + EXPECT_EQ(0u, zero.getSExtValue()); + EXPECT_EQ(0u, zero.getZExtValue()); + + APInt one(128, 1, true); + EXPECT_EQ(127u, one.countLeadingZeros()); + EXPECT_EQ(0u, one.countLeadingOnes()); + EXPECT_EQ(1u, one.getActiveBits()); + EXPECT_EQ(0u, one.countTrailingZeros()); + EXPECT_EQ(1u, one.countTrailingOnes()); + EXPECT_EQ(1u, one.countPopulation()); + EXPECT_EQ(1, one.getSExtValue()); + EXPECT_EQ(1u, one.getZExtValue()); +} + +TEST(APIntTest, i1) { + const APInt neg_two(1, static_cast<uint64_t>(-2), true); + const APInt neg_one(1, static_cast<uint64_t>(-1), true); + const APInt zero(1, 0); + const APInt one(1, 1); + const APInt two(1, 2); + + EXPECT_EQ(0, neg_two.getSExtValue()); + EXPECT_EQ(-1, neg_one.getSExtValue()); + EXPECT_EQ(1u, neg_one.getZExtValue()); + EXPECT_EQ(0u, zero.getZExtValue()); + EXPECT_EQ(-1, one.getSExtValue()); + EXPECT_EQ(1u, one.getZExtValue()); + EXPECT_EQ(0u, two.getZExtValue()); + EXPECT_EQ(0, two.getSExtValue()); + + // Basic equalities for 1-bit values. + EXPECT_EQ(zero, two); + EXPECT_EQ(zero, neg_two); + EXPECT_EQ(one, neg_one); + EXPECT_EQ(two, neg_two); + + // Additions. + EXPECT_EQ(two, one + one); + EXPECT_EQ(zero, neg_one + one); + EXPECT_EQ(neg_two, neg_one + neg_one); + + // Subtractions. + EXPECT_EQ(neg_two, neg_one - one); + EXPECT_EQ(two, one - neg_one); + EXPECT_EQ(zero, one - one); + + // Shifts. + EXPECT_EQ(zero, one << one); + EXPECT_EQ(one, one << zero); + EXPECT_EQ(zero, one.shl(1)); + EXPECT_EQ(one, one.shl(0)); + EXPECT_EQ(zero, one.lshr(1)); + EXPECT_EQ(zero, one.ashr(1)); + + // Multiplies. + EXPECT_EQ(neg_one, neg_one * one); + EXPECT_EQ(neg_one, one * neg_one); + EXPECT_EQ(one, neg_one * neg_one); + EXPECT_EQ(one, one * one); + + // Divides. + EXPECT_EQ(neg_one, one.sdiv(neg_one)); + EXPECT_EQ(neg_one, neg_one.sdiv(one)); + EXPECT_EQ(one, neg_one.sdiv(neg_one)); + EXPECT_EQ(one, one.sdiv(one)); + + EXPECT_EQ(neg_one, one.udiv(neg_one)); + EXPECT_EQ(neg_one, neg_one.udiv(one)); + EXPECT_EQ(one, neg_one.udiv(neg_one)); + EXPECT_EQ(one, one.udiv(one)); + + // Remainders. + EXPECT_EQ(zero, neg_one.srem(one)); + EXPECT_EQ(zero, neg_one.urem(one)); + EXPECT_EQ(zero, one.srem(neg_one)); +} + +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)); +} + +} diff --git a/unittests/ADT/DenseMapTest.cpp b/unittests/ADT/DenseMapTest.cpp new file mode 100644 index 0000000..15a5379 --- /dev/null +++ b/unittests/ADT/DenseMapTest.cpp @@ -0,0 +1,167 @@ +//===- llvm/unittest/ADT/DenseMapMap.cpp - DenseMap unit tests --*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "gtest/gtest.h" +#include "llvm/ADT/DenseMap.h" + +using namespace llvm; + +namespace { + +// Test fixture +class DenseMapTest : public testing::Test { +protected: + DenseMap<uint32_t, uint32_t> uintMap; + DenseMap<uint32_t *, uint32_t *> uintPtrMap; + uint32_t dummyInt; +}; + +// Empty map tests +TEST_F(DenseMapTest, EmptyIntMapTest) { + // Size tests + EXPECT_EQ(0u, uintMap.size()); + EXPECT_TRUE(uintMap.empty()); + + // Iterator tests + EXPECT_TRUE(uintMap.begin() == uintMap.end()); + + // Lookup tests + EXPECT_FALSE(uintMap.count(0u)); + EXPECT_TRUE(uintMap.find(0u) == uintMap.end()); + EXPECT_EQ(0u, uintMap.lookup(0u)); +} + +// Empty map tests for pointer map +TEST_F(DenseMapTest, EmptyPtrMapTest) { + // Size tests + EXPECT_EQ(0u, uintPtrMap.size()); + EXPECT_TRUE(uintPtrMap.empty()); + + // Iterator tests + EXPECT_TRUE(uintPtrMap.begin() == uintPtrMap.end()); + + // Lookup tests + EXPECT_FALSE(uintPtrMap.count(&dummyInt)); + EXPECT_TRUE(uintPtrMap.find(&dummyInt) == uintPtrMap.begin()); + EXPECT_EQ(0, uintPtrMap.lookup(&dummyInt)); +} + +// Constant map tests +TEST_F(DenseMapTest, ConstEmptyMapTest) { + const DenseMap<uint32_t, uint32_t> & constUintMap = uintMap; + const DenseMap<uint32_t *, uint32_t *> & constUintPtrMap = uintPtrMap; + EXPECT_EQ(0u, constUintMap.size()); + EXPECT_EQ(0u, constUintPtrMap.size()); + EXPECT_TRUE(constUintMap.empty()); + EXPECT_TRUE(constUintPtrMap.empty()); + EXPECT_TRUE(constUintMap.begin() == constUintMap.end()); + EXPECT_TRUE(constUintPtrMap.begin() == constUintPtrMap.end()); +} + +// A map with a single entry +TEST_F(DenseMapTest, SingleEntryMapTest) { + uintMap[0] = 1; + + // Size tests + EXPECT_EQ(1u, uintMap.size()); + EXPECT_FALSE(uintMap.begin() == uintMap.end()); + EXPECT_FALSE(uintMap.empty()); + + // Iterator tests + DenseMap<uint32_t, uint32_t>::iterator it = uintMap.begin(); + EXPECT_EQ(0u, it->first); + EXPECT_EQ(1u, it->second); + ++it; + EXPECT_TRUE(it == uintMap.end()); + + // Lookup tests + EXPECT_TRUE(uintMap.count(0u)); + EXPECT_TRUE(uintMap.find(0u) == uintMap.begin()); + EXPECT_EQ(1u, uintMap.lookup(0u)); + EXPECT_EQ(1u, uintMap[0]); +} + +// Test clear() method +TEST_F(DenseMapTest, ClearTest) { + uintMap[0] = 1; + uintMap.clear(); + + EXPECT_EQ(0u, uintMap.size()); + EXPECT_TRUE(uintMap.empty()); + EXPECT_TRUE(uintMap.begin() == uintMap.end()); +} + +// Test erase(iterator) method +TEST_F(DenseMapTest, EraseTest) { + uintMap[0] = 1; + uintMap.erase(uintMap.begin()); + + EXPECT_EQ(0u, uintMap.size()); + EXPECT_TRUE(uintMap.empty()); + EXPECT_TRUE(uintMap.begin() == uintMap.end()); +} + +// Test erase(value) method +TEST_F(DenseMapTest, EraseTest2) { + uintMap[0] = 1; + uintMap.erase(0); + + EXPECT_EQ(0u, uintMap.size()); + EXPECT_TRUE(uintMap.empty()); + EXPECT_TRUE(uintMap.begin() == uintMap.end()); +} + +// Test insert() method +TEST_F(DenseMapTest, InsertTest) { + uintMap.insert(std::make_pair(0u, 1u)); + EXPECT_EQ(1u, uintMap.size()); + EXPECT_EQ(1u, uintMap[0]); +} + +// Test copy constructor method +TEST_F(DenseMapTest, CopyConstructorTest) { + uintMap[0] = 1; + DenseMap<uint32_t, uint32_t> copyMap(uintMap); + + EXPECT_EQ(1u, copyMap.size()); + EXPECT_EQ(1u, copyMap[0]); +} + +// Test assignment operator method +TEST_F(DenseMapTest, AssignmentTest) { + uintMap[0] = 1; + DenseMap<uint32_t, uint32_t> copyMap = uintMap; + + EXPECT_EQ(1u, copyMap.size()); + EXPECT_EQ(1u, copyMap[0]); +} + +// A more complex iteration test +TEST_F(DenseMapTest, IterationTest) { + bool visited[100]; + + // Insert 100 numbers into the map + for (int i = 0; i < 100; ++i) { + visited[i] = false; + uintMap[i] = 3; + } + + // Iterate over all numbers and mark each one found. + for (DenseMap<uint32_t, uint32_t>::iterator it = uintMap.begin(); + it != uintMap.end(); ++it) { + visited[it->first] = true; + } + + // Ensure every number was visited. + for (int i = 0; i < 100; ++i) { + ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited"; + } +} + +} diff --git a/unittests/ADT/DenseSetTest.cpp b/unittests/ADT/DenseSetTest.cpp new file mode 100644 index 0000000..7a35f52 --- /dev/null +++ b/unittests/ADT/DenseSetTest.cpp @@ -0,0 +1,30 @@ +//===- llvm/unittest/ADT/DenseSetTest.cpp - DenseSet unit tests --*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "gtest/gtest.h" +#include <llvm/ADT/DenseSet.h> + +using namespace llvm; + +namespace { + +// Test fixture +class DenseSetTest : public testing::Test { +}; + +// Test hashing with a set of only two entries. +TEST_F(DenseSetTest, DoubleEntrySetTest) { + llvm::DenseSet<unsigned> set(2); + set.insert(0); + set.insert(1); + // Original failure was an infinite loop in this call: + EXPECT_EQ(0, set.count(2)); +} + +} diff --git a/unittests/ADT/ImmutableSetTest.cpp b/unittests/ADT/ImmutableSetTest.cpp new file mode 100644 index 0000000..1be510d --- /dev/null +++ b/unittests/ADT/ImmutableSetTest.cpp @@ -0,0 +1,201 @@ +//===----------- ImmutableSetTest.cpp - ImmutableSet unit tests ------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "gtest/gtest.h" +#include "llvm/ADT/ImmutableSet.h" + +using namespace llvm; + +namespace { +class ImmutableSetTest : public testing::Test { +protected: + // for callback tests + static char buffer[10]; + + struct MyIter { + int counter; + char *ptr; + + MyIter() : counter(0), ptr(buffer) { + for (unsigned i=0; i<sizeof(buffer);++i) buffer[i]='\0'; + } + void operator()(char c) { + *ptr++ = c; + ++counter; + } + }; +}; +char ImmutableSetTest::buffer[10]; + + +TEST_F(ImmutableSetTest, EmptyIntSetTest) { + ImmutableSet<int>::Factory f; + + EXPECT_TRUE(f.GetEmptySet() == f.GetEmptySet()); + EXPECT_FALSE(f.GetEmptySet() != f.GetEmptySet()); + EXPECT_TRUE(f.GetEmptySet().isEmpty()); + + ImmutableSet<int> S = f.GetEmptySet(); + EXPECT_EQ(0u, S.getHeight()); + EXPECT_TRUE(S.begin() == S.end()); + EXPECT_FALSE(S.begin() != S.end()); +} + + +TEST_F(ImmutableSetTest, OneElemIntSetTest) { + ImmutableSet<int>::Factory f; + ImmutableSet<int> S = f.GetEmptySet(); + + ImmutableSet<int> S2 = f.Add(S, 3); + EXPECT_TRUE(S.isEmpty()); + EXPECT_FALSE(S2.isEmpty()); + EXPECT_FALSE(S == S2); + EXPECT_TRUE(S != S2); + EXPECT_FALSE(S.contains(3)); + EXPECT_TRUE(S2.contains(3)); + EXPECT_FALSE(S2.begin() == S2.end()); + EXPECT_TRUE(S2.begin() != S2.end()); + + ImmutableSet<int> S3 = f.Add(S, 2); + EXPECT_TRUE(S.isEmpty()); + EXPECT_FALSE(S3.isEmpty()); + EXPECT_FALSE(S == S3); + EXPECT_TRUE(S != S3); + EXPECT_FALSE(S.contains(2)); + EXPECT_TRUE(S3.contains(2)); + + EXPECT_FALSE(S2 == S3); + EXPECT_TRUE(S2 != S3); + EXPECT_FALSE(S2.contains(2)); + EXPECT_FALSE(S3.contains(3)); +} + +TEST_F(ImmutableSetTest, MultiElemIntSetTest) { + ImmutableSet<int>::Factory f; + ImmutableSet<int> S = f.GetEmptySet(); + + ImmutableSet<int> S2 = f.Add(f.Add(f.Add(S, 3), 4), 5); + ImmutableSet<int> S3 = f.Add(f.Add(f.Add(S2, 9), 20), 43); + ImmutableSet<int> S4 = f.Add(S2, 9); + + EXPECT_TRUE(S.isEmpty()); + EXPECT_FALSE(S2.isEmpty()); + EXPECT_FALSE(S3.isEmpty()); + EXPECT_FALSE(S4.isEmpty()); + + EXPECT_FALSE(S.contains(3)); + EXPECT_FALSE(S.contains(9)); + + EXPECT_TRUE(S2.contains(3)); + EXPECT_TRUE(S2.contains(4)); + EXPECT_TRUE(S2.contains(5)); + EXPECT_FALSE(S2.contains(9)); + EXPECT_FALSE(S2.contains(0)); + + EXPECT_TRUE(S3.contains(43)); + EXPECT_TRUE(S3.contains(20)); + EXPECT_TRUE(S3.contains(9)); + EXPECT_TRUE(S3.contains(3)); + EXPECT_TRUE(S3.contains(4)); + EXPECT_TRUE(S3.contains(5)); + EXPECT_FALSE(S3.contains(0)); + + EXPECT_TRUE(S4.contains(9)); + EXPECT_TRUE(S4.contains(3)); + EXPECT_TRUE(S4.contains(4)); + EXPECT_TRUE(S4.contains(5)); + EXPECT_FALSE(S4.contains(20)); + EXPECT_FALSE(S4.contains(43)); +} + +TEST_F(ImmutableSetTest, RemoveIntSetTest) { + ImmutableSet<int>::Factory f; + ImmutableSet<int> S = f.GetEmptySet(); + + ImmutableSet<int> S2 = f.Add(f.Add(S, 4), 5); + ImmutableSet<int> S3 = f.Add(S2, 3); + ImmutableSet<int> S4 = f.Remove(S3, 3); + + EXPECT_TRUE(S3.contains(3)); + EXPECT_FALSE(S2.contains(3)); + EXPECT_FALSE(S4.contains(3)); + + EXPECT_TRUE(S2 == S4); + EXPECT_TRUE(S3 != S2); + EXPECT_TRUE(S3 != S4); + + EXPECT_TRUE(S3.contains(4)); + EXPECT_TRUE(S3.contains(5)); + + EXPECT_TRUE(S4.contains(4)); + EXPECT_TRUE(S4.contains(5)); +} + +TEST_F(ImmutableSetTest, CallbackCharSetTest) { + ImmutableSet<char>::Factory f; + ImmutableSet<char> S = f.GetEmptySet(); + + ImmutableSet<char> S2 = f.Add(f.Add(f.Add(S, 'a'), 'e'), 'i'); + ImmutableSet<char> S3 = f.Add(f.Add(S2, 'o'), 'u'); + + S3.foreach<MyIter>(); + + ASSERT_STREQ("aeiou", buffer); +} + +TEST_F(ImmutableSetTest, Callback2CharSetTest) { + ImmutableSet<char>::Factory f; + ImmutableSet<char> S = f.GetEmptySet(); + + ImmutableSet<char> S2 = f.Add(f.Add(f.Add(S, 'b'), 'c'), 'd'); + ImmutableSet<char> S3 = f.Add(f.Add(f.Add(S2, 'f'), 'g'), 'h'); + + MyIter obj; + S3.foreach<MyIter>(obj); + ASSERT_STREQ("bcdfgh", buffer); + ASSERT_EQ(6, obj.counter); + + MyIter obj2; + S2.foreach<MyIter>(obj2); + ASSERT_STREQ("bcd", buffer); + ASSERT_EQ(3, obj2.counter); + + MyIter obj3; + S.foreach<MyIter>(obj); + ASSERT_STREQ("", buffer); + ASSERT_EQ(0, obj3.counter); +} + +TEST_F(ImmutableSetTest, IterLongSetTest) { + ImmutableSet<long>::Factory f; + ImmutableSet<long> S = f.GetEmptySet(); + + ImmutableSet<long> S2 = f.Add(f.Add(f.Add(S, 0), 1), 2); + ImmutableSet<long> S3 = f.Add(f.Add(f.Add(S2, 3), 4), 5); + + int i = 0; + for (ImmutableSet<long>::iterator I = S.begin(), E = S.end(); I != E; ++I) { + ASSERT_EQ(i++, *I); + } + ASSERT_EQ(0, i); + + i = 0; + for (ImmutableSet<long>::iterator I = S2.begin(), E = S2.end(); I != E; ++I) { + ASSERT_EQ(i++, *I); + } + ASSERT_EQ(3, i); + + i = 0; + for (ImmutableSet<long>::iterator I = S3.begin(), E = S3.end(); I != E; I++) { + ASSERT_EQ(i++, *I); + } + ASSERT_EQ(6, i); +} + +} diff --git a/unittests/ADT/Makefile b/unittests/ADT/Makefile new file mode 100644 index 0000000..c56b951 --- /dev/null +++ b/unittests/ADT/Makefile @@ -0,0 +1,15 @@ +##===- unittests/ADT/Makefile ------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. +TESTNAME = ADT +LINK_COMPONENTS := core support + +include $(LEVEL)/Makefile.config +include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest diff --git a/unittests/ADT/SmallVectorTest.cpp b/unittests/ADT/SmallVectorTest.cpp new file mode 100644 index 0000000..19ef099 --- /dev/null +++ b/unittests/ADT/SmallVectorTest.cpp @@ -0,0 +1,383 @@ +//===- llvm/unittest/ADT/SmallVectorTest.cpp ------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// SmallVector unit tests. +// +//===----------------------------------------------------------------------===// + +#include "gtest/gtest.h" +#include "llvm/ADT/SmallVector.h" +#include <stdarg.h> + +using namespace llvm; + +namespace { + +/// A helper class that counts the total number of constructor and +/// destructor calls. +class Constructable { +private: + static int numConstructorCalls; + static int numDestructorCalls; + static int numAssignmentCalls; + + int value; + +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); + } + + static void reset() { + numConstructorCalls = 0; + numDestructorCalls = 0; + numAssignmentCalls = 0; + } + + static int getNumConstructorCalls() { + return numConstructorCalls; + } + + static int getNumDestructorCalls() { + return numDestructorCalls; + } + + friend bool operator==(const Constructable & c0, const Constructable & c1) { + return c0.getValue() == c1.getValue(); + } + + friend bool operator!=(const Constructable & c0, const Constructable & c1) { + return c0.getValue() != c1.getValue(); + } +}; + +int Constructable::numConstructorCalls; +int Constructable::numDestructorCalls; +int Constructable::numAssignmentCalls; + +// Test fixture class +class SmallVectorTest : public testing::Test { +protected: + typedef SmallVector<Constructable, 4> VectorType; + + VectorType theVector; + VectorType otherVector; + + void SetUp() { + Constructable::reset(); + } + + void assertEmpty(VectorType & v) { + // Size tests + EXPECT_EQ(0u, v.size()); + EXPECT_TRUE(v.empty()); + + // Iterator tests + EXPECT_TRUE(v.begin() == v.end()); + } + + // 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) { + int value = va_arg(ap, int); + EXPECT_EQ(value, v[i].getValue()); + } + + 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) { + v.push_back(Constructable(i)); + } + } +}; + +// New vector test. +TEST_F(SmallVectorTest, EmptyVectorTest) { + SCOPED_TRACE("EmptyVectorTest"); + assertEmpty(theVector); + EXPECT_TRUE(theVector.rbegin() == theVector.rend()); + EXPECT_EQ(0, Constructable::getNumConstructorCalls()); + EXPECT_EQ(0, Constructable::getNumDestructorCalls()); +} + +// Simple insertions and deletions. +TEST_F(SmallVectorTest, PushPopTest) { + SCOPED_TRACE("PushPopTest"); + + // Push an element + theVector.push_back(Constructable(1)); + + // Size tests + assertValuesInOrder(theVector, 1u, 1); + EXPECT_FALSE(theVector.begin() == theVector.end()); + EXPECT_FALSE(theVector.empty()); + + // Push another element + theVector.push_back(Constructable(2)); + assertValuesInOrder(theVector, 2u, 1, 2); + + // Pop one element + theVector.pop_back(); + assertValuesInOrder(theVector, 1u, 1); + + // Pop another element + 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()); +} + +// Clear test. +TEST_F(SmallVectorTest, ClearTest) { + SCOPED_TRACE("ClearTest"); + + makeSequence(theVector, 1, 2); + theVector.clear(); + + assertEmpty(theVector); + EXPECT_EQ(4, Constructable::getNumConstructorCalls()); + EXPECT_EQ(4, Constructable::getNumDestructorCalls()); +} + +// Resize smaller test. +TEST_F(SmallVectorTest, ResizeShrinkTest) { + SCOPED_TRACE("ResizeShrinkTest"); + + makeSequence(theVector, 1, 3); + theVector.resize(1); + + assertValuesInOrder(theVector, 1u, 1); + EXPECT_EQ(6, Constructable::getNumConstructorCalls()); + EXPECT_EQ(5, Constructable::getNumDestructorCalls()); +} + +// Resize bigger test. +TEST_F(SmallVectorTest, ResizeGrowTest) { + SCOPED_TRACE("ResizeGrowTest"); + + theVector.resize(2); + + // XXX: I don't know where the extra construct/destruct is coming from. + EXPECT_EQ(3, Constructable::getNumConstructorCalls()); + EXPECT_EQ(1, Constructable::getNumDestructorCalls()); + EXPECT_EQ(2u, theVector.size()); +} + +// Resize with fill value. +TEST_F(SmallVectorTest, ResizeFillTest) { + SCOPED_TRACE("ResizeFillTest"); + + theVector.resize(3, Constructable(77)); + assertValuesInOrder(theVector, 3u, 77, 77, 77); +} + +// Overflow past fixed size. +TEST_F(SmallVectorTest, OverflowTest) { + SCOPED_TRACE("OverflowTest"); + + // Push more elements than the fixed size + makeSequence(theVector, 1, 10); + + // test size and values + EXPECT_EQ(10u, theVector.size()); + 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); +} + +// Iteration tests. +TEST_F(SmallVectorTest, IterationTest) { + makeSequence(theVector, 1, 2); + + // Forward Iteration + VectorType::iterator it = theVector.begin(); + EXPECT_TRUE(*it == theVector.front()); + EXPECT_TRUE(*it == theVector[0]); + EXPECT_EQ(1, it->getValue()); + ++it; + EXPECT_TRUE(*it == theVector[1]); + EXPECT_TRUE(*it == theVector.back()); + EXPECT_EQ(2, it->getValue()); + ++it; + EXPECT_TRUE(it == theVector.end()); + --it; + EXPECT_TRUE(*it == theVector[1]); + EXPECT_EQ(2, it->getValue()); + --it; + EXPECT_TRUE(*it == theVector[0]); + EXPECT_EQ(1, it->getValue()); + + // Reverse Iteration + VectorType::reverse_iterator rit = theVector.rbegin(); + EXPECT_TRUE(*rit == theVector[1]); + EXPECT_EQ(2, rit->getValue()); + ++rit; + EXPECT_TRUE(*rit == theVector[0]); + EXPECT_EQ(1, rit->getValue()); + ++rit; + EXPECT_TRUE(rit == theVector.rend()); + --rit; + EXPECT_TRUE(*rit == theVector[0]); + EXPECT_EQ(1, rit->getValue()); + --rit; + EXPECT_TRUE(*rit == theVector[1]); + EXPECT_EQ(2, rit->getValue()); +} + +// Swap test. +TEST_F(SmallVectorTest, SwapTest) { + SCOPED_TRACE("SwapTest"); + + makeSequence(theVector, 1, 2); + std::swap(theVector, otherVector); + + assertEmpty(theVector); + assertValuesInOrder(otherVector, 2u, 1, 2); +} + +// Append test +TEST_F(SmallVectorTest, AppendTest) { + SCOPED_TRACE("AppendTest"); + + makeSequence(otherVector, 2, 3); + + theVector.push_back(Constructable(1)); + theVector.append(otherVector.begin(), otherVector.end()); + + assertValuesInOrder(theVector, 3u, 1, 2, 3); +} + +// Append repeated test +TEST_F(SmallVectorTest, AppendRepeatedTest) { + SCOPED_TRACE("AppendRepeatedTest"); + + theVector.push_back(Constructable(1)); + theVector.append(2, Constructable(77)); + assertValuesInOrder(theVector, 3u, 1, 77, 77); +} + +// Assign test +TEST_F(SmallVectorTest, AssignTest) { + SCOPED_TRACE("AssignTest"); + + theVector.push_back(Constructable(1)); + theVector.assign(2, Constructable(77)); + assertValuesInOrder(theVector, 2u, 77, 77); +} + +// Erase a single element +TEST_F(SmallVectorTest, EraseTest) { + SCOPED_TRACE("EraseTest"); + + makeSequence(theVector, 1, 3); + theVector.erase(theVector.begin()); + assertValuesInOrder(theVector, 2u, 2, 3); +} + +// Erase a range of elements +TEST_F(SmallVectorTest, EraseRangeTest) { + SCOPED_TRACE("EraseRangeTest"); + + makeSequence(theVector, 1, 3); + theVector.erase(theVector.begin(), theVector.begin() + 2); + assertValuesInOrder(theVector, 1u, 3); +} + +// Insert a single element. +TEST_F(SmallVectorTest, InsertTest) { + SCOPED_TRACE("InsertTest"); + + makeSequence(theVector, 1, 3); + theVector.insert(theVector.begin() + 1, Constructable(77)); + assertValuesInOrder(theVector, 4u, 1, 77, 2, 3); +} + +// Insert repeated elements. +TEST_F(SmallVectorTest, InsertRepeatedTest) { + SCOPED_TRACE("InsertRepeatedTest"); + + makeSequence(theVector, 10, 15); + theVector.insert(theVector.begin() + 1, 2, Constructable(16)); + assertValuesInOrder(theVector, 8u, 10, 16, 16, 11, 12, 13, 14, 15); +} + +// Insert range. +TEST_F(SmallVectorTest, InsertRangeTest) { + SCOPED_TRACE("InsertRepeatedTest"); + + makeSequence(theVector, 1, 3); + theVector.insert(theVector.begin() + 1, 3, Constructable(77)); + assertValuesInOrder(theVector, 6u, 1, 77, 77, 77, 2, 3); +} + +// Comparison tests. +TEST_F(SmallVectorTest, ComparisonTest) { + SCOPED_TRACE("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); +} + +// Constant vector tests. +TEST_F(SmallVectorTest, ConstVectorTest) { + const VectorType constVector; + + EXPECT_EQ(0u, constVector.size()); + EXPECT_TRUE(constVector.empty()); + EXPECT_TRUE(constVector.begin() == constVector.end()); +} + +} diff --git a/unittests/ADT/StringMapTest.cpp b/unittests/ADT/StringMapTest.cpp new file mode 100644 index 0000000..9f9f2e4 --- /dev/null +++ b/unittests/ADT/StringMapTest.cpp @@ -0,0 +1,202 @@ +//===- llvm/unittest/ADT/StringMapMap.cpp - StringMap unit tests ----------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "gtest/gtest.h" +#include "llvm/ADT/StringMap.h" +using namespace llvm; + +namespace { + +// Test fixture +class StringMapTest : public testing::Test { +protected: + StringMap<uint32_t> testMap; + + static const char testKey[]; + static const uint32_t testValue; + static const char* testKeyFirst; + static const char* testKeyLast; + static const std::string testKeyStr; + + void assertEmptyMap() { + // Size tests + EXPECT_EQ(0u, testMap.size()); + EXPECT_TRUE(testMap.empty()); + + // Iterator tests + EXPECT_TRUE(testMap.begin() == testMap.end()); + + // Lookup tests + EXPECT_EQ(0u, testMap.count(testKey)); + EXPECT_EQ(0u, testMap.count(testKeyFirst, testKeyLast)); + EXPECT_EQ(0u, testMap.count(testKeyStr)); + EXPECT_TRUE(testMap.find(testKey) == testMap.end()); + EXPECT_TRUE(testMap.find(testKeyFirst, testKeyLast) == testMap.end()); + EXPECT_TRUE(testMap.find(testKeyStr) == testMap.end()); + } + + void assertSingleItemMap() { + // Size tests + EXPECT_EQ(1u, testMap.size()); + EXPECT_FALSE(testMap.begin() == testMap.end()); + EXPECT_FALSE(testMap.empty()); + + // Iterator tests + StringMap<uint32_t>::iterator it = testMap.begin(); + EXPECT_STREQ(testKey, it->first()); + EXPECT_EQ(testValue, it->second); + ++it; + EXPECT_TRUE(it == testMap.end()); + + // Lookup tests + EXPECT_EQ(1u, testMap.count(testKey)); + EXPECT_EQ(1u, testMap.count(testKeyFirst, testKeyLast)); + EXPECT_EQ(1u, testMap.count(testKeyStr)); + EXPECT_TRUE(testMap.find(testKey) == testMap.begin()); + EXPECT_TRUE(testMap.find(testKeyFirst, testKeyLast) == testMap.begin()); + EXPECT_TRUE(testMap.find(testKeyStr) == testMap.begin()); + } +}; + +const char StringMapTest::testKey[] = "key"; +const uint32_t StringMapTest::testValue = 1u; +const char* StringMapTest::testKeyFirst = testKey; +const char* StringMapTest::testKeyLast = testKey + sizeof(testKey) - 1; +const std::string StringMapTest::testKeyStr(testKey); + +// Empty map tests. +TEST_F(StringMapTest, EmptyMapTest) { + SCOPED_TRACE("EmptyMapTest"); + assertEmptyMap(); +} + +// Constant map tests. +TEST_F(StringMapTest, ConstEmptyMapTest) { + const StringMap<uint32_t>& constTestMap = testMap; + + // Size tests + EXPECT_EQ(0u, constTestMap.size()); + EXPECT_TRUE(constTestMap.empty()); + + // Iterator tests + EXPECT_TRUE(constTestMap.begin() == constTestMap.end()); + + // Lookup tests + EXPECT_EQ(0u, constTestMap.count(testKey)); + EXPECT_EQ(0u, constTestMap.count(testKeyFirst, testKeyLast)); + EXPECT_EQ(0u, constTestMap.count(testKeyStr)); + EXPECT_TRUE(constTestMap.find(testKey) == constTestMap.end()); + EXPECT_TRUE(constTestMap.find(testKeyFirst, testKeyLast) == + constTestMap.end()); + EXPECT_TRUE(constTestMap.find(testKeyStr) == constTestMap.end()); +} + +// A map with a single entry. +TEST_F(StringMapTest, SingleEntryMapTest) { + SCOPED_TRACE("SingleEntryMapTest"); + testMap[testKey] = testValue; + assertSingleItemMap(); +} + +// Test clear() method. +TEST_F(StringMapTest, ClearTest) { + SCOPED_TRACE("ClearTest"); + testMap[testKey] = testValue; + testMap.clear(); + assertEmptyMap(); +} + +// Test erase(iterator) method. +TEST_F(StringMapTest, EraseIteratorTest) { + SCOPED_TRACE("EraseIteratorTest"); + testMap[testKey] = testValue; + testMap.erase(testMap.begin()); + assertEmptyMap(); +} + +// Test erase(value) method. +TEST_F(StringMapTest, EraseValueTest) { + SCOPED_TRACE("EraseValueTest"); + testMap[testKey] = testValue; + testMap.erase(testKey); + assertEmptyMap(); +} + +// Test inserting two values and erasing one. +TEST_F(StringMapTest, InsertAndEraseTest) { + SCOPED_TRACE("InsertAndEraseTest"); + testMap[testKey] = testValue; + testMap["otherKey"] = 2; + testMap.erase("otherKey"); + assertSingleItemMap(); +} + +// A more complex iteration test. +TEST_F(StringMapTest, IterationTest) { + bool visited[100]; + + // Insert 100 numbers into the map + for (int i = 0; i < 100; ++i) { + std::stringstream ss; + ss << "key_" << i; + testMap[ss.str()] = i; + visited[i] = false; + } + + // Iterate over all numbers and mark each one found. + for (StringMap<uint32_t>::iterator it = testMap.begin(); + it != testMap.end(); ++it) { + std::stringstream ss; + ss << "key_" << it->second; + ASSERT_STREQ(ss.str().c_str(), it->first()); + visited[it->second] = true; + } + + // Ensure every number was visited. + for (int i = 0; i < 100; ++i) { + ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited"; + } +} + +} // end anonymous namespace + +namespace llvm { + +template <> +class StringMapEntryInitializer<uint32_t> { +public: + template <typename InitTy> + static void Initialize(StringMapEntry<uint32_t> &T, InitTy InitVal) { + T.second = InitVal; + } +}; + +} // end llvm namespace + +namespace { + +// Test StringMapEntry::Create() method. +TEST_F(StringMapTest, StringMapEntryTest) { + StringMap<uint32_t>::value_type* entry = + StringMap<uint32_t>::value_type::Create( + testKeyFirst, testKeyLast, 1u); + EXPECT_STREQ(testKey, entry->first()); + EXPECT_EQ(1u, entry->second); +} + +// Test insert() method. +TEST_F(StringMapTest, InsertTest) { + SCOPED_TRACE("InsertTest"); + testMap.insert( + StringMap<uint32_t>::value_type::Create( + testKeyFirst, testKeyLast, testMap.getAllocator(), 1u)); + assertSingleItemMap(); +} + +} // end anonymous namespace diff --git a/unittests/ADT/TripleTest.cpp b/unittests/ADT/TripleTest.cpp new file mode 100644 index 0000000..771d1c7 --- /dev/null +++ b/unittests/ADT/TripleTest.cpp @@ -0,0 +1,137 @@ +//===----------- Triple.cpp - Triple unit tests ---------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "gtest/gtest.h" +#include "llvm/ADT/Triple.h" + +using namespace llvm; + +namespace { + +TEST(TripleTest, BasicParsing) { + Triple T; + + T = Triple(""); + EXPECT_EQ(T.getArchName(), ""); + EXPECT_EQ(T.getVendorName(), ""); + EXPECT_EQ(T.getOSName(), ""); + EXPECT_EQ(T.getEnvironmentName(), ""); + + T = Triple("-"); + EXPECT_EQ(T.getArchName(), ""); + EXPECT_EQ(T.getVendorName(), ""); + EXPECT_EQ(T.getOSName(), ""); + EXPECT_EQ(T.getEnvironmentName(), ""); + + T = Triple("--"); + EXPECT_EQ(T.getArchName(), ""); + EXPECT_EQ(T.getVendorName(), ""); + EXPECT_EQ(T.getOSName(), ""); + EXPECT_EQ(T.getEnvironmentName(), ""); + + T = Triple("---"); + EXPECT_EQ(T.getArchName(), ""); + EXPECT_EQ(T.getVendorName(), ""); + EXPECT_EQ(T.getOSName(), ""); + EXPECT_EQ(T.getEnvironmentName(), ""); + + T = Triple("----"); + EXPECT_EQ(T.getArchName(), ""); + EXPECT_EQ(T.getVendorName(), ""); + EXPECT_EQ(T.getOSName(), ""); + EXPECT_EQ(T.getEnvironmentName(), "-"); + + T = Triple("a"); + EXPECT_EQ(T.getArchName(), "a"); + EXPECT_EQ(T.getVendorName(), ""); + EXPECT_EQ(T.getOSName(), ""); + EXPECT_EQ(T.getEnvironmentName(), ""); + + T = Triple("a-b"); + EXPECT_EQ(T.getArchName(), "a"); + EXPECT_EQ(T.getVendorName(), "b"); + EXPECT_EQ(T.getOSName(), ""); + EXPECT_EQ(T.getEnvironmentName(), ""); + + T = Triple("a-b-c"); + EXPECT_EQ(T.getArchName(), "a"); + EXPECT_EQ(T.getVendorName(), "b"); + EXPECT_EQ(T.getOSName(), "c"); + EXPECT_EQ(T.getEnvironmentName(), ""); + + T = Triple("a-b-c-d"); + EXPECT_EQ(T.getArchName(), "a"); + EXPECT_EQ(T.getVendorName(), "b"); + EXPECT_EQ(T.getOSName(), "c"); + EXPECT_EQ(T.getEnvironmentName(), "d"); +} + +TEST(TripleTest, ParsedIDs) { + Triple T; + + T = Triple("i386-apple-darwin"); + EXPECT_EQ(T.getArch(), Triple::x86); + EXPECT_EQ(T.getVendor(), Triple::Apple); + EXPECT_EQ(T.getOS(), Triple::Darwin); + + T = Triple("x86_64-pc-linux-gnu"); + EXPECT_EQ(T.getArch(), Triple::x86_64); + EXPECT_EQ(T.getVendor(), Triple::PC); + EXPECT_EQ(T.getOS(), Triple::Linux); + + T = Triple("powerpc-dunno-notsure"); + EXPECT_EQ(T.getArch(), Triple::ppc); + EXPECT_EQ(T.getVendor(), Triple::UnknownVendor); + EXPECT_EQ(T.getOS(), Triple::UnknownOS); + + T = Triple("huh"); + EXPECT_EQ(T.getArch(), Triple::UnknownArch); +} + +TEST(TripleTest, MutateName) { + Triple T; + EXPECT_EQ(T.getArch(), Triple::UnknownArch); + EXPECT_EQ(T.getVendor(), Triple::UnknownVendor); + EXPECT_EQ(T.getOS(), Triple::UnknownOS); + + T.setArchName("i386"); + EXPECT_EQ(T.getArch(), Triple::x86); + EXPECT_EQ(T.getTriple(), "i386--"); + + T.setVendorName("pc"); + EXPECT_EQ(T.getArch(), Triple::x86); + EXPECT_EQ(T.getVendor(), Triple::PC); + EXPECT_EQ(T.getTriple(), "i386-pc-"); + + T.setOSName("linux"); + EXPECT_EQ(T.getArch(), Triple::x86); + EXPECT_EQ(T.getVendor(), Triple::PC); + EXPECT_EQ(T.getOS(), Triple::Linux); + EXPECT_EQ(T.getTriple(), "i386-pc-linux"); + + T.setEnvironmentName("gnu"); + EXPECT_EQ(T.getArch(), Triple::x86); + EXPECT_EQ(T.getVendor(), Triple::PC); + EXPECT_EQ(T.getOS(), Triple::Linux); + EXPECT_EQ(T.getTriple(), "i386-pc-linux-gnu"); + + T.setOSName("freebsd"); + EXPECT_EQ(T.getArch(), Triple::x86); + EXPECT_EQ(T.getVendor(), Triple::PC); + EXPECT_EQ(T.getOS(), Triple::FreeBSD); + EXPECT_EQ(T.getTriple(), "i386-pc-freebsd-gnu"); + + T.setOSAndEnvironmentName("darwin"); + EXPECT_EQ(T.getArch(), Triple::x86); + EXPECT_EQ(T.getVendor(), Triple::PC); + EXPECT_EQ(T.getOS(), Triple::Darwin); + EXPECT_EQ(T.getTriple(), "i386-pc-darwin"); +} + +} |