From 234c2f3657ec50671299a359877b058d55aa1d1d Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 19 Sep 2015 00:14:18 -0700 Subject: tests: Convert several files to Unix line endings They were still using Windows EOL by mistake --- tests/test_dom_text.cpp | 762 ++++++++++----------- tests/test_header_string_iostream.cpp | 10 +- tests/test_version.cpp | 10 +- tests/test_xpath_variables.cpp | 1164 ++++++++++++++++----------------- 4 files changed, 973 insertions(+), 973 deletions(-) (limited to 'tests') diff --git a/tests/test_dom_text.cpp b/tests/test_dom_text.cpp index 007334a..01c815c 100644 --- a/tests/test_dom_text.cpp +++ b/tests/test_dom_text.cpp @@ -1,381 +1,381 @@ -#include "common.hpp" - -#include "helpers.hpp" - -TEST_XML_FLAGS(dom_text_empty, "foo", parse_default | parse_pi) -{ - xml_node node = doc.child(STR("node")); - - CHECK(node.child(STR("a")).text()); - CHECK(node.child(STR("b")).text()); - CHECK(!node.child(STR("c")).text()); - CHECK(!node.child(STR("d")).text()); - CHECK(!xml_node().text()); - CHECK(!xml_text()); - - generic_empty_test(node.child(STR("a")).text()); -} - -TEST_XML(dom_text_bool_ops, "foo") -{ - generic_bool_ops_test(doc.child(STR("node")).text()); -} - -TEST_XML_FLAGS(dom_text_get, "foo", parse_default | parse_pi) -{ - xml_node node = doc.child(STR("node")); - - CHECK_STRING(node.child(STR("a")).text().get(), STR("foo")); - CHECK_STRING(node.child(STR("a")).first_child().text().get(), STR("foo")); - - CHECK_STRING(node.child(STR("b")).text().get(), STR("bar")); - CHECK_STRING(node.child(STR("b")).last_child().text().get(), STR("bar")); - - CHECK_STRING(node.child(STR("c")).text().get(), STR("")); - CHECK_STRING(node.child(STR("c")).first_child().text().get(), STR("")); - - CHECK_STRING(node.child(STR("d")).text().get(), STR("")); - - CHECK_STRING(xml_node().text().get(), STR("")); -} - -TEST_XML_FLAGS(dom_text_as_string, "foo", parse_default | parse_pi) -{ - xml_node node = doc.child(STR("node")); - - CHECK_STRING(node.child(STR("a")).text().as_string(), STR("foo")); - CHECK_STRING(node.child(STR("a")).first_child().text().as_string(), STR("foo")); - - CHECK_STRING(node.child(STR("b")).text().as_string(), STR("bar")); - CHECK_STRING(node.child(STR("b")).last_child().text().as_string(), STR("bar")); - - CHECK_STRING(node.child(STR("c")).text().as_string(), STR("")); - CHECK_STRING(node.child(STR("c")).first_child().text().as_string(), STR("")); - - CHECK_STRING(node.child(STR("d")).text().as_string(), STR("")); - - CHECK_STRING(xml_node().text().as_string(), STR("")); -} - -TEST_XML(dom_text_as_int, "1-1-214748364821474836470") -{ - xml_node node = doc.child(STR("node")); - - CHECK(xml_text().as_int() == 0); - CHECK(node.child(STR("text1")).text().as_int() == 1); - CHECK(node.child(STR("text2")).text().as_int() == -1); - CHECK(node.child(STR("text3")).text().as_int() == -2147483647 - 1); - CHECK(node.child(STR("text4")).text().as_int() == 2147483647); - CHECK(node.child(STR("text5")).text().as_int() == 0); -} - -TEST_XML(dom_text_as_int_hex, "07770x5ab0XFf-0x20-0x800000000x") -{ - xml_node node = doc.child(STR("node")); - - CHECK(node.child(STR("text1")).text().as_int() == 777); // no octal support! intentional - CHECK(node.child(STR("text2")).text().as_int() == 1451); - CHECK(node.child(STR("text3")).text().as_int() == 255); - CHECK(node.child(STR("text4")).text().as_int() == -32); - CHECK(node.child(STR("text5")).text().as_int() == -2147483647 - 1); - CHECK(node.child(STR("text6")).text().as_int() == 0); -} - -TEST_XML(dom_text_as_uint, "01214748364742949672950") -{ - xml_node node = doc.child(STR("node")); - - CHECK(xml_text().as_uint() == 0); - CHECK(node.child(STR("text1")).text().as_uint() == 0); - CHECK(node.child(STR("text2")).text().as_uint() == 1); - CHECK(node.child(STR("text3")).text().as_uint() == 2147483647); - CHECK(node.child(STR("text4")).text().as_uint() == 4294967295u); - CHECK(node.child(STR("text5")).text().as_uint() == 0); -} - -TEST_XML(dom_text_as_uint_hex, "07770x5ab0XFf0x200xFFFFFFFF0x") -{ - xml_node node = doc.child(STR("node")); - - CHECK(node.child(STR("text1")).text().as_uint() == 777); // no octal support! intentional - CHECK(node.child(STR("text2")).text().as_uint() == 1451); - CHECK(node.child(STR("text3")).text().as_uint() == 255); - CHECK(node.child(STR("text4")).text().as_uint() == 32); - CHECK(node.child(STR("text5")).text().as_uint() == 4294967295u); - CHECK(node.child(STR("text6")).text().as_uint() == 0); -} - -TEST_XML(dom_text_as_integer_space, " \t\n1234\n\t 0x123- 16- 0x10") -{ - xml_node node = doc.child(STR("node")); - - CHECK(node.child(STR("text1")).text().as_int() == 1234); - CHECK(node.child(STR("text2")).text().as_int() == 291); - CHECK(node.child(STR("text3")).text().as_int() == 0); - CHECK(node.child(STR("text4")).text().as_int() == 0); -} - -TEST_XML(dom_text_as_float, "010.12-5.13e-43.14159265358979323846") -{ - xml_node node = doc.child(STR("node")); - - CHECK(xml_text().as_float() == 0); - CHECK_DOUBLE(node.child(STR("text1")).text().as_float(), 0); - CHECK_DOUBLE(node.child(STR("text2")).text().as_float(), 1); - CHECK_DOUBLE(node.child(STR("text3")).text().as_float(), 0.12); - CHECK_DOUBLE(node.child(STR("text4")).text().as_float(), -5.1); - CHECK_DOUBLE(node.child(STR("text5")).text().as_float(), 3e-4); - CHECK_DOUBLE(node.child(STR("text6")).text().as_float(), 3.14159265358979323846); -} - -TEST_XML(dom_text_as_double, "010.12-5.13e-43.14159265358979323846") -{ - xml_node node = doc.child(STR("node")); - - CHECK(xml_text().as_double() == 0); - CHECK_DOUBLE(node.child(STR("text1")).text().as_double(), 0); - CHECK_DOUBLE(node.child(STR("text2")).text().as_double(), 1); - CHECK_DOUBLE(node.child(STR("text3")).text().as_double(), 0.12); - CHECK_DOUBLE(node.child(STR("text4")).text().as_double(), -5.1); - CHECK_DOUBLE(node.child(STR("text5")).text().as_double(), 3e-4); - CHECK_DOUBLE(node.child(STR("text6")).text().as_double(), 3.14159265358979323846); -} - -TEST_XML(dom_text_as_bool, "01trueTrueYesyesfalse") -{ - xml_node node = doc.child(STR("node")); - - CHECK(!xml_text().as_bool()); - CHECK(!node.child(STR("text1")).text().as_bool()); - CHECK(node.child(STR("text2")).text().as_bool()); - CHECK(node.child(STR("text3")).text().as_bool()); - CHECK(node.child(STR("text4")).text().as_bool()); - CHECK(node.child(STR("text5")).text().as_bool()); - CHECK(node.child(STR("text6")).text().as_bool()); - CHECK(!node.child(STR("text7")).text().as_bool()); -} - -#ifdef PUGIXML_HAS_LONG_LONG -TEST_XML(dom_text_as_llong, "1-1-922337203685477580892233720368547758070") -{ - xml_node node = doc.child(STR("node")); - - CHECK(xml_text().as_llong() == 0); - CHECK(node.child(STR("text1")).text().as_llong() == 1); - CHECK(node.child(STR("text2")).text().as_llong() == -1); - CHECK(node.child(STR("text3")).text().as_llong() == -9223372036854775807ll - 1); - CHECK(node.child(STR("text4")).text().as_llong() == 9223372036854775807ll); - CHECK(node.child(STR("text5")).text().as_llong() == 0); -} - -TEST_XML(dom_text_as_llong_hex, "07770x5ab0XFf-0x20-0x80000000000000000x") -{ - xml_node node = doc.child(STR("node")); - - CHECK(node.child(STR("text1")).text().as_llong() == 777); // no octal support! intentional - CHECK(node.child(STR("text2")).text().as_llong() == 1451); - CHECK(node.child(STR("text3")).text().as_llong() == 255); - CHECK(node.child(STR("text4")).text().as_llong() == -32); - CHECK(node.child(STR("text5")).text().as_llong() == -9223372036854775807ll - 1); - CHECK(node.child(STR("text6")).text().as_llong() == 0); -} - -TEST_XML(dom_text_as_ullong, "019223372036854775807184467440737095516150") -{ - xml_node node = doc.child(STR("node")); - - CHECK(xml_text().as_ullong() == 0); - CHECK(node.child(STR("text1")).text().as_ullong() == 0); - CHECK(node.child(STR("text2")).text().as_ullong() == 1); - CHECK(node.child(STR("text3")).text().as_ullong() == 9223372036854775807ll); - CHECK(node.child(STR("text4")).text().as_ullong() == 18446744073709551615ull); - CHECK(node.child(STR("text5")).text().as_ullong() == 0); -} - -TEST_XML(dom_text_as_ullong_hex, "07770x5ab0XFf0x200xFFFFFFFFFFFFFFFF0x") -{ - xml_node node = doc.child(STR("node")); - - CHECK(node.child(STR("text1")).text().as_ullong() == 777); // no octal support! intentional - CHECK(node.child(STR("text2")).text().as_ullong() == 1451); - CHECK(node.child(STR("text3")).text().as_ullong() == 255); - CHECK(node.child(STR("text4")).text().as_ullong() == 32); - CHECK(node.child(STR("text5")).text().as_ullong() == 18446744073709551615ull); - CHECK(node.child(STR("text6")).text().as_ullong() == 0); -} -#endif - -TEST_XML(dom_text_get_no_state, "") -{ - xml_node node = doc.child(STR("node")); - xml_text t = node.text(); - - CHECK(!t); - CHECK(t.get() && *t.get() == 0); - CHECK(!node.first_child()); - - node.append_child(node_pcdata); - - CHECK(t); - CHECK_STRING(t.get(), STR("")); - - node.first_child().set_value(STR("test")); - - CHECK(t); - CHECK_STRING(t.get(), STR("test")); -} - -TEST_XML(dom_text_set, "") -{ - xml_node node = doc.child(STR("node")); - xml_text t = node.text(); - - t.set(STR("")); - CHECK(node.first_child().type() == node_pcdata); - CHECK_NODE(node, STR("")); - - t.set(STR("boo")); - CHECK(node.first_child().type() == node_pcdata); - CHECK(node.first_child() == node.last_child()); - CHECK_NODE(node, STR("boo")); - - t.set(STR("foobarfoobar")); - CHECK(node.first_child().type() == node_pcdata); - CHECK(node.first_child() == node.last_child()); - CHECK_NODE(node, STR("foobarfoobar")); -} - -TEST_XML(dom_text_assign, "") -{ - xml_node node = doc.child(STR("node")); - - node.append_child(STR("text1")).text() = STR("v1"); - xml_text() = STR("v1"); - - node.append_child(STR("text2")).text() = -2147483647; - node.append_child(STR("text3")).text() = -2147483647 - 1; - xml_text() = -2147483647 - 1; - - node.append_child(STR("text4")).text() = 4294967295u; - node.append_child(STR("text5")).text() = 4294967294u; - xml_text() = 4294967295u; - - node.append_child(STR("text6")).text() = 0.5; - xml_text() = 0.5; - - node.append_child(STR("text7")).text() = 0.25f; - xml_text() = 0.25f; - - node.append_child(STR("text8")).text() = true; - xml_text() = true; - - CHECK_NODE(node, STR("v1-2147483647-2147483648429496729542949672940.50.25true")); -} - -TEST_XML(dom_text_set_value, "") -{ - xml_node node = doc.child(STR("node")); - - CHECK(node.append_child(STR("text1")).text().set(STR("v1"))); - CHECK(!xml_text().set(STR("v1"))); - - CHECK(node.append_child(STR("text2")).text().set(-2147483647)); - CHECK(node.append_child(STR("text3")).text().set(-2147483647 - 1)); - CHECK(!xml_text().set(-2147483647 - 1)); - - CHECK(node.append_child(STR("text4")).text().set(4294967295u)); - CHECK(node.append_child(STR("text5")).text().set(4294967294u)); - CHECK(!xml_text().set(4294967295u)); - - CHECK(node.append_child(STR("text6")).text().set(0.5)); - CHECK(!xml_text().set(0.5)); - - CHECK(node.append_child(STR("text7")).text().set(0.25f)); - CHECK(!xml_text().set(0.25f)); - - CHECK(node.append_child(STR("text8")).text().set(true)); - CHECK(!xml_text().set(true)); - - CHECK_NODE(node, STR("v1-2147483647-2147483648429496729542949672940.50.25true")); -} - -#ifdef PUGIXML_HAS_LONG_LONG -TEST_XML(dom_text_assign_llong, "") -{ - xml_node node = doc.child(STR("node")); - - node.append_child(STR("text1")).text() = -9223372036854775807ll; - node.append_child(STR("text2")).text() = -9223372036854775807ll - 1; - xml_text() = -9223372036854775807ll - 1; - - node.append_child(STR("text3")).text() = 18446744073709551615ull; - node.append_child(STR("text4")).text() = 18446744073709551614ull; - xml_text() = 18446744073709551615ull; - - CHECK_NODE(node, STR("-9223372036854775807-92233720368547758081844674407370955161518446744073709551614")); -} - -TEST_XML(dom_text_set_value_llong, "") -{ - xml_node node = doc.child(STR("node")); - - CHECK(node.append_child(STR("text1")).text().set(-9223372036854775807ll)); - CHECK(node.append_child(STR("text2")).text().set(-9223372036854775807ll - 1)); - CHECK(!xml_text().set(-9223372036854775807ll - 1)); - - CHECK(node.append_child(STR("text3")).text().set(18446744073709551615ull)); - CHECK(node.append_child(STR("text4")).text().set(18446744073709551614ull)); - CHECK(!xml_text().set(18446744073709551615ull)); - - CHECK_NODE(node, STR("-9223372036854775807-92233720368547758081844674407370955161518446744073709551614")); -} -#endif - -TEST_XML(dom_text_middle, "notthisonetext") -{ - xml_node node = doc.child(STR("node")); - xml_text t = node.text(); - - CHECK_STRING(t.get(), STR("text")); - t.set(STR("notext")); - - CHECK_NODE(node, STR("notthisonenotext")); - CHECK(node.remove_child(t.data())); - - CHECK(!t); - CHECK_NODE(node, STR("notthisone")); - - t.set(STR("yestext")); - - CHECK(t); - CHECK_NODE(node, STR("notthisoneyestext")); - CHECK(t.data() == node.last_child()); -} - -TEST_XML_FLAGS(dom_text_data, "foo", parse_default | parse_pi) -{ - xml_node node = doc.child(STR("node")); - - CHECK(node.child(STR("a")).text().data() == node.child(STR("a")).first_child()); - CHECK(node.child(STR("b")).text().data() == node.child(STR("b")).first_child()); - CHECK(!node.child(STR("c")).text().data()); - CHECK(!node.child(STR("d")).text().data()); - CHECK(!xml_text().data()); -} - -TEST(dom_text_defaults) -{ - xml_text text; - - CHECK_STRING(text.as_string(STR("foo")), STR("foo")); - CHECK(text.as_int(42) == 42); - CHECK(text.as_uint(42) == 42); - CHECK(text.as_double(42) == 42); - CHECK(text.as_float(42) == 42); - CHECK(text.as_bool(true) == true); - -#ifdef PUGIXML_HAS_LONG_LONG - CHECK(text.as_llong(42) == 42); - CHECK(text.as_ullong(42) == 42); -#endif -} +#include "common.hpp" + +#include "helpers.hpp" + +TEST_XML_FLAGS(dom_text_empty, "foo", parse_default | parse_pi) +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.child(STR("a")).text()); + CHECK(node.child(STR("b")).text()); + CHECK(!node.child(STR("c")).text()); + CHECK(!node.child(STR("d")).text()); + CHECK(!xml_node().text()); + CHECK(!xml_text()); + + generic_empty_test(node.child(STR("a")).text()); +} + +TEST_XML(dom_text_bool_ops, "foo") +{ + generic_bool_ops_test(doc.child(STR("node")).text()); +} + +TEST_XML_FLAGS(dom_text_get, "foo", parse_default | parse_pi) +{ + xml_node node = doc.child(STR("node")); + + CHECK_STRING(node.child(STR("a")).text().get(), STR("foo")); + CHECK_STRING(node.child(STR("a")).first_child().text().get(), STR("foo")); + + CHECK_STRING(node.child(STR("b")).text().get(), STR("bar")); + CHECK_STRING(node.child(STR("b")).last_child().text().get(), STR("bar")); + + CHECK_STRING(node.child(STR("c")).text().get(), STR("")); + CHECK_STRING(node.child(STR("c")).first_child().text().get(), STR("")); + + CHECK_STRING(node.child(STR("d")).text().get(), STR("")); + + CHECK_STRING(xml_node().text().get(), STR("")); +} + +TEST_XML_FLAGS(dom_text_as_string, "foo", parse_default | parse_pi) +{ + xml_node node = doc.child(STR("node")); + + CHECK_STRING(node.child(STR("a")).text().as_string(), STR("foo")); + CHECK_STRING(node.child(STR("a")).first_child().text().as_string(), STR("foo")); + + CHECK_STRING(node.child(STR("b")).text().as_string(), STR("bar")); + CHECK_STRING(node.child(STR("b")).last_child().text().as_string(), STR("bar")); + + CHECK_STRING(node.child(STR("c")).text().as_string(), STR("")); + CHECK_STRING(node.child(STR("c")).first_child().text().as_string(), STR("")); + + CHECK_STRING(node.child(STR("d")).text().as_string(), STR("")); + + CHECK_STRING(xml_node().text().as_string(), STR("")); +} + +TEST_XML(dom_text_as_int, "1-1-214748364821474836470") +{ + xml_node node = doc.child(STR("node")); + + CHECK(xml_text().as_int() == 0); + CHECK(node.child(STR("text1")).text().as_int() == 1); + CHECK(node.child(STR("text2")).text().as_int() == -1); + CHECK(node.child(STR("text3")).text().as_int() == -2147483647 - 1); + CHECK(node.child(STR("text4")).text().as_int() == 2147483647); + CHECK(node.child(STR("text5")).text().as_int() == 0); +} + +TEST_XML(dom_text_as_int_hex, "07770x5ab0XFf-0x20-0x800000000x") +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.child(STR("text1")).text().as_int() == 777); // no octal support! intentional + CHECK(node.child(STR("text2")).text().as_int() == 1451); + CHECK(node.child(STR("text3")).text().as_int() == 255); + CHECK(node.child(STR("text4")).text().as_int() == -32); + CHECK(node.child(STR("text5")).text().as_int() == -2147483647 - 1); + CHECK(node.child(STR("text6")).text().as_int() == 0); +} + +TEST_XML(dom_text_as_uint, "01214748364742949672950") +{ + xml_node node = doc.child(STR("node")); + + CHECK(xml_text().as_uint() == 0); + CHECK(node.child(STR("text1")).text().as_uint() == 0); + CHECK(node.child(STR("text2")).text().as_uint() == 1); + CHECK(node.child(STR("text3")).text().as_uint() == 2147483647); + CHECK(node.child(STR("text4")).text().as_uint() == 4294967295u); + CHECK(node.child(STR("text5")).text().as_uint() == 0); +} + +TEST_XML(dom_text_as_uint_hex, "07770x5ab0XFf0x200xFFFFFFFF0x") +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.child(STR("text1")).text().as_uint() == 777); // no octal support! intentional + CHECK(node.child(STR("text2")).text().as_uint() == 1451); + CHECK(node.child(STR("text3")).text().as_uint() == 255); + CHECK(node.child(STR("text4")).text().as_uint() == 32); + CHECK(node.child(STR("text5")).text().as_uint() == 4294967295u); + CHECK(node.child(STR("text6")).text().as_uint() == 0); +} + +TEST_XML(dom_text_as_integer_space, " \t\n1234\n\t 0x123- 16- 0x10") +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.child(STR("text1")).text().as_int() == 1234); + CHECK(node.child(STR("text2")).text().as_int() == 291); + CHECK(node.child(STR("text3")).text().as_int() == 0); + CHECK(node.child(STR("text4")).text().as_int() == 0); +} + +TEST_XML(dom_text_as_float, "010.12-5.13e-43.14159265358979323846") +{ + xml_node node = doc.child(STR("node")); + + CHECK(xml_text().as_float() == 0); + CHECK_DOUBLE(node.child(STR("text1")).text().as_float(), 0); + CHECK_DOUBLE(node.child(STR("text2")).text().as_float(), 1); + CHECK_DOUBLE(node.child(STR("text3")).text().as_float(), 0.12); + CHECK_DOUBLE(node.child(STR("text4")).text().as_float(), -5.1); + CHECK_DOUBLE(node.child(STR("text5")).text().as_float(), 3e-4); + CHECK_DOUBLE(node.child(STR("text6")).text().as_float(), 3.14159265358979323846); +} + +TEST_XML(dom_text_as_double, "010.12-5.13e-43.14159265358979323846") +{ + xml_node node = doc.child(STR("node")); + + CHECK(xml_text().as_double() == 0); + CHECK_DOUBLE(node.child(STR("text1")).text().as_double(), 0); + CHECK_DOUBLE(node.child(STR("text2")).text().as_double(), 1); + CHECK_DOUBLE(node.child(STR("text3")).text().as_double(), 0.12); + CHECK_DOUBLE(node.child(STR("text4")).text().as_double(), -5.1); + CHECK_DOUBLE(node.child(STR("text5")).text().as_double(), 3e-4); + CHECK_DOUBLE(node.child(STR("text6")).text().as_double(), 3.14159265358979323846); +} + +TEST_XML(dom_text_as_bool, "01trueTrueYesyesfalse") +{ + xml_node node = doc.child(STR("node")); + + CHECK(!xml_text().as_bool()); + CHECK(!node.child(STR("text1")).text().as_bool()); + CHECK(node.child(STR("text2")).text().as_bool()); + CHECK(node.child(STR("text3")).text().as_bool()); + CHECK(node.child(STR("text4")).text().as_bool()); + CHECK(node.child(STR("text5")).text().as_bool()); + CHECK(node.child(STR("text6")).text().as_bool()); + CHECK(!node.child(STR("text7")).text().as_bool()); +} + +#ifdef PUGIXML_HAS_LONG_LONG +TEST_XML(dom_text_as_llong, "1-1-922337203685477580892233720368547758070") +{ + xml_node node = doc.child(STR("node")); + + CHECK(xml_text().as_llong() == 0); + CHECK(node.child(STR("text1")).text().as_llong() == 1); + CHECK(node.child(STR("text2")).text().as_llong() == -1); + CHECK(node.child(STR("text3")).text().as_llong() == -9223372036854775807ll - 1); + CHECK(node.child(STR("text4")).text().as_llong() == 9223372036854775807ll); + CHECK(node.child(STR("text5")).text().as_llong() == 0); +} + +TEST_XML(dom_text_as_llong_hex, "07770x5ab0XFf-0x20-0x80000000000000000x") +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.child(STR("text1")).text().as_llong() == 777); // no octal support! intentional + CHECK(node.child(STR("text2")).text().as_llong() == 1451); + CHECK(node.child(STR("text3")).text().as_llong() == 255); + CHECK(node.child(STR("text4")).text().as_llong() == -32); + CHECK(node.child(STR("text5")).text().as_llong() == -9223372036854775807ll - 1); + CHECK(node.child(STR("text6")).text().as_llong() == 0); +} + +TEST_XML(dom_text_as_ullong, "019223372036854775807184467440737095516150") +{ + xml_node node = doc.child(STR("node")); + + CHECK(xml_text().as_ullong() == 0); + CHECK(node.child(STR("text1")).text().as_ullong() == 0); + CHECK(node.child(STR("text2")).text().as_ullong() == 1); + CHECK(node.child(STR("text3")).text().as_ullong() == 9223372036854775807ll); + CHECK(node.child(STR("text4")).text().as_ullong() == 18446744073709551615ull); + CHECK(node.child(STR("text5")).text().as_ullong() == 0); +} + +TEST_XML(dom_text_as_ullong_hex, "07770x5ab0XFf0x200xFFFFFFFFFFFFFFFF0x") +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.child(STR("text1")).text().as_ullong() == 777); // no octal support! intentional + CHECK(node.child(STR("text2")).text().as_ullong() == 1451); + CHECK(node.child(STR("text3")).text().as_ullong() == 255); + CHECK(node.child(STR("text4")).text().as_ullong() == 32); + CHECK(node.child(STR("text5")).text().as_ullong() == 18446744073709551615ull); + CHECK(node.child(STR("text6")).text().as_ullong() == 0); +} +#endif + +TEST_XML(dom_text_get_no_state, "") +{ + xml_node node = doc.child(STR("node")); + xml_text t = node.text(); + + CHECK(!t); + CHECK(t.get() && *t.get() == 0); + CHECK(!node.first_child()); + + node.append_child(node_pcdata); + + CHECK(t); + CHECK_STRING(t.get(), STR("")); + + node.first_child().set_value(STR("test")); + + CHECK(t); + CHECK_STRING(t.get(), STR("test")); +} + +TEST_XML(dom_text_set, "") +{ + xml_node node = doc.child(STR("node")); + xml_text t = node.text(); + + t.set(STR("")); + CHECK(node.first_child().type() == node_pcdata); + CHECK_NODE(node, STR("")); + + t.set(STR("boo")); + CHECK(node.first_child().type() == node_pcdata); + CHECK(node.first_child() == node.last_child()); + CHECK_NODE(node, STR("boo")); + + t.set(STR("foobarfoobar")); + CHECK(node.first_child().type() == node_pcdata); + CHECK(node.first_child() == node.last_child()); + CHECK_NODE(node, STR("foobarfoobar")); +} + +TEST_XML(dom_text_assign, "") +{ + xml_node node = doc.child(STR("node")); + + node.append_child(STR("text1")).text() = STR("v1"); + xml_text() = STR("v1"); + + node.append_child(STR("text2")).text() = -2147483647; + node.append_child(STR("text3")).text() = -2147483647 - 1; + xml_text() = -2147483647 - 1; + + node.append_child(STR("text4")).text() = 4294967295u; + node.append_child(STR("text5")).text() = 4294967294u; + xml_text() = 4294967295u; + + node.append_child(STR("text6")).text() = 0.5; + xml_text() = 0.5; + + node.append_child(STR("text7")).text() = 0.25f; + xml_text() = 0.25f; + + node.append_child(STR("text8")).text() = true; + xml_text() = true; + + CHECK_NODE(node, STR("v1-2147483647-2147483648429496729542949672940.50.25true")); +} + +TEST_XML(dom_text_set_value, "") +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.append_child(STR("text1")).text().set(STR("v1"))); + CHECK(!xml_text().set(STR("v1"))); + + CHECK(node.append_child(STR("text2")).text().set(-2147483647)); + CHECK(node.append_child(STR("text3")).text().set(-2147483647 - 1)); + CHECK(!xml_text().set(-2147483647 - 1)); + + CHECK(node.append_child(STR("text4")).text().set(4294967295u)); + CHECK(node.append_child(STR("text5")).text().set(4294967294u)); + CHECK(!xml_text().set(4294967295u)); + + CHECK(node.append_child(STR("text6")).text().set(0.5)); + CHECK(!xml_text().set(0.5)); + + CHECK(node.append_child(STR("text7")).text().set(0.25f)); + CHECK(!xml_text().set(0.25f)); + + CHECK(node.append_child(STR("text8")).text().set(true)); + CHECK(!xml_text().set(true)); + + CHECK_NODE(node, STR("v1-2147483647-2147483648429496729542949672940.50.25true")); +} + +#ifdef PUGIXML_HAS_LONG_LONG +TEST_XML(dom_text_assign_llong, "") +{ + xml_node node = doc.child(STR("node")); + + node.append_child(STR("text1")).text() = -9223372036854775807ll; + node.append_child(STR("text2")).text() = -9223372036854775807ll - 1; + xml_text() = -9223372036854775807ll - 1; + + node.append_child(STR("text3")).text() = 18446744073709551615ull; + node.append_child(STR("text4")).text() = 18446744073709551614ull; + xml_text() = 18446744073709551615ull; + + CHECK_NODE(node, STR("-9223372036854775807-92233720368547758081844674407370955161518446744073709551614")); +} + +TEST_XML(dom_text_set_value_llong, "") +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.append_child(STR("text1")).text().set(-9223372036854775807ll)); + CHECK(node.append_child(STR("text2")).text().set(-9223372036854775807ll - 1)); + CHECK(!xml_text().set(-9223372036854775807ll - 1)); + + CHECK(node.append_child(STR("text3")).text().set(18446744073709551615ull)); + CHECK(node.append_child(STR("text4")).text().set(18446744073709551614ull)); + CHECK(!xml_text().set(18446744073709551615ull)); + + CHECK_NODE(node, STR("-9223372036854775807-92233720368547758081844674407370955161518446744073709551614")); +} +#endif + +TEST_XML(dom_text_middle, "notthisonetext") +{ + xml_node node = doc.child(STR("node")); + xml_text t = node.text(); + + CHECK_STRING(t.get(), STR("text")); + t.set(STR("notext")); + + CHECK_NODE(node, STR("notthisonenotext")); + CHECK(node.remove_child(t.data())); + + CHECK(!t); + CHECK_NODE(node, STR("notthisone")); + + t.set(STR("yestext")); + + CHECK(t); + CHECK_NODE(node, STR("notthisoneyestext")); + CHECK(t.data() == node.last_child()); +} + +TEST_XML_FLAGS(dom_text_data, "foo", parse_default | parse_pi) +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.child(STR("a")).text().data() == node.child(STR("a")).first_child()); + CHECK(node.child(STR("b")).text().data() == node.child(STR("b")).first_child()); + CHECK(!node.child(STR("c")).text().data()); + CHECK(!node.child(STR("d")).text().data()); + CHECK(!xml_text().data()); +} + +TEST(dom_text_defaults) +{ + xml_text text; + + CHECK_STRING(text.as_string(STR("foo")), STR("foo")); + CHECK(text.as_int(42) == 42); + CHECK(text.as_uint(42) == 42); + CHECK(text.as_double(42) == 42); + CHECK(text.as_float(42) == 42); + CHECK(text.as_bool(true) == true); + +#ifdef PUGIXML_HAS_LONG_LONG + CHECK(text.as_llong(42) == 42); + CHECK(text.as_ullong(42) == 42); +#endif +} diff --git a/tests/test_header_string_iostream.cpp b/tests/test_header_string_iostream.cpp index 1105626..3210080 100644 --- a/tests/test_header_string_iostream.cpp +++ b/tests/test_header_string_iostream.cpp @@ -1,5 +1,5 @@ -// Tests compatibility with string/iostream -#include -#include "../src/pugixml.hpp" -#include -#include +// Tests compatibility with string/iostream +#include +#include "../src/pugixml.hpp" +#include +#include diff --git a/tests/test_version.cpp b/tests/test_version.cpp index cf64efc..38ec7bc 100644 --- a/tests/test_version.cpp +++ b/tests/test_version.cpp @@ -1,5 +1,5 @@ -#include "../src/pugixml.hpp" - -#if PUGIXML_VERSION != 160 -#error Unexpected pugixml version -#endif +#include "../src/pugixml.hpp" + +#if PUGIXML_VERSION != 160 +#error Unexpected pugixml version +#endif diff --git a/tests/test_xpath_variables.cpp b/tests/test_xpath_variables.cpp index ab436cd..fec1386 100644 --- a/tests/test_xpath_variables.cpp +++ b/tests/test_xpath_variables.cpp @@ -1,582 +1,582 @@ -#ifndef PUGIXML_NO_XPATH - -#include "common.hpp" - -#include - -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()); - - CHECK(var->set(STR("abcdef"))); - CHECK_STRING(var->get_string(), STR("abcdef")); -} - -TEST_XML(xpath_variables_type_node_set, "") -{ - 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(set).get(STR("var1")) == v1); - CHECK(static_cast(set).get(STR("var3")) == 0); -} - -TEST_XML(xpath_variables_set_operations_set, "") -{ - 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 = 0; - CHECK_ALLOC_FAIL(var = set.add(STR("target"), xpath_type_number)); - CHECK(!var); -} - -TEST(xpath_variables_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_ALLOC_FAIL(CHECK(!var->set(STR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")))); -} - -TEST_XML(xpath_variables_evaluate, "") -{ - xpath_variable_set set; - set.set(STR("var1"), true); - set.set(STR("var2"), 0.5); - set.set(STR("var3"), STR("value")); - set.set(STR("var4"), doc.select_nodes(STR("*"))); - - 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") -{ - xpath_variable_set set; - set.set(STR("var"), doc.select_nodes(STR("*"))); - - 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) -{ - xpath_variable_set set; - set.set(STR("var"), false); - - xpath_query q(STR("$var"), &set); - -#ifdef PUGIXML_NO_EXCEPTIONS - CHECK(q.evaluate_node_set(xml_node()).empty()); -#else - try - { - q.evaluate_node_set(xml_node()); - - CHECK_FORCE_FAIL("Expected exception"); - } - catch (const xpath_exception&) - { - } -#endif -} - -TEST(xpath_variables_multiple_documents) -{ - xml_document doc; - doc.append_child().set_name(STR("node")); - - xml_document doc1; - doc1.append_child().set_name(STR("node")); - - xml_document doc2; - doc2.append_child().set_name(STR("node")); - - xpath_variable_set set; - set.set(STR("var1"), doc1.select_nodes(STR("*"))); - set.set(STR("var2"), doc2.select_nodes(STR("*"))); - - xpath_node_set ns = doc.select_nodes(STR("$var1 | $var2 | node"), &set); - ns.sort(); - - CHECK(ns.size() == 3); - CHECK(ns[0] != ns[1] && ns[0] != ns[2]); - - xml_node n0 = doc.child(STR("node")), n1 = doc1.child(STR("node")), n2 = doc2.child(STR("node")); - - CHECK(n0 == ns[0].node() || n0 == ns[1].node() || n0 == ns[2].node()); - CHECK(n1 == ns[0].node() || n1 == ns[1].node() || n1 == ns[2].node()); - CHECK(n2 == ns[0].node() || n2 == ns[1].node() || n2 == ns[2].node()); -} - -TEST(xpath_variables_long_name) -{ - xpath_variable_set set; - set.set(STR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), true); - - CHECK_XPATH_BOOLEAN_VAR(xml_node(), STR("$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &set, true); -} - -TEST(xpath_variables_long_name_out_of_memory) -{ - xpath_variable_set set; - set.set(STR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), true); - - test_runner::_memory_fail_threshold = 4096 + 64 + 52 * sizeof(char_t); - - CHECK_ALLOC_FAIL(CHECK(!xpath_query(STR("$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &set))); -} - -TEST_XML(xpath_variables_select, "") -{ - xpath_variable_set set; - set.set(STR("one"), 1.0); - - xpath_node_set ns = doc.select_nodes(STR("node[@attr=$one+1]"), &set); - CHECK(ns.size() == 1 && ns[0].node() == doc.last_child()); - - xpath_node n = doc.select_node(STR("node[@attr=$one+1]"), &set); - CHECK(n == ns[0]); -} - -TEST(xpath_variables_empty_name) -{ - xpath_variable_set set; - CHECK(!set.add(STR(""), xpath_type_number)); -} - -TEST_XML(xpath_variables_inside_filter, "") -{ - xpath_variable_set set; - set.set(STR("one"), 1.0); - - xpath_node_set ns = doc.select_nodes(STR("(node[@key = $one])[@value = $one]"), &set); - CHECK(ns.size() == 1 && ns[0].node() == doc.last_child()); -} - -TEST_XML(xpath_variables_step, "") -{ - xpath_variable_set set; - set.set(STR("root"), doc.select_nodes(STR("node"))); - - CHECK_XPATH_NODESET_VAR(xml_node(), STR("$root/child"), &set) % 3 % 4 % 5; - CHECK_XPATH_NODESET_VAR(xml_node(), STR("$root//child"), &set) % 3 % 4 % 5 % 6; -} - -TEST_XML(xpath_variables_index, "") -{ - xpath_variable_set set; - set.set(STR("index"), 2.0); - - CHECK_XPATH_NODESET_VAR(doc, STR("node/child[$index]"), &set) % 4; - CHECK_XPATH_NODESET_VAR(doc, STR("node/child[position()=$index]"), &set) % 4; -} - -TEST(xpath_variables_qname) -{ - xpath_variable_set set; - set.set(STR("foo:bar"), true); - - CHECK_XPATH_BOOLEAN_VAR(xml_node(), STR("$foo:bar"), &set, true); -} - -TEST(xpath_variables_name_error) -{ - xpath_variable_set set; - set.set(STR("foo:"), true); - set.set(STR(":bar"), true); - set.set(STR("foo:*"), true); - set.set(STR("foo"), true); - set.set(STR("3"), true); - - CHECK_XPATH_FAIL_VAR(STR("$foo:"), &set); - CHECK_XPATH_FAIL_VAR(STR("$:bar"), &set); - CHECK_XPATH_FAIL_VAR(STR("$foo:*"), &set); - CHECK_XPATH_FAIL_VAR(STR("$foo:bar:baz"), &set); - CHECK_XPATH_FAIL_VAR(STR("$ foo"), &set); - - CHECK_XPATH_FAIL_VAR(STR("$3"), &set); -} - -TEST(xpath_variables_empty_string) -{ - xpath_variable_set set; - set.add(STR("empty"), xpath_type_string); - - CHECK_XPATH_BOOLEAN_VAR(xml_node(), STR("$empty = substring-before('a', 'z')"), &set, true); -} - -TEST(xpath_variables_name_underscore) -{ - xpath_variable_set set; - set.set(STR("_foo_bar"), true); - - CHECK_XPATH_BOOLEAN_VAR(xml_node(), STR("$_foo_bar"), &set, true); -} - -TEST(xpath_variables_name_case) -{ - xpath_variable_set set; - set.set(STR("i"), 5.0); - set.set(STR("I"), 2.0); - - CHECK_XPATH_NUMBER_VAR(xml_node(), STR("$i div $I"), &set, 2.5); -} - -TEST(xpath_variables_name_unicode) -{ -#ifdef PUGIXML_WCHAR_MODE - #ifdef U_LITERALS - const char_t* name = L"\u0400\u203D"; - #else - const char_t* name = L"\x0400\x203D"; - #endif -#else - const char_t* name = "\xd0\x80\xe2\x80\xbd"; -#endif - - xpath_variable_set set; - set.set(name, STR("value")); - - std::basic_string var = STR("$"); - var += name; - - CHECK_XPATH_STRING_VAR(xml_node(), var.c_str(), &set, STR("value")); -} - -TEST_XML(xpath_variables_count_sum, "122334") -{ - xpath_variable_set set; - set.set(STR("c12"), doc.select_nodes(STR("node/c1 | node/c2"))); - set.set(STR("c3"), doc.select_nodes(STR("node/c3"))); - set.set(STR("c"), doc.select_nodes(STR("node/*"))); - - CHECK_XPATH_NUMBER_VAR(xml_node(), STR("sum($c12) * count($c) - sum($c3)"), &set, 71); -} - -TEST_XML(xpath_variables_copy, "") -{ - xpath_variable_set set1; - set1.set(STR("a"), true); - set1.set(STR("b"), 2.0); - set1.set(STR("c"), STR("string")); - set1.set(STR("d"), doc.select_nodes(STR("//*"))); - - CHECK_XPATH_STRING_VAR(xml_node(), STR("substring($c, count($d[$a]) + $b)"), &set1, STR("ring")); - - xpath_variable_set set2 = set1; - - CHECK_XPATH_STRING_VAR(xml_node(), STR("substring($c, count($d[$a]) + $b)"), &set2, STR("ring")); - - xpath_variable_set set3; - - CHECK(!set3.get(STR("a"))); - - set3 = set1; - - CHECK_XPATH_STRING_VAR(xml_node(), STR("substring($c, count($d[$a]) + $b)"), &set2, STR("ring")); - - set3 = set3; - - CHECK_XPATH_STRING_VAR(xml_node(), STR("substring($c, count($d[$a]) + $b)"), &set2, STR("ring")); - - set3 = xpath_variable_set(); - - CHECK(!set3.get(STR("a"))); -} - -TEST_XML(xpath_variables_copy_out_of_memory, "") -{ - xpath_variable_set set1; - set1.set(STR("a"), true); - set1.set(STR("b"), 2.0); - set1.set(STR("c"), STR("string")); - set1.set(STR("d"), doc.select_nodes(STR("//*"))); - - xpath_variable_set set2 = set1; - - test_runner::_memory_fail_threshold = 32768 + 75 * sizeof(void*); - - CHECK_ALLOC_FAIL(xpath_variable_set set3 = set1); - - xpath_variable_set set4; - - CHECK_ALLOC_FAIL(set4 = set1); - CHECK(!set4.get(STR("a")) && !set4.get(STR("b")) && !set4.get(STR("c")) && !set4.get(STR("d"))); - - CHECK_ALLOC_FAIL(set2 = set1); - - CHECK(set2.get(STR("a")) && set2.get(STR("b")) && set2.get(STR("c")) && set2.get(STR("d"))); - - CHECK(set2.get(STR("a"))->get_boolean() == true); - CHECK(set2.get(STR("b"))->get_number() == 2.0); - CHECK_STRING(set2.get(STR("c"))->get_string(), STR("string")); - CHECK(set2.get(STR("d"))->get_node_set().size() == 2); -} - -#if __cplusplus >= 201103 -TEST_XML(xpath_variables_move, "") -{ - xpath_variable_set set; - set.set(STR("a"), true); - set.set(STR("b"), 2.0); - set.set(STR("c"), STR("string")); - set.set(STR("d"), doc.select_nodes(STR("//*"))); - - xpath_variable_set copy = set; - copy.set(STR("e"), 42.0); - - test_runner::_memory_fail_threshold = 1; - - xpath_variable_set move1 = std::move(set); - - CHECK(!set.get(STR("a")) && !set.get(STR("b")) && !set.get(STR("c")) && !set.get(STR("d"))); - CHECK(move1.get(STR("a")) && move1.get(STR("b")) && move1.get(STR("c")) && move1.get(STR("d"))); - - CHECK(move1.get(STR("a"))->get_boolean() == true); - CHECK(move1.get(STR("b"))->get_number() == 2.0); - CHECK_STRING(move1.get(STR("c"))->get_string(), STR("string")); - CHECK(move1.get(STR("d"))->get_node_set().size() == 1); - - xpath_variable_set move2; - move2 = std::move(move1); - - CHECK(!move1.get(STR("a")) && !move1.get(STR("b")) && !move1.get(STR("c")) && !move1.get(STR("d"))); - CHECK(move2.get(STR("a")) && move2.get(STR("b")) && move2.get(STR("c")) && move2.get(STR("d"))); - - CHECK(copy.get(STR("e"))); - - copy = std::move(move2); - - CHECK(!move2.get(STR("a")) && !move2.get(STR("b")) && !move2.get(STR("c")) && !move2.get(STR("d"))); - CHECK(copy.get(STR("a")) && copy.get(STR("b")) && copy.get(STR("c")) && copy.get(STR("d"))); - CHECK(!copy.get(STR("e"))); - - CHECK(copy.get(STR("a"))->get_boolean() == true); - CHECK(copy.get(STR("b"))->get_number() == 2.0); - CHECK_STRING(copy.get(STR("c"))->get_string(), STR("string")); - CHECK(copy.get(STR("d"))->get_node_set().size() == 1); -} -#endif - -TEST(xpath_variables_copy_big) -{ - xpath_variable_set set; - - for (int i = 0; i < 100; ++i) - { - char_t name[4]; - name[0] = 'a'; - name[1] = char_t('0' + i / 10); - name[2] = char_t('0' + i % 10); - name[3] = 0; - - set.set(name, double(i)); - } - - xpath_variable_set copy = set; - - for (int j = 0; j < 100; ++j) - { - char_t name[4]; - name[0] = 'a'; - name[1] = char_t('0' + j / 10); - name[2] = char_t('0' + j % 10); - name[3] = 0; - - CHECK(copy.get(name) && copy.get(name)->get_number() == j); - } -} - -TEST(xpath_variables_copy_big_out_of_memory) -{ - xpath_variable_set set; - - for (int i = 0; i < 100; ++i) - { - char_t name[4]; - name[0] = 'a'; - name[1] = char_t('0' + i / 10); - name[2] = char_t('0' + i % 10); - name[3] = 0; - - set.set(name, double(i)); - } - - test_runner::_memory_fail_threshold = 1; - - xpath_variable_set copy; - CHECK_ALLOC_FAIL(copy = set); - - for (int j = 0; j < 100; ++j) - { - char_t name[4]; - name[0] = 'a'; - name[1] = char_t('0' + j / 10); - name[2] = char_t('0' + j % 10); - name[3] = 0; - - CHECK(!copy.get(name)); - } -} -#endif +#ifndef PUGIXML_NO_XPATH + +#include "common.hpp" + +#include + +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()); + + CHECK(var->set(STR("abcdef"))); + CHECK_STRING(var->get_string(), STR("abcdef")); +} + +TEST_XML(xpath_variables_type_node_set, "") +{ + 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(set).get(STR("var1")) == v1); + CHECK(static_cast(set).get(STR("var3")) == 0); +} + +TEST_XML(xpath_variables_set_operations_set, "") +{ + 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 = 0; + CHECK_ALLOC_FAIL(var = set.add(STR("target"), xpath_type_number)); + CHECK(!var); +} + +TEST(xpath_variables_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_ALLOC_FAIL(CHECK(!var->set(STR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")))); +} + +TEST_XML(xpath_variables_evaluate, "") +{ + xpath_variable_set set; + set.set(STR("var1"), true); + set.set(STR("var2"), 0.5); + set.set(STR("var3"), STR("value")); + set.set(STR("var4"), doc.select_nodes(STR("*"))); + + 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") +{ + xpath_variable_set set; + set.set(STR("var"), doc.select_nodes(STR("*"))); + + 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) +{ + xpath_variable_set set; + set.set(STR("var"), false); + + xpath_query q(STR("$var"), &set); + +#ifdef PUGIXML_NO_EXCEPTIONS + CHECK(q.evaluate_node_set(xml_node()).empty()); +#else + try + { + q.evaluate_node_set(xml_node()); + + CHECK_FORCE_FAIL("Expected exception"); + } + catch (const xpath_exception&) + { + } +#endif +} + +TEST(xpath_variables_multiple_documents) +{ + xml_document doc; + doc.append_child().set_name(STR("node")); + + xml_document doc1; + doc1.append_child().set_name(STR("node")); + + xml_document doc2; + doc2.append_child().set_name(STR("node")); + + xpath_variable_set set; + set.set(STR("var1"), doc1.select_nodes(STR("*"))); + set.set(STR("var2"), doc2.select_nodes(STR("*"))); + + xpath_node_set ns = doc.select_nodes(STR("$var1 | $var2 | node"), &set); + ns.sort(); + + CHECK(ns.size() == 3); + CHECK(ns[0] != ns[1] && ns[0] != ns[2]); + + xml_node n0 = doc.child(STR("node")), n1 = doc1.child(STR("node")), n2 = doc2.child(STR("node")); + + CHECK(n0 == ns[0].node() || n0 == ns[1].node() || n0 == ns[2].node()); + CHECK(n1 == ns[0].node() || n1 == ns[1].node() || n1 == ns[2].node()); + CHECK(n2 == ns[0].node() || n2 == ns[1].node() || n2 == ns[2].node()); +} + +TEST(xpath_variables_long_name) +{ + xpath_variable_set set; + set.set(STR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), true); + + CHECK_XPATH_BOOLEAN_VAR(xml_node(), STR("$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &set, true); +} + +TEST(xpath_variables_long_name_out_of_memory) +{ + xpath_variable_set set; + set.set(STR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), true); + + test_runner::_memory_fail_threshold = 4096 + 64 + 52 * sizeof(char_t); + + CHECK_ALLOC_FAIL(CHECK(!xpath_query(STR("$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &set))); +} + +TEST_XML(xpath_variables_select, "") +{ + xpath_variable_set set; + set.set(STR("one"), 1.0); + + xpath_node_set ns = doc.select_nodes(STR("node[@attr=$one+1]"), &set); + CHECK(ns.size() == 1 && ns[0].node() == doc.last_child()); + + xpath_node n = doc.select_node(STR("node[@attr=$one+1]"), &set); + CHECK(n == ns[0]); +} + +TEST(xpath_variables_empty_name) +{ + xpath_variable_set set; + CHECK(!set.add(STR(""), xpath_type_number)); +} + +TEST_XML(xpath_variables_inside_filter, "") +{ + xpath_variable_set set; + set.set(STR("one"), 1.0); + + xpath_node_set ns = doc.select_nodes(STR("(node[@key = $one])[@value = $one]"), &set); + CHECK(ns.size() == 1 && ns[0].node() == doc.last_child()); +} + +TEST_XML(xpath_variables_step, "") +{ + xpath_variable_set set; + set.set(STR("root"), doc.select_nodes(STR("node"))); + + CHECK_XPATH_NODESET_VAR(xml_node(), STR("$root/child"), &set) % 3 % 4 % 5; + CHECK_XPATH_NODESET_VAR(xml_node(), STR("$root//child"), &set) % 3 % 4 % 5 % 6; +} + +TEST_XML(xpath_variables_index, "") +{ + xpath_variable_set set; + set.set(STR("index"), 2.0); + + CHECK_XPATH_NODESET_VAR(doc, STR("node/child[$index]"), &set) % 4; + CHECK_XPATH_NODESET_VAR(doc, STR("node/child[position()=$index]"), &set) % 4; +} + +TEST(xpath_variables_qname) +{ + xpath_variable_set set; + set.set(STR("foo:bar"), true); + + CHECK_XPATH_BOOLEAN_VAR(xml_node(), STR("$foo:bar"), &set, true); +} + +TEST(xpath_variables_name_error) +{ + xpath_variable_set set; + set.set(STR("foo:"), true); + set.set(STR(":bar"), true); + set.set(STR("foo:*"), true); + set.set(STR("foo"), true); + set.set(STR("3"), true); + + CHECK_XPATH_FAIL_VAR(STR("$foo:"), &set); + CHECK_XPATH_FAIL_VAR(STR("$:bar"), &set); + CHECK_XPATH_FAIL_VAR(STR("$foo:*"), &set); + CHECK_XPATH_FAIL_VAR(STR("$foo:bar:baz"), &set); + CHECK_XPATH_FAIL_VAR(STR("$ foo"), &set); + + CHECK_XPATH_FAIL_VAR(STR("$3"), &set); +} + +TEST(xpath_variables_empty_string) +{ + xpath_variable_set set; + set.add(STR("empty"), xpath_type_string); + + CHECK_XPATH_BOOLEAN_VAR(xml_node(), STR("$empty = substring-before('a', 'z')"), &set, true); +} + +TEST(xpath_variables_name_underscore) +{ + xpath_variable_set set; + set.set(STR("_foo_bar"), true); + + CHECK_XPATH_BOOLEAN_VAR(xml_node(), STR("$_foo_bar"), &set, true); +} + +TEST(xpath_variables_name_case) +{ + xpath_variable_set set; + set.set(STR("i"), 5.0); + set.set(STR("I"), 2.0); + + CHECK_XPATH_NUMBER_VAR(xml_node(), STR("$i div $I"), &set, 2.5); +} + +TEST(xpath_variables_name_unicode) +{ +#ifdef PUGIXML_WCHAR_MODE + #ifdef U_LITERALS + const char_t* name = L"\u0400\u203D"; + #else + const char_t* name = L"\x0400\x203D"; + #endif +#else + const char_t* name = "\xd0\x80\xe2\x80\xbd"; +#endif + + xpath_variable_set set; + set.set(name, STR("value")); + + std::basic_string var = STR("$"); + var += name; + + CHECK_XPATH_STRING_VAR(xml_node(), var.c_str(), &set, STR("value")); +} + +TEST_XML(xpath_variables_count_sum, "122334") +{ + xpath_variable_set set; + set.set(STR("c12"), doc.select_nodes(STR("node/c1 | node/c2"))); + set.set(STR("c3"), doc.select_nodes(STR("node/c3"))); + set.set(STR("c"), doc.select_nodes(STR("node/*"))); + + CHECK_XPATH_NUMBER_VAR(xml_node(), STR("sum($c12) * count($c) - sum($c3)"), &set, 71); +} + +TEST_XML(xpath_variables_copy, "") +{ + xpath_variable_set set1; + set1.set(STR("a"), true); + set1.set(STR("b"), 2.0); + set1.set(STR("c"), STR("string")); + set1.set(STR("d"), doc.select_nodes(STR("//*"))); + + CHECK_XPATH_STRING_VAR(xml_node(), STR("substring($c, count($d[$a]) + $b)"), &set1, STR("ring")); + + xpath_variable_set set2 = set1; + + CHECK_XPATH_STRING_VAR(xml_node(), STR("substring($c, count($d[$a]) + $b)"), &set2, STR("ring")); + + xpath_variable_set set3; + + CHECK(!set3.get(STR("a"))); + + set3 = set1; + + CHECK_XPATH_STRING_VAR(xml_node(), STR("substring($c, count($d[$a]) + $b)"), &set2, STR("ring")); + + set3 = set3; + + CHECK_XPATH_STRING_VAR(xml_node(), STR("substring($c, count($d[$a]) + $b)"), &set2, STR("ring")); + + set3 = xpath_variable_set(); + + CHECK(!set3.get(STR("a"))); +} + +TEST_XML(xpath_variables_copy_out_of_memory, "") +{ + xpath_variable_set set1; + set1.set(STR("a"), true); + set1.set(STR("b"), 2.0); + set1.set(STR("c"), STR("string")); + set1.set(STR("d"), doc.select_nodes(STR("//*"))); + + xpath_variable_set set2 = set1; + + test_runner::_memory_fail_threshold = 32768 + 75 * sizeof(void*); + + CHECK_ALLOC_FAIL(xpath_variable_set set3 = set1); + + xpath_variable_set set4; + + CHECK_ALLOC_FAIL(set4 = set1); + CHECK(!set4.get(STR("a")) && !set4.get(STR("b")) && !set4.get(STR("c")) && !set4.get(STR("d"))); + + CHECK_ALLOC_FAIL(set2 = set1); + + CHECK(set2.get(STR("a")) && set2.get(STR("b")) && set2.get(STR("c")) && set2.get(STR("d"))); + + CHECK(set2.get(STR("a"))->get_boolean() == true); + CHECK(set2.get(STR("b"))->get_number() == 2.0); + CHECK_STRING(set2.get(STR("c"))->get_string(), STR("string")); + CHECK(set2.get(STR("d"))->get_node_set().size() == 2); +} + +#if __cplusplus >= 201103 +TEST_XML(xpath_variables_move, "") +{ + xpath_variable_set set; + set.set(STR("a"), true); + set.set(STR("b"), 2.0); + set.set(STR("c"), STR("string")); + set.set(STR("d"), doc.select_nodes(STR("//*"))); + + xpath_variable_set copy = set; + copy.set(STR("e"), 42.0); + + test_runner::_memory_fail_threshold = 1; + + xpath_variable_set move1 = std::move(set); + + CHECK(!set.get(STR("a")) && !set.get(STR("b")) && !set.get(STR("c")) && !set.get(STR("d"))); + CHECK(move1.get(STR("a")) && move1.get(STR("b")) && move1.get(STR("c")) && move1.get(STR("d"))); + + CHECK(move1.get(STR("a"))->get_boolean() == true); + CHECK(move1.get(STR("b"))->get_number() == 2.0); + CHECK_STRING(move1.get(STR("c"))->get_string(), STR("string")); + CHECK(move1.get(STR("d"))->get_node_set().size() == 1); + + xpath_variable_set move2; + move2 = std::move(move1); + + CHECK(!move1.get(STR("a")) && !move1.get(STR("b")) && !move1.get(STR("c")) && !move1.get(STR("d"))); + CHECK(move2.get(STR("a")) && move2.get(STR("b")) && move2.get(STR("c")) && move2.get(STR("d"))); + + CHECK(copy.get(STR("e"))); + + copy = std::move(move2); + + CHECK(!move2.get(STR("a")) && !move2.get(STR("b")) && !move2.get(STR("c")) && !move2.get(STR("d"))); + CHECK(copy.get(STR("a")) && copy.get(STR("b")) && copy.get(STR("c")) && copy.get(STR("d"))); + CHECK(!copy.get(STR("e"))); + + CHECK(copy.get(STR("a"))->get_boolean() == true); + CHECK(copy.get(STR("b"))->get_number() == 2.0); + CHECK_STRING(copy.get(STR("c"))->get_string(), STR("string")); + CHECK(copy.get(STR("d"))->get_node_set().size() == 1); +} +#endif + +TEST(xpath_variables_copy_big) +{ + xpath_variable_set set; + + for (int i = 0; i < 100; ++i) + { + char_t name[4]; + name[0] = 'a'; + name[1] = char_t('0' + i / 10); + name[2] = char_t('0' + i % 10); + name[3] = 0; + + set.set(name, double(i)); + } + + xpath_variable_set copy = set; + + for (int j = 0; j < 100; ++j) + { + char_t name[4]; + name[0] = 'a'; + name[1] = char_t('0' + j / 10); + name[2] = char_t('0' + j % 10); + name[3] = 0; + + CHECK(copy.get(name) && copy.get(name)->get_number() == j); + } +} + +TEST(xpath_variables_copy_big_out_of_memory) +{ + xpath_variable_set set; + + for (int i = 0; i < 100; ++i) + { + char_t name[4]; + name[0] = 'a'; + name[1] = char_t('0' + i / 10); + name[2] = char_t('0' + i % 10); + name[3] = 0; + + set.set(name, double(i)); + } + + test_runner::_memory_fail_threshold = 1; + + xpath_variable_set copy; + CHECK_ALLOC_FAIL(copy = set); + + for (int j = 0; j < 100; ++j) + { + char_t name[4]; + name[0] = 'a'; + name[1] = char_t('0' + j / 10); + name[2] = char_t('0' + j % 10); + name[3] = 0; + + CHECK(!copy.get(name)); + } +} +#endif -- cgit v1.2.3