1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
#include "common.hh"
#include "strutil.hh"
#include <gtest/gtest.h>
TEST(strutil, parse_uint16) {
std::optional<uint16_t> value;
value = str::parse_uint16("0");
EXPECT_TRUE(value.has_value());
if (value.has_value())
EXPECT_EQ(0, value.value());
value = str::parse_uint16("10");
EXPECT_TRUE(value.has_value());
if (value.has_value())
EXPECT_EQ(10, value.value());
value = str::parse_uint16("65535");
EXPECT_TRUE(value.has_value());
if (value.has_value())
EXPECT_EQ(65535, value.value());
EXPECT_FALSE(str::parse_uint16("-1"));
EXPECT_FALSE(str::parse_uint16("0x10"));
EXPECT_FALSE(str::parse_uint16("65536"));
EXPECT_FALSE(str::parse_uint16(""));
EXPECT_FALSE(str::parse_uint16("NaN"));
}
TEST(strutil, parse_uint32) {
std::optional<uint32_t> value;
value = str::parse_uint32("0");
EXPECT_TRUE(value.has_value());
if (value.has_value())
EXPECT_EQ(0, value.value());
value = str::parse_uint32("10");
EXPECT_TRUE(value.has_value());
if (value.has_value())
EXPECT_EQ(10, value);
value = str::parse_uint32("4294967295");
EXPECT_TRUE(value.has_value());
if (value.has_value())
EXPECT_EQ(4294967295ul, value);
EXPECT_FALSE(str::parse_uint32("-1"));
EXPECT_FALSE(str::parse_uint32("0x10"));
EXPECT_FALSE(str::parse_uint32("4294967296"));
EXPECT_FALSE(str::parse_uint32(""));
EXPECT_FALSE(str::parse_uint32("NaN"));
}
TEST(strutil, parse_uint64) {
std::optional<uint64_t> value;
value = str::parse_uint64("0");
EXPECT_TRUE(value.has_value());
if (value.has_value())
EXPECT_EQ(0, value.value());
value = str::parse_uint64("10");
EXPECT_TRUE(value.has_value());
if (value.has_value())
EXPECT_EQ(10, value.value());
value = str::parse_uint64("18446744073709551615");
EXPECT_TRUE(value.has_value());
if (value.has_value())
EXPECT_EQ(18446744073709551615ull, value.value());
EXPECT_FALSE(str::parse_uint64("-1"));
EXPECT_FALSE(str::parse_uint64("0x10"));
EXPECT_FALSE(str::parse_uint64("18446744073709551616"));
EXPECT_FALSE(str::parse_uint64(""));
EXPECT_FALSE(str::parse_uint64("NaN"));
}
TEST(strutil, split) {
auto out = str::split(std::string_view(""));
ASSERT_EQ(1, out.size());
EXPECT_EQ("", out[0]);
out = str::split(std::string_view("foo"));
ASSERT_EQ(1, out.size());
EXPECT_EQ("foo", out[0]);
out = str::split(std::string_view(" f o o "));
ASSERT_EQ(3, out.size());
EXPECT_EQ("f", out[0]);
EXPECT_EQ("o", out[1]);
EXPECT_EQ("o", out[2]);
out = str::split(std::string_view(" f o o "));
ASSERT_EQ(3, out.size());
EXPECT_EQ("f", out[0]);
EXPECT_EQ("o", out[1]);
EXPECT_EQ("o", out[2]);
out = str::split(std::string_view("abba"), 'b');
ASSERT_EQ(2, out.size());
EXPECT_EQ("a", out[0]);
EXPECT_EQ("a", out[1]);
}
TEST(strutil, split_str) {
auto out = str::split(std::string(""));
ASSERT_EQ(1, out.size());
EXPECT_EQ("", out[0]);
out = str::split(std::string("foo"));
ASSERT_EQ(1, out.size());
EXPECT_EQ("foo", out[0]);
out = str::split(std::string(" f o o "));
ASSERT_EQ(3, out.size());
EXPECT_EQ("f", out[0]);
EXPECT_EQ("o", out[1]);
EXPECT_EQ("o", out[2]);
out = str::split(std::string(" f o o "));
ASSERT_EQ(3, out.size());
EXPECT_EQ("f", out[0]);
EXPECT_EQ("o", out[1]);
EXPECT_EQ("o", out[2]);
out = str::split(std::string("abba"), 'b');
ASSERT_EQ(2, out.size());
EXPECT_EQ("a", out[0]);
EXPECT_EQ("a", out[1]);
}
TEST(strutil, trim_str) {
EXPECT_EQ("", str::trim(std::string("")));
EXPECT_EQ("foo", str::trim(std::string("foo")));
EXPECT_EQ("foo", str::trim(std::string("foo ")));
EXPECT_EQ("foo", str::trim(std::string(" foo")));
EXPECT_EQ("foo", str::trim(std::string(" foo ")));
EXPECT_EQ("foo", str::trim(std::string(" foo ")));
EXPECT_EQ("", str::ltrim(std::string("")));
EXPECT_EQ("foo", str::ltrim(std::string("foo")));
EXPECT_EQ("foo ", str::ltrim(std::string("foo ")));
EXPECT_EQ("foo", str::ltrim(std::string(" foo")));
EXPECT_EQ("foo ", str::ltrim(std::string(" foo ")));
EXPECT_EQ("foo ", str::ltrim(std::string(" foo ")));
EXPECT_EQ("", str::rtrim(std::string("")));
EXPECT_EQ("foo", str::rtrim(std::string("foo")));
EXPECT_EQ("foo", str::rtrim(std::string("foo ")));
EXPECT_EQ(" foo", str::rtrim(std::string(" foo")));
EXPECT_EQ(" foo", str::rtrim(std::string(" foo ")));
EXPECT_EQ(" foo", str::rtrim(std::string(" foo ")));
}
TEST(strutil, trim_view) {
EXPECT_EQ("", str::trim(std::string_view("")));
EXPECT_EQ("foo", str::trim(std::string_view("foo")));
EXPECT_EQ("foo", str::trim(std::string_view("foo ")));
EXPECT_EQ("foo", str::trim(std::string_view(" foo")));
EXPECT_EQ("foo", str::trim(std::string_view(" foo ")));
EXPECT_EQ("foo", str::trim(std::string_view(" foo ")));
EXPECT_EQ("", str::ltrim(std::string("")));
EXPECT_EQ("foo", str::ltrim(std::string("foo")));
EXPECT_EQ("foo ", str::ltrim(std::string("foo ")));
EXPECT_EQ("foo", str::ltrim(std::string(" foo")));
EXPECT_EQ("foo ", str::ltrim(std::string(" foo ")));
EXPECT_EQ("foo ", str::ltrim(std::string(" foo ")));
EXPECT_EQ("", str::rtrim(std::string("")));
EXPECT_EQ("foo", str::rtrim(std::string("foo")));
EXPECT_EQ("foo", str::rtrim(std::string("foo ")));
EXPECT_EQ(" foo", str::rtrim(std::string(" foo")));
EXPECT_EQ(" foo", str::rtrim(std::string(" foo ")));
EXPECT_EQ(" foo", str::rtrim(std::string(" foo ")));
}
TEST(strutil, starts_with) {
EXPECT_TRUE(str::starts_with("", ""));
EXPECT_TRUE(str::starts_with("foo", ""));
EXPECT_TRUE(str::starts_with("foo", "foo"));
EXPECT_FALSE(str::starts_with("foo", "foobar"));
EXPECT_TRUE(str::starts_with("foo", "f"));
EXPECT_FALSE(str::starts_with("foo", "o"));
EXPECT_TRUE(str::starts_with("bar", ""));
EXPECT_FALSE(str::starts_with("bar", "foo"));
EXPECT_FALSE(str::starts_with("bar", "f"));
EXPECT_FALSE(str::starts_with("bar", "barfoo"));
}
TEST(strutil, ends_with) {
EXPECT_TRUE(str::ends_with("", ""));
EXPECT_TRUE(str::ends_with("foo", ""));
EXPECT_TRUE(str::ends_with("foo", "foo"));
EXPECT_FALSE(str::ends_with("foo", "barfoo"));
EXPECT_TRUE(str::ends_with("foo", "o"));
EXPECT_FALSE(str::ends_with("foo", "f"));
EXPECT_TRUE(str::ends_with("bar", ""));
EXPECT_FALSE(str::ends_with("bar", "foo"));
EXPECT_FALSE(str::ends_with("bar", "f"));
EXPECT_FALSE(str::ends_with("bar", "barfoo"));
}
TEST(strutil, join_str) {
EXPECT_EQ("", str::join(std::vector<std::string>(), ','));
EXPECT_EQ("foo", str::join(std::vector<std::string>({"foo"}), ','));
EXPECT_EQ("foo,bar", str::join(std::vector<std::string>({"foo", "bar"}),
','));
EXPECT_EQ(",,", str::join(std::vector<std::string>({"", "", ""}),
','));
EXPECT_EQ(",foo,", str::join(std::vector<std::string>({",", ","}),
"foo"));
}
TEST(strutil, join_view) {
EXPECT_EQ("", str::join(std::vector<std::string_view>(), ','));
EXPECT_EQ("foo", str::join(std::vector<std::string_view>({"foo"}), ','));
EXPECT_EQ("foo,bar", str::join(std::vector<std::string_view>({"foo", "bar"}),
','));
EXPECT_EQ(",,", str::join(std::vector<std::string_view>({"", "", ""}),
','));
EXPECT_EQ(",foo,", str::join(std::vector<std::string_view>({",", ","}),
"foo"));
}
|