From 998a534df7232c142521c57e7abd5e366ac3a8eb Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Sun, 29 Aug 2010 15:49:06 +0000 Subject: tests: Changed XPath checking macros to avoid query copying under GCC git-svn-id: http://pugixml.googlecode.com/svn/trunk@692 99668b35-9821-0410-8761-19e4c4f06640 --- tests/test.cpp | 56 +++++++++++++++--------------------------- tests/test.hpp | 34 ++++++++++++------------- tests/test_xpath_api.cpp | 16 +++++++++--- tests/test_xpath_variables.cpp | 42 +++++++++++++++---------------- 4 files changed, 69 insertions(+), 79 deletions(-) (limited to 'tests') diff --git a/tests/test.cpp b/tests/test.cpp index a48ef48..0295cad 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -70,65 +70,49 @@ bool test_double_nan(double value) } #ifndef PUGIXML_NO_XPATH -bool test_xpath_string(const pugi::xpath_node& node, const pugi::xpath_query& query, const pugi::char_t* expected) +bool test_xpath_string(const pugi::xpath_node& node, const pugi::char_t* query, pugi::xpath_variable_set* variables, const pugi::char_t* expected) { + pugi::xpath_query q(query, variables); + if (!q) return false; + const size_t capacity = 64; pugi::char_t result[capacity]; - size_t size = query.evaluate_string(result, capacity, node); + size_t size = q.evaluate_string(result, capacity, node); if (size <= capacity) return test_string_equal(result, expected); std::basic_string buffer(size, ' '); - return query.evaluate_string(&buffer[0], size, node) == size && test_string_equal(buffer.c_str(), expected); + return q.evaluate_string(&buffer[0], size, node) == size && test_string_equal(buffer.c_str(), expected); } -bool test_xpath_boolean(const pugi::xpath_node& node, const pugi::xpath_query& query, bool expected) +bool test_xpath_boolean(const pugi::xpath_node& node, const pugi::char_t* query, pugi::xpath_variable_set* variables, bool expected) { - return query.evaluate_boolean(node) == expected; + pugi::xpath_query q(query, variables); + if (!q) return false; + + return q.evaluate_boolean(node) == expected; } -bool test_xpath_number(const pugi::xpath_node& node, const pugi::xpath_query& query, double expected) +bool test_xpath_number(const pugi::xpath_node& node, const pugi::char_t* query, pugi::xpath_variable_set* variables, double expected) { - double value = query.evaluate_number(node); + pugi::xpath_query q(query, variables); + if (!q) return false; + + double value = q.evaluate_number(node); double absolute_error = fabs(value - expected); const double tolerance = 1e-15f; return absolute_error < tolerance || absolute_error < fabs(expected) * tolerance; } -bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::xpath_query& query) -{ - return test_double_nan(query.evaluate_number(node)); -} - -bool test_xpath_string(const pugi::xpath_node& node, const pugi::char_t* query, const pugi::char_t* expected) -{ - pugi::xpath_query q(query); - - return q && test_xpath_string(node, q, expected); -} - -bool test_xpath_boolean(const pugi::xpath_node& node, const pugi::char_t* query, bool expected) -{ - pugi::xpath_query q(query); - - return q && test_xpath_boolean(node, q, expected); -} - -bool test_xpath_number(const pugi::xpath_node& node, const pugi::char_t* query, double expected) -{ - pugi::xpath_query q(query); - - return q && test_xpath_number(node, q, expected); -} - -bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::char_t* query) +bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::char_t* query, pugi::xpath_variable_set* variables) { - pugi::xpath_query q(query); + pugi::xpath_query q(query, variables); + if (!q) return false; - return q && test_xpath_number_nan(node, q); + return test_double_nan(q.evaluate_number(node)); } bool test_xpath_fail_compile(const pugi::char_t* query, pugi::xpath_variable_set* variables) diff --git a/tests/test.hpp b/tests/test.hpp index 7dd4df3..4ab2802 100644 --- a/tests/test.hpp +++ b/tests/test.hpp @@ -38,17 +38,12 @@ bool test_node(const pugi::xml_node& node, const pugi::char_t* contents, const p bool test_double_nan(double value); #ifndef PUGIXML_NO_XPATH -bool test_xpath_string(const pugi::xpath_node& node, const pugi::xpath_query& query, const pugi::char_t* expected); -bool test_xpath_boolean(const pugi::xpath_node& node, const pugi::xpath_query& query, bool expected); -bool test_xpath_number(const pugi::xpath_node& node, const pugi::xpath_query& query, double expected); -bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::xpath_query& query); +bool test_xpath_string(const pugi::xpath_node& node, const pugi::char_t* query, pugi::xpath_variable_set* variables, const pugi::char_t* expected); +bool test_xpath_boolean(const pugi::xpath_node& node, const pugi::char_t* query, pugi::xpath_variable_set* variables, bool expected); +bool test_xpath_number(const pugi::xpath_node& node, const pugi::char_t* query, pugi::xpath_variable_set* variables, double expected); +bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::char_t* query, pugi::xpath_variable_set* variables); -bool test_xpath_string(const pugi::xpath_node& node, const pugi::char_t* query, const pugi::char_t* expected); -bool test_xpath_boolean(const pugi::xpath_node& node, const pugi::char_t* query, bool expected); -bool test_xpath_number(const pugi::xpath_node& node, const pugi::char_t* query, double expected); -bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::char_t* query); - -bool test_xpath_fail_compile(const pugi::char_t* query, pugi::xpath_variable_set* variables = 0); +bool test_xpath_fail_compile(const pugi::char_t* query, pugi::xpath_variable_set* variables); struct xpath_node_set_tester { @@ -129,14 +124,19 @@ struct dummy_fixture {}; #define CHECK_NODE(node, expected) CHECK_NODE_EX(node, expected, PUGIXML_TEXT(""), pugi::format_raw) #ifndef PUGIXML_NO_XPATH -#define CHECK_XPATH_STRING(node, query, expected) CHECK_TEXT(test_xpath_string(node, query, expected), STRINGIZE(query) " does not evaluate to " STRINGIZE(expected) " in context " STRINGIZE(node)) -#define CHECK_XPATH_BOOLEAN(node, query, expected) CHECK_TEXT(test_xpath_boolean(node, query, expected), STRINGIZE(query) " does not evaluate to " STRINGIZE(expected) " in context " STRINGIZE(node)) -#define CHECK_XPATH_NUMBER(node, query, expected) CHECK_TEXT(test_xpath_number(node, query, expected), STRINGIZE(query) " does not evaluate to " STRINGIZE(expected) " in context " STRINGIZE(node)) -#define CHECK_XPATH_NUMBER_NAN(node, query) CHECK_TEXT(test_xpath_number_nan(node, query), STRINGIZE(query) " does not evaluate to NaN in context " STRINGIZE(node)) -#define CHECK_XPATH_FAIL(query) CHECK_TEXT(test_xpath_fail_compile(query), STRINGIZE(query) " should not compile") +#define CHECK_XPATH_STRING_VAR(node, query, variables, expected) CHECK_TEXT(test_xpath_string(node, query, variables, expected), STRINGIZE(query) " does not evaluate to " STRINGIZE(expected) " in context " STRINGIZE(node)) +#define CHECK_XPATH_BOOLEAN_VAR(node, query, variables, expected) CHECK_TEXT(test_xpath_boolean(node, query, variables, expected), STRINGIZE(query) " does not evaluate to " STRINGIZE(expected) " in context " STRINGIZE(node)) +#define CHECK_XPATH_NUMBER_VAR(node, query, variables, expected) CHECK_TEXT(test_xpath_number(node, query, variables, expected), STRINGIZE(query) " does not evaluate to " STRINGIZE(expected) " in context " STRINGIZE(node)) +#define CHECK_XPATH_NUMBER_NAN_VAR(node, query, variables) CHECK_TEXT(test_xpath_number_nan(node, query, variables), STRINGIZE(query) " does not evaluate to NaN in context " STRINGIZE(node)) +#define CHECK_XPATH_NODESET_VAR(node, query, variables) xpath_node_set_tester(pugi::xpath_query(query, variables).evaluate_node_set(node), CHECK_JOIN2(STRINGIZE(query) " does not evaluate to expected set in context " STRINGIZE(node), " at "__FILE__ ":", __LINE__)) #define CHECK_XPATH_FAIL_VAR(query, variables) CHECK_TEXT(test_xpath_fail_compile(query, variables), STRINGIZE(query) " should not compile") -#define CHECK_XPATH_NODESET_Q(node, query) xpath_node_set_tester(query.evaluate_node_set(node), CHECK_JOIN2(STRINGIZE(query) " does not evaluate to expected set in context " STRINGIZE(node), " at "__FILE__ ":", __LINE__)) -#define CHECK_XPATH_NODESET(node, query) CHECK_XPATH_NODESET_Q(node, pugi::xpath_query(query)) + +#define CHECK_XPATH_STRING(node, query, expected) CHECK_XPATH_STRING_VAR(node, query, 0, expected) +#define CHECK_XPATH_BOOLEAN(node, query, expected) CHECK_XPATH_BOOLEAN_VAR(node, query, 0, expected) +#define CHECK_XPATH_NUMBER(node, query, expected) CHECK_XPATH_NUMBER_VAR(node, query, 0, expected) +#define CHECK_XPATH_NUMBER_NAN(node, query) CHECK_XPATH_NUMBER_NAN_VAR(node, query, 0) +#define CHECK_XPATH_NODESET(node, query) CHECK_XPATH_NODESET_VAR(node, query, 0) +#define CHECK_XPATH_FAIL(query) CHECK_XPATH_FAIL_VAR(query, 0) #endif #define STR(text) PUGIXML_TEXT(text) diff --git a/tests/test_xpath_api.cpp b/tests/test_xpath_api.cpp index b91910a..90f316e 100644 --- a/tests/test_xpath_api.cpp +++ b/tests/test_xpath_api.cpp @@ -169,10 +169,18 @@ TEST_XML(xpath_api_evaluate_attr, "") #ifdef PUGIXML_NO_EXCEPTIONS TEST_XML(xpath_api_evaluate_fail, "") { - CHECK_XPATH_BOOLEAN(doc, STR(""), false); - CHECK_XPATH_NUMBER_NAN(doc, STR("")); - CHECK_XPATH_STRING(doc, STR(""), STR("")); - CHECK_XPATH_NODESET(doc, STR("")); + xpath_query q(STR("")); + + CHECK(q.evaluate_boolean(doc) == false); + CHECK_DOUBLE_NAN(q.evaluate_number(doc)); + + CHECK(q.evaluate_string(0, 0, doc) == 1); // null terminator + +#ifndef PUGIXML_NO_STL + CHECK(q.evaluate_string(doc).empty()); +#endif + + CHECK(q.evaluate_node_set(doc).empty()); } #endif diff --git a/tests/test_xpath_variables.cpp b/tests/test_xpath_variables.cpp index f759dc1..001b1b5 100644 --- a/tests/test_xpath_variables.cpp +++ b/tests/test_xpath_variables.cpp @@ -198,10 +198,10 @@ TEST_XML(xpath_variables_evaluate, "") set.set(STR("var3"), STR("value")); set.set(STR("var4"), doc.select_nodes(STR("*"))); - CHECK_XPATH_BOOLEAN(doc, xpath_query(STR("$var1"), &set), true); - CHECK_XPATH_NUMBER(doc, xpath_query(STR("$var2"), &set), 0.5); - CHECK_XPATH_STRING(doc, xpath_query(STR("$var3"), &set), STR("value")); - CHECK_XPATH_NODESET_Q(doc, xpath_query(STR("$var4"), &set)) % 2; + CHECK_XPATH_BOOLEAN_VAR(doc, STR("$var1"), &set, true); + CHECK_XPATH_NUMBER_VAR(doc, STR("$var2"), &set, 0.5); + CHECK_XPATH_STRING_VAR(doc, STR("$var3"), &set, STR("value")); + CHECK_XPATH_NODESET_VAR(doc, STR("$var4"), &set) % 2; } TEST_XML(xpath_variables_evaluate_conversion, "3") @@ -209,12 +209,10 @@ TEST_XML(xpath_variables_evaluate_conversion, "3") xpath_variable_set set; set.set(STR("var"), doc.select_nodes(STR("*"))); - xpath_query query(STR("$var"), &set); - - CHECK_XPATH_BOOLEAN(doc, query, true); - CHECK_XPATH_NUMBER(doc, query, 3); - CHECK_XPATH_STRING(doc, query, STR("3")); - CHECK_XPATH_NODESET_Q(doc, query) % 2; + CHECK_XPATH_BOOLEAN_VAR(doc, STR("$var"), &set, true); + CHECK_XPATH_NUMBER_VAR(doc, STR("$var"), &set, 3); + CHECK_XPATH_STRING_VAR(doc, STR("$var"), &set, STR("3")); + CHECK_XPATH_NODESET_VAR(doc, STR("$var"), &set) % 2; } TEST(xpath_variables_evaluate_node_set_fail) @@ -225,7 +223,7 @@ TEST(xpath_variables_evaluate_node_set_fail) xpath_query q(STR("$var"), &set); #ifdef PUGIXML_NO_EXCEPTIONS - CHECK_XPATH_NODESET_Q(xml_node(), q); + CHECK(q.evaluate_node_set(xml_node()).empty()); #else try { @@ -269,7 +267,7 @@ TEST(xpath_variables_long_name) xpath_variable_set set; set.set(STR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), true); - CHECK_XPATH_BOOLEAN(xml_node(), xpath_query(STR("$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &set), true); + CHECK_XPATH_BOOLEAN_VAR(xml_node(), STR("$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &set, true); } TEST_XML(xpath_variables_select, "") @@ -304,8 +302,8 @@ TEST_XML(xpath_variables_step, "") @@ -313,8 +311,8 @@ TEST_XML(xpath_variables_index, " var = STR("$"); var += name; - CHECK_XPATH_STRING(xml_node(), xpath_query(var.c_str(), &set), STR("value")); + CHECK_XPATH_STRING_VAR(xml_node(), var.c_str(), &set, STR("value")); } TEST_XML(xpath_variables_count_sum, "122334") @@ -396,6 +394,6 @@ TEST_XML(xpath_variables_count_sum, "122334