-
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
Open
GoranShekerov
wants to merge
15
commits into
alexkutsan:master
Choose a base branch
from
GoranShekerov:unit-test-init-goran
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
928ab63
init calculator unit test
58d7d8c
Init project backbone
63aff00
WIP: init Calculator
519cc20
inprove parsing input
36effa8
fix division by zero
8f16a19
Init complex operation
a7858ad
replace index names with more meaningfull names
9ca7e71
implement isNumber()
8b89e3d
Refactor calculator to comply with Polish notation
e0b6886
Extract poping operands
1f6ca54
extract tokenize()
14d0a04
Fix - swap operands order
3859df5
Fix zero devide
88ad611
Fix No need of break after return in swithch
0415176
Extract operand/operator handles
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,134 @@ | ||
#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 (isNumber(token)) { | ||
m_tmp_operands.push(atof(token.c_str())); | ||
continue; | ||
} | ||
|
||
if (isOperator(token.at(0))) { | ||
auto res = calculateNext(popOperands(), token.at(0)); | ||
|
||
if (!res) { | ||
return 0.0; | ||
} | ||
|
||
m_tmp_operands.push(res.value()); | ||
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; | ||
break; | ||
|
||
case '/' : | ||
if (op1 == 0) { | ||
GoranShekerov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
m_error = "result is indefinite"; | ||
return {}; | ||
} | ||
if (op2 == 0) { | ||
m_error = "division by zero is undefined"; | ||
return {}; | ||
} | ||
return op1 / op2; | ||
break; | ||
|
||
case '+' : | ||
return op1 + op2; | ||
break; | ||
GoranShekerov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
case '-' : | ||
return op1 - op2; | ||
break; | ||
|
||
default: | ||
return {}; | ||
} | ||
} | ||
|
||
Calculator::tokens Calculator::tokenize(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(char oper) const { | ||
return std::find(m_availableOperators.begin(), m_availableOperators.end(), oper) != m_availableOperators.end(); | ||
} | ||
|
||
bool Calculator::isNumber(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; | ||
} |
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,41 @@ | ||
#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); | ||
|
||
tokens tokenize(std::string input) const; | ||
bool isOperator(char oper) const; | ||
bool isNumber(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}; | ||
}; |
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,66 @@ | ||
#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); | ||
ASSERT_EQ(calculator.error(), "result is indefinite"); | ||
} | ||
|
||
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); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
suggest to thing about continue refactoring of the for loop