From fc602fd37554f5e2d58fcee71a58e380d23d22d4 Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Thu, 29 Oct 2009 07:17:30 +0000 Subject: tests: Supported all pugixml compilation modes git-svn-id: http://pugixml.googlecode.com/svn/trunk@191 99668b35-9821-0410-8761-19e4c4f06640 --- tests/main.cpp | 60 ++++++++++++++++++++++++------------------ tests/test.hpp | 22 +++++++++++++--- tests/test_document.cpp | 29 ++++++++++---------- tests/test_dom_traverse.cpp | 14 ++++++++++ tests/test_unicode.cpp | 2 ++ tests/test_write.cpp | 9 ++++--- tests/test_xpath.cpp | 4 +++ tests/test_xpath_functions.cpp | 4 +++ tests/test_xpath_operators.cpp | 4 +++ tests/test_xpath_parse.cpp | 4 +++ 10 files changed, 105 insertions(+), 47 deletions(-) diff --git a/tests/main.cpp b/tests/main.cpp index 1886412..a1149ef 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -46,6 +46,35 @@ static void replace_memory_management() pugi::set_memory_management_functions(custom_allocate, custom_deallocate); } +static bool run_test(test_runner* test) +{ + try + { + g_memory_total_size = 0; + test_runner::_memory_fail_threshold = 0; + + test->run(); + + if (g_memory_total_size != 0) throw "Memory leaks found"; + + return true; + } + catch (const std::exception& e) + { + printf("Test %s failed: exception %s\n", test->_name, e.what()); + } + catch (const char* e) + { + printf("Test %s failed: %s\n", test->_name, e); + } + catch (...) + { + printf("Test %s failed for unknown reason\n", test->_name); + } + + return false; +} + int main() { replace_memory_management(); @@ -53,33 +82,12 @@ int main() unsigned int total = 0; unsigned int passed = 0; - for (test_runner* test = test_runner::_tests; test; test = test->_next) + test_runner* test = 0; // gcc3 "variable might be used uninitialized in this function" bug workaround + + for (test = test_runner::_tests; test; test = test->_next) { - try - { - total++; - - g_memory_total_size = 0; - test_runner::_memory_fail_threshold = 0; - - test->run(); - - if (g_memory_total_size != 0) throw "Memory leaks found"; - - passed++; - } - catch (const std::exception& e) - { - printf("Test %s failed: exception %s\n", test->_name, e.what()); - } - catch (const char* e) - { - printf("Test %s failed: %s\n", test->_name, e); - } - catch (...) - { - printf("Test %s failed for unknown reason\n", test->_name); - } + total++; + passed += run_test(test); } unsigned int failed = total - passed; diff --git a/tests/test.hpp b/tests/test.hpp index 5bbfbbd..b1e470e 100644 --- a/tests/test.hpp +++ b/tests/test.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include "../src/pugixml.hpp" @@ -18,14 +18,25 @@ template inline bool test_node_name_value(const Node& node, cons return test_string_equal(node.name(), name) && test_string_equal(node.value(), value); } +struct xml_writer_string: public pugi::xml_writer +{ + std::string result; + + virtual void write(const void* data, size_t size) + { + result += std::string(static_cast(data), size); + } +}; + inline bool test_node(const pugi::xml_node& node, const char* contents, const char* indent, unsigned int flags) { - std::ostringstream oss; - node.print(oss, indent, flags); + xml_writer_string writer; + node.print(writer, indent, flags); - return oss.str() == contents; + return writer.result == contents; } +#ifndef PUGIXML_NO_XPATH inline bool test_xpath_string(const pugi::xml_node& node, const char* query, const char* expected) { pugi::xpath_query q(query); @@ -72,6 +83,7 @@ inline bool test_xpath_fail_compile(const char* query) return true; } } +#endif struct test_runner { @@ -150,10 +162,12 @@ struct dummy_fixture {}; #define CHECK_NODE_EX(node, expected, indent, flags) CHECK_TEXT(test_node(node, expected, indent, flags), STR(node) " contents does not match " STR(expected)) #define CHECK_NODE(node, expected) CHECK_NODE_EX(node, expected, "", pugi::format_raw) +#ifndef PUGIXML_NO_XPATH #define CHECK_XPATH_STRING(node, query, expected) CHECK_TEXT(test_xpath_string(node, query, expected), STR(query) " does not evaluate to " STR(expected) " in context " STR(node)) #define CHECK_XPATH_BOOLEAN(node, query, expected) CHECK_TEXT(test_xpath_boolean(node, query, expected), STR(query) " does not evaluate to " STR(expected) " in context " STR(node)) #define CHECK_XPATH_NUMBER(node, query, expected) CHECK_TEXT(test_xpath_number(node, query, expected), STR(query) " does not evaluate to " STR(expected) " in context " STR(node)) #define CHECK_XPATH_NUMBER_NAN(node, query) CHECK_TEXT(test_xpath_number_nan(node, query), STR(query) " does not evaluate to NaN in context " STR(node)) #define CHECK_XPATH_FAIL(query) CHECK_TEXT(test_xpath_fail_compile(query), STR(query) " should not compile") +#endif #endif diff --git a/tests/test_document.cpp b/tests/test_document.cpp index ec54e95..d52635c 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -1,6 +1,8 @@ #include "common.hpp" #include +#include +#include #ifdef _MSC_VER #pragma warning(disable: 4996) @@ -13,6 +15,7 @@ TEST(document_create) CHECK_NODE(doc, ""); } +#ifndef PUGIXML_NO_STL TEST(document_load_stream) { pugi::xml_document doc; @@ -41,6 +44,7 @@ TEST(document_load_stream_error) std::istringstream iss(""); CHECK(doc.load(iss).status == status_out_of_memory); } +#endif TEST(document_load_string) { @@ -64,12 +68,12 @@ TEST(document_load_file_large) CHECK(doc.load_file("tests/data/large.xml")); - std::ostringstream oss; - oss << ""; - for (int i = 0; i < 10000; ++i) oss << ""; - oss << ""; + std::string str; + str += ""; + for (int i = 0; i < 10000; ++i) str += ""; + str += ""; - CHECK_NODE(doc, oss.str().c_str()); + CHECK_NODE(doc, str.c_str()); } TEST(document_load_file_error) @@ -90,32 +94,29 @@ TEST(document_load_file_error) TEST_XML(document_save, "") { - std::ostringstream oss; - xml_writer_stream writer(oss); + xml_writer_string writer; doc.save(writer, "", pugi::format_no_declaration | pugi::format_raw); - CHECK(oss.str() == ""); + CHECK(writer.result == ""); } TEST_XML(document_save_bom, "") { - std::ostringstream oss; - xml_writer_stream writer(oss); + xml_writer_string writer; doc.save(writer, "", pugi::format_no_declaration | pugi::format_raw | pugi::format_write_bom_utf8); - CHECK(oss.str() == "\xef\xbb\xbf"); + CHECK(writer.result == "\xef\xbb\xbf"); } TEST_XML(document_save_declaration, "") { - std::ostringstream oss; - xml_writer_stream writer(oss); + xml_writer_string writer; doc.save(writer); - CHECK(oss.str() == "\n\n"); + CHECK(writer.result == "\n\n"); } TEST_XML(document_save_file, "") diff --git a/tests/test_dom_traverse.cpp b/tests/test_dom_traverse.cpp index ab3ed35..eef3e83 100644 --- a/tests/test_dom_traverse.cpp +++ b/tests/test_dom_traverse.cpp @@ -8,11 +8,20 @@ #pragma warning(disable: 4996) #endif +#ifdef PUGIXML_NO_STL +template static I move_iter(I base, int n) +{ + if (n > 0) while (n--) ++base; + else while (n++) --base; + return base; +} +#else template static I move_iter(I base, int n) { std::advance(base, n); return base; } +#endif template static void generic_bool_ops_test(const T& obj) { @@ -566,6 +575,7 @@ TEST_XML(dom_node_find_node, "") CHECK(doc.find_node(find_predicate_prefix("child3")) == xml_node()); } +#ifndef PUGIXML_NO_STL TEST_XML(dom_node_path, "text") { CHECK(xml_node().path() == ""); @@ -578,6 +588,7 @@ TEST_XML(dom_node_path, "text") CHECK(doc.child("node").child("child1").path('\\') == "\\node\\child1"); } +#endif TEST_XML(dom_node_first_element_by_path, "text") { @@ -591,7 +602,10 @@ TEST_XML(dom_node_first_element_by_path, "text(value); // to avoid C4310 on MSVC } +#ifndef PUGIXML_NO_STL TEST(as_utf16) { // valid 1-byte, 2-byte and 3-byte inputs @@ -35,6 +36,7 @@ TEST(as_utf8) CHECK(as_utf8(L"\x97624 \x1003ff") == "\xf2\x97\x98\xa4 \xf4\x80\x8f\xbf"); #endif } +#endif TEST_XML(parse_bom_utf8, "\xef\xbb\xbf") { diff --git a/tests/test_write.cpp b/tests/test_write.cpp index 81e945c..8981a39 100644 --- a/tests/test_write.cpp +++ b/tests/test_write.cpp @@ -1,6 +1,7 @@ #include "common.hpp" #include +#include TEST_XML(write_simple, "text") { @@ -72,6 +73,7 @@ TEST_XML(write_print_writer, "") CHECK(writer.contents == "\n"); } +#ifndef PUGIXML_NO_STL TEST_XML(write_print_stream, "") { std::ostringstream oss; @@ -79,14 +81,15 @@ TEST_XML(write_print_stream, "") CHECK(oss.str() == "\n"); } +#endif TEST_XML(write_huge_chunk, "") { std::string name(10000, 'n'); doc.child("node").set_name(name.c_str()); - std::ostringstream oss; - doc.print(oss); + test_writer writer; + doc.print(writer); - CHECK(oss.str() == "<" + name + " />\n"); + CHECK(writer.contents == "<" + name + " />\n"); } diff --git a/tests/test_xpath.cpp b/tests/test_xpath.cpp index b3ace66..39814a5 100644 --- a/tests/test_xpath.cpp +++ b/tests/test_xpath.cpp @@ -1,3 +1,5 @@ +#ifndef PUGIXML_NO_XPATH + #include "common.hpp" TEST_XML(xpath_document_order, "test") @@ -17,3 +19,5 @@ TEST_XML(xpath_document_order, "123") @@ -530,3 +532,5 @@ TEST(xpath_function_arguments) // lack of commas CHECK_XPATH_FAIL("substring(1 2)"); } + +#endif diff --git a/tests/test_xpath_operators.cpp b/tests/test_xpath_operators.cpp index 08a54c4..8a83723 100644 --- a/tests/test_xpath_operators.cpp +++ b/tests/test_xpath_operators.cpp @@ -1,3 +1,5 @@ +#ifndef PUGIXML_NO_XPATH + #include "common.hpp" #if defined(_MSC_VER) && _MSC_VER == 1200 @@ -395,3 +397,5 @@ TEST(xpath_operators_boolean_precedence) CHECK_XPATH_BOOLEAN(c, "(3 > 2) > 1", false); CHECK_XPATH_BOOLEAN(c, "3 > (2 > 1)", true); } + +#endif diff --git a/tests/test_xpath_parse.cpp b/tests/test_xpath_parse.cpp index d9595c8..6d9a4e7 100644 --- a/tests/test_xpath_parse.cpp +++ b/tests/test_xpath_parse.cpp @@ -1,3 +1,5 @@ +#ifndef PUGIXML_NO_XPATH + #include "common.hpp" TEST(xpath_literal_parse) @@ -32,3 +34,5 @@ TEST(xpath_number_error) CHECK_XPATH_FAIL("123.a"); CHECK_XPATH_FAIL(".123a"); } + +#endif -- cgit v1.2.3