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
This commit is contained in:
arseny.kapoulkine 2010-08-29 15:49:06 +00:00
parent 6d44879c5a
commit 998a534df7
4 changed files with 69 additions and 79 deletions

View File

@ -70,65 +70,49 @@ bool test_double_nan(double value)
} }
#ifndef PUGIXML_NO_XPATH #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; const size_t capacity = 64;
pugi::char_t result[capacity]; 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); if (size <= capacity) return test_string_equal(result, expected);
std::basic_string<pugi::char_t> buffer(size, ' '); std::basic_string<pugi::char_t> 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); double absolute_error = fabs(value - expected);
const double tolerance = 1e-15f; const double tolerance = 1e-15f;
return absolute_error < tolerance || absolute_error < fabs(expected) * tolerance; return absolute_error < tolerance || absolute_error < fabs(expected) * tolerance;
} }
bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::xpath_query& query) bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::char_t* query, pugi::xpath_variable_set* variables)
{ {
return test_double_nan(query.evaluate_number(node)); pugi::xpath_query q(query, variables);
} if (!q) return false;
bool test_xpath_string(const pugi::xpath_node& node, const pugi::char_t* query, const pugi::char_t* expected) return test_double_nan(q.evaluate_number(node));
{
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)
{
pugi::xpath_query q(query);
return q && test_xpath_number_nan(node, q);
} }
bool test_xpath_fail_compile(const pugi::char_t* query, pugi::xpath_variable_set* variables) bool test_xpath_fail_compile(const pugi::char_t* query, pugi::xpath_variable_set* variables)

View File

@ -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); bool test_double_nan(double value);
#ifndef PUGIXML_NO_XPATH #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);
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);
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);
bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::xpath_query& query); 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_fail_compile(const pugi::char_t* query, pugi::xpath_variable_set* variables);
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);
struct xpath_node_set_tester 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) #define CHECK_NODE(node, expected) CHECK_NODE_EX(node, expected, PUGIXML_TEXT(""), pugi::format_raw)
#ifndef PUGIXML_NO_XPATH #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_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(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_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(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_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(node, query) CHECK_TEXT(test_xpath_number_nan(node, query), STRINGIZE(query) " does not evaluate to NaN 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_FAIL(query) CHECK_TEXT(test_xpath_fail_compile(query), STRINGIZE(query) " should not compile") #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_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 #endif
#define STR(text) PUGIXML_TEXT(text) #define STR(text) PUGIXML_TEXT(text)

View File

@ -169,10 +169,18 @@ TEST_XML(xpath_api_evaluate_attr, "<node attr='3'/>")
#ifdef PUGIXML_NO_EXCEPTIONS #ifdef PUGIXML_NO_EXCEPTIONS
TEST_XML(xpath_api_evaluate_fail, "<node attr='3'/>") TEST_XML(xpath_api_evaluate_fail, "<node attr='3'/>")
{ {
CHECK_XPATH_BOOLEAN(doc, STR(""), false); xpath_query q(STR(""));
CHECK_XPATH_NUMBER_NAN(doc, STR(""));
CHECK_XPATH_STRING(doc, STR(""), STR("")); CHECK(q.evaluate_boolean(doc) == false);
CHECK_XPATH_NODESET(doc, STR("")); 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 #endif

View File

@ -198,10 +198,10 @@ TEST_XML(xpath_variables_evaluate, "<node/>")
set.set(STR("var3"), STR("value")); set.set(STR("var3"), STR("value"));
set.set(STR("var4"), doc.select_nodes(STR("*"))); set.set(STR("var4"), doc.select_nodes(STR("*")));
CHECK_XPATH_BOOLEAN(doc, xpath_query(STR("$var1"), &set), true); CHECK_XPATH_BOOLEAN_VAR(doc, STR("$var1"), &set, true);
CHECK_XPATH_NUMBER(doc, xpath_query(STR("$var2"), &set), 0.5); CHECK_XPATH_NUMBER_VAR(doc, STR("$var2"), &set, 0.5);
CHECK_XPATH_STRING(doc, xpath_query(STR("$var3"), &set), STR("value")); CHECK_XPATH_STRING_VAR(doc, STR("$var3"), &set, STR("value"));
CHECK_XPATH_NODESET_Q(doc, xpath_query(STR("$var4"), &set)) % 2; CHECK_XPATH_NODESET_VAR(doc, STR("$var4"), &set) % 2;
} }
TEST_XML(xpath_variables_evaluate_conversion, "<node>3</node>") TEST_XML(xpath_variables_evaluate_conversion, "<node>3</node>")
@ -209,12 +209,10 @@ TEST_XML(xpath_variables_evaluate_conversion, "<node>3</node>")
xpath_variable_set set; xpath_variable_set set;
set.set(STR("var"), doc.select_nodes(STR("*"))); set.set(STR("var"), doc.select_nodes(STR("*")));
xpath_query query(STR("$var"), &set); CHECK_XPATH_BOOLEAN_VAR(doc, STR("$var"), &set, true);
CHECK_XPATH_NUMBER_VAR(doc, STR("$var"), &set, 3);
CHECK_XPATH_BOOLEAN(doc, query, true); CHECK_XPATH_STRING_VAR(doc, STR("$var"), &set, STR("3"));
CHECK_XPATH_NUMBER(doc, query, 3); CHECK_XPATH_NODESET_VAR(doc, STR("$var"), &set) % 2;
CHECK_XPATH_STRING(doc, query, STR("3"));
CHECK_XPATH_NODESET_Q(doc, query) % 2;
} }
TEST(xpath_variables_evaluate_node_set_fail) TEST(xpath_variables_evaluate_node_set_fail)
@ -225,7 +223,7 @@ TEST(xpath_variables_evaluate_node_set_fail)
xpath_query q(STR("$var"), &set); xpath_query q(STR("$var"), &set);
#ifdef PUGIXML_NO_EXCEPTIONS #ifdef PUGIXML_NO_EXCEPTIONS
CHECK_XPATH_NODESET_Q(xml_node(), q); CHECK(q.evaluate_node_set(xml_node()).empty());
#else #else
try try
{ {
@ -269,7 +267,7 @@ TEST(xpath_variables_long_name)
xpath_variable_set set; xpath_variable_set set;
set.set(STR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), true); 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, "<node attr='1'/><node attr='2'/>") TEST_XML(xpath_variables_select, "<node attr='1'/><node attr='2'/>")
@ -304,8 +302,8 @@ TEST_XML(xpath_variables_step, "<node><child/><child/><child><child/></child></n
xpath_variable_set set; xpath_variable_set set;
set.set(STR("root"), doc.select_nodes(STR("node"))); set.set(STR("root"), doc.select_nodes(STR("node")));
CHECK_XPATH_NODESET_Q(xml_node(), xpath_query(STR("$root/child"), &set)) % 3 % 4 % 5; CHECK_XPATH_NODESET_VAR(xml_node(), STR("$root/child"), &set) % 3 % 4 % 5;
CHECK_XPATH_NODESET_Q(xml_node(), xpath_query(STR("$root//child"), &set)) % 3 % 4 % 5 % 6; CHECK_XPATH_NODESET_VAR(xml_node(), STR("$root//child"), &set) % 3 % 4 % 5 % 6;
} }
TEST_XML(xpath_variables_index, "<node><child/><child/><child><child/></child></node>") TEST_XML(xpath_variables_index, "<node><child/><child/><child><child/></child></node>")
@ -313,8 +311,8 @@ TEST_XML(xpath_variables_index, "<node><child/><child/><child><child/></child></
xpath_variable_set set; xpath_variable_set set;
set.set(STR("index"), 2.0); set.set(STR("index"), 2.0);
CHECK_XPATH_NODESET_Q(doc, xpath_query(STR("node/child[$index]"), &set)) % 4; CHECK_XPATH_NODESET_VAR(doc, STR("node/child[$index]"), &set) % 4;
CHECK_XPATH_NODESET_Q(doc, xpath_query(STR("node/child[position()=$index]"), &set)) % 4; CHECK_XPATH_NODESET_VAR(doc, STR("node/child[position()=$index]"), &set) % 4;
} }
TEST(xpath_variables_qname) TEST(xpath_variables_qname)
@ -322,7 +320,7 @@ TEST(xpath_variables_qname)
xpath_variable_set set; xpath_variable_set set;
set.set(STR("foo:bar"), true); set.set(STR("foo:bar"), true);
CHECK_XPATH_BOOLEAN(xml_node(), xpath_query(STR("$foo:bar"), &set), true); CHECK_XPATH_BOOLEAN_VAR(xml_node(), STR("$foo:bar"), &set, true);
} }
TEST(xpath_variables_name_error) TEST(xpath_variables_name_error)
@ -348,7 +346,7 @@ TEST(xpath_variables_empty_string)
xpath_variable_set set; xpath_variable_set set;
set.add(STR("empty"), xpath_type_string); set.add(STR("empty"), xpath_type_string);
CHECK_XPATH_BOOLEAN(xml_node(), xpath_query(STR("$empty = substring-before('a', 'z')"), &set), true); CHECK_XPATH_BOOLEAN_VAR(xml_node(), STR("$empty = substring-before('a', 'z')"), &set, true);
} }
TEST(xpath_variables_name_underscore) TEST(xpath_variables_name_underscore)
@ -356,7 +354,7 @@ TEST(xpath_variables_name_underscore)
xpath_variable_set set; xpath_variable_set set;
set.set(STR("_foo_bar"), true); set.set(STR("_foo_bar"), true);
CHECK_XPATH_BOOLEAN(xml_node(), xpath_query(STR("$_foo_bar"), &set), true); CHECK_XPATH_BOOLEAN_VAR(xml_node(), STR("$_foo_bar"), &set, true);
} }
TEST(xpath_variables_name_case) TEST(xpath_variables_name_case)
@ -365,7 +363,7 @@ TEST(xpath_variables_name_case)
set.set(STR("i"), 5.0); set.set(STR("i"), 5.0);
set.set(STR("I"), 2.0); set.set(STR("I"), 2.0);
CHECK_XPATH_NUMBER(xml_node(), xpath_query(STR("$i div $I"), &set), 2.5); CHECK_XPATH_NUMBER_VAR(xml_node(), STR("$i div $I"), &set, 2.5);
} }
TEST(xpath_variables_name_unicode) TEST(xpath_variables_name_unicode)
@ -386,7 +384,7 @@ TEST(xpath_variables_name_unicode)
std::basic_string<char_t> var = STR("$"); std::basic_string<char_t> var = STR("$");
var += name; 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, "<node><c1>12</c1><c2>23</c2><c3>34</c3></node>") TEST_XML(xpath_variables_count_sum, "<node><c1>12</c1><c2>23</c2><c3>34</c3></node>")
@ -396,6 +394,6 @@ TEST_XML(xpath_variables_count_sum, "<node><c1>12</c1><c2>23</c2><c3>34</c3></no
set.set(STR("c3"), doc.select_nodes(STR("node/c3"))); set.set(STR("c3"), doc.select_nodes(STR("node/c3")));
set.set(STR("c"), doc.select_nodes(STR("node/*"))); set.set(STR("c"), doc.select_nodes(STR("node/*")));
CHECK_XPATH_NUMBER(xml_node(), xpath_query(STR("sum($c12) * count($c) - sum($c3)"), &set), 71); CHECK_XPATH_NUMBER_VAR(xml_node(), STR("sum($c12) * count($c) - sum($c3)"), &set, 71);
} }
#endif #endif