From 62c3907d6b57dd9b25c5c4d9f8fd43fb6ebbb19b Mon Sep 17 00:00:00 2001 From: Jacek Galowicz Date: Mon, 30 Sep 2024 17:31:48 +0200 Subject: [PATCH] Add gtest tests for comparison --- CMakeLists.txt | 1 + build.nix | 2 + gtest/CMakeLists.txt | 12 ++++ gtest/math_expression.cpp | 144 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+) create mode 100644 gtest/CMakeLists.txt create mode 100644 gtest/math_expression.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c2e7e0..9282714 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17) enable_testing() add_subdirectory(test) +add_subdirectory(gtest) add_subdirectory(benchmark) diff --git a/build.nix b/build.nix index 011f4ab..2b57f1c 100644 --- a/build.nix +++ b/build.nix @@ -7,6 +7,7 @@ , gcovr , llvm , ninja +, gtest , doCheck ? true }: @@ -27,6 +28,7 @@ stdenv.mkDerivation { checkInputs = [ catch2_3 gbenchmark + gtest ] ++ lib.optional stdenv.cc.isClang llvm; # for llvm-cov diff --git a/gtest/CMakeLists.txt b/gtest/CMakeLists.txt new file mode 100644 index 0000000..7bc7311 --- /dev/null +++ b/gtest/CMakeLists.txt @@ -0,0 +1,12 @@ +include(CTest) + +find_package(GTest REQUIRED) + +add_executable(${PROJECT_NAME}-gtest + math_expression.cpp + ) +target_link_libraries(${PROJECT_NAME}-gtest ${PROJECT_NAME} GTest::gtest_main) +target_compile_features(${PROJECT_NAME}-gtest INTERFACE cxx_std_17) +target_compile_options(${PROJECT_NAME}-gtest PRIVATE -Wall -Wextra -Werror) + +catch_discover_tests(${PROJECT_NAME}-gtest) diff --git a/gtest/math_expression.cpp b/gtest/math_expression.cpp new file mode 100644 index 0000000..db11de2 --- /dev/null +++ b/gtest/math_expression.cpp @@ -0,0 +1,144 @@ +#include + +#include + +using namespace apl; + +TEST(MathExpressionParser, AddOpParserEmptyString) { + const auto r{run_parser(add_op, "")}; + EXPECT_FALSE(!!r.first); +} + +TEST(MathExpressionParser, AddOpParserNonOpString) { + const auto r{run_parser(add_op, "a")}; + EXPECT_FALSE(!!r.first); +} + +TEST(MathExpressionParser, AddOpParserPlusString) { + const auto r{run_parser(add_op, "+")}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ((*r.first)(100, 10), 110); +} + +TEST(MathExpressionParser, AddOpParserMinusString) { + const auto r{run_parser(add_op, "-")}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ((*r.first)(100, 10), 90); +} + +TEST(MathExpressionParser, MulOpParserEmptyString) { + const auto r{run_parser(mul_op, "")}; + EXPECT_FALSE(!!r.first); +} + +TEST(MathExpressionParser, MulOpParserNonOpString) { + const auto r{run_parser(mul_op, "a")}; + EXPECT_FALSE(!!r.first); +} + +TEST(MathExpressionParser, MulOpParserMultiplicationString) { + const auto r{run_parser(mul_op, "*")}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ((*r.first)(10, 2), 20); +} + +TEST(MathExpressionParser, MulOpParserDivisionString) { + const auto r{run_parser(mul_op, "/")}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ((*r.first)(10, 2), 5); +} + +TEST(MathExpressionParser, ExprParserEmptyString) { + const auto r{run_parser(expr, "")}; + EXPECT_FALSE(!!r.first); +} + +TEST(MathExpressionParser, ExprParserSingleInteger) { + const auto r{run_parser(expr, "123")}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ(r.first, 123); +} + +TEST(MathExpressionParser, ExprParserTwoIntsSum) { + const auto r{run_parser(expr, "123 + 456")}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ(r.first, 579); +} + +TEST(MathExpressionParser, ExprParserFiveIntsSumAndSub) { + const auto r{run_parser(expr, "1 + 2 + 3 - 4 - 5")}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ(r.first, -3); +} + +TEST(MathExpressionParser, ExprParserTwoIntsProduct) { + const auto r{run_parser(expr, "10 * 5")}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ(r.first, 50); +} + +TEST(MathExpressionParser, ExprParserFiveIntsProductAndDiv) { + const auto r{run_parser(expr, "1 * 2 * 3 * 4 / 2")}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ(r.first, 12); +} + +TEST(MathExpressionParser, ExprParserMultiplicationThenAddition) { + const auto r{run_parser(expr, "2 * 3 + 5")}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ(r.first, 11); +} + +TEST(MathExpressionParser, ExprParserAdditionThenMultiplication) { + const auto r{run_parser(expr, "2 + 3 * 5")}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ(r.first, 17); +} + +TEST(MathExpressionParser, ExprParserComplexParentheses) { + const auto r{run_parser(expr, "((((((2))))))")}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ(r.first, 2); +} + +TEST(MathExpressionParser, ExprParserMixedSubExpressions) { + const auto r{run_parser(expr, "(2 * 3) + 5")}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ(r.first, 11); +} + +TEST(MathExpressionParser, ExprParserAdditionWithinParentheses) { + const auto r{run_parser(expr, "2 * (3 + 5)")}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ(r.first, 16); +} + +TEST(MathExpressionParser, ExprParserMoreComplexExpression) { + const auto r{run_parser(expr, "1 + (2 * (4 + 3) + 12 * 12 - (6 / 3))")}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ(r.first, 157); +} + +TEST(MathExpressionParser, ExprParserTrailingOperationSingleInt) { + const std::string str{"123 +"}; + const auto r{run_parser(expr, str)}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ(r.first, 123); + EXPECT_TRUE(r.second.at_end()); +} + +TEST(MathExpressionParser, ExprParserTrailingOperationTwoInts) { + const std::string str{"123 - 0 +"}; + const auto r{run_parser(expr, str)}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ(r.first, 123); + EXPECT_TRUE(r.second.at_end()); +} + +TEST(MathExpressionParser, ExprParserTrailingOperationComplexExpr) { + const std::string str{"1 + (2 * (4 + 3) + 12 * 12 - (6 / 3)) +"}; + const auto r{run_parser(expr, str)}; + ASSERT_TRUE(!!r.first); + EXPECT_EQ(r.first, 157); + EXPECT_TRUE(r.second.at_end()); +}