aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/unit_tests/parser.cpp
blob: b86d4d4dea8537342ae3f8bc9b243077591cec0a (plain) (blame)
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
// Copyright (c) 2019 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "math-server" project.
// For details, see https://github.com/egor-tensin/math-server.
// Distributed under the MIT License.

#include <parser/error.hpp>
#include <parser/parser.hpp>

#include <boost/test/data/monomorphic.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/unit_test.hpp>

#include <string>
#include <string_view>
#include <vector>

namespace bdata = boost::unit_test::data;
using math::server::Parser;
using math::server::ParserError;

namespace {
namespace exec::valid {

const std::vector<std::string_view> input{
    // Constants and unary operators:
    "1",
    "-1",
    "--1",
    "+--+-2",
    // Basic exponentiation:
    "0^0",
    "4^.5",
    " 4 ^ ---0.5 ",
    // Exponentiation is right-associative:
    "2 ^ 3 ^ 3",
    "(2 ^ 3) ^ 3",
    // Exponentiation has higher precedence than the unary minus:
    "(-2) ^ 2",
    "-2 ^ 2",
    "(.5 ^ -1) ^ 4",
    ".5 ^ -1 ^ 4",
    // Basic binary operators:
    " 1 + 2 ",
    " 2 * 1 + 3 ",
    " 2 * (1 + 3) ",
    " 2 * (1 + 3 * (1 - -3)) ",
    " -2 * (1 + --3 * (1 - -3)) ",
    // Looks weird, but also works in e.g. Python:
    " -2 * -+--- (3 + -100e-1)  ",
    " -2^2 --+- -3^0 / (4 + +12) ^.5^2 ",
    " -2^2 --+- -3^0 / ((4 + +12) ^.5)^2 ",
};

const std::vector<double> expected{
    // Constants and unary operators:
    1,
    -1,
    1,
    -2,
    // Basic exponentiation:
    1,
    2,
    0.5,
    // Exponentiation is right-associative:
    134217728,
    512,
    // Exponentiation has higher precedence than the unary minus:
    4,
    -4,
    16,
    2,
    // Basic binary operators:
    3,
    5,
    8,
    26,
    -26,
    // Looks weird, but also works in e.g. Python:
    14,
    -3.5,
    -3.9375,
};

} // namespace exec::valid

namespace exec::invalid {

const std::vector<std::string_view> input{
    "",
    "-",
    "-+-",
    // Missing operand:
    " 1 + ",
    "-2 + 3 ^ -",
    // Unmatched parentheses:
    " 2 * (1 + 3 ",
    " 2 * (1 + 3) )",
};

const std::vector<std::string> error_msg{
    "server error: parser error: expected '-', '+', '(' or a number",
    "server error: parser error: expected '-', '+', '(' or a number",
    "server error: parser error: expected '-', '+', '(' or a number",
    // Missing operand:
    "server error: parser error: expected '-', '+', '(' or a number",
    "server error: parser error: expected '-', '+', '(' or a number",
    // Unmatched parentheses:
    "server error: parser error: missing closing ')'",
    "server error: parser error: expected a binary operator",
};

} // namespace exec::invalid
} // namespace

BOOST_AUTO_TEST_SUITE(parser_tests)

BOOST_DATA_TEST_CASE(test_exec_valid,
                     bdata::make(exec::valid::input) ^ exec::valid::expected,
                     input,
                     expected) {
    Parser parser{input};
    BOOST_TEST(parser.exec() == expected);
}

BOOST_DATA_TEST_CASE(test_exec_invalid,
                     bdata::make(exec::invalid::input) ^ exec::invalid::error_msg,
                     input,
                     error_msg) {
    BOOST_REQUIRE_THROW(
        do {
            Parser parser{input};
            parser.exec();
        } while (0),
        ParserError);

    try {
        Parser parser{input};
        parser.exec();
    } catch (const ParserError& e) {
        BOOST_TEST(error_msg == e.what());
    }
}

BOOST_AUTO_TEST_SUITE_END()