summaryrefslogtreecommitdiffstats
path: root/src/parser/parser_test_numeric.cpp
blob: b16ed9a7118c9e203085e0e913fedf34b663d269 (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "test_global.hpp"
#include "numeric_parser.hpp"

#include <cassert>
#include <iostream>
#include <vector>
#include <limits>

using namespace orcus;
using std::cout;
using std::endl;

namespace {

struct check
{
    std::string_view str;
    double expected;
};

const double invalid = std::numeric_limits<double>::quiet_NaN();

template<typename ParserT>
bool run_checks(const std::vector<check>& checks)
{
    for (const check& c : checks)
    {
        ParserT parser(c.str.data(), c.str.data() + c.str.size());
        double v = parser.parse();

        if (std::isnan(c.expected))
        {
            if (!std::isnan(v))
            {
                cout << "'" << c.str << "' was expected to be invalid, but parser parsed as if it was valid." << endl;
                return false;
            }
        }
        else
        {
            if (v != c.expected)
            {
                cout << "'" << c.str << "' was expected to be parsed as (" << c.expected << "), but the parser parsed it as (" << v << ")" << endl;
                return false;
            }
        }
    }

    return true;
}

}

void test_generic_number_parsing()
{
    using parser_type = detail::numeric_parser<detail::generic_parser_trait>;

    std::vector<check> checks = {
        { "-6.e3", -6e3 },
        { "true", invalid },
        { "1", 1.0 },
        { "1.0", 1.0 },
        { "-1.0", -1.0 },
        { "-01", -1.0 },
        { "2e2", 200.0 },
        { "1.2", 1.2 },
        { "-0.0001", -0.0001 },
        { "-0.0", 0.0 },
        { "+.", invalid },
        { "+e", invalid },
        { "+e1", invalid },
        { "+ ", invalid },
        { "- ", invalid }
    };

    assert(run_checks<parser_type>(checks));
}

void test_json_number_parsing()
{
    using parser_type = detail::numeric_parser<detail::json_parser_trait>;

    std::vector<check> checks = {
        { "-01", invalid }, // Leading zeros are invalid.
    };

    assert(run_checks<parser_type>(checks));
}

int main()
{
    test_generic_number_parsing();
    test_json_number_parsing();

    return EXIT_SUCCESS;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */