Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(658)

Unified Diff: third_party/WebKit/Source/wtf/VectorTest.cpp

Issue 1505773002: Teach WTF::Vector to work with move-only types (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments from Nico Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/Source/wtf/Vector.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/wtf/VectorTest.cpp
diff --git a/third_party/WebKit/Source/wtf/VectorTest.cpp b/third_party/WebKit/Source/wtf/VectorTest.cpp
index df283acd3a9e6a23cb0d46a8698c567d9e3572fc..15a4ed52cfa209f2a2221235c0f57f9d9b5c6c57 100644
--- a/third_party/WebKit/Source/wtf/VectorTest.cpp
+++ b/third_party/WebKit/Source/wtf/VectorTest.cpp
@@ -190,7 +190,6 @@ TEST(VectorTest, OwnPtr)
for (index = 0; index < vector.size(); index++) {
OwnPtr<DestructCounter>& refCounter = vector[index];
EXPECT_EQ(index, static_cast<size_t>(refCounter->get()));
- index++;
}
EXPECT_EQ(0, destructNumber);
@@ -229,6 +228,71 @@ TEST(VectorTest, OwnPtr)
EXPECT_EQ(count, static_cast<size_t>(destructNumber));
}
+class MoveOnly {
+public:
+ explicit MoveOnly(int i = 0)
+ : m_i(i)
+ { }
+
+ MoveOnly(MoveOnly&& other)
+ : m_i(other.m_i)
+ {
+ other.m_i = 0;
+ }
+
+ MoveOnly& operator=(MoveOnly&& other)
+ {
+ if (this != &other) {
+ m_i = other.m_i;
+ other.m_i = 0;
+ }
+ return *this;
+ }
+
+ int value() const { return m_i; }
+
+private:
+ WTF_MAKE_NONCOPYABLE(MoveOnly);
+ int m_i;
+};
+
+TEST(VectorTest, MoveOnlyType)
+{
+ WTF::Vector<MoveOnly> vector;
+ vector.append(MoveOnly(1));
+ vector.append(MoveOnly(2));
+ EXPECT_EQ(2u, vector.size());
+
+ ASSERT_EQ(1, vector.first().value());
+ ASSERT_EQ(2, vector.last().value());
+
+ vector.remove(0);
+ EXPECT_EQ(2, vector[0].value());
+ EXPECT_EQ(1u, vector.size());
+
+ MoveOnly moveOnly(std::move(vector[0]));
+ vector.remove(0);
+ ASSERT_EQ(2, moveOnly.value());
+ ASSERT_EQ(0u, vector.size());
+
+ size_t count = vector.capacity() + 1;
+ for (size_t i = 0; i < count; i++)
+ vector.append(MoveOnly(i + 1)); // +1 to distinguish from default-constructed.
+
+ // Reallocation did not affect the vector's content.
+ EXPECT_EQ(count, vector.size());
+ for (size_t i = 0; i < vector.size(); i++)
+ EXPECT_EQ(static_cast<int>(i + 1), vector[i].value());
+
+ WTF::Vector<MoveOnly> otherVector;
+ vector.swap(otherVector);
+ EXPECT_EQ(count, otherVector.size());
+ EXPECT_EQ(0u, vector.size());
+
+ vector = std::move(otherVector);
+ EXPECT_EQ(count, vector.size());
+}
+
// WrappedInt class will fail if it was memmoved or memcpyed.
static HashSet<void*> constructedWrappedInts;
class WrappedInt {
@@ -381,6 +445,21 @@ TEST(VectorTest, Compare)
compare<WTF::String>();
}
+TEST(VectorTest, AppendFirst)
+{
+ Vector<WTF::String> vector;
+ vector.append("string");
+ // Test passes if it does not crash (reallocation did not make
+ // the input reference stale).
+ size_t limit = vector.capacity() + 1;
+ for (size_t i = 0; i < limit; i++)
+ vector.append(vector.first());
+
+ limit = vector.capacity() + 1;
+ for (size_t i = 0; i < limit; i++)
+ vector.append(const_cast<const WTF::String&>(vector.first()));
+}
+
} // anonymous namespace
} // namespace WTF
« no previous file with comments | « third_party/WebKit/Source/wtf/Vector.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698