xaizek / uncov (License: AGPLv3+) (since 2018-12-07)
Uncov(er) is a tool that collects and processes code coverage reports.
<root> / src / coverage.cpp (b06116ea2daeaf0ebdb92da73fb9dd03f9e11ab5) (2,680B) (mode 100644) [raw]
// Copyright (C) 2016 xaizek <xaizek@posteo.net>
//
// This file is part of uncov.
//
// uncov is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// uncov is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with uncov.  If not, see <http://www.gnu.org/licenses/>.

#include "coverage.hpp"

#include <iomanip>
#include <sstream>
#include <string>

#include "printing.hpp"

void
CovInfo::add(const CovInfo &other)
{
    coveredCount += other.coveredCount;
    missedCount += other.missedCount;
}

std::string
CovInfo::formatCoverageRate() const
{
    const float coverage = (getRelevantLines() == 0) ? 100.0f : getCoverage();

    std::ostringstream oss;
    oss << std::fixed << std::right
        << std::setprecision(2) << Coverage{coverage};
    return oss.str();
}

std::string
CovInfo::formatLines(const std::string &separator) const
{
    return std::to_string(coveredCount) + separator
         + std::to_string(getRelevantLines());
}

float
CovInfo::getCoverage() const
{
    if (getRelevantLines() == 0) {
        // Return 100 instead of NaN here to make it easier for CovChange.
        return 100.0f;
    }
    return (100.0f*coveredCount)/getRelevantLines();
}

int
CovInfo::getRelevantLines() const
{
    return coveredCount + missedCount;
}

CovChange::CovChange(const CovInfo &oldCov, const CovInfo &newCov)
{
    coverageChange = newCov.getCoverage() - oldCov.getCoverage();
    coveredChange = newCov.coveredCount - oldCov.coveredCount;
    missedChange = newCov.missedCount - oldCov.missedCount;
    relevantChange = newCov.getRelevantLines() - oldCov.getRelevantLines();
}

bool
CovChange::isChanged() const
{
    return coveredChange != 0 || missedChange != 0;
}

std::string
CovChange::formatCoverageRate() const
{
    std::ostringstream oss;
    oss << std::fixed << std::right
        << std::setprecision(4) << CoverageChange{coverageChange};
    return oss.str();
}

std::string
CovChange::formatLines(const std::string &separator, int width) const
{
    std::ostringstream oss;
    oss << CLinesChange{coveredChange} << separator << std::setw(width)
        << MLinesChange{missedChange} << separator << std::setw(width)
        << RLinesChange{relevantChange};
    return oss.str();
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/uncov

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/uncov

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master