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

init calculator unit test #10

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.5.1)
project(dummy_cmake_project)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

set(SOURCES project.cc)
Expand Down
140 changes: 140 additions & 0 deletions src/Calculator/calculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#include "include/calculator.h"
#include <iostream>
#include <algorithm>
#include <sstream>
#include <iterator>

Calculator::Calculator()
{
}

double Calculator::calculate(std::string input)
{
clear();

for (auto token : tokenize(input)) {

if (isOperand(token)) {
handleOperandToken(token);
continue;
}

if (isOperator(token)) {
if (!handleOperatorToken(token)) {
return {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case if there is wrong input, user will see 0 as calculation result?

}
continue;
}

m_error = "input simbol: " + token + " not supported!";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest to extract token validation to separate loop and use calculation loop without error handling for this.

return 0.0;
}

return m_tmp_operands.top();
}

std::string Calculator::error() const
{
return m_error;
}

void Calculator::clear()
{
m_tmp_operands = {};
m_error.clear();
}

Calculator::operands Calculator::popOperands()
{
if (m_tmp_operands.size() < min_operandsNumber) {
m_error = "wrong input string!";
return {};
}

auto op2 = m_tmp_operands.top();
m_tmp_operands.pop();
auto op1 = m_tmp_operands.top();
m_tmp_operands.pop();

return std::make_pair(op1, op2);
}

Calculator::result Calculator::calculateNext(operands operands, char oper)
{
auto op1 = operands.first;
auto op2 = operands.second;

switch (oper) {

case '*' :
return op1 * op2;

case '/' :
if (op2 == 0) {
m_error = "division by zero is undefined";
return {};
}
return op1 / op2;

case '+' :
return op1 + op2;

case '-' :
return op1 - op2;

default:
return {};
}
}

void Calculator::handleOperandToken(const std::string &token)
{
m_tmp_operands.push(atof(token.c_str()));
}

Calculator::result Calculator::handleOperatorToken(const std::string &token)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do assertion in places where you need

{
auto res = calculateNext(popOperands(), token.at(0));

if (!res) {
return {};
}

m_tmp_operands.push(res.value());
return res;
}

Calculator::tokens Calculator::tokenize(const std::string &input) const
{
std::istringstream inputStream(input);
std::vector<std::string> tokens(std::istream_iterator<std::string>{inputStream},
std::istream_iterator<std::string>());

if (tokens.empty()) {
return {};
}

return tokens;
}


bool Calculator::isOperator(const std::string &oper) const {
auto _oper = oper.at(0);
return std::find(m_availableOperators.begin(), m_availableOperators.end(), _oper) != m_availableOperators.end();
}

bool Calculator::isOperand(std::string token) const
{
// consider negative numbers
if (token.at(0) == '-') {
token.erase(0, 1);
}
bool isDigit = !token.empty() &&
std::find_if(token.begin(),
token.end(),
[](unsigned char c) {
return !std::isdigit(c);
}
) == token.end();
return isDigit;
}
43 changes: 43 additions & 0 deletions src/Calculator/include/calculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <string>
#include <array>
#include <stack>
#include <optional>
#include <utility>
#include <vector>

/**
* @brief Polish notation calculator
*/

class Calculator{

public:
Calculator();

double calculate(std::string input);
void clear();
std::string error() const;

private:
using result = std::optional<double>;
using operands = std::pair<double, double>;
using tokens = std::vector<std::string>;

GoranShekerov marked this conversation as resolved.
Show resolved Hide resolved
private:
operands popOperands();
result calculateNext(operands operands, char oper);
void handleOperandToken(const std::string& token);
result handleOperatorToken(const std::string& token);

tokens tokenize(const std::string& input) const;
bool isOperator(const std::string& oper) const;
bool isOperand(std::string token) const;

std::stack<double> m_tmp_operands;
std::string m_error;

static constexpr std::array<char, 4> m_availableOperators{'*', '/', '+', '-'};
static constexpr int min_operandsNumber{2};
};
5 changes: 5 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ target_link_libraries(ProjectTest PUBLIC ProjectLib)

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

add_executable(CalculatorTest
${CMAKE_CURRENT_SOURCE_DIR}/calculator_test.cpp
${CMAKE_SOURCE_DIR}/src/Calculator/calculator.cpp)
add_gtest(CalculatorTest)
65 changes: 65 additions & 0 deletions test/calculator_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <gtest/gtest.h>

#include "../src/Calculator/include/calculator.h"

#include <iostream>

class CalculatorTest : public ::testing::Test {

public:
Calculator calculator;
};

TEST_F(CalculatorTest, AddPositive) {
ASSERT_EQ(calculator.calculate("2 3 +"), 5);
}

TEST_F(CalculatorTest, AddNegative) {
ASSERT_EQ(calculator.calculate("-2 -3 +"), -5);
}

TEST_F(CalculatorTest, Substract) {
ASSERT_EQ(calculator.calculate("5 2 -"), 3);
}

TEST_F(CalculatorTest, Multiply) {
ASSERT_EQ(calculator.calculate("3 7 *"), 21);
}

TEST_F(CalculatorTest, Devide) {
ASSERT_EQ(calculator.calculate("21 7 /"), 3);
}

TEST_F(CalculatorTest, MultiplyByZero) {
ASSERT_EQ(calculator.calculate("21 0 *"), 0);
}

TEST_F(CalculatorTest, DevideByZero) {
ASSERT_EQ(calculator.calculate("21 0 /"), 0);
ASSERT_EQ(calculator.error(), "division by zero is undefined");
}

TEST_F(CalculatorTest, ComplexAdd) {
ASSERT_EQ(calculator.calculate("2 3 6 + +"), 11);
}

TEST_F(CalculatorTest, ComplexPriorityOperation) {
ASSERT_EQ(calculator.calculate("2 3 6 + *"), 18);
}

TEST_F(CalculatorTest, ComplexPriorityOperationMultiplyByZero) {
ASSERT_EQ(calculator.calculate("2 3 0 + *"), 6);
}

TEST_F(CalculatorTest, ComplexPriorityOperationIndefinite) {
ASSERT_EQ(calculator.calculate("0 3 2 + /"), 0);
}

TEST_F(CalculatorTest, SimbolNotSuported) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not enough tests for negative test cases

ASSERT_EQ(calculator.calculate("2 3 #"), 0);
ASSERT_EQ(calculator.error(), "input simbol: # not supported!");
}

TEST_F(CalculatorTest, VeryComplex) {
ASSERT_EQ(calculator.calculate("4 12 3 + * 2 / 5 5 + * 100 2 * - 2 /"), 50);
}