2020-06-26 18:01:27 +00:00
|
|
|
// This file is a part of toml++ and is subject to the the terms of the MIT license.
|
|
|
|
// Copyright (c) 2019-2020 Mark Gillard <mark.gillard@outlook.com.au>
|
|
|
|
// See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
#include "tests.h"
|
|
|
|
|
2020-04-13 20:23:11 +00:00
|
|
|
TOML_DISABLE_FLOAT_WARNINGS
|
|
|
|
|
2020-02-03 09:12:43 +00:00
|
|
|
TEST_CASE("parsing - floats")
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-04-06 12:57:49 +00:00
|
|
|
parsing_should_succeed(
|
|
|
|
FILE_LINE_ARGS,
|
|
|
|
S(R"(
|
2020-06-08 15:31:23 +00:00
|
|
|
# fractional
|
|
|
|
flt1 = +1.0
|
|
|
|
flt2 = 3.1415
|
|
|
|
flt3 = -0.01
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-06-08 15:31:23 +00:00
|
|
|
# exponent
|
|
|
|
flt4 = 5e+22
|
|
|
|
flt5 = 1e06
|
|
|
|
flt6 = -2E-2
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-06-08 15:31:23 +00:00
|
|
|
# both
|
|
|
|
flt7 = 6.626e-34
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-06-08 15:31:23 +00:00
|
|
|
flt8 = 224_617.445_991_228
|
|
|
|
)"sv),
|
2020-04-18 13:14:07 +00:00
|
|
|
[](table&& tbl)
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
|
|
|
CHECK(tbl[S("flt1")] == 1.0);
|
|
|
|
CHECK(tbl[S("flt2")] == 3.1415);
|
|
|
|
CHECK(tbl[S("flt3")] == -0.01);
|
|
|
|
CHECK(tbl[S("flt4")].as<double>()->get() == 5e+22_a);
|
|
|
|
CHECK(tbl[S("flt5")].as<double>()->get() == 1e6_a);
|
|
|
|
CHECK(tbl[S("flt6")] == -2E-2);
|
|
|
|
CHECK(tbl[S("flt7")].as<double>()->get() == 6.626e-34_a);
|
|
|
|
CHECK(tbl[S("flt8")].as<double>()->get() == 224617.445991228_a);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// "Each underscore must be surrounded by at least one digit on each side."
|
2020-04-06 12:57:49 +00:00
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt8 = 224_617.445_991_228_"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt8 = _224_617.445_991_228"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt8 = 224__617.445_991_228"sv));
|
2020-01-04 14:21:38 +00:00
|
|
|
|
|
|
|
// "Float values -0.0 and +0.0 are valid and should map according to IEEE 754."
|
|
|
|
parsing_should_succeed(
|
2020-04-06 12:57:49 +00:00
|
|
|
FILE_LINE_ARGS,
|
2020-01-04 14:21:38 +00:00
|
|
|
S(R"(zeroes = [-0.0, +0.0])"sv),
|
2020-04-18 13:14:07 +00:00
|
|
|
[](table&& tbl)
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
|
|
|
CHECK(tbl[S("zeroes")][0] == -0.0);
|
|
|
|
CHECK(tbl[S("zeroes")][1] == +0.0);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-06-25 14:33:01 +00:00
|
|
|
// "A float consists of an integer part followed by a fractional part and/or an exponent part"
|
|
|
|
// (i.e. omitting the leading digits before the '.' is not legal in TOML)
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = .1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = +.1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = -.1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = .1e1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = .1e+1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = .1e-1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = +.1e1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = +.1e+1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = +.1e-1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = -.1e1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = -.1e+1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = -.1e-1"sv));
|
|
|
|
|
|
|
|
// likewise, so is omitting digits _after_ the '.'
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = 1."sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = +1."sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = -1."sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = 1.e1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = 1.e+1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = 1.e-1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = +1.e1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = +1.e+1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = +1.e-1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = -1.e1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = -1.e+1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S("flt = -1.e-1"sv));
|
|
|
|
|
2020-04-13 20:23:11 +00:00
|
|
|
// value tests
|
2020-04-06 12:57:49 +00:00
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1e1"sv, 1e1);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1e+1"sv, 1e+1);
|
2020-04-09 08:13:12 +00:00
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1e-1"sv, 1e-1);
|
2020-04-06 12:57:49 +00:00
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1.0"sv, 1.0);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1.0e1"sv, 1.0e1);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1.0e+1"sv, 1.0e+1);
|
2020-04-09 08:13:12 +00:00
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1.0e-1"sv, 1.0e-1);
|
2020-04-06 12:57:49 +00:00
|
|
|
parse_expected_value( FILE_LINE_ARGS, "+1e1"sv, +1e1);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "+1.0"sv, +1.0);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "+1.0e1"sv, +1.0e1);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "+1.0e+1"sv, +1.0e+1);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "+1.0e-1"sv, +1.0e-1);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "-1.0e+1"sv, -1.0e+1);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "-1e1"sv, -1e1);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "-1.0"sv, -1.0);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "-1.0e1"sv, -1.0e1);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "-1.0e-1"sv, -1.0e-1);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "0.1"sv, 0.1);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "0.001"sv, 0.001);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "0.100"sv, 0.100);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "+3.14"sv, +3.14);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "-3.14"sv, -3.14);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "3.1415_9265_3589"sv, 3.141592653589);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "+3.1415_9265_3589"sv, +3.141592653589);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "-3.1415_9265_3589"sv, -3.141592653589);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "123_456.789"sv, 123456.789);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "+123_456.789"sv, +123456.789);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "-123_456.789"sv, -123456.789);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "+0.0"sv, +0.0);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "-0.0"sv, -0.0);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1e10"sv, 1e10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1e+10"sv, 1e+10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1e-10"sv, 1e-10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "+1e10"sv, +1e10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "+1e+10"sv, +1e+10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "+1e-10"sv, +1e-10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "-1e10"sv, -1e10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "-1e+10"sv, -1e+10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "-1e-10"sv, -1e-10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "123e-10"sv, 123e-10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1E10"sv, 1E10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1E+10"sv, 1E+10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1E-10"sv, 1E-10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "123E-10"sv, 123E-10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1_2_3E-10"sv, 123E-10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1_2_3E-1_0"sv, 123E-10);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "+0e0"sv, +0e0);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "-0e0"sv, -0e0);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1_2_3E-01"sv, 123E-01);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1_2_3E-0_1"sv, 123E-01);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "6.02e23"sv, 6.02e23);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "6.02e+23"sv, 6.02e+23);
|
|
|
|
parse_expected_value( FILE_LINE_ARGS, "1.112_650_06e-17"sv, 1.11265006e-17);
|
2020-01-06 18:21:16 +00:00
|
|
|
|
2020-04-13 20:23:11 +00:00
|
|
|
// toml/issues/562 (hexfloats)
|
2020-04-02 21:39:21 +00:00
|
|
|
#if TOML_LANG_UNRELEASED
|
2020-04-06 12:57:49 +00:00
|
|
|
parse_expected_value(FILE_LINE_ARGS, " 0x1.2p3"sv, 0x1.2p3);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " 0x10p1"sv, 0x10p1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " 0x10p-1"sv, 0x10p-1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " 0x10p+1"sv, 0x10p+1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " -0x10p1"sv, -0x10p1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " -0x10p-1"sv, -0x10p-1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " +0x10p1"sv, +0x10p1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " +0x10p+1"sv, +0x10p+1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " -0x10p+1"sv, -0x10p+1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " +0x10p-1"sv, +0x10p-1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " 0x10.1p1"sv, 0x10.1p1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " 0x10.1p-1"sv, 0x10.1p-1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " 0x10.1p+1"sv, 0x10.1p+1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " -0x10.1p1"sv, -0x10.1p1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " -0x10.1p-1"sv, -0x10.1p-1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " +0x10.1p1"sv, +0x10.1p1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " +0x10.1p+1"sv, +0x10.1p+1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " -0x10.1p+1"sv, -0x10.1p+1);
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, " +0x10.1p-1"sv, +0x10.1p-1);
|
2020-01-06 18:21:16 +00:00
|
|
|
#else
|
2020-04-06 12:57:49 +00:00
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = 0x10p1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = 0x10p-1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = 0x10p+1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = -0x10p1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = -0x10p-1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = +0x10p1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = +0x10p+1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = -0x10p+1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = +0x10p-1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = 0x10.1p1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = 0x10.1p-1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = 0x10.1p+1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = -0x10.1p1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = -0x10.1p-1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = +0x10.1p1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = +0x10.1p+1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = -0x10.1p+1"sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = +0x10.1p-1"sv));
|
2020-01-06 18:21:16 +00:00
|
|
|
#endif
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 09:12:43 +00:00
|
|
|
TEST_CASE("parsing - inf and nan")
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-04-06 12:57:49 +00:00
|
|
|
parsing_should_succeed(
|
|
|
|
FILE_LINE_ARGS,
|
|
|
|
S(R"(
|
2020-06-08 15:31:23 +00:00
|
|
|
# infinity
|
|
|
|
sf1 = inf # positive infinity
|
|
|
|
sf2 = +inf # positive infinity
|
|
|
|
sf3 = -inf # negative infinity
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-06-08 15:31:23 +00:00
|
|
|
# not a number
|
|
|
|
sf4 = nan # actual sNaN/qNaN encoding is implementation specific
|
|
|
|
sf5 = +nan # same as `nan`
|
|
|
|
sf6 = -nan # valid, actual encoding is implementation specific
|
|
|
|
)"sv),
|
2020-04-18 13:14:07 +00:00
|
|
|
[](table&& tbl)
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
|
|
|
CHECK(tbl[S("sf1")] == std::numeric_limits<double>::infinity());
|
|
|
|
CHECK(tbl[S("sf2")] == std::numeric_limits<double>::infinity());
|
|
|
|
CHECK(tbl[S("sf3")] == -std::numeric_limits<double>::infinity());
|
|
|
|
CHECK(std::isnan(tbl[S("sf4")].as<double>()->get()));
|
|
|
|
CHECK(std::isnan(tbl[S("sf5")].as<double>()->get()));
|
|
|
|
CHECK(std::isnan(tbl[S("sf6")].as<double>()->get()));
|
|
|
|
}
|
|
|
|
);
|
2020-04-09 08:13:12 +00:00
|
|
|
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = NaN "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = Nan "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = NAN "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = +NaN "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = +Nan "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = +NAN "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = -NaN "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = -Nan "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = -NAN "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = 1.nan "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = 1,nan "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = .nan "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = ,nan "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = nan.1 "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = nan,1 "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = nan. "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = nan, "sv));
|
|
|
|
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = Inf "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = INF "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = +Inf "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = +INF "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = -Inf "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = -INF "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = 1.inf "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = 1,inf "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = .inf "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = ,inf "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = inf.1 "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = inf,1 "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = inf. "sv));
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, S(" val = inf, "sv));
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|