1 | /*
|
---|
2 | * Copyright (c) 2017 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <string>
|
---|
30 | #include <initializer_list>
|
---|
31 | #include <internal/test/tests.hpp>
|
---|
32 | #include <cstdio>
|
---|
33 |
|
---|
34 | namespace std::test
|
---|
35 | {
|
---|
36 | bool string_test::run()
|
---|
37 | {
|
---|
38 | test_construction_and_assignment();
|
---|
39 | test_append();
|
---|
40 | test_insert();
|
---|
41 | test_erase();
|
---|
42 | test_replace();
|
---|
43 | test_copy();
|
---|
44 | test_find();
|
---|
45 |
|
---|
46 | return true;
|
---|
47 | }
|
---|
48 |
|
---|
49 | const char* string_test::name()
|
---|
50 | {
|
---|
51 | return "string";
|
---|
52 | }
|
---|
53 |
|
---|
54 | void string_test::test_construction_and_assignment()
|
---|
55 | {
|
---|
56 | const char* check1 = "hello";
|
---|
57 |
|
---|
58 | std::string str1{"hello"};
|
---|
59 | test_eq(
|
---|
60 | "size of string",
|
---|
61 | str1.size(), 5ul
|
---|
62 | );
|
---|
63 | test_eq(
|
---|
64 | "initialization from a cstring literal",
|
---|
65 | str1.begin(), str1.end(),
|
---|
66 | check1, check1 + 5
|
---|
67 | );
|
---|
68 |
|
---|
69 | std::string str2{str1};
|
---|
70 | test_eq(
|
---|
71 | "copy constructor",
|
---|
72 | str1.begin(), str1.end(),
|
---|
73 | str2.begin(), str2.end()
|
---|
74 | );
|
---|
75 |
|
---|
76 | std::string str3{std::move(str1)};
|
---|
77 | test_eq(
|
---|
78 | "move constructor equality",
|
---|
79 | str2.begin(), str2.end(),
|
---|
80 | str3.begin(), str3.end()
|
---|
81 | );
|
---|
82 | test_eq(
|
---|
83 | "move constructor source empty",
|
---|
84 | str1.size(), 0ul
|
---|
85 | );
|
---|
86 |
|
---|
87 | std::string str4{};
|
---|
88 | test_eq(
|
---|
89 | "default constructor empty",
|
---|
90 | str4.size(), 0ul
|
---|
91 | );
|
---|
92 |
|
---|
93 | str4.assign(str3, 2ul, 2ul);
|
---|
94 | test_eq(
|
---|
95 | "assign substring to an empty string",
|
---|
96 | str4.begin(), str4.end(),
|
---|
97 | str3.begin() + 2, str3.begin() + 4
|
---|
98 | );
|
---|
99 |
|
---|
100 | std::string str5{str3.begin() + 2, str3.begin() + 4};
|
---|
101 | test_eq(
|
---|
102 | "constructor from a pair of iterators",
|
---|
103 | str5.begin(), str5.end(),
|
---|
104 | str3.begin() + 2, str3.begin() + 4
|
---|
105 | );
|
---|
106 | }
|
---|
107 |
|
---|
108 | void string_test::test_append()
|
---|
109 | {
|
---|
110 | std::string check{"hello, world"};
|
---|
111 |
|
---|
112 | std::string str1{"hello, "};
|
---|
113 | str1.append("world");
|
---|
114 | test_eq(
|
---|
115 | "append cstring",
|
---|
116 | str1.begin(), str1.end(),
|
---|
117 | check.begin(), check.end()
|
---|
118 | );
|
---|
119 |
|
---|
120 | std::string str2{"hello, "};
|
---|
121 | str2.append(std::string{"world"});
|
---|
122 | test_eq(
|
---|
123 | "append rvalue string",
|
---|
124 | str2.begin(), str2.end(),
|
---|
125 | check.begin(), check.end()
|
---|
126 | );
|
---|
127 |
|
---|
128 | std::string str3{"hello, "};
|
---|
129 | std::string apendee{"world"};
|
---|
130 | str3.append(apendee);
|
---|
131 | test_eq(
|
---|
132 | "append lvalue string",
|
---|
133 | str3.begin(), str3.end(),
|
---|
134 | check.begin(), check.end()
|
---|
135 | );
|
---|
136 |
|
---|
137 | std::string str4{"hello, "};
|
---|
138 | str4.append(apendee.begin(), apendee.end());
|
---|
139 | test_eq(
|
---|
140 | "append iterator range",
|
---|
141 | str4.begin(), str4.end(),
|
---|
142 | check.begin(), check.end()
|
---|
143 | );
|
---|
144 |
|
---|
145 | std::string str5{"hello, "};
|
---|
146 | str5.append({'w', 'o', 'r', 'l', 'd'});
|
---|
147 | test_eq(
|
---|
148 | "append initializer list",
|
---|
149 | str5.begin(), str5.end(),
|
---|
150 | check.begin(), check.end()
|
---|
151 | );
|
---|
152 |
|
---|
153 | std::string str6{"hello, "};
|
---|
154 | str6 += "world";
|
---|
155 | test_eq(
|
---|
156 | "append using +=",
|
---|
157 | str6.begin(), str6.end(),
|
---|
158 | check.begin(), check.end()
|
---|
159 | );
|
---|
160 | }
|
---|
161 |
|
---|
162 | void string_test::test_insert()
|
---|
163 | {
|
---|
164 | std::string check{"hello, world"};
|
---|
165 |
|
---|
166 | std::string str1{", world"};
|
---|
167 | str1.insert(0, "hello");
|
---|
168 | test_eq(
|
---|
169 | "insert at the beggining",
|
---|
170 | str1.begin(), str1.end(),
|
---|
171 | check.begin(), check.end()
|
---|
172 | );
|
---|
173 |
|
---|
174 | std::string str2{"hello,world"};
|
---|
175 | str2.insert(str2.begin() + 6, ' ');
|
---|
176 | test_eq(
|
---|
177 | "insert char in the middle",
|
---|
178 | str2.begin(), str2.end(),
|
---|
179 | check.begin(), check.end()
|
---|
180 | );
|
---|
181 |
|
---|
182 | std::string str3{"heo, world"};
|
---|
183 | str3.insert(str3.begin() + 2, 2ul, 'l');
|
---|
184 | test_eq(
|
---|
185 | "insert n chars",
|
---|
186 | str3.begin(), str3.end(),
|
---|
187 | check.begin(), check.end()
|
---|
188 | );
|
---|
189 |
|
---|
190 | std::string str4{"h, world"};
|
---|
191 | std::string insertee{"ello"};
|
---|
192 | str4.insert(str4.begin() + 1, insertee.begin(),
|
---|
193 | insertee.end());
|
---|
194 | test_eq(
|
---|
195 | "insert iterator range",
|
---|
196 | str4.begin(), str4.end(),
|
---|
197 | check.begin(), check.end()
|
---|
198 | );
|
---|
199 |
|
---|
200 | std::string str5{"hel, world"};
|
---|
201 | std::initializer_list<char> init{'l', 'o'};
|
---|
202 | str5.insert(str5.begin() + 3, init);
|
---|
203 | test_eq(
|
---|
204 | "insert initializer list",
|
---|
205 | str5.begin(), str5.end(),
|
---|
206 | check.begin(), check.end()
|
---|
207 | );
|
---|
208 | }
|
---|
209 |
|
---|
210 | void string_test::test_erase()
|
---|
211 | {
|
---|
212 | std::string check{"hello"};
|
---|
213 |
|
---|
214 | std::string str1{"heXllo"};
|
---|
215 | str1.erase(str1.begin() + 2);
|
---|
216 | test_eq(
|
---|
217 | "erase single char in the middle",
|
---|
218 | str1.begin(), str1.end(),
|
---|
219 | check.begin(), check.end()
|
---|
220 | );
|
---|
221 |
|
---|
222 | std::string str2{"Xhello"};
|
---|
223 | str2.erase(str2.begin());
|
---|
224 | test_eq(
|
---|
225 | "erase single char at the beginning",
|
---|
226 | str2.begin(), str2.end(),
|
---|
227 | check.begin(), check.end()
|
---|
228 | );
|
---|
229 |
|
---|
230 | std::string str3{"helloX"};
|
---|
231 | str3.erase(str3.begin() + 5);
|
---|
232 | test_eq(
|
---|
233 | "erase single char at the end",
|
---|
234 | str3.begin(), str3.end(),
|
---|
235 | check.begin(), check.end()
|
---|
236 | );
|
---|
237 |
|
---|
238 | std::string str4{"XXXhello"};
|
---|
239 | str4.erase(0, 3);
|
---|
240 | test_eq(
|
---|
241 | "erase string at the beginning",
|
---|
242 | str4.begin(), str4.end(),
|
---|
243 | check.begin(), check.end()
|
---|
244 | );
|
---|
245 |
|
---|
246 | std::string str5{"heXXXllo"};
|
---|
247 | str5.erase(2, 3);
|
---|
248 | test_eq(
|
---|
249 | "erase string in the middle",
|
---|
250 | str5.begin(), str5.end(),
|
---|
251 | check.begin(), check.end()
|
---|
252 | );
|
---|
253 |
|
---|
254 | std::string str6{"helloXXX"};
|
---|
255 | str6.erase(5);
|
---|
256 | test_eq(
|
---|
257 | "erase string at the end",
|
---|
258 | str6.begin(), str6.end(),
|
---|
259 | check.begin(), check.end()
|
---|
260 | );
|
---|
261 |
|
---|
262 | std::string str7{"hellXXXo"};
|
---|
263 | str7.erase(str7.begin() + 4, str7.begin() + 7);
|
---|
264 | test_eq(
|
---|
265 | "erase iterator range",
|
---|
266 | str7.begin(), str7.end(),
|
---|
267 | check.begin(), check.end()
|
---|
268 | );
|
---|
269 | }
|
---|
270 |
|
---|
271 | void string_test::test_replace()
|
---|
272 | {
|
---|
273 | std::string check{"hello, world"};
|
---|
274 |
|
---|
275 | std::string str1{"helXXX world"};
|
---|
276 | str1.replace(3, 3, "lo,", 3);
|
---|
277 | test_eq(
|
---|
278 | "replace with full string",
|
---|
279 | str1.begin(), str1.end(),
|
---|
280 | check.begin(), check.end()
|
---|
281 | );
|
---|
282 |
|
---|
283 | std::string str2{"helXXX world"};
|
---|
284 | str2.replace(3, 3, "lo,YYY", 3);
|
---|
285 | test_eq(
|
---|
286 | "replace with prefix of a string",
|
---|
287 | str2.begin(), str2.end(),
|
---|
288 | check.begin(), check.end()
|
---|
289 | );
|
---|
290 |
|
---|
291 | std::string str3{"helXXX world"};
|
---|
292 | str3.replace(3, 3, "YYlo,YYY", 2, 3);
|
---|
293 | test_eq(
|
---|
294 | "replace with substring of a string",
|
---|
295 | str3.begin(), str3.end(),
|
---|
296 | check.begin(), check.end()
|
---|
297 | );
|
---|
298 |
|
---|
299 | std::string str4{"heXXo, world"};
|
---|
300 | str4.replace(2, 2, 2, 'l');
|
---|
301 | test_eq(
|
---|
302 | "replace with repeated characters",
|
---|
303 | str4.begin(), str4.end(),
|
---|
304 | check.begin(), check.end()
|
---|
305 | );
|
---|
306 |
|
---|
307 | std::string str5{"heXXXXo, world"};
|
---|
308 | str5.replace(2, 4, 2, 'l');
|
---|
309 | test_eq(
|
---|
310 | "replace with repeated characters (shrinking)",
|
---|
311 | str5.begin(), str5.end(),
|
---|
312 | check.begin(), check.end()
|
---|
313 | );
|
---|
314 |
|
---|
315 | std::string str6{"helXXXXX world"};
|
---|
316 | str6.replace(3, 5, "YYlo,YYY", 2, 3);
|
---|
317 | test_eq(
|
---|
318 | "replace with substring of a string (shrinking)",
|
---|
319 | str6.begin(), str6.end(),
|
---|
320 | check.begin(), check.end()
|
---|
321 | );
|
---|
322 |
|
---|
323 | std::string str7{"helXXXXX world"};
|
---|
324 | std::string replacer{"YYlo,YYY"};
|
---|
325 | str7.replace(3, 5, replacer, 2, 3);
|
---|
326 | test_eq(
|
---|
327 | "replace with substring of a string (shrinking, std::string)",
|
---|
328 | str7.begin(), str7.end(),
|
---|
329 | check.begin(), check.end()
|
---|
330 | );
|
---|
331 |
|
---|
332 | std::string str8{"helXXXXX world"};
|
---|
333 | str8.replace(str8.begin() + 3, str8.begin() + 8, "lo,");
|
---|
334 | test_eq(
|
---|
335 | "replace with a string (iterators)",
|
---|
336 | str8.begin(), str8.end(),
|
---|
337 | check.begin(), check.end()
|
---|
338 | );
|
---|
339 |
|
---|
340 | std::string str9{"heXXXXo, world"};
|
---|
341 | str9.replace(str9.begin() + 2, str9.begin() + 6, 2, 'l');
|
---|
342 | test_eq(
|
---|
343 | "replace with repeated characters (shrinking, iterators)",
|
---|
344 | str9.begin(), str9.end(),
|
---|
345 | check.begin(), check.end()
|
---|
346 | );
|
---|
347 |
|
---|
348 | std::string str10{"helXXXXX world"};
|
---|
349 | str10.replace(str10.begin() + 3, str10.begin() + 8,
|
---|
350 | replacer.begin() + 2, replacer.begin() + 5);
|
---|
351 | test_eq(
|
---|
352 | "replace with substring of a string (shrinking, iterators x2)",
|
---|
353 | str10.begin(), str10.end(),
|
---|
354 | check.begin(), check.end()
|
---|
355 | );
|
---|
356 |
|
---|
357 | std::string str11{"helXXXXX world"};
|
---|
358 | str11.replace(str11.begin() + 3, str11.begin() + 8,
|
---|
359 | {'l', 'o', ','});
|
---|
360 | test_eq(
|
---|
361 | "replace with an initializer list (shrinking, iterators)",
|
---|
362 | str11.begin(), str11.end(),
|
---|
363 | check.begin(), check.end()
|
---|
364 | );
|
---|
365 |
|
---|
366 | std::string str12{"helXXX world"};
|
---|
367 | str12.replace(str12.begin() + 3, str12.begin() + 6,
|
---|
368 | {'l', 'o', ','});
|
---|
369 | test_eq(
|
---|
370 | "replace with an initializer list (iterators)",
|
---|
371 | str12.begin(), str12.end(),
|
---|
372 | check.begin(), check.end()
|
---|
373 | );
|
---|
374 | }
|
---|
375 |
|
---|
376 | void string_test::test_copy()
|
---|
377 | {
|
---|
378 | std::string check{"CCABB"};
|
---|
379 |
|
---|
380 | std::string str1{"ACCCA"};
|
---|
381 | std::string str2{"BBBBB"};
|
---|
382 |
|
---|
383 | str1.copy(const_cast<char*>(str2.c_str()), 3, 2);
|
---|
384 | test_eq(
|
---|
385 | "copy",
|
---|
386 | str2.begin(), str2.end(),
|
---|
387 | check.begin(), check.end()
|
---|
388 | );
|
---|
389 | }
|
---|
390 |
|
---|
391 | void string_test::test_find()
|
---|
392 | {
|
---|
393 | std::string target{"ABC"};
|
---|
394 | auto miss = std::string::npos;
|
---|
395 |
|
---|
396 | std::string str1{"xxABCxx"};
|
---|
397 |
|
---|
398 | auto idx = str1.find(target, 0);
|
---|
399 | test_eq(
|
---|
400 | "find from start (success)",
|
---|
401 | idx, 2ul
|
---|
402 | );
|
---|
403 |
|
---|
404 | idx = str1.find(target, 3);
|
---|
405 | test_eq(
|
---|
406 | "find from start (fail, late start)",
|
---|
407 | idx, miss
|
---|
408 | );
|
---|
409 |
|
---|
410 | idx = str1.rfind(target, miss);
|
---|
411 | test_eq(
|
---|
412 | "rfind from start (success)",
|
---|
413 | idx, 2ul
|
---|
414 | );
|
---|
415 |
|
---|
416 | idx = str1.rfind(target, 1);
|
---|
417 | test_eq(
|
---|
418 | "rfind from start (fail, late start)",
|
---|
419 | idx, miss
|
---|
420 | );
|
---|
421 |
|
---|
422 | std::string str2{"xxABCxxABCxx"};
|
---|
423 |
|
---|
424 | idx = str2.find(target, 0);
|
---|
425 | test_eq(
|
---|
426 | "find from start (success, multiple)",
|
---|
427 | idx, 2ul
|
---|
428 | );
|
---|
429 | }
|
---|
430 | }
|
---|