Skip to content

Commit 5d74353

Browse files
author
MarkusB
committed
add tests
1 parent df0a97b commit 5d74353

6 files changed

Lines changed: 255 additions & 6 deletions

File tree

string/include/ul/string/string_misc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ inline char make_lower(char& c) {
3939
return c;
4040
}
4141

42-
//! Expects fromSub non-empty.
42+
//! Expects from_sub non-empty.
4343
inline void replace_all(std::string& s, const std::string& from_sub, const std::string& to_sub) {
4444
UL_EXPECT(!from_sub.empty());
4545
size_t start_pos{};
@@ -49,8 +49,8 @@ inline void replace_all(std::string& s, const std::string& from_sub, const std::
4949
}
5050
}
5151

52-
/** Expects fromSub non-empty. Expects toSubs to be of at least the
53-
count of occurrences of fromSub within s.*/
52+
/** Expects from_sub non-empty. Expects to_subs to be of at least the
53+
count of occurrences of from_sub within s.*/
5454
inline void replace_all(std::string& s, const std::string& from_sub, const std::vector<std::string>& to_subs) {
5555
UL_EXPECT(!from_sub.empty());
5656
if (s.empty()) {

string/include/ul/string/string_trim.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
#include <string>
77

88
namespace mb::ul::str {
9-
inline std::string& ltrim(std::string& s, const std::string& trimchars = " \t\n") {
9+
inline std::string& ltrim(std::string& s, const std::string& trimchars = " \t\n\r") {
1010
// Following suggestion from Evan Teran, stackoverflow doesn't compile.
1111
// s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
1212
s.erase(0, s.find_first_not_of(trimchars));
1313
return s;
1414
}
1515

16-
inline std::string& rtrim(std::string& s, const std::string& trimchars = " \t\n") {
16+
inline std::string& rtrim(std::string& s, const std::string& trimchars = " \t\n\r") {
1717
// s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
1818
s.erase(s.find_last_not_of(trimchars) + 1);
1919
return s;

string/include/ul/string/ulstring.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
\file
33
If you expected this file to define yet another custom string type, you are fortunately wrong.
4-
Instead it serves just as a documentation of guidelines of how to deal with strings in your
4+
Instead, it serves just as a documentation of guidelines of how to deal with strings in your
55
program.
66
A short glance at function getLength and its implementation below should give you an immediate
77
idea of what your dealing with here. (The count of bytes isn't equal to the count of characters...)

string/src/string_misc.test.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "ul/string/string_misc.h"
22
#include "gtest/gtest.h"
33
#include <string>
4+
#include <vector>
45

56
namespace ul = mb::ul;
67

@@ -51,3 +52,86 @@ TEST(str_apply_ellipse, test_wider_chars) {
5152
const auto ret4 = ul::str::apply_ellipse(s, 4);
5253
EXPECT_STREQ("\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88", ret4.c_str());
5354
}
55+
56+
TEST(str_make_upper, basic) {
57+
std::string s = "hello";
58+
EXPECT_EQ(ul::str::make_upper(s), "HELLO");
59+
}
60+
61+
TEST(str_make_upper, special_chars) {
62+
std::string s = "hello!@#$%^&*()"; // Only ASCII special characters
63+
EXPECT_EQ(ul::str::make_upper(s), "HELLO!@#$%^&*()");
64+
}
65+
66+
TEST(str_make_lower, basic) {
67+
std::string s = "HELLO";
68+
EXPECT_EQ(ul::str::make_lower(s), "hello");
69+
}
70+
71+
TEST(str_make_lower, special_chars) {
72+
std::string s = "HELLO!@#$%^&*()"; // Only ASCII special characters
73+
EXPECT_EQ(ul::str::make_lower(s), "hello!@#$%^&*()");
74+
}
75+
76+
TEST(str_replace_all, basic) {
77+
std::string s = "hello world";
78+
ul::str::replace_all(s, "world", "there");
79+
EXPECT_EQ(s, "hello there");
80+
81+
s = "hello hello hello";
82+
ul::str::replace_all(s, "hello", "hi");
83+
EXPECT_EQ(s, "hi hi hi");
84+
}
85+
86+
TEST(str_replace_all, empty_strings) {
87+
std::string s = "";
88+
ul::str::replace_all(s, "hello", "hi"); // Empty source string is OK
89+
EXPECT_EQ(s, "");
90+
91+
// Note: Empty from_sub is not supported by design
92+
}
93+
94+
TEST(str_replace_all, with_vector) {
95+
std::string s = "hello {1} world {2}";
96+
std::vector<std::string> replacements = {"beautiful", "today"};
97+
ul::str::replace_all(s, "{1}", replacements);
98+
EXPECT_EQ(s, "hello beautiful world {2}");
99+
100+
s = "hello {} world {}";
101+
ul::str::replace_all(s, "{}", replacements);
102+
EXPECT_EQ(s, "hello beautiful world today");
103+
}
104+
105+
TEST(str_bool_conversion, bool2str) {
106+
EXPECT_EQ(ul::str::bool2str(true), "1");
107+
EXPECT_EQ(ul::str::bool2str(false), "0");
108+
109+
EXPECT_EQ(ul::str::bool2str<ul::str::BoolStrBoolalpha>(true), "true");
110+
EXPECT_EQ(ul::str::bool2str<ul::str::BoolStrBoolalpha>(false), "false");
111+
}
112+
113+
TEST(str_bool_conversion, str2bool) {
114+
EXPECT_TRUE(ul::str::str2bool("1"));
115+
EXPECT_FALSE(ul::str::str2bool("0"));
116+
117+
EXPECT_TRUE(ul::str::str2bool<ul::str::BoolStrBoolalpha>("true"));
118+
EXPECT_FALSE(ul::str::str2bool<ul::str::BoolStrBoolalpha>("false"));
119+
}
120+
121+
TEST(str_ends_with, basic) {
122+
EXPECT_TRUE(ul::str::ends_with("hello world", "world"));
123+
EXPECT_FALSE(ul::str::ends_with("hello world", "hello"));
124+
EXPECT_TRUE(ul::str::ends_with("hello", "hello"));
125+
}
126+
127+
TEST(str_ends_with, empty_strings) {
128+
EXPECT_TRUE(ul::str::ends_with("", ""));
129+
EXPECT_FALSE(ul::str::ends_with("", "hello"));
130+
EXPECT_TRUE(ul::str::ends_with("hello", ""));
131+
}
132+
133+
TEST(str_ends_with, special_chars) {
134+
EXPECT_TRUE(ul::str::ends_with("hello\xc3\xa4", "\xc3\xa4")); // UTF-8 character
135+
EXPECT_TRUE(ul::str::ends_with("hello.txt", ".txt"));
136+
EXPECT_TRUE(ul::str::ends_with("hello\n", "\n"));
137+
}

string/src/string_trim.test.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,101 @@
11
#include "ul/string/string_trim.h" // NOLINT
2+
#include "gtest/gtest.h"
3+
#include <string>
4+
5+
namespace ul = mb::ul;
6+
7+
TEST(StringTrimTest, ltrim_basic) {
8+
std::string s = " hello ";
9+
EXPECT_EQ(ul::str::ltrim(s), "hello ");
10+
11+
s = "\t\n\r hello";
12+
EXPECT_EQ(ul::str::ltrim(s), "hello");
13+
14+
s = "hello";
15+
EXPECT_EQ(ul::str::ltrim(s), "hello");
16+
}
17+
18+
TEST(StringTrimTest, ltrim_custom_chars) {
19+
std::string s = "xxxhello";
20+
EXPECT_EQ(ul::str::ltrim(s, "x"), "hello");
21+
22+
s = ".-hello";
23+
EXPECT_EQ(ul::str::ltrim(s, ".-"), "hello");
24+
25+
s = "abcabchello";
26+
EXPECT_EQ(ul::str::ltrim(s, "abc"), "hello");
27+
}
28+
29+
TEST(StringTrimTest, rtrim_basic) {
30+
std::string s = " hello ";
31+
EXPECT_EQ(ul::str::rtrim(s), " hello");
32+
33+
s = "hello\t\n\r";
34+
EXPECT_EQ(ul::str::rtrim(s), "hello");
35+
36+
s = "hello";
37+
EXPECT_EQ(ul::str::rtrim(s), "hello");
38+
}
39+
40+
TEST(StringTrimTest, rtrim_custom_chars) {
41+
std::string s = "helloxxx";
42+
EXPECT_EQ(ul::str::rtrim(s, "x"), "hello");
43+
44+
s = "hello.-";
45+
EXPECT_EQ(ul::str::rtrim(s, ".-"), "hello");
46+
47+
s = "helloabcabc";
48+
EXPECT_EQ(ul::str::rtrim(s, "abc"), "hello");
49+
}
50+
51+
TEST(StringTrimTest, trim_basic) {
52+
std::string s = " hello ";
53+
EXPECT_EQ(ul::str::trim(s), "hello");
54+
55+
s = "\t\n\r hello\t\n\r";
56+
EXPECT_EQ(ul::str::trim(s), "hello");
57+
58+
s = "hello";
59+
EXPECT_EQ(ul::str::trim(s), "hello");
60+
}
61+
62+
TEST(StringTrimTest, trim_empty) {
63+
std::string s = "";
64+
EXPECT_EQ(ul::str::trim(s), "");
65+
66+
s = " ";
67+
EXPECT_EQ(ul::str::trim(s), "");
68+
69+
s = "\t\n\r";
70+
EXPECT_EQ(ul::str::trim(s), "");
71+
}
72+
73+
TEST(StringTrimTest, trim_special_chars) {
74+
std::string s = "\0hello\0";
75+
EXPECT_EQ(ul::str::trim(s), "\0hello\0");
76+
77+
s = " hello\xc3\xa4 "; // UTF-8 character
78+
EXPECT_EQ(ul::str::trim(s), "hello\xc3\xa4");
79+
}
80+
81+
TEST(StringTrimTest, trim_all_spaces) {
82+
std::string s = " ";
83+
EXPECT_EQ(ul::str::trim(s), "");
84+
85+
s = "\t\n\r \t\n\r";
86+
EXPECT_EQ(ul::str::trim(s), "");
87+
88+
s = " \t\n\r \t\n\r ";
89+
EXPECT_EQ(ul::str::trim(s), "");
90+
}
91+
92+
TEST(StringTrimTest, trim_mixed) {
93+
std::string s = " hello\xc3\xa4 "; // UTF-8 character with spaces
94+
EXPECT_EQ(ul::str::trim(s), "hello\xc3\xa4");
95+
96+
s = "\t\n\r hello\xc3\xa4\t\n\r"; // UTF-8 character with control chars
97+
EXPECT_EQ(ul::str::trim(s), "hello\xc3\xa4");
98+
99+
s = " hello\xc3\xa4\t\n\r "; // UTF-8 character with mixed whitespace
100+
EXPECT_EQ(ul::str::trim(s), "hello\xc3\xa4");
101+
}

string/src/ulstring.test.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,66 @@
11
#include "ul/string/ulstring.h" // NOLINT
2+
#include "gtest/gtest.h"
3+
#include <string>
4+
5+
namespace ul = mb::ul;
6+
7+
TEST(ulStringTest, get_length_ascii) {
8+
// Test with ASCII strings
9+
EXPECT_EQ(ul::str::get_length(""), 0);
10+
EXPECT_EQ(ul::str::get_length("a"), 1);
11+
EXPECT_EQ(ul::str::get_length("abc"), 3);
12+
EXPECT_EQ(ul::str::get_length("Hello, World!"), 13);
13+
}
14+
15+
TEST(ulStringTest, get_length_utf8) {
16+
// Test with UTF-8 strings
17+
// German "ähnlich" (similar)
18+
EXPECT_EQ(ul::str::get_length("\xc3\xa4hnlich"), 7);
19+
// Japanese "こんにちは" (hello)
20+
EXPECT_EQ(ul::str::get_length("\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf"), 5);
21+
// Chinese "你好" (hello)
22+
EXPECT_EQ(ul::str::get_length("\xe4\xbd\xa0\xe5\xa5\xbd"), 2);
23+
// Korean "안녕하세요" (hello)
24+
EXPECT_EQ(ul::str::get_length("\xec\x95\x88\xeb\x85\x95\xed\x95\x98\xec\x84\xb8\xec\x9a\x94"), 5);
25+
}
26+
27+
TEST(ulStringTest, get_length_mixed) {
28+
// Test with mixed ASCII and UTF-8 strings
29+
EXPECT_EQ(ul::str::get_length("Hello \xc3\xa4hnlich"), 13);
30+
EXPECT_EQ(ul::str::get_length("Hello こんにちは"), 11);
31+
EXPECT_EQ(ul::str::get_length("Hello 你好"), 8);
32+
EXPECT_EQ(ul::str::get_length("Hello 안녕하세요"), 11);
33+
}
34+
35+
TEST(ulStringTest, get_length_special) {
36+
// Test with special characters
37+
EXPECT_EQ(ul::str::get_length("\n\t\r"), 3); // Control characters
38+
EXPECT_EQ(ul::str::get_length(" "), 1); // Space
39+
EXPECT_EQ(ul::str::get_length(" "), 2); // Multiple spaces
40+
EXPECT_EQ(ul::str::get_length("!@#$%^&*()"), 10); // Special ASCII characters
41+
}
42+
43+
TEST(ulStringTest, get_length_emoji) {
44+
// Test with emoji (which are UTF-8 characters)
45+
EXPECT_EQ(ul::str::get_length("😀"), 1); // Basic emoji
46+
EXPECT_EQ(ul::str::get_length("👋🌍"), 2); // Multiple emoji
47+
EXPECT_EQ(ul::str::get_length("Hello 😀"), 7); // Mixed with ASCII
48+
}
49+
50+
TEST(ulStringTest, get_length_edge_cases) {
51+
// Test edge cases
52+
std::string empty;
53+
EXPECT_EQ(ul::str::get_length(empty), 0);
54+
55+
// Test with string containing only null bytes
56+
std::string nulls(3, '\0');
57+
EXPECT_EQ(ul::str::get_length(nulls), 3);
58+
59+
// Test with string containing invalid UTF-8
60+
std::string invalid_utf8 = "\xc3"; // Incomplete UTF-8 sequence
61+
EXPECT_ANY_THROW(ul::str::get_length(invalid_utf8));
62+
63+
// Test with very long string
64+
std::string long_string(1000, 'a');
65+
EXPECT_EQ(ul::str::get_length(long_string), 1000);
66+
}

0 commit comments

Comments
 (0)