source: mainline/uspace/lib/cpp/src/internal/test/string.cpp@ 417296cd

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

cpp: added missing iterator range erase test for string

  • Property mode set to 100644
File size: 7.5 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
43 return true;
44 }
45
46 const char* string_test::name()
47 {
48 return "string";
49 }
50
51 void string_test::test_construction_and_assignment()
52 {
53 const char* check1 = "hello";
54
55 std::string str1{"hello"};
56 test_eq(
57 "size of string",
58 str1.size(), 5ul
59 );
60 test_eq(
61 "initialization from a cstring literal",
62 str1.begin(), str1.end(),
63 check1, check1 + 5
64 );
65
66 std::string str2{str1};
67 test_eq(
68 "copy constructor",
69 str1.begin(), str1.end(),
70 str2.begin(), str2.end()
71 );
72
73 std::string str3{std::move(str1)};
74 test_eq(
75 "move constructor equality",
76 str2.begin(), str2.end(),
77 str3.begin(), str3.end()
78 );
79 test_eq(
80 "move constructor source empty",
81 str1.size(), 0ul
82 );
83
84 std::string str4{};
85 test_eq(
86 "default constructor empty",
87 str4.size(), 0ul
88 );
89
90 str4.assign(str3, 2ul, 2ul);
91 test_eq(
92 "assign substring to an empty string",
93 str4.begin(), str4.end(),
94 str3.begin() + 2, str3.begin() + 4
95 );
96
97 std::string str5{str3.begin() + 2, str3.begin() + 4};
98 test_eq(
99 "constructor from a pair of iterators",
100 str5.begin(), str5.end(),
101 str3.begin() + 2, str3.begin() + 4
102 );
103 }
104
105 void string_test::test_append()
106 {
107 std::string check{"hello, world"};
108
109 std::string str1{"hello, "};
110 str1.append("world");
111 test_eq(
112 "append cstring",
113 str1.begin(), str1.end(),
114 check.begin(), check.end()
115 );
116
117 std::string str2{"hello, "};
118 str2.append(std::string{"world"});
119 test_eq(
120 "append rvalue string",
121 str2.begin(), str2.end(),
122 check.begin(), check.end()
123 );
124
125 std::string str3{"hello, "};
126 std::string apendee{"world"};
127 str3.append(apendee);
128 test_eq(
129 "append lvalue string",
130 str3.begin(), str3.end(),
131 check.begin(), check.end()
132 );
133
134 std::string str4{"hello, "};
135 str4.append(apendee.begin(), apendee.end());
136 test_eq(
137 "append iterator range",
138 str4.begin(), str4.end(),
139 check.begin(), check.end()
140 );
141
142 std::string str5{"hello, "};
143 str5.append({'w', 'o', 'r', 'l', 'd'});
144 test_eq(
145 "append initializer list",
146 str5.begin(), str5.end(),
147 check.begin(), check.end()
148 );
149
150 std::string str6{"hello, "};
151 str6 += "world";
152 test_eq(
153 "append using +=",
154 str6.begin(), str6.end(),
155 check.begin(), check.end()
156 );
157 }
158
159 void string_test::test_insert()
160 {
161 std::string check{"hello, world"};
162
163 std::string str1{", world"};
164 str1.insert(0, "hello");
165 test_eq(
166 "insert at the beggining",
167 str1.begin(), str1.end(),
168 check.begin(), check.end()
169 );
170
171 std::string str2{"hello,world"};
172 str2.insert(str2.begin() + 6, ' ');
173 test_eq(
174 "insert char in the middle",
175 str2.begin(), str2.end(),
176 check.begin(), check.end()
177 );
178
179 std::string str3{"heo, world"};
180 str3.insert(str3.begin() + 2, 2ul, 'l');
181 test_eq(
182 "insert n chars",
183 str3.begin(), str3.end(),
184 check.begin(), check.end()
185 );
186
187 std::string str4{"h, world"};
188 std::string insertee{"ello"};
189 str4.insert(str4.begin() + 1, insertee.begin(),
190 insertee.end());
191 test_eq(
192 "insert iterator range",
193 str4.begin(), str4.end(),
194 check.begin(), check.end()
195 );
196
197 std::string str5{"hel, world"};
198 std::initializer_list<char> init{'l', 'o'};
199 str5.insert(str5.begin() + 3, init);
200 test_eq(
201 "insert initializer list",
202 str5.begin(), str5.end(),
203 check.begin(), check.end()
204 );
205 }
206
207 void string_test::test_erase()
208 {
209 std::string check{"hello"};
210
211 std::string str1{"heXllo"};
212 str1.erase(str1.begin() + 2);
213 test_eq(
214 "erase single char in the middle",
215 str1.begin(), str1.end(),
216 check.begin(), check.end()
217 );
218
219 std::string str2{"Xhello"};
220 str2.erase(str2.begin());
221 test_eq(
222 "erase single char at the beginning",
223 str2.begin(), str2.end(),
224 check.begin(), check.end()
225 );
226
227 std::string str3{"helloX"};
228 str3.erase(str3.begin() + 5);
229 test_eq(
230 "erase single char at the end",
231 str3.begin(), str3.end(),
232 check.begin(), check.end()
233 );
234
235 std::string str4{"XXXhello"};
236 str4.erase(0, 3);
237 test_eq(
238 "erase string at the beginning",
239 str4.begin(), str4.end(),
240 check.begin(), check.end()
241 );
242
243 std::string str5{"heXXXllo"};
244 str5.erase(2, 3);
245 test_eq(
246 "erase string in the middle",
247 str5.begin(), str5.end(),
248 check.begin(), check.end()
249 );
250
251 std::string str6{"helloXXX"};
252 str6.erase(5);
253 test_eq(
254 "erase string at the end",
255 str6.begin(), str6.end(),
256 check.begin(), check.end()
257 );
258
259 std::string str7{"hellXXXo"};
260 str7.erase(str7.begin() + 4, str7.begin() + 7);
261 test_eq(
262 "erase iterator range",
263 str7.begin(), str7.end(),
264 check.begin(), check.end()
265 );
266 }
267}
Note: See TracBrowser for help on using the repository browser.