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
Prev Previous commit
Next Next commit
implement isNumber()
Goran Shekerov committed Jul 22, 2020
commit 9ca7e710d6155314dee076fa724176622fd78825
26 changes: 19 additions & 7 deletions src/Calculator/calculator.cpp
Original file line number Diff line number Diff line change
@@ -45,14 +45,14 @@ bool Calculator::parseInput(std::string input)
std::istream_iterator<std::string>());

for (auto token : tokens) {
if (isOperator(token.at(0)) && token.size() == 1) {

if (token.size() == 1 && isOperator(token.at(0))) {
m_operators.push_back(token.at(0));
continue;
}

auto operand = atof(token.c_str());
if (isNumber(operand)) {
m_operands.push_back(operand);
if (isNumber(token)) {
m_operands.push_back(atof(token.c_str()));
continue;
}

@@ -77,12 +77,24 @@ void Calculator::clear()


bool Calculator::isOperator(char oper) {
return m_availableOperators.find(oper) != m_availableOperators.end();
bool Ok = m_availableOperators.find(oper) != m_availableOperators.end();
return Ok;
}

bool Calculator::isNumber(double)
bool Calculator::isNumber(std::string token)
{
return true;
// 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;
}

bool Calculator::intermediateCalculation(u_int operator_idx) {
2 changes: 1 addition & 1 deletion src/Calculator/include/calculator.h
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ class Calculator{
bool parseInput(std::string input);
bool intermediateCalculation(u_int operator_idx);
bool isOperator(char oper);
bool isNumber(double);
bool isNumber(std::string token);
int maxOperatorsPriority();
int operatorPriority(const char& oper);

10 changes: 4 additions & 6 deletions test/calculator_test.cpp
Original file line number Diff line number Diff line change
@@ -63,9 +63,7 @@ TEST_F(CalculatorTest, ComplexPriorityOperationDevideByZero) {
ASSERT_EQ(calculator.error(), "division by zero is undefined");
}

//TEST_F(CalculatorTest, SimbolNotSuported) {
// ASSERT_EQ(calculator.calculate("2 3 #"), 0);
// // isOperand() returns true for # ??
// std::cout << calculator.error();
// ASSERT_EQ(calculator.error(), "input simbol: # not supported!");
//}
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!");
}