Added replace methods and unit tests

This commit is contained in:
Leonetienne 2021-11-20 19:24:35 +01:00
parent 87abd1cb51
commit f6b96b2271
12 changed files with 487 additions and 45 deletions

View File

@ -139,7 +139,12 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Exec.cpp" />
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StringTools\StringTools.vcxproj">
<Project>{0270ac5e-eba3-4d8f-8d50-995fd44959b4}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -15,7 +15,7 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Exec.cpp">
<ClCompile Include="main.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>

View File

@ -5,6 +5,16 @@ VisualStudioVersion = 16.0.30907.101
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StringTools", "StringTools\StringTools.vcxproj", "{0270AC5E-EBA3-4D8F-8D50-995FD44959B4}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Test", "Test\Test.vcxproj", "{64EF270C-0A13-4AD8-8D50-23A1CEEBF98B}"
ProjectSection(ProjectDependencies) = postProject
{0270AC5E-EBA3-4D8F-8D50-995FD44959B4} = {0270AC5E-EBA3-4D8F-8D50-995FD44959B4}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Exec", "Exec\Exec.vcxproj", "{02F9FA44-902F-4695-846B-0B45E952A962}"
ProjectSection(ProjectDependencies) = postProject
{0270AC5E-EBA3-4D8F-8D50-995FD44959B4} = {0270AC5E-EBA3-4D8F-8D50-995FD44959B4}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
@ -21,6 +31,22 @@ Global
{0270AC5E-EBA3-4D8F-8D50-995FD44959B4}.Release|x64.Build.0 = Release|x64
{0270AC5E-EBA3-4D8F-8D50-995FD44959B4}.Release|x86.ActiveCfg = Release|Win32
{0270AC5E-EBA3-4D8F-8D50-995FD44959B4}.Release|x86.Build.0 = Release|Win32
{64EF270C-0A13-4AD8-8D50-23A1CEEBF98B}.Debug|x64.ActiveCfg = Debug|x64
{64EF270C-0A13-4AD8-8D50-23A1CEEBF98B}.Debug|x64.Build.0 = Debug|x64
{64EF270C-0A13-4AD8-8D50-23A1CEEBF98B}.Debug|x86.ActiveCfg = Debug|Win32
{64EF270C-0A13-4AD8-8D50-23A1CEEBF98B}.Debug|x86.Build.0 = Debug|Win32
{64EF270C-0A13-4AD8-8D50-23A1CEEBF98B}.Release|x64.ActiveCfg = Release|x64
{64EF270C-0A13-4AD8-8D50-23A1CEEBF98B}.Release|x64.Build.0 = Release|x64
{64EF270C-0A13-4AD8-8D50-23A1CEEBF98B}.Release|x86.ActiveCfg = Release|Win32
{64EF270C-0A13-4AD8-8D50-23A1CEEBF98B}.Release|x86.Build.0 = Release|Win32
{02F9FA44-902F-4695-846B-0B45E952A962}.Debug|x64.ActiveCfg = Debug|x64
{02F9FA44-902F-4695-846B-0B45E952A962}.Debug|x64.Build.0 = Debug|x64
{02F9FA44-902F-4695-846B-0B45E952A962}.Debug|x86.ActiveCfg = Debug|Win32
{02F9FA44-902F-4695-846B-0B45E952A962}.Debug|x86.Build.0 = Debug|Win32
{02F9FA44-902F-4695-846B-0B45E952A962}.Release|x64.ActiveCfg = Release|x64
{02F9FA44-902F-4695-846B-0B45E952A962}.Release|x64.Build.0 = Release|x64
{02F9FA44-902F-4695-846B-0B45E952A962}.Release|x86.ActiveCfg = Release|Win32
{02F9FA44-902F-4695-846B-0B45E952A962}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,62 @@
#include "StringTools.h"
#include <sstream>
std::string StringTools::Replace(const std::string& str, const char find, const std::string& subst)
{
std::stringstream ss;
for (std::size_t i = 0; i < str.length(); i++)
{
if (str[i] != find)
ss << str[i];
else
ss << subst;
}
return ss.str();
}
std::string StringTools::Replace(const std::string& str, const std::string& find, const std::string& subst)
{
if (find.length() == 0)
return str;
std::stringstream ss;
std::size_t posFound = 0;
std::size_t lastFound = 0;
while (posFound != std::string::npos)
{
lastFound = posFound;
posFound = str.find(find, posFound);
if (posFound != std::string::npos)
{
ss << str.substr(lastFound, posFound - lastFound) << subst;
posFound += find.length();
}
else
{
ss << str.substr(lastFound, (str.length()) - lastFound);
}
}
return ss.str();
}
std::string StringTools::Replace(const std::string& str, const char find, const char subst)
{
std::stringstream ss;
ss << subst;
return Replace(str, find, ss.str());
}
std::string StringTools::Replace(const std::string& str, const std::string& find, const char subst)
{
std::stringstream ss;
ss << subst;
return Replace(str, find, ss.str());
}

View File

@ -1,5 +1,23 @@
#pragma once
#include <string>
/* Handy utensils to manipulate strings */
class StringTools
{
};
public:
//! Will replace every occurence of `find` in `str` by `subst`.
static std::string Replace(const std::string& str, const char find, const std::string& subst);
//! Will replace every occurence of `find` in `str` by `subst`.
static std::string Replace(const std::string& str, const std::string& find, const std::string& subst);
//! Will replace every occurence of `find` in `str` by `subst`.
static std::string Replace(const std::string& str, const char find, const char subst);
//! Will replace every occurence of `find` in `str` by `subst`.
static std::string Replace(const std::string& str, const std::string& find, const char subst);
private:
// No instanciation! >:(
StringTools();
};

View File

@ -18,6 +18,12 @@
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="StringTools.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="StringTools.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
@ -27,26 +33,26 @@
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
@ -138,9 +144,6 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="StringTools.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View File

@ -14,6 +14,11 @@
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="StringTools.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="StringTools.cpp">
<Filter>Quelldateien</Filter>

View File

@ -0,0 +1,166 @@
#include "CppUnitTest.h"
#include "../StringTools/StringTools.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace _StringTools
{
TEST_CLASS(_Replace_Char)
{
public:
// Tests that replacing something in an empty string returns an empty string
TEST_METHOD(EmptyString)
{
// Setup
const std::string in = "";
// Exercise
const std::string out = StringTools::Replace(in, 'a', "Subst");
// Verify
Assert::AreEqual(out.c_str(), "");
return;
}
// Tests that replacing a char to an empty string works
TEST_METHOD(Single_ReplaceToEmpty)
{
// Setup
const std::string in = "i";
// Exercise
const std::string out = StringTools::Replace(in, 'i', "");
// Verify
Assert::AreEqual(out.c_str(), "");
return;
}
// Tests that replacing to a single char works
TEST_METHOD(Single_ReplaceToSingleChar)
{
// Setup
const std::string in = "a";
// Exercise
const std::string out = StringTools::Replace(in, 'a', "i");
// Verify
Assert::AreEqual(out.c_str(), "i");
return;
}
// Tests that replacing to a single char works, passing a char
TEST_METHOD(Single_ReplaceToSingleChar_AsChar)
{
// Setup
const std::string in = "Oilbanger";
// Exercise
const std::string out = StringTools::Replace(in, 'a', 'i');
// Verify
Assert::AreEqual(out.c_str(), "Oilbinger");
return;
}
// Tests that replacing the find to something longer works
TEST_METHOD(Single_ReplaceToLonger)
{
// Setup
const std::string in = "Littled";
// Exercise
const std::string out = StringTools::Replace(in, 'd', "binger");
// Verify
Assert::AreEqual(out.c_str(), "Littlebinger");
return;
}
// Tests that replacing a char to an empty string works
TEST_METHOD(Multiple_ReplaceToEmpty)
{
// Setup
const std::string in = "dirty dogs dig dirt daringly";
// Exercise
const std::string out = StringTools::Replace(in, 'd', "");
// Verify
Assert::AreEqual(out.c_str(), "irty ogs ig irt aringly");
return;
}
// Tests that replacing to a single char works
TEST_METHOD(Multiple_ReplaceToSingleChar)
{
// Setup
const std::string in = "Oilbanger, Bangerfanger, Lattle brattle oaly skattle.";
// Exercise
const std::string out = StringTools::Replace(in, 'a', "i");
// Verify
Assert::AreEqual(out.c_str(), "Oilbinger, Bingerfinger, Little brittle oily skittle.");
return;
}
// Tests that replacing to a single char works, passing a char
TEST_METHOD(Multiple_ReplaceToSingleChar_AsChar)
{
// Setup
const std::string in = "Oilbanger, Bangerfanger, Lattle brattle oaly skattle.";
// Exercise
const std::string out = StringTools::Replace(in, 'a', 'i');
// Verify
Assert::AreEqual(out.c_str(), "Oilbinger, Bingerfinger, Little brittle oily skittle.");
return;
}
// Tests that replacing the find to something longer works
TEST_METHOD(Multiple_ReplaceToLonger)
{
// Setup
const std::string in = "d d d d d d d d";
// Exercise
const std::string out = StringTools::Replace(in, 'd', "bla");
// Verify
Assert::AreEqual(out.c_str(), "bla bla bla bla bla bla bla bla");
return;
}
// Tests that the replacer ignores chars put in by the replacer
TEST_METHOD(ReplacerIgnoresReplaced)
{
// Setup
const std::string in = "b b b b b b b b";
// Exercise
const std::string out = StringTools::Replace(in, 'b', "bla");
// Verify
Assert::AreEqual(out.c_str(), "bla bla bla bla bla bla bla bla");
return;
}
// Tests that replacing succesive findings works
TEST_METHOD(Replace_Successive)
{
// Setup
const std::string in = "bbbbbbbb";
// Exercise
const std::string out = StringTools::Replace(in, 'b', "bla");
// Verify
Assert::AreEqual(out.c_str(), "blablablablablablablabla");
return;
}
};
}

View File

@ -0,0 +1,180 @@
#include "CppUnitTest.h"
#include "../StringTools/StringTools.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace _StringTools
{
TEST_CLASS(_Replace_String)
{
public:
// Tests that replacing something in an empty string returns an empty string
TEST_METHOD(EmptyString)
{
// Setup
const std::string in = "";
// Exercise
const std::string out = StringTools::Replace(in, "burger", "Subst");
// Verify
Assert::AreEqual(out.c_str(), "");
return;
}
// Tests that replacing a string to an empty string works
TEST_METHOD(Single_ReplaceToEmpty)
{
// Setup
const std::string in = "Squarepants";
// Exercise
const std::string out = StringTools::Replace(in, "Squarepants", "");
// Verify
Assert::AreEqual(out.c_str(), "");
return;
}
// Tests that replacing to a single char works
TEST_METHOD(Single_ReplaceToSingleChar)
{
// Setup
const std::string in = "Squarepants";
// Exercise
const std::string out = StringTools::Replace(in, "Squarepants", "i");
// Verify
Assert::AreEqual(out.c_str(), "i");
return;
}
// Tests that replacing to a single char works, passing a char
TEST_METHOD(Single_ReplaceToSingleChar_AsChar)
{
// Setup
const std::string in = "Oilbanger";
// Exercise
const std::string out = StringTools::Replace(in, "Oilbanger", 'i');
// Verify
Assert::AreEqual(out.c_str(), "i");
return;
}
// Tests that replacing the find to something longer works
TEST_METHOD(Single_ReplaceToLonger)
{
// Setup
const std::string in = "LittleDong";
// Exercise
const std::string out = StringTools::Replace(in, "Dong", "Binger");
// Verify
Assert::AreEqual(out.c_str(), "LittleBinger");
return;
}
// Tests that replacing a string to an empty string works
TEST_METHOD(Multiple_ReplaceToEmpty)
{
// Setup
const std::string in = "The fucking dogs are fucking eating the fucking chicken.";
// Exercise
const std::string out = StringTools::Replace(in, "fucking ", "");
// Verify
Assert::AreEqual(out.c_str(), "The dogs are eating the chicken.");
return;
}
// Tests that replacing to a single char works
TEST_METHOD(Multiple_ReplaceToSingleChar)
{
// Setup
const std::string in = "Oilbsmearynger, Bsmearyngerfsmearynger, Lsmearyttle brsmearyttle osmearyly sksmearyttle.";
// Exercise
const std::string out = StringTools::Replace(in, "smeary", "i");
// Verify
Assert::AreEqual(out.c_str(), "Oilbinger, Bingerfinger, Little brittle oily skittle.");
return;
}
// Tests that replacing to a single char works, passing a char
TEST_METHOD(Multiple_ReplaceToSingleChar_AsChar)
{
// Setup
const std::string in = "Oilbsmearynger, Bsmearyngerfsmearynger, Lsmearyttle brsmearyttle osmearyly sksmearyttle.";
// Exercise
const std::string out = StringTools::Replace(in, "smeary", 'i');
// Verify
Assert::AreEqual(out.c_str(), "Oilbinger, Bingerfinger, Little brittle oily skittle.");
return;
}
// Tests that replacing the find to something longer works
TEST_METHOD(Multiple_ReplaceToLonger)
{
// Setup
const std::string in = "honk honk honk honk honk honk honk honk";
// Exercise
const std::string out = StringTools::Replace(in, "honk", "hallery");
// Verify
Assert::AreEqual(out.c_str(), "hallery hallery hallery hallery hallery hallery hallery hallery");
return;
}
// Tests that the replacer ignores chars put in by the replacer
TEST_METHOD(ReplacerIgnoresReplaced)
{
// Setup
const std::string in = "honk honk honk honk honk honk honk honk";
// Exercise
const std::string out = StringTools::Replace(in, "honk", "honka");
// Verify
Assert::AreEqual(out.c_str(), "honka honka honka honka honka honka honka honka");
return;
}
// Tests that replacing succesive findings works
TEST_METHOD(Replace_Successive)
{
// Setup
const std::string in = "honkhonkhonkhonkhonkhonkhonkhonk";
// Exercise
const std::string out = StringTools::Replace(in, "honk", "hallery");
// Verify
Assert::AreEqual(out.c_str(), "halleryhalleryhalleryhalleryhalleryhalleryhalleryhallery");
return;
}
// Tests that if find.length() == 0, it returns just the input
TEST_METHOD(Find_Length0_Returns_Input)
{
// Setup
const std::string in = "Littled";
// Exercise
const std::string out = StringTools::Replace(in, "", "binger");
// Verify
Assert::AreEqual(out.c_str(), "Littled");
return;
}
};
}

View File

@ -1,16 +0,0 @@
#include "pch.h"
#include "CppUnitTest.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace Test
{
TEST_CLASS(Test)
{
public:
TEST_METHOD(TestMethod1)
{
}
};
}

View File

@ -25,6 +25,7 @@
<RootNamespace>Test</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectSubType>NativeUnitTestProject</ProjectSubType>
<ProjectName>Test_Stringtools</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@ -89,7 +90,7 @@
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
@ -104,7 +105,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
@ -119,7 +120,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
@ -138,7 +139,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
@ -156,16 +157,13 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Test.cpp" />
<ProjectReference Include="..\StringTools\StringTools.vcxproj">
<Project>{0270ac5e-eba3-4d8f-8d50-995fd44959b4}</Project>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
<ClCompile Include="Replace_Char.cpp" />
<ClCompile Include="Replace_String.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -15,16 +15,11 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Test.cpp">
<ClCompile Include="Replace_Char.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="pch.cpp">
<ClCompile Include="Replace_String.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
</Project>