diff --git a/src/hz/CMakeLists.txt b/src/hz/CMakeLists.txt index 1a539f8..aeda7d0 100644 --- a/src/hz/CMakeLists.txt +++ b/src/hz/CMakeLists.txt @@ -59,6 +59,5 @@ target_compile_definitions(hz ) -add_subdirectory(examples) add_subdirectory(tests) diff --git a/src/hz/examples/CMakeLists.txt b/src/hz/examples/CMakeLists.txt deleted file mode 100644 index 90cd907..0000000 --- a/src/hz/examples/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -############################################################################### -# License: BSD Zero Clause License file -# Copyright: -# (C) 2021 Alexander Shaduri -############################################################################### - -if (NOT APP_BUILD_EXAMPLES) - set_directory_properties(PROPERTIES EXCLUDE_FROM_ALL true) -else() - set_directory_properties(PROPERTIES EXCLUDE_FROM_ALL false) -endif() - - -add_executable(example_string_algo) -target_sources(example_string_algo PRIVATE - example_string_algo.cpp -) -target_link_libraries(example_string_algo PRIVATE - hz -) diff --git a/src/hz/examples/example_string_algo.cpp b/src/hz/examples/example_string_algo.cpp deleted file mode 100644 index 5b5e3cb..0000000 --- a/src/hz/examples/example_string_algo.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/****************************************************************************** -License: BSD Zero Clause License -Copyright: - (C) 2008 - 2021 Alexander Shaduri -******************************************************************************/ -/// \file -/// \author Alexander Shaduri -/// \ingroup hz_examples -/// \weakgroup hz_examples -/// @{ - -// disable libdebug, we don't link to it -#undef HZ_USE_LIBDEBUG -#define HZ_USE_LIBDEBUG 0 -// enable libdebug emulation through std::cerr -#undef HZ_EMULATE_LIBDEBUG -#define HZ_EMULATE_LIBDEBUG 1 - -// The first header should be then one we're testing, to avoid missing -// header pitfalls. -#include "hz/string_algo.h" - -#include -#include - -#include "hz/main_tools.h" - - - -/// Main function for the test -int main() -{ - return hz::main_exception_wrapper([]() - { - using namespace hz; - - { - std::string s = "/aa/bbb/ccccc//dsada//"; - - std::vector v; - - string_split(s, '/', v, false); - - for (const auto& str : v) { - std::cerr << (str.empty() ? "[empty]" : str) << "\n"; - } - } - - - { - std::string s = "//aa////bbb/ccccc//dsada////"; - - std::vector v; - - string_split(s, "//", v, false); - - for (const auto& str : v) { - std::cerr << (str.empty() ? "[empty]" : str) << "\n"; - } - } - - - { - std::string s = " a b bb c d "; - std::cerr << string_remove_adjacent_duplicates_copy(s, ' ') << "\n"; - std::cerr << string_remove_adjacent_duplicates_copy(s, ' ', 2) << "\n"; - } - - - - { - std::string s = "/a/b/c/dd//e/"; - string_replace(s, '/', ':'); - std::cerr << s << "\n"; - } - - - { - std::string s = "112/2123412"; - string_replace(s, "12", "AB"); - std::cerr << s << "\n"; - } - - - { - std::vector from, to; - - from.emplace_back("12"); - from.emplace_back("abc"); - - to.emplace_back("345"); - to.emplace_back("de"); - - std::string s = "12345678abcdefg abc ab"; - std::cerr << s << "\n"; - string_replace_array(s, from, to); - std::cerr << s << "\n"; - } - - { - std::vector from, to; - - from.emplace_back("12"); - from.emplace_back("abc"); - - std::string s = "12345678abcdefg abc ab"; - std::cerr << s << "\n"; - string_replace_array(s, from, ":"); - std::cerr << s << "\n"; - } - - return EXIT_SUCCESS; - }); -} - - - - - - -/// @} diff --git a/src/hz/tests/CMakeLists.txt b/src/hz/tests/CMakeLists.txt index ed1c78c..a627d2a 100644 --- a/src/hz/tests/CMakeLists.txt +++ b/src/hz/tests/CMakeLists.txt @@ -15,6 +15,7 @@ endif() add_library(hz_tests OBJECT) target_sources(hz_tests PRIVATE test_format_unit.cpp + test_string_algo.cpp test_string_num.cpp ) target_link_libraries(hz_tests PRIVATE diff --git a/src/hz/tests/test_string_algo.cpp b/src/hz/tests/test_string_algo.cpp new file mode 100644 index 0000000..64b28ad --- /dev/null +++ b/src/hz/tests/test_string_algo.cpp @@ -0,0 +1,134 @@ +/****************************************************************************** +License: BSD Zero Clause License +Copyright: + (C) 2008 - 2022 Alexander Shaduri +******************************************************************************/ +/// \file +/// \author Alexander Shaduri +/// \ingroup hz_tests +/// \weakgroup hz_tests +/// @{ + +// Catch2 v3 +//#include "catch2/catch_test_macros.hpp" + +// Catch2 v2 +#include "catch2/catch.hpp" + +// disable libdebug, we don't link to it +#undef HZ_USE_LIBDEBUG +#define HZ_USE_LIBDEBUG 0 +// enable libdebug emulation through std::cerr +#undef HZ_EMULATE_LIBDEBUG +#define HZ_EMULATE_LIBDEBUG 1 + +// The first header should be then one we're testing, to avoid missing +// header pitfalls. +#include "hz/string_algo.h" + +#include + + + +/// Main function for the test +TEST_CASE("StringAlgorithms", "[hz][string]") +{ + using namespace hz; + + SECTION("string_split, single-character") { + std::string s = "/aa/bbb/ccccc//dsada//"; + std::vector result; + string_split(s, '/', result, false); + + std::vector expected = { + "", + "aa", + "bbb", + "ccccc", + "", + "dsada", + "", + "" + }; + REQUIRE(result == expected); + } + + SECTION("string_split, single-character, skip empty") { + std::string s = "/aa/bbb/ccccc//dsada//"; + std::vector result; + string_split(s, '/', result, true); + + REQUIRE(result == std::vector { + "aa", + "bbb", + "ccccc", + "dsada", + }); + } + + SECTION("string_split, multi-character") { + std::string s = "//aa////bbb/ccccc//dsada////"; + std::vector result; + string_split(s, "//", result, false); + + REQUIRE(result == std::vector { + "", + "aa", + "", + "bbb/ccccc", + "dsada", + "", + "", + }); + } + + SECTION("string_remove_adjacent_duplicates") { + std::string s = " a b bb c d "; + REQUIRE(string_remove_adjacent_duplicates_copy(s, ' ') == " a b bb c d "); + REQUIRE(string_remove_adjacent_duplicates_copy(s, ' ', 2) == " a b bb c d "); + } + + SECTION("string_replace single-character") { + std::string s = "/a/b/c/dd//e/"; + string_replace(s, '/', ':'); + REQUIRE(s == ":a:b:c:dd::e:"); + } + + SECTION("string_replace multi-character") { + std::string s = "112/2123412"; + string_replace(s, "12", "AB"); + REQUIRE(s == "1AB/2AB34AB"); + } + + SECTION("string_replace_array multi -> multi") { + std::vector from, to; + + from.emplace_back("12"); + from.emplace_back("abc"); + + to.emplace_back("345"); + to.emplace_back("de"); + + std::string s = "12345678abcdefg abc ab"; + string_replace_array(s, from, to); + REQUIRE(s == "345345678dedefg de ab"); + } + + SECTION("string_replace_array multi -> single") { + std::vector from, to; + + from.emplace_back("12"); + from.emplace_back("abc"); + + std::string s = "12345678abcdefg abc ab"; + string_replace_array(s, from, ":"); + REQUIRE(s == ":345678:defg : ab"); + } +} + + + + + + +/// @}