Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/shaderc
Path: blob/main/libshaderc_util/src/string_piece_test.cc
1560 views
1
// Copyright 2015 The Shaderc Authors. All rights reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include "libshaderc_util/string_piece.h"
16
17
#include <gtest/gtest.h>
18
#include <sstream>
19
#include <unordered_map>
20
21
#include "death_test.h"
22
23
namespace {
24
25
using shaderc_util::string_piece;
26
27
TEST(string_piece, creation) {
28
std::string my_string("std::string");
29
const char* my_c_string = "c::string";
30
31
string_piece my_string_piece(my_string);
32
string_piece my_c_string_piece(my_c_string);
33
string_piece my_partial_c_string_piece(my_c_string, my_c_string + 3);
34
string_piece my_string_piece_string_piece(my_string_piece);
35
36
EXPECT_EQ("std::string", my_string_piece);
37
EXPECT_EQ("c::string", my_c_string_piece);
38
EXPECT_EQ("c::", my_partial_c_string_piece);
39
EXPECT_EQ("std::string", my_string_piece_string_piece);
40
}
41
42
TEST(string_piece, creation_with_empty_data) {
43
string_piece my_string_piece(nullptr, nullptr);
44
EXPECT_EQ("", my_string_piece);
45
}
46
47
TEST(string_piece, creation_with_nullptr) {
48
string_piece my_string_piece(nullptr);
49
EXPECT_EQ("", my_string_piece);
50
}
51
52
TEST(string_pieceDeathTest, creation_causing_assert) {
53
EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece("my_cstring", nullptr), ".*");
54
EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece(nullptr, "my_cstring"), ".*");
55
}
56
57
TEST(string_pieceDeathTest, front) {
58
EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece(nullptr).front(), "Assertion");
59
EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece(nullptr, nullptr).front(),
60
"Assertion");
61
EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece("").front(), "Assertion");
62
string_piece s("nonempty");
63
s.clear();
64
EXPECT_DEBUG_DEATH_IF_SUPPORTED(s.front(), "Assertion");
65
}
66
67
TEST(string_pieceDeathTest, back) {
68
EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece(nullptr).back(), "Assertion");
69
EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece(nullptr, nullptr).back(),
70
"Assertion");
71
EXPECT_DEBUG_DEATH_IF_SUPPORTED(string_piece("").back(), "Assertion");
72
string_piece s("nonempty");
73
s.clear();
74
EXPECT_DEBUG_DEATH_IF_SUPPORTED(s.back(), "Assertion");
75
}
76
77
TEST(string_piece, substr) {
78
string_piece my_string("my really long string");
79
EXPECT_EQ("my really long string", my_string.substr(0, string_piece::npos));
80
EXPECT_EQ("my really long string", my_string.substr(0));
81
EXPECT_EQ("really long string", my_string.substr(3, string_piece::npos));
82
EXPECT_EQ("really long string", my_string.substr(3));
83
EXPECT_EQ("really", my_string.substr(3, 6));
84
}
85
86
TEST(string_piece, length) {
87
EXPECT_EQ(0u, string_piece().size());
88
EXPECT_TRUE(string_piece().empty());
89
EXPECT_EQ(10u, string_piece("0123456789").size());
90
91
std::string my_string("std::string");
92
EXPECT_EQ(my_string.size(), string_piece(my_string).size());
93
}
94
95
TEST(string_piece, clear) {
96
string_piece my_string("my really long string");
97
EXPECT_EQ("my really long string", my_string);
98
99
string_piece other_string(my_string);
100
EXPECT_EQ("my really long string", other_string);
101
102
my_string.clear();
103
EXPECT_EQ("", my_string);
104
EXPECT_EQ("my really long string", other_string);
105
}
106
107
TEST(string_piece, str) {
108
std::string test_string;
109
{
110
std::string temporary_string("my really long string");
111
string_piece my_stringpiece(temporary_string);
112
string_piece my_substring = my_stringpiece.substr(3, 6);
113
114
EXPECT_EQ("really", my_substring);
115
test_string = my_substring.str();
116
}
117
EXPECT_EQ("really", test_string);
118
}
119
120
template <char C>
121
bool find_char(char c) {
122
return c == C;
123
}
124
125
TEST(string_piece, find_first_not_matching) {
126
string_piece my_string("aaaaaaa b");
127
EXPECT_EQ(7u, my_string.find_first_not_matching(find_char<'a'>));
128
EXPECT_EQ(0u, my_string.find_first_not_matching(find_char<'b'>));
129
EXPECT_EQ(0u, string_piece(" ").find_first_not_matching(::isdigit));
130
size_t npos = string_piece::npos;
131
EXPECT_EQ(npos, string_piece("").find_first_not_matching(::isdigit));
132
EXPECT_EQ(npos, string_piece("123").find_first_not_matching(::isdigit));
133
EXPECT_EQ(3u, string_piece("123 ").find_first_not_matching(::isdigit));
134
}
135
136
TEST(string_piece, find_first_not_of) {
137
size_t npos = string_piece::npos;
138
string_piece my_string("aaaaaaa b");
139
EXPECT_EQ(7u, my_string.find_first_not_of("a"));
140
EXPECT_EQ(0u, my_string.find_first_not_of("b"));
141
EXPECT_EQ(7u, my_string.find_first_not_of('a'));
142
EXPECT_EQ(0u, my_string.find_first_not_of('b'));
143
EXPECT_EQ(0u, string_piece(" ").find_first_not_of("0123456789"));
144
145
EXPECT_EQ(7u, my_string.find_first_not_of("a", 2));
146
EXPECT_EQ(2u, my_string.find_first_not_of("b", 2));
147
EXPECT_EQ(7u, my_string.find_first_not_of('a', 2));
148
EXPECT_EQ(4u, my_string.find_first_not_of('b', 4));
149
EXPECT_EQ(0u, string_piece(" ").find_first_not_of("0123456789"));
150
EXPECT_EQ(npos, string_piece(" ").find_first_not_of("0123456789", 5));
151
152
EXPECT_EQ(npos, string_piece("").find_first_not_of("012345689"));
153
EXPECT_EQ(npos, string_piece("").find_first_not_of("012345689", 1));
154
EXPECT_EQ(npos, string_piece("123").find_first_not_of("0123456789"));
155
EXPECT_EQ(npos, string_piece("123").find_first_not_of("0123456789", 1));
156
EXPECT_EQ(3u, string_piece("123 ").find_first_not_of("0123456789", 2));
157
EXPECT_EQ(npos, string_piece("123 ").find_first_not_of("0123456789", 4));
158
EXPECT_EQ(npos, string_piece("").find_first_not_of("1"));
159
EXPECT_EQ(npos, string_piece("111").find_first_not_of('1'));
160
}
161
162
TEST(string_piece, find_first_of_char) {
163
const size_t npos = string_piece::npos;
164
string_piece my_string("my really long string");
165
EXPECT_EQ(0u, my_string.find_first_of('m'));
166
EXPECT_EQ(3u, my_string.find_first_of('r'));
167
EXPECT_EQ(npos, my_string.find_first_of('z'));
168
169
size_t pos = my_string.find_first_of('l');
170
EXPECT_EQ(6u, pos);
171
// If pos points to a 'l' then we should just find that one
172
EXPECT_EQ(6u, my_string.find_first_of('l', pos));
173
EXPECT_EQ(7u, my_string.find_first_of('l', pos + 1));
174
EXPECT_EQ(10u, my_string.find_first_of('l', pos + 2));
175
EXPECT_EQ(npos, my_string.find_first_of('l', pos + 5));
176
EXPECT_EQ(npos, my_string.find_first_of('z', 0));
177
EXPECT_EQ(npos, my_string.find_first_of('z', npos));
178
179
my_string.clear();
180
EXPECT_EQ(npos, my_string.find_first_of('a'));
181
EXPECT_EQ(npos, my_string.find_first_of('a', 0));
182
}
183
184
TEST(string_piece, find_first_of) {
185
string_piece my_string("aaaaaa b");
186
EXPECT_EQ(0u, my_string.find_first_of("a"));
187
EXPECT_EQ(7u, my_string.find_first_of("b"));
188
EXPECT_EQ(6u, my_string.find_first_of(" "));
189
size_t npos = string_piece::npos;
190
EXPECT_EQ(npos, my_string.find_first_of("xh"));
191
EXPECT_EQ(6u, my_string.find_first_of(" x"));
192
EXPECT_EQ(6u, my_string.find_first_of(" b"));
193
EXPECT_EQ(0u, my_string.find_first_of("ab"));
194
195
EXPECT_EQ(6u, my_string.find_first_of(" x", 2));
196
EXPECT_EQ(6u, my_string.find_first_of(" b", 2));
197
EXPECT_EQ(2u, my_string.find_first_of("ab", 2));
198
EXPECT_EQ(npos, my_string.find_first_of("ab", 10));
199
200
EXPECT_EQ(npos, my_string.find_first_of("c"));
201
EXPECT_EQ(npos, my_string.find_first_of("c", 1));
202
EXPECT_EQ(npos, string_piece(" ").find_first_of("a"));
203
EXPECT_EQ(npos, string_piece(" ").find_first_of("a", 10));
204
EXPECT_EQ(npos, string_piece("aa").find_first_of(""));
205
EXPECT_EQ(npos, string_piece("aa").find_first_of("", 1));
206
EXPECT_EQ(npos, string_piece("").find_first_of(""));
207
EXPECT_EQ(npos, string_piece("").find_first_of("", 1));
208
EXPECT_EQ(npos, string_piece("").find_first_of("a"));
209
EXPECT_EQ(npos, string_piece("").find_first_of("ae"));
210
EXPECT_EQ(npos, string_piece("").find_first_of("ae", 32));
211
}
212
213
TEST(string_piece, find_last_of) {
214
string_piece my_string("aaaaaa b");
215
EXPECT_EQ(5u, my_string.find_last_of('a'));
216
EXPECT_EQ(7u, my_string.find_last_of('b'));
217
EXPECT_EQ(6u, my_string.find_last_of(' '));
218
EXPECT_EQ(5u, my_string.find_last_of("a"));
219
EXPECT_EQ(7u, my_string.find_last_of("b"));
220
EXPECT_EQ(6u, my_string.find_last_of(" "));
221
size_t npos = string_piece::npos;
222
EXPECT_EQ(npos, my_string.find_last_of("xh"));
223
EXPECT_EQ(6u, my_string.find_last_of(" x"));
224
EXPECT_EQ(7u, my_string.find_last_of(" b"));
225
EXPECT_EQ(7u, my_string.find_last_of("ab"));
226
227
EXPECT_EQ(4u, my_string.find_last_of('a', 4));
228
EXPECT_EQ(5u, my_string.find_last_of('a', 6));
229
EXPECT_EQ(0u, string_piece("abbbaa").find_last_of('a', 3));
230
EXPECT_EQ(4u, string_piece("abbbaa").find_last_of('a', 4));
231
EXPECT_EQ(5u, string_piece("abbbaa").find_last_of('a', 5));
232
EXPECT_EQ(5u, string_piece("abbbaa").find_last_of('a', 6));
233
EXPECT_EQ(npos, string_piece("abbbaa").find_last_of('c', 2));
234
235
EXPECT_EQ(npos, my_string.find_last_of("c"));
236
EXPECT_EQ(npos, string_piece(" ").find_last_of("a"));
237
EXPECT_EQ(npos, string_piece("aa").find_last_of(""));
238
EXPECT_EQ(npos, string_piece("").find_last_of(""));
239
EXPECT_EQ(npos, string_piece("").find_last_of("a"));
240
EXPECT_EQ(npos, my_string.find_last_of('c'));
241
EXPECT_EQ(npos, string_piece(" ").find_last_of('a'));
242
EXPECT_EQ(npos, string_piece("").find_last_of('a'));
243
EXPECT_EQ(npos, string_piece("").find_last_of("ae"));
244
}
245
246
TEST(string_piece, begin_end) {
247
const char* my_string = "my really long string";
248
string_piece p(my_string);
249
size_t pos = 0;
250
for (auto it = p.begin(); it != p.end(); ++it) {
251
EXPECT_EQ(my_string[pos++], *it);
252
}
253
pos = 0;
254
for (auto c : p) {
255
EXPECT_EQ(my_string[pos++], c);
256
}
257
}
258
259
TEST(string_piece, front_back) {
260
// EXPECT_TRUE() is used here because gtest will think we are comparing
261
// between pointer and integer here if EXPECT_EQ() is used.
262
const string_piece one_char("a");
263
EXPECT_TRUE(one_char.front() == 'a');
264
EXPECT_TRUE(one_char.back() == 'a');
265
266
const string_piece two_chars("bc");
267
EXPECT_TRUE(two_chars.front() == 'b');
268
EXPECT_TRUE(two_chars.back() == 'c');
269
270
const string_piece multi_chars("w vm g gg t\t");
271
EXPECT_TRUE(multi_chars.front() == 'w');
272
EXPECT_TRUE(multi_chars.back() == '\t');
273
}
274
275
TEST(string_piece, starts_with) {
276
EXPECT_TRUE(string_piece("my string").starts_with("my"));
277
EXPECT_TRUE(string_piece("my string").starts_with("my s"));
278
EXPECT_TRUE(string_piece("my string").starts_with("m"));
279
EXPECT_TRUE(string_piece("my string").starts_with(""));
280
EXPECT_TRUE(string_piece("my string").starts_with("my string"));
281
EXPECT_TRUE(string_piece("").starts_with(""));
282
283
EXPECT_FALSE(string_piece("").starts_with("a"));
284
EXPECT_FALSE(string_piece("my string").starts_with(" "));
285
EXPECT_FALSE(string_piece("my string").starts_with("my stq"));
286
EXPECT_FALSE(string_piece("my string").starts_with("a"));
287
EXPECT_FALSE(string_piece("my string").starts_with("my strings"));
288
}
289
290
TEST(string_piece, find) {
291
const size_t npos = string_piece::npos;
292
string_piece my_string("gooogle gooogle");
293
294
EXPECT_EQ(0u, my_string.find(""));
295
296
EXPECT_EQ(0u, my_string.find("g"));
297
EXPECT_EQ(4u, my_string.find("g", 1));
298
299
EXPECT_EQ(0u, my_string.find("go"));
300
EXPECT_EQ(8u, my_string.find("go", 1));
301
302
EXPECT_EQ(1u, my_string.find("oo"));
303
EXPECT_EQ(1u, my_string.find("oo", 1));
304
EXPECT_EQ(2u, my_string.find("oo", 2));
305
EXPECT_EQ(9u, my_string.find("oo", 3));
306
307
EXPECT_EQ(4u, my_string.find("gle"));
308
EXPECT_EQ(12u, my_string.find("gle", 5));
309
310
EXPECT_EQ(npos, my_string.find("0"));
311
EXPECT_EQ(npos, my_string.find("does-not-exist"));
312
EXPECT_EQ(npos, my_string.find("longer than gooogle gooogle"));
313
314
EXPECT_EQ(npos, my_string.find("", npos));
315
EXPECT_EQ(npos, my_string.find("gle", npos));
316
}
317
318
TEST(string_piece, get_fields) {
319
string_piece input;
320
std::vector<string_piece> expected_lines;
321
EXPECT_EQ(expected_lines, input.get_fields('\n'));
322
EXPECT_EQ(expected_lines, input.get_fields('\n', true));
323
324
input = "first line";
325
expected_lines = {"first line"};
326
EXPECT_EQ(expected_lines, input.get_fields('\n'));
327
EXPECT_EQ(expected_lines, input.get_fields('\n', true));
328
329
input = "first line\n";
330
expected_lines = {"first line"};
331
EXPECT_EQ(expected_lines, input.get_fields('\n'));
332
expected_lines = {"first line\n"};
333
EXPECT_EQ(expected_lines, input.get_fields('\n', true));
334
335
input = "\nfirst line";
336
expected_lines = {"", "first line"};
337
EXPECT_EQ(expected_lines, input.get_fields('\n'));
338
expected_lines = {"\n", "first line"};
339
EXPECT_EQ(expected_lines, input.get_fields('\n', true));
340
341
input = "first line\nsecond line\nthird line\n";
342
expected_lines = {"first line", "second line", "third line"};
343
EXPECT_EQ(expected_lines, input.get_fields('\n'));
344
expected_lines = {"first line\n", "second line\n", "third line\n"};
345
EXPECT_EQ(expected_lines, input.get_fields('\n', true));
346
347
input = "first line\n\nsecond line\n\nthird line\n\n";
348
expected_lines = {"first line", "", "second line", "", "third line", ""};
349
EXPECT_EQ(expected_lines, input.get_fields('\n'));
350
expected_lines = {"first line\n", "\n", "second line\n",
351
"\n", "third line\n", "\n"};
352
EXPECT_EQ(expected_lines, input.get_fields('\n', true));
353
}
354
355
TEST(string_piece, operator_stream_out) {
356
std::stringstream stream;
357
string_piece my_string("my really long string");
358
stream << my_string;
359
EXPECT_EQ("my really long string", stream.str());
360
stream.str("");
361
stream << my_string.substr(3, 6);
362
EXPECT_EQ("really", stream.str());
363
stream.str("");
364
stream << string_piece();
365
EXPECT_EQ("", stream.str());
366
}
367
368
TEST(string_piece, lrstrip) {
369
string_piece nothing_to_remove("abcdefg");
370
EXPECT_EQ("abcdefg", nothing_to_remove.lstrip("hijklmn"));
371
EXPECT_EQ("abcdefg", nothing_to_remove.rstrip("hijklmn"));
372
EXPECT_EQ("abcdefg", nothing_to_remove.strip("hijklmn"));
373
374
string_piece empty_string("");
375
EXPECT_EQ(0u, empty_string.lstrip("google").size());
376
EXPECT_EQ(0u, empty_string.rstrip("google").size());
377
EXPECT_EQ(0u, empty_string.strip("google").size());
378
379
string_piece remove_nothing("asdfghjkl");
380
EXPECT_EQ("asdfghjkl", remove_nothing.lstrip(""));
381
EXPECT_EQ("asdfghjkl", remove_nothing.rstrip(""));
382
EXPECT_EQ("asdfghjkl", remove_nothing.strip(""));
383
384
string_piece strip_numbers("0123g4o5o6g7l8e9");
385
EXPECT_EQ("g4o5o6g7l8e9", strip_numbers.lstrip("0123456789"));
386
EXPECT_EQ("0123g4o5o6g7l8e", strip_numbers.rstrip("0123456789"));
387
EXPECT_EQ("g4o5o6g7l8e", strip_numbers.strip("0123456789"));
388
}
389
390
TEST(string_piece, strip_whitespace) {
391
string_piece lots_of_space(" b i n g o ");
392
EXPECT_EQ("b i n g o", lots_of_space.strip_whitespace());
393
394
string_piece whitespaces("\v\t\f\n\rleft\r\t\f\n\vright\f\n\t\v\r");
395
EXPECT_EQ("left\r\t\f\n\vright", whitespaces.strip_whitespace());
396
397
string_piece remove_all(" \t ");
398
EXPECT_EQ(0u, remove_all.strip_whitespace().size());
399
}
400
401
TEST(string_piece, not_equal) {
402
EXPECT_FALSE(string_piece() != string_piece());
403
EXPECT_FALSE(string_piece("") != string_piece());
404
EXPECT_TRUE(string_piece() != string_piece(" "));
405
EXPECT_FALSE(string_piece("abc") != string_piece("abc"));
406
EXPECT_TRUE(string_piece("abc") != string_piece("abc "));
407
EXPECT_TRUE(string_piece("abc") != string_piece("abd"));
408
409
EXPECT_FALSE("" != string_piece());
410
EXPECT_FALSE("" != string_piece(""));
411
EXPECT_TRUE(" " != string_piece(""));
412
EXPECT_FALSE("abc" != string_piece("abc"));
413
EXPECT_TRUE(" abc" != string_piece("abc"));
414
EXPECT_TRUE("abd" != string_piece("abc"));
415
}
416
417
TEST(string_piece, data) {
418
EXPECT_EQ(nullptr, string_piece().data());
419
const char* empty = "";
420
EXPECT_EQ(empty, string_piece(empty).data());
421
const char* space = " ";
422
EXPECT_EQ(space, string_piece(space).data());
423
const char* a = "a";
424
EXPECT_EQ(a, string_piece(a).data());
425
const char* abc = "abc";
426
EXPECT_EQ(abc, string_piece(abc).data());
427
EXPECT_EQ(abc + 1, string_piece(abc).substr(1).data());
428
EXPECT_EQ(abc + 3, string_piece(abc).substr(3).data());
429
}
430
431
TEST(string_piece, unordered_map) {
432
std::unordered_map<string_piece, int> dict;
433
dict["abc"] = 123;
434
EXPECT_EQ(123, dict["abc"]);
435
}
436
437
} // anonymous namespace
438
439