1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>
#include <toml11/parser.hpp>
#include <toml11/serializer.hpp>
#include <toml11/types.hpp>
TEST_CASE("testing fractional float")
{
auto fmt = [](std::size_t prec) {
toml::floating_format_info f;
f.fmt = toml::floating_format::fixed;
f.prec = prec;
return f;
};
auto format_as = [](const double f, int w) -> std::string {
std::ostringstream oss;
oss.imbue(std::locale::classic());
oss << std::fixed << std::setprecision(w) << f;
return oss.str();
};
CHECK_EQ(format_as( 1.0 , 1), toml::format(toml::value( 1.0 , fmt( 1))));
CHECK_EQ(format_as( 0.1 , 1), toml::format(toml::value( 0.1 , fmt( 1))));
CHECK_EQ(format_as( 0.001 , 3), toml::format(toml::value( 0.001 , fmt( 3))));
CHECK_EQ(format_as( 0.1 , 3), toml::format(toml::value( 0.1 , fmt( 3))));
CHECK_EQ(format_as( 3.14 , 2), toml::format(toml::value( 3.14 , fmt( 2))));
CHECK_EQ(format_as(-3.14 , 2), toml::format(toml::value(-3.14 , fmt( 2))));
CHECK_EQ(format_as( 3.141592653589, 12), toml::format(toml::value( 3.141592653589, fmt(12))));
CHECK_EQ(format_as(-3.141592653589, 12), toml::format(toml::value(-3.141592653589, fmt(12))));
}
TEST_CASE("testing scientific float")
{
auto fmt = [](std::size_t prec) {
toml::floating_format_info f;
f.fmt = toml::floating_format::scientific;
f.prec = prec;
return f;
};
auto format_as = [](const double f, int w) -> std::string {
std::ostringstream oss;
oss.imbue(std::locale::classic());
oss << std::scientific << std::setprecision(w) << f;
return oss.str();
};
CHECK_EQ(format_as( 1.0 , 1), toml::format(toml::value( 1.0 , fmt( 1))));
CHECK_EQ(format_as( 0.1 , 1), toml::format(toml::value( 0.1 , fmt( 1))));
CHECK_EQ(format_as( 0.001 , 3), toml::format(toml::value( 0.001 , fmt( 3))));
CHECK_EQ(format_as( 0.1 , 3), toml::format(toml::value( 0.1 , fmt( 3))));
CHECK_EQ(format_as( 3.14 , 2), toml::format(toml::value( 3.14 , fmt( 2))));
CHECK_EQ(format_as(-3.14 , 2), toml::format(toml::value(-3.14 , fmt( 2))));
CHECK_EQ(format_as( 3.141592653589, 12), toml::format(toml::value( 3.141592653589, fmt(12))));
CHECK_EQ(format_as(-3.141592653589, 12), toml::format(toml::value(-3.141592653589, fmt(12))));
}
TEST_CASE("testing hex float")
{
toml::spec s = toml::spec::v(1,0,0);
s.ext_hex_float = true;
toml::floating_format_info fmt;
fmt.fmt = toml::floating_format::hex;
auto format_as = [](const double f) -> std::string {
std::ostringstream oss;
oss.imbue(std::locale::classic());
oss << std::hexfloat << f;
return oss.str();
};
CHECK_EQ(format_as( 1.0 ), toml::format(toml::value( 1.0 , fmt), s));
CHECK_EQ(format_as( 0.1 ), toml::format(toml::value( 0.1 , fmt), s));
CHECK_EQ(format_as( 0.001 ), toml::format(toml::value( 0.001 , fmt), s));
CHECK_EQ(format_as( 0.1 ), toml::format(toml::value( 0.1 , fmt), s));
CHECK_EQ(format_as( 3.14 ), toml::format(toml::value( 3.14 , fmt), s));
CHECK_EQ(format_as(-3.14 ), toml::format(toml::value(-3.14 , fmt), s));
CHECK_EQ(format_as( 3.141592653589), toml::format(toml::value( 3.141592653589, fmt), s));
CHECK_EQ(format_as(-3.141592653589), toml::format(toml::value(-3.141592653589, fmt), s));
}
TEST_CASE("testing suffix + fractional")
{
toml::spec s = toml::spec::v(1,0,0);
s.ext_hex_float = true;
s.ext_num_suffix = true;
auto fmt = [](std::string sfx) {
toml::floating_format_info f;
f.fmt = toml::floating_format::fixed;
f.suffix = sfx;
return f;
};
auto format_as = [](const double f, std::string sfx) -> std::string {
std::ostringstream oss;
oss.imbue(std::locale::classic());
oss << std::fixed << f << "_" << sfx;
return oss.str();
};
CHECK_EQ(format_as( 1.0 , "m" ), toml::format(toml::value( 1.0 , fmt("m" )), s));
CHECK_EQ(format_as( 0.1 , "m" ), toml::format(toml::value( 0.1 , fmt("m" )), s));
CHECK_EQ(format_as( 0.001 , "mm" ), toml::format(toml::value( 0.001 , fmt("mm" )), s));
CHECK_EQ(format_as( 0.1 , "mm" ), toml::format(toml::value( 0.1 , fmt("mm" )), s));
CHECK_EQ(format_as( 3.14 , "rad"), toml::format(toml::value( 3.14 , fmt("rad")), s));
CHECK_EQ(format_as(-3.14 , "rad"), toml::format(toml::value(-3.14 , fmt("rad")), s));
CHECK_EQ(format_as( 3.141592653589, "rad"), toml::format(toml::value( 3.141592653589, fmt("rad")), s));
CHECK_EQ(format_as(-3.141592653589, "rad"), toml::format(toml::value(-3.141592653589, fmt("rad")), s));
}
TEST_CASE("testing suffix + scientific")
{
toml::spec s = toml::spec::v(1,0,0);
s.ext_hex_float = true;
s.ext_num_suffix = true;
auto fmt = [](std::string sfx) {
toml::floating_format_info f;
f.fmt = toml::floating_format::scientific;
f.suffix = sfx;
return f;
};
auto format_as = [](const double f, std::string sfx) -> std::string {
std::ostringstream oss;
oss.imbue(std::locale::classic());
oss << std::scientific << f << "_" << sfx;
return oss.str();
};
CHECK_EQ(format_as( 1.0 , "m" ), toml::format(toml::value( 1.0 , fmt("m" )), s));
CHECK_EQ(format_as( 0.1 , "m" ), toml::format(toml::value( 0.1 , fmt("m" )), s));
CHECK_EQ(format_as( 0.001 , "mm" ), toml::format(toml::value( 0.001 , fmt("mm" )), s));
CHECK_EQ(format_as( 0.1 , "mm" ), toml::format(toml::value( 0.1 , fmt("mm" )), s));
CHECK_EQ(format_as( 3.14 , "rad"), toml::format(toml::value( 3.14 , fmt("rad")), s));
CHECK_EQ(format_as(-3.14 , "rad"), toml::format(toml::value(-3.14 , fmt("rad")), s));
CHECK_EQ(format_as( 3.141592653589, "rad"), toml::format(toml::value( 3.141592653589, fmt("rad")), s));
CHECK_EQ(format_as(-3.141592653589, "rad"), toml::format(toml::value(-3.141592653589, fmt("rad")), s));
}
|