< Summary - calculator unit test code coverage (Linux)

Information
Class: /home/runner/work/cpp-cmake-ctest-template/cpp-cmake-ctest-template/src/calculator/src/expressionParser.cpp
Assembly: Default
File(s): /home/runner/work/cpp-cmake-ctest-template/cpp-cmake-ctest-template/src/calculator/src/expressionParser.cpp
Line coverage
98%
Covered lines: 90
Uncovered lines: 1
Coverable lines: 91
Total lines: 143
Line coverage: 98.9%
Branch coverage
74%
Covered branches: 148
Total branches: 199
Branch coverage: 74.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 09/30/2026 - 03:06:01 Line coverage: 98.9% (90/91) Branch coverage: 74.3% (148/199) Total lines: 143 09/30/2026 - 03:06:01 Line coverage: 98.9% (90/91) Branch coverage: 74.3% (148/199) Total lines: 143

File(s)

/home/runner/work/cpp-cmake-ctest-template/cpp-cmake-ctest-template/src/calculator/src/expressionParser.cpp

#LineLine coverage
 1#include "../header/expressionParser.h"
 2#include "../header/calculator.h"
 3
 4#include <cctype>
 5#include <sstream>
 6#include <stack>
 7#include <stdexcept>
 8
 9using namespace Coruh::Calculator;
 10
 15811bool ExpressionParser::isOperator(char c) {
 15812    return (c == '+' || c == '-' || c == '*' || c == '/');
 13}
 14
 1415int ExpressionParser::precedence(char c) {
 1416    if(c == '+' || c == '-') return 1;
 717    if(c == '*' || c == '/') return 2;
 018    return 0;
 19}
 20
 3821std::string ExpressionParser::toPostfix(const std::string& infix) {
 3822    std::stack<char> s;
 3823    std::ostringstream postfix;
 3824    bool expectOperand = true;  // true at the start, after an operator and after '('
 25
 18226    for(size_t i = 0; i < infix.size(); ++i) {
 15627        char c = infix[i];
 28
 15629        if(std::isdigit(static_cast<unsigned char>(c)) || c == '.') {
 7130            if(!expectOperand) {
 131                throw std::invalid_argument("Missing operator between numbers.");
 32            }
 33            // read the whole number (digits and at most one decimal point)
 7034            std::string number;
 7035            bool seenDot = false;
 16136            while(i < infix.size() && (std::isdigit(static_cast<unsigned char>(infix[i])) || infix[i] == '.')) {
 9237                if(infix[i] == '.') {
 1038                    if(seenDot) {
 139                        throw std::invalid_argument("A number has more than one decimal point.");
 40                    }
 941                    seenDot = true;
 42                }
 9143                number += infix[i++];
 44            }
 6945            --i;
 6946            if(number == ".") {
 247                throw std::invalid_argument("A decimal point without digits is not a number.");
 48            }
 6749            postfix << number << ' ';
 6750            expectOperand = false;
 15551        } else if(isOperator(c)) {
 4252            if(expectOperand) {
 253                throw std::invalid_argument("An operator is missing its left operand.");
 54            }
 4355            while(!s.empty() && s.top() != '(' && precedence(s.top()) >= precedence(c)) {
 356                postfix << s.top() << ' ';
 357                s.pop();
 58            }
 4059            s.push(c);
 4060            expectOperand = true;
 4361        } else if(c == '(') {
 1362            if(!expectOperand) {
 163                throw std::invalid_argument("Missing operator before '('.");
 64            }
 1265            s.push(c);
 3066        } else if(c == ')') {
 1267            if(expectOperand) {
 268                throw std::invalid_argument("Empty parentheses or an operator before ')'.");
 69            }
 1870            while(!s.empty() && s.top() != '(') {
 871                postfix << s.top() << ' ';
 872                s.pop();
 73            }
 1074            if(s.empty()) {
 175                throw std::invalid_argument("Unmatched ')'.");
 76            }
 977            s.pop();  // discard '('
 1878        } else if(!std::isspace(static_cast<unsigned char>(c))) {
 679            throw std::invalid_argument(std::string("Unexpected character '") + c + "'.");
 80        }
 81    }
 82
 2683    if(expectOperand) {
 384        throw std::invalid_argument(infix.find_first_not_of(" \t\r\n") == std::string::npos
 85                                    ? "The expression is empty."
 386                                    : "The expression ends with an operator.");
 87    }
 88
 4889    while(!s.empty()) {
 2690        if(s.top() == '(') {
 191            throw std::invalid_argument("Unmatched '('.");
 92        }
 2593        postfix << s.top() << ' ';
 2594        s.pop();
 95    }
 96
 4497    return postfix.str();
 5498}
 99
 23100double ExpressionParser::evaluatePostfix(const std::string& postfix) {
 23101    std::stack<double> s;
 23102    std::istringstream iss(postfix);
 23103    std::string token;
 104
 105105    while(iss >> token) {
 85106        if(token.size() == 1 && isOperator(token[0])) {
 31107            if(s.size() < 2) {
 1108                throw std::invalid_argument("Not enough operands for '" + token + "'.");
 109            }
 30110            double b = s.top(); s.pop();
 30111            double a = s.top(); s.pop();
 30112            double result = 0.0;
 113
 30114            switch(token[0]) {
 13115                case '+': result = Calculator::add(a, b); break;
 5116                case '-': result = Calculator::subtract(a, b); break;
 7117                case '*': result = Calculator::multiply(a, b); break;
 5118                case '/':
 5119                    if (b == 0) {
 1120                        throw std::invalid_argument("Division by zero is not allowed.");
 121                    }
 4122                    result = Calculator::divide(a, b); break;
 123            }
 124
 29125            s.push(result);
 126        } else {
 127            try {
 54128                s.push(std::stod(token));
 1129            } catch(const std::exception&) {
 1130                throw std::invalid_argument("'" + token + "' is not a valid number.");
 1131            }
 132        }
 133    }
 134
 20135    if(s.size() != 1) {
 1136        throw std::invalid_argument("The expression is not well formed.");
 137    }
 38138    return s.top();
 31139}
 140
 36141double ExpressionParser::evaluateInfix(const std::string& infix) {
 36142    return evaluatePostfix(toPostfix(infix));
 143}