Skip to content

Commit

Permalink
Add CodeCoverageRunnerTest.OptimizedBuild
Browse files Browse the repository at this point in the history
  • Loading branch information
OpenCppCoverage committed Jan 26, 2017
1 parent f963798 commit 797811e
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 10 deletions.
16 changes: 16 additions & 0 deletions CppCoverage.sln
Expand Up @@ -48,6 +48,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FileFilterTest", "FileFilte
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ToolsTest", "ToolsTest\ToolsTest.vcxproj", "{42F32EB3-42B6-498A-9823-26E6E5982EE3}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestCoverageOptimizedBuild", "TestCoverageOptimizedBuild\TestCoverageOptimizedBuild.vcxproj", "{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -252,6 +254,20 @@ Global
{42F32EB3-42B6-498A-9823-26E6E5982EE3}.Release|Win32.Build.0 = Release|Win32
{42F32EB3-42B6-498A-9823-26E6E5982EE3}.Release|x64.ActiveCfg = Release|x64
{42F32EB3-42B6-498A-9823-26E6E5982EE3}.Release|x64.Build.0 = Release|x64
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Debug|Any CPU.ActiveCfg = Debug|Win32
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Debug|Mixed Platforms.Build.0 = Debug|Win32
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Debug|Win32.ActiveCfg = Debug|Win32
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Debug|Win32.Build.0 = Debug|Win32
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Debug|x64.ActiveCfg = Debug|x64
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Debug|x64.Build.0 = Debug|x64
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Release|Any CPU.ActiveCfg = Release|Win32
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Release|Mixed Platforms.ActiveCfg = Release|Win32
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Release|Mixed Platforms.Build.0 = Release|Win32
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Release|Win32.ActiveCfg = Release|Win32
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Release|Win32.Build.0 = Release|Win32
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Release|x64.ActiveCfg = Release|x64
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion CppCoverage/CodeCoverageRunner.cpp
Expand Up @@ -62,7 +62,7 @@ namespace CppCoverage
coverageFilterManager_ = std::make_unique<CoverageFilterManager>(
settings.GetCoverageFilterSettings(),
settings.GetUnifiedDiffSettings(),
false);
settings.GetOptimizedBuildSupport());
const auto& startInfo = settings.GetStartInfo();
int exitCode = debugger.Debug(startInfo, *this);
const auto& path = startInfo.GetPath();
Expand Down
13 changes: 13 additions & 0 deletions CppCoverage/RunCoverageSettings.cpp
Expand Up @@ -30,6 +30,7 @@ namespace CppCoverage
, coverChildren_{ false }
, continueAfterCppException_{ false }
, maxUnmatchPathsForWarning_{ 0 }
, optimizedBuildSupport_{ false }
{
}

Expand All @@ -51,6 +52,12 @@ namespace CppCoverage
maxUnmatchPathsForWarning_ = maxUnmatchPathsForWarning;
}

//-------------------------------------------------------------------------
void RunCoverageSettings::SetOptimizedBuildSupport(bool optimizedBuildSupport)
{
optimizedBuildSupport_ = optimizedBuildSupport;
}

//-------------------------------------------------------------------------
const StartInfo& RunCoverageSettings::GetStartInfo() const
{
Expand Down Expand Up @@ -86,4 +93,10 @@ namespace CppCoverage
{
return maxUnmatchPathsForWarning_;
}

//-------------------------------------------------------------------------
bool RunCoverageSettings::GetOptimizedBuildSupport() const
{
return optimizedBuildSupport_;
}
}
3 changes: 3 additions & 0 deletions CppCoverage/RunCoverageSettings.hpp
Expand Up @@ -38,13 +38,15 @@ namespace CppCoverage
void SetCoverChildren(bool);
void SetContinueAfterCppException(bool);
void SetMaxUnmatchPathsForWarning(size_t);
void SetOptimizedBuildSupport(bool);

const StartInfo& GetStartInfo() const;
const CoverageFilterSettings& GetCoverageFilterSettings() const;
const std::vector<UnifiedDiffSettings>& GetUnifiedDiffSettings() const;
bool GetCoverChildren() const;
bool GetContinueAfterCppException() const;
size_t GetMaxUnmatchPathsForWarning() const;
bool GetOptimizedBuildSupport() const;

private:
StartInfo startInfo_;
Expand All @@ -53,5 +55,6 @@ namespace CppCoverage
bool coverChildren_;
bool continueAfterCppException_;
size_t maxUnmatchPathsForWarning_;
bool optimizedBuildSupport_;
};
}
51 changes: 44 additions & 7 deletions CppCoverageTest/CodeCoverageRunnerTest.cpp
Expand Up @@ -50,6 +50,7 @@
#include "TestCoverageConsole/TestDiff.hpp"

#include "TestCoverageSharedLib/TestCoverageSharedLib.hpp"
#include "TestCoverageOptimizedBuild/TestCoverageOptimizedBuild.hpp"

#include "TestTools.hpp"

Expand Down Expand Up @@ -96,7 +97,8 @@ namespace CppCoverageTest
const std::vector<std::wstring>& sourcePatternCollection,
const std::vector<cov::UnifiedDiffSettings>& unifiedDiffSettingsCollection = {},
bool coverChildren = true,
bool continueAfterCppException = false)
bool continueAfterCppException = false,
bool optimizedBuildSupport = false)
{
cov::CodeCoverageRunner codeCoverageRunner;
cov::Patterns modulePatterns{false};
Expand Down Expand Up @@ -126,6 +128,7 @@ namespace CppCoverageTest
unifiedDiffSettingsCollection);
settings.SetCoverChildren(coverChildren);
settings.SetContinueAfterCppException(continueAfterCppException);
settings.SetOptimizedBuildSupport(optimizedBuildSupport);

auto coverageData = codeCoverageRunner.RunCoverage(settings);

Expand All @@ -142,15 +145,17 @@ namespace CppCoverageTest
const std::wstring& sourcePattern,
const std::vector<cov::UnifiedDiffSettings>& unifiedDiffSettingsCollection = {},
bool coverChildren = true,
bool continueAfterCppException = false)
bool continueAfterCppException = false,
bool optimizedBuildSupport = false)
{
return ComputeCoverageDataPatterns(
arguments,
{ modulePattern },
{ sourcePattern },
unifiedDiffSettingsCollection,
coverChildren,
continueAfterCppException);
continueAfterCppException,
optimizedBuildSupport);
}

//---------------------------------------------------------------------
Expand Down Expand Up @@ -223,7 +228,15 @@ namespace CppCoverageTest
std::wstring GetError() const
{
return Tools::LocalToWString(error_->str());
}
}

//-------------------------------------------------------------------------
intptr_t CountExecutedLines(const cov::FileCoverage& file)
{
auto lines = file.GetLines();
return boost::count_if(lines,
[](const auto& line) { return line.HasBeenExecuted(); });
}

private:
boost::shared_ptr<std::ostringstream> error_;
Expand Down Expand Up @@ -460,8 +473,32 @@ namespace CppCoverageTest
const auto& file = GetFirstFileCoverage(coverageData);
auto filename = file.GetPath().filename().wstring();
ASSERT_TRUE(boost::algorithm::iequals(unloadDllFilename, filename));
auto lines = file.GetLines();
ASSERT_NE(0, boost::count_if(lines,
[](const auto& line) { return line.HasBeenExecuted(); }));
ASSERT_NE(0, CountExecutedLines(file));
}

//-------------------------------------------------------------------------
TEST_F(CodeCoverageRunnerTest, OptimizedBuild)
{
auto computeCoverage = [&](bool optimizedBuild)
{
return ComputeCoverageData(
{ TestCoverageConsole::TestOptimizedBuild },
TestCoverageOptimizedBuild::GetOutputBinaryPath().wstring(),
TestCoverageOptimizedBuild::GetMainCppPath().wstring(),
{}, false, false, optimizedBuild);
};

auto coverageData = computeCoverage(false);
ASSERT_NE(0, coverageData.GetExitCode());

auto coverageDataOptimizedBuild = computeCoverage(true);
ASSERT_EQ(0, coverageDataOptimizedBuild.GetExitCode());

const auto& fileOptimizedBuild = GetFirstFileCoverage(coverageDataOptimizedBuild);
const auto& file = GetFirstFileCoverage(coverageData);
auto optimizedBuildCount = CountExecutedLines(fileOptimizedBuild);
auto count = CountExecutedLines(file);

ASSERT_GT(optimizedBuildCount, count);
}
}
3 changes: 3 additions & 0 deletions CppCoverageTest/CppCoverageTest.vcxproj
Expand Up @@ -208,6 +208,9 @@
<ProjectReference Include="..\FileFilter\FileFilter.vcxproj">
<Project>{6fd7c5be-04bd-496d-a924-285a3e867814}</Project>
</ProjectReference>
<ProjectReference Include="..\TestCoverageOptimizedBuild\TestCoverageOptimizedBuild.vcxproj">
<Project>{2fd17ba1-0e02-49e6-84c3-0a8f63fef871}</Project>
</ProjectReference>
<ProjectReference Include="..\TestCoverageSharedLib\TestCoverageSharedLib.vcxproj">
<Project>{0481b51c-98f1-4e92-a51e-162b77ecd939}</Project>
</ProjectReference>
Expand Down
3 changes: 3 additions & 0 deletions TestCoverageConsole/TestCoverageConsole.cpp
Expand Up @@ -21,6 +21,7 @@
#include <iostream>

#include "TestCoverageSharedLib/TestCoverageSharedLib.hpp"
#include "TestCoverageOptimizedBuild/TestCoverageOptimizedBuild.hpp"

#include "SpecialLineInfo.hpp"
#include "TestCoverageConsole.hpp"
Expand Down Expand Up @@ -86,6 +87,8 @@ int _tmain(int argc, _TCHAR* argv[])
TestCoverageConsole::UnloadReloadDll();
else if (type == TestCoverageConsole::TestDiff)
TestCoverageConsole::FilterByDiff();
else if (type == TestCoverageConsole::TestOptimizedBuild)
TestCoverageOptimizedBuild::TestOptimizedBuild();
else
std::wcerr << L"Unsupported type:" << type << std::endl;
}
Expand Down
5 changes: 3 additions & 2 deletions TestCoverageConsole/TestCoverageConsole.hpp
Expand Up @@ -49,13 +49,13 @@ namespace TestCoverageConsole
//-------------------------------------------------------------------------
inline int GetTestCoverageConsoleCppMainStartLine()
{
return 55;
return 56;
}

//-------------------------------------------------------------------------
inline int GetTestCoverageConsoleCppMainReturnLine()
{
return GetTestCoverageConsoleCppMainStartLine() + 37;
return GetTestCoverageConsoleCppMainStartLine() + 39;
}

const std::wstring TestBasic = L"TestBasic";
Expand All @@ -70,4 +70,5 @@ namespace TestCoverageConsole
const std::wstring TestSpecialLineInfo = L"TestSpecialLineInfo";
const std::wstring TestUnloadReloadDll = L"TestUnloadReloadDll";
const std::wstring TestDiff = L"TestDiff";
const std::wstring TestOptimizedBuild = L"TestOptimizedBuild";
}
3 changes: 3 additions & 0 deletions TestCoverageConsole/TestCoverageConsole.vcxproj
Expand Up @@ -177,6 +177,9 @@
<ClCompile Include="TestThread.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TestCoverageOptimizedBuild\TestCoverageOptimizedBuild.vcxproj">
<Project>{2fd17ba1-0e02-49e6-84c3-0a8f63fef871}</Project>
</ProjectReference>
<ProjectReference Include="..\TestCoverageSharedLib\TestCoverageSharedLib.vcxproj">
<Project>{0481b51c-98f1-4e92-a51e-162b77ecd939}</Project>
</ProjectReference>
Expand Down
59 changes: 59 additions & 0 deletions TestCoverageOptimizedBuild/TestCoverageOptimizedBuild.cpp
@@ -0,0 +1,59 @@
// OpenCppCoverage is an open source code coverage for C++.
// Copyright (C) 2017 OpenCppCoverage
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "TestCoverageOptimizedBuild.hpp"

#include <memory>

namespace TestCoverageOptimizedBuild
{
//-------------------------------------------------------------------------
boost::filesystem::path GetMainCppPath()
{
return __FILE__;
}

//-------------------------------------------------------------------------
boost::filesystem::path GetOutputBinaryPath()
{
return boost::filesystem::path(OUT_DIR) / TARGET_FILE_NAME;
}

//-------------------------------------------------------------------------
class CrashInOptimizedBuild
{
public:
explicit CrashInOptimizedBuild(int type)
{
switch (type)
{
case 0: x = 1; break;
case 1: x = 2; break;
case 2: x = 3; break;
case 3: x = 4; break;
default: throw std::runtime_error("Error");
};
}

int x;
};

//-------------------------------------------------------------------------
void TestOptimizedBuild()
{
std::make_unique<CrashInOptimizedBuild>(0);
}
}
24 changes: 24 additions & 0 deletions TestCoverageOptimizedBuild/TestCoverageOptimizedBuild.hpp
@@ -0,0 +1,24 @@
// OpenCppCoverage is an open source code coverage for C++.
// Copyright (C) 2017 OpenCppCoverage
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include <boost/filesystem/path.hpp>

namespace TestCoverageOptimizedBuild
{
__declspec(dllexport) boost::filesystem::path GetMainCppPath();
__declspec(dllexport) boost::filesystem::path GetOutputBinaryPath();
__declspec(dllexport) void TestOptimizedBuild();
}

0 comments on commit 797811e

Please sign in to comment.