tests: Supported all pugixml compilation modes

git-svn-id: http://pugixml.googlecode.com/svn/trunk@191 99668b35-9821-0410-8761-19e4c4f06640
This commit is contained in:
arseny.kapoulkine 2009-10-29 07:17:30 +00:00
parent 0640f87859
commit fc602fd375
10 changed files with 105 additions and 47 deletions

View File

@ -46,6 +46,35 @@ static void replace_memory_management()
pugi::set_memory_management_functions(custom_allocate, custom_deallocate); 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() int main()
{ {
replace_memory_management(); replace_memory_management();
@ -53,33 +82,12 @@ int main()
unsigned int total = 0; unsigned int total = 0;
unsigned int passed = 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++;
{ passed += run_test(test);
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);
}
} }
unsigned int failed = total - passed; unsigned int failed = total - passed;

View File

@ -4,7 +4,7 @@
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <sstream> #include <string>
#include "../src/pugixml.hpp" #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); 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) inline bool test_node(const pugi::xml_node& node, const char* contents, const char* indent, unsigned int flags)
{ {
std::ostringstream oss; xml_writer_string writer;
node.print(oss, indent, flags); 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) inline bool test_xpath_string(const pugi::xml_node& node, const char* query, const char* expected)
{ {
pugi::xpath_query q(query); pugi::xpath_query q(query);
@ -72,6 +83,7 @@ inline bool test_xpath_fail_compile(const char* query)
return true; return true;
} }
} }
#endif
struct test_runner 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_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) #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_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_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(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_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") #define CHECK_XPATH_FAIL(query) CHECK_TEXT(test_xpath_fail_compile(query), STR(query) " should not compile")
#endif
#endif #endif

View File

@ -1,6 +1,8 @@
#include "common.hpp" #include "common.hpp"
#include <fstream> #include <fstream>
#include <sstream>
#include <string>
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning(disable: 4996) #pragma warning(disable: 4996)
@ -13,6 +15,7 @@ TEST(document_create)
CHECK_NODE(doc, "<node />"); CHECK_NODE(doc, "<node />");
} }
#ifndef PUGIXML_NO_STL
TEST(document_load_stream) TEST(document_load_stream)
{ {
pugi::xml_document doc; pugi::xml_document doc;
@ -41,6 +44,7 @@ TEST(document_load_stream_error)
std::istringstream iss("<node/>"); std::istringstream iss("<node/>");
CHECK(doc.load(iss).status == status_out_of_memory); CHECK(doc.load(iss).status == status_out_of_memory);
} }
#endif
TEST(document_load_string) TEST(document_load_string)
{ {
@ -64,12 +68,12 @@ TEST(document_load_file_large)
CHECK(doc.load_file("tests/data/large.xml")); CHECK(doc.load_file("tests/data/large.xml"));
std::ostringstream oss; std::string str;
oss << "<node>"; str += "<node>";
for (int i = 0; i < 10000; ++i) oss << "<node />"; for (int i = 0; i < 10000; ++i) str += "<node />";
oss << "</node>"; str += "</node>";
CHECK_NODE(doc, oss.str().c_str()); CHECK_NODE(doc, str.c_str());
} }
TEST(document_load_file_error) TEST(document_load_file_error)
@ -90,32 +94,29 @@ TEST(document_load_file_error)
TEST_XML(document_save, "<node/>") TEST_XML(document_save, "<node/>")
{ {
std::ostringstream oss; xml_writer_string writer;
xml_writer_stream writer(oss);
doc.save(writer, "", pugi::format_no_declaration | pugi::format_raw); doc.save(writer, "", pugi::format_no_declaration | pugi::format_raw);
CHECK(oss.str() == "<node />"); CHECK(writer.result == "<node />");
} }
TEST_XML(document_save_bom, "<node/>") TEST_XML(document_save_bom, "<node/>")
{ {
std::ostringstream oss; xml_writer_string writer;
xml_writer_stream writer(oss);
doc.save(writer, "", pugi::format_no_declaration | pugi::format_raw | pugi::format_write_bom_utf8); 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/>") TEST_XML(document_save_declaration, "<node/>")
{ {
std::ostringstream oss; xml_writer_string writer;
xml_writer_stream writer(oss);
doc.save(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/>") TEST_XML(document_save_file, "<node/>")

View File

@ -8,11 +8,20 @@
#pragma warning(disable: 4996) #pragma warning(disable: 4996)
#endif #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) template <typename I> static I move_iter(I base, int n)
{ {
std::advance(base, n); std::advance(base, n);
return base; return base;
} }
#endif
template <typename T> static void generic_bool_ops_test(const T& obj) 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()); 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>") TEST_XML(dom_node_path, "<node><child1>text<child2/></child1></node>")
{ {
CHECK(xml_node().path() == ""); 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"); CHECK(doc.child("node").child("child1").path('\\') == "\\node\\child1");
} }
#endif
TEST_XML(dom_node_first_element_by_path, "<node><child1>text<child2/></child1></node>") 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"));
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"); 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/child2") == xml_node());
CHECK(doc.first_element_by_path("\\node\\child1", '\\') == doc.child("node").child("child1")); CHECK(doc.first_element_by_path("\\node\\child1", '\\') == doc.child("node").child("child1"));

View File

@ -7,6 +7,7 @@ inline wchar_t wchar_cast(unsigned int value)
return static_cast<wchar_t>(value); // to avoid C4310 on MSVC return static_cast<wchar_t>(value); // to avoid C4310 on MSVC
} }
#ifndef PUGIXML_NO_STL
TEST(as_utf16) TEST(as_utf16)
{ {
// valid 1-byte, 2-byte and 3-byte inputs // 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"); CHECK(as_utf8(L"\x97624 \x1003ff") == "\xf2\x97\x98\xa4 \xf4\x80\x8f\xbf");
#endif #endif
} }
#endif
TEST_XML(parse_bom_utf8, "\xef\xbb\xbf<node/>") TEST_XML(parse_bom_utf8, "\xef\xbb\xbf<node/>")
{ {

View File

@ -1,6 +1,7 @@
#include "common.hpp" #include "common.hpp"
#include <string> #include <string>
#include <sstream>
TEST_XML(write_simple, "<node attr='1'><child>text</child></node>") 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"); CHECK(writer.contents == "<node />\n");
} }
#ifndef PUGIXML_NO_STL
TEST_XML(write_print_stream, "<node/>") TEST_XML(write_print_stream, "<node/>")
{ {
std::ostringstream oss; std::ostringstream oss;
@ -79,14 +81,15 @@ TEST_XML(write_print_stream, "<node/>")
CHECK(oss.str() == "<node />\n"); CHECK(oss.str() == "<node />\n");
} }
#endif
TEST_XML(write_huge_chunk, "<node/>") TEST_XML(write_huge_chunk, "<node/>")
{ {
std::string name(10000, 'n'); std::string name(10000, 'n');
doc.child("node").set_name(name.c_str()); doc.child("node").set_name(name.c_str());
std::ostringstream oss; test_writer writer;
doc.print(oss); doc.print(writer);
CHECK(oss.str() == "<" + name + " />\n"); CHECK(writer.contents == "<" + name + " />\n");
} }

View File

@ -1,3 +1,5 @@
#ifndef PUGIXML_NO_XPATH
#include "common.hpp" #include "common.hpp"
TEST_XML(xpath_document_order, "<node><child1 attr1='value1' attr2='value2'/><child2 attr1='value1'>test</child2></node>") 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").attribute("attr1").document_order() == 7);
CHECK(doc.child("node").child("child2").first_child().document_order() == 8); CHECK(doc.child("node").child("child2").first_child().document_order() == 8);
} }
#endif

View File

@ -1,3 +1,5 @@
#ifndef PUGIXML_NO_XPATH
#include "common.hpp" #include "common.hpp"
TEST_XML(xpath_number_number, "<node>123</node>") TEST_XML(xpath_number_number, "<node>123</node>")
@ -530,3 +532,5 @@ TEST(xpath_function_arguments)
// lack of commas // lack of commas
CHECK_XPATH_FAIL("substring(1 2)"); CHECK_XPATH_FAIL("substring(1 2)");
} }
#endif

View File

@ -1,3 +1,5 @@
#ifndef PUGIXML_NO_XPATH
#include "common.hpp" #include "common.hpp"
#if defined(_MSC_VER) && _MSC_VER == 1200 #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", false);
CHECK_XPATH_BOOLEAN(c, "3 > (2 > 1)", true); CHECK_XPATH_BOOLEAN(c, "3 > (2 > 1)", true);
} }
#endif

View File

@ -1,3 +1,5 @@
#ifndef PUGIXML_NO_XPATH
#include "common.hpp" #include "common.hpp"
TEST(xpath_literal_parse) TEST(xpath_literal_parse)
@ -32,3 +34,5 @@ TEST(xpath_number_error)
CHECK_XPATH_FAIL("123.a"); CHECK_XPATH_FAIL("123.a");
CHECK_XPATH_FAIL(".123a"); CHECK_XPATH_FAIL(".123a");
} }
#endif