-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
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
1 parent
c7d5498
commit 29db33e
Showing
7 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
build | ||
CMakeLists.txt.user* | ||
|
||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |