From 90bd600c5025ede4db99122f13dfb07b27de46ae Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 30 Nov 2019 01:38:08 +0300 Subject: initial commit --- test/unit_tests/CMakeLists.txt | 6 +++ test/unit_tests/lexer.cpp | 109 +++++++++++++++++++++++++++++++++++++++++ test/unit_tests/main.cpp | 2 + test/unit_tests/parser.cpp | 48 ++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 test/unit_tests/CMakeLists.txt create mode 100644 test/unit_tests/lexer.cpp create mode 100644 test/unit_tests/main.cpp create mode 100644 test/unit_tests/parser.cpp (limited to 'test/unit_tests') diff --git a/test/unit_tests/CMakeLists.txt b/test/unit_tests/CMakeLists.txt new file mode 100644 index 0000000..d320974 --- /dev/null +++ b/test/unit_tests/CMakeLists.txt @@ -0,0 +1,6 @@ +find_package(Boost REQUIRED) + +add_executable(unit_tests main.cpp lexer.cpp parser.cpp) +target_include_directories(unit_tests SYSTEM PRIVATE ${Boost_INCLUDE_DIRS}) + +target_include_directories(unit_tests PRIVATE ../..) 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 + +#include + +#include + +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{})); + } + { + Lexer lexer{" + - "}; + BOOST_TEST((lexer.get_tokens() == std::vector{ + 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{1}, + Token{Token::Type::PLUS}, + Token{2}, + })); + } + { + Lexer lexer{"1+2 * (3- 4e-2)"}; + BOOST_TEST((lexer.get_tokens() == std::vector{ + 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{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}, + })); + } +} diff --git a/test/unit_tests/main.cpp b/test/unit_tests/main.cpp new file mode 100644 index 0000000..5d8b492 --- /dev/null +++ b/test/unit_tests/main.cpp @@ -0,0 +1,2 @@ +#define BOOST_TEST_MODULE math_server tests +#include diff --git a/test/unit_tests/parser.cpp b/test/unit_tests/parser.cpp new file mode 100644 index 0000000..11f48d3 --- /dev/null +++ b/test/unit_tests/parser.cpp @@ -0,0 +1,48 @@ +#include + +#include + +BOOST_AUTO_TEST_CASE(test_parser_exec) { + using namespace math::server; + + { + Parser parser{""}; + BOOST_CHECK_THROW(parser.exec(), parser::Error); + } + { + Parser parser{"1"}; + BOOST_TEST(parser.exec() == 1); + } + { + Parser parser{" 1 + "}; + BOOST_CHECK_THROW(parser.exec(), parser::Error); + } + { + Parser parser{" 1 + 2 "}; + BOOST_TEST(parser.exec() == 3); + } + { + Parser parser{" 2 * 1 + 3 "}; + BOOST_TEST(parser.exec() == 5); + } + { + Parser parser{" 2 * (1 + 3) "}; + BOOST_TEST(parser.exec() == 8); + } + { + Parser parser{" 2 * (1 + 3 "}; + BOOST_CHECK_THROW(parser.exec(), parser::Error); + } + { + Parser parser{" 2 * (1 + 3) )"}; + BOOST_CHECK_THROW(parser.exec(), parser::Error); + } + { + Parser parser{" 2 * (1 + 3 * (1 - -3)) "}; + BOOST_TEST(parser.exec() == 26); + } + { + Parser parser{" -2 * ---- (3 + -100e-1) "}; // Looks weird, but also works in e.g. Python + BOOST_TEST(parser.exec() == 14); + } +} -- cgit v1.2.3