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