From ab1a3672c3392cb2eaf21d020fd09e698e96e255 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Sat, 12 Feb 2022 20:07:48 +0100 Subject: Added tests for char tools --- Src/CharTools.cpp | 34 ++++---- Src/CharTools.h | 18 ++--- Test/CMakeLists.txt | 20 ++++- Test/Char__CopySign.cpp | 69 ++++++++++++++++ Test/Char__IsDigit.cpp | 20 +++++ Test/Char__IsLetter.cpp | 19 +++++ Test/Char__IsLower.cpp | 19 +++++ Test/Char__IsUpper.cpp | 20 +++++ Test/Char__IsVowel.cpp | 19 +++++ Test/Char__MakeLower.cpp | 47 +++++++++++ Test/Char__MakeUpper.cpp | 47 +++++++++++ Test/Lower.cpp | 72 ----------------- Test/Replace_Char.cpp | 156 ------------------------------------ Test/Replace_String.cpp | 170 ---------------------------------------- Test/String__Lower.cpp | 72 +++++++++++++++++ Test/String__Replace_Char.cpp | 156 ++++++++++++++++++++++++++++++++++++ Test/String__Replace_String.cpp | 170 ++++++++++++++++++++++++++++++++++++++++ Test/String__Upper.cpp | 72 +++++++++++++++++ Test/Upper.cpp | 72 ----------------- 19 files changed, 771 insertions(+), 501 deletions(-) create mode 100644 Test/Char__CopySign.cpp create mode 100644 Test/Char__IsDigit.cpp create mode 100644 Test/Char__IsLetter.cpp create mode 100644 Test/Char__IsLower.cpp create mode 100644 Test/Char__IsUpper.cpp create mode 100644 Test/Char__IsVowel.cpp create mode 100644 Test/Char__MakeLower.cpp create mode 100644 Test/Char__MakeUpper.cpp delete mode 100644 Test/Lower.cpp delete mode 100644 Test/Replace_Char.cpp delete mode 100644 Test/Replace_String.cpp create mode 100644 Test/String__Lower.cpp create mode 100644 Test/String__Replace_Char.cpp create mode 100644 Test/String__Replace_String.cpp create mode 100644 Test/String__Upper.cpp delete mode 100644 Test/Upper.cpp diff --git a/Src/CharTools.cpp b/Src/CharTools.cpp index 79cc679..5a7028c 100644 --- a/Src/CharTools.cpp +++ b/Src/CharTools.cpp @@ -2,11 +2,13 @@ #include bool CharTools::IsVowel(const char c, const std::string &vowels) { + const char lc = MakeLower(c); + return std::any_of( vowels.cbegin(), vowels.cend(), - [c](const char vowel) { - return c == vowel; + [lc](const char vowel) { + return lc == vowel; } ); } @@ -18,19 +20,28 @@ bool CharTools::IsLetter(const char c) { return (lowercase_c >= 'a') && (lowercase_c <= 'z'); } -bool CharTools::IsGeneric(const char c) { - return (c == UPPERCASE) || (c == LOWERCASE); +bool CharTools::IsDigit(const char c) { + return (c >= '0') && (c <= '9'); } bool CharTools::IsUpper(const char c) { - if ((!IsLetter(c)) && (!IsGeneric(c))) + if (!IsLetter(c)) return false; else return !(c & (1<<5)); } +bool CharTools::IsLower(const char c) { + // Can't just return !IsUpper(c), because it should still return false for digits and symbols... + + if (!IsLetter(c)) + return false; + else + return (c & (1<<5)); +} + char CharTools::MakeUpper(char c) { - if ((!IsLetter(c)) && (!IsGeneric(c))) + if (!IsLetter(c)) return c; else if (IsUpper(c)) return c; @@ -39,7 +50,7 @@ char CharTools::MakeUpper(char c) { } char CharTools::MakeLower(char c) { - if ((!IsLetter(c)) && (!IsGeneric(c))) + if (!IsLetter(c)) return c; else if (!IsUpper(c)) return c; @@ -47,15 +58,8 @@ char CharTools::MakeLower(char c) { return c | (1<<5); } -char CharTools::GetSign(char c) { - if (IsUpper(c)) - return UPPERCASE; - else - return LOWERCASE; -} - char CharTools::CopySign(char sign, char c) { - if ((!IsLetter(c)) && (!IsGeneric(c))) + if ((!IsLetter(c)) || (!IsLetter(sign))) return c; if (IsUpper(sign)) return MakeUpper(c); diff --git a/Src/CharTools.h b/Src/CharTools.h index 3d7f255..66c32c0 100644 --- a/Src/CharTools.h +++ b/Src/CharTools.h @@ -8,34 +8,28 @@ class CharTools { public: //! Checks whether or not `c` is a vowel. You can define custom vowel characters. - static bool IsVowel(const char c, const std::string& vowels = "euioay"); + static bool IsVowel(const char c, const std::string &vowels = "euioay"); //! Returns whether or not `c` is a letter. static bool IsLetter(const char c); - //! Returns whether or not `c` is a generic character (that contains JUST the sign) - static bool IsGeneric(const char c); + //! Returns whether or not `c` is a digit. + static bool IsDigit(const char c); //! Checks whether or not `c` is an uppercase character. static bool IsUpper(const char c); + //! Checks whether or not `c` is a lowercase character. + static bool IsLower(const char c); + //! Will return `c` as an uppercase character. static char MakeUpper(char c); //! Will return `c` as a lowercase character. static char MakeLower(char c); - //! Will return an empty character with the same sign/capitalization as `c`. - static char GetSign(char c); - //! Will return `c` with the same capitalization as `sign`. static char CopySign(char sign, char c); - - //! Generic uppercase character. - static constexpr char UPPERCASE = 0; - - //! Generic lowercase character. - static constexpr char LOWERCASE = (1<<5); }; #endif //STRINGTOOLS_CHARTOOLS_H diff --git a/Test/CMakeLists.txt b/Test/CMakeLists.txt index 31793bb..d4d3ea1 100644 --- a/Test/CMakeLists.txt +++ b/Test/CMakeLists.txt @@ -9,10 +9,22 @@ link_directories(../Src/cmake-build-debug) add_executable(Test Catch2.h main.cpp - Lower.cpp - Upper.cpp - Replace_Char.cpp - Replace_String.cpp + + # StringTools-Tests + String__Lower.cpp + String__Upper.cpp + String__Replace_Char.cpp + String__Replace_String.cpp + + # CharTools-Tests + Char__IsVowel.cpp + Char__IsLetter.cpp + Char__IsDigit.cpp + Char__IsUpper.cpp + Char__IsLower.cpp + Char__MakeUpper.cpp + Char__MakeLower.cpp + Char__CopySign.cpp ) target_link_libraries(Test StringTools) diff --git a/Test/Char__CopySign.cpp b/Test/Char__CopySign.cpp new file mode 100644 index 0000000..b205575 --- /dev/null +++ b/Test/Char__CopySign.cpp @@ -0,0 +1,69 @@ +#include +#include "Catch2.h" + +TEST_CASE(__FILE__"/JustChars", "[Char][CopySign]") +{ + // Setup + // Correct letters + const std::string in = "tHEEuEUROPEanunioNCOnsiStsOFStATESIncLudingGERmanySWedenAndfRanCE"; + // Correct signs + const std::string signs = "AaaAAApqlkzicZnionceroigjreiojiopjaopickwapPjfipojWqfpohoijFucmwp"; + // Correct signs and letters + const std::string expected = "TheEUEuropeanUnionconsistsofstatesincludingGermanySwedenandFrance"; + + // Exercise + std::string out = in; + for (std::size_t i = 0; i < in.size(); i++) + { + const char cs = signs[i]; + char& co = out[i]; + co = CharTools::CopySign(cs, co); + } + + // Verify: + REQUIRE(out == expected); +} + +TEST_CASE(__FILE__"/WithSymbols", "[Char][CopySign]") +{ + // Setup + // Correct letters + const std::string in = "ThE eu (euRoPeAN uNIon) cONsiSts Of 20 STAtes, iNCLUDInG GeRMAnY, sweden, aND fRancE."; + // Correct signs + const std::string signs = "DwefOPerKofkaqdioJeriofgjqeiopqwqefijoqgehjloivxcvmopfkuoQpwfioqjiOqgjeprjgnvqPemrqij"; + // Correct signs and letters + const std::string expected = "The EU (European Union) consists of 20 states, including Germany, Sweden, and France."; + + // Exercise + std::string out = in; + for (std::size_t i = 0; i < in.size(); i++) + { + const char cs = signs[i]; + char& co = out[i]; + co = CharTools::CopySign(cs, co); + } + + // Verify: + REQUIRE(out == expected); +} + +TEST_CASE(__FILE__"/DoesntChangeSignsIfSymbolSupplied", "[Char][CopySign]") +{ + // Setup + const std::string in = "ThE eu (euRoPeAN uNIon) cONsiSts Of 20 STAtes, iNCLUDInG GeRMAnY, sweden, aND fRancE."; + const std::string signs = "!§$)=%164)';:'*?)/!?/&()()?)*'_;:_,.93ß04750928372!!$==)()/!§$)=%)*'';:'*?)/!1572?/&("; + const std::string expected = in; + + // Exercise + std::string out = in; + for (std::size_t i = 0; i < in.size(); i++) + { + const char cs = signs[i]; + + char& co = out[i]; + co = CharTools::CopySign(cs, co); + } + + // Verify: + REQUIRE(out == expected); +} \ No newline at end of file diff --git a/Test/Char__IsDigit.cpp b/Test/Char__IsDigit.cpp new file mode 100644 index 0000000..a388f98 --- /dev/null +++ b/Test/Char__IsDigit.cpp @@ -0,0 +1,20 @@ +#include +#include "Catch2.h" + +// Tests character digit-ness by checking it against a map +TEST_CASE(__FILE__"/MapTest", "[Char][IsUpper]") +{ + // Setup + const std::string in = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHJIKLMNOPQRSTUVWXYZ!&/()=?*'#-.,;:_"; + const std::string map = "000000000000000000000000001111111111000000000000000000000000000000000000000000"; + // 0 -> no digit, 1 -> digit + + // Verify that I didn't frick up compiling the map by hand + if (in.length() != map.length()) + FAIL("map.size() does not match in.size(). (" << in.length() << " : " << map.length() << ")"); + + // Exercise & Verify + for (std::size_t i = 0; i < in.size(); i++) + if (CharTools::IsDigit(in[i]) != (map[i] == '1')) + FAIL("'" << in[i] << "' differs from the map. Map says '" << map[i] << "', 1-> is a digit. Check first if the map is wrong, before trying to debug."); +} diff --git a/Test/Char__IsLetter.cpp b/Test/Char__IsLetter.cpp new file mode 100644 index 0000000..08f27a6 --- /dev/null +++ b/Test/Char__IsLetter.cpp @@ -0,0 +1,19 @@ +#include +#include "Catch2.h" + +// Tests character letter-ness by checking it against a map +TEST_CASE(__FILE__"/MapTest", "[Char][IsLetter]") +{ + // Setup + const std::string in = "New album 'Cowboy Tears' out February 18! I am excited!"; + const std::string map = "1110111110011111101111100111011111111000001011011111110"; // 0 -> no letter, 1 -> letter + + // Verify that I didn't frick up compiling the map by hand + if (in.length() != map.length()) + FAIL("map.size() does not match in.size(). (" << in.length() << " : " << map.length() << ")"); + + // Exercise & Verify + for (std::size_t i = 0; i < in.size(); i++) + if (CharTools::IsLetter(in[i]) != (map[i] == '1')) + FAIL("'" << in[i] << "' differs from the map. Map says '" << map[i] << "', 1-> is a letter. Check first if the map is wrong, before trying to debug."); +} diff --git a/Test/Char__IsLower.cpp b/Test/Char__IsLower.cpp new file mode 100644 index 0000000..69390f4 --- /dev/null +++ b/Test/Char__IsLower.cpp @@ -0,0 +1,19 @@ +#include +#include "Catch2.h" + +// Tests character sign by checking it against a map +TEST_CASE(__FILE__"/MapTest", "[Char][IsLower]") +{ + // Setup + const std::string in = "New album 'Cowboy Tears' out February 18! I am excited!"; + const std::string map = "0110111110001111100111100111001111111000000011011111110"; // 1 -> lowercase, 0 -> not lowercase + + // Verify that I didn't frick up compiling the map by hand + if (in.length() != map.length()) + FAIL("map.size() does not match in.size(). (" << in.length() << " : " << map.length() << ")"); + + // Exercise & Verify + for (std::size_t i = 0; i < in.size(); i++) + if (CharTools::IsLower(in[i]) != (map[i] == '1')) + FAIL("'" << in[i] << "' differs from the map. Map says '" << map[i] << "', 1-> is a lowercase. Check first if the map is wrong, before trying to debug."); +} diff --git a/Test/Char__IsUpper.cpp b/Test/Char__IsUpper.cpp new file mode 100644 index 0000000..6bc10b3 --- /dev/null +++ b/Test/Char__IsUpper.cpp @@ -0,0 +1,20 @@ +#include +#include "Catch2.h" +#include + +// Tests character sign by checking it against a map +TEST_CASE(__FILE__"/MapTest", "[Char][IsUpper]") +{ + // Setup + const std::string in = "New album 'Cowboy Tears' out February 18! I am excited!"; + const std::string map = "1000000000010000001000000000010000000000001000000000000"; // 1 -> uppercase, 0 -> not uppercase + + // Verify that I didn't frick up compiling the map by hand + if (in.length() != map.length()) + FAIL("map.size() does not match in.size(). (" << in.length() << " : " << map.length() << ")"); + + // Exercise & Verify + for (std::size_t i = 0; i < in.size(); i++) + if (CharTools::IsUpper(in[i]) != (map[i] == '1')) + FAIL("'" << in[i] << "' differs from the map. Map says '" << map[i] << "', 1-> is an uppercase. Check first if the map is wrong, before trying to debug."); +} diff --git a/Test/Char__IsVowel.cpp b/Test/Char__IsVowel.cpp new file mode 100644 index 0000000..31e281d --- /dev/null +++ b/Test/Char__IsVowel.cpp @@ -0,0 +1,19 @@ +#include +#include "Catch2.h" + +// Tests character vowel-ness by checking it against a map +TEST_CASE(__FILE__"/MapTest", "[Char][IsVowel]") +{ + // Setup + const std::string in = "New album 'Cowboy Tears' out February 18! I am excited!"; + const std::string map = "0100100100001001100110000110001001101000001010010010100"; // 0 -> no vowel, 1 -> vowel + + // Verify that I didn't frick up compiling the map by hand + if (in.length() != map.length()) + FAIL("map.size() does not match in.size(). (" << in.length() << " : " << map.length() << ")"); + + // Exercise & Verify + for (std::size_t i = 0; i < in.size(); i++) + if (CharTools::IsVowel(in[i]) != (map[i] == '1')) + FAIL("'" << in[i] << "' differs from the map. Map says '" << map[i] << "', 1-> is a vowel. Check first if the map is wrong, before trying to debug."); +} diff --git a/Test/Char__MakeLower.cpp b/Test/Char__MakeLower.cpp new file mode 100644 index 0000000..524992d --- /dev/null +++ b/Test/Char__MakeLower.cpp @@ -0,0 +1,47 @@ +#include +#include "Catch2.h" + +TEST_CASE(__FILE__"/LowerToUpper_NoSymbols", "[Char][MakeLower]") +{ + // Setup + const std::string in = "NEWALBUMCOWBOYTEARSOUTFEBRUARYIAMEXCITED"; + const std::string expected = "newalbumcowboytearsoutfebruaryiamexcited"; + + // Exercise + std::string out = in; + for (char& c : out) + c = CharTools::MakeLower(c); + + // Verify: + REQUIRE(out == expected); +} + +TEST_CASE(__FILE__"/LowerToUpper_Symbols", "[Char][MakeLower]") +{ + // Setup + const std::string in = "NEW ALBUM 'COWBOY TEARS' OUT FEBRUARY 18! I AM EXCITED!"; + const std::string expected = "new album 'cowboy tears' out february 18! i am excited!"; + + // Exercise + std::string out = in; + for (char& c : out) + c = CharTools::MakeLower(c); + + // Verify: + REQUIRE(out == expected); +} + +TEST_CASE(__FILE__"/LowerToUpper_Mixed_And_Symbols", "[Char][MakeLower]") +{ + // Setup + const std::string in = "New album 'Cowboy Tears' out February 18! I am excited!"; + const std::string expected = "new album 'cowboy tears' out february 18! i am excited!"; + + // Exercise + std::string out = in; + for (char& c : out) + c = CharTools::MakeLower(c); + + // Verify: + REQUIRE(out == expected); +} diff --git a/Test/Char__MakeUpper.cpp b/Test/Char__MakeUpper.cpp new file mode 100644 index 0000000..8a808d8 --- /dev/null +++ b/Test/Char__MakeUpper.cpp @@ -0,0 +1,47 @@ +#include +#include "Catch2.h" + +TEST_CASE(__FILE__"/LowerToUpper_NoSymbols", "[Char][MakeUpper]") +{ + // Setup + const std::string in = "newalbumcowboytearsoutfebruaryiamexcited"; + const std::string expected = "NEWALBUMCOWBOYTEARSOUTFEBRUARYIAMEXCITED"; + + // Exercise + std::string out = in; + for (char& c : out) + c = CharTools::MakeUpper(c); + + // Verify: + REQUIRE(out == expected); +} + +TEST_CASE(__FILE__"/LowerToUpper_Symbols", "[Char][MakeUpper]") +{ + // Setup + const std::string in = "new album 'Cowboy Tears' out february 18! i am excited!"; + const std::string expected = "NEW ALBUM 'COWBOY TEARS' OUT FEBRUARY 18! I AM EXCITED!"; + + // Exercise + std::string out = in; + for (char& c : out) + c = CharTools::MakeUpper(c); + + // Verify: + REQUIRE(out == expected); +} + +TEST_CASE(__FILE__"/LowerToUpper_Mixed_And_Symbols", "[Char][MakeUpper]") +{ + // Setup + const std::string in = "New album 'Cowboy Tears' out February 18! I am excited!"; + const std::string expected = "NEW ALBUM 'COWBOY TEARS' OUT FEBRUARY 18! I AM EXCITED!"; + + // Exercise + std::string out = in; + for (char& c : out) + c = CharTools::MakeUpper(c); + + // Verify: + REQUIRE(out == expected); +} diff --git a/Test/Lower.cpp b/Test/Lower.cpp deleted file mode 100644 index 8e4fe27..0000000 --- a/Test/Lower.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include "Catch2.h" - -// Tests that lowering an empty string returns an empty string -TEST_CASE(__FILE__"/EmptyString", "[Lower]") -{ - // Setup - const std::string in = ""; - - // Exercise - const std::string out = StringTools::Lower(in); - - // Verify - REQUIRE(out == ""); - return; -} - -// Tests that lowering a string without any letters returns itself -TEST_CASE(__FILE__"/Symbols", "[Lower]") -{ - // Setup - const std::string in = "66! _-\n*"; - - // Exercise - const std::string out = StringTools::Lower(in); - - // Verify - REQUIRE(out == "66! _-\n*"); - return; -} - -// Tests that lowering a string of lowercase letters returns itself -TEST_CASE(__FILE__"/AlreadyLowered", "[Lower]") -{ - // Setup - const std::string in = "ughareyouserious"; - - // Exercise - const std::string out = StringTools::Lower(in); - - // Verify - REQUIRE(out == "ughareyouserious"); - return; -} - -// Tests that lowering a string of uppercase letters returns the lowercase version -TEST_CASE(__FILE__"/Uppercase", "[Lower]") -{ - // Setup - const std::string in = "UGHAREYOUSERIOUS"; - - // Exercise - const std::string out = StringTools::Lower(in); - - // Verify - REQUIRE(out == "ughareyouserious"); - return; -} - -// Tests that lowering a string of uppercase, lowercase letters and symbols returns the lowercase version -TEST_CASE(__FILE__"/Mixed", "[Lower]") -{ - // Setup - const std::string in = "Ugh, Are You Serious?! DON'T DO THAT!!!"; - - // Exercise - const std::string out = StringTools::Lower(in); - - // Verify - REQUIRE(out == "ugh, are you serious?! don't do that!!!"); - return; -} diff --git a/Test/Replace_Char.cpp b/Test/Replace_Char.cpp deleted file mode 100644 index 72d5622..0000000 --- a/Test/Replace_Char.cpp +++ /dev/null @@ -1,156 +0,0 @@ -#include -#include "Catch2.h" - -// Tests that replacing something in an empty string returns an empty string -TEST_CASE(__FILE__"/EmptyString", "[ReplaceChar]") -{ - // Setup - const std::string in = ""; - - // Exercise - const std::string out = StringTools::Replace(in, 'a', "Subst"); - - // Verify - REQUIRE(out == ""); - return; -} - -// Tests that replacing a char to an empty string works -TEST_CASE(__FILE__"/Single_ReplaceToEmpty", "[ReplaceChar]") -{ - // Setup - const std::string in = "i"; - - // Exercise - const std::string out = StringTools::Replace(in, 'i', ""); - - // Verify - REQUIRE(out == ""); - return; -} - -// Tests that replacing to a single char works -TEST_CASE(__FILE__"/Single_ReplaceToSingleChar", "[ReplaceChar]") -{ - // Setup - const std::string in = "a"; - - // Exercise - const std::string out = StringTools::Replace(in, 'a', "i"); - - // Verify - REQUIRE(out == "i"); - return; -} - -// Tests that replacing to a single char works, passing a char -TEST_CASE(__FILE__"/Single_ReplaceToSingleChar_AsChar", "[ReplaceChar]") -{ - // Setup - const std::string in = "Oilbanger"; - - // Exercise - const std::string out = StringTools::Replace(in, 'a', 'i'); - - // Verify - REQUIRE(out == "Oilbinger"); - return; -} - -// Tests that replacing the find to something longer works -TEST_CASE(__FILE__"/Single_ReplaceToLonger", "[ReplaceChar]") -{ - // Setup - const std::string in = "Littled"; - - // Exercise - const std::string out = StringTools::Replace(in, 'd', "binger"); - - // Verify - REQUIRE(out == "Littlebinger"); - return; -} - -// Tests that replacing a char to an empty string works -TEST_CASE(__FILE__"/Multiple_ReplaceToEmpty", "[ReplaceChar]") -{ - // Setup - const std::string in = "dirty dogs dig dirt daringly"; - - // Exercise - const std::string out = StringTools::Replace(in, 'd', ""); - - // Verify - REQUIRE(out == "irty ogs ig irt aringly"); - return; -} - -// Tests that replacing to a single char works -TEST_CASE(__FILE__"/Multiple_ReplaceToSingleChar", "[ReplaceChar]") -{ - // Setup - const std::string in = "Oilbanger, Bangerfanger, Lattle brattle oaly skattle."; - - // Exercise - const std::string out = StringTools::Replace(in, 'a', "i"); - - // Verify - REQUIRE(out == "Oilbinger, Bingerfinger, Little brittle oily skittle."); - return; -} - -// Tests that replacing to a single char works, passing a char -TEST_CASE(__FILE__"/Multiple_ReplaceToSingleChar_AsChar", "[ReplaceChar]") -{ - // Setup - const std::string in = "Oilbanger, Bangerfanger, Lattle brattle oaly skattle."; - - // Exercise - const std::string out = StringTools::Replace(in, 'a', 'i'); - - // Verify - REQUIRE(out == "Oilbinger, Bingerfinger, Little brittle oily skittle."); - return; -} - -// Tests that replacing the find to something longer works -TEST_CASE(__FILE__"/Multiple_ReplaceToLonger", "[ReplaceChar]") -{ - // Setup - const std::string in = "d d d d d d d d"; - - // Exercise - const std::string out = StringTools::Replace(in, 'd', "bla"); - - // Verify - REQUIRE(out == "bla bla bla bla bla bla bla bla"); - return; -} - -// Tests that the replacer ignores chars put in by the replacer -TEST_CASE(__FILE__"/ReplacerIgnoresReplaced", "[ReplaceChar]") -{ - // Setup - const std::string in = "b b b b b b b b"; - - // Exercise - const std::string out = StringTools::Replace(in, 'b', "bla"); - - // Verify - REQUIRE(out == "bla bla bla bla bla bla bla bla"); - return; -} - -// Tests that replacing succesive findings works -TEST_CASE(__FILE__"/Replace_Successive", "[ReplaceChar]") -{ - // Setup - const std::string in = "bbbbbbbb"; - - // Exercise - const std::string out = StringTools::Replace(in, 'b', "bla"); - - // Verify - REQUIRE(out == "blablablablablablablabla"); - return; -} diff --git a/Test/Replace_String.cpp b/Test/Replace_String.cpp deleted file mode 100644 index 853d411..0000000 --- a/Test/Replace_String.cpp +++ /dev/null @@ -1,170 +0,0 @@ -#include -#include "Catch2.h" - -// Tests that replacing something in an empty string returns an empty string -TEST_CASE(__FILE__"/EmptyString", "[ReplaceString]") -{ - // Setup - const std::string in = ""; - - // Exercise - const std::string out = StringTools::Replace(in, "burger", "Subst"); - - // Verify - REQUIRE(out == ""); - return; -} - -// Tests that replacing a string to an empty string works -TEST_CASE(__FILE__"/Single_ReplaceToEmpty", "[ReplaceString]") -{ - // Setup - const std::string in = "Squarepants"; - - // Exercise - const std::string out = StringTools::Replace(in, "Squarepants", ""); - - // Verify - REQUIRE(out == ""); - return; -} - -// Tests that replacing to a single char works -TEST_CASE(__FILE__"/Single_ReplaceToSingleChar", "[ReplaceString]") -{ - // Setup - const std::string in = "Squarepants"; - - // Exercise - const std::string out = StringTools::Replace(in, "Squarepants", "i"); - - // Verify - REQUIRE(out == "i"); - return; -} - -// Tests that replacing to a single char works, passing a char -TEST_CASE(__FILE__"/Single_ReplaceToSingleChar_AsChar", "[ReplaceString]") -{ - // Setup - const std::string in = "Oilbanger"; - - // Exercise - const std::string out = StringTools::Replace(in, "Oilbanger", 'i'); - - // Verify - REQUIRE(out == "i"); - return; -} - -// Tests that replacing the find to something longer works -TEST_CASE(__FILE__"/Single_ReplaceToLonger", "[ReplaceString]") -{ - // Setup - const std::string in = "LittleDong"; - - // Exercise - const std::string out = StringTools::Replace(in, "Dong", "Binger"); - - // Verify - REQUIRE(out == "LittleBinger"); - return; -} - -// Tests that replacing a string to an empty string works -TEST_CASE(__FILE__"/Multiple_ReplaceToEmpty", "[ReplaceString]") -{ - // Setup - const std::string in = "The fucking dogs are fucking eating the fucking chicken."; - - // Exercise - const std::string out = StringTools::Replace(in, "fucking ", ""); - - // Verify - REQUIRE(out == "The dogs are eating the chicken."); - return; -} - -// Tests that replacing to a single char works -TEST_CASE(__FILE__"/Multiple_ReplaceToSingleChar", "[ReplaceString]") -{ - // Setup - const std::string in = "Oilbsmearynger, Bsmearyngerfsmearynger, Lsmearyttle brsmearyttle osmearyly sksmearyttle."; - - // Exercise - const std::string out = StringTools::Replace(in, "smeary", "i"); - - // Verify - REQUIRE(out == "Oilbinger, Bingerfinger, Little brittle oily skittle."); - return; -} - -// Tests that replacing to a single char works, passing a char -TEST_CASE(__FILE__"/Multiple_ReplaceToSingleChar_AsChar", "[ReplaceString]") -{ - // Setup - const std::string in = "Oilbsmearynger, Bsmearyngerfsmearynger, Lsmearyttle brsmearyttle osmearyly sksmearyttle."; - - // Exercise - const std::string out = StringTools::Replace(in, "smeary", 'i'); - - // Verify - REQUIRE(out == "Oilbinger, Bingerfinger, Little brittle oily skittle."); - return; -} - -// Tests that replacing the find to something longer works -TEST_CASE(__FILE__"/Multiple_ReplaceToLonger", "[ReplaceString]") -{ - // Setup - const std::string in = "honk honk honk honk honk honk honk honk"; - - // Exercise - const std::string out = StringTools::Replace(in, "honk", "hallery"); - - // Verify - REQUIRE(out == "hallery hallery hallery hallery hallery hallery hallery hallery"); - return; -} - -// Tests that the replacer ignores chars put in by the replacer -TEST_CASE(__FILE__"/ReplacerIgnoresReplaced", "[ReplaceString]") -{ - // Setup - const std::string in = "honk honk honk honk honk honk honk honk"; - - // Exercise - const std::string out = StringTools::Replace(in, "honk", "honka"); - - // Verify - REQUIRE(out == "honka honka honka honka honka honka honka honka"); - return; -} - -// Tests that replacing successive findings works -TEST_CASE(__FILE__"/Replace_Successive", "[ReplaceString]") -{ - // Setup - const std::string in = "honkhonkhonkhonkhonkhonkhonkhonk"; - - // Exercise - const std::string out = StringTools::Replace(in, "honk", "hallery"); - - // Verify - REQUIRE(out == "halleryhalleryhalleryhalleryhalleryhalleryhalleryhallery"); - return; -} - -// Tests that if find.length() == 0, it returns just the input -TEST_CASE(__FILE__"/Find_Length0_Returns_Input", "[ReplaceString]") -{ - // Setup - const std::string in = "Littled"; - - // Exercise - const std::string out = StringTools::Replace(in, "", "binger"); - - // Verify - REQUIRE(out == "Littled"); - return; -} diff --git a/Test/String__Lower.cpp b/Test/String__Lower.cpp new file mode 100644 index 0000000..c198394 --- /dev/null +++ b/Test/String__Lower.cpp @@ -0,0 +1,72 @@ +#include +#include "Catch2.h" + +// Tests that lowering an empty string returns an empty string +TEST_CASE(__FILE__"/EmptyString", "[Strings][Lower]") +{ + // Setup + const std::string in = ""; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + REQUIRE(out == ""); + return; +} + +// Tests that lowering a string without any letters returns itself +TEST_CASE(__FILE__"/Symbols", "[Strings][Lower]") +{ + // Setup + const std::string in = "66! _-\n*"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + REQUIRE(out == "66! _-\n*"); + return; +} + +// Tests that lowering a string of lowercase letters returns itself +TEST_CASE(__FILE__"/AlreadyLowered", "[Strings][Lower]") +{ + // Setup + const std::string in = "ughareyouserious"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + REQUIRE(out == "ughareyouserious"); + return; +} + +// Tests that lowering a string of uppercase letters returns the lowercase version +TEST_CASE(__FILE__"/Uppercase", "[Strings][Lower]") +{ + // Setup + const std::string in = "UGHAREYOUSERIOUS"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + REQUIRE(out == "ughareyouserious"); + return; +} + +// Tests that lowering a string of uppercase, lowercase letters and symbols returns the lowercase version +TEST_CASE(__FILE__"/Mixed", "[Strings][Lower]") +{ + // Setup + const std::string in = "Ugh, Are You Serious?! DON'T DO THAT!!!"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + REQUIRE(out == "ugh, are you serious?! don't do that!!!"); + return; +} diff --git a/Test/String__Replace_Char.cpp b/Test/String__Replace_Char.cpp new file mode 100644 index 0000000..b935cc2 --- /dev/null +++ b/Test/String__Replace_Char.cpp @@ -0,0 +1,156 @@ +#include +#include "Catch2.h" + +// Tests that replacing something in an empty string returns an empty string +TEST_CASE(__FILE__"/EmptyString", "[Strings][ReplaceChar]") +{ + // Setup + const std::string in = ""; + + // Exercise + const std::string out = StringTools::Replace(in, 'a', "Subst"); + + // Verify + REQUIRE(out == ""); + return; +} + +// Tests that replacing a char to an empty string works +TEST_CASE(__FILE__"/Single_ReplaceToEmpty", "[Strings][ReplaceChar]") +{ + // Setup + const std::string in = "i"; + + // Exercise + const std::string out = StringTools::Replace(in, 'i', ""); + + // Verify + REQUIRE(out == ""); + return; +} + +// Tests that replacing to a single char works +TEST_CASE(__FILE__"/Single_ReplaceToSingleChar", "[Strings][ReplaceChar]") +{ + // Setup + const std::string in = "a"; + + // Exercise + const std::string out = StringTools::Replace(in, 'a', "i"); + + // Verify + REQUIRE(out == "i"); + return; +} + +// Tests that replacing to a single char works, passing a char +TEST_CASE(__FILE__"/Single_ReplaceToSingleChar_AsChar", "[Strings][ReplaceChar]") +{ + // Setup + const std::string in = "Oilbanger"; + + // Exercise + const std::string out = StringTools::Replace(in, 'a', 'i'); + + // Verify + REQUIRE(out == "Oilbinger"); + return; +} + +// Tests that replacing the find to something longer works +TEST_CASE(__FILE__"/Single_ReplaceToLonger", "[Strings][ReplaceChar]") +{ + // Setup + const std::string in = "Littled"; + + // Exercise + const std::string out = StringTools::Replace(in, 'd', "binger"); + + // Verify + REQUIRE(out == "Littlebinger"); + return; +} + +// Tests that replacing a char to an empty string works +TEST_CASE(__FILE__"/Multiple_ReplaceToEmpty", "[Strings][ReplaceChar]") +{ + // Setup + const std::string in = "dirty dogs dig dirt daringly"; + + // Exercise + const std::string out = StringTools::Replace(in, 'd', ""); + + // Verify + REQUIRE(out == "irty ogs ig irt aringly"); + return; +} + +// Tests that replacing to a single char works +TEST_CASE(__FILE__"/Multiple_ReplaceToSingleChar", "[Strings][ReplaceChar]") +{ + // Setup + const std::string in = "Oilbanger, Bangerfanger, Lattle brattle oaly skattle."; + + // Exercise + const std::string out = StringTools::Replace(in, 'a', "i"); + + // Verify + REQUIRE(out == "Oilbinger, Bingerfinger, Little brittle oily skittle."); + return; +} + +// Tests that replacing to a single char works, passing a char +TEST_CASE(__FILE__"/Multiple_ReplaceToSingleChar_AsChar", "[Strings][ReplaceChar]") +{ + // Setup + const std::string in = "Oilbanger, Bangerfanger, Lattle brattle oaly skattle."; + + // Exercise + const std::string out = StringTools::Replace(in, 'a', 'i'); + + // Verify + REQUIRE(out == "Oilbinger, Bingerfinger, Little brittle oily skittle."); + return; +} + +// Tests that replacing the find to something longer works +TEST_CASE(__FILE__"/Multiple_ReplaceToLonger", "[Strings][ReplaceChar]") +{ + // Setup + const std::string in = "d d d d d d d d"; + + // Exercise + const std::string out = StringTools::Replace(in, 'd', "bla"); + + // Verify + REQUIRE(out == "bla bla bla bla bla bla bla bla"); + return; +} + +// Tests that the replacer ignores chars put in by the replacer +TEST_CASE(__FILE__"/ReplacerIgnoresReplaced", "[Strings][ReplaceChar]") +{ + // Setup + const std::string in = "b b b b b b b b"; + + // Exercise + const std::string out = StringTools::Replace(in, 'b', "bla"); + + // Verify + REQUIRE(out == "bla bla bla bla bla bla bla bla"); + return; +} + +// Tests that replacing succesive findings works +TEST_CASE(__FILE__"/Replace_Successive", "[Strings][ReplaceChar]") +{ + // Setup + const std::string in = "bbbbbbbb"; + + // Exercise + const std::string out = StringTools::Replace(in, 'b', "bla"); + + // Verify + REQUIRE(out == "blablablablablablablabla"); + return; +} diff --git a/Test/String__Replace_String.cpp b/Test/String__Replace_String.cpp new file mode 100644 index 0000000..7547fcd --- /dev/null +++ b/Test/String__Replace_String.cpp @@ -0,0 +1,170 @@ +#include +#include "Catch2.h" + +// Tests that replacing something in an empty string returns an empty string +TEST_CASE(__FILE__"/EmptyString", "[Strings][ReplaceString]") +{ + // Setup + const std::string in = ""; + + // Exercise + const std::string out = StringTools::Replace(in, "burger", "Subst"); + + // Verify + REQUIRE(out == ""); + return; +} + +// Tests that replacing a string to an empty string works +TEST_CASE(__FILE__"/Single_ReplaceToEmpty", "[Strings][ReplaceString]") +{ + // Setup + const std::string in = "Squarepants"; + + // Exercise + const std::string out = StringTools::Replace(in, "Squarepants", ""); + + // Verify + REQUIRE(out == ""); + return; +} + +// Tests that replacing to a single char works +TEST_CASE(__FILE__"/Single_ReplaceToSingleChar", "[Strings][ReplaceString]") +{ + // Setup + const std::string in = "Squarepants"; + + // Exercise + const std::string out = StringTools::Replace(in, "Squarepants", "i"); + + // Verify + REQUIRE(out == "i"); + return; +} + +// Tests that replacing to a single char works, passing a char +TEST_CASE(__FILE__"/Single_ReplaceToSingleChar_AsChar", "[Strings][ReplaceString]") +{ + // Setup + const std::string in = "Oilbanger"; + + // Exercise + const std::string out = StringTools::Replace(in, "Oilbanger", 'i'); + + // Verify + REQUIRE(out == "i"); + return; +} + +// Tests that replacing the find to something longer works +TEST_CASE(__FILE__"/Single_ReplaceToLonger", "[Strings][ReplaceString]") +{ + // Setup + const std::string in = "LittleDong"; + + // Exercise + const std::string out = StringTools::Replace(in, "Dong", "Binger"); + + // Verify + REQUIRE(out == "LittleBinger"); + return; +} + +// Tests that replacing a string to an empty string works +TEST_CASE(__FILE__"/Multiple_ReplaceToEmpty", "[Strings][ReplaceString]") +{ + // Setup + const std::string in = "The fucking dogs are fucking eating the fucking chicken."; + + // Exercise + const std::string out = StringTools::Replace(in, "fucking ", ""); + + // Verify + REQUIRE(out == "The dogs are eating the chicken."); + return; +} + +// Tests that replacing to a single char works +TEST_CASE(__FILE__"/Multiple_ReplaceToSingleChar", "[Strings][ReplaceString]") +{ + // Setup + const std::string in = "Oilbsmearynger, Bsmearyngerfsmearynger, Lsmearyttle brsmearyttle osmearyly sksmearyttle."; + + // Exercise + const std::string out = StringTools::Replace(in, "smeary", "i"); + + // Verify + REQUIRE(out == "Oilbinger, Bingerfinger, Little brittle oily skittle."); + return; +} + +// Tests that replacing to a single char works, passing a char +TEST_CASE(__FILE__"/Multiple_ReplaceToSingleChar_AsChar", "[Strings][ReplaceString]") +{ + // Setup + const std::string in = "Oilbsmearynger, Bsmearyngerfsmearynger, Lsmearyttle brsmearyttle osmearyly sksmearyttle."; + + // Exercise + const std::string out = StringTools::Replace(in, "smeary", 'i'); + + // Verify + REQUIRE(out == "Oilbinger, Bingerfinger, Little brittle oily skittle."); + return; +} + +// Tests that replacing the find to something longer works +TEST_CASE(__FILE__"/Multiple_ReplaceToLonger", "[Strings][ReplaceString]") +{ + // Setup + const std::string in = "honk honk honk honk honk honk honk honk"; + + // Exercise + const std::string out = StringTools::Replace(in, "honk", "hallery"); + + // Verify + REQUIRE(out == "hallery hallery hallery hallery hallery hallery hallery hallery"); + return; +} + +// Tests that the replacer ignores chars put in by the replacer +TEST_CASE(__FILE__"/ReplacerIgnoresReplaced", "[Strings][ReplaceString]") +{ + // Setup + const std::string in = "honk honk honk honk honk honk honk honk"; + + // Exercise + const std::string out = StringTools::Replace(in, "honk", "honka"); + + // Verify + REQUIRE(out == "honka honka honka honka honka honka honka honka"); + return; +} + +// Tests that replacing successive findings works +TEST_CASE(__FILE__"/Replace_Successive", "[Strings][ReplaceString]") +{ + // Setup + const std::string in = "honkhonkhonkhonkhonkhonkhonkhonk"; + + // Exercise + const std::string out = StringTools::Replace(in, "honk", "hallery"); + + // Verify + REQUIRE(out == "halleryhalleryhalleryhalleryhalleryhalleryhalleryhallery"); + return; +} + +// Tests that if find.length() == 0, it returns just the input +TEST_CASE(__FILE__"/Find_Length0_Returns_Input", "[Strings][ReplaceString]") +{ + // Setup + const std::string in = "Littled"; + + // Exercise + const std::string out = StringTools::Replace(in, "", "binger"); + + // Verify + REQUIRE(out == "Littled"); + return; +} diff --git a/Test/String__Upper.cpp b/Test/String__Upper.cpp new file mode 100644 index 0000000..37fe7b7 --- /dev/null +++ b/Test/String__Upper.cpp @@ -0,0 +1,72 @@ +#include +#include "Catch2.h" + +// Tests that uppering an empty string returns an empty string +TEST_CASE(__FILE__"/EmptyString", "[Strings][Upper]") +{ + // Setup + const std::string in = ""; + + // Exercise + const std::string out = StringTools::Upper(in); + + // Verify + REQUIRE(out == ""); + return; +} + +// Tests that uppering a string without any letters returns itself +TEST_CASE(__FILE__"/Symbols", "[Strings][Upper]") +{ + // Setup + const std::string in = "66! _-\n*"; + + // Exercise + const std::string out = StringTools::Upper(in); + + // Verify + REQUIRE(out == "66! _-\n*"); + return; +} + +// Tests that uppering a string of uppercase letters returns itself +TEST_CASE(__FILE__"/AlreadyUppered", "[Strings][Upper]") +{ + // Setup + const std::string in = "UGHAREYOUSERIOUS"; + + // Exercise + const std::string out = StringTools::Upper(in); + + // Verify + REQUIRE(out == "UGHAREYOUSERIOUS"); + return; +} + +// Tests that uppering a string of lowercase letters returns the uppercase version +TEST_CASE(__FILE__"/Lowercase", "[Strings][Upper]") +{ + // Setup + const std::string in = "ughareyouserious"; + + // Exercise + const std::string out = StringTools::Upper(in); + + // Verify + REQUIRE(out == "UGHAREYOUSERIOUS"); + return; +} + +// Tests that uppering a string of uppercase, lowercase letters and symbols returns the uppercase version +TEST_CASE(__FILE__"/Mixed", "[Strings][Upper]") +{ + // Setup + const std::string in = "Ugh, Are You Serious?! DON'T do that!!!"; + + // Exercise + const std::string out = StringTools::Upper(in); + + // Verify + REQUIRE(out == "UGH, ARE YOU SERIOUS?! DON'T DO THAT!!!"); + return; +} diff --git a/Test/Upper.cpp b/Test/Upper.cpp deleted file mode 100644 index 8d34fa2..0000000 --- a/Test/Upper.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include "Catch2.h" - -// Tests that uppering an empty string returns an empty string -TEST_CASE(__FILE__"/EmptyString", "[Upper]") -{ - // Setup - const std::string in = ""; - - // Exercise - const std::string out = StringTools::Upper(in); - - // Verify - REQUIRE(out == ""); - return; -} - -// Tests that uppering a string without any letters returns itself -TEST_CASE(__FILE__"/Symbols", "[Upper]") -{ - // Setup - const std::string in = "66! _-\n*"; - - // Exercise - const std::string out = StringTools::Upper(in); - - // Verify - REQUIRE(out == "66! _-\n*"); - return; -} - -// Tests that uppering a string of uppercase letters returns itself -TEST_CASE(__FILE__"/AlreadyUppered", "[Upper]") -{ - // Setup - const std::string in = "UGHAREYOUSERIOUS"; - - // Exercise - const std::string out = StringTools::Upper(in); - - // Verify - REQUIRE(out == "UGHAREYOUSERIOUS"); - return; -} - -// Tests that uppering a string of lowercase letters returns the uppercase version -TEST_CASE(__FILE__"/Lowercase", "[Upper]") -{ - // Setup - const std::string in = "ughareyouserious"; - - // Exercise - const std::string out = StringTools::Upper(in); - - // Verify - REQUIRE(out == "UGHAREYOUSERIOUS"); - return; -} - -// Tests that uppering a string of uppercase, lowercase letters and symbols returns the uppercase version -TEST_CASE(__FILE__"/Mixed", "[Upper]") -{ - // Setup - const std::string in = "Ugh, Are You Serious?! DON'T do that!!!"; - - // Exercise - const std::string out = StringTools::Upper(in); - - // Verify - REQUIRE(out == "UGH, ARE YOU SERIOUS?! DON'T DO THAT!!!"); - return; -} -- cgit v1.2.3