source: mainline/uspace/lib/cpp/src/internal/test/string.cpp@ 173a246

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 173a246 was 173a246, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: added a test for std::string::copy

  • Property mode set to 100644
File size: 11.2 KB
Line 
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
34namespace 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
45 return true;
46 }
47
48 const char* string_test::name()
49 {
50 return "string";
51 }
52
53 void string_test::test_construction_and_assignment()
54 {
55 const char* check1 = "hello";
56
57 std::string str1{"hello"};
58 test_eq(
59 "size of string",
60 str1.size(), 5ul
61 );
62 test_eq(
63 "initialization from a cstring literal",
64 str1.begin(), str1.end(),
65 check1, check1 + 5
66 );
67
68 std::string str2{str1};
69 test_eq(
70 "copy constructor",
71 str1.begin(), str1.end(),
72 str2.begin(), str2.end()
73 );
74
75 std::string str3{std::move(str1)};
76 test_eq(
77 "move constructor equality",
78 str2.begin(), str2.end(),
79 str3.begin(), str3.end()
80 );
81 test_eq(
82 "move constructor source empty",
83 str1.size(), 0ul
84 );
85
86 std::string str4{};
87 test_eq(
88 "default constructor empty",
89 str4.size(), 0ul
90 );
91
92 str4.assign(str3, 2ul, 2ul);
93 test_eq(
94 "assign substring to an empty string",
95 str4.begin(), str4.end(),
96 str3.begin() + 2, str3.begin() + 4
97 );
98
99 std::string str5{str3.begin() + 2, str3.begin() + 4};
100 test_eq(
101 "constructor from a pair of iterators",
102 str5.begin(), str5.end(),
103 str3.begin() + 2, str3.begin() + 4
104 );
105 }
106
107 void string_test::test_append()
108 {
109 std::string check{"hello, world"};
110
111 std::string str1{"hello, "};
112 str1.append("world");
113 test_eq(
114 "append cstring",
115 str1.begin(), str1.end(),
116 check.begin(), check.end()
117 );
118
119 std::string str2{"hello, "};
120 str2.append(std::string{"world"});
121 test_eq(
122 "append rvalue string",
123 str2.begin(), str2.end(),
124 check.begin(), check.end()
125 );
126
127 std::string str3{"hello, "};
128 std::string apendee{"world"};
129 str3.append(apendee);
130 test_eq(
131 "append lvalue string",
132 str3.begin(), str3.end(),
133 check.begin(), check.end()
134 );
135
136 std::string str4{"hello, "};
137 str4.append(apendee.begin(), apendee.end());
138 test_eq(
139 "append iterator range",
140 str4.begin(), str4.end(),
141 check.begin(), check.end()
142 );
143
144 std::string str5{"hello, "};
145 str5.append({'w', 'o', 'r', 'l', 'd'});
146 test_eq(
147 "append initializer list",
148 str5.begin(), str5.end(),
149 check.begin(), check.end()
150 );
151
152 std::string str6{"hello, "};
153 str6 += "world";
154 test_eq(
155 "append using +=",
156 str6.begin(), str6.end(),
157 check.begin(), check.end()
158 );
159 }
160
161 void string_test::test_insert()
162 {
163 std::string check{"hello, world"};
164
165 std::string str1{", world"};
166 str1.insert(0, "hello");
167 test_eq(
168 "insert at the beggining",
169 str1.begin(), str1.end(),
170 check.begin(), check.end()
171 );
172
173 std::string str2{"hello,world"};
174 str2.insert(str2.begin() + 6, ' ');
175 test_eq(
176 "insert char in the middle",
177 str2.begin(), str2.end(),
178 check.begin(), check.end()
179 );
180
181 std::string str3{"heo, world"};
182 str3.insert(str3.begin() + 2, 2ul, 'l');
183 test_eq(
184 "insert n chars",
185 str3.begin(), str3.end(),
186 check.begin(), check.end()
187 );
188
189 std::string str4{"h, world"};
190 std::string insertee{"ello"};
191 str4.insert(str4.begin() + 1, insertee.begin(),
192 insertee.end());
193 test_eq(
194 "insert iterator range",
195 str4.begin(), str4.end(),
196 check.begin(), check.end()
197 );
198
199 std::string str5{"hel, world"};
200 std::initializer_list<char> init{'l', 'o'};
201 str5.insert(str5.begin() + 3, init);
202 test_eq(
203 "insert initializer list",
204 str5.begin(), str5.end(),
205 check.begin(), check.end()
206 );
207 }
208
209 void string_test::test_erase()
210 {
211 std::string check{"hello"};
212
213 std::string str1{"heXllo"};
214 str1.erase(str1.begin() + 2);
215 test_eq(
216 "erase single char in the middle",
217 str1.begin(), str1.end(),
218 check.begin(), check.end()
219 );
220
221 std::string str2{"Xhello"};
222 str2.erase(str2.begin());
223 test_eq(
224 "erase single char at the beginning",
225 str2.begin(), str2.end(),
226 check.begin(), check.end()
227 );
228
229 std::string str3{"helloX"};
230 str3.erase(str3.begin() + 5);
231 test_eq(
232 "erase single char at the end",
233 str3.begin(), str3.end(),
234 check.begin(), check.end()
235 );
236
237 std::string str4{"XXXhello"};
238 str4.erase(0, 3);
239 test_eq(
240 "erase string at the beginning",
241 str4.begin(), str4.end(),
242 check.begin(), check.end()
243 );
244
245 std::string str5{"heXXXllo"};
246 str5.erase(2, 3);
247 test_eq(
248 "erase string in the middle",
249 str5.begin(), str5.end(),
250 check.begin(), check.end()
251 );
252
253 std::string str6{"helloXXX"};
254 str6.erase(5);
255 test_eq(
256 "erase string at the end",
257 str6.begin(), str6.end(),
258 check.begin(), check.end()
259 );
260
261 std::string str7{"hellXXXo"};
262 str7.erase(str7.begin() + 4, str7.begin() + 7);
263 test_eq(
264 "erase iterator range",
265 str7.begin(), str7.end(),
266 check.begin(), check.end()
267 );
268 }
269
270 void string_test::test_replace()
271 {
272 std::string check{"hello, world"};
273
274 std::string str1{"helXXX world"};
275 str1.replace(3, 3, "lo,", 3);
276 test_eq(
277 "replace with full string",
278 str1.begin(), str1.end(),
279 check.begin(), check.end()
280 );
281
282 std::string str2{"helXXX world"};
283 str2.replace(3, 3, "lo,YYY", 3);
284 test_eq(
285 "replace with prefix of a string",
286 str2.begin(), str2.end(),
287 check.begin(), check.end()
288 );
289
290 std::string str3{"helXXX world"};
291 str3.replace(3, 3, "YYlo,YYY", 2, 3);
292 test_eq(
293 "replace with substring of a string",
294 str3.begin(), str3.end(),
295 check.begin(), check.end()
296 );
297
298 std::string str4{"heXXo, world"};
299 str4.replace(2, 2, 2, 'l');
300 test_eq(
301 "replace with repeated characters",
302 str4.begin(), str4.end(),
303 check.begin(), check.end()
304 );
305
306 std::string str5{"heXXXXo, world"};
307 str5.replace(2, 4, 2, 'l');
308 test_eq(
309 "replace with repeated characters (shrinking)",
310 str5.begin(), str5.end(),
311 check.begin(), check.end()
312 );
313
314 std::string str6{"helXXXXX world"};
315 str6.replace(3, 5, "YYlo,YYY", 2, 3);
316 test_eq(
317 "replace with substring of a string (shrinking)",
318 str6.begin(), str6.end(),
319 check.begin(), check.end()
320 );
321
322 std::string str7{"helXXXXX world"};
323 std::string replacer{"YYlo,YYY"};
324 str7.replace(3, 5, replacer, 2, 3);
325 test_eq(
326 "replace with substring of a string (shrinking, std::string)",
327 str7.begin(), str7.end(),
328 check.begin(), check.end()
329 );
330
331 std::string str8{"helXXXXX world"};
332 str8.replace(str8.begin() + 3, str8.begin() + 8, "lo,");
333 test_eq(
334 "replace with a string (iterators)",
335 str8.begin(), str8.end(),
336 check.begin(), check.end()
337 );
338
339 std::string str9{"heXXXXo, world"};
340 str9.replace(str9.begin() + 2, str9.begin() + 6, 2, 'l');
341 test_eq(
342 "replace with repeated characters (shrinking, iterators)",
343 str9.begin(), str9.end(),
344 check.begin(), check.end()
345 );
346
347 std::string str10{"helXXXXX world"};
348 str10.replace(str10.begin() + 3, str10.begin() + 8,
349 replacer.begin() + 2, replacer.begin() + 5);
350 test_eq(
351 "replace with substring of a string (shrinking, iterators x2)",
352 str10.begin(), str10.end(),
353 check.begin(), check.end()
354 );
355
356 std::string str11{"helXXXXX world"};
357 str11.replace(str11.begin() + 3, str11.begin() + 8,
358 {'l', 'o', ','});
359 test_eq(
360 "replace with an initializer list (shrinking, iterators)",
361 str11.begin(), str11.end(),
362 check.begin(), check.end()
363 );
364
365 std::string str12{"helXXX world"};
366 str12.replace(str12.begin() + 3, str12.begin() + 6,
367 {'l', 'o', ','});
368 test_eq(
369 "replace with an initializer list (iterators)",
370 str12.begin(), str12.end(),
371 check.begin(), check.end()
372 );
373 }
374
375 void string_test::test_copy()
376 {
377 std::string check{"CCABB"};
378
379 std::string str1{"ACCCA"};
380 std::string str2{"BBBBB"};
381
382 str1.copy(const_cast<char*>(str2.c_str()), 3, 2);
383 test_eq(
384 "copy",
385 str2.begin(), str2.end(),
386 check.begin(), check.end()
387 );
388 }
389}
Note: See TracBrowser for help on using the repository browser.