diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2019-11-30 01:38:08 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2019-12-01 05:23:11 +0300 |
commit | 90bd600c5025ede4db99122f13dfb07b27de46ae (patch) | |
tree | 0b581b43e5fcb54114c6c373352ed6ab5fcd61dc /test/unit_tests/lexer.cpp | |
download | math-server-90bd600c5025ede4db99122f13dfb07b27de46ae.tar.gz math-server-90bd600c5025ede4db99122f13dfb07b27de46ae.zip |
initial commit
Diffstat (limited to '')
-rw-r--r-- | test/unit_tests/lexer.cpp | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/test/unit_tests/lexer.cpp b/test/unit_tests/lexer.cpp new file mode 100644 index 0000000..7b513e8 --- /dev/null +++ b/test/unit_tests/lexer.cpp @@ -0,0 +1,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}, + })); + } +} |