From 7f1d6d688f6ae288a16a4151e8a27b518d97f6f7 Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Tue, 18 Dec 2012 19:28:37 +0000 Subject: Add an assertion for a likely ilist::splice() contract violation. The single-element ilist::splice() function supports a noop move: List.splice(I, List, I); The corresponding std::list function doesn't allow that, so add a unit test to document that behavior. This also means that List.splice(I, List, F); is somewhat surprisingly not equivalent to List.splice(I, List, F, next(F)); This patch adds an assertion to catch the illegal case I == F above. Alternatively, we could make I == F a legal noop, but that would make ilist differ even more from std::list. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@170443 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/ilistTest.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'unittests/ADT') diff --git a/unittests/ADT/ilistTest.cpp b/unittests/ADT/ilistTest.cpp index 83eaa31981..711192ed89 100644 --- a/unittests/ADT/ilistTest.cpp +++ b/unittests/ADT/ilistTest.cpp @@ -9,6 +9,7 @@ #include "llvm/ADT/ilist.h" #include "llvm/ADT/ilist_node.h" +#include "llvm/ADT/STLExtras.h" #include "gtest/gtest.h" #include @@ -41,4 +42,24 @@ TEST(ilistTest, Basic) { EXPECT_EQ(1, ConstList.back().getPrevNode()->Value); } +TEST(ilistTest, SpliceOne) { + ilist List; + List.push_back(1); + + // The single-element splice operation supports noops. + List.splice(List.begin(), List, List.begin()); + EXPECT_EQ(1u, List.size()); + EXPECT_EQ(1, List.front().Value); + EXPECT_TRUE(llvm::next(List.begin()) == List.end()); + + // Altenative noop. Move the first element behind itself. + List.push_back(2); + List.push_back(3); + List.splice(llvm::next(List.begin()), List, List.begin()); + EXPECT_EQ(3u, List.size()); + EXPECT_EQ(1, List.front().Value); + EXPECT_EQ(2, llvm::next(List.begin())->Value); + EXPECT_EQ(3, List.back().Value); +} + } -- cgit v1.2.3-18-g5258 From 7f00f87767036e74445aad0164eea13cf2642610 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Wed, 2 Jan 2013 10:26:28 +0000 Subject: Sort a few more #include lines in tools/... unittests/... and utils/... git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171363 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/ilistTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'unittests/ADT') diff --git a/unittests/ADT/ilistTest.cpp b/unittests/ADT/ilistTest.cpp index 711192ed89..07bd5ec601 100644 --- a/unittests/ADT/ilistTest.cpp +++ b/unittests/ADT/ilistTest.cpp @@ -8,8 +8,8 @@ //===----------------------------------------------------------------------===// #include "llvm/ADT/ilist.h" -#include "llvm/ADT/ilist_node.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/ilist_node.h" #include "gtest/gtest.h" #include -- cgit v1.2.3-18-g5258 From 7c5c12bd4d60070c90161df9f6ae078c1f7b1ce5 Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Fri, 4 Jan 2013 22:35:42 +0000 Subject: Add an iplist::clearAndLeakNodesUnsafely() function. The iplist::clear() function can be quite expensive because it traverses the entire list, calling deleteNode() and removeNodeFromList() on each element. If node destruction and deallocation can be handled some other way, clearAndLeakNodesUnsafely() can be used to jettison all nodes without bringing them into cache. The function name is meant to be ominous. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171540 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/ilistTest.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'unittests/ADT') diff --git a/unittests/ADT/ilistTest.cpp b/unittests/ADT/ilistTest.cpp index 07bd5ec601..0c0cd0fd56 100644 --- a/unittests/ADT/ilistTest.cpp +++ b/unittests/ADT/ilistTest.cpp @@ -22,6 +22,7 @@ struct Node : ilist_node { Node() {} Node(int _Value) : Value(_Value) {} + ~Node() { Value = -1; } }; TEST(ilistTest, Basic) { @@ -62,4 +63,36 @@ TEST(ilistTest, SpliceOne) { EXPECT_EQ(3, List.back().Value); } +TEST(ilistTest, UnsafeClear) { + ilist List; + + // Before even allocating a sentinel. + List.clearAndLeakNodesUnsafely(); + EXPECT_EQ(0u, List.size()); + + // Empty list with sentinel. + ilist::iterator E = List.end(); + List.clearAndLeakNodesUnsafely(); + EXPECT_EQ(0u, List.size()); + // The sentinel shouldn't change. + EXPECT_TRUE(E == List.end()); + + // List with contents. + List.push_back(1); + ASSERT_EQ(1u, List.size()); + Node *N = List.begin(); + EXPECT_EQ(1, N->Value); + List.clearAndLeakNodesUnsafely(); + EXPECT_EQ(0u, List.size()); + ASSERT_EQ(1, N->Value); + delete N; + + // List is still functional. + List.push_back(5); + List.push_back(6); + ASSERT_EQ(2u, List.size()); + EXPECT_EQ(5, List.front().Value); + EXPECT_EQ(6, List.back().Value); +} + } -- cgit v1.2.3-18-g5258 From 7aa1c321f00d29fdc84e9a03080853aa25dd06fc Mon Sep 17 00:00:00 2001 From: Shuxin Yang Date: Mon, 7 Jan 2013 18:59:35 +0000 Subject: Implement APFloat::isDenormal() git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171764 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/APFloatTest.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'unittests/ADT') diff --git a/unittests/ADT/APFloatTest.cpp b/unittests/ADT/APFloatTest.cpp index ff350a7872..7345e124cd 100644 --- a/unittests/ADT/APFloatTest.cpp +++ b/unittests/ADT/APFloatTest.cpp @@ -33,6 +33,58 @@ static std::string convertToString(double d, unsigned Prec, unsigned Pad) { namespace { +TEST(APFloatTest, Denormal) { + APFloat::roundingMode rdmd = APFloat::rmNearestTiesToEven; + + // Test single precision + { + const char *MinNormalStr = "1.17549435082228750797e-38"; + EXPECT_FALSE(APFloat(APFloat::IEEEsingle, MinNormalStr).isDenormal()); + EXPECT_FALSE(APFloat(APFloat::IEEEsingle, 0.0).isDenormal()); + + APFloat Val2(APFloat::IEEEsingle, 2.0e0); + APFloat T(APFloat::IEEEsingle, MinNormalStr); + T.divide(Val2, rdmd); + EXPECT_TRUE(T.isDenormal()); + } + + // Test double precision + { + const char *MinNormalStr = "2.22507385850720138309e-308"; + EXPECT_FALSE(APFloat(APFloat::IEEEdouble, MinNormalStr).isDenormal()); + EXPECT_FALSE(APFloat(APFloat::IEEEdouble, 0.0).isDenormal()); + + APFloat Val2(APFloat::IEEEdouble, 2.0e0); + APFloat T(APFloat::IEEEdouble, MinNormalStr); + T.divide(Val2, rdmd); + EXPECT_TRUE(T.isDenormal()); + } + + // Test Intel double-ext + { + const char *MinNormalStr = "3.36210314311209350626e-4932"; + EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended, MinNormalStr).isDenormal()); + EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended, 0.0).isDenormal()); + + APFloat Val2(APFloat::x87DoubleExtended, 2.0e0); + APFloat T(APFloat::x87DoubleExtended, MinNormalStr); + T.divide(Val2, rdmd); + EXPECT_TRUE(T.isDenormal()); + } + + // Test quadruple precision + { + const char *MinNormalStr = "3.36210314311209350626267781732175260e-4932"; + EXPECT_FALSE(APFloat(APFloat::IEEEquad, MinNormalStr).isDenormal()); + EXPECT_FALSE(APFloat(APFloat::IEEEquad, 0.0).isDenormal()); + + APFloat Val2(APFloat::IEEEquad, 2.0e0); + APFloat T(APFloat::IEEEquad, MinNormalStr); + T.divide(Val2, rdmd); + EXPECT_TRUE(T.isDenormal()); + } +} + TEST(APFloatTest, Zero) { EXPECT_EQ(0.0f, APFloat(0.0f).convertToFloat()); EXPECT_EQ(-0.0f, APFloat(-0.0f).convertToFloat()); -- cgit v1.2.3-18-g5258 From afe77f33b2a361ed0d001596dcdde0e16d57abee Mon Sep 17 00:00:00 2001 From: Michael Ilseman Date: Mon, 21 Jan 2013 18:18:53 +0000 Subject: Introduce a new data structure, the SparseMultiSet, and changes to the MI scheduler to use it. A SparseMultiSet adds multiset behavior to SparseSet, while retaining SparseSet's desirable properties. Essentially, SparseMultiSet provides multiset behavior by storing its dense data in doubly linked lists that are inlined into the dense vector. This allows it to provide good data locality as well as vector-like constant-time clear() and fast constant time find(), insert(), and erase(). It also allows SparseMultiSet to have a builtin recycler rather than keeping SparseSet's behavior of always swapping upon removal, which allows it to preserve more iterators. It's often a better alternative to a SparseSet of a growable container or vector-of-vector. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@173064 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/CMakeLists.txt | 1 + unittests/ADT/SparseMultiSetTest.cpp | 235 +++++++++++++++++++++++++++++++++++ 2 files changed, 236 insertions(+) create mode 100644 unittests/ADT/SparseMultiSetTest.cpp (limited to 'unittests/ADT') diff --git a/unittests/ADT/CMakeLists.txt b/unittests/ADT/CMakeLists.txt index 94f7fda2a9..09ca89944b 100644 --- a/unittests/ADT/CMakeLists.txt +++ b/unittests/ADT/CMakeLists.txt @@ -24,6 +24,7 @@ set(ADTSources SmallStringTest.cpp SmallVectorTest.cpp SparseBitVectorTest.cpp + SparseMultiSetTest.cpp SparseSetTest.cpp StringMapTest.cpp StringRefTest.cpp diff --git a/unittests/ADT/SparseMultiSetTest.cpp b/unittests/ADT/SparseMultiSetTest.cpp new file mode 100644 index 0000000000..4ac0388063 --- /dev/null +++ b/unittests/ADT/SparseMultiSetTest.cpp @@ -0,0 +1,235 @@ +//===------ ADT/SparseSetTest.cpp - SparseSet 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 "llvm/ADT/SparseMultiSet.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +typedef SparseMultiSet USet; + +// Empty set tests. +TEST(SparseMultiSetTest, EmptySet) { + USet Set; + EXPECT_TRUE(Set.empty()); + EXPECT_EQ(0u, Set.size()); + + Set.setUniverse(10); + + // Lookups on empty set. + EXPECT_TRUE(Set.find(0) == Set.end()); + EXPECT_TRUE(Set.find(9) == Set.end()); + + // Same thing on a const reference. + const USet &CSet = Set; + EXPECT_TRUE(CSet.empty()); + EXPECT_EQ(0u, CSet.size()); + EXPECT_TRUE(CSet.find(0) == CSet.end()); + USet::const_iterator I = CSet.find(5); + EXPECT_TRUE(I == CSet.end()); +} + +// Single entry set tests. +TEST(SparseMultiSetTest, SingleEntrySet) { + USet Set; + Set.setUniverse(10); + USet::iterator I = Set.insert(5); + EXPECT_TRUE(I != Set.end()); + EXPECT_TRUE(*I == 5); + + EXPECT_FALSE(Set.empty()); + EXPECT_EQ(1u, Set.size()); + + EXPECT_TRUE(Set.find(0) == Set.end()); + EXPECT_TRUE(Set.find(9) == Set.end()); + + EXPECT_FALSE(Set.contains(0)); + EXPECT_TRUE(Set.contains(5)); + + // Extra insert. + I = Set.insert(5); + EXPECT_TRUE(I != Set.end()); + EXPECT_TRUE(I == ++Set.find(5)); + I--; + EXPECT_TRUE(I == Set.find(5)); + + // Erase non-existent element. + I = Set.find(1); + EXPECT_TRUE(I == Set.end()); + EXPECT_EQ(2u, Set.size()); + EXPECT_EQ(5u, *Set.find(5)); + + // Erase iterator. + I = Set.find(5); + EXPECT_TRUE(I != Set.end()); + I = Set.erase(I); + EXPECT_TRUE(I != Set.end()); + I = Set.erase(I); + EXPECT_TRUE(I == Set.end()); + EXPECT_TRUE(Set.empty()); +} + +// Multiple entry set tests. +TEST(SparseMultiSetTest, MultipleEntrySet) { + USet Set; + Set.setUniverse(10); + + Set.insert(5); + Set.insert(5); + Set.insert(5); + Set.insert(3); + Set.insert(2); + Set.insert(1); + Set.insert(4); + EXPECT_EQ(7u, Set.size()); + + // Erase last element by key. + EXPECT_TRUE(Set.erase(Set.find(4)) == Set.end()); + EXPECT_EQ(6u, Set.size()); + EXPECT_FALSE(Set.contains(4)); + EXPECT_TRUE(Set.find(4) == Set.end()); + + // Erase first element by key. + EXPECT_EQ(3u, Set.count(5)); + EXPECT_TRUE(Set.find(5) != Set.end()); + EXPECT_TRUE(Set.erase(Set.find(5)) != Set.end()); + EXPECT_EQ(5u, Set.size()); + EXPECT_EQ(2u, Set.count(5)); + + Set.insert(6); + Set.insert(7); + EXPECT_EQ(7u, Set.size()); + + // Erase tail by iterator. + EXPECT_TRUE(Set.getTail(6) == Set.getHead(6)); + USet::iterator I = Set.erase(Set.find(6)); + EXPECT_TRUE(I == Set.end()); + EXPECT_EQ(6u, Set.size()); + + // Erase tails by iterator. + EXPECT_EQ(2u, Set.count(5)); + I = Set.getTail(5); + I = Set.erase(I); + EXPECT_TRUE(I == Set.end()); + --I; + EXPECT_EQ(1u, Set.count(5)); + EXPECT_EQ(5u, *I); + I = Set.erase(I); + EXPECT_TRUE(I == Set.end()); + EXPECT_EQ(0u, Set.count(5)); + + Set.insert(8); + Set.insert(8); + Set.insert(8); + Set.insert(8); + Set.insert(8); + + // Erase all the 8s + EXPECT_EQ(5u, std::distance(Set.getHead(8), Set.end())); + Set.eraseAll(8); + EXPECT_EQ(0u, std::distance(Set.getHead(8), Set.end())); + + // Clear and resize the universe. + Set.clear(); + EXPECT_EQ(0u, Set.size()); + EXPECT_FALSE(Set.contains(3)); + Set.setUniverse(1000); + + // Add more than 256 elements. + for (unsigned i = 100; i != 800; ++i) + Set.insert(i); + + for (unsigned i = 0; i != 10; ++i) + Set.eraseAll(i); + + for (unsigned i = 100; i != 800; ++i) + EXPECT_EQ(1u, Set.count(i)); + + EXPECT_FALSE(Set.contains(99)); + EXPECT_FALSE(Set.contains(800)); + EXPECT_EQ(700u, Set.size()); +} + +// Test out iterators +TEST(SparseMultiSetTest, Iterators) { + USet Set; + Set.setUniverse(100); + + Set.insert(0); + Set.insert(1); + Set.insert(2); + Set.insert(0); + Set.insert(1); + Set.insert(0); + + USet::RangePair RangePair = Set.equal_range(0); + USet::iterator B = RangePair.first; + USet::iterator E = RangePair.second; + + // Move the iterators around, going to end and coming back. + EXPECT_EQ(3u, std::distance(B, E)); + EXPECT_EQ(B, --(--(--E))); + EXPECT_EQ(++(++(++E)), Set.end()); + EXPECT_EQ(B, --(--(--E))); + EXPECT_EQ(++(++(++E)), Set.end()); + + // Insert into the tail, and move around again + Set.insert(0); + EXPECT_EQ(B, --(--(--(--E)))); + EXPECT_EQ(++(++(++(++E))), Set.end()); + EXPECT_EQ(B, --(--(--(--E)))); + EXPECT_EQ(++(++(++(++E))), Set.end()); + + // Erase a tail, and move around again + USet::iterator Erased = Set.erase(Set.getTail(0)); + EXPECT_EQ(Erased, E); + EXPECT_EQ(B, --(--(--E))); + + USet Set2; + Set2.setUniverse(11); + Set2.insert(3); + EXPECT_TRUE(!Set2.contains(0)); + EXPECT_TRUE(!Set.contains(3)); + + EXPECT_EQ(Set2.getHead(3), Set2.getTail(3)); + EXPECT_EQ(Set2.getHead(0), Set2.getTail(0)); + B = Set2.find(3); + EXPECT_EQ(Set2.find(3), --(++B)); +} + +struct Alt { + unsigned Value; + explicit Alt(unsigned x) : Value(x) {} + unsigned getSparseSetIndex() const { return Value - 1000; } +}; + +TEST(SparseMultiSetTest, AltStructSet) { + typedef SparseMultiSet ASet; + ASet Set; + Set.setUniverse(10); + Set.insert(Alt(1005)); + + ASet::iterator I = Set.find(5); + ASSERT_TRUE(I != Set.end()); + EXPECT_EQ(1005u, I->Value); + + Set.insert(Alt(1006)); + Set.insert(Alt(1006)); + I = Set.erase(Set.find(6)); + ASSERT_TRUE(I != Set.end()); + EXPECT_EQ(1006u, I->Value); + I = Set.erase(Set.find(6)); + ASSERT_TRUE(I == Set.end()); + + EXPECT_TRUE(Set.contains(5)); + EXPECT_FALSE(Set.contains(6)); +} +} // namespace -- cgit v1.2.3-18-g5258 From 19f24454b516042b618298522e79bf9e6fa5ccbd Mon Sep 17 00:00:00 2001 From: NAKAMURA Takumi Date: Tue, 22 Jan 2013 05:30:15 +0000 Subject: ADT/SparseMultiSetTest.cpp: Try to appease cygwin-clang (libstdc++-4.5) on stage2. [-Wsign-compare] git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@173127 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/SparseMultiSetTest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'unittests/ADT') diff --git a/unittests/ADT/SparseMultiSetTest.cpp b/unittests/ADT/SparseMultiSetTest.cpp index 4ac0388063..d8999ca351 100644 --- a/unittests/ADT/SparseMultiSetTest.cpp +++ b/unittests/ADT/SparseMultiSetTest.cpp @@ -133,9 +133,9 @@ TEST(SparseMultiSetTest, MultipleEntrySet) { Set.insert(8); // Erase all the 8s - EXPECT_EQ(5u, std::distance(Set.getHead(8), Set.end())); + EXPECT_EQ(5, std::distance(Set.getHead(8), Set.end())); Set.eraseAll(8); - EXPECT_EQ(0u, std::distance(Set.getHead(8), Set.end())); + EXPECT_EQ(0, std::distance(Set.getHead(8), Set.end())); // Clear and resize the universe. Set.clear(); -- cgit v1.2.3-18-g5258 From 421db34270ca7d5a08b571713a5d9c83ed07b872 Mon Sep 17 00:00:00 2001 From: NAKAMURA Takumi Date: Tue, 22 Jan 2013 10:39:31 +0000 Subject: ADT/SparseMultiSetTest.cpp: Try to appease cygwin-clang on stage2, take two. [-Wsign-compare] git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@173144 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/SparseMultiSetTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'unittests/ADT') diff --git a/unittests/ADT/SparseMultiSetTest.cpp b/unittests/ADT/SparseMultiSetTest.cpp index d8999ca351..032990e4bc 100644 --- a/unittests/ADT/SparseMultiSetTest.cpp +++ b/unittests/ADT/SparseMultiSetTest.cpp @@ -175,7 +175,7 @@ TEST(SparseMultiSetTest, Iterators) { USet::iterator E = RangePair.second; // Move the iterators around, going to end and coming back. - EXPECT_EQ(3u, std::distance(B, E)); + EXPECT_EQ(3, std::distance(B, E)); EXPECT_EQ(B, --(--(--E))); EXPECT_EQ(++(++(++E)), Set.end()); EXPECT_EQ(B, --(--(--E))); -- cgit v1.2.3-18-g5258 From bd7561ea29649625775ef814dded2856d91c4dcf Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Fri, 25 Jan 2013 17:01:00 +0000 Subject: APFloat: Make sure that we get a well-formed x87 NaN when converting from a smaller type. Fixes PR15054. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@173459 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/APFloatTest.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'unittests/ADT') diff --git a/unittests/ADT/APFloatTest.cpp b/unittests/ADT/APFloatTest.cpp index 7345e124cd..278983565c 100644 --- a/unittests/ADT/APFloatTest.cpp +++ b/unittests/ADT/APFloatTest.cpp @@ -794,6 +794,32 @@ TEST(APFloatTest, convert) { test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo); EXPECT_EQ(4294967295.0, test.convertToDouble()); EXPECT_FALSE(losesInfo); + + test = APFloat::getSNaN(APFloat::IEEEsingle); + APFloat X87SNaN = APFloat::getSNaN(APFloat::x87DoubleExtended); + test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven, + &losesInfo); + EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN)); + EXPECT_FALSE(losesInfo); + + test = APFloat::getQNaN(APFloat::IEEEsingle); + APFloat X87QNaN = APFloat::getQNaN(APFloat::x87DoubleExtended); + test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven, + &losesInfo); + EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN)); + EXPECT_FALSE(losesInfo); + + test = APFloat::getSNaN(APFloat::x87DoubleExtended); + test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven, + &losesInfo); + EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN)); + EXPECT_FALSE(losesInfo); + + test = APFloat::getQNaN(APFloat::x87DoubleExtended); + test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven, + &losesInfo); + EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN)); + EXPECT_FALSE(losesInfo); } TEST(APFloatTest, PPCDoubleDouble) { -- cgit v1.2.3-18-g5258 From 6bbf4ff9c545c881422da37494b1ccb9c18d9c6a Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Fri, 25 Jan 2013 22:11:02 +0000 Subject: Add an insert() method to MapVector. Adds the first MapVector unit test. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@173505 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/MapVectorTest.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 unittests/ADT/MapVectorTest.cpp (limited to 'unittests/ADT') diff --git a/unittests/ADT/MapVectorTest.cpp b/unittests/ADT/MapVectorTest.cpp new file mode 100644 index 0000000000..9f613697d5 --- /dev/null +++ b/unittests/ADT/MapVectorTest.cpp @@ -0,0 +1,41 @@ +//===- unittest/ADT/MapVectorTest.cpp - MapVector 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/MapVector.h" +#include + +using namespace llvm; + +TEST(MapVectorTest, insert) { + MapVector MV; + std::pair::iterator, bool> R; + + R = MV.insert(std::make_pair(1, 2)); + ASSERT_EQ(R.first, MV.begin()); + EXPECT_EQ(R.first->first, 1); + EXPECT_EQ(R.first->second, 2); + EXPECT_TRUE(R.second); + + R = MV.insert(std::make_pair(1, 3)); + ASSERT_EQ(R.first, MV.begin()); + EXPECT_EQ(R.first->first, 1); + EXPECT_EQ(R.first->second, 2); + EXPECT_FALSE(R.second); + + R = MV.insert(std::make_pair(4, 5)); + ASSERT_NE(R.first, MV.end()); + EXPECT_EQ(R.first->first, 4); + EXPECT_EQ(R.first->second, 5); + EXPECT_TRUE(R.second); + + EXPECT_EQ(MV.size(), 2u); + EXPECT_EQ(MV[1], 2); + EXPECT_EQ(MV[4], 5); +} -- cgit v1.2.3-18-g5258 From c73b7f702258b844dc2702fd9d79d8a8706460a7 Mon Sep 17 00:00:00 2001 From: Dmitri Gribenko Date: Fri, 25 Jan 2013 22:29:23 +0000 Subject: Add file to CMakeLists (file added in r173505) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@173513 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) (limited to 'unittests/ADT') diff --git a/unittests/ADT/CMakeLists.txt b/unittests/ADT/CMakeLists.txt index 09ca89944b..0f39e02c1f 100644 --- a/unittests/ADT/CMakeLists.txt +++ b/unittests/ADT/CMakeLists.txt @@ -18,6 +18,7 @@ set(ADTSources IntEqClassesTest.cpp IntervalMapTest.cpp IntrusiveRefCntPtrTest.cpp + MapVectorTest.cpp PackedVectorTest.cpp SCCIteratorTest.cpp SmallPtrSetTest.cpp -- cgit v1.2.3-18-g5258 From 69f60e7c4a77866b3d0a5907e4955baa05216a20 Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Wed, 30 Jan 2013 23:48:13 +0000 Subject: Added a unit test for r173983 that verifies that Target.isiOS() works correctly. As a bonus I put in some extra checks to make sure that we are identifying the machine word of various Mac OS X/iOS targets appropriately. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@173994 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/TripleTest.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'unittests/ADT') diff --git a/unittests/ADT/TripleTest.cpp b/unittests/ADT/TripleTest.cpp index 7c3ab97389..b402896390 100644 --- a/unittests/ADT/TripleTest.cpp +++ b/unittests/ADT/TripleTest.cpp @@ -407,6 +407,11 @@ TEST(TripleTest, getOSVersion) { unsigned Major, Minor, Micro; T = Triple("i386-apple-darwin9"); + EXPECT_TRUE(T.isMacOSX()); + EXPECT_FALSE(T.isiOS()); + EXPECT_FALSE(T.isArch16Bit()); + EXPECT_TRUE(T.isArch32Bit()); + EXPECT_FALSE(T.isArch64Bit()); T.getMacOSXVersion(Major, Minor, Micro); EXPECT_EQ((unsigned)10, Major); EXPECT_EQ((unsigned)5, Minor); @@ -417,6 +422,11 @@ TEST(TripleTest, getOSVersion) { EXPECT_EQ((unsigned)0, Micro); T = Triple("x86_64-apple-darwin9"); + EXPECT_TRUE(T.isMacOSX()); + EXPECT_FALSE(T.isiOS()); + EXPECT_FALSE(T.isArch16Bit()); + EXPECT_FALSE(T.isArch32Bit()); + EXPECT_TRUE(T.isArch64Bit()); T.getMacOSXVersion(Major, Minor, Micro); EXPECT_EQ((unsigned)10, Major); EXPECT_EQ((unsigned)5, Minor); @@ -427,6 +437,11 @@ TEST(TripleTest, getOSVersion) { EXPECT_EQ((unsigned)0, Micro); T = Triple("x86_64-apple-macosx"); + EXPECT_TRUE(T.isMacOSX()); + EXPECT_FALSE(T.isiOS()); + EXPECT_FALSE(T.isArch16Bit()); + EXPECT_FALSE(T.isArch32Bit()); + EXPECT_TRUE(T.isArch64Bit()); T.getMacOSXVersion(Major, Minor, Micro); EXPECT_EQ((unsigned)10, Major); EXPECT_EQ((unsigned)4, Minor); @@ -437,6 +452,11 @@ TEST(TripleTest, getOSVersion) { EXPECT_EQ((unsigned)0, Micro); T = Triple("x86_64-apple-macosx10.7"); + EXPECT_TRUE(T.isMacOSX()); + EXPECT_FALSE(T.isiOS()); + EXPECT_FALSE(T.isArch16Bit()); + EXPECT_FALSE(T.isArch32Bit()); + EXPECT_TRUE(T.isArch64Bit()); T.getMacOSXVersion(Major, Minor, Micro); EXPECT_EQ((unsigned)10, Major); EXPECT_EQ((unsigned)7, Minor); @@ -447,6 +467,11 @@ TEST(TripleTest, getOSVersion) { EXPECT_EQ((unsigned)0, Micro); T = Triple("armv7-apple-ios"); + EXPECT_FALSE(T.isMacOSX()); + EXPECT_TRUE(T.isiOS()); + EXPECT_FALSE(T.isArch16Bit()); + EXPECT_TRUE(T.isArch32Bit()); + EXPECT_FALSE(T.isArch64Bit()); T.getMacOSXVersion(Major, Minor, Micro); EXPECT_EQ((unsigned)10, Major); EXPECT_EQ((unsigned)4, Minor); @@ -457,6 +482,11 @@ TEST(TripleTest, getOSVersion) { EXPECT_EQ((unsigned)0, Micro); T = Triple("armv7-apple-ios5.0"); + EXPECT_FALSE(T.isMacOSX()); + EXPECT_TRUE(T.isiOS()); + EXPECT_FALSE(T.isArch16Bit()); + EXPECT_TRUE(T.isArch32Bit()); + EXPECT_FALSE(T.isArch64Bit()); T.getMacOSXVersion(Major, Minor, Micro); EXPECT_EQ((unsigned)10, Major); EXPECT_EQ((unsigned)4, Minor); -- cgit v1.2.3-18-g5258 From 35b1423ee67a6ec7052016dda486e6ee4a118db4 Mon Sep 17 00:00:00 2001 From: Meador Inge Date: Thu, 7 Feb 2013 18:36:50 +0000 Subject: ADT: Correct APInt::getActiveWords for zero values PR15138 was opened because of a segfault in the Bitcode writer. The actual issue ended up being a bug in APInt where calls to APInt::getActiveWords returns a bogus value when the APInt value is 0. This patch fixes the problem by ensuring that getActiveWords returns 1 for 0 valued APInts. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@174641 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/APIntTest.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'unittests/ADT') diff --git a/unittests/ADT/APIntTest.cpp b/unittests/ADT/APIntTest.cpp index 2a4c5b4c49..2510bc738d 100644 --- a/unittests/ADT/APIntTest.cpp +++ b/unittests/ADT/APIntTest.cpp @@ -56,6 +56,14 @@ TEST(APIntTest, i33_Count) { #endif TEST(APIntTest, i65_Count) { + APInt i65(65, 0, true); + EXPECT_EQ(65u, i65.countLeadingZeros()); + EXPECT_EQ(0u, i65.countLeadingOnes()); + EXPECT_EQ(0u, i65.getActiveBits()); + EXPECT_EQ(1u, i65.getActiveWords()); + EXPECT_EQ(65u, i65.countTrailingZeros()); + EXPECT_EQ(0u, i65.countPopulation()); + APInt i65minus(65, 0, true); i65minus.setBit(64); EXPECT_EQ(0u, i65minus.countLeadingZeros()); -- cgit v1.2.3-18-g5258 From 1f1713ff7a53c9c491c59886984f6a0534ce3630 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Tue, 19 Feb 2013 18:26:07 +0000 Subject: Remove my bogus MapVector::erase() with a narrower ::pop_back(), and add a unit test. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175538 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/MapVectorTest.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'unittests/ADT') diff --git a/unittests/ADT/MapVectorTest.cpp b/unittests/ADT/MapVectorTest.cpp index 9f613697d5..11178bc15e 100644 --- a/unittests/ADT/MapVectorTest.cpp +++ b/unittests/ADT/MapVectorTest.cpp @@ -13,7 +13,7 @@ using namespace llvm; -TEST(MapVectorTest, insert) { +TEST(MapVectorTest, insert_pop) { MapVector MV; std::pair::iterator, bool> R; @@ -38,4 +38,18 @@ TEST(MapVectorTest, insert) { EXPECT_EQ(MV.size(), 2u); EXPECT_EQ(MV[1], 2); EXPECT_EQ(MV[4], 5); + + MV.pop_back(); + EXPECT_EQ(MV.size(), 1u); + EXPECT_EQ(MV[1], 2); + + R = MV.insert(std::make_pair(4, 7)); + ASSERT_NE(R.first, MV.end()); + EXPECT_EQ(R.first->first, 4); + EXPECT_EQ(R.first->second, 7); + EXPECT_TRUE(R.second); + + EXPECT_EQ(MV.size(), 2u); + EXPECT_EQ(MV[1], 2); + EXPECT_EQ(MV[4], 7); } -- cgit v1.2.3-18-g5258 From a28eda7e401f37a2fe42ca9bba96b6e662b60cf0 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Wed, 20 Feb 2013 00:26:04 +0000 Subject: Allow llvm::Optional to work with types without default constructors. This generalizes Optional to require less from the T type by using aligned storage for backing & placement new/deleting the T into it when necessary. Also includes unit tests. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175580 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/CMakeLists.txt | 1 + unittests/ADT/OptionalTest.cpp | 173 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 unittests/ADT/OptionalTest.cpp (limited to 'unittests/ADT') diff --git a/unittests/ADT/CMakeLists.txt b/unittests/ADT/CMakeLists.txt index 0f39e02c1f..9aad793d8b 100644 --- a/unittests/ADT/CMakeLists.txt +++ b/unittests/ADT/CMakeLists.txt @@ -19,6 +19,7 @@ set(ADTSources IntervalMapTest.cpp IntrusiveRefCntPtrTest.cpp MapVectorTest.cpp + OptionalTest.cpp PackedVectorTest.cpp SCCIteratorTest.cpp SmallPtrSetTest.cpp diff --git a/unittests/ADT/OptionalTest.cpp b/unittests/ADT/OptionalTest.cpp new file mode 100644 index 0000000000..8bdea45c5a --- /dev/null +++ b/unittests/ADT/OptionalTest.cpp @@ -0,0 +1,173 @@ +//===- llvm/unittest/ADT/OptionalTest.cpp - Optional 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/Optional.h" +using namespace llvm; + +namespace { + +struct NonDefaultConstructible { + static unsigned CopyConstructions; + static unsigned Destructions; + static unsigned CopyAssignments; + explicit NonDefaultConstructible(int) { + } + NonDefaultConstructible(const NonDefaultConstructible&) { + ++CopyConstructions; + } + NonDefaultConstructible &operator=(const NonDefaultConstructible&) { + ++CopyAssignments; + return *this; + } + ~NonDefaultConstructible() { + ++Destructions; + } + static void ResetCounts() { + CopyConstructions = 0; + Destructions = 0; + CopyAssignments = 0; + } +}; + +unsigned NonDefaultConstructible::CopyConstructions = 0; +unsigned NonDefaultConstructible::Destructions = 0; +unsigned NonDefaultConstructible::CopyAssignments = 0; + +// Test fixture +class OptionalTest : public testing::Test { +}; + +TEST_F(OptionalTest, NonDefaultConstructibleTest) { + Optional O; + EXPECT_FALSE(O); +} + +TEST_F(OptionalTest, ResetTest) { + NonDefaultConstructible::ResetCounts(); + Optional O(NonDefaultConstructible(3)); + EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(1u, NonDefaultConstructible::Destructions); + NonDefaultConstructible::ResetCounts(); + O.Reset(); + EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(1u, NonDefaultConstructible::Destructions); +} + +TEST_F(OptionalTest, InitializationLeakTest) { + NonDefaultConstructible::ResetCounts(); + Optional(NonDefaultConstructible(3)); + EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(2u, NonDefaultConstructible::Destructions); +} + +TEST_F(OptionalTest, CopyConstructionTest) { + NonDefaultConstructible::ResetCounts(); + { + Optional A(NonDefaultConstructible(3)); + EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(1u, NonDefaultConstructible::Destructions); + NonDefaultConstructible::ResetCounts(); + Optional B(A); + EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(0u, NonDefaultConstructible::Destructions); + NonDefaultConstructible::ResetCounts(); + } + EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(2u, NonDefaultConstructible::Destructions); +} + +TEST_F(OptionalTest, ConstructingCopyAssignmentTest) { + NonDefaultConstructible::ResetCounts(); + { + Optional A(NonDefaultConstructible(3)); + Optional B; + EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(1u, NonDefaultConstructible::Destructions); + NonDefaultConstructible::ResetCounts(); + B = A; + EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(0u, NonDefaultConstructible::Destructions); + NonDefaultConstructible::ResetCounts(); + } + EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(2u, NonDefaultConstructible::Destructions); +} + +TEST_F(OptionalTest, CopyingCopyAssignmentTest) { + NonDefaultConstructible::ResetCounts(); + { + Optional A(NonDefaultConstructible(3)); + Optional B(NonDefaultConstructible(4)); + EXPECT_EQ(2u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(2u, NonDefaultConstructible::Destructions); + NonDefaultConstructible::ResetCounts(); + B = A; + EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(1u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(0u, NonDefaultConstructible::Destructions); + NonDefaultConstructible::ResetCounts(); + } + EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(2u, NonDefaultConstructible::Destructions); +} + +TEST_F(OptionalTest, DeletingCopyAssignmentTest) { + NonDefaultConstructible::ResetCounts(); + { + Optional A; + Optional B(NonDefaultConstructible(3)); + EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(1u, NonDefaultConstructible::Destructions); + NonDefaultConstructible::ResetCounts(); + B = A; + EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(1u, NonDefaultConstructible::Destructions); + NonDefaultConstructible::ResetCounts(); + } + EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(0u, NonDefaultConstructible::Destructions); +} + +TEST_F(OptionalTest, NullCopyConstructionTest) { + NonDefaultConstructible::ResetCounts(); + { + Optional A; + Optional B; + EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(0u, NonDefaultConstructible::Destructions); + NonDefaultConstructible::ResetCounts(); + B = A; + EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(0u, NonDefaultConstructible::Destructions); + NonDefaultConstructible::ResetCounts(); + } + EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions); + EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); + EXPECT_EQ(0u, NonDefaultConstructible::Destructions); +} + +} // end anonymous namespace + -- cgit v1.2.3-18-g5258 From 4318fc5e1e44f355ae117dcac385f1718d971e90 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Wed, 20 Feb 2013 06:25:36 +0000 Subject: Rename llvm::Optional::Reset to 'reset' as per LLVM naming conventions. Code review feedback on r175580 from Jordan Rose. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175595 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/OptionalTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'unittests/ADT') diff --git a/unittests/ADT/OptionalTest.cpp b/unittests/ADT/OptionalTest.cpp index 8bdea45c5a..6fd8bbaf09 100644 --- a/unittests/ADT/OptionalTest.cpp +++ b/unittests/ADT/OptionalTest.cpp @@ -56,7 +56,7 @@ TEST_F(OptionalTest, ResetTest) { EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); EXPECT_EQ(1u, NonDefaultConstructible::Destructions); NonDefaultConstructible::ResetCounts(); - O.Reset(); + O.reset(); EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions); EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments); EXPECT_EQ(1u, NonDefaultConstructible::Destructions); -- cgit v1.2.3-18-g5258 From ad4da0fc321230261b4d0387f0ec216eb8aa50ca Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Wed, 20 Feb 2013 13:00:06 +0000 Subject: Move the SplatByte helper to APInt and generalize it a bit. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175621 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/APIntTest.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'unittests/ADT') diff --git a/unittests/ADT/APIntTest.cpp b/unittests/ADT/APIntTest.cpp index 2510bc738d..f129fa71c8 100644 --- a/unittests/ADT/APIntTest.cpp +++ b/unittests/ADT/APIntTest.cpp @@ -522,4 +522,14 @@ TEST(APIntTest, Rotate) { EXPECT_EQ(Rot, Big.rotr(144)); } +TEST(APIntTest, Splat) { + APInt ValA(8, 0x01); + EXPECT_EQ(ValA, APInt::getSplat(8, ValA)); + EXPECT_EQ(APInt(64, 0x0101010101010101ULL), APInt::getSplat(64, ValA)); + + APInt ValB(3, 5); + EXPECT_EQ(APInt(4, 0xD), APInt::getSplat(4, ValB)); + EXPECT_EQ(APInt(15, 0xDB6D), APInt::getSplat(15, ValB)); +} + } -- cgit v1.2.3-18-g5258 From ea72255f5b0e8d92d5ae9feb5e92605d123888ca Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Thu, 21 Feb 2013 07:55:39 +0000 Subject: Add move ctor/assignment to Optional Code review feedback for r175580 by Jordan Rose. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175729 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/OptionalTest.cpp | 109 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) (limited to 'unittests/ADT') diff --git a/unittests/ADT/OptionalTest.cpp b/unittests/ADT/OptionalTest.cpp index 6fd8bbaf09..6d37bf4e18 100644 --- a/unittests/ADT/OptionalTest.cpp +++ b/unittests/ADT/OptionalTest.cpp @@ -40,6 +40,36 @@ unsigned NonDefaultConstructible::CopyConstructions = 0; unsigned NonDefaultConstructible::Destructions = 0; unsigned NonDefaultConstructible::CopyAssignments = 0; +struct MoveOnly { + static unsigned MoveConstructions; + static unsigned Destructions; + static unsigned MoveAssignments; + int val; + explicit MoveOnly(int val) : val(val) { + } + MoveOnly(MoveOnly&& other) { + val = other.val; + ++MoveConstructions; + } + MoveOnly &operator=(MoveOnly&& other) { + val = other.val; + ++MoveAssignments; + return *this; + } + ~MoveOnly() { + ++Destructions; + } + static void ResetCounts() { + MoveConstructions = 0; + Destructions = 0; + MoveAssignments = 0; + } +}; + +unsigned MoveOnly::MoveConstructions = 0; +unsigned MoveOnly::Destructions = 0; +unsigned MoveOnly::MoveAssignments = 0; + // Test fixture class OptionalTest : public testing::Test { }; @@ -169,5 +199,84 @@ TEST_F(OptionalTest, NullCopyConstructionTest) { EXPECT_EQ(0u, NonDefaultConstructible::Destructions); } +TEST_F(OptionalTest, MoveOnlyNull) { + MoveOnly::ResetCounts(); + Optional O; + EXPECT_EQ(0u, MoveOnly::MoveConstructions); + EXPECT_EQ(0u, MoveOnly::MoveAssignments); + EXPECT_EQ(0u, MoveOnly::Destructions); +} + +TEST_F(OptionalTest, MoveOnlyConstruction) { + MoveOnly::ResetCounts(); + Optional O(MoveOnly(3)); + EXPECT_TRUE((bool)O); + EXPECT_EQ(3, O->val); + EXPECT_EQ(1u, MoveOnly::MoveConstructions); + EXPECT_EQ(0u, MoveOnly::MoveAssignments); + EXPECT_EQ(1u, MoveOnly::Destructions); +} + +TEST_F(OptionalTest, MoveOnlyMoveConstruction) { + Optional A(MoveOnly(3)); + MoveOnly::ResetCounts(); + Optional B(std::move(A)); + EXPECT_FALSE((bool)A); + EXPECT_TRUE((bool)B); + EXPECT_EQ(3, B->val); + EXPECT_EQ(1u, MoveOnly::MoveConstructions); + EXPECT_EQ(0u, MoveOnly::MoveAssignments); + EXPECT_EQ(1u, MoveOnly::Destructions); +} + +TEST_F(OptionalTest, MoveOnlyAssignment) { + MoveOnly::ResetCounts(); + Optional O; + O = MoveOnly(3); + EXPECT_TRUE((bool)O); + EXPECT_EQ(3, O->val); + EXPECT_EQ(1u, MoveOnly::MoveConstructions); + EXPECT_EQ(0u, MoveOnly::MoveAssignments); + EXPECT_EQ(1u, MoveOnly::Destructions); +} + +TEST_F(OptionalTest, MoveOnlyInitializingAssignment) { + Optional A(MoveOnly(3)); + Optional B; + MoveOnly::ResetCounts(); + B = std::move(A); + EXPECT_FALSE((bool)A); + EXPECT_TRUE((bool)B); + EXPECT_EQ(3, B->val); + EXPECT_EQ(1u, MoveOnly::MoveConstructions); + EXPECT_EQ(0u, MoveOnly::MoveAssignments); + EXPECT_EQ(1u, MoveOnly::Destructions); +} + +TEST_F(OptionalTest, MoveOnlyNullingAssignment) { + Optional A; + Optional B(MoveOnly(3)); + MoveOnly::ResetCounts(); + B = std::move(A); + EXPECT_FALSE((bool)A); + EXPECT_FALSE((bool)B); + EXPECT_EQ(0u, MoveOnly::MoveConstructions); + EXPECT_EQ(0u, MoveOnly::MoveAssignments); + EXPECT_EQ(1u, MoveOnly::Destructions); +} + +TEST_F(OptionalTest, MoveOnlyAssigningAssignment) { + Optional A(MoveOnly(3)); + Optional B(MoveOnly(4)); + MoveOnly::ResetCounts(); + B = std::move(A); + EXPECT_FALSE((bool)A); + EXPECT_TRUE((bool)B); + EXPECT_EQ(3, B->val); + EXPECT_EQ(0u, MoveOnly::MoveConstructions); + EXPECT_EQ(1u, MoveOnly::MoveAssignments); + EXPECT_EQ(1u, MoveOnly::Destructions); +} + } // end anonymous namespace -- cgit v1.2.3-18-g5258 From 485740205bcaa9c348c1675f81e8d1673b7694aa Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Thu, 21 Feb 2013 07:58:45 +0000 Subject: Only include move-related Optional tests when rvalue references are available. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175730 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/OptionalTest.cpp | 62 ++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 30 deletions(-) (limited to 'unittests/ADT') diff --git a/unittests/ADT/OptionalTest.cpp b/unittests/ADT/OptionalTest.cpp index 6d37bf4e18..21e38475d3 100644 --- a/unittests/ADT/OptionalTest.cpp +++ b/unittests/ADT/OptionalTest.cpp @@ -40,36 +40,6 @@ unsigned NonDefaultConstructible::CopyConstructions = 0; unsigned NonDefaultConstructible::Destructions = 0; unsigned NonDefaultConstructible::CopyAssignments = 0; -struct MoveOnly { - static unsigned MoveConstructions; - static unsigned Destructions; - static unsigned MoveAssignments; - int val; - explicit MoveOnly(int val) : val(val) { - } - MoveOnly(MoveOnly&& other) { - val = other.val; - ++MoveConstructions; - } - MoveOnly &operator=(MoveOnly&& other) { - val = other.val; - ++MoveAssignments; - return *this; - } - ~MoveOnly() { - ++Destructions; - } - static void ResetCounts() { - MoveConstructions = 0; - Destructions = 0; - MoveAssignments = 0; - } -}; - -unsigned MoveOnly::MoveConstructions = 0; -unsigned MoveOnly::Destructions = 0; -unsigned MoveOnly::MoveAssignments = 0; - // Test fixture class OptionalTest : public testing::Test { }; @@ -199,6 +169,37 @@ TEST_F(OptionalTest, NullCopyConstructionTest) { EXPECT_EQ(0u, NonDefaultConstructible::Destructions); } +#if LLVM_HAS_RVALUE_REFERENCES +struct MoveOnly { + static unsigned MoveConstructions; + static unsigned Destructions; + static unsigned MoveAssignments; + int val; + explicit MoveOnly(int val) : val(val) { + } + MoveOnly(MoveOnly&& other) { + val = other.val; + ++MoveConstructions; + } + MoveOnly &operator=(MoveOnly&& other) { + val = other.val; + ++MoveAssignments; + return *this; + } + ~MoveOnly() { + ++Destructions; + } + static void ResetCounts() { + MoveConstructions = 0; + Destructions = 0; + MoveAssignments = 0; + } +}; + +unsigned MoveOnly::MoveConstructions = 0; +unsigned MoveOnly::Destructions = 0; +unsigned MoveOnly::MoveAssignments = 0; + TEST_F(OptionalTest, MoveOnlyNull) { MoveOnly::ResetCounts(); Optional O; @@ -277,6 +278,7 @@ TEST_F(OptionalTest, MoveOnlyAssigningAssignment) { EXPECT_EQ(1u, MoveOnly::MoveAssignments); EXPECT_EQ(1u, MoveOnly::Destructions); } +#endif } // end anonymous namespace -- cgit v1.2.3-18-g5258