Skip to content

Commit 131bf52

Browse files
authored
Issue #168 Fix src/library/case_insensitive_string tests (#245)
1 parent b65c88c commit 131bf52

File tree

4 files changed

+98
-54
lines changed

4 files changed

+98
-54
lines changed

src/library/case_insensitive_string/ut/ya.make

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/library/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
add_subdirectory(cache)
2+
add_subdirectory(case_insensitive_string)
23
add_subdirectory(coroutine)
34
add_subdirectory(login)
45
add_subdirectory(operation_id)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
add_ydb_test(NAME library-case_insensitive_string_ut
2+
SOURCES
3+
case_insensitive_string_ut.cpp
4+
INCLUDE_DIRS
5+
${YDB_SDK_SOURCE_DIR}/src/library/case_insensitive_string
6+
LINK_LIBRARIES
7+
library-cpp-case_insensitive_string
8+
cpp-testing-unittest_main
9+
)

src/library/case_insensitive_string/case_insensitive_string_ut.cpp renamed to tests/library/case_insensitive_string/case_insensitive_string_ut.cpp

Lines changed: 88 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
#include "case_insensitive_string.h"
22

3-
#include <src/util/generic/string_ut.h>
4-
5-
class TCaseInsensitiveStringTest : public TTestBase, private TStringTestImpl<TCaseInsensitiveString, TTestData<char>> {
6-
public:
7-
UNIT_TEST_SUITE(TCaseInsensitiveStringTest);
8-
UNIT_TEST(TestOperators);
9-
UNIT_TEST(TestOperatorsCI);
10-
11-
UNIT_TEST_SUITE_END();
12-
};
13-
14-
UNIT_TEST_SUITE_REGISTRATION(TCaseInsensitiveStringTest);
3+
#include <src/library/testing/unittest/registar.h>
154

165
Y_UNIT_TEST_SUITE(TCaseInsensitiveCharTraits) {
176
Y_UNIT_TEST(EqualTo) {
@@ -30,12 +19,12 @@ Y_UNIT_TEST_SUITE(TCaseInsensitiveCharTraits) {
3019
for (const auto& [c1, c2, answer] : cases) {
3120
const auto uc1 = static_cast<char>(std::toupper(static_cast<unsigned char>(c1)));
3221
const auto uc2 = static_cast<char>(std::toupper(static_cast<unsigned char>(c2)));
33-
22+
3423
UNIT_ASSERT_EQUAL(eq(c1, c2), answer);
3524
UNIT_ASSERT_EQUAL(eq(c2, c1), answer);
3625
UNIT_ASSERT_EQUAL(eq_int_type(to_int_type(c1), to_int_type(c2)), answer);
3726
UNIT_ASSERT_EQUAL(eq_int_type(to_int_type(c2), to_int_type(c1)), answer);
38-
27+
3928
UNIT_ASSERT_EQUAL(eq(c1, uc2), answer);
4029
UNIT_ASSERT_EQUAL(eq(uc2, c1), answer);
4130
UNIT_ASSERT_EQUAL(eq_int_type(to_int_type(c1), to_int_type(uc2)), answer);
@@ -45,7 +34,7 @@ Y_UNIT_TEST_SUITE(TCaseInsensitiveCharTraits) {
4534
UNIT_ASSERT_EQUAL(eq(c2, uc1), answer);
4635
UNIT_ASSERT_EQUAL(eq_int_type(to_int_type(uc1), to_int_type(c2)), answer);
4736
UNIT_ASSERT_EQUAL(eq_int_type(to_int_type(c2), to_int_type(uc1)), answer);
48-
37+
4938
UNIT_ASSERT_EQUAL(eq(uc1, uc2), answer);
5039
UNIT_ASSERT_EQUAL(eq(uc2, uc1), answer);
5140
UNIT_ASSERT_EQUAL(eq_int_type(to_int_type(uc1), to_int_type(uc2)), answer);
@@ -95,7 +84,7 @@ Y_UNIT_TEST_SUITE(TCaseInsensitiveCharTraits) {
9584
{"Bar", "bAR", 4},
9685
};
9786

98-
for (const auto& [s1, s2, size] : equal_cases) {
87+
for (const auto& [s1, s2, size] : equal_cases) {
9988
for (std::size_t n = 0; n < size; ++n) {
10089
UNIT_ASSERT_EQUAL(compare(s1, s1, n), 0);
10190
UNIT_ASSERT_EQUAL(compare(s2, s2, n), 0);
@@ -141,13 +130,13 @@ Y_UNIT_TEST_SUITE(TCaseInsensitiveCharTraits) {
141130

142131
UNIT_ASSERT_EQUAL(find(s1, 0, ch), nullptr);
143132
UNIT_ASSERT_EQUAL(find(s1, 0, uch), nullptr);
144-
133+
145134
UNIT_ASSERT_EQUAL(find(s2, 0, ch), nullptr);
146135
UNIT_ASSERT_EQUAL(find(s2, 0, uch), nullptr);
147136

148137
UNIT_ASSERT_EQUAL(find(s1, offset, ch), nullptr);
149138
UNIT_ASSERT_EQUAL(find(s1, offset, uch), nullptr);
150-
139+
151140
UNIT_ASSERT_EQUAL(find(s2, offset, ch), nullptr);
152141
UNIT_ASSERT_EQUAL(find(s2, offset, uch), nullptr);
153142

@@ -160,53 +149,105 @@ Y_UNIT_TEST_SUITE(TCaseInsensitiveCharTraits) {
160149
}
161150
}
162151

163-
Y_UNIT_TEST_SUITE(TCaseInsensitiveStringTestEx) {
164-
Y_UNIT_TEST(BasicTString) {
165-
TCaseInsensitiveString foo("foo");
166-
TCaseInsensitiveString FOO("FOO");
167-
TCaseInsensitiveString Bar("Bar");
168-
TCaseInsensitiveString bAR("bAR");
152+
Y_UNIT_TEST_SUITE(TCaseInsensitiveStringTest) {
153+
using TCIString = TCaseInsensitiveString;
154+
using TCIStringBuf = TCaseInsensitiveStringBuf;
169155

170-
UNIT_ASSERT_EQUAL(foo, FOO);
171-
UNIT_ASSERT_EQUAL(Bar, bAR);
156+
Y_UNIT_TEST(TestOperators) {
157+
TCIString s("0123456");
158+
159+
const auto x = "x";
160+
const auto y = "y";
161+
const auto z = "z";
162+
163+
// operator +=
164+
s += TCIString(x);
165+
UNIT_ASSERT(s == "0123456x");
172166

173-
constexpr TCaseInsensitiveStringBuf foobar("foobar");
174-
UNIT_ASSERT(foobar.StartsWith(foo));
175-
UNIT_ASSERT(foobar.StartsWith(FOO));
176-
UNIT_ASSERT(foobar.EndsWith(Bar));
177-
UNIT_ASSERT(foobar.EndsWith(bAR));
178-
UNIT_ASSERT(foobar.Contains(FOO));
179-
UNIT_ASSERT(foobar.Contains(Bar));
167+
s += y;
168+
UNIT_ASSERT(s == "0123456xy");
169+
170+
s += *z;
171+
UNIT_ASSERT(s == "0123456xyz");
172+
173+
// operator +
174+
s = "0123456";
175+
s = s + TCIString(x);
176+
UNIT_ASSERT(s == "0123456x");
177+
178+
s = s + y;
179+
UNIT_ASSERT(s == "0123456xy");
180+
181+
s = s + *z;
182+
UNIT_ASSERT(s == "0123456xyz");
183+
184+
// operator !=
185+
s = "012345";
186+
const auto xyz = "xyz";
187+
UNIT_ASSERT(s != TCIString(xyz));
188+
UNIT_ASSERT(s != xyz);
189+
UNIT_ASSERT(xyz != s);
190+
191+
// operator <
192+
UNIT_ASSERT_EQUAL(s < TCIString(xyz), true);
193+
UNIT_ASSERT_EQUAL(s < xyz, true);
194+
UNIT_ASSERT_EQUAL(xyz < s, false);
195+
196+
// operator <=
197+
UNIT_ASSERT_EQUAL(s <= TCIString(xyz), true);
198+
UNIT_ASSERT_EQUAL(s <= xyz, true);
199+
UNIT_ASSERT_EQUAL(xyz <= s, false);
200+
201+
// operator >
202+
UNIT_ASSERT_EQUAL(s > TCIString(xyz), false);
203+
UNIT_ASSERT_EQUAL(s > xyz, false);
204+
UNIT_ASSERT_EQUAL(xyz > s, true);
205+
206+
// operator >=
207+
UNIT_ASSERT_EQUAL(s >= TCIString(xyz), false);
208+
UNIT_ASSERT_EQUAL(s >= xyz, false);
209+
UNIT_ASSERT_EQUAL(xyz >= s, true);
180210
}
181211

182-
Y_UNIT_TEST(BasicStdString) {
183-
using TCaseInsensitiveStdString = std::basic_string<char, TCaseInsensitiveCharTraits>;
184-
using TCaseInsensitiveStringView = std::basic_string_view<char, TCaseInsensitiveCharTraits>;
212+
Y_UNIT_TEST(TestOperatorsCI) {
213+
TCIString s("ABCD");
214+
UNIT_ASSERT(s > "abc0123456xyz");
215+
UNIT_ASSERT(s == "abcd");
216+
UNIT_ASSERT(s > TCIStringBuf("abc0123456xyz"));
217+
UNIT_ASSERT(TCIStringBuf("abc0123456xyz") < s);
218+
UNIT_ASSERT(s == TCIStringBuf("abcd"));
219+
}
185220

186-
TCaseInsensitiveStdString foo("foo");
187-
TCaseInsensitiveStdString FOO("FOO");
188-
TCaseInsensitiveStdString Bar("Bar");
189-
TCaseInsensitiveStdString bAR("bAR");
221+
Y_UNIT_TEST(BasicStdString) {
222+
TCIString foo("foo");
223+
TCIString FOO("FOO");
224+
TCIString Bar("Bar");
225+
TCIString bAR("bAR");
190226

191227
UNIT_ASSERT_EQUAL(foo, FOO);
192228
UNIT_ASSERT_EQUAL(Bar, bAR);
193229

194-
constexpr TCaseInsensitiveStringView foobar("foobar");
230+
constexpr TCIStringBuf foobar("foobar");
195231
UNIT_ASSERT(foobar.starts_with(foo));
196232
UNIT_ASSERT(foobar.starts_with(FOO));
197233
UNIT_ASSERT(foobar.ends_with(Bar));
198234
UNIT_ASSERT(foobar.ends_with(bAR));
199-
//TODO: test contains after C++23
235+
#if __cpp_lib_string_contains >= 202011L
236+
UNIT_ASSERT(foobar.contains(FOO));
237+
UNIT_ASSERT(foobar.contains(Bar));
238+
#endif
200239
}
201240

202-
/*
241+
// StringSplitter does not use CharTraits properly
242+
// Splitting such strings is explicitly disabled.
243+
#if 0
203244
Y_UNIT_TEST(TestSplit) {
204-
TCaseInsensitiveStringBuf input("splitAmeAbro");
205-
std::vector<TCaseInsensitiveStringBuf> expected{"split", "me", "bro"};
245+
TCIStringBuf input("splitAmeAbro");
246+
std::vector<TCIStringBuf> expected{"split", "me", "bro"};
206247

207-
std::vector<TCaseInsensitiveStringBuf> split = StringSplitter(input).Split('a');
248+
std::vector<TCIStringBuf> split = StringSplitter(input).Split('a');
208249

209250
UNIT_ASSERT_VALUES_EQUAL(split, expected);
210251
}
211-
*/
252+
#endif
212253
}

0 commit comments

Comments
 (0)