tests: Add tests for fp roundtrip

We test min/max and several different mantissas for the entire exponent range
for both float and double.

It's not clear whether all supported compilers provide an implementation of
sprintf/strtod that supports roundtripping so we may need to disable some of
these tests in the future.
This commit is contained in:
Arseny Kapoulkine 2015-01-16 21:43:28 -08:00
parent f9ee391233
commit d454013cff

View File

@ -2,6 +2,7 @@
#include <limits>
#include <string>
#include <cmath>
TEST_XML(dom_attr_assign, "<node/>")
{
@ -99,6 +100,17 @@ TEST_XML(dom_attr_set_value_llong, "<node/>")
}
#endif
TEST_XML(dom_attr_assign_large_number, "<node attr1='' attr2='' />")
{
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();
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));
}
TEST_XML(dom_node_set_name, "<node>text</node>")
{
CHECK(doc.child(STR("node")).set_name(STR("n")));
@ -752,17 +764,6 @@ TEST_XML_FLAGS(dom_node_copy_types, "<?xml version='1.0'?><!DOCTYPE id><root><?p
CHECK_NODE(doc, STR("<?xml version=\"1.0\"?><!DOCTYPE id><?xml version=\"1.0\"?><!DOCTYPE id><root><?pi value?><!--comment--><node id=\"1\">pcdata<![CDATA[cdata]]></node></root><root><?pi value?><!--comment--><node id=\"1\">pcdata<![CDATA[cdata]]></node></root>"));
}
TEST_XML(dom_attr_assign_large_number, "<node attr1='' attr2='' />")
{
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();
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));
}
TEST(dom_node_declaration_name)
{
xml_document doc;
@ -1444,3 +1445,64 @@ TEST(dom_node_copy_declaration_empty_name)
CHECK_STRING(decl2.name(), STR(""));
}
TEST(dom_fp_roundtrip_min_max)
{
xml_document doc;
xml_node node = doc.append_child(STR("node"));
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());
attr.set_value(std::numeric_limits<float>::max());
CHECK(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());
node.text().set(std::numeric_limits<double>::max());
CHECK(node.text().as_double() == std::numeric_limits<double>::max());
}
const double fp_roundtrip_base[] =
{
0.31830988618379067154,
0.43429448190325182765,
0.57721566490153286061,
0.69314718055994530942,
0.70710678118654752440,
0.78539816339744830962,
};
TEST(dom_fp_roundtrip_float)
{
xml_document doc;
for (int e = -125; e <= 128; ++e)
{
for (size_t i = 0; i < sizeof(fp_roundtrip_base) / sizeof(fp_roundtrip_base[0]); ++i)
{
float value = ldexpf(fp_roundtrip_base[i], e);
doc.text().set(value);
CHECK(doc.text().as_float() == value);
}
}
}
TEST(dom_fp_roundtrip_double)
{
xml_document doc;
for (int e = -1021; e <= 1024; ++e)
{
for (size_t i = 0; i < sizeof(fp_roundtrip_base) / sizeof(fp_roundtrip_base[0]); ++i)
{
double value = ldexp(fp_roundtrip_base[i], e);
doc.text().set(value);
CHECK(doc.text().as_double() == value);
}
}
}