From 5fbf07f946995fed76804555a7a5e38f1ef94f38 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Sun, 13 Mar 2022 16:42:40 +0100 Subject: [PATCH] Added tests for string-split, and fixed some bigs --- Exec/.main.cpp.swp | Bin 0 -> 12288 bytes Exec/main.cpp | 2 +- Src/StringTools.cpp | 28 +++++-- Test/.String__Split.cpp.swp | Bin 0 -> 20480 bytes Test/CMakeLists.txt | 1 + Test/String__Split.cpp | 156 ++++++++++++++++++++++++++++++++++++ 6 files changed, 178 insertions(+), 9 deletions(-) create mode 100644 Exec/.main.cpp.swp create mode 100644 Test/.String__Split.cpp.swp create mode 100644 Test/String__Split.cpp diff --git a/Exec/.main.cpp.swp b/Exec/.main.cpp.swp new file mode 100644 index 0000000000000000000000000000000000000000..22cdcce812971e4e3dc77c4bcb5a49cdf5f69a89 GIT binary patch literal 12288 zcmeI&&uY{_90%}Uy@&ABx&Wt^q)I3kvdh;fzJ)2cNOMLptyl_0K25*aSY*Q}Hv-Nee&xP|x z2O;F8+4*QwGftfEq?5_T4})F^K;XPU#lwxQ1$z8wvs-trMk{LZ{^9v?xB~$QKmY;| zfB*y_009WxSOK2Q(N}r9ThlkZJFVybtEWv=5P$##AOHafKmY;|fB*y_009VGLxChC zI$0pPeNP@<{{NqS|DQ_xkL0)Hm*l79yX08nB(EgPk|oIg;(8eYMM%woW*e(DptLoiXKLFh4&t=nw#L5{|U vqSsuP&Efcdy|j8lpUHVv_4)X=Y_#fpnpc@sy_wBAqy9{X3$lPs#(nw= foo = - StringTools::Split("Hello, lol, test", ", "); + StringTools::Split(",,Hello,,lol,,test,,", ",,"); for (const auto& it : foo) std::cout << "'" << it << "'" << std::endl; diff --git a/Src/StringTools.cpp b/Src/StringTools.cpp index 302fad9..346e1bd 100644 --- a/Src/StringTools.cpp +++ b/Src/StringTools.cpp @@ -94,10 +94,14 @@ std::string StringTools::Upper(const std::string& str) { } std::vector StringTools::Split(const std::string& str, const std::string& seperator) { - std::vector toRet; + std::vector toRet; + + // Quick-accept: str length is 0 + if (str.length() == 0) + toRet.push_back(""); // Quick-accept: seperator length is 0 - if (seperator.length() == 0) { + else if (seperator.length() == 0) { for (const char c : str) toRet.push_back(std::string(&c, (&c) + 1)); } @@ -106,15 +110,23 @@ std::vector StringTools::Split(const std::string& str, const std::s std::size_t idx = 0; while (idx != std::string::npos) { std::size_t lastIdx = idx; - idx = str.find(seperator, idx + seperator.length()); + idx = str.find(seperator, idx); - toRet.push_back(str.substr( - lastIdx, - idx - lastIdx - )); + // Grab our substring until the next finding of sep + if (idx != std::string::npos) { + toRet.push_back(str.substr( + lastIdx, + idx - lastIdx + )); - if (idx != std::string::npos) idx += seperator.length(); + } + // No more seperator found. Grab the rest until the end of the string + else { + toRet.push_back(str.substr( + lastIdx + )); + } } } diff --git a/Test/.String__Split.cpp.swp b/Test/.String__Split.cpp.swp new file mode 100644 index 0000000000000000000000000000000000000000..776cfc7b5afa7b96a887bd59bb9fa627015403c7 GIT binary patch literal 20480 zcmeI2U2GIp6vrMtu=)hnF@?_p1d;heDdVni6Phcj2X!b-FvS3)`7xW|p=z z_>uU4i4Y$^K`=oWh*{BEI;b5MRKcFBEj4 z{FLcic6iW{BUYwpXG}Xi9eyU~XN%**uCvEV`(C(z_3CiKcXRgcq~qkhaMJSp;EPl$ zQOM`~P`Xg4e^%Ve3a|nd1&aQzj_#$RGZu}|cdN8k*|K6=rI|ml0;~WlzzVPetN<&( z3a|pKz}>HaUsxnwz-2BBE|or0Sy0+kwu^)PhlA})H?=Pc_VEZomBYcd+)uE}8&-f7 zUtq1@H*o%klr!WkOs6--6e{b6_Xf z1Y)2C{JvC(-@s4c6gUYE0|z9*Fc<{{-U=(Zu8$lO{fhdT8U+)p(1MogL1NMN;;2Iv*Tm|2QW8f$_ z1oB`nFhL6J1doFYu>DuyJfLx~4XgxDfMwtU9u9pC&VzTr+u#jw3>*bp0F`i9Qlf?@ zkv8JXthpcg8`851mTUTs>q&f>voqF|j7ZbY$d0IVEx+j6p0p+k{wESfsuHdQ}ktTue7ELND{F!?FQQLpW7`rw`-aXOgpycqnk`m&wd=@xEs{x zQokFe`n{Z$SJKm?a!J~9wod0+?l3-Ox#^r|HPL1HTx74Dky=F4r4~gs`%Fszjw{D8 zT0|N~6iA{|lXc3hi>dAcJs2pRT5$adh*%IaA{kF4Q~iC3cv~v<^uU&QDy4@@PpDZ){ZQF{ZUQw55$+r0+-z{YZy8lDjRK{%*6R z79)W=sp_I?4OMHYT1QnkRqHi*kZf_Q#W1MI>V~0gb!&StD3nEGFsWg58O9pJSZf&T z45QmH*58?t>8u(W)uTKrpXR6;Og%Y?G%V~*(5UH1W7yo7zH-uW_fj(0e4U)6No9hT z%-YCqdR?>W5k0EMO2=Rssy7W2P&sc^&%-`HKIOaCgf;BsY~O3jWdG+iRU?J4WY(W* z^-V?=)0Db2202b3HWWd_w@>ESq~xgWl$S=X3U+RH*j5Ir`n5FkljgKn8LTs!b`Yt( zIXj&%W-O`on|?YQ3uUz%-)$(l`YNqHl-(#O{*NLqr?{Qs|ML6$j}YH~2+o1$0mc50 zfk(j-@FkA_0(=4}?tc@!295yL7X^e|SOHdm6<`He0akz&U zWfaEW=DURk?}Q8^QhqC>o;fsl!BFEpUvuvlDE^;8%zgn-@Q+vRs$4;g{}wm}j)2Wz z0Q7=ZAOSrKcmz-`fcky#Ti&n&tN<&(3a|pK04u->umY?AE5Hh{0{@}{2$4DcSfw8~ Kmnx^8K>Q6^36>`S literal 0 HcmV?d00001 diff --git a/Test/CMakeLists.txt b/Test/CMakeLists.txt index d4d3ea1..41ad3c9 100644 --- a/Test/CMakeLists.txt +++ b/Test/CMakeLists.txt @@ -15,6 +15,7 @@ add_executable(Test String__Upper.cpp String__Replace_Char.cpp String__Replace_String.cpp + String__Split.cpp # CharTools-Tests Char__IsVowel.cpp diff --git a/Test/String__Split.cpp b/Test/String__Split.cpp new file mode 100644 index 0000000..9da8e94 --- /dev/null +++ b/Test/String__Split.cpp @@ -0,0 +1,156 @@ +#include +#include "Catch2.h" + +// Tests that splitting an empty string always returns {""} +TEST_CASE(__FILE__"/EmptyString", "[Strings][Split]") +{ + SECTION("Empty seperator") { + // Setup + const std::string in = ""; + const std::string sep = ""; + + // Exercise + const std::vector out = StringTools::Split(in, sep); + + // Verify + REQUIRE(out.size() == 1); + REQUIRE(out[0] == ""); + } + + SECTION("Nonempty seperator") { + // Setup + const std::string in = ""; + const std::string sep = ","; + + // Exercise + const std::vector out = StringTools::Split(in, sep); + + // Verify + REQUIRE(out.size() == 1); + REQUIRE(out[0] == ""); + } + + + return; +} + +// Tests that splitting a string with an empty seperator returns all the chars +TEST_CASE(__FILE__"/EmptySeperator", "[Strings][Split]") +{ + // Setup + const std::string in = "hello world"; + const std::string sep = ""; + const std::vector expected = { "h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d" }; + + // Exercise + const std::vector out = StringTools::Split(in, sep); + + // Verify + REQUIRE(out == expected); + + return; +} + +// Tests that splitting a string with a single-char seperator works +TEST_CASE(__FILE__"/SingleCharSeperator", "[Strings][Split]") +{ + // Setup + const std::string in = "0,1,2,3,4,5,6,7,8,9"; + const std::string sep = ","; + const std::vector expected = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; + + // Exercise + const std::vector out = StringTools::Split(in, sep); + + // Verify + REQUIRE(out == expected); + + return; +} + +// Tests that having seperators next to each other gets empty strings +TEST_CASE(__FILE__"/SingleCharSeperatorsNextToEachOther", "[Strings][Split]") +{ + // Setup + const std::string in = "0,1,,3"; + const std::string sep = ","; + const std::vector expected = { "0", "1", "", "3" }; + + // Exercise + const std::vector out = StringTools::Split(in, sep); + + // Verify + REQUIRE(out == expected); + + return; +} + +// Tests that having seperators at index 0 and -1 returns empty strings +TEST_CASE(__FILE__"/SingleCharSeperatorsAtExtremePoints", "[Strings][Split]") +{ + // Setup + const std::string in = ",0,1,2,"; + const std::string sep = ","; + const std::vector expected = { "", "0", "1", "2", "" }; + + // Exercise + const std::vector out = StringTools::Split(in, sep); + + // Verify + REQUIRE(out == expected); + + return; +} + + +// Tests that splitting a string with a multi-char seperator works +TEST_CASE(__FILE__"/MultiCharSeperator", "[Strings][Split]") +{ + // Setup + const std::string in = "0;;1;;2;;3;;4;;5;;6;;7;;8;;9"; + const std::string sep = ";;"; + const std::vector expected = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; + + // Exercise + const std::vector out = StringTools::Split(in, sep); + + // Verify + REQUIRE(out == expected); + + return; +} + +// Tests that having seperators next to each other gets empty strings +TEST_CASE(__FILE__"/MultiCharSeperatorsNextToEachOther", "[Strings][Split]") +{ + // Setup + const std::string in = "0;;1;;;;3"; + const std::string sep = ";;"; + const std::vector expected = { "0", "1", "", "3" }; + + // Exercise + const std::vector out = StringTools::Split(in, sep); + + // Verify + REQUIRE(out == expected); + + return; +} + +// Tests that having seperators at index 0 and -1 returns empty strings +TEST_CASE(__FILE__"/MultiCharSeperatorsAtExtremePoints", "[Strings][Split]") +{ + // Setup + const std::string in = ";;0;;1;;2;;"; + const std::string sep = ";;"; + const std::vector expected = { "", "0", "1", "2", "" }; + + // Exercise + const std::vector out = StringTools::Split(in, sep); + + // Verify + REQUIRE(out == expected); + + return; +} +