Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculator: add calculator and unittest for tdd #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -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)
Expand Down
76 changes: 76 additions & 0 deletions calculator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "calculator.h"

namespace dev {

bool Calculator::isOperator(char op){
switch (op){
case '+':
case '-':
case '*':
case '/':
return true;
default:
return false;
}
}

void Calculator::getVariables(float& firstOperand, float& secondOperand)
{
firstOperand = mOperandStack.top();
mOperandStack.pop();
secondOperand = mOperandStack.top();
mOperandStack.pop();
}

double Calculator::evaluate(const std::string& expression)
{
double ret =0.0;
float firstOperand,secondOperand;
firstOperand=secondOperand=0.0;
for(int index=0;index < expression.length();index++)
{
// ignore the spaces
if (isspace(expression[index])){
continue;
}

// Push to stack, if digit
if(isdigit(expression[index]))
{
std::string digitsStr;

while (index < expression.size() && ((isdigit(expression[index]) || expression[index] == '.')))
{
digitsStr.push_back(expression[index]);
index++;
}

mOperandStack.push(stof(digitsStr));
// Decrease index as currently points to advanced element
index--;
}
// Check if operator and perform the operation of numbers from stack
else if (isOperator(expression[index])){
getVariables(firstOperand,secondOperand);
if (expression[index] == '+'){
ret = firstOperand+secondOperand;
}
if (expression[index] == '-'){
ret = secondOperand-firstOperand;
}
if (expression[index] == '*'){
ret = firstOperand*secondOperand;
}
if (expression[index] == '/'){
ret = secondOperand/firstOperand;
}

// push the final result
mOperandStack.push(ret);
}
}
// return answer
return mOperandStack.top();
}

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

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

/**
* method to evaluate the expression
* param expression string fetched from console input
*/
double evaluate(const std::string& expression);

private:

//Stack to hold the numbers
std::stack<float> mOperandStack;
bool isOperator(char op);
void getVariables(float& var1, float& var2);
};

} // namespace calculator
9 changes: 9 additions & 0 deletions main.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#include "run.h"
#include "project.h"
#include "calculator.h"
#include<iostream>

int main() {

dev::Calculator calc;
std::string s;
std::cout<<"Enter the string: ";
std::getline(std::cin,s);
std::cout<<"Result:"<<calc.evaluate(s)<<std::endl;

dev::Project p;
return run(p);
}
1 change: 0 additions & 1 deletion project.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "project.h"

namespace dev {

int Project::run() {
Expand Down
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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