aboutsummaryrefslogtreecommitdiffstats
path: root/unittests/ADT/TinyPtrVectorTest.cpp
diff options
context:
space:
mode:
authorStephen Hines <srhines@google.com>2012-09-05 22:31:59 -0700
committerAndroid Git Automerger <android-git-automerger@android.com>2012-09-05 22:31:59 -0700
commitcbbf0ced2c07892c0df3dd17def157ecb5e58b95 (patch)
treec1970fcebc736d4f731db0559a79a7ac5cb0f8bf /unittests/ADT/TinyPtrVectorTest.cpp
parenta81c41dc02ccbc654a9c2f638f9fbf2b599f5dfd (diff)
parent31675153bd2d7617db8cb6aeb58054934c7b9f73 (diff)
downloadexternal_llvm-cbbf0ced2c07892c0df3dd17def157ecb5e58b95.zip
external_llvm-cbbf0ced2c07892c0df3dd17def157ecb5e58b95.tar.gz
external_llvm-cbbf0ced2c07892c0df3dd17def157ecb5e58b95.tar.bz2
am 31675153: Merge branch \'upstream\' into merge_2
* commit '31675153bd2d7617db8cb6aeb58054934c7b9f73': (542 commits) MaximumSpanningTree::EdgeWeightCompare: Make this comparator actually be a strict weak ordering, and don't pass possibly-null pointers to dyn_cast. Fix misaligned access in MachO object file reader: despite containing an int64_t, Symbol64TableEntry is actually only stored with 4-byte alignment within the file. Fix unaligned memory accesses when performing relocations in X86 JIT. There's no cost to using memcpy here: the fixed code is optimized by LLVM to perfect machine code. Don't pass a null pointer to cast<> in its unit tests. Don't bind a reference to a dereferenced null pointer (for return value of WeakVH::operator*). [ms-inline asm] Do not report a Parser error when matching inline assembly. Ignore the documentation-suggested location for compile_commands.json The presence of the empty file "foo" unfortunately does not improve LLVM in any way. Remove unnecessary cast that was also unnecessarily casting away constness. Provide a portability macro for __builtin_trap. Fix macros arguments with an underscore, dot or dollar in them. This is based on a patch by Andy/PaX. I added the support for dot and dollar. [ms-inline asm] Expose the ErrorInfo from the MatchInstructionImpl. In general, this is the index of the operand that failed to match. Formatting. No functional change. Make the wording in of the "expected identifier" error in the .macro directive consistent with the other "expected identifier" errors. Extracted from the Andy/PaX patch. I added the test. Pacify PVS-Studio by changing the type rather than doing a cast, a tweak suggested by David Blaikie. Add support for the --param ssp-buffer-size= driver option. PR9673 Use typedefs. Fix indentation. Extracted from the Andy/PaX patch. Remove unused variable. Extracted from the Andy/PaX patch. Fix typo. Extracted from the Andy/PaX patch. MCJIT: Tidy up the constructor. ...
Diffstat (limited to 'unittests/ADT/TinyPtrVectorTest.cpp')
-rw-r--r--unittests/ADT/TinyPtrVectorTest.cpp448
1 files changed, 448 insertions, 0 deletions
diff --git a/unittests/ADT/TinyPtrVectorTest.cpp b/unittests/ADT/TinyPtrVectorTest.cpp
new file mode 100644
index 0000000..05dd797
--- /dev/null
+++ b/unittests/ADT/TinyPtrVectorTest.cpp
@@ -0,0 +1,448 @@
+//===- llvm/unittest/ADT/TinyPtrVectorTest.cpp ----------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// TinyPtrVector unit tests.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/TinyPtrVector.h"
+#include "llvm/Support/type_traits.h"
+#include <algorithm>
+#include <list>
+#include <vector>
+
+using namespace llvm;
+
+namespace {
+
+// The world's worst RNG, but it is deterministic and makes it easy to get
+// *some* shuffling of elements.
+static ptrdiff_t test_shuffle_rng(ptrdiff_t i) {
+ return (i + i * 33) % i;
+}
+static ptrdiff_t (*test_shuffle_rng_p)(ptrdiff_t) = &test_shuffle_rng;
+
+template <typename VectorT>
+class TinyPtrVectorTest : public testing::Test {
+protected:
+ typedef typename VectorT::value_type PtrT;
+ typedef typename remove_pointer<PtrT>::type ValueT;
+
+ VectorT V;
+ VectorT V2;
+
+ ValueT TestValues[1024];
+ std::vector<PtrT> TestPtrs;
+
+ TinyPtrVectorTest() {
+ for (size_t i = 0, e = array_lengthof(TestValues); i != e; ++i)
+ TestPtrs.push_back(&TestValues[i]);
+
+ std::random_shuffle(TestPtrs.begin(), TestPtrs.end(), test_shuffle_rng_p);
+ }
+
+ ArrayRef<PtrT> testArray(size_t N) {
+ return makeArrayRef(&TestPtrs[0], N);
+ }
+
+ void appendValues(VectorT &V, ArrayRef<PtrT> Values) {
+ for (size_t i = 0, e = Values.size(); i != e; ++i)
+ V.push_back(Values[i]);
+ }
+
+ void setVectors(ArrayRef<PtrT> Values1, ArrayRef<PtrT> Values2) {
+ V.clear();
+ appendValues(V, Values1);
+ V2.clear();
+ appendValues(V2, Values2);
+ }
+
+ void expectValues(const VectorT &V, ArrayRef<PtrT> Values) {
+ EXPECT_EQ(Values.empty(), V.empty());
+ EXPECT_EQ(Values.size(), V.size());
+ for (size_t i = 0, e = Values.size(); i != e; ++i) {
+ EXPECT_EQ(Values[i], V[i]);
+ EXPECT_EQ(Values[i], *llvm::next(V.begin(), i));
+ }
+ EXPECT_EQ(V.end(), llvm::next(V.begin(), Values.size()));
+ }
+};
+
+typedef ::testing::Types<TinyPtrVector<int*>,
+ TinyPtrVector<double*>
+ > TinyPtrVectorTestTypes;
+TYPED_TEST_CASE(TinyPtrVectorTest, TinyPtrVectorTestTypes);
+
+TYPED_TEST(TinyPtrVectorTest, EmptyTest) {
+ this->expectValues(this->V, this->testArray(0));
+}
+
+TYPED_TEST(TinyPtrVectorTest, PushPopBack) {
+ this->V.push_back(this->TestPtrs[0]);
+ this->expectValues(this->V, this->testArray(1));
+ this->V.push_back(this->TestPtrs[1]);
+ this->expectValues(this->V, this->testArray(2));
+ this->V.push_back(this->TestPtrs[2]);
+ this->expectValues(this->V, this->testArray(3));
+ this->V.push_back(this->TestPtrs[3]);
+ this->expectValues(this->V, this->testArray(4));
+ this->V.push_back(this->TestPtrs[4]);
+ this->expectValues(this->V, this->testArray(5));
+
+ // Pop and clobber a few values to keep things interesting.
+ this->V.pop_back();
+ this->expectValues(this->V, this->testArray(4));
+ this->V.pop_back();
+ this->expectValues(this->V, this->testArray(3));
+ this->TestPtrs[3] = &this->TestValues[42];
+ this->TestPtrs[4] = &this->TestValues[43];
+ this->V.push_back(this->TestPtrs[3]);
+ this->expectValues(this->V, this->testArray(4));
+ this->V.push_back(this->TestPtrs[4]);
+ this->expectValues(this->V, this->testArray(5));
+
+ this->V.pop_back();
+ this->expectValues(this->V, this->testArray(4));
+ this->V.pop_back();
+ this->expectValues(this->V, this->testArray(3));
+ this->V.pop_back();
+ this->expectValues(this->V, this->testArray(2));
+ this->V.pop_back();
+ this->expectValues(this->V, this->testArray(1));
+ this->V.pop_back();
+ this->expectValues(this->V, this->testArray(0));
+
+ this->appendValues(this->V, this->testArray(42));
+ this->expectValues(this->V, this->testArray(42));
+}
+
+TYPED_TEST(TinyPtrVectorTest, ClearTest) {
+ this->expectValues(this->V, this->testArray(0));
+ this->V.clear();
+ this->expectValues(this->V, this->testArray(0));
+
+ this->appendValues(this->V, this->testArray(1));
+ this->expectValues(this->V, this->testArray(1));
+ this->V.clear();
+ this->expectValues(this->V, this->testArray(0));
+
+ this->appendValues(this->V, this->testArray(42));
+ this->expectValues(this->V, this->testArray(42));
+ this->V.clear();
+ this->expectValues(this->V, this->testArray(0));
+}
+
+TYPED_TEST(TinyPtrVectorTest, CopyAndMoveCtorTest) {
+ this->appendValues(this->V, this->testArray(42));
+ TypeParam Copy(this->V);
+ this->expectValues(Copy, this->testArray(42));
+
+ // This is a separate copy, and so it shouldn't destroy the original.
+ Copy.clear();
+ this->expectValues(Copy, this->testArray(0));
+ this->expectValues(this->V, this->testArray(42));
+
+ TypeParam Copy2(this->V2);
+ this->appendValues(Copy2, this->testArray(42));
+ this->expectValues(Copy2, this->testArray(42));
+ this->expectValues(this->V2, this->testArray(0));
+
+#if LLVM_USE_RVALUE_REFERENCES
+ TypeParam Move(std::move(Copy2));
+ this->expectValues(Move, this->testArray(42));
+ this->expectValues(Copy2, this->testArray(0));
+#endif
+}
+
+TYPED_TEST(TinyPtrVectorTest, CopyAndMoveTest) {
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(0));
+ this->expectValues(this->V2, this->testArray(0));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(0));
+#endif
+
+ this->setVectors(this->testArray(1), this->testArray(0));
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(0));
+ this->expectValues(this->V2, this->testArray(0));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->setVectors(this->testArray(1), this->testArray(0));
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(0));
+#endif
+
+ this->setVectors(this->testArray(2), this->testArray(0));
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(0));
+ this->expectValues(this->V2, this->testArray(0));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->setVectors(this->testArray(2), this->testArray(0));
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(0));
+#endif
+
+ this->setVectors(this->testArray(42), this->testArray(0));
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(0));
+ this->expectValues(this->V2, this->testArray(0));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->setVectors(this->testArray(42), this->testArray(0));
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(0));
+#endif
+
+ this->setVectors(this->testArray(0), this->testArray(1));
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(1));
+ this->expectValues(this->V2, this->testArray(1));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->setVectors(this->testArray(0), this->testArray(1));
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(1));
+#endif
+
+ this->setVectors(this->testArray(0), this->testArray(2));
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(2));
+ this->expectValues(this->V2, this->testArray(2));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->setVectors(this->testArray(0), this->testArray(2));
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(2));
+#endif
+
+ this->setVectors(this->testArray(0), this->testArray(42));
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(42));
+ this->expectValues(this->V2, this->testArray(42));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->setVectors(this->testArray(0), this->testArray(42));
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(42));
+#endif
+
+ this->setVectors(this->testArray(1), this->testArray(1));
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(1));
+ this->expectValues(this->V2, this->testArray(1));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(1));
+#endif
+
+ this->setVectors(this->testArray(1), this->testArray(2));
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(2));
+ this->expectValues(this->V2, this->testArray(2));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->setVectors(this->testArray(1), this->testArray(2));
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(2));
+#endif
+
+ this->setVectors(this->testArray(1), this->testArray(42));
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(42));
+ this->expectValues(this->V2, this->testArray(42));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->setVectors(this->testArray(1), this->testArray(42));
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(42));
+#endif
+
+ this->setVectors(this->testArray(2), this->testArray(1));
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(1));
+ this->expectValues(this->V2, this->testArray(1));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->setVectors(this->testArray(2), this->testArray(1));
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(1));
+#endif
+
+ this->setVectors(this->testArray(2), this->testArray(2));
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(2));
+ this->expectValues(this->V2, this->testArray(2));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->setVectors(this->testArray(2), this->testArray(2));
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(2));
+#endif
+
+ this->setVectors(this->testArray(2), this->testArray(42));
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(42));
+ this->expectValues(this->V2, this->testArray(42));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->setVectors(this->testArray(2), this->testArray(42));
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(42));
+#endif
+
+ this->setVectors(this->testArray(42), this->testArray(1));
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(1));
+ this->expectValues(this->V2, this->testArray(1));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->setVectors(this->testArray(42), this->testArray(1));
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(1));
+#endif
+
+ this->setVectors(this->testArray(42), this->testArray(2));
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(2));
+ this->expectValues(this->V2, this->testArray(2));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->setVectors(this->testArray(42), this->testArray(2));
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(2));
+#endif
+
+ this->setVectors(this->testArray(42), this->testArray(42));
+ this->V = this->V2;
+ this->expectValues(this->V, this->testArray(42));
+ this->expectValues(this->V2, this->testArray(42));
+#if LLVM_USE_RVALUE_REFERENCES
+ this->setVectors(this->testArray(42), this->testArray(42));
+ this->V = std::move(this->V2);
+ this->expectValues(this->V, this->testArray(42));
+#endif
+}
+
+TYPED_TEST(TinyPtrVectorTest, EraseTest) {
+ this->appendValues(this->V, this->testArray(1));
+ this->expectValues(this->V, this->testArray(1));
+ this->V.erase(this->V.begin());
+ this->expectValues(this->V, this->testArray(0));
+
+ this->appendValues(this->V, this->testArray(42));
+ this->expectValues(this->V, this->testArray(42));
+ this->V.erase(this->V.begin());
+ this->TestPtrs.erase(this->TestPtrs.begin());
+ this->expectValues(this->V, this->testArray(41));
+ this->V.erase(llvm::next(this->V.begin(), 1));
+ this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 1));
+ this->expectValues(this->V, this->testArray(40));
+ this->V.erase(llvm::next(this->V.begin(), 2));
+ this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 2));
+ this->expectValues(this->V, this->testArray(39));
+ this->V.erase(llvm::next(this->V.begin(), 5));
+ this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 5));
+ this->expectValues(this->V, this->testArray(38));
+ this->V.erase(llvm::next(this->V.begin(), 13));
+ this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 13));
+ this->expectValues(this->V, this->testArray(37));
+
+ typename TypeParam::iterator I = this->V.begin();
+ do {
+ I = this->V.erase(I);
+ } while (I != this->V.end());
+ this->expectValues(this->V, this->testArray(0));
+}
+
+TYPED_TEST(TinyPtrVectorTest, EraseRangeTest) {
+ this->appendValues(this->V, this->testArray(1));
+ this->expectValues(this->V, this->testArray(1));
+ this->V.erase(this->V.begin(), this->V.begin());
+ this->expectValues(this->V, this->testArray(1));
+ this->V.erase(this->V.end(), this->V.end());
+ this->expectValues(this->V, this->testArray(1));
+ this->V.erase(this->V.begin(), this->V.end());
+ this->expectValues(this->V, this->testArray(0));
+
+ this->appendValues(this->V, this->testArray(42));
+ this->expectValues(this->V, this->testArray(42));
+ this->V.erase(this->V.begin(), llvm::next(this->V.begin(), 1));
+ this->TestPtrs.erase(this->TestPtrs.begin(),
+ llvm::next(this->TestPtrs.begin(), 1));
+ this->expectValues(this->V, this->testArray(41));
+ this->V.erase(llvm::next(this->V.begin(), 1), llvm::next(this->V.begin(), 2));
+ this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 1),
+ llvm::next(this->TestPtrs.begin(), 2));
+ this->expectValues(this->V, this->testArray(40));
+ this->V.erase(llvm::next(this->V.begin(), 2), llvm::next(this->V.begin(), 4));
+ this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 2),
+ llvm::next(this->TestPtrs.begin(), 4));
+ this->expectValues(this->V, this->testArray(38));
+ this->V.erase(llvm::next(this->V.begin(), 5), llvm::next(this->V.begin(), 10));
+ this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 5),
+ llvm::next(this->TestPtrs.begin(), 10));
+ this->expectValues(this->V, this->testArray(33));
+ this->V.erase(llvm::next(this->V.begin(), 13), llvm::next(this->V.begin(), 26));
+ this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 13),
+ llvm::next(this->TestPtrs.begin(), 26));
+ this->expectValues(this->V, this->testArray(20));
+ this->V.erase(llvm::next(this->V.begin(), 7), this->V.end());
+ this->expectValues(this->V, this->testArray(7));
+ this->V.erase(this->V.begin(), this->V.end());
+ this->expectValues(this->V, this->testArray(0));
+}
+
+TYPED_TEST(TinyPtrVectorTest, Insert) {
+ this->V.insert(this->V.end(), this->TestPtrs[0]);
+ this->expectValues(this->V, this->testArray(1));
+ this->V.clear();
+ this->appendValues(this->V, this->testArray(4));
+ this->expectValues(this->V, this->testArray(4));
+ this->V.insert(this->V.end(), this->TestPtrs[4]);
+ this->expectValues(this->V, this->testArray(5));
+ this->V.insert(this->V.begin(), this->TestPtrs[42]);
+ this->TestPtrs.insert(this->TestPtrs.begin(), this->TestPtrs[42]);
+ this->expectValues(this->V, this->testArray(6));
+ this->V.insert(llvm::next(this->V.begin(), 3), this->TestPtrs[43]);
+ this->TestPtrs.insert(llvm::next(this->TestPtrs.begin(), 3),
+ this->TestPtrs[43]);
+ this->expectValues(this->V, this->testArray(7));
+}
+
+TYPED_TEST(TinyPtrVectorTest, InsertRange) {
+ this->V.insert(this->V.end(), this->TestPtrs.begin(), this->TestPtrs.begin());
+ this->expectValues(this->V, this->testArray(0));
+ this->V.insert(this->V.begin(), this->TestPtrs.begin(),
+ this->TestPtrs.begin());
+ this->expectValues(this->V, this->testArray(0));
+ this->V.insert(this->V.end(), this->TestPtrs.end(), this->TestPtrs.end());
+ this->expectValues(this->V, this->testArray(0));
+ this->V.insert(this->V.end(), this->TestPtrs.begin(),
+ llvm::next(this->TestPtrs.begin()));
+ this->expectValues(this->V, this->testArray(1));
+ this->V.clear();
+ this->V.insert(this->V.end(), this->TestPtrs.begin(),
+ llvm::next(this->TestPtrs.begin(), 2));
+ this->expectValues(this->V, this->testArray(2));
+ this->V.clear();
+ this->V.insert(this->V.end(), this->TestPtrs.begin(),
+ llvm::next(this->TestPtrs.begin(), 42));
+ this->expectValues(this->V, this->testArray(42));
+ this->V.clear();
+ this->V.insert(this->V.end(),
+ llvm::next(this->TestPtrs.begin(), 5),
+ llvm::next(this->TestPtrs.begin(), 13));
+ this->V.insert(this->V.begin(),
+ llvm::next(this->TestPtrs.begin(), 0),
+ llvm::next(this->TestPtrs.begin(), 3));
+ this->V.insert(llvm::next(this->V.begin(), 2),
+ llvm::next(this->TestPtrs.begin(), 2),
+ llvm::next(this->TestPtrs.begin(), 4));
+ this->V.erase(llvm::next(this->V.begin(), 4));
+ this->V.insert(llvm::next(this->V.begin(), 4),
+ llvm::next(this->TestPtrs.begin(), 4),
+ llvm::next(this->TestPtrs.begin(), 5));
+ this->expectValues(this->V, this->testArray(13));
+}
+
+}