From 6eb7519dbae860a789e972c63cd6e83685d961a0 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Mon, 25 Sep 2017 19:18:50 -0700 Subject: tests: Add basic move tests These just verify that move ctor/assignment operator work as expected in simple cases - there are a number of ways in which the internal structure can be incorrect... --- tests/test_document.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'tests') diff --git a/tests/test_document.cpp b/tests/test_document.cpp index ecbe6dc..43d3906 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -1621,3 +1621,34 @@ TEST(document_convert_out_of_memory) delete[] files[j].data; } } + +#ifdef PUGIXML_HAS_MOVE +TEST_XML(document_move_ctor, "") +{ + xml_document other = std::move(doc); + + CHECK(doc.first_child().empty()); + + CHECK_STRING(other.first_child().name(), STR("node1")); + CHECK(other.first_child().parent() == other); + + CHECK_STRING(other.last_child().name(), STR("node2")); + CHECK(other.last_child().parent() == other); +} + +TEST_XML(document_move_assign, "") +{ + xml_document other; + CHECK(other.load_string(STR(""))); + + other = std::move(doc); + + CHECK(doc.first_child().empty()); + + CHECK_STRING(other.first_child().name(), STR("node1")); + CHECK(other.first_child().parent() == other); + + CHECK_STRING(other.last_child().name(), STR("node2")); + CHECK(other.last_child().parent() == other); +} +#endif -- cgit v1.2.3 From faba4786c077f32ca57d48895c058b6894836cfc Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Mon, 25 Sep 2017 22:38:30 -0700 Subject: tests: Add more document move tests Verify that move doesn't allocate and that it preserves structures required for tree memory management and append_buffer in tact. --- tests/test_document.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'tests') diff --git a/tests/test_document.cpp b/tests/test_document.cpp index 43d3906..c2f55a7 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -1651,4 +1651,56 @@ TEST_XML(document_move_assign, "") CHECK_STRING(other.last_child().name(), STR("node2")); CHECK(other.last_child().parent() == other); } + +TEST_XML(document_move_zero_alloc, "") +{ + test_runner::_memory_fail_threshold = 1; + + xml_document other = std::move(doc); + + CHECK(doc.first_child().empty()); + + CHECK_STRING(other.first_child().name(), STR("node1")); + CHECK(other.first_child().parent() == other); + + CHECK_STRING(other.last_child().name(), STR("node2")); + CHECK(other.last_child().parent() == other); +} + +TEST(document_move_append_buffer) +{ + xml_document* doc = new xml_document(); + CHECK(doc->load_string(STR(""))); + CHECK(doc->child(STR("node1")).append_buffer("", 8)); + CHECK(doc->child(STR("node1")).append_buffer("", 8)); + + xml_document other = std::move(*doc); + delete doc; + + CHECK(other.child(STR("node1")).append_buffer("", 8)); + CHECK(other.child(STR("node1")).append_buffer("", 8)); + + CHECK_NODE(other, STR("")); +} + +TEST(document_move_append_child) +{ + xml_document* doc = new xml_document(); + CHECK(doc->load_string(STR(""))); + + xml_document other = std::move(*doc); + delete doc; + + for (int i = 0; i < 1000; ++i) + other.child(STR("node1")).append_child(STR("node")); + + for (int i = 0; i < 1000; ++i) + other.child(STR("node1")).remove_child(other.child(STR("node1")).last_child()); + + CHECK_NODE(other, STR("")); + + other.remove_child(other.first_child()); + + CHECK(!other.first_child()); +} #endif -- cgit v1.2.3 From 402b967fa95bebbc4786ae755391f0c745717df6 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Mon, 25 Sep 2017 22:47:10 -0700 Subject: tests: Add more move tests Make sure we have coverage for empty documents and for large documents that trigger compact_shared_parent != root for some pages. --- tests/test_document.cpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test_document.cpp b/tests/test_document.cpp index c2f55a7..fa0dc8d 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -1691,10 +1691,10 @@ TEST(document_move_append_child) xml_document other = std::move(*doc); delete doc; - for (int i = 0; i < 1000; ++i) + for (int i = 0; i < 3000; ++i) other.child(STR("node1")).append_child(STR("node")); - for (int i = 0; i < 1000; ++i) + for (int i = 0; i < 3000; ++i) other.child(STR("node1")).remove_child(other.child(STR("node1")).last_child()); CHECK_NODE(other, STR("")); @@ -1703,4 +1703,27 @@ TEST(document_move_append_child) CHECK(!other.first_child()); } + +TEST(document_move_empty) +{ + xml_document* doc = new xml_document(); + xml_document other = std::move(*doc); + delete doc; +} + +TEST(document_move_large) +{ + xml_document* doc = new xml_document(); + + for (int i = 0; i < 3000; ++i) + doc->append_child(STR("node")); + + xml_document other = std::move(*doc); + delete doc; + + for (int i = 0; i < 3000; ++i) + CHECK(other.remove_child(other.first_child())); + + CHECK(!other.first_child()); +} #endif -- cgit v1.2.3 From 26ead385a782934bbc5b2485bb9f3b71802e645c Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Mon, 25 Sep 2017 22:52:03 -0700 Subject: tests: Add more move tests Add a test that checks that static buffer pointer was moved correctly by checking if offset_debug still works. --- tests/test_document.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'tests') diff --git a/tests/test_document.cpp b/tests/test_document.cpp index fa0dc8d..3ca69cd 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -1726,4 +1726,13 @@ TEST(document_move_large) CHECK(!other.first_child()); } + +TEST_XML(document_move_buffer, "") +{ + CHECK(doc.child(STR("node2")).offset_debug() == 9); + + xml_document other = std::move(doc); + + CHECK(other.child(STR("node2")).offset_debug() == 9); +} #endif -- cgit v1.2.3 From 50bc0d5a69afcbad0df8de8f831f6ee58504aa6d Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Mon, 25 Sep 2017 22:54:42 -0700 Subject: tests: Adjust move coverage tests Large test wasn't testing shared parent condition properly - add one more level of hierarchy so that it works as expected. --- tests/test_document.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/test_document.cpp b/tests/test_document.cpp index 3ca69cd..4205949 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -1715,16 +1715,20 @@ TEST(document_move_large) { xml_document* doc = new xml_document(); + xml_node dn = doc->append_child(STR("node")); + for (int i = 0; i < 3000; ++i) - doc->append_child(STR("node")); + dn.append_child(STR("child")); xml_document other = std::move(*doc); delete doc; + xml_node on = other.child(STR("node")); + for (int i = 0; i < 3000; ++i) - CHECK(other.remove_child(other.first_child())); + CHECK(on.remove_child(on.first_child())); - CHECK(!other.first_child()); + CHECK(!on.first_child()); } TEST_XML(document_move_buffer, "") -- cgit v1.2.3 From b0fc587a7fbeb902bb0760dba73ee621105a86bc Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Fri, 20 Oct 2017 21:53:42 -0700 Subject: tests: Add more move tests We now check that appending a child to a moved document performs no allocations - this is already the case, but if we neglected to copy the allocator state this test would fail. --- tests/test_document.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'tests') diff --git a/tests/test_document.cpp b/tests/test_document.cpp index 4205949..e6b9081 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -1739,4 +1739,15 @@ TEST_XML(document_move_buffer, "") CHECK(other.child(STR("node2")).offset_debug() == 9); } + +TEST_XML(document_move_append_child_zero_alloc, "") +{ + test_runner::_memory_fail_threshold = 1; + + xml_document other = std::move(doc); + + CHECK(other.append_child(STR("node3"))); + + CHECK_NODE(other, STR("")); +} #endif -- cgit v1.2.3 From 0504fa4e90e0024ff612fdfdf740ef7826191ebe Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Thu, 26 Oct 2017 08:36:05 -0700 Subject: tests: Add more tests for document move These tests currently fail for compact mode because of ->reserve() failing. --- tests/test_document.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'tests') diff --git a/tests/test_document.cpp b/tests/test_document.cpp index e6b9081..8b2573b 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -1750,4 +1750,37 @@ TEST_XML(document_move_append_child_zero_alloc, "") CHECK_NODE(other, STR("")); } + +#ifndef PUGIXML_COMPACT +TEST(document_move_empty_zero_alloc) +{ + xml_document* docs = new xml_document[32]; + + test_runner::_memory_fail_threshold = 1; + + for (int i = 1; i < 32; ++i) + docs[i] = std::move(docs[i-1]); + + delete[] docs; +} + +TEST(document_move_repeated_zero_alloc) +{ + xml_document* docs = new xml_document[32]; + + CHECK(docs[0].load_string(STR(""))); + + test_runner::_memory_fail_threshold = 1; + + for (int i = 1; i < 32; ++i) + docs[i] = std::move(docs[i-1]); + + for (int i = 0; i < 31; ++i) + CHECK(!docs[i].first_child()); + + CHECK_NODE(docs[31], STR("")); + + delete[] docs; +} +#endif #endif -- cgit v1.2.3 From 58611c87026699c6fdc4b2d2a2057b594985282c Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Mon, 13 Nov 2017 08:59:16 -0800 Subject: tests: Add compact move tests This helps make sure our error handling logic works and is exercised. --- tests/test_document.cpp | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/test_document.cpp b/tests/test_document.cpp index 8b2573b..08d836c 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -1751,7 +1751,6 @@ TEST_XML(document_move_append_child_zero_alloc, "") CHECK_NODE(other, STR("")); } -#ifndef PUGIXML_COMPACT TEST(document_move_empty_zero_alloc) { xml_document* docs = new xml_document[32]; @@ -1764,9 +1763,10 @@ TEST(document_move_empty_zero_alloc) delete[] docs; } +#ifndef PUGIXML_COMPACT TEST(document_move_repeated_zero_alloc) { - xml_document* docs = new xml_document[32]; + xml_document docs[32]; CHECK(docs[0].load_string(STR(""))); @@ -1779,8 +1779,30 @@ TEST(document_move_repeated_zero_alloc) CHECK(!docs[i].first_child()); CHECK_NODE(docs[31], STR("")); +} +#endif - delete[] docs; +#ifdef PUGIXML_COMPACT +TEST(document_move_compact_fail) +{ + xml_document docs[32]; + + CHECK(docs[0].load_string(STR(""))); + + test_runner::_memory_fail_threshold = 1; + + int safe_count = 21; + + for (int i = 1; i <= safe_count; ++i) + docs[i] = std::move(docs[i-1]); + + CHECK_ALLOC_FAIL(docs[safe_count+1] = std::move(docs[safe_count])); + + for (int i = 0; i < safe_count; ++i) + CHECK(!docs[i].first_child()); + + CHECK_NODE(docs[safe_count], STR("")); + CHECK(!docs[safe_count+1].first_child()); } #endif #endif -- cgit v1.2.3