File: test_parse_floating.cpp

package info (click to toggle)
toml11 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,464 kB
  • sloc: cpp: 38,446; makefile: 8; sh: 5
file content (167 lines) | stat: -rw-r--r-- 7,627 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

#include "utility.hpp"

#include <toml11/parser.hpp>
#include <toml11/types.hpp>

TEST_CASE("testing fractional float")
{
    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);
}

TEST_CASE("testing exponents")
{
    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);
}

TEST_CASE("testing fraction + exponents")
{
    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>("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);
}

TEST_CASE("testing +/-inf")
{
    toml::detail::context<toml::type_config> ctx(toml::spec::v(1,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); // +
    }
    {
        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); // +
    }
    {
        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); // -
    }
}

TEST_CASE("testing +/-nan")
{
    toml::detail::context<toml::type_config> ctx(toml::spec::v(1,0,0));

    {
        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()));
    }
    {
        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()));
    }
    {
        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()));
    }
}

TEST_CASE("testing hexfloat")
{
    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);
    }
}