Skip to content

Commit

Permalink
Add gtest tests for comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
tfc committed Sep 30, 2024
1 parent a3b513d commit 62c3907
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)

enable_testing()
add_subdirectory(test)
add_subdirectory(gtest)
add_subdirectory(benchmark)


Expand Down
2 changes: 2 additions & 0 deletions build.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
, gcovr
, llvm
, ninja
, gtest
, doCheck ? true
}:

Expand All @@ -27,6 +28,7 @@ stdenv.mkDerivation {
checkInputs = [
catch2_3
gbenchmark
gtest
]
++ lib.optional stdenv.cc.isClang llvm; # for llvm-cov

Expand Down
12 changes: 12 additions & 0 deletions gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
144 changes: 144 additions & 0 deletions gtest/math_expression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#include <attoparsecpp/math_expression.hpp>

#include <gtest/gtest.h>

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());
}

0 comments on commit 62c3907

Please sign in to comment.