Moved Eule to its own repository
This commit is contained in:
5
_TestingUtilities/HandyMacros.h
Normal file
5
_TestingUtilities/HandyMacros.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
#define LARGE_RAND_DOUBLE (((rng() % 6969000) - 3500000) / 1000.0)
|
||||
#define LARGE_RAND_POSITIVE_DOUBLE (((rng() % 3500000)) / 1000.0)
|
||||
#define LARGE_RAND_INT ((rng() % 6969) - 3500)
|
||||
#define LARGE_RAND_POSITIVE_INT ((rng() % 3500))
|
44
_TestingUtilities/MemoryLeakDetector.h
Normal file
44
_TestingUtilities/MemoryLeakDetector.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
#include <crtdbg.h>
|
||||
|
||||
// Don't even allow compilation in release mode unless handled correctly.
|
||||
// This class ONLY WORKS IN DEBUG MODE!!!
|
||||
#ifdef _DEBUG
|
||||
class MemoryLeakDetector
|
||||
{
|
||||
public:
|
||||
// Defines the initial mem state to check against
|
||||
void Init()
|
||||
{
|
||||
_CrtMemCheckpoint(&stateInit);
|
||||
return;
|
||||
};
|
||||
|
||||
// Returns whether or not the process uses more memory than at the point when Init() was called
|
||||
bool DetectLeak()
|
||||
{
|
||||
_CrtMemCheckpoint(&stateEnd);
|
||||
return Evaluate();
|
||||
};
|
||||
|
||||
// Returns the absolute memory difference since calling Init(). WARNING: The ABSOLUTE difference! No negative values!
|
||||
int Difference()
|
||||
{
|
||||
// I know that it's the exact same code as in DetectedLeak().
|
||||
_CrtMemCheckpoint(&stateEnd);
|
||||
return Evaluate();
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
int Evaluate()
|
||||
{
|
||||
return _CrtMemDifference(&stateBfr, &stateInit, &stateEnd);
|
||||
}
|
||||
|
||||
_CrtMemState stateInit;
|
||||
_CrtMemState stateEnd;
|
||||
_CrtMemState stateBfr;
|
||||
|
||||
};
|
||||
#endif
|
27
_TestingUtilities/Testutil.h
Normal file
27
_TestingUtilities/Testutil.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
class Testutil
|
||||
{
|
||||
public:
|
||||
template <typename T>
|
||||
static double Stddev(const std::vector<T>& distribution)
|
||||
{
|
||||
// Calculate mean
|
||||
double sum = 0;
|
||||
for (const T& i : distribution)
|
||||
sum += i;
|
||||
const double mean = sum / distribution.size();
|
||||
|
||||
// Calculate variance
|
||||
sum = 0;
|
||||
for (const T& i : distribution)
|
||||
sum += (i - mean) * (i - mean);
|
||||
const double variance = sum / (distribution.size() - 1);
|
||||
|
||||
// Calcuate stddev
|
||||
const double stddev = sqrt(variance);
|
||||
|
||||
return stddev;
|
||||
}
|
||||
};
|
105
_TestingUtilities/_MemoryLeakDetector.cpp
Normal file
105
_TestingUtilities/_MemoryLeakDetector.cpp
Normal file
@@ -0,0 +1,105 @@
|
||||
#include "CppUnitTest.h"
|
||||
#include "MemoryLeakDetector.h"
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
|
||||
namespace TestingUtilities
|
||||
{
|
||||
TEST_CLASS(_MemoryLeakDetector)
|
||||
{
|
||||
public:
|
||||
// =========== MEMORY LEAK TESTS ===========
|
||||
// These tests depends on debug-mode for memory insights.
|
||||
// Thus, they only works in debug mode.
|
||||
#ifdef _DEBUG
|
||||
|
||||
// Tests to detect no memory leak, if the test does nothing at all
|
||||
TEST_METHOD(No_Memleak_For_Nothing)
|
||||
{
|
||||
MemoryLeakDetector mld;
|
||||
mld.Init();
|
||||
|
||||
{
|
||||
// Do nothing here
|
||||
}
|
||||
|
||||
Assert::IsFalse(mld.DetectLeak());
|
||||
return;
|
||||
}
|
||||
|
||||
// Tests to detect no memory leak when not even touching pointers
|
||||
TEST_METHOD(No_Memleak_For_No_Pointer_Action)
|
||||
{
|
||||
MemoryLeakDetector mld;
|
||||
mld.Init();
|
||||
|
||||
{
|
||||
int i = 33;
|
||||
int c = i * 9;
|
||||
}
|
||||
|
||||
Assert::IsFalse(mld.DetectLeak());
|
||||
return;
|
||||
}
|
||||
|
||||
// Tests to detect no memory leak when correctly cleaning up pointers
|
||||
TEST_METHOD(No_Memleak_For_Cleaned_Up_Pointers)
|
||||
{
|
||||
MemoryLeakDetector mld;
|
||||
mld.Init();
|
||||
|
||||
{
|
||||
int* ptr = new int[333];
|
||||
delete[] ptr;
|
||||
}
|
||||
|
||||
Assert::IsFalse(mld.DetectLeak());
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Tests to detect a memory leak when not cleaning up pointers
|
||||
TEST_METHOD(Memleak_For_No_Pointer_Cleanup)
|
||||
{
|
||||
MemoryLeakDetector mld;
|
||||
mld.Init();
|
||||
|
||||
{
|
||||
int* ptr = new int[333];
|
||||
}
|
||||
|
||||
Assert::IsTrue(mld.DetectLeak());
|
||||
return;
|
||||
}
|
||||
|
||||
// Tests to detect no memory leak when correctly cleaning up pointers, using C-Methods
|
||||
TEST_METHOD(No_Memleak_For_Cleaned_Up_Pointers_C_Like)
|
||||
{
|
||||
MemoryLeakDetector mld;
|
||||
mld.Init();
|
||||
|
||||
{
|
||||
int* ptr = (int*)malloc(sizeof(int) * 333);
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
Assert::IsFalse(mld.DetectLeak());
|
||||
return;
|
||||
}
|
||||
|
||||
// Tests to detect a memory leak when not cleaning up pointers, using C-Methods
|
||||
TEST_METHOD(Memleak_For_No_Pointer_Cleanup_C_Like)
|
||||
{
|
||||
MemoryLeakDetector mld;
|
||||
mld.Init();
|
||||
|
||||
{
|
||||
int* ptr = (int*)malloc(sizeof(int) * 333);
|
||||
}
|
||||
|
||||
Assert::IsTrue(mld.DetectLeak());
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
168
_TestingUtilities/_TestingUtilities.vcxproj
Normal file
168
_TestingUtilities/_TestingUtilities.vcxproj
Normal file
@@ -0,0 +1,168 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<ProjectGuid>{7C7C6FE1-FF52-4C65-8FC1-05A8A9D9AEF7}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<RootNamespace>TestingUtilities</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
<ProjectSubType>NativeUnitTestProject</ProjectSubType>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>_DEBUG;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<UseFullPaths>true</UseFullPaths>
|
||||
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>_DEBUG;WIN32;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<UseFullPaths>true</UseFullPaths>
|
||||
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<UseFullPaths>true</UseFullPaths>
|
||||
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<UseFullPaths>true</UseFullPaths>
|
||||
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="_MemoryLeakDetector.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="HandyMacros.h" />
|
||||
<ClInclude Include="MemoryLeakDetector.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
29
_TestingUtilities/_TestingUtilities.vcxproj.filters
Normal file
29
_TestingUtilities/_TestingUtilities.vcxproj.filters
Normal file
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Headerdateien">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Ressourcendateien">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Tests">
|
||||
<UniqueIdentifier>{9a838f9d-c9d6-420b-bf8a-df0f91bb4731}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="HandyMacros.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="MemoryLeakDetector.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="_MemoryLeakDetector.cpp">
|
||||
<Filter>Tests</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
Reference in New Issue
Block a user