From 4e004176bacb0160007102742ec62e79a9d958bb Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 11 Apr 2015 22:46:08 -0700 Subject: tests: Improve out-of-memory tests Previously there was no guarantee that the tests that check for out of memory handling behavior are actually correct - e.g. that they correctly simulate out of memory conditions. Now every simulated out of memory condition has to be "guarded" using CHECK_ALLOC_FAIL. It makes sure that every piece of code that is supposed to cause out-of-memory does so, and that no other code runs out of memory unnoticed. --- tests/test_xpath.cpp | 72 +++++----------------------------------------------- 1 file changed, 6 insertions(+), 66 deletions(-) (limited to 'tests/test_xpath.cpp') diff --git a/tests/test_xpath.cpp b/tests/test_xpath.cpp index f5b4c66..57fa95b 100644 --- a/tests/test_xpath.cpp +++ b/tests/test_xpath.cpp @@ -378,19 +378,7 @@ TEST(xpath_out_of_memory_evaluate_concat) pugi::xpath_query q(query.c_str()); -#ifdef PUGIXML_NO_EXCEPTIONS - CHECK(q.evaluate_string(0, 0, xml_node()) == 1); -#else - try - { - q.evaluate_string(0, 0, xml_node()); - - CHECK_FORCE_FAIL("Expected out of memory exception"); - } - catch (const std::bad_alloc&) - { - } -#endif + CHECK_ALLOC_FAIL(CHECK(q.evaluate_string(0, 0, xml_node()) == 1)); } TEST(xpath_out_of_memory_evaluate_substring) @@ -404,19 +392,7 @@ TEST(xpath_out_of_memory_evaluate_substring) pugi::xpath_query q(query.c_str()); -#ifdef PUGIXML_NO_EXCEPTIONS - CHECK(q.evaluate_string(0, 0, xml_node()) == 1); -#else - try - { - q.evaluate_string(0, 0, xml_node()); - - CHECK_FORCE_FAIL("Expected out of memory exception"); - } - catch (const std::bad_alloc&) - { - } -#endif + CHECK_ALLOC_FAIL(CHECK(q.evaluate_string(0, 0, xml_node()) == 1)); } TEST_XML(xpath_out_of_memory_evaluate_union, "") @@ -425,19 +401,7 @@ TEST_XML(xpath_out_of_memory_evaluate_union, " pugi::xpath_query q(STR("a|(a|(a|(a|(a|(a|(a|(a|(a|(a|(a|(a|(a|(a|(a|(a|(a|(a|(a|(a|a)))))))))))))))))))")); -#ifdef PUGIXML_NO_EXCEPTIONS - CHECK(q.evaluate_node_set(doc.child(STR("node"))).empty()); -#else - try - { - q.evaluate_node_set(doc.child(STR("node"))); - - CHECK_FORCE_FAIL("Expected out of memory exception"); - } - catch (const std::bad_alloc&) - { - } -#endif + CHECK_ALLOC_FAIL(CHECK(q.evaluate_node_set(doc.child(STR("node"))).empty())); } TEST_XML(xpath_out_of_memory_evaluate_predicate, "") @@ -446,19 +410,7 @@ TEST_XML(xpath_out_of_memory_evaluate_predicate, " pugi::xpath_query q(STR("//a[//a[//a[//a[//a[//a[//a[//a[//a[//a[//a[//a[//a[//a[true()]]]]]]]]]]]]]]")); -#ifdef PUGIXML_NO_EXCEPTIONS - CHECK(q.evaluate_node_set(doc).empty()); -#else - try - { - q.evaluate_node_set(doc); - - CHECK_FORCE_FAIL("Expected out of memory exception"); - } - catch (const std::bad_alloc&) - { - } -#endif + CHECK_ALLOC_FAIL(CHECK(q.evaluate_node_set(doc).empty())); } TEST(xpath_memory_concat_massive) @@ -634,20 +586,8 @@ TEST(xpath_allocate_string_out_of_memory) test_runner::_memory_fail_threshold = 8*1024; -#ifdef PUGIXML_NO_EXCEPTIONS - CHECK(!xpath_query(query.c_str())); -#else - try - { - #ifndef __DMC__ // DigitalMars exception handling crashes instead of catching the exception... - xpath_query q(query.c_str()); - - CHECK_FORCE_FAIL("Expected out of memory exception"); - #endif - } - catch (const std::bad_alloc&) - { - } +#ifndef __DMC__ // DigitalMars exception handling crashes instead of catching the exception... + CHECK_ALLOC_FAIL(CHECK(!xpath_query(query.c_str()))); #endif } -- cgit v1.2.3