From f6b96b227196b4c2736a1716aad8968993187c8a Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Sat, 20 Nov 2021 19:24:35 +0100 Subject: [PATCH] Added replace methods and unit tests --- StringTools/Exec/Exec.vcxproj | 7 +- StringTools/Exec/Exec.vcxproj.filters | 2 +- StringTools/StringTools.sln | 26 +++ StringTools/StringTools/StringTools.cpp | 62 ++++++ StringTools/StringTools/StringTools.h | 20 +- StringTools/StringTools/StringTools.vcxproj | 17 +- .../StringTools/StringTools.vcxproj.filters | 5 + StringTools/Test/Replace_Char.cpp | 166 ++++++++++++++++ StringTools/Test/Replace_String.cpp | 180 ++++++++++++++++++ StringTools/Test/Test.cpp | 16 -- StringTools/Test/Test.vcxproj | 22 +-- StringTools/Test/Test.vcxproj.filters | 9 +- 12 files changed, 487 insertions(+), 45 deletions(-) create mode 100644 StringTools/Test/Replace_Char.cpp create mode 100644 StringTools/Test/Replace_String.cpp delete mode 100644 StringTools/Test/Test.cpp diff --git a/StringTools/Exec/Exec.vcxproj b/StringTools/Exec/Exec.vcxproj index a80df50..fdea7b4 100644 --- a/StringTools/Exec/Exec.vcxproj +++ b/StringTools/Exec/Exec.vcxproj @@ -139,7 +139,12 @@ - + + + + + {0270ac5e-eba3-4d8f-8d50-995fd44959b4} + diff --git a/StringTools/Exec/Exec.vcxproj.filters b/StringTools/Exec/Exec.vcxproj.filters index c3e833c..46d313e 100644 --- a/StringTools/Exec/Exec.vcxproj.filters +++ b/StringTools/Exec/Exec.vcxproj.filters @@ -15,7 +15,7 @@ - + Quelldateien diff --git a/StringTools/StringTools.sln b/StringTools/StringTools.sln index be70dd8..f175fda 100644 --- a/StringTools/StringTools.sln +++ b/StringTools/StringTools.sln @@ -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 diff --git a/StringTools/StringTools/StringTools.cpp b/StringTools/StringTools/StringTools.cpp index e69de29..728506a 100644 --- a/StringTools/StringTools/StringTools.cpp +++ b/StringTools/StringTools/StringTools.cpp @@ -0,0 +1,62 @@ +#include "StringTools.h" +#include + +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()); +} \ No newline at end of file diff --git a/StringTools/StringTools/StringTools.h b/StringTools/StringTools/StringTools.h index 268bae5..730fa3e 100644 --- a/StringTools/StringTools/StringTools.h +++ b/StringTools/StringTools/StringTools.h @@ -1,5 +1,23 @@ #pragma once +#include + +/* 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(); +}; diff --git a/StringTools/StringTools/StringTools.vcxproj b/StringTools/StringTools/StringTools.vcxproj index e9cba79..d56fe01 100644 --- a/StringTools/StringTools/StringTools.vcxproj +++ b/StringTools/StringTools/StringTools.vcxproj @@ -18,6 +18,12 @@ x64 + + + + + + 16.0 Win32Proj @@ -27,26 +33,26 @@ - Application + StaticLibrary true v142 Unicode - Application + StaticLibrary false v142 true Unicode - Application + StaticLibrary true v142 Unicode - Application + StaticLibrary false v142 true @@ -138,9 +144,6 @@ true - - - diff --git a/StringTools/StringTools/StringTools.vcxproj.filters b/StringTools/StringTools/StringTools.vcxproj.filters index a119ed4..f6e4482 100644 --- a/StringTools/StringTools/StringTools.vcxproj.filters +++ b/StringTools/StringTools/StringTools.vcxproj.filters @@ -14,6 +14,11 @@ rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + Headerdateien + + Quelldateien diff --git a/StringTools/Test/Replace_Char.cpp b/StringTools/Test/Replace_Char.cpp new file mode 100644 index 0000000..d562651 --- /dev/null +++ b/StringTools/Test/Replace_Char.cpp @@ -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; + } + }; +} diff --git a/StringTools/Test/Replace_String.cpp b/StringTools/Test/Replace_String.cpp new file mode 100644 index 0000000..ff67ac4 --- /dev/null +++ b/StringTools/Test/Replace_String.cpp @@ -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; + } + }; +} diff --git a/StringTools/Test/Test.cpp b/StringTools/Test/Test.cpp deleted file mode 100644 index 4441b10..0000000 --- a/StringTools/Test/Test.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "pch.h" -#include "CppUnitTest.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; - -namespace Test -{ - TEST_CLASS(Test) - { - public: - - TEST_METHOD(TestMethod1) - { - } - }; -} diff --git a/StringTools/Test/Test.vcxproj b/StringTools/Test/Test.vcxproj index d60a246..51fba2c 100644 --- a/StringTools/Test/Test.vcxproj +++ b/StringTools/Test/Test.vcxproj @@ -25,6 +25,7 @@ Test 10.0 NativeUnitTestProject + Test_Stringtools @@ -89,7 +90,7 @@ - Use + NotUsing Level3 true $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) @@ -104,7 +105,7 @@ - Use + NotUsing Level3 true $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) @@ -119,7 +120,7 @@ - Use + NotUsing Level3 true true @@ -138,7 +139,7 @@ - Use + NotUsing Level3 true true @@ -156,16 +157,13 @@ - - Create - Create - Create - Create - - + + {0270ac5e-eba3-4d8f-8d50-995fd44959b4} + - + + diff --git a/StringTools/Test/Test.vcxproj.filters b/StringTools/Test/Test.vcxproj.filters index 99bfffa..e159865 100644 --- a/StringTools/Test/Test.vcxproj.filters +++ b/StringTools/Test/Test.vcxproj.filters @@ -15,16 +15,11 @@ - + Quelldateien - + Quelldateien - - - Headerdateien - - \ No newline at end of file