Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
calculator: add calculator and unittest for tdd
Browse files Browse the repository at this point in the history
gannicus309 committed Jul 20, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent c7d5498 commit 29db33e
Showing 7 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
CMakeLists.txt.user*

.vscode
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ project(dummy_cmake_project)
set(CMAKE_CXX_STANDARD 14)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

set(SOURCES project.cc)
set(SOURCES project.cc calculator.cc)
add_library(ProjectLib ${SOURCES})

add_executable(project main.cc run.cc)
9 changes: 9 additions & 0 deletions calculator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "calculator.h"

namespace dev {

double Calculator::evaluate(std::string expression) {
return 0;
}

} // namespace calculator
14 changes: 14 additions & 0 deletions calculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include<string>

namespace dev {

class Calculator {
public:
Calculator() = default;
~Calculator() = default;

double evaluate(std::string expression);

};

} // namespace calculator
4 changes: 3 additions & 1 deletion project.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "project.h"

#include "calculator.h"
namespace dev {

int Project::run() {
Calculator calc;
calc.evaluate("2 3 +");
return 0;
}
} // namespace dev
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -8,3 +8,6 @@ target_link_libraries(ProjectTest PUBLIC ProjectLib)

add_executable (RunTest ${CMAKE_SOURCE_DIR}/run.cc run_tests.cc)
add_gtest(RunTest)

add_executable (CalculatorTest ${CMAKE_SOURCE_DIR}/calculator.cc calculator_test.cc)
add_gtest(CalculatorTest)
60 changes: 60 additions & 0 deletions test/calculator_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "calculator.h"

#include <gtest/gtest.h>

namespace dev {
namespace testing {

class CalculatorTest : public ::testing::Test {
public:
void SetUp() override {}
void TearDown() override {}
Calculator calc_;
};

TEST_F(CalculatorTest, Add) {
ASSERT_DOUBLE_EQ(7.0, calc_.evaluate("5 2 +"));
}

TEST_F(CalculatorTest, Subtract) {
ASSERT_DOUBLE_EQ(3.0, calc_.evaluate("5 2 -"));
}

TEST_F(CalculatorTest, SubtractNegative) {
ASSERT_DOUBLE_EQ(-3.0, calc_.evaluate("2 5 -"));
}

TEST_F(CalculatorTest, Multiply) {
ASSERT_DOUBLE_EQ(10.0, calc_.evaluate("5 2 *"));
}

TEST_F(CalculatorTest, CalcDiv) {
ASSERT_DOUBLE_EQ(2.5, calc_.evaluate("5 2 /"));
}

TEST_F(CalculatorTest, AddFloat) {
ASSERT_DOUBLE_EQ(5.0, calc_.evaluate("2.5 2.5 +"));
}

TEST_F(CalculatorTest, SubFloat) {
ASSERT_DOUBLE_EQ(-1.0, calc_.evaluate("1.5 2.5 -"));
}

TEST_F(CalculatorTest, MultFloat) {
ASSERT_DOUBLE_EQ(6.25, calc_.evaluate("2.5 2.5 *"));
}

TEST_F(CalculatorTest, Divfloat) {
ASSERT_DOUBLE_EQ(6.0, calc_.evaluate("15.0 2.5 /"));
}

TEST_F(CalculatorTest, MultipleOperator1) {
ASSERT_DOUBLE_EQ(2.25, calc_.evaluate("1.5 3 + 2.0 /"));
}

TEST_F(CalculatorTest, MultipleOperator2) {
ASSERT_DOUBLE_EQ(0, calc_.evaluate("2.5 2.5 - 4.0 *"));
}

} // namespace testing
} // namespace dev

0 comments on commit 29db33e

Please sign in to comment.