aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/unit_tests/lexer.cpp
blob: 7b513e8dffb6173745f4fd232efa371c7a0a23a2 (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
#include <server/lexer.hpp>

#include <boost/test/unit_test.hpp>

#include <vector>

BOOST_AUTO_TEST_CASE(test_lexer_parse_number) {
    using namespace math::server::lexer;

    // These are valid numbers:
    BOOST_TEST(details::parse_number("0").value() == 0);
    BOOST_TEST(details::parse_number("1.").value() == 1.);
    BOOST_TEST(details::parse_number(".1").value() == .1);
    // parse_* functions only consume a single token:
    BOOST_TEST(details::parse_number(".1+1").value() == .1);
    BOOST_TEST(details::parse_number("1e3").value() == 1e3);
    BOOST_TEST(details::parse_number(".123e1").value() == .123e1);
    BOOST_TEST(details::parse_number("123e-1").value() == 123e-1);
    BOOST_TEST(details::parse_number("123e+1").value() == 123e+1);
    BOOST_TEST(details::parse_number("1.e6").value() == 1.e6);
    BOOST_TEST(details::parse_number("2.3e-4").value() == 2.3e-4);
    // These are not numbers, but perhaps something else?
    BOOST_TEST(!details::parse_number(".").has_value());
    BOOST_TEST(!details::parse_number(".e3").has_value());
    BOOST_TEST(!details::parse_number("e12").has_value());
    // This is definitely a number, but a malformed one (an exponent must be
    // followed by some digits).
    BOOST_CHECK_THROW(details::parse_number("12e"), Error);
}

BOOST_AUTO_TEST_CASE(test_lexer_parse_const_token) {
    using namespace math::server::lexer;

    // TODO: No time to implement the required string conversions, hence the
    // extra parentheses.
    BOOST_TEST((details::parse_const_token("+").value() == Token::Type::PLUS));
    // parse_* functions only consume a single token:
    BOOST_TEST((details::parse_const_token("+++").value() == Token::Type::PLUS));
    BOOST_TEST((details::parse_const_token("-").value() == Token::Type::MINUS));
    BOOST_TEST(!details::parse_const_token("&+").has_value());
}

BOOST_AUTO_TEST_CASE(test_lexer_get_tokens) {
    using namespace math::server;
    using namespace math::server::lexer;

    // TODO: No time to implement the required string conversions, hence the
    // extra parentheses.
    {
        Lexer lexer{""};
        BOOST_TEST((lexer.get_tokens() == std::vector<Token>{}));
    }
    {
        Lexer lexer{" + - "};
        BOOST_TEST((lexer.get_tokens() == std::vector<Token>{
            Token{Token::Type::PLUS},
            Token{Token::Type::MINUS},
        }));
    }
    {
        Lexer lexer{"&"};
        BOOST_CHECK_THROW((lexer.get_tokens()), lexer::Error);
    }
    {
        Lexer lexer{" 1 + 123 & 456"};
        BOOST_CHECK_THROW((lexer.get_tokens()), lexer::Error);
    }
    {
        Lexer lexer{"1+2"};
        BOOST_TEST((lexer.get_tokens() == std::vector<Token>{
            Token{1},
            Token{Token::Type::PLUS},
            Token{2},
        }));
    }
    {
        Lexer lexer{"1+2 *  (3- 4e-2)"};
        BOOST_TEST((lexer.get_tokens() == std::vector<Token>{
            Token{1},
            Token{Token::Type::PLUS},
            Token{2},
            Token{Token::Type::ASTERISK},
            Token{Token::Type::LEFT_PAREN},
            Token{3},
            Token{Token::Type::MINUS},
            Token{4e-2},
            Token{Token::Type::RIGHT_PAREN},
        }));
    }
    {
        Lexer lexer{" 2 * (1 + 3 * (1 - -3)) "};
        BOOST_TEST((lexer.get_tokens() == std::vector<Token>{
            Token{2},
            Token{Token::Type::ASTERISK},
            Token{Token::Type::LEFT_PAREN},
            Token{1},
            Token{Token::Type::PLUS},
            Token{3},
            Token{Token::Type::ASTERISK},
            Token{Token::Type::LEFT_PAREN},
            Token{1},
            Token{Token::Type::MINUS},
            Token{Token::Type::MINUS},
            Token{3},
            Token{Token::Type::RIGHT_PAREN},
            Token{Token::Type::RIGHT_PAREN},
        }));
    }
}