Skip to content

Commit 911de6d

Browse files
Use string_case_equal for case-insensitive string equality
Add tests for string operations in config.
1 parent 23305a9 commit 911de6d

19 files changed

+317
-69
lines changed

config/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ target_include_directories(config PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/include" .
1919
set_target_properties(config PROPERTIES FOLDER "Libraries")
2020
source_group("CMake Templates" REGULAR_EXPRESSION "^.*\\.in$")
2121
source_group("CMake Scripts" REGULAR_EXPRESSION "^.*\\.cmake$")
22+
23+
if(BUILD_TESTING)
24+
add_subdirectory(tests)
25+
endif()

config/cmake/string_case_compare.Linux.h.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@ inline int string_case_compare(const char *s1, const char *s2, int len)
1010
{
1111
return strncasecmp(s1, s2, int len);
1212
}
13+
inline bool string_case_equal(const char *s1, const char *s2)
14+
{
15+
return string_case_compare(s1, s2) == 0;
16+
}
17+
inline int string_case_equal(const char *s1, const char *s2, int len)
18+
{
19+
return string_case_compare(s1, s2, len) == 0;
20+
}

config/cmake/string_case_compare.Windows.h.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@
22

33
int string_case_compare(const char *s1, const char *s2);
44
int string_case_compare(const char *s1, const char *s2, int len);
5+
inline bool string_case_equal(const char *s1, const char *s2)
6+
{
7+
return string_case_compare(s1, s2) == 0;
8+
}
9+
inline int string_case_equal(const char *s1, const char *s2, int len)
10+
{
11+
return string_case_compare(s1, s2, len) == 0;
12+
}

config/tests/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
include(GoogleTest)
2+
3+
find_package(GTest CONFIG REQUIRED)
4+
5+
add_executable(config-test
6+
test_string_case_compare.cpp
7+
)
8+
target_link_libraries(config-test PUBLIC config GTest::gmock_main GTest::gtest)
9+
set_target_properties(config-test PROPERTIES FOLDER "Tests")
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
#include <string_case_compare.h>
2+
3+
#include <gtest/gtest.h>
4+
5+
TEST(TestStringCaseCompareCount, firstStringEmpty)
6+
{
7+
char buffer[80]{"foo"};
8+
9+
EXPECT_GT(0, string_case_compare("", buffer, sizeof(buffer)));
10+
}
11+
12+
TEST(TestStringCaseCompareCount, secondStringEmpty)
13+
{
14+
char buffer[80]{"foo"};
15+
16+
EXPECT_LT(0, string_case_compare(buffer, "", sizeof(buffer)));
17+
}
18+
19+
TEST(TestStringCaseCompareCount, firstStringExhaustedEqual)
20+
{
21+
char buffer[]{"foo"};
22+
23+
EXPECT_EQ(0, string_case_compare(buffer, "foobar", sizeof(buffer) - 1));
24+
}
25+
26+
TEST(TestStringCaseCompareCount, secondStringExhaustedEqual)
27+
{
28+
char buffer[]{"foo"};
29+
30+
EXPECT_EQ(0, string_case_compare("foobar", buffer, sizeof(buffer) - 1));
31+
}
32+
33+
TEST(TestStringCaseCompareCount, zeroLengthAlwaysEqual)
34+
{
35+
EXPECT_EQ(0, string_case_compare("foo", "bar", 0));
36+
}
37+
38+
TEST(TestStringCaseCompareCount, differOnlyByCaseEqual)
39+
{
40+
EXPECT_EQ(0, string_case_compare("foo", "FoO", 3));
41+
}
42+
43+
TEST(TestStringCaseCompareCount, differNotZero)
44+
{
45+
EXPECT_LT(0, string_case_compare("foo", "bar", 3));
46+
}
47+
48+
TEST(TestStringCaseCompareCount, lessNegative)
49+
{
50+
EXPECT_GT(0, string_case_compare("bar", "foo", 3));
51+
}
52+
53+
TEST(TestStringCaseCompareCount, greaterPositive)
54+
{
55+
EXPECT_LT(0, string_case_compare("foo", "bar", 3));
56+
}
57+
58+
TEST(TestStringCaseCompareCount, prefixIsLessCount)
59+
{
60+
EXPECT_GT(0, string_case_compare("frac", "fractint", 8));
61+
}
62+
63+
TEST(TestStringCaseCompare, firstStringEmptyEqual)
64+
{
65+
EXPECT_GT(0, string_case_compare("", "foo"));
66+
}
67+
68+
TEST(TestStringCaseCompare, secondStringEmptyNotEqual)
69+
{
70+
EXPECT_LT(0, string_case_compare("foo", ""));
71+
}
72+
73+
TEST(TestStringCaseCompare, firstStringExhaustedEqual)
74+
{
75+
EXPECT_GT(0, string_case_compare("foo", "foobar"));
76+
}
77+
78+
TEST(TestStringCaseCompare, secondStringExhaustedGreater)
79+
{
80+
EXPECT_LT(0, string_case_compare("foobar", "foo"));
81+
}
82+
83+
TEST(TestStringCaseCompare, zeroLengthAlwaysEqual)
84+
{
85+
EXPECT_EQ(0, string_case_compare("", ""));
86+
}
87+
88+
TEST(TestStringCaseCompare, differOnlyByCaseEqual)
89+
{
90+
EXPECT_EQ(0, string_case_compare("foo", "FoO"));
91+
}
92+
93+
TEST(TestStringCaseCompare, differNotZero)
94+
{
95+
EXPECT_LT(0, string_case_compare("foo", "bar"));
96+
}
97+
98+
TEST(TestStringCaseCompare, lessNegative)
99+
{
100+
EXPECT_GT(0, string_case_compare("bar", "foo"));
101+
}
102+
103+
TEST(TestStringCaseCompare, greaterPositive)
104+
{
105+
EXPECT_LT(0, string_case_compare("foo", "bar"));
106+
}
107+
108+
TEST(TestStringCaseCompare, prefixIsLess)
109+
{
110+
EXPECT_GT(0, string_case_compare("frac", "fractint"));
111+
}
112+
113+
TEST(TestStringCaseEqualCount, firstStringEmpty)
114+
{
115+
char buffer[80]{"foo"};
116+
117+
EXPECT_FALSE(string_case_equal("", buffer, sizeof(buffer)));
118+
}
119+
120+
TEST(TestStringCaseEqualCount, secondStringEmpty)
121+
{
122+
char buffer[80]{"foo"};
123+
124+
EXPECT_FALSE(string_case_equal(buffer, "", sizeof(buffer)));
125+
}
126+
127+
TEST(TestStringCaseEqualCount, firstStringExhaustedEqual)
128+
{
129+
char buffer[]{"foo"};
130+
131+
EXPECT_TRUE(string_case_equal(buffer, "foobar", sizeof(buffer) - 1));
132+
}
133+
134+
TEST(TestStringCaseEqualCount, secondStringExhaustedEqual)
135+
{
136+
char buffer[]{"foo"};
137+
138+
EXPECT_TRUE(string_case_equal("foobar", buffer, sizeof(buffer) - 1));
139+
}
140+
141+
TEST(TestStringCaseEqualCount, zeroLengthAlwaysEqual)
142+
{
143+
EXPECT_TRUE(string_case_equal("foo", "bar", 0));
144+
}
145+
146+
TEST(TestStringCaseEqualCount, differOnlyByCaseEqual)
147+
{
148+
EXPECT_TRUE(string_case_equal("foo", "FoO", 3));
149+
}
150+
151+
TEST(TestStringCaseEqualCount, differNotZero)
152+
{
153+
EXPECT_FALSE(string_case_equal("foo", "bar", 3));
154+
}
155+
156+
TEST(TestStringCaseEqualCount, lessNegative)
157+
{
158+
EXPECT_FALSE(string_case_equal("bar", "foo", 3));
159+
}
160+
161+
TEST(TestStringCaseEqualCount, greaterPositive)
162+
{
163+
EXPECT_FALSE(string_case_equal("foo", "bar", 3));
164+
}
165+
166+
TEST(TestStringCaseEqualCount, prefixIsLessCount)
167+
{
168+
EXPECT_FALSE(string_case_equal("frac", "fractint", 8));
169+
}
170+
171+
TEST(TestStringCaseEqual, firstStringEmptyEqual)
172+
{
173+
EXPECT_FALSE(string_case_equal("", "foo"));
174+
}
175+
176+
TEST(TestStringCaseEqual, secondStringEmptyNotEqual)
177+
{
178+
EXPECT_FALSE(string_case_equal("foo", ""));
179+
}
180+
181+
TEST(TestStringCaseEqual, firstStringExhaustedEqual)
182+
{
183+
EXPECT_FALSE(string_case_equal("foo", "foobar"));
184+
}
185+
186+
TEST(TestStringCaseEqual, secondStringExhaustedGreater)
187+
{
188+
EXPECT_FALSE(string_case_equal("foobar", "foo"));
189+
}
190+
191+
TEST(TestStringCaseEqual, zeroLengthAlwaysEqual)
192+
{
193+
EXPECT_TRUE(string_case_equal("", ""));
194+
}
195+
196+
TEST(TestStringCaseEqual, differOnlyByCaseEqual)
197+
{
198+
EXPECT_TRUE(string_case_equal("foo", "FoO"));
199+
}
200+
201+
TEST(TestStringCaseEqual, differNotZero)
202+
{
203+
EXPECT_FALSE(string_case_equal("foo", "bar"));
204+
}
205+
206+
TEST(TestStringCaseEqual, lessNegative)
207+
{
208+
EXPECT_FALSE(string_case_equal("bar", "foo"));
209+
}
210+
211+
TEST(TestStringCaseEqual, greaterPositive)
212+
{
213+
EXPECT_FALSE(string_case_equal("foo", "bar"));
214+
}
215+
216+
TEST(TestStringCaseEqual, prefixIsLess)
217+
{
218+
EXPECT_FALSE(string_case_equal("frac", "fractint"));
219+
}

inews/inews.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ int main(int argc, char *argv[])
175175
break;
176176
}
177177
in_header = true;
178-
if (!string_case_compare(cp, "From:", 5))
178+
if (string_case_equal(cp, "From:", 5))
179179
has_fromline = true;
180-
else if (!string_case_compare(cp, "Path:", 5))
180+
else if (string_case_equal(cp, "Path:", 5))
181181
has_pathline = true;
182182
}
183183
artpos += len;
@@ -361,7 +361,7 @@ int nntp_handle_timeout()
361361
static bool handling_timeout = false;
362362
char last_command_save[NNTP_STRLEN];
363363

364-
if (!string_case_compare(g_last_command,"quit"))
364+
if (string_case_equal(g_last_command, "quit"))
365365
return 0;
366366
if (handling_timeout)
367367
return -1;

libtrn/color.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void color_rc_attribute(const char *object, char *value)
129129
/* Find the specified object. */
130130
int i;
131131
for (i = 0; i < MAX_COLORS; i++) {
132-
if (!string_case_compare(object, s_objects[i].name))
132+
if (string_case_equal(object, s_objects[i].name))
133133
break;
134134
}
135135
if (i >= MAX_COLORS) {

libtrn/datasrc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ char *read_datasrcs(const char *filename)
193193
while ((s = next_ini_section(s,&section,&cond)) != nullptr) {
194194
if (*cond && !check_ini_cond(cond))
195195
continue;
196-
if (!string_case_compare(section, "group ", 6))
196+
if (string_case_equal(section, "group ", 6))
197197
continue;
198198
s = parse_ini_section(s, s_datasrc_ini);
199199
if (!s)

libtrn/decode.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ static bool bad_filename(const char *filename)
8585
int len = strlen(filename);
8686
#ifdef MSDOS
8787
if (len == 3) {
88-
if (!string_case_compare(filename, "aux") || !string_case_compare(filename, "con")
89-
|| !string_case_compare(filename, "nul") || !string_case_compare(filename, "prn"))
88+
if (string_case_equal(filename, "aux") || string_case_equal(filename, "con")
89+
|| string_case_equal(filename, "nul") || string_case_equal(filename, "prn"))
9090
return true;
9191
}
9292
else if (len == 4) {
93-
if (!string_case_compare(filename, "com1") || !string_case_compare(filename, "com2")
94-
|| !string_case_compare(filename, "com3") || !string_case_compare(filename, "com4")
95-
|| !string_case_compare(filename, "lpt1") || !string_case_compare(filename, "lpt2")
96-
|| !string_case_compare(filename, "lpt3"))
93+
if (string_case_equal(filename, "com1") || string_case_equal(filename, "com2")
94+
|| string_case_equal(filename, "com3") || string_case_equal(filename, "com4")
95+
|| string_case_equal(filename, "lpt1") || string_case_equal(filename, "lpt2")
96+
|| string_case_equal(filename, "lpt3"))
9797
return true;
9898
}
9999
#else
@@ -125,11 +125,11 @@ char *decode_subject(ART_NUM artnum, int *partp, int *totalp)
125125
/* Skip leading whitespace and other garbage */
126126
char *s = subject;
127127
while (is_hor_space(*s) || *s == '-') s++;
128-
if (!string_case_compare(s, "repost", 6)) {
128+
if (string_case_equal(s, "repost", 6)) {
129129
for (s += 6; is_hor_space(*s) || *s == ':' || *s == '-'; s++);
130130
}
131131

132-
while (!string_case_compare(s, "re:", 3)) {
132+
while (string_case_equal(s, "re:", 3)) {
133133
s = skip_space(s + 3);
134134
}
135135

@@ -204,7 +204,7 @@ char *decode_subject(ART_NUM artnum, int *partp, int *totalp)
204204
}
205205

206206
/* look for "6 parts" or "part 1" */
207-
if (!string_case_compare("part", s, 4)) {
207+
if (string_case_equal("part", s, 4)) {
208208
if (s[4] == 's') {
209209
for (t = s; t >= subject && !isdigit(*t); t--);
210210
if (t > subject) {

0 commit comments

Comments
 (0)