diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/CMakeLists.txt | 1 | ||||
-rw-r--r-- | test/unit_tests/CMakeLists.txt | 6 | ||||
-rw-r--r-- | test/unit_tests/lexer.cpp | 109 | ||||
-rw-r--r-- | test/unit_tests/main.cpp | 2 | ||||
-rw-r--r-- | test/unit_tests/parser.cpp | 48 |
5 files changed, 166 insertions, 0 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..bed23ce --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(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 <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}, + })); + } +} 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 <boost/test/included/unit_test.hpp> 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 <server/parser.hpp> + +#include <boost/test/unit_test.hpp> + +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); + } +} |