diff options
author | Arseny Kapoulkine <arseny.kapoulkine@gmail.com> | 2017-02-05 21:34:54 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-05 21:34:54 -0800 |
commit | a9fe2bb62e0976ab74fdb5266cc3725250eca075 (patch) | |
tree | 85c96e92afb2e3437b0eb20c805227b91e5e69f2 /tests/test_compact.cpp | |
parent | d3b9e4e1e85d0aca562d0e6b62533e68e5a4a749 (diff) | |
parent | 10676b6b8548ddbf9458993062e6a27c2c233d48 (diff) |
Merge pull request #131 from zeux/xpath-noeh
XPath: Remove exceptional control flow
Diffstat (limited to 'tests/test_compact.cpp')
-rw-r--r-- | tests/test_compact.cpp | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/tests/test_compact.cpp b/tests/test_compact.cpp new file mode 100644 index 0000000..f9560c9 --- /dev/null +++ b/tests/test_compact.cpp @@ -0,0 +1,114 @@ +#ifdef PUGIXML_COMPACT +#include "common.hpp" + +static void overflow_hash_table(xml_document& doc) +{ + xml_node n = doc.child(STR("n")); + + // compact encoding assumes next_sibling is a forward-only pointer so we can allocate hash entries by reordering nodes + // we allocate enough hash entries to be exactly on the edge of rehash threshold + for (int i = 0; i < 8; ++i) + CHECK(n.prepend_child(node_element)); +} + +TEST_XML_FLAGS(compact_out_of_memory_string, "<n a='v'/><?n v?>", parse_pi) +{ + test_runner::_memory_fail_threshold = 1; + + overflow_hash_table(doc); + + xml_attribute a = doc.child(STR("n")).attribute(STR("a")); + xml_node pi = doc.last_child(); + + CHECK_ALLOC_FAIL(CHECK(!pi.set_name(STR("name")))); + CHECK_ALLOC_FAIL(CHECK(!pi.set_value(STR("value")))); + CHECK_ALLOC_FAIL(CHECK(!a.set_name(STR("name")))); + CHECK_ALLOC_FAIL(CHECK(!a.set_value(STR("value")))); +} + +TEST_XML(compact_out_of_memory_attribute, "<n a='v'/>") +{ + test_runner::_memory_fail_threshold = 1; + + overflow_hash_table(doc); + + xml_node n = doc.child(STR("n")); + xml_attribute a = n.attribute(STR("a")); + + CHECK_ALLOC_FAIL(CHECK(!n.append_attribute(STR("")))); + CHECK_ALLOC_FAIL(CHECK(!n.prepend_attribute(STR("")))); + CHECK_ALLOC_FAIL(CHECK(!n.insert_attribute_after(STR(""), a))); + CHECK_ALLOC_FAIL(CHECK(!n.insert_attribute_before(STR(""), a))); +} + +TEST_XML(compact_out_of_memory_attribute_copy, "<n a='v'/>") +{ + test_runner::_memory_fail_threshold = 1; + + overflow_hash_table(doc); + + xml_node n = doc.child(STR("n")); + xml_attribute a = n.attribute(STR("a")); + + CHECK_ALLOC_FAIL(CHECK(!n.append_copy(a))); + CHECK_ALLOC_FAIL(CHECK(!n.prepend_copy(a))); + CHECK_ALLOC_FAIL(CHECK(!n.insert_copy_after(a, a))); + CHECK_ALLOC_FAIL(CHECK(!n.insert_copy_before(a, a))); +} + +TEST_XML(compact_out_of_memory_node, "<n/>") +{ + test_runner::_memory_fail_threshold = 1; + + overflow_hash_table(doc); + + xml_node n = doc.child(STR("n")); + + CHECK_ALLOC_FAIL(CHECK(!doc.append_child(node_element))); + CHECK_ALLOC_FAIL(CHECK(!doc.prepend_child(node_element))); + CHECK_ALLOC_FAIL(CHECK(!doc.insert_child_after(node_element, n))); + CHECK_ALLOC_FAIL(CHECK(!doc.insert_child_before(node_element, n))); +} + +TEST_XML(compact_out_of_memory_node_copy, "<n/>") +{ + test_runner::_memory_fail_threshold = 1; + + overflow_hash_table(doc); + + xml_node n = doc.child(STR("n")); + + CHECK_ALLOC_FAIL(CHECK(!doc.append_copy(n))); + CHECK_ALLOC_FAIL(CHECK(!doc.prepend_copy(n))); + CHECK_ALLOC_FAIL(CHECK(!doc.insert_copy_after(n, n))); + CHECK_ALLOC_FAIL(CHECK(!doc.insert_copy_before(n, n))); +} + +TEST_XML(compact_out_of_memory_node_move, "<n/><ne/>") +{ + test_runner::_memory_fail_threshold = 1; + + overflow_hash_table(doc); + + xml_node n = doc.child(STR("n")); + xml_node ne = doc.child(STR("ne")); + + CHECK_ALLOC_FAIL(CHECK(!doc.append_move(n))); + CHECK_ALLOC_FAIL(CHECK(!doc.prepend_move(n))); + CHECK_ALLOC_FAIL(CHECK(!doc.insert_move_after(n, ne))); + CHECK_ALLOC_FAIL(CHECK(!doc.insert_move_before(n, ne))); +} + +TEST_XML(compact_out_of_memory_remove, "<n a='v'/>") +{ + test_runner::_memory_fail_threshold = 1; + + overflow_hash_table(doc); + + xml_node n = doc.child(STR("n")); + xml_attribute a = n.attribute(STR("a")); + + CHECK_ALLOC_FAIL(CHECK(!n.remove_attribute(a))); + CHECK_ALLOC_FAIL(CHECK(!doc.remove_child(n))); +} +#endif |