From 3ae516abe2d51415c9527f4f96e97b85413aa479 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Wed, 1 Oct 2014 07:02:59 +0000 Subject: 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 --- tests/test_dom_modify.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) (limited to 'tests/test_dom_modify.cpp') 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, "t1t2< TEST(dom_node_copy_stackless) { unsigned int count = 20000; - std::basic_string data; + std::basic_string data; for (unsigned int i = 0; i < count; ++i) data += STR(""); @@ -1264,3 +1264,80 @@ TEST(dom_node_copy_stackless) CHECK_NODE(doc, data.c_str()); } + +TEST(dom_node_copy_copyless) +{ + std::basic_string data; + data += STR(""); + for (int i = 0; i < 10000; ++i) + data += STR("pcdata"); + data += STR(""); + + std::basic_string 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("pcdata"), 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 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 dataxml; + dataxml += STR(""); + dataxml += data; + dataxml += STR(""); + + CHECK_NODE(copy, dataxml.c_str()); + CHECK_NODE(copy2, dataxml.c_str()); +} + +TEST_XML(dom_node_copyless_taint, "") +{ + xml_node node = doc.child(STR("node")); + xml_node copy = doc.append_copy(node); + + CHECK_NODE(doc, STR("")); + + node.set_name(STR("nod1")); + + CHECK_NODE(doc, STR("")); + + xml_node copy2 = doc.append_copy(copy); + + CHECK_NODE(doc, STR("")); + + copy.attribute(STR("attr")).set_value(STR("valu2")); + + CHECK_NODE(doc, STR("")); + + copy2.attribute(STR("attr")).set_name(STR("att3")); + + CHECK_NODE(doc, STR("")); +} -- cgit v1.2.3