diff options
-rw-r--r-- | tests/test.cpp | 17 | ||||
-rw-r--r-- | tests/test.hpp | 2 | ||||
-rw-r--r-- | tests/test_xpath_variables.cpp | 191 |
3 files changed, 203 insertions, 7 deletions
diff --git a/tests/test.cpp b/tests/test.cpp index 43c6b22..9091f14 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -60,6 +60,15 @@ bool test_node(const pugi::xml_node& node, const pugi::char_t* contents, const p return writer.as_string() == contents; } +bool test_double_nan(double value) +{ +#if defined(_MSC_VER) || defined(__BORLANDC__) + return _isnan(value) != 0; +#else + return value != value; +#endif +} + #ifndef PUGIXML_NO_XPATH bool test_xpath_string(const pugi::xpath_node& node, const pugi::char_t* query, const pugi::char_t* expected) { @@ -99,13 +108,7 @@ bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::char_t* que { pugi::xpath_query q(query); - double r = q.evaluate_number(node); - -#if defined(_MSC_VER) || defined(__BORLANDC__) - return _isnan(r) != 0; -#else - return r != r; -#endif + return test_double_nan(q.evaluate_number(node)); } bool test_xpath_fail_compile(const pugi::char_t* query) diff --git a/tests/test.hpp b/tests/test.hpp index 4127697..37f78c8 100644 --- a/tests/test.hpp +++ b/tests/test.hpp @@ -35,6 +35,7 @@ template <typename Node> inline bool test_node_name_value(const Node& node, cons } bool test_node(const pugi::xml_node& node, const pugi::char_t* contents, const pugi::char_t* indent, unsigned int flags); +bool test_double_nan(double value); #ifndef PUGIXML_NO_XPATH bool test_xpath_string(const pugi::xpath_node& node, const pugi::char_t* query, const pugi::char_t* expected); @@ -116,6 +117,7 @@ struct dummy_fixture {}; #define CHECK(condition) CHECK_TEXT(condition, STRINGIZE(condition) " is false") #define CHECK_STRING(value, expected) CHECK_TEXT(test_string_equal(value, expected), STRINGIZE(value) " is not equal to " STRINGIZE(expected)) #define CHECK_DOUBLE(value, expected) CHECK_TEXT((value > expected ? value - expected : expected - value) < 1e-6, STRINGIZE(value) " is not equal to " STRINGIZE(expected)) +#define CHECK_DOUBLE_NAN(value) CHECK_TEXT(test_double_nan(value), STRINGIZE(value) " is not equal to NaN") #define CHECK_NAME_VALUE(node, name, value) CHECK_TEXT(test_node_name_value(node, name, value), STRINGIZE(node) " name/value do not match " STRINGIZE(name) " and " STRINGIZE(value)) #define CHECK_NODE_EX(node, expected, indent, flags) CHECK_TEXT(test_node(node, expected, indent, flags), STRINGIZE(node) " contents does not match " STRINGIZE(expected)) #define CHECK_NODE(node, expected) CHECK_NODE_EX(node, expected, PUGIXML_TEXT(""), pugi::format_raw) diff --git a/tests/test_xpath_variables.cpp b/tests/test_xpath_variables.cpp new file mode 100644 index 0000000..77ca53f --- /dev/null +++ b/tests/test_xpath_variables.cpp @@ -0,0 +1,191 @@ +#ifndef PUGIXML_NO_XPATH
+
+#include "common.hpp"
+
+TEST(xpath_variables_type_none)
+{
+ xpath_variable_set set;
+
+ xpath_variable* var = set.add(STR("target"), xpath_type_none);
+ CHECK(!var);
+}
+
+TEST(xpath_variables_type_boolean)
+{
+ xpath_variable_set set;
+
+ xpath_variable* var = set.add(STR("target"), xpath_type_boolean);
+ CHECK(var);
+
+ CHECK(var->type() == xpath_type_boolean);
+ CHECK_STRING(var->name(), STR("target"));
+
+ CHECK(var->get_boolean() == false);
+ CHECK_DOUBLE_NAN(var->get_number());
+ CHECK_STRING(var->get_string(), STR(""));
+ CHECK(var->get_node_set().empty());
+
+ CHECK(var->set(true));
+ CHECK(!var->set(1.0));
+ CHECK(!var->set(STR("abc")));
+ CHECK(!var->set(xpath_node_set()));
+
+ CHECK(var->get_boolean() == true);
+ CHECK_DOUBLE_NAN(var->get_number());
+ CHECK_STRING(var->get_string(), STR(""));
+ CHECK(var->get_node_set().empty());
+}
+
+TEST(xpath_variables_type_number)
+{
+ xpath_variable_set set;
+
+ xpath_variable* var = set.add(STR("target"), xpath_type_number);
+ CHECK(var);
+
+ CHECK(var->type() == xpath_type_number);
+ CHECK_STRING(var->name(), STR("target"));
+
+ CHECK(var->get_boolean() == false);
+ CHECK_DOUBLE(var->get_number(), 0);
+ CHECK_STRING(var->get_string(), STR(""));
+ CHECK(var->get_node_set().empty());
+
+ CHECK(!var->set(true));
+ CHECK(var->set(1.0));
+ CHECK(!var->set(STR("abc")));
+ CHECK(!var->set(xpath_node_set()));
+
+ CHECK(var->get_boolean() == false);
+ CHECK_DOUBLE(var->get_number(), 1);
+ CHECK_STRING(var->get_string(), STR(""));
+ CHECK(var->get_node_set().empty());
+}
+
+TEST(xpath_variables_type_string)
+{
+ xpath_variable_set set;
+
+ xpath_variable* var = set.add(STR("target"), xpath_type_string);
+ CHECK(var);
+
+ CHECK(var->type() == xpath_type_string);
+ CHECK_STRING(var->name(), STR("target"));
+
+ CHECK(var->get_boolean() == false);
+ CHECK_DOUBLE_NAN(var->get_number());
+ CHECK_STRING(var->get_string(), STR(""));
+ CHECK(var->get_node_set().empty());
+
+ CHECK(!var->set(true));
+ CHECK(!var->set(1.0));
+ CHECK(var->set(STR("abc")));
+ CHECK(!var->set(xpath_node_set()));
+
+ CHECK(var->get_boolean() == false);
+ CHECK_DOUBLE_NAN(var->get_number());
+ CHECK_STRING(var->get_string(), STR("abc"));
+ CHECK(var->get_node_set().empty());
+}
+
+TEST_XML(xpath_variables_type_node_set, "<node/>")
+{
+ xpath_variable_set set;
+
+ xpath_variable* var = set.add(STR("target"), xpath_type_node_set);
+ CHECK(var);
+
+ CHECK(var->type() == xpath_type_node_set);
+ CHECK_STRING(var->name(), STR("target"));
+
+ CHECK(var->get_boolean() == false);
+ CHECK_DOUBLE_NAN(var->get_number());
+ CHECK_STRING(var->get_string(), STR(""));
+ CHECK(var->get_node_set().empty());
+
+ CHECK(!var->set(true));
+ CHECK(!var->set(1.0));
+ CHECK(!var->set(STR("abc")));
+ CHECK(var->set(doc.select_nodes(STR("*"))));
+
+ CHECK(var->get_boolean() == false);
+ CHECK_DOUBLE_NAN(var->get_number());
+ CHECK_STRING(var->get_string(), STR(""));
+ CHECK(var->get_node_set().size() == 1 && var->get_node_set()[0] == doc.first_child());
+}
+
+TEST(xpath_variables_set_operations)
+{
+ xpath_variable_set set;
+
+ xpath_variable* v1 = set.add(STR("var1"), xpath_type_number);
+ CHECK(v1);
+
+ xpath_variable* v2 = set.add(STR("var2"), xpath_type_string);
+ CHECK(v2);
+
+ CHECK(v1 != v2);
+
+ CHECK(set.add(STR("var1"), xpath_type_number) == v1);
+ CHECK(set.add(STR("var2"), xpath_type_string) == v2);
+ CHECK(set.add(STR("var2"), xpath_type_node_set) == 0);
+
+ CHECK(set.get(STR("var1")) == v1);
+ CHECK(set.get(STR("var2")) == v2);
+ CHECK(set.get(STR("var")) == 0);
+ CHECK(set.get(STR("var11")) == 0);
+
+ CHECK(static_cast<const xpath_variable_set&>(set).get(STR("var1")) == v1);
+ CHECK(static_cast<const xpath_variable_set&>(set).get(STR("var3")) == 0);
+}
+
+TEST_XML(xpath_variables_set_operations_set, "<node/>")
+{
+ xpath_variable_set set;
+
+ xpath_variable* v1 = set.add(STR("var1"), xpath_type_number);
+ CHECK(v1);
+
+ xpath_variable* v2 = set.add(STR("var2"), xpath_type_string);
+ CHECK(v2);
+
+ CHECK(set.set(STR("var1"), 1.0));
+ CHECK_DOUBLE(v1->get_number(), 1.0);
+
+ CHECK(set.set(STR("var2"), STR("value")));
+ CHECK_STRING(v2->get_string(), STR("value"));
+
+ CHECK(!set.set(STR("var1"), true));
+
+ CHECK(set.set(STR("var3"), doc.select_nodes(STR("*"))));
+
+ xpath_variable* v3 = set.get(STR("var3"));
+
+ CHECK(v3);
+ CHECK(v3->type() == xpath_type_node_set);
+ CHECK(v3->get_node_set().size() == 1);
+}
+
+TEST(xpath_variables_set_out_of_memory)
+{
+ test_runner::_memory_fail_threshold = 1;
+
+ xpath_variable_set set;
+
+ xpath_variable* var = set.add(STR("target"), xpath_type_number);
+ CHECK(!var);
+}
+
+TEST(xpath_variable_out_of_memory)
+{
+ test_runner::_memory_fail_threshold = 64;
+
+ xpath_variable_set set;
+
+ xpath_variable* var = set.add(STR("target"), xpath_type_string);
+ CHECK(var);
+
+ CHECK(!var->set(STR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")));
+}
+
+#endif
|