diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/main.cpp | 60 | ||||
-rw-r--r-- | tests/test.hpp | 22 | ||||
-rw-r--r-- | tests/test_document.cpp | 29 | ||||
-rw-r--r-- | tests/test_dom_traverse.cpp | 14 | ||||
-rw-r--r-- | tests/test_unicode.cpp | 2 | ||||
-rw-r--r-- | tests/test_write.cpp | 9 | ||||
-rw-r--r-- | tests/test_xpath.cpp | 4 | ||||
-rw-r--r-- | tests/test_xpath_functions.cpp | 4 | ||||
-rw-r--r-- | tests/test_xpath_operators.cpp | 4 | ||||
-rw-r--r-- | 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 <string.h>
#include <math.h>
#include <float.h>
-#include <sstream>
+#include <string>
#include "../src/pugixml.hpp"
@@ -18,14 +18,25 @@ template <typename Node> 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<const char*>(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 <fstream>
+#include <sstream>
+#include <string>
#ifdef _MSC_VER
#pragma warning(disable: 4996)
@@ -13,6 +15,7 @@ TEST(document_create) CHECK_NODE(doc, "<node />");
}
+#ifndef PUGIXML_NO_STL
TEST(document_load_stream)
{
pugi::xml_document doc;
@@ -41,6 +44,7 @@ TEST(document_load_stream_error) std::istringstream iss("<node/>");
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 << "<node>";
- for (int i = 0; i < 10000; ++i) oss << "<node />";
- oss << "</node>";
+ std::string str;
+ str += "<node>";
+ for (int i = 0; i < 10000; ++i) str += "<node />";
+ str += "</node>";
- 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, "<node/>")
{
- 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() == "<node />");
+ CHECK(writer.result == "<node />");
}
TEST_XML(document_save_bom, "<node/>")
{
- 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<node />");
+ CHECK(writer.result == "\xef\xbb\xbf<node />");
}
TEST_XML(document_save_declaration, "<node/>")
{
- std::ostringstream oss;
- xml_writer_stream writer(oss);
+ xml_writer_string writer;
doc.save(writer);
- CHECK(oss.str() == "<?xml version=\"1.0\"?>\n<node />\n");
+ CHECK(writer.result == "<?xml version=\"1.0\"?>\n<node />\n");
}
TEST_XML(document_save_file, "<node/>")
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 <typename I> static I move_iter(I base, int n)
+{
+ if (n > 0) while (n--) ++base;
+ else while (n++) --base;
+ return base;
+}
+#else
template <typename I> static I move_iter(I base, int n)
{
std::advance(base, n);
return base;
}
+#endif
template <typename T> static void generic_bool_ops_test(const T& obj)
{
@@ -566,6 +575,7 @@ TEST_XML(dom_node_find_node, "<node><child1/><child2/></node>") CHECK(doc.find_node(find_predicate_prefix("child3")) == xml_node());
}
+#ifndef PUGIXML_NO_STL
TEST_XML(dom_node_path, "<node><child1>text<child2/></child1></node>")
{
CHECK(xml_node().path() == "");
@@ -578,6 +588,7 @@ TEST_XML(dom_node_path, "<node><child1>text<child2/></child1></node>") CHECK(doc.child("node").child("child1").path('\\') == "\\node\\child1");
}
+#endif
TEST_XML(dom_node_first_element_by_path, "<node><child1>text<child2/></child1></node>")
{
@@ -591,7 +602,10 @@ TEST_XML(dom_node_first_element_by_path, "<node><child1>text<child2/></child1></ CHECK(doc.first_element_by_path("node") == doc.child("node"));
CHECK(doc.first_element_by_path("/node") == doc.child("node"));
+#ifndef PUGIXML_NO_STL
CHECK(doc.first_element_by_path("/node/child1/child2").path() == "/node/child1/child2");
+#endif
+
CHECK(doc.first_element_by_path("/node/child2") == xml_node());
CHECK(doc.first_element_by_path("\\node\\child1", '\\') == doc.child("node").child("child1"));
diff --git a/tests/test_unicode.cpp b/tests/test_unicode.cpp index e7adc47..ce48edb 100644 --- a/tests/test_unicode.cpp +++ b/tests/test_unicode.cpp @@ -7,6 +7,7 @@ inline wchar_t wchar_cast(unsigned int value) return static_cast<wchar_t>(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<node/>")
{
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 <string>
+#include <sstream>
TEST_XML(write_simple, "<node attr='1'><child>text</child></node>")
{
@@ -72,6 +73,7 @@ TEST_XML(write_print_writer, "<node/>") CHECK(writer.contents == "<node />\n");
}
+#ifndef PUGIXML_NO_STL
TEST_XML(write_print_stream, "<node/>")
{
std::ostringstream oss;
@@ -79,14 +81,15 @@ TEST_XML(write_print_stream, "<node/>") CHECK(oss.str() == "<node />\n");
}
+#endif
TEST_XML(write_huge_chunk, "<node/>")
{
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, "<node><child1 attr1='value1' attr2='value2'/><child2 attr1='value1'>test</child2></node>")
@@ -17,3 +19,5 @@ TEST_XML(xpath_document_order, "<node><child1 attr1='value1' attr2='value2'/><ch CHECK(doc.child("node").child("child2").attribute("attr1").document_order() == 7);
CHECK(doc.child("node").child("child2").first_child().document_order() == 8);
}
+
+#endif
diff --git a/tests/test_xpath_functions.cpp b/tests/test_xpath_functions.cpp index d580d89..ef04667 100644 --- a/tests/test_xpath_functions.cpp +++ b/tests/test_xpath_functions.cpp @@ -1,3 +1,5 @@ +#ifndef PUGIXML_NO_XPATH
+
#include "common.hpp"
TEST_XML(xpath_number_number, "<node>123</node>")
@@ -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
|