From fd12dcb3dbb2345638f89d3aa8b5e389d81cfa3a Mon Sep 17 00:00:00 2001 From: ecorm Date: Fri, 24 Oct 2014 14:05:32 -0300 Subject: [PATCH] Added MemoryPoolAllocator to GenericDocument moveunit tests. Added comment in GenericDocument move assignment operator explaining why the static_cast is needed to move the base class. --- include/rapidjson/document.h | 2 ++ test/unittest/documenttest.cpp | 49 +++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 68288c75..ffccabaa 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -1652,6 +1652,8 @@ public: //! Move assignment in C++11 GenericDocument& operator=(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT { + // The static cast is necessary here, because otherwise it would + // attempt to call GenericValue's templated assignment operator. ValueType::operator=(std::move(static_cast(rhs))); // Calling the destructor here would prematurely call stack_'s destructor diff --git a/test/unittest/documenttest.cpp b/test/unittest/documenttest.cpp index 9f7dde4b..1542434a 100644 --- a/test/unittest/documenttest.cpp +++ b/test/unittest/documenttest.cpp @@ -229,9 +229,17 @@ TEST(Document, UTF16_Document) { #if RAPIDJSON_HAS_CXX11_RVALUE_REFS -TEST(Document, MoveConstructor) { - typedef GenericDocument, CrtAllocator> Document; - Document::AllocatorType allocator; +template +struct DocumentMove: public ::testing::Test { +}; + +typedef ::testing::Types< CrtAllocator, MemoryPoolAllocator<> > MoveAllocatorTypes; +TYPED_TEST_CASE(DocumentMove, MoveAllocatorTypes); + +TYPED_TEST(DocumentMove, MoveConstructor) { + typedef TypeParam Allocator; + typedef GenericDocument, Allocator> Document; + Allocator allocator; Document a(&allocator); a.Parse("[\"one\", \"two\", \"three\"]"); @@ -262,8 +270,9 @@ TEST(Document, MoveConstructor) { EXPECT_EQ(&c.GetAllocator(), &allocator); } -TEST(Document, MoveConstructorParseError) { - typedef GenericDocument, CrtAllocator> Document; +TYPED_TEST(DocumentMove, MoveConstructorParseError) { + typedef TypeParam Allocator; + typedef GenericDocument, Allocator> Document; ParseResult noError; Document a; @@ -290,18 +299,19 @@ TEST(Document, MoveConstructorParseError) { EXPECT_EQ(c.GetErrorOffset(), error.Offset()); } -TEST(Document, MoveConstructorStack) { +TYPED_TEST(DocumentMove, MoveConstructorStack) { + typedef TypeParam Allocator; typedef UTF8<> Encoding; - typedef GenericDocument Document; + typedef GenericDocument Document; Document a; size_t defaultCapacity = a.GetStackCapacity(); // Trick Document into getting GetStackCapacity() to return non-zero - typedef GenericReader Reader; + typedef GenericReader Reader; Reader reader(&a.GetAllocator()); GenericStringStream is("[\"one\", \"two\", \"three\"]"); - reader.Parse(is, a); + reader.template Parse(is, a); size_t capacity = a.GetStackCapacity(); EXPECT_GT(capacity, 0); @@ -314,9 +324,10 @@ TEST(Document, MoveConstructorStack) { EXPECT_EQ(c.GetStackCapacity(), capacity); } -TEST(Document, MoveAssignment) { - typedef GenericDocument, CrtAllocator> Document; - Document::AllocatorType allocator; +TYPED_TEST(DocumentMove, MoveAssignment) { + typedef TypeParam Allocator; + typedef GenericDocument, Allocator> Document; + Allocator allocator; Document a(&allocator); a.Parse("[\"one\", \"two\", \"three\"]"); @@ -349,8 +360,9 @@ TEST(Document, MoveAssignment) { EXPECT_EQ(&c.GetAllocator(), &allocator); } -TEST(Document, MoveAssignmentParseError) { - typedef GenericDocument, CrtAllocator> Document; +TYPED_TEST(DocumentMove, MoveAssignmentParseError) { + typedef TypeParam Allocator; + typedef GenericDocument, Allocator> Document; ParseResult noError; Document a; @@ -379,18 +391,19 @@ TEST(Document, MoveAssignmentParseError) { EXPECT_EQ(c.GetErrorOffset(), error.Offset()); } -TEST(Document, MoveAssignmentStack) { +TYPED_TEST(DocumentMove, MoveAssignmentStack) { + typedef TypeParam Allocator; typedef UTF8<> Encoding; - typedef GenericDocument Document; + typedef GenericDocument Document; Document a; size_t defaultCapacity = a.GetStackCapacity(); // Trick Document into getting GetStackCapacity() to return non-zero - typedef GenericReader Reader; + typedef GenericReader Reader; Reader reader(&a.GetAllocator()); GenericStringStream is("[\"one\", \"two\", \"three\"]"); - reader.Parse(is, a); + reader.template Parse(is, a); size_t capacity = a.GetStackCapacity(); EXPECT_GT(capacity, 0);