summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArseny Kapoulkine <arseny.kapoulkine@gmail.com>2014-10-01 07:02:59 +0000
committerArseny Kapoulkine <arseny.kapoulkine@gmail.com>2014-10-01 07:02:59 +0000
commit3ae516abe2d51415c9527f4f96e97b85413aa479 (patch)
tree59c6391345af77d22214a9c2a42e6f9d12a9e6f4
parentfebe4f0209f86225ebeedfb0874feb3cb96e7c89 (diff)
tests: Add tests for copyless copy and related potential bugs
git-svn-id: https://pugixml.googlecode.com/svn/trunk@1033 99668b35-9821-0410-8761-19e4c4f06640
-rw-r--r--tests/test_dom_modify.cpp79
-rw-r--r--tests/test_xpath.cpp26
2 files changed, 104 insertions, 1 deletions
diff --git a/tests/test_dom_modify.cpp b/tests/test_dom_modify.cpp
index 6e17dd4..a66f56a 100644
--- a/tests/test_dom_modify.cpp
+++ b/tests/test_dom_modify.cpp
@@ -1246,7 +1246,7 @@ TEST_XML(dom_node_move_tree, "<root><n1 a1='v1'><c1/>t1</n1><n2 a2='v2'><c2/>t2<
TEST(dom_node_copy_stackless)
{
unsigned int count = 20000;
- std::basic_string<pugi::char_t> data;
+ std::basic_string<char_t> data;
for (unsigned int i = 0; i < count; ++i)
data += STR("<a>");
@@ -1264,3 +1264,80 @@ TEST(dom_node_copy_stackless)
CHECK_NODE(doc, data.c_str());
}
+
+TEST(dom_node_copy_copyless)
+{
+ std::basic_string<char_t> data;
+ data += STR("<node>");
+ for (int i = 0; i < 10000; ++i)
+ data += STR("pcdata");
+ data += STR("<?name value?><child attr1=\"\" attr2=\"value2\" /></node>");
+
+ std::basic_string<char_t> datacopy = data;
+
+ // the document is parsed in-place so there should only be 1 page worth of allocations
+ test_runner::_memory_fail_threshold = 32768 + 128;
+
+ xml_document doc;
+ CHECK(doc.load_buffer_inplace(&datacopy[0], datacopy.size() * sizeof(char_t), parse_full));
+
+ // this copy should share all string storage; since there are not a lot of nodes we should not have *any* allocations here (everything will fit in the same page in the document)
+ xml_node copy = doc.append_copy(doc.child(STR("node")));
+ xml_node copy2 = doc.append_copy(copy);
+
+ CHECK_NODE(copy, data.c_str());
+ CHECK_NODE(copy2, data.c_str());
+}
+
+TEST(dom_node_copy_copyless_mix)
+{
+ xml_document doc;
+ CHECK(doc.load(STR("<node>pcdata<?name value?><child attr1=\"\" attr2=\"value2\" /></node>"), parse_full));
+
+ xml_node child = doc.child(STR("node")).child(STR("child"));
+
+ child.set_name(STR("copychild"));
+ child.attribute(STR("attr2")).set_name(STR("copyattr2"));
+ child.attribute(STR("attr1")).set_value(STR("copyvalue1"));
+
+ std::basic_string<char_t> data;
+ for (int i = 0; i < 10000; ++i)
+ data += STR("pcdata");
+
+ doc.child(STR("node")).text().set(data.c_str());
+
+ xml_node copy = doc.append_copy(doc.child(STR("node")));
+ xml_node copy2 = doc.append_copy(copy);
+
+ std::basic_string<char_t> dataxml;
+ dataxml += STR("<node>");
+ dataxml += data;
+ dataxml += STR("<?name value?><copychild attr1=\"copyvalue1\" copyattr2=\"value2\" /></node>");
+
+ CHECK_NODE(copy, dataxml.c_str());
+ CHECK_NODE(copy2, dataxml.c_str());
+}
+
+TEST_XML(dom_node_copyless_taint, "<node attr=\"value\" />")
+{
+ xml_node node = doc.child(STR("node"));
+ xml_node copy = doc.append_copy(node);
+
+ CHECK_NODE(doc, STR("<node attr=\"value\" /><node attr=\"value\" />"));
+
+ node.set_name(STR("nod1"));
+
+ CHECK_NODE(doc, STR("<nod1 attr=\"value\" /><node attr=\"value\" />"));
+
+ xml_node copy2 = doc.append_copy(copy);
+
+ CHECK_NODE(doc, STR("<nod1 attr=\"value\" /><node attr=\"value\" /><node attr=\"value\" />"));
+
+ copy.attribute(STR("attr")).set_value(STR("valu2"));
+
+ CHECK_NODE(doc, STR("<nod1 attr=\"value\" /><node attr=\"valu2\" /><node attr=\"value\" />"));
+
+ copy2.attribute(STR("attr")).set_name(STR("att3"));
+
+ CHECK_NODE(doc, STR("<nod1 attr=\"value\" /><node attr=\"valu2\" /><node att3=\"value\" />"));
+}
diff --git a/tests/test_xpath.cpp b/tests/test_xpath.cpp
index f3af88a..1a9a159 100644
--- a/tests/test_xpath.cpp
+++ b/tests/test_xpath.cpp
@@ -434,4 +434,30 @@ TEST(xpath_memory_concat_massive)
CHECK(size == 5001);
}
+TEST_XML(xpath_sort_copy_share, "<node><child1 attr1='value1' attr2='value2'/><child2 attr1='value1'>test</child2></node>")
+{
+ // copy sharing shares the name/value data for nodes that can potentially make document order optimization invalid (silently)
+ xml_node node = doc.child(STR("node"));
+ xml_node child1 = node.child(STR("child1"));
+ xml_node child2 = node.child(STR("child2"));
+
+ // swap child1 & child2
+ node.prepend_copy(child2);
+ node.append_copy(child1);
+
+ node.remove_child(child1);
+ node.remove_child(child2);
+
+ // just some random union order, it should not matter probably?
+ xpath_node_set ns = doc.child(STR("node")).select_nodes(STR("child1 | child2 | child1/@* | . | child2/@* | child2/text()"));
+
+ ns.sort(false);
+ xpath_node_set sorted = ns;
+
+ ns.sort(true);
+ xpath_node_set reverse_sorted = ns;
+
+ xpath_node_set_tester(sorted, "sorted order failed") % 2 % 3 % 4 % 5 % 6 % 7 % 8;
+ xpath_node_set_tester(reverse_sorted, "reverse sorted order failed") % 8 % 7 % 6 % 5 % 4 % 3 % 2;
+}
#endif