summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/allocator.cpp24
-rw-r--r--tests/archive.pl3
-rw-r--r--tests/data/truncation.xml16
-rw-r--r--tests/test_document.cpp6
-rw-r--r--tests/test_dom_modify.cpp66
-rw-r--r--tests/test_header_only.cpp3
-rw-r--r--tests/test_version.cpp2
-rw-r--r--tests/test_write.cpp55
-rw-r--r--tests/writer_string.cpp6
9 files changed, 137 insertions, 44 deletions
diff --git a/tests/allocator.cpp b/tests/allocator.cpp
index 74bbf10..8ca0963 100644
--- a/tests/allocator.cpp
+++ b/tests/allocator.cpp
@@ -23,11 +23,11 @@
namespace
{
- const size_t PAGE_SIZE = 4096;
+ const size_t page_size = 4096;
size_t align_to_page(size_t value)
{
- return (value + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
+ return (value + page_size - 1) & ~(page_size - 1);
}
void* allocate_page_aligned(size_t size)
@@ -36,7 +36,7 @@ namespace
// We can't use malloc because of occasional problems with CW on CRT termination
static HANDLE heap = HeapCreate(0, 0, 0);
- void* result = HeapAlloc(heap, 0, size + PAGE_SIZE);
+ void* result = HeapAlloc(heap, 0, size + page_size);
return reinterpret_cast<void*>(align_to_page(reinterpret_cast<size_t>(result)));
}
@@ -45,13 +45,13 @@ namespace
{
size_t aligned_size = align_to_page(size);
- void* ptr = allocate_page_aligned(aligned_size + PAGE_SIZE);
+ void* ptr = allocate_page_aligned(aligned_size + page_size);
if (!ptr) return 0;
char* end = static_cast<char*>(ptr) + aligned_size;
DWORD old_flags;
- VirtualProtect(end, PAGE_SIZE, PAGE_NOACCESS, &old_flags);
+ VirtualProtect(end, page_size, PAGE_NOACCESS, &old_flags);
return end - size;
}
@@ -63,7 +63,7 @@ namespace
void* rptr = static_cast<char*>(ptr) + size - aligned_size;
DWORD old_flags;
- VirtualProtect(rptr, aligned_size + PAGE_SIZE, PAGE_NOACCESS, &old_flags);
+ VirtualProtect(rptr, aligned_size + page_size, PAGE_NOACCESS, &old_flags);
}
}
#elif defined(__APPLE__) || defined(__linux__)
@@ -71,28 +71,28 @@ namespace
namespace
{
- const size_t PAGE_SIZE = 4096;
+ const size_t page_size = 4096;
size_t align_to_page(size_t value)
{
- return (value + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
+ return (value + page_size - 1) & ~(page_size - 1);
}
void* allocate_page_aligned(size_t size)
{
- return mmap(0, size + PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
+ return mmap(0, size + page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
}
void* allocate(size_t size)
{
size_t aligned_size = align_to_page(size);
- void* ptr = allocate_page_aligned(aligned_size + PAGE_SIZE);
+ void* ptr = allocate_page_aligned(aligned_size + page_size);
if (!ptr) return 0;
char* end = static_cast<char*>(ptr) + aligned_size;
- int res = mprotect(end, PAGE_SIZE, PROT_NONE);
+ int res = mprotect(end, page_size, PROT_NONE);
assert(res == 0);
(void)!res;
@@ -105,7 +105,7 @@ namespace
void* rptr = static_cast<char*>(ptr) + size - aligned_size;
- int res = mprotect(rptr, aligned_size + PAGE_SIZE, PROT_NONE);
+ int res = mprotect(rptr, aligned_size + page_size, PROT_NONE);
assert(res == 0);
(void)!res;
}
diff --git a/tests/archive.pl b/tests/archive.pl
index 0a03b23..76484f7 100644
--- a/tests/archive.pl
+++ b/tests/archive.pl
@@ -2,11 +2,12 @@
use Archive::Tar;
use Archive::Zip;
+use File::Basename;
my $target = shift @ARGV;
my @sources = @ARGV;
-my $basedir = ($target =~ /^(.*)(\.zip|\.tar.gz|\.tgz)$/) ? "$1/" : '';
+my $basedir = basename($target, ('.zip', '.tar.gz', '.tgz')) . '/';
my $zip = $target =~ /\.zip$/;
my $arch = $zip ? Archive::Zip->new : Archive::Tar->new;
diff --git a/tests/data/truncation.xml b/tests/data/truncation.xml
index 9cdbe13..1b0e9a8 100644
--- a/tests/data/truncation.xml
+++ b/tests/data/truncation.xml
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
-<!DOCTYPE _++ SYSTEM "weekly-utf-8.dtd">
-<!-- _++'___-_< -->
+<!DOCTYPE 週報 SYSTEM "weekly-utf-8.dtd">
+<!-- 週報サンプル -->
<mesh name="mesh_root">
<!-- here is a mesh node -->
some text
@@ -8,12 +8,12 @@
some more text
<node attr1="value1" attr2="value2" />
<node attr1="value2">
- <+%- __--="name" >__="value">-__%___-_-</+%->
+ <汉语 名字="name" 价值="value">世界有很多语言𤭢</汉语>
<innernode/>
</node>
- <___>
- <_>++"</_>
- <__>太__</__>
- </___>
+ <氏名>
+ <氏>山田</氏>
+ <名>太郎</名>
+ </氏名>
<?include somedata?>
-</mesh>
+</mesh> \ No newline at end of file
diff --git a/tests/test_document.cpp b/tests/test_document.cpp
index 49428f2..09d89d7 100644
--- a/tests/test_document.cpp
+++ b/tests/test_document.cpp
@@ -1010,7 +1010,7 @@ TEST(document_progressive_truncation)
char* buffer = new char[original_size];
- for (size_t i = 1; i < original_size; ++i)
+ for (size_t i = 1; i <= original_size; ++i)
{
char* truncated_data = buffer + original_size - i;
@@ -1022,7 +1022,7 @@ TEST(document_progressive_truncation)
bool result = doc.load_buffer_inplace(truncated_data, i);
// only eof is parseable
- CHECK((i >= 3325) ? result : !result);
+ CHECK((i == original_size) ? result : !result);
}
// fragment mode
@@ -1033,7 +1033,7 @@ TEST(document_progressive_truncation)
bool result = doc.load_buffer_inplace(truncated_data, i, parse_default | parse_fragment);
// some truncate locations are parseable - those that come after declaration, declaration + doctype, declaration + doctype + comment and eof
- CHECK(((i - 21) < 3 || (i - 66) < 3 || (i - 95) < 3 || i >= 3325) ? result : !result);
+ CHECK(((i - 21) < 3 || (i - 66) < 3 || (i - 95) < 3 || i == original_size) ? result : !result);
}
}
diff --git a/tests/test_dom_modify.cpp b/tests/test_dom_modify.cpp
index 1feed21..44b13e3 100644
--- a/tests/test_dom_modify.cpp
+++ b/tests/test_dom_modify.cpp
@@ -4,6 +4,11 @@
#include <string>
#include <math.h>
+#include <string.h>
+
+#ifdef __BORLANDC__
+using std::ldexpf;
+#endif
TEST_XML(dom_attr_assign, "<node/>")
{
@@ -101,15 +106,28 @@ TEST_XML(dom_attr_set_value_llong, "<node/>")
}
#endif
-TEST_XML(dom_attr_assign_large_number, "<node attr1='' attr2='' />")
+TEST_XML(dom_attr_assign_large_number_float, "<node attr='' />")
{
xml_node node = doc.child(STR("node"));
- node.attribute(STR("attr1")) = std::numeric_limits<float>::max();
- node.attribute(STR("attr2")) = std::numeric_limits<double>::max();
+ node.attribute(STR("attr")) = std::numeric_limits<float>::max();
- CHECK(test_node(node, STR("<node attr1=\"3.40282347e+038\" attr2=\"1.7976931348623157e+308\" />"), STR(""), pugi::format_raw) ||
- test_node(node, STR("<node attr1=\"3.40282347e+38\" attr2=\"1.7976931348623157e+308\" />"), STR(""), pugi::format_raw));
+ CHECK(test_node(node, STR("<node attr=\"3.40282347e+038\" />"), STR(""), pugi::format_raw) ||
+ test_node(node, STR("<node attr=\"3.40282347e+38\" />"), STR(""), pugi::format_raw));
+}
+
+TEST_XML(dom_attr_assign_large_number_double, "<node attr='' />")
+{
+ xml_node node = doc.child(STR("node"));
+
+ node.attribute(STR("attr")) = std::numeric_limits<double>::max();
+
+ // Borland C does not print double values with enough precision
+#ifdef __BORLANDC__
+ CHECK_NODE(node, STR("<node attr=\"1.7976931348623156e+308\" />"));
+#else
+ CHECK_NODE(node, STR("<node attr=\"1.7976931348623157e+308\" />"));
+#endif
}
TEST_XML(dom_node_set_name, "<node>text</node>")
@@ -1452,6 +1470,17 @@ TEST(dom_node_copy_declaration_empty_name)
CHECK_STRING(decl2.name(), STR(""));
}
+template <typename T> bool fp_equal(T lhs, T rhs)
+{
+ // Several compilers compare float/double values on x87 stack without proper rounding
+ // This causes roundtrip tests to fail, although they correctly preserve the data.
+#if (defined(_MSC_VER) && _MSC_VER < 1400) || defined(__MWERKS__)
+ return memcmp(&lhs, &rhs, sizeof(T)) == 0;
+#else
+ return lhs == rhs;
+#endif
+}
+
TEST(dom_fp_roundtrip_min_max)
{
xml_document doc;
@@ -1459,16 +1488,16 @@ TEST(dom_fp_roundtrip_min_max)
xml_attribute attr = node.append_attribute(STR("attr"));
node.text().set(std::numeric_limits<float>::min());
- CHECK(node.text().as_float() == std::numeric_limits<float>::min());
+ CHECK(fp_equal(node.text().as_float(), std::numeric_limits<float>::min()));
attr.set_value(std::numeric_limits<float>::max());
- CHECK(attr.as_float() == std::numeric_limits<float>::max());
+ CHECK(fp_equal(attr.as_float(), std::numeric_limits<float>::max()));
attr.set_value(std::numeric_limits<double>::min());
- CHECK(attr.as_double() == std::numeric_limits<double>::min());
+ CHECK(fp_equal(attr.as_double(), std::numeric_limits<double>::min()));
node.text().set(std::numeric_limits<double>::max());
- CHECK(node.text().as_double() == std::numeric_limits<double>::max());
+ CHECK(fp_equal(node.text().as_double(), std::numeric_limits<double>::max()));
}
const double fp_roundtrip_base[] =
@@ -1492,11 +1521,13 @@ TEST(dom_fp_roundtrip_float)
float value = ldexpf(static_cast<float>(fp_roundtrip_base[i]), e);
doc.text().set(value);
- CHECK(doc.text().as_float() == value);
+ CHECK(fp_equal(doc.text().as_float(), value));
}
}
}
+// Borland C does not print double values with enough precision
+#ifndef __BORLANDC__
TEST(dom_fp_roundtrip_double)
{
xml_document doc;
@@ -1505,10 +1536,23 @@ TEST(dom_fp_roundtrip_double)
{
for (size_t i = 0; i < sizeof(fp_roundtrip_base) / sizeof(fp_roundtrip_base[0]); ++i)
{
+ #if (defined(_MSC_VER) && _MSC_VER < 1400) || defined(__MWERKS__)
+ // Not all runtime libraries guarantee roundtripping for denormals
+ if (e == -1021 && fp_roundtrip_base[i] < 0.5)
+ continue;
+ #endif
+
+ #ifdef __DMC__
+ // Digital Mars C does not roundtrip on exactly one combination
+ if (e == -12 && i == 1)
+ continue;
+ #endif
+
double value = ldexp(fp_roundtrip_base[i], e);
doc.text().set(value);
- CHECK(doc.text().as_double() == value);
+ CHECK(fp_equal(doc.text().as_double(), value));
}
}
}
+#endif
diff --git a/tests/test_header_only.cpp b/tests/test_header_only.cpp
index f1990dd..17cafca 100644
--- a/tests/test_header_only.cpp
+++ b/tests/test_header_only.cpp
@@ -12,5 +12,8 @@ TEST(header_only)
xml_document doc;
CHECK(doc.load_string(STR("<node/>")));
CHECK_STRING(doc.first_child().name(), STR("node"));
+
+#ifndef PUGIXML_NO_XPATH
CHECK(doc.first_child() == doc.select_node(STR("//*")).node());
+#endif
}
diff --git a/tests/test_version.cpp b/tests/test_version.cpp
index 24036fc..cf64efc 100644
--- a/tests/test_version.cpp
+++ b/tests/test_version.cpp
@@ -1,5 +1,5 @@
#include "../src/pugixml.hpp"
-#if PUGIXML_VERSION != 150
+#if PUGIXML_VERSION != 160
#error Unexpected pugixml version
#endif
diff --git a/tests/test_write.cpp b/tests/test_write.cpp
index 59cdb3e..ad6c409 100644
--- a/tests/test_write.cpp
+++ b/tests/test_write.cpp
@@ -22,19 +22,19 @@ TEST_XML(write_indent, "<node attr='1'><child><sub>text</sub></child></node>")
TEST_XML(write_pcdata, "<node attr='1'><child><sub/>text</child></node>")
{
- CHECK_NODE_EX(doc, STR("<node attr=\"1\">\n\t<child>\n\t\t<sub />\n\t\ttext\n\t</child>\n</node>\n"), STR("\t"), format_indent);
+ CHECK_NODE_EX(doc, STR("<node attr=\"1\">\n\t<child>\n\t\t<sub />text</child>\n</node>\n"), STR("\t"), format_indent);
}
TEST_XML_FLAGS(write_cdata, "<![CDATA[value]]>", parse_cdata | parse_fragment)
{
CHECK_NODE(doc, STR("<![CDATA[value]]>"));
- CHECK_NODE_EX(doc, STR("<![CDATA[value]]>\n"), STR(""), 0);
+ CHECK_NODE_EX(doc, STR("<![CDATA[value]]>"), STR(""), 0);
}
TEST_XML_FLAGS(write_cdata_empty, "<![CDATA[]]>", parse_cdata | parse_fragment)
{
CHECK_NODE(doc, STR("<![CDATA[]]>"));
- CHECK_NODE_EX(doc, STR("<![CDATA[]]>\n"), STR(""), 0);
+ CHECK_NODE_EX(doc, STR("<![CDATA[]]>"), STR(""), 0);
}
TEST_XML_FLAGS(write_cdata_escape, "<![CDATA[value]]>", parse_cdata | parse_fragment)
@@ -123,7 +123,7 @@ TEST(write_pi_invalid)
node.set_name(STR("test"));
node.set_value(STR("?"));
- CHECK_NODE(doc, STR("<?test ?" "?>"));
+ CHECK_NODE(doc, STR("<?test ?") STR("?>"));
node.set_value(STR("?>"));
@@ -527,5 +527,50 @@ TEST(write_pcdata_null)
doc.first_child().append_child(node_pcdata);
- CHECK_NODE_EX(doc, STR("<node>\n\t\n\t\n</node>\n"), STR("\t"), format_indent);
+ CHECK_NODE_EX(doc, STR("<node></node>\n"), STR("\t"), format_indent);
+}
+
+TEST(write_pcdata_whitespace_fixedpoint)
+{
+ const char_t* data = STR("<node> test <child>\n <sub/>\n </child>\n</node>");
+
+ static const unsigned int flags_parse[] =
+ {
+ 0,
+ parse_ws_pcdata,
+ parse_ws_pcdata_single,
+ parse_trim_pcdata
+ };
+
+ static const unsigned int flags_format[] =
+ {
+ 0,
+ format_raw,
+ format_indent
+ };
+
+ for (unsigned int i = 0; i < sizeof(flags_parse) / sizeof(flags_parse[0]); ++i)
+ {
+ xml_document doc;
+ CHECK(doc.load_string(data, flags_parse[i]));
+
+ for (unsigned int j = 0; j < sizeof(flags_format) / sizeof(flags_format[0]); ++j)
+ {
+ std::string saved = write_narrow(doc, flags_format[j], encoding_auto);
+
+ xml_document rdoc;
+ CHECK(rdoc.load_buffer(&saved[0], saved.size(), flags_parse[i]));
+
+ std::string rsaved = write_narrow(rdoc, flags_format[j], encoding_auto);
+
+ CHECK(saved == rsaved);
+ }
+ }
+}
+
+TEST_XML_FLAGS(write_mixed, "<node><child1/><child2>pre<![CDATA[data]]>mid<!--comment--><test/>post<?pi value?>fin</child2><child3/></node>", parse_full)
+{
+ CHECK_NODE(doc, STR("<node><child1 /><child2>pre<![CDATA[data]]>mid<!--comment--><test />post<?pi value?>fin</child2><child3 /></node>"));
+ CHECK_NODE_EX(doc, STR("<node>\n<child1 />\n<child2>pre<![CDATA[data]]>mid<!--comment-->\n<test />post<?pi value?>fin</child2>\n<child3 />\n</node>\n"), STR("\t"), 0);
+ CHECK_NODE_EX(doc, STR("<node>\n\t<child1 />\n\t<child2>pre<![CDATA[data]]>mid<!--comment-->\n\t\t<test />post<?pi value?>fin</child2>\n\t<child3 />\n</node>\n"), STR("\t"), format_indent);
}
diff --git a/tests/writer_string.cpp b/tests/writer_string.cpp
index 661c792..26bca8d 100644
--- a/tests/writer_string.cpp
+++ b/tests/writer_string.cpp
@@ -45,7 +45,7 @@ std::string save_narrow(const pugi::xml_document& doc, unsigned int flags, pugi:
{
xml_writer_string writer;
- doc.save(writer, STR(""), flags, encoding);
+ doc.save(writer, STR("\t"), flags, encoding);
return writer.as_narrow();
}
@@ -59,7 +59,7 @@ std::string write_narrow(pugi::xml_node node, unsigned int flags, pugi::xml_enco
{
xml_writer_string writer;
- node.print(writer, STR(""), flags, encoding);
+ node.print(writer, STR("\t"), flags, encoding);
return writer.as_narrow();
}
@@ -73,7 +73,7 @@ std::basic_string<wchar_t> write_wide(pugi::xml_node node, unsigned int flags, p
{
xml_writer_string writer;
- node.print(writer, STR(""), flags, encoding);
+ node.print(writer, STR("\t"), flags, encoding);
return writer.as_wide();
}