Converted hz string algorithms example into a test.

This commit is contained in:
Alexander Shaduri
2022-02-11 14:08:57 +04:00
parent 907c9ac423
commit 582d4ad5cd
5 changed files with 135 additions and 142 deletions
-1
View File
@@ -59,6 +59,5 @@ target_compile_definitions(hz
)
add_subdirectory(examples)
add_subdirectory(tests)
-20
View File
@@ -1,20 +0,0 @@
###############################################################################
# License: BSD Zero Clause License file
# Copyright:
# (C) 2021 Alexander Shaduri <ashaduri@gmail.com>
###############################################################################
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
)
-121
View File
@@ -1,121 +0,0 @@
/******************************************************************************
License: BSD Zero Clause License
Copyright:
(C) 2008 - 2021 Alexander Shaduri <ashaduri@gmail.com>
******************************************************************************/
/// \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 <vector>
#include <iostream>
#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<std::string> 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<std::string> 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<std::string> 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<std::string> 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;
});
}
/// @}
+1
View File
@@ -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
+134
View File
@@ -0,0 +1,134 @@
/******************************************************************************
License: BSD Zero Clause License
Copyright:
(C) 2008 - 2022 Alexander Shaduri <ashaduri@gmail.com>
******************************************************************************/
/// \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 <vector>
/// 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<std::string> result;
string_split(s, '/', result, false);
std::vector<std::string> expected = {
"",
"aa",
"bbb",
"ccccc",
"",
"dsada",
"",
""
};
REQUIRE(result == expected);
}
SECTION("string_split, single-character, skip empty") {
std::string s = "/aa/bbb/ccccc//dsada//";
std::vector<std::string> result;
string_split(s, '/', result, true);
REQUIRE(result == std::vector<std::string> {
"aa",
"bbb",
"ccccc",
"dsada",
});
}
SECTION("string_split, multi-character") {
std::string s = "//aa////bbb/ccccc//dsada////";
std::vector<std::string> result;
string_split(s, "//", result, false);
REQUIRE(result == std::vector<std::string> {
"",
"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<std::string> 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<std::string> 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");
}
}
/// @}