From 5fbf07f946995fed76804555a7a5e38f1ef94f38 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Sun, 13 Mar 2022 16:42:40 +0100 Subject: Added tests for string-split, and fixed some bigs --- Test/.String__Split.cpp.swp | Bin 0 -> 20480 bytes Test/CMakeLists.txt | 1 + Test/String__Split.cpp | 156 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 Test/.String__Split.cpp.swp create mode 100644 Test/String__Split.cpp (limited to 'Test') diff --git a/Test/.String__Split.cpp.swp b/Test/.String__Split.cpp.swp new file mode 100644 index 0000000..776cfc7 Binary files /dev/null and b/Test/.String__Split.cpp.swp differ 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; +} + -- cgit v1.2.3