mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 09:08:08 +08:00
test: update test codes to v4
This commit is contained in:
@@ -1,181 +1,167 @@
|
||||
#include <toml/parser.hpp>
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "doctest.h"
|
||||
|
||||
#include "unit_test.hpp"
|
||||
#include "test_parse_aux.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <toml11/parser.hpp>
|
||||
#include <toml11/types.hpp>
|
||||
|
||||
using namespace toml;
|
||||
using namespace detail;
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_fractional)
|
||||
TEST_CASE("testing fractional float")
|
||||
{
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "1.0", 1.0);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "0.1", 0.1);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "0.001", 0.001);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "0.100", 0.1);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "+3.14", 3.14);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "-3.14", -3.14);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "3.1415_9265_3589", 3.141592653589);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "+3.1415_9265_3589", 3.141592653589);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "-3.1415_9265_3589", -3.141592653589);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "123_456.789", 123456.789);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "+123_456.789", 123456.789);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "-123_456.789", -123456.789);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "+0.0", 0.0);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "-0.0", -0.0);
|
||||
toml::detail::context<toml::type_config> ctx(toml::spec::v(1,0,0));
|
||||
|
||||
auto fmt = [](std::size_t prec) {
|
||||
toml::floating_format_info f;
|
||||
f.fmt = toml::floating_format::fixed;
|
||||
f.prec = prec;
|
||||
return f;
|
||||
};
|
||||
|
||||
toml11_test_parse_success<toml::value_t::floating>("1.0", 1.0 , comments(), fmt( 1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("0.1", 0.1 , comments(), fmt( 1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("0.001", 0.001 , comments(), fmt( 3), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("0.100", 0.1 , comments(), fmt( 3), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("+3.14", 3.14 , comments(), fmt( 2), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("-3.14", -3.14 , comments(), fmt( 2), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("3.1415_9265_3589", 3.141592653589, comments(), fmt(12), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("+3.1415_9265_3589", 3.141592653589, comments(), fmt(12), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("-3.1415_9265_3589", -3.141592653589, comments(), fmt(12), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("123_456.789", 123456.789 , comments(), fmt( 3), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("+123_456.789", 123456.789 , comments(), fmt( 3), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("-123_456.789", -123456.789 , comments(), fmt( 3), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("+0.0", 0.0 , comments(), fmt( 1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("-0.0", -0.0 , comments(), fmt( 1), ctx);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_fractional_value)
|
||||
TEST_CASE("testing exponents")
|
||||
{
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "1.0", value( 1.0));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "0.1", value( 0.1));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "0.001", value( 0.001));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "0.100", value( 0.1));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "+3.14", value( 3.14));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "-3.14", value(-3.14));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "3.1415_9265_3589", value( 3.141592653589));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "+3.1415_9265_3589", value( 3.141592653589));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "-3.1415_9265_3589", value(-3.141592653589));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "123_456.789", value( 123456.789));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "+123_456.789", value( 123456.789));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "-123_456.789", value(-123456.789));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "+0.0", value( 0.0));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "-0.0", value(-0.0));
|
||||
toml::detail::context<toml::type_config> ctx(toml::spec::v(1,0,0));
|
||||
|
||||
auto fmt = [](std::size_t prec) {
|
||||
toml::floating_format_info f;
|
||||
f.fmt = toml::floating_format::scientific;
|
||||
f.prec = prec;
|
||||
return f;
|
||||
};
|
||||
|
||||
toml11_test_parse_success<toml::value_t::floating>("1e10", 1e10 , comments(), fmt(1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("1e+10", 1e10 , comments(), fmt(1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("1e-10", 1e-10 , comments(), fmt(1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("+1e10", 1e10 , comments(), fmt(1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("+1e+10", 1e10 , comments(), fmt(1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("+1e-10", 1e-10 , comments(), fmt(1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("-1e10", -1e10 , comments(), fmt(1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("-1e+10", -1e10 , comments(), fmt(1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("-1e-10", -1e-10 , comments(), fmt(1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("123e-10", 123e-10, comments(), fmt(3), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("1E10", 1e10 , comments(), fmt(1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("1E+10", 1e10 , comments(), fmt(1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("1E-10", 1e-10 , comments(), fmt(1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("123E-10", 123e-10, comments(), fmt(3), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("1_2_3E-10", 123e-10, comments(), fmt(3), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("1_2_3E-1_0", 123e-10, comments(), fmt(3), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("+0e0", 0.0 , comments(), fmt(1), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("-0e0", -0.0 , comments(), fmt(1), ctx);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_exponential)
|
||||
TEST_CASE("testing fraction + exponents")
|
||||
{
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "1e10", 1e10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "1e+10", 1e10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "1e-10", 1e-10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "+1e10", 1e10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "+1e+10", 1e10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "+1e-10", 1e-10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "-1e10", -1e10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "-1e+10", -1e10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "-1e-10", -1e-10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "123e-10", 123e-10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "1E10", 1e10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "1E+10", 1e10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "1E-10", 1e-10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "123E-10", 123e-10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "1_2_3E-10", 123e-10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "1_2_3E-1_0", 123e-10);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "+0e0", 0.0);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "-0e0", -0.0);
|
||||
toml::detail::context<toml::type_config> ctx(toml::spec::v(1,0,0));
|
||||
|
||||
#ifdef TOML11_USE_UNRELEASED_TOML_FEATURES
|
||||
BOOST_TEST_MESSAGE("testing an unreleased toml feature: leading zeroes in float exponent part");
|
||||
// toml-lang/toml master permits leading 0s in exp part (unreleased)
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "1_2_3E-01", 123e-1);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "1_2_3E-0_1", 123e-1);
|
||||
#endif
|
||||
auto fmt = [](std::size_t prec) {
|
||||
toml::floating_format_info f;
|
||||
f.fmt = toml::floating_format::scientific;
|
||||
f.prec = prec;
|
||||
return f;
|
||||
};
|
||||
|
||||
toml11_test_parse_success<toml::value_t::floating>("6.02e23", 6.02e23, comments(), fmt(3), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("6.02e+23", 6.02e23, comments(), fmt(3), ctx);
|
||||
toml11_test_parse_success<toml::value_t::floating>("1.112_650_06e-17", 1.11265006e-17, comments(), fmt(9), ctx);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_exponential_value)
|
||||
TEST_CASE("testing +/-inf")
|
||||
{
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "1e10", value(1e10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "1e+10", value(1e10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "1e-10", value(1e-10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "+1e10", value(1e10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "+1e+10", value(1e10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "+1e-10", value(1e-10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "-1e10", value(-1e10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "-1e+10", value(-1e10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "-1e-10", value(-1e-10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "123e-10", value(123e-10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "1E10", value(1e10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "1E+10", value(1e10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "1E-10", value(1e-10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "123E-10", value(123e-10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "1_2_3E-10", value(123e-10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "1_2_3E-1_0", value(123e-10));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "+0e0", value( 0.0));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "-0e0", value(-0.0));
|
||||
toml::detail::context<toml::type_config> ctx(toml::spec::v(1,0,0));
|
||||
|
||||
#ifdef TOML11_USE_UNRELEASED_TOML_FEATURES
|
||||
BOOST_TEST_MESSAGE("testing an unreleased toml feature: leading zeroes in float exponent part");
|
||||
// toml-lang/toml master permits leading 0s in exp part (unreleased)
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "1_2_3E-01", value(123e-1));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "1_2_3E-0_1", value(123e-1));
|
||||
#endif
|
||||
}
|
||||
BOOST_AUTO_TEST_CASE(test_fe)
|
||||
{
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "6.02e23", 6.02e23);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "6.02e+23", 6.02e23);
|
||||
TOML11_TEST_PARSE_EQUAL(parse_floating, "1.112_650_06e-17", 1.11265006e-17);
|
||||
}
|
||||
BOOST_AUTO_TEST_CASE(test_fe_vaule)
|
||||
{
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "6.02e23", value(6.02e23));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "6.02e+23", value(6.02e23));
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "1.112_650_06e-17", value(1.11265006e-17));
|
||||
|
||||
#ifdef TOML11_USE_UNRELEASED_TOML_FEATURES
|
||||
BOOST_TEST_MESSAGE("testing an unreleased toml feature: leading zeroes in float exponent part");
|
||||
// toml-lang/toml master permits leading 0s in exp part (unreleased)
|
||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "3.141_5e-01", value(3.1415e-1));
|
||||
#endif
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_inf)
|
||||
{
|
||||
{
|
||||
const std::string token("inf");
|
||||
toml::detail::location loc("test", token);
|
||||
const auto r = parse_floating(loc);
|
||||
BOOST_CHECK(r.is_ok());
|
||||
BOOST_CHECK(std::isinf(r.unwrap().first));
|
||||
BOOST_CHECK(r.unwrap().first > 0.0);
|
||||
auto loc = toml::detail::make_temporary_location("inf");
|
||||
const auto res = toml::detail::parse_floating(loc, ctx);
|
||||
REQUIRE_UNARY(res.is_ok());
|
||||
const auto val = res.unwrap();
|
||||
REQUIRE_UNARY(val.is_floating());
|
||||
|
||||
CHECK_UNARY(std::isinf(val.as_floating()));
|
||||
CHECK_UNARY(val.as_floating() > 0); // +
|
||||
}
|
||||
{
|
||||
const std::string token("+inf");
|
||||
toml::detail::location loc("test", token);
|
||||
const auto r = parse_floating(loc);
|
||||
BOOST_CHECK(r.is_ok());
|
||||
BOOST_CHECK(std::isinf(r.unwrap().first));
|
||||
BOOST_CHECK(r.unwrap().first > 0.0);
|
||||
auto loc = toml::detail::make_temporary_location("+inf");
|
||||
const auto res = toml::detail::parse_floating(loc, ctx);
|
||||
REQUIRE_UNARY(res.is_ok());
|
||||
const auto val = res.unwrap();
|
||||
REQUIRE_UNARY(val.is_floating());
|
||||
|
||||
CHECK_UNARY(std::isinf(val.as_floating()));
|
||||
CHECK_UNARY(val.as_floating() > 0); // +
|
||||
}
|
||||
{
|
||||
const std::string token("-inf");
|
||||
toml::detail::location loc("test", token);
|
||||
const auto r = parse_floating(loc);
|
||||
BOOST_CHECK(r.is_ok());
|
||||
BOOST_CHECK(std::isinf(r.unwrap().first));
|
||||
BOOST_CHECK(r.unwrap().first < 0.0);
|
||||
auto loc = toml::detail::make_temporary_location("-inf");
|
||||
const auto res = toml::detail::parse_floating(loc, ctx);
|
||||
REQUIRE_UNARY(res.is_ok());
|
||||
const auto val = res.unwrap();
|
||||
REQUIRE_UNARY(val.is_floating());
|
||||
|
||||
CHECK_UNARY(std::isinf(val.as_floating()));
|
||||
CHECK_UNARY(val.as_floating() < 0); // -
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_nan)
|
||||
TEST_CASE("testing +/-nan")
|
||||
{
|
||||
toml::detail::context<toml::type_config> ctx(toml::spec::v(1,0,0));
|
||||
|
||||
{
|
||||
const std::string token("nan");
|
||||
toml::detail::location loc("test", token);
|
||||
const auto r = parse_floating(loc);
|
||||
BOOST_CHECK(r.is_ok());
|
||||
BOOST_CHECK(std::isnan(r.unwrap().first));
|
||||
auto loc = toml::detail::make_temporary_location("nan");
|
||||
const auto res = toml::detail::parse_floating(loc, ctx);
|
||||
REQUIRE_UNARY(res.is_ok());
|
||||
const auto val = res.unwrap();
|
||||
REQUIRE_UNARY(val.is_floating());
|
||||
|
||||
CHECK_UNARY(std::isnan(val.as_floating()));
|
||||
}
|
||||
{
|
||||
const std::string token("+nan");
|
||||
toml::detail::location loc("test", token);
|
||||
const auto r = parse_floating(loc);
|
||||
BOOST_CHECK(r.is_ok());
|
||||
BOOST_CHECK(std::isnan(r.unwrap().first));
|
||||
auto loc = toml::detail::make_temporary_location("+nan");
|
||||
const auto res = toml::detail::parse_floating(loc, ctx);
|
||||
REQUIRE_UNARY(res.is_ok());
|
||||
const auto val = res.unwrap();
|
||||
REQUIRE_UNARY(val.is_floating());
|
||||
|
||||
CHECK_UNARY(std::isnan(val.as_floating()));
|
||||
}
|
||||
{
|
||||
const std::string token("-nan");
|
||||
toml::detail::location loc("test", token);
|
||||
const auto r = parse_floating(loc);
|
||||
BOOST_CHECK(r.is_ok());
|
||||
BOOST_CHECK(std::isnan(r.unwrap().first));
|
||||
auto loc = toml::detail::make_temporary_location("-nan");
|
||||
const auto res = toml::detail::parse_floating(loc, ctx);
|
||||
REQUIRE_UNARY(res.is_ok());
|
||||
const auto val = res.unwrap();
|
||||
REQUIRE_UNARY(val.is_floating());
|
||||
|
||||
CHECK_UNARY(std::isnan(val.as_floating()));
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_overflow)
|
||||
TEST_CASE("testing hexfloat")
|
||||
{
|
||||
std::istringstream float_overflow (std::string("float-overflow = 1.0e+1024"));
|
||||
BOOST_CHECK_THROW(toml::parse(float_overflow ), toml::syntax_error);
|
||||
// istringstream >> float does not set failbit in case of underflow.
|
||||
toml::spec s = toml::spec::v(1,0,0);
|
||||
s.ext_hex_float = true;
|
||||
toml::detail::context<toml::type_config> ctx(s);
|
||||
|
||||
{
|
||||
auto loc = toml::detail::make_temporary_location("0xABCp-3");
|
||||
const auto res = toml::detail::parse_floating(loc, ctx);
|
||||
REQUIRE_UNARY(res.is_ok());
|
||||
const auto val = res.unwrap();
|
||||
REQUIRE_UNARY(val.is_floating());
|
||||
|
||||
CHECK_EQ(val.as_floating(), 343.5);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user