Skip to content

Commit 797811e

Browse files
Add CodeCoverageRunnerTest.OptimizedBuild
1 parent f963798 commit 797811e

12 files changed

+334
-10
lines changed

CppCoverage.sln

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FileFilterTest", "FileFilte
4848
EndProject
4949
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ToolsTest", "ToolsTest\ToolsTest.vcxproj", "{42F32EB3-42B6-498A-9823-26E6E5982EE3}"
5050
EndProject
51+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestCoverageOptimizedBuild", "TestCoverageOptimizedBuild\TestCoverageOptimizedBuild.vcxproj", "{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}"
52+
EndProject
5153
Global
5254
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5355
Debug|Any CPU = Debug|Any CPU
@@ -252,6 +254,20 @@ Global
252254
{42F32EB3-42B6-498A-9823-26E6E5982EE3}.Release|Win32.Build.0 = Release|Win32
253255
{42F32EB3-42B6-498A-9823-26E6E5982EE3}.Release|x64.ActiveCfg = Release|x64
254256
{42F32EB3-42B6-498A-9823-26E6E5982EE3}.Release|x64.Build.0 = Release|x64
257+
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Debug|Any CPU.ActiveCfg = Debug|Win32
258+
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32
259+
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Debug|Mixed Platforms.Build.0 = Debug|Win32
260+
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Debug|Win32.ActiveCfg = Debug|Win32
261+
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Debug|Win32.Build.0 = Debug|Win32
262+
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Debug|x64.ActiveCfg = Debug|x64
263+
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Debug|x64.Build.0 = Debug|x64
264+
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Release|Any CPU.ActiveCfg = Release|Win32
265+
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Release|Mixed Platforms.ActiveCfg = Release|Win32
266+
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Release|Mixed Platforms.Build.0 = Release|Win32
267+
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Release|Win32.ActiveCfg = Release|Win32
268+
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Release|Win32.Build.0 = Release|Win32
269+
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Release|x64.ActiveCfg = Release|x64
270+
{2FD17BA1-0E02-49E6-84C3-0A8F63FEF871}.Release|x64.Build.0 = Release|x64
255271
EndGlobalSection
256272
GlobalSection(SolutionProperties) = preSolution
257273
HideSolutionNode = FALSE

CppCoverage/CodeCoverageRunner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ namespace CppCoverage
6262
coverageFilterManager_ = std::make_unique<CoverageFilterManager>(
6363
settings.GetCoverageFilterSettings(),
6464
settings.GetUnifiedDiffSettings(),
65-
false);
65+
settings.GetOptimizedBuildSupport());
6666
const auto& startInfo = settings.GetStartInfo();
6767
int exitCode = debugger.Debug(startInfo, *this);
6868
const auto& path = startInfo.GetPath();

CppCoverage/RunCoverageSettings.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace CppCoverage
3030
, coverChildren_{ false }
3131
, continueAfterCppException_{ false }
3232
, maxUnmatchPathsForWarning_{ 0 }
33+
, optimizedBuildSupport_{ false }
3334
{
3435
}
3536

@@ -51,6 +52,12 @@ namespace CppCoverage
5152
maxUnmatchPathsForWarning_ = maxUnmatchPathsForWarning;
5253
}
5354

55+
//-------------------------------------------------------------------------
56+
void RunCoverageSettings::SetOptimizedBuildSupport(bool optimizedBuildSupport)
57+
{
58+
optimizedBuildSupport_ = optimizedBuildSupport;
59+
}
60+
5461
//-------------------------------------------------------------------------
5562
const StartInfo& RunCoverageSettings::GetStartInfo() const
5663
{
@@ -86,4 +93,10 @@ namespace CppCoverage
8693
{
8794
return maxUnmatchPathsForWarning_;
8895
}
96+
97+
//-------------------------------------------------------------------------
98+
bool RunCoverageSettings::GetOptimizedBuildSupport() const
99+
{
100+
return optimizedBuildSupport_;
101+
}
89102
}

CppCoverage/RunCoverageSettings.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ namespace CppCoverage
3838
void SetCoverChildren(bool);
3939
void SetContinueAfterCppException(bool);
4040
void SetMaxUnmatchPathsForWarning(size_t);
41+
void SetOptimizedBuildSupport(bool);
4142

4243
const StartInfo& GetStartInfo() const;
4344
const CoverageFilterSettings& GetCoverageFilterSettings() const;
4445
const std::vector<UnifiedDiffSettings>& GetUnifiedDiffSettings() const;
4546
bool GetCoverChildren() const;
4647
bool GetContinueAfterCppException() const;
4748
size_t GetMaxUnmatchPathsForWarning() const;
49+
bool GetOptimizedBuildSupport() const;
4850

4951
private:
5052
StartInfo startInfo_;
@@ -53,5 +55,6 @@ namespace CppCoverage
5355
bool coverChildren_;
5456
bool continueAfterCppException_;
5557
size_t maxUnmatchPathsForWarning_;
58+
bool optimizedBuildSupport_;
5659
};
5760
}

CppCoverageTest/CodeCoverageRunnerTest.cpp

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "TestCoverageConsole/TestDiff.hpp"
5151

5252
#include "TestCoverageSharedLib/TestCoverageSharedLib.hpp"
53+
#include "TestCoverageOptimizedBuild/TestCoverageOptimizedBuild.hpp"
5354

5455
#include "TestTools.hpp"
5556

@@ -96,7 +97,8 @@ namespace CppCoverageTest
9697
const std::vector<std::wstring>& sourcePatternCollection,
9798
const std::vector<cov::UnifiedDiffSettings>& unifiedDiffSettingsCollection = {},
9899
bool coverChildren = true,
99-
bool continueAfterCppException = false)
100+
bool continueAfterCppException = false,
101+
bool optimizedBuildSupport = false)
100102
{
101103
cov::CodeCoverageRunner codeCoverageRunner;
102104
cov::Patterns modulePatterns{false};
@@ -126,6 +128,7 @@ namespace CppCoverageTest
126128
unifiedDiffSettingsCollection);
127129
settings.SetCoverChildren(coverChildren);
128130
settings.SetContinueAfterCppException(continueAfterCppException);
131+
settings.SetOptimizedBuildSupport(optimizedBuildSupport);
129132

130133
auto coverageData = codeCoverageRunner.RunCoverage(settings);
131134

@@ -142,15 +145,17 @@ namespace CppCoverageTest
142145
const std::wstring& sourcePattern,
143146
const std::vector<cov::UnifiedDiffSettings>& unifiedDiffSettingsCollection = {},
144147
bool coverChildren = true,
145-
bool continueAfterCppException = false)
148+
bool continueAfterCppException = false,
149+
bool optimizedBuildSupport = false)
146150
{
147151
return ComputeCoverageDataPatterns(
148152
arguments,
149153
{ modulePattern },
150154
{ sourcePattern },
151155
unifiedDiffSettingsCollection,
152156
coverChildren,
153-
continueAfterCppException);
157+
continueAfterCppException,
158+
optimizedBuildSupport);
154159
}
155160

156161
//---------------------------------------------------------------------
@@ -223,7 +228,15 @@ namespace CppCoverageTest
223228
std::wstring GetError() const
224229
{
225230
return Tools::LocalToWString(error_->str());
226-
}
231+
}
232+
233+
//-------------------------------------------------------------------------
234+
intptr_t CountExecutedLines(const cov::FileCoverage& file)
235+
{
236+
auto lines = file.GetLines();
237+
return boost::count_if(lines,
238+
[](const auto& line) { return line.HasBeenExecuted(); });
239+
}
227240

228241
private:
229242
boost::shared_ptr<std::ostringstream> error_;
@@ -460,8 +473,32 @@ namespace CppCoverageTest
460473
const auto& file = GetFirstFileCoverage(coverageData);
461474
auto filename = file.GetPath().filename().wstring();
462475
ASSERT_TRUE(boost::algorithm::iequals(unloadDllFilename, filename));
463-
auto lines = file.GetLines();
464-
ASSERT_NE(0, boost::count_if(lines,
465-
[](const auto& line) { return line.HasBeenExecuted(); }));
476+
ASSERT_NE(0, CountExecutedLines(file));
477+
}
478+
479+
//-------------------------------------------------------------------------
480+
TEST_F(CodeCoverageRunnerTest, OptimizedBuild)
481+
{
482+
auto computeCoverage = [&](bool optimizedBuild)
483+
{
484+
return ComputeCoverageData(
485+
{ TestCoverageConsole::TestOptimizedBuild },
486+
TestCoverageOptimizedBuild::GetOutputBinaryPath().wstring(),
487+
TestCoverageOptimizedBuild::GetMainCppPath().wstring(),
488+
{}, false, false, optimizedBuild);
489+
};
490+
491+
auto coverageData = computeCoverage(false);
492+
ASSERT_NE(0, coverageData.GetExitCode());
493+
494+
auto coverageDataOptimizedBuild = computeCoverage(true);
495+
ASSERT_EQ(0, coverageDataOptimizedBuild.GetExitCode());
496+
497+
const auto& fileOptimizedBuild = GetFirstFileCoverage(coverageDataOptimizedBuild);
498+
const auto& file = GetFirstFileCoverage(coverageData);
499+
auto optimizedBuildCount = CountExecutedLines(fileOptimizedBuild);
500+
auto count = CountExecutedLines(file);
501+
502+
ASSERT_GT(optimizedBuildCount, count);
466503
}
467504
}

CppCoverageTest/CppCoverageTest.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@
208208
<ProjectReference Include="..\FileFilter\FileFilter.vcxproj">
209209
<Project>{6fd7c5be-04bd-496d-a924-285a3e867814}</Project>
210210
</ProjectReference>
211+
<ProjectReference Include="..\TestCoverageOptimizedBuild\TestCoverageOptimizedBuild.vcxproj">
212+
<Project>{2fd17ba1-0e02-49e6-84c3-0a8f63fef871}</Project>
213+
</ProjectReference>
211214
<ProjectReference Include="..\TestCoverageSharedLib\TestCoverageSharedLib.vcxproj">
212215
<Project>{0481b51c-98f1-4e92-a51e-162b77ecd939}</Project>
213216
</ProjectReference>

TestCoverageConsole/TestCoverageConsole.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <iostream>
2222

2323
#include "TestCoverageSharedLib/TestCoverageSharedLib.hpp"
24+
#include "TestCoverageOptimizedBuild/TestCoverageOptimizedBuild.hpp"
2425

2526
#include "SpecialLineInfo.hpp"
2627
#include "TestCoverageConsole.hpp"
@@ -86,6 +87,8 @@ int _tmain(int argc, _TCHAR* argv[])
8687
TestCoverageConsole::UnloadReloadDll();
8788
else if (type == TestCoverageConsole::TestDiff)
8889
TestCoverageConsole::FilterByDiff();
90+
else if (type == TestCoverageConsole::TestOptimizedBuild)
91+
TestCoverageOptimizedBuild::TestOptimizedBuild();
8992
else
9093
std::wcerr << L"Unsupported type:" << type << std::endl;
9194
}

TestCoverageConsole/TestCoverageConsole.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ namespace TestCoverageConsole
4949
//-------------------------------------------------------------------------
5050
inline int GetTestCoverageConsoleCppMainStartLine()
5151
{
52-
return 55;
52+
return 56;
5353
}
5454

5555
//-------------------------------------------------------------------------
5656
inline int GetTestCoverageConsoleCppMainReturnLine()
5757
{
58-
return GetTestCoverageConsoleCppMainStartLine() + 37;
58+
return GetTestCoverageConsoleCppMainStartLine() + 39;
5959
}
6060

6161
const std::wstring TestBasic = L"TestBasic";
@@ -70,4 +70,5 @@ namespace TestCoverageConsole
7070
const std::wstring TestSpecialLineInfo = L"TestSpecialLineInfo";
7171
const std::wstring TestUnloadReloadDll = L"TestUnloadReloadDll";
7272
const std::wstring TestDiff = L"TestDiff";
73+
const std::wstring TestOptimizedBuild = L"TestOptimizedBuild";
7374
}

TestCoverageConsole/TestCoverageConsole.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@
177177
<ClCompile Include="TestThread.cpp" />
178178
</ItemGroup>
179179
<ItemGroup>
180+
<ProjectReference Include="..\TestCoverageOptimizedBuild\TestCoverageOptimizedBuild.vcxproj">
181+
<Project>{2fd17ba1-0e02-49e6-84c3-0a8f63fef871}</Project>
182+
</ProjectReference>
180183
<ProjectReference Include="..\TestCoverageSharedLib\TestCoverageSharedLib.vcxproj">
181184
<Project>{0481b51c-98f1-4e92-a51e-162b77ecd939}</Project>
182185
</ProjectReference>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// OpenCppCoverage is an open source code coverage for C++.
2+
// Copyright (C) 2017 OpenCppCoverage
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
#include "TestCoverageOptimizedBuild.hpp"
18+
19+
#include <memory>
20+
21+
namespace TestCoverageOptimizedBuild
22+
{
23+
//-------------------------------------------------------------------------
24+
boost::filesystem::path GetMainCppPath()
25+
{
26+
return __FILE__;
27+
}
28+
29+
//-------------------------------------------------------------------------
30+
boost::filesystem::path GetOutputBinaryPath()
31+
{
32+
return boost::filesystem::path(OUT_DIR) / TARGET_FILE_NAME;
33+
}
34+
35+
//-------------------------------------------------------------------------
36+
class CrashInOptimizedBuild
37+
{
38+
public:
39+
explicit CrashInOptimizedBuild(int type)
40+
{
41+
switch (type)
42+
{
43+
case 0: x = 1; break;
44+
case 1: x = 2; break;
45+
case 2: x = 3; break;
46+
case 3: x = 4; break;
47+
default: throw std::runtime_error("Error");
48+
};
49+
}
50+
51+
int x;
52+
};
53+
54+
//-------------------------------------------------------------------------
55+
void TestOptimizedBuild()
56+
{
57+
std::make_unique<CrashInOptimizedBuild>(0);
58+
}
59+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// OpenCppCoverage is an open source code coverage for C++.
2+
// Copyright (C) 2017 OpenCppCoverage
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
#include <boost/filesystem/path.hpp>
18+
19+
namespace TestCoverageOptimizedBuild
20+
{
21+
__declspec(dllexport) boost::filesystem::path GetMainCppPath();
22+
__declspec(dllexport) boost::filesystem::path GetOutputBinaryPath();
23+
__declspec(dllexport) void TestOptimizedBuild();
24+
}

0 commit comments

Comments
 (0)