From 45dc0226d18abb281e506927a0264e20c0131edc Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Wed, 28 Oct 2009 18:39:43 +0000 Subject: tests: Added one more normalize-space test, added tests for all logical & comparison operators git-svn-id: http://pugixml.googlecode.com/svn/trunk@183 99668b35-9821-0410-8761-19e4c4f06640 --- tests/test_xpath_functions.cpp | 1 + tests/test_xpath_operators.cpp | 309 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 310 insertions(+) (limited to 'tests') diff --git a/tests/test_xpath_functions.cpp b/tests/test_xpath_functions.cpp index bc432af..d580d89 100644 --- a/tests/test_xpath_functions.cpp +++ b/tests/test_xpath_functions.cpp @@ -461,6 +461,7 @@ TEST_XML_FLAGS(xpath_string_normalize_space, " \t\r\rval1 \rval2\r\nval3\ CHECK_XPATH_STRING(c, "normalize-space('abcd \n\r')", "abcd"); CHECK_XPATH_STRING(c, "normalize-space('ab\r\n\tcd')", "ab cd"); CHECK_XPATH_STRING(c, "normalize-space('ab cd')", "ab cd"); + CHECK_XPATH_STRING(c, "normalize-space('\07')", "\07"); // normalize-space with 2 arguments CHECK_XPATH_FAIL("normalize-space(1, 2)"); diff --git a/tests/test_xpath_operators.cpp b/tests/test_xpath_operators.cpp index 41005d4..7ff2f70 100644 --- a/tests/test_xpath_operators.cpp +++ b/tests/test_xpath_operators.cpp @@ -63,3 +63,312 @@ TEST_XML(xpath_operators_arithmetic, "102abacbdabb") +{ + xml_node c; + xml_node n = doc.child("node"); + + // node set vs node set + CHECK_XPATH_BOOLEAN(c, "x = x", false); // empty node set compares as false with any other object via any comparison operator, as per XPath spec + CHECK_XPATH_BOOLEAN(c, "x != x", false); + CHECK_XPATH_BOOLEAN(n, "c1/v = c2/v", true); + CHECK_XPATH_BOOLEAN(n, "c1/v = c3/v", true); + CHECK_XPATH_BOOLEAN(n, "c2/v = c3/v", false); + CHECK_XPATH_BOOLEAN(n, "c1/v = c4/v", false); + CHECK_XPATH_BOOLEAN(n, "c1/v = x", false); + CHECK_XPATH_BOOLEAN(n, "x = c1", false); + + CHECK_XPATH_BOOLEAN(n, "c1/v != c2/v", true); + CHECK_XPATH_BOOLEAN(n, "c1/v != c3/v", true); + CHECK_XPATH_BOOLEAN(n, "c2/v != c3/v", true); + CHECK_XPATH_BOOLEAN(n, "c1/v != c4/v", true); + CHECK_XPATH_BOOLEAN(n, "c1/v != c5/v", true); // (a, b) != (a, b), since a != b, as per XPath spec (comparison operators are so not intutive) + CHECK_XPATH_BOOLEAN(n, "c3/v != c6/v", false); + CHECK_XPATH_BOOLEAN(n, "c1/v != x", false); + CHECK_XPATH_BOOLEAN(n, "x != c1/v", false); +} + +TEST_XML(xpath_operators_equality_node_set_primitive, "1-11001nan") +{ + xml_node c; + xml_node n = doc.child("node"); + + // node set vs number + CHECK_XPATH_BOOLEAN(c, "x = 1", false); + CHECK_XPATH_BOOLEAN(c, "x != 1", false); + CHECK_XPATH_BOOLEAN(c, "1 = x", false); + CHECK_XPATH_BOOLEAN(c, "1 != x", false); + + CHECK_XPATH_BOOLEAN(n, "c1/v = 1", true); + CHECK_XPATH_BOOLEAN(n, "c1/v = -1", true); + CHECK_XPATH_BOOLEAN(n, "c1/v != 1", true); + CHECK_XPATH_BOOLEAN(n, "c1/v = 5", false); + CHECK_XPATH_BOOLEAN(n, "c2/v = 1", true); + CHECK_XPATH_BOOLEAN(n, "c2/v != 1", true); + + CHECK_XPATH_BOOLEAN(n, "1 = c1/v", true); + CHECK_XPATH_BOOLEAN(n, "-1 = c1/v", true); + CHECK_XPATH_BOOLEAN(n, "1 != c1/v", true); + CHECK_XPATH_BOOLEAN(n, "5 = c1/v", false); + CHECK_XPATH_BOOLEAN(n, "1 = c2/v", true); + CHECK_XPATH_BOOLEAN(n, "1 != c2/v", true); + + // node set vs string + CHECK_XPATH_BOOLEAN(c, "x = '1'", false); + CHECK_XPATH_BOOLEAN(c, "x != '1'", false); + CHECK_XPATH_BOOLEAN(c, "'1' = x", false); + CHECK_XPATH_BOOLEAN(c, "'1' != x", false); + + CHECK_XPATH_BOOLEAN(n, "c1/v = '1'", true); + CHECK_XPATH_BOOLEAN(n, "c1/v = '-1'", true); + CHECK_XPATH_BOOLEAN(n, "c1/v != '1'", true); + CHECK_XPATH_BOOLEAN(n, "c1/v = '5'", false); + CHECK_XPATH_BOOLEAN(n, "c2/v = '1'", true); + CHECK_XPATH_BOOLEAN(n, "c2/v != '1'", true); + + CHECK_XPATH_BOOLEAN(n, "'1' = c1/v", true); + CHECK_XPATH_BOOLEAN(n, "'-1' = c1/v", true); + CHECK_XPATH_BOOLEAN(n, "'1' != c1/v", true); + CHECK_XPATH_BOOLEAN(n, "'5' = c1/v", false); + CHECK_XPATH_BOOLEAN(n, "'1' = c2/v", true); + CHECK_XPATH_BOOLEAN(n, "'1' != c2/v", true); + + // node set vs almost-numeric string just in case + CHECK_XPATH_BOOLEAN(n, "c1/v = '1.0'", false); + + // node set vs boolean - special rules! empty sets are equal to true() + CHECK_XPATH_BOOLEAN(n, "x = true()", false); + CHECK_XPATH_BOOLEAN(n, "x != true()", true); + CHECK_XPATH_BOOLEAN(n, "x = false()", true); + CHECK_XPATH_BOOLEAN(n, "c1/v = true()", true); + CHECK_XPATH_BOOLEAN(n, "c1/v != true()", false); + CHECK_XPATH_BOOLEAN(n, "c1/v = false()", false); + + CHECK_XPATH_BOOLEAN(n, "true() = x", false); + CHECK_XPATH_BOOLEAN(n, "true() != x", true); + CHECK_XPATH_BOOLEAN(n, "false() = x", true); + CHECK_XPATH_BOOLEAN(n, "true() = c1/v", true); + CHECK_XPATH_BOOLEAN(n, "true() != c1/v", false); + CHECK_XPATH_BOOLEAN(n, "false() = c1/v", false); +} + +TEST(xpath_operators_inequality_primitive) +{ + xml_node c; + + // number vs number + CHECK_XPATH_BOOLEAN(c, "1 < 2", true); + CHECK_XPATH_BOOLEAN(c, "1 <= 2", true); + CHECK_XPATH_BOOLEAN(c, "1 > 2", false); + CHECK_XPATH_BOOLEAN(c, "1 >= 2", false); + + CHECK_XPATH_BOOLEAN(c, "1 < 1", false); + CHECK_XPATH_BOOLEAN(c, "1 <= 1", true); + CHECK_XPATH_BOOLEAN(c, "1 > 1", false); + CHECK_XPATH_BOOLEAN(c, "1 >= 1", true); + + // infinity/nan + CHECK_XPATH_BOOLEAN(c, "1 div 0 <= 2 div 0", true); + CHECK_XPATH_BOOLEAN(c, "1 div 0 < 2 div 0", false); + CHECK_XPATH_BOOLEAN(c, "-1 div 0 < 2 div 0", true); + CHECK_XPATH_BOOLEAN(c, "-1 div 0 > 2 div 0", false); + + CHECK_XPATH_BOOLEAN(c, "0 div 0 < 1", false); + CHECK_XPATH_BOOLEAN(c, "0 div 0 <= 1", false); + CHECK_XPATH_BOOLEAN(c, "0 div 0 > 1", false); + CHECK_XPATH_BOOLEAN(c, "0 div 0 >= 1", false); + + // upcast to number + CHECK_XPATH_BOOLEAN(c, "2 < '2'", false); + CHECK_XPATH_BOOLEAN(c, "1 < '2'", true); + CHECK_XPATH_BOOLEAN(c, "2 <= '2'", true); + CHECK_XPATH_BOOLEAN(c, "3 <= '2'", false); + CHECK_XPATH_BOOLEAN(c, "2 > '2'", false); + CHECK_XPATH_BOOLEAN(c, "3 > '2'", true); + CHECK_XPATH_BOOLEAN(c, "2 >= '2'", true); + CHECK_XPATH_BOOLEAN(c, "3 >= '2'", true); + CHECK_XPATH_BOOLEAN(c, "1 >= true()", true); + CHECK_XPATH_BOOLEAN(c, "1 > true()", false); +} + +TEST_XML(xpath_operators_inequality_node_set_node_set, "1-1-1001nan") +{ + xml_node c; + xml_node n = doc.child("node"); + + // node set vs node set + CHECK_XPATH_BOOLEAN(c, "x < x", false); + CHECK_XPATH_BOOLEAN(c, "x > x", false); + CHECK_XPATH_BOOLEAN(c, "x <= x", false); + CHECK_XPATH_BOOLEAN(c, "x >= x", false); + + CHECK_XPATH_BOOLEAN(n, "c1/v > x", false); + CHECK_XPATH_BOOLEAN(n, "c1/v < x", false); + CHECK_XPATH_BOOLEAN(n, "c1/v >= x", false); + CHECK_XPATH_BOOLEAN(n, "c1/v <= x", false); + + CHECK_XPATH_BOOLEAN(n, "x > c1/v", false); + CHECK_XPATH_BOOLEAN(n, "x < c1/v", false); + CHECK_XPATH_BOOLEAN(n, "x >= c1/v", false); + CHECK_XPATH_BOOLEAN(n, "x <= c1/v", false); + + CHECK_XPATH_BOOLEAN(n, "c1/v > c2/v", false); + CHECK_XPATH_BOOLEAN(n, "c1/v >= c2/v", true); + CHECK_XPATH_BOOLEAN(n, "c1/v < c2/v", true); + CHECK_XPATH_BOOLEAN(n, "c1/v <= c2/v", true); +} + +TEST_XML(xpath_operators_inequality_node_set_primitive, "1-1-1001nan") +{ + xml_node c; + xml_node n = doc.child("node"); + + // node set vs number + CHECK_XPATH_BOOLEAN(c, "x < 0", false); + CHECK_XPATH_BOOLEAN(c, "x > 0", false); + CHECK_XPATH_BOOLEAN(c, "x <= 0", false); + CHECK_XPATH_BOOLEAN(c, "x >= 0", false); + + CHECK_XPATH_BOOLEAN(c, "0 < x", false); + CHECK_XPATH_BOOLEAN(c, "0 > x", false); + CHECK_XPATH_BOOLEAN(c, "0 <= x", false); + CHECK_XPATH_BOOLEAN(c, "0 >= x", false); + + CHECK_XPATH_BOOLEAN(n, "c1/v > 0", true); + CHECK_XPATH_BOOLEAN(n, "c1/v > 1", false); + CHECK_XPATH_BOOLEAN(n, "c1/v >= 0", true); + CHECK_XPATH_BOOLEAN(n, "c1/v < 0", true); + CHECK_XPATH_BOOLEAN(n, "c1/v <= 0", true); + + CHECK_XPATH_BOOLEAN(n, "0 < c1/v", true); + CHECK_XPATH_BOOLEAN(n, "1 < c1/v", false); + CHECK_XPATH_BOOLEAN(n, "0 <= c1/v", true); + CHECK_XPATH_BOOLEAN(n, "0 > c1/v", true); + CHECK_XPATH_BOOLEAN(n, "0 >= c1/v", true); + + // node set vs string + CHECK_XPATH_BOOLEAN(n, "c1/v > '0'", true); + CHECK_XPATH_BOOLEAN(n, "c1/v > '1'", false); + CHECK_XPATH_BOOLEAN(n, "c1/v >= '0'", true); + CHECK_XPATH_BOOLEAN(n, "c1/v < '0'", true); + CHECK_XPATH_BOOLEAN(n, "c1/v <= '0'", true); + + CHECK_XPATH_BOOLEAN(n, "'0' < c1/v", true); + CHECK_XPATH_BOOLEAN(n, "'1' < c1/v", false); + CHECK_XPATH_BOOLEAN(n, "'0' <= c1/v", true); + CHECK_XPATH_BOOLEAN(n, "'0' > c1/v", true); + CHECK_XPATH_BOOLEAN(n, "'0' >= c1/v", true); + + // node set vs boolean + CHECK_XPATH_BOOLEAN(n, "c1/v > false()", true); + CHECK_XPATH_BOOLEAN(n, "c1/v > true()", false); + CHECK_XPATH_BOOLEAN(n, "c1/v >= false()", true); + CHECK_XPATH_BOOLEAN(n, "c1/v < false()", true); + CHECK_XPATH_BOOLEAN(n, "c1/v <= false()", true); + + CHECK_XPATH_BOOLEAN(n, "false() < c1/v", true); + CHECK_XPATH_BOOLEAN(n, "true() < c1/v", false); + CHECK_XPATH_BOOLEAN(n, "false() <= c1/v", true); + CHECK_XPATH_BOOLEAN(n, "false() > c1/v", true); + CHECK_XPATH_BOOLEAN(n, "false() >= c1/v", true); +} + +TEST(xpath_operators_boolean_precedence) +{ + xml_node c; + + CHECK_XPATH_BOOLEAN(c, "1 = 0 or 2 = 2", true); + CHECK_XPATH_BOOLEAN(c, "1 = (0 or 2) = false()", false); + CHECK_XPATH_BOOLEAN(c, "1 < 0 or 2 > 2", false); + CHECK_XPATH_BOOLEAN(c, "2 < 1 = false()", true); + CHECK_XPATH_BOOLEAN(c, "2 < (1 = false())", false); + CHECK_XPATH_BOOLEAN(c, "3 > 2 > 1", false); + CHECK_XPATH_BOOLEAN(c, "(3 > 2) > 1", false); + CHECK_XPATH_BOOLEAN(c, "3 > (2 > 1)", true); +} -- cgit v1.2.3