-
Notifications
You must be signed in to change notification settings - Fork 9
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
base: master
Are you sure you want to change the base?
Changes from all commits
928ab63
58d7d8c
63aff00
519cc20
36effa8
8f16a19
a7858ad
9ca7e71
8b89e3d
e0b6886
1f6ca54
14d0a04
3859df5
88ad611
0415176
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 {}; | ||
} | ||
continue; | ||
} | ||
|
||
m_error = "input simbol: " + token + " not supported!"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
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}; | ||
}; |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} |
There was a problem hiding this comment.
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?