From 65b4316da95f759cb9aa0477293d5611149e1667 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Fri, 28 Feb 2014 13:11:21 +0100 Subject: [PATCH] valuetest: add deep copy unit test This commit adds some simple tests for the deep-copying of values, either based on the explicit constructor, or the CopyFrom function. It uses the CrtAllocator to test for possible double-free errors due to insufficient copying. --- test/unittest/valuetest.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/unittest/valuetest.cpp b/test/unittest/valuetest.cpp index 6800b7b..d1bf63c 100644 --- a/test/unittest/valuetest.cpp +++ b/test/unittest/valuetest.cpp @@ -25,6 +25,36 @@ TEST(Value, assignment_operator) { EXPECT_EQ(1234, y.GetInt()); } +TEST(Value, CopyFrom) +{ + // use CrtAllocator to explicitly malloc/free any memory + // comment this line to use the default Allocator instead + typedef GenericValue,CrtAllocator> Value; + + Value::AllocatorType a; + Value v1(1234); + Value v2(v1,a); // deep copy constructor + EXPECT_TRUE(v1.GetType() == v2.GetType()); + EXPECT_EQ(v1.GetInt(), v2.GetInt()); + + v1.SetString("foo"); + v2.CopyFrom(v1,a); + EXPECT_TRUE(v1.GetType() == v2.GetType()); + EXPECT_STREQ(v1.GetString(), v2.GetString()); + EXPECT_EQ(v1.GetString(), v2.GetString()); // string NOT copied + + v1.SetArray().PushBack(1234,a); + v2.CopyFrom(v1,a); + EXPECT_TRUE(v2.IsArray()); + EXPECT_EQ(v1.Size(), v2.Size()); + + v1.PushBack(Value().SetString("foo",a),a); // push string copy + EXPECT_TRUE(v1.Size() != v2.Size()); + v2.CopyFrom(v1,a); + EXPECT_TRUE(v1.Size() == v2.Size()); + EXPECT_STREQ(v1[1].GetString(), v2[1].GetString()); + EXPECT_NE(v1[1].GetString(), v2[1].GetString()); // string got copied +} TEST(Value, Null) { // Default constructor