From e570687bd8bff68921a2fa9840ad5aa270a8c9d8 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Sun, 8 Jun 2014 16:00:02 +0000 Subject: Ensure SmallVector::insert doesn't overwrite the last element in the range with the already-moved-from value This would cause the last element in a range to be in a moved-from state after an insert at a non-end position, losing that value entirely in the process. Side note: move_backward is subtle. It copies [A, B) to C-1 and down. (the fact that it decrements both the second and third iterators before the first movement is the subtle part... kind of surprising, anyway) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@210426 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/SmallVectorTest.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'unittests') diff --git a/unittests/ADT/SmallVectorTest.cpp b/unittests/ADT/SmallVectorTest.cpp index 58f55914ba..cccf93b6bc 100644 --- a/unittests/ADT/SmallVectorTest.cpp +++ b/unittests/ADT/SmallVectorTest.cpp @@ -531,4 +531,26 @@ TEST(SmallVectorCustomTest, NoAssignTest) { EXPECT_EQ(42, vec.pop_back_val().x); } +struct MovedFrom { + bool hasValue; + MovedFrom() : hasValue(true) { + } + MovedFrom(MovedFrom&& m) : hasValue(m.hasValue) { + m.hasValue = false; + } + MovedFrom &operator=(MovedFrom&& m) { + hasValue = m.hasValue; + m.hasValue = false; + return *this; + } +}; + +TEST(SmallVectorTest, MidInsert) { + SmallVector v; + v.push_back(MovedFrom()); + v.insert(v.begin(), MovedFrom()); + for (MovedFrom &m : v) + EXPECT_TRUE(m.hasValue); +} + } -- cgit v1.2.3