From 515fd5218a34da44c8a852a34a03566baa8340d6 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 26 Apr 2014 06:49:22 -0700 Subject: [PATCH] Test move assignment in Array. --- format-test.cc | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/format-test.cc b/format-test.cc index de715d59..db74c619 100644 --- a/format-test.cc +++ b/format-test.cc @@ -237,6 +237,36 @@ TEST(ArrayTest, MoveCtor) { EXPECT_GT(array2.capacity(), 5); } +void CheckMoveAssignArray(const char *str, Array &array) { + Array array2; + array2 = std::move(array); + // Move shouldn't destroy the inline content of the first array. + EXPECT_EQ(str, std::string(&array[0], array.size())); + EXPECT_EQ(str, std::string(&array2[0], array2.size())); + EXPECT_EQ(5, array2.capacity()); +} + +TEST(ArrayTest, MoveAssignment) { + Array array; + const char test[] = "test"; + array.append(test, test + 4); + CheckMoveAssignArray("test", array); + // Adding one more character fills the inline buffer, but doesn't cause + // dynamic allocation. + array.push_back('a'); + CheckMoveAssignArray("testa", array); + const char *inline_buffer_ptr = &array[0]; + // Adding one more character causes the content to move from the inline to + // a dynamically allocated buffer. + array.push_back('b'); + Array array2; + array2 = std::move(array); + // Move should rip the guts of the first array. + EXPECT_EQ(inline_buffer_ptr, &array[0]); + EXPECT_EQ("testab", std::string(&array2[0], array2.size())); + EXPECT_GT(array2.capacity(), 5); +} + #endif // FMT_USE_RVALUE_REFERENCES TEST(ArrayTest, Access) {