summaryrefslogtreecommitdiffstats
path: root/src/parser/parser_global_test.cpp
blob: 680a2216071b1749480f0adebe4e1ef2bca6c714 (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
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
168
169
170
171
172
173
174
175
/* -*- 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 <orcus/parser_global.hpp>
#include <orcus/cell_buffer.hpp>

#include <vector>
#include <cmath>
#include <cstring>
#include <limits>

namespace {

void test_parse_numbers()
{
    orcus::test::stack_printer __sp__(__func__);

    struct test_case
    {
        const char* str;
        double val;
    };

    std::vector<test_case> test_cases = {
        {"1", 1.0},
        {"1.0", 1.0},
        {"-1.0", -1.0},
        {"2e2", 200.0},
        {"1.2", 1.2},
        {"-0.0001", -0.0001},
        {"-0.0", 0.0},
        {"+.", std::numeric_limits<double>::signaling_NaN()},
        {"+e", std::numeric_limits<double>::signaling_NaN()},
        {"+e1", std::numeric_limits<double>::signaling_NaN()},
        {"+ ",  std::numeric_limits<double>::signaling_NaN()},
        {"- ",  std::numeric_limits<double>::signaling_NaN()}
    };

    for (const test_case& test_data : test_cases)
    {
        const char* str = test_data.str;
        double val;
        orcus::parse_numeric(str, str + std::strlen(test_data.str), val);
        if (std::isnan(test_data.val))
        {
            assert(std::isnan(val));
        }
        else
        {
            assert(val == test_data.val);
        }
    }
}

void test_parse_integers()
{
    orcus::test::stack_printer __sp__(__func__);

    std::string_view test_str = "-100";

    long value;
    const char* p = test_str.data();
    const char* p_end = p + test_str.size();
    const char* p_last = orcus::parse_integer(p, p_end, value);

    assert(value == -100);
    assert(p_last == p_end);

    --p_end;
    p_last = orcus::parse_integer(p, p_end, value);
    assert(value == -10);
    assert(p_last == p_end);

    --p_end;
    p_last = orcus::parse_integer(p, p_end, value);
    assert(value == -1);
    assert(p_last == p_end);

    test_str = "13.4";  // the parsing should end on the '.'
    p = test_str.data();
    p_end = p + test_str.size();
    p_last = orcus::parse_integer(p, p_end, value);
    assert(value == 13);
    assert(p_last == p + 2);

    // What if the p_end points to an earlier address than the p ...
    std::swap(p, p_end);
    assert(p > p_end);
    p_last = orcus::parse_integer(p, p_end, value);
    assert(p == p_last);

    // Empty char range
    p = test_str.data();
    p_end = p;
    p_last = orcus::parse_integer(p, p_end, value);
    assert(p_last == p);
}

void test_parse_double_quoted_strings()
{
    orcus::test::stack_printer __sp__(__func__);

    struct test_case
    {
        std::string input;
        const char* expected_p;
        size_t expected_n;
    };

    std::vector<test_case> test_cases = {
        { "\"", nullptr, orcus::parse_quoted_string_state::error_no_closing_quote },
        { "\"\"", "", 0 },
        { "\"a\"", "a", 1 },

    };

    for (const test_case& tc : test_cases)
    {
        orcus::cell_buffer buf;
        const char* p = tc.input.data();
        size_t n = tc.input.size();
        orcus::parse_quoted_string_state ret = orcus::parse_double_quoted_string(p, n, buf);

        if (tc.expected_p)
        {
            std::string expected(tc.expected_p, tc.expected_n);
            std::string actual(ret.str, ret.length);
            assert(expected == actual);
        }
        else
        {
            assert(ret.str == nullptr);
            assert(ret.length == tc.expected_n);
        }
    }

}

void test_trim()
{
    // test for trimming.
    std::string s1("test"), s2("  test"), s3("   test  "), s4("test   ");
    std::string_view sv1(s1), sv2(s2), sv3(s3), sv4(s4);
    assert(sv1 != sv2);
    assert(sv1 != sv3);
    assert(sv2 != sv3);
    assert(sv1 != sv4);

    std::string_view trimmed = orcus::trim(sv1);
    assert(sv1 == trimmed); // nothing to trim.
    assert(sv1 == orcus::trim(sv2));
    assert(sv1 == orcus::trim(sv3));
    assert(sv1 == orcus::trim(sv4));
    assert(sv1.size() == orcus::trim(sv2).size());
    assert(sv1.size() == orcus::trim(sv3).size());
}

}

int main()
{
    test_parse_numbers();
    test_parse_integers();
    test_parse_double_quoted_strings();
    test_trim();

    return 0;
}

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