test_string.cpp revision 8b5f35d0aa39e39119dc1e3e2fc4d283995faa2a
1/* -*- c++ -*- */ 2/* 3 * Copyright (C) 2009 The Android Open Source Project 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 20 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 23 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#include "../include/string" 31#ifndef ANDROID_ASTL_STRING__ 32#error "Wrong header included!!" 33#endif 34#include <climits> 35#include <cstring> 36#include "macros.h" 37 38#ifndef MAX_SIZE_T 39#define MAX_SIZE_T (~(size_t)0) 40#endif 41 42namespace android { 43using std::string; 44 45bool testConstructorCString() 46{ 47 string empty_str1; 48 EXPECT_TRUE(empty_str1.size() == 0); 49 EXPECT_TRUE(empty_str1.capacity() == 0); 50 51 string empty_str2(""); 52 EXPECT_TRUE(empty_str2.size() == 0); 53 54 const char empty_as_array[] = ""; 55 string empty_str3(empty_as_array); 56 EXPECT_TRUE(empty_str3.size() == 0); 57 58 const char literal[] = "scott mills cracks me up"; 59 string str1(literal); 60 EXPECT_TRUE(strcmp(literal, str1.c_str()) == 0); 61 62 string str2(literal, 11); 63 EXPECT_TRUE(strcmp("scott mills", str2.c_str()) == 0); 64 65 string str3(literal, sizeof(literal)); 66 EXPECT_TRUE(strcmp(literal, str3.c_str()) == 0); 67 68 // Pass the end of the string => still ok, there is \0 69 string str4(literal, sizeof(literal) + 1); 70 EXPECT_TRUE(str4.size() == sizeof(literal) + 1); 71 72 string str5(literal, literal + 11); 73 EXPECT_TRUE(strcmp("scott mills", str5.c_str()) == 0); 74 75 const char text[] = {'l','a','d','y',' ','g','a','g','a'}; 76 77 string str6(text, ARRAYSIZE(text)); 78 EXPECT_TRUE(str6 == "lady gaga"); 79 80 string str7(NULL); 81 EXPECT_TRUE(empty_str1.size() == 0); 82 EXPECT_TRUE(empty_str1.empty()); 83 return true; 84} 85 86bool testConstructorString() 87{ 88 string empty_str1; 89 string empty_str2; 90 EXPECT_TRUE(empty_str1.c_str() == empty_str2.c_str()); 91 92 string empty_str3(empty_str2); 93 EXPECT_TRUE(empty_str3.size() == 0); 94 95 const char string_with_nulls[] = "contains 2 \0 bytes \0."; 96 string str1 (string_with_nulls, 21); 97 EXPECT_TRUE(str1.size() == 21); 98 99 string str2 (str1); 100 EXPECT_TRUE(str1.size() == 21); 101 102 const string str3("scott mills cracks me up"); 103 string str4(str3, 12); 104 EXPECT_TRUE(strcmp("cracks me up", str4.c_str()) == 0); 105 106 string str5(str3, 12, 6); 107 EXPECT_TRUE(strcmp("cracks", str5.c_str()) == 0); 108 109 string str6(str3, 23); 110 EXPECT_TRUE(strcmp("p", str6.c_str()) == 0); 111 112 string str7(str3, 24); 113 EXPECT_TRUE(strcmp("", str7.c_str()) == 0); 114 115 string str8(str3, 23, 1); 116 EXPECT_TRUE(strcmp("p", str8.c_str()) == 0); 117 118 string str9(str3, 24, 1); 119 EXPECT_TRUE(strcmp("", str9.c_str()) == 0); 120 121 return true; 122} 123 124bool testConstructorPointers() 125{ 126 const string empty; 127 char data[] = "a 16 char string"; 128 129 string str01(data, data + 0); 130 EXPECT_TRUE(str01.c_str() == empty.c_str()); 131 132 string str02(data, data + 1); 133 EXPECT_TRUE(str02 == "a"); 134 135 string str03(data + 2, data + 16); 136 EXPECT_TRUE(str03 == "16 char string"); 137 138 string str04(data + 15, data + 16); 139 EXPECT_TRUE(str04 == "g"); 140 141 string str05(data + 16, data + 16); 142 EXPECT_TRUE(str05 == ""); 143 144 return true; 145} 146 147bool testConstructorRepeatChar() 148{ 149 string str01(0, 'c'); 150 151 EXPECT_TRUE(str01.empty()); 152 EXPECT_TRUE(str01.size() == 0); 153 EXPECT_TRUE(str01.capacity() == 0); 154 155 string str02(10, 'c'); 156 157 EXPECT_TRUE(!str02.empty()); 158 EXPECT_TRUE(str02.size() == 10); 159 EXPECT_TRUE(str02.capacity() == 10); 160 161 for (size_t i = 0; i < 100; ++i) 162 { 163 string str03(i, 'x'); 164 165 EXPECT_TRUE(str03[i] == '\0'); 166 EXPECT_TRUE(str03.length() == i); 167 168 str03.reserve(i + 20); 169 EXPECT_TRUE(str03.capacity() == i + 20); 170 EXPECT_TRUE(str03.length() == i); 171 EXPECT_TRUE(str03[i] == '\0'); 172 } 173 174 return true; 175} 176 177bool testConstructorInvalidValues() 178{ 179 const string empty; 180 const string str01("a 16 char string"); 181 182 EXPECT_TRUE(str01.size() == 16); 183 184 string str02(str01, 17, 1); // invalid index 185 EXPECT_TRUE(str02.c_str() == empty.c_str()); 186 187 string str03(str01, 17, 0); // invalid index 188 EXPECT_TRUE(str03.c_str() == empty.c_str()); 189 190 string str04(str01, -1, 0); // invalid index 191 EXPECT_TRUE(str04.c_str() == empty.c_str()); 192 193 string str05(str01, 0, 17); // invalid length 194 EXPECT_TRUE(str05.c_str() == empty.c_str()); 195 196 string str06(str01, 17); // invalid index 197 EXPECT_TRUE(str06.c_str() == empty.c_str()); 198 199 char end[] = "a string"; 200 char *begin = end + 1; // begin after end. 201 202 string str07(begin, end); 203 EXPECT_TRUE(str07.c_str() == empty.c_str()); 204 205 return true; 206} 207 208bool testSize() 209{ 210 string str01; 211 EXPECT_TRUE(str01.size() == 0); 212 EXPECT_TRUE(str01.length() == 0); 213 214 str01 += "a string."; 215 216 EXPECT_TRUE(str01.size() == 9); 217 EXPECT_TRUE(str01.length() == 9); 218 219 return true; 220} 221 222bool testCString() 223{ 224 string str01; 225 string str02; 226 227 // Should point to the same empty string. 228 EXPECT_TRUE(str01.c_str() == str02.c_str()); 229 // c_str() == data() 230 EXPECT_TRUE(str01.c_str() == str01.data()); 231 EXPECT_TRUE(str01.empty()); 232 233 const char text[] = "a string"; 234 str01 += text; 235 EXPECT_TRUE(strcmp(str01.c_str(), text) == 0); 236 EXPECT_TRUE(strcmp(str01.data(), text) == 0); 237 EXPECT_TRUE(!str01.empty()); 238 239 // after a clear, points back to the original empty string. 240 str01.clear(); 241 EXPECT_TRUE(str01.c_str() == str02.c_str()); 242 EXPECT_TRUE(str01.empty()); 243 244 return true; 245} 246 247bool testReserve() 248{ 249 string str01; 250 size_t capacity = str01.capacity(); 251 252 EXPECT_TRUE(0 == capacity); 253 254 str01.reserve(5); 255 EXPECT_TRUE(5 == str01.capacity()); 256 str01.reserve(0); 257 EXPECT_TRUE(0 == str01.capacity()); 258 259 string str02("7 chars"); 260 EXPECT_TRUE(7 == str02.capacity()); 261 EXPECT_TRUE(7 == str02.size()); 262 263 str02.reserve(10); 264 EXPECT_TRUE(str02 == "7 chars"); 265 EXPECT_TRUE(10 == str02.capacity()); 266 EXPECT_TRUE(7 == str02.size()); 267 268 str02.reserve(6); // no effect 269 EXPECT_TRUE(str02 == "7 chars"); 270 EXPECT_TRUE(10 == str02.capacity()); 271 EXPECT_TRUE(7 == str02.size()); 272 273 string str03; 274 const string str04; 275 276 // Both point to kEmptyString. 277 EXPECT_TRUE(str03.c_str() == str04.c_str()); 278 279 str03.reserve(); 280 EXPECT_TRUE(0 == str03.capacity()); 281 EXPECT_TRUE(str03.c_str() == str04.c_str()); 282 283 str03.reserve(10); 284 EXPECT_TRUE(10 == str03.capacity()); 285 // Not pointing at the empty string anymore. 286 EXPECT_TRUE(str03.c_str() != str04.c_str()); 287 288 str03.reserve(); 289 EXPECT_TRUE(0 == str03.capacity()); 290 // c_str() points back to the empty string. 291 EXPECT_TRUE(str03.c_str() == str04.c_str()); 292 293 str03.reserve(10); 294 str03.append("7 chars"); 295 EXPECT_TRUE(str03 == "7 chars"); 296 str03.reserve(); // shrink to fit. 297 EXPECT_TRUE(7 == str03.capacity()); 298 299 string str05 = "twelve chars"; 300 string str06 = str05; 301 str05.reserve(1); 302 EXPECT_TRUE(str05.capacity() == 12); 303 304 305 for (size_t i = 1; i <= 100; i *= 2) 306 { 307 string str(i, 'x'); 308 str.reserve(3 * i); 309 EXPECT_TRUE(str.capacity() == 3 * i); 310 311 str.reserve(2 * i); 312 EXPECT_TRUE(str.capacity() == 2 * i); 313 314 str.reserve(); 315 EXPECT_TRUE(str.capacity() == i); 316 } 317 318 // Check overflow. 319 string str07; 320 321 str07.reserve(10); 322 EXPECT_TRUE(str07.capacity() == 10); 323 324 str07.reserve(MAX_SIZE_T); 325 326 EXPECT_TRUE(str07.capacity() == 10); 327 328 return true; 329} 330 331bool testAppend() 332{ 333 string str1; 334 const char *text = "You spin my head right round."; 335 336 str1.append(text); 337 EXPECT_TRUE(str1 == text); 338 339 str1.append(" Flo Rida."); 340 EXPECT_TRUE(str1 == "You spin my head right round. Flo Rida."); 341 342 string str2; 343 str2.append(str1); 344 EXPECT_TRUE(str2 == "You spin my head right round. Flo Rida."); 345 346 string str3("You spin "); 347 str3.append("my head right round."); 348 EXPECT_TRUE(str3 == "You spin my head right round."); 349 350 string str4("You spin "); 351 string str5("my head right round."); 352 str4.append(str5); 353 EXPECT_TRUE(str4 == "You spin my head right round."); 354 355 string str6(""); 356 string str7(""); 357 str6.append(str7); 358 EXPECT_TRUE(str6 == ""); 359 EXPECT_TRUE(str6.empty()); 360 361 string str8; 362 str8.append("a"); 363 EXPECT_TRUE(str8 == "a"); 364 365 const char more_text[] = {'l','a','d','y',' ','g','a','g','a'}; 366 367 string str9; 368 str9.append(more_text, ARRAYSIZE(more_text)); 369 EXPECT_TRUE(str9 == string(more_text, ARRAYSIZE(more_text))); 370 371 string str10; 372 str10.append("", 0); 373 EXPECT_TRUE(str10.size() == 0 ); 374 str10.append(text, strlen(text)); 375 EXPECT_TRUE(str10 == "You spin my head right round."); 376 377 string str11; 378 str11.append("You spin my head right round.", 5, 11); 379 380 EXPECT_TRUE(str11 == "pin my head"); 381 382 // Append overflow 383 string str12("original"); 384 char dummy[] = "unused"; 385 // We lie about the size but that is ok. Since the lenght of the new string 386 // is going to be MAX_SIZE_T, the call will have not effect (there is no 387 // space for the trailing '\0'). 388 str12.append(dummy, MAX_SIZE_T); 389 EXPECT_TRUE(str12 == "original"); 390 391 return true; 392} 393 394bool testAppendOperator() 395{ 396 string str1; 397 const char *text = "You spin my head right round."; 398 399 str1 += text; 400 EXPECT_TRUE(str1 == text); 401 402 str1 += " Flo Rida."; 403 EXPECT_TRUE(str1 == "You spin my head right round. Flo Rida."); 404 405 string str2; 406 str2 += str1; 407 EXPECT_TRUE(str2 == "You spin my head right round. Flo Rida."); 408 409 string str3("You spin "); 410 str3 += "my head right round."; 411 EXPECT_TRUE(str3 == "You spin my head right round."); 412 413 string str4("You spin "); 414 string str5("my head right round."); 415 str4 += str5; 416 EXPECT_TRUE(str4 == "You spin my head right round."); 417 418 string str6(""); 419 string str7(""); 420 str6 += str7; 421 EXPECT_TRUE(str6 == ""); 422 EXPECT_TRUE(str6.empty()); 423 424 string str8; 425 str8 += "a"; 426 EXPECT_TRUE(str8 == "a"); 427 428 const char more_text[] = {'l','a','d','y',' ','g','a','g','a'}; 429 430 string str9; 431 for (size_t i = 0; i < ARRAYSIZE(more_text); ++i) 432 { 433 str9 += more_text[i]; 434 } 435 EXPECT_TRUE(str9 == "lady gaga"); 436 437 str9 += (const char *)NULL; 438 EXPECT_TRUE(str9 == "lady gaga"); 439 440 string str10(more_text, ARRAYSIZE(more_text)); 441 EXPECT_TRUE(str10 == "lady gaga"); 442 str10 += '\0'; 443 EXPECT_TRUE(str10 == "lady gaga"); 444 EXPECT_TRUE(str10 == string("lady gaga\0", 10)); 445 str10 += 'x'; 446 EXPECT_TRUE(str10 == string("lady gaga\0x", 11)); 447 EXPECT_TRUE(str10[11] == '\0'); 448 449 return true; 450} 451 452 453bool testCompare() 454{ 455 string str01("bell helmet"); 456 string str02("bell moto"); 457 string str03("bell"); 458 string str04("bell pants"); 459 string str05; 460 461 str05 = str01; 462 // Compare with self. 463 EXPECT_TRUE(str01 == str01); 464 EXPECT_TRUE(!(str01 != str01)); 465 466 EXPECT_TRUE(str01 == str05); 467 EXPECT_TRUE(str05 == str01); 468 EXPECT_TRUE(!(str01 != str05)); 469 EXPECT_TRUE(!(str05 != str01)); 470 471 EXPECT_TRUE(str01 != str02); 472 EXPECT_TRUE(str01 != str03); 473 EXPECT_TRUE(str01 != str04); 474 475 // Compare with literals. 476 EXPECT_TRUE(str01 == "bell helmet"); 477 EXPECT_TRUE(!(str01 != "bell helmet")); 478 EXPECT_TRUE("bell helmet" == str01); 479 EXPECT_TRUE(!("bell helmet" != str01)); 480 481 // Compare with char array. 482 char array[] = { 'a', ' ', 'b', 'u', 'g', '\0'}; 483 str01 = "a bug"; 484 EXPECT_TRUE(array == str01); 485 486 EXPECT_TRUE(strcmp("a bug", "a bugg") < 0); 487 488 char array2[] = { 'a', 'b', 'u', 'g', 'g' }; 489 EXPECT_TRUE(str01.compare(array2) < 0); 490 491 string str06; 492 EXPECT_TRUE(str06 != NULL); 493 return true; 494} 495 496bool testSwap() 497{ 498 string str01; 499 string str02("test"); 500 501 str01.swap(str02); 502 EXPECT_TRUE(str02.empty()); 503 EXPECT_TRUE(str01 == "test"); 504 505 string str03("altima"); 506 string str04("versa"); 507 str03.swap(str04); 508 EXPECT_TRUE(str03 == "versa"); 509 EXPECT_TRUE(str04 == "altima"); 510 511 { 512 string empty; 513 // swap can be used to clean strings 514 str04.swap(empty); 515 } 516 EXPECT_TRUE(str04.empty()); 517 518 return true; 519} 520 521bool testAccessor() 522{ 523 string str01 = "earmarks"; 524 525 EXPECT_TRUE(str01[0] == 'e'); 526 EXPECT_TRUE(str01[7] == 's'); 527 528 str01[0] = 'E'; 529 str01[7] = 'S'; 530 EXPECT_TRUE(str01 == "EarmarkS"); 531 532 for (int i = 0; i < 100; ++i) 533 { 534 string str02(i, 'x'); 535 536 str02.reserve(20); 537 538 EXPECT_TRUE(str02[i] == '\0'); 539 540 const string str03(str02); 541 EXPECT_TRUE(str03[i] == '\0'); 542 } 543 544 string str05; 545 str05.reserve(100); 546 str05[99] = 'a'; 547 548 return true; 549} 550 551 552bool testAssignment() 553{ 554 const char *literal = "Need to buy a full face helmet for Lilie."; 555 const string str01 = literal; 556 557 EXPECT_TRUE(str01.length() == strlen(literal)); 558 EXPECT_TRUE(str01.size() == strlen(literal)); 559 EXPECT_TRUE(str01.capacity() == strlen(literal)); 560 EXPECT_TRUE(str01 == literal); 561 562 string str02; 563 564 str02.assign(str01, 8, 33); 565 EXPECT_TRUE(str02 == "buy a full face helmet for Lilie."); 566 567 str02.assign(str01, 8, 0); 568 EXPECT_TRUE(str02 == ""); 569 570 str02.assign(str01, 0, 7); 571 EXPECT_TRUE(str02 == "Need to"); 572 573 str02.assign("unchanged"); 574 str02.assign(str01, 35, 1000); 575 EXPECT_TRUE(str02 == "unchanged"); 576 577 str02.assign(str01, 35, 6); 578 EXPECT_TRUE(str02 == "Lilie."); 579 580 581 str02.assign(str01, 35, 5); 582 EXPECT_TRUE(str02 == "Lilie"); 583 584 string str03; 585 586 str03.assign(literal); 587 EXPECT_TRUE(str03 == "Need to buy a full face helmet for Lilie."); 588 589 string str04; 590 591 str04.assign(str03.c_str()); 592 EXPECT_TRUE(str04 == "Need to buy a full face helmet for Lilie."); 593 594 str04.assign(str03.c_str() + 5, 10); 595 EXPECT_TRUE(str04 == "to buy a f"); 596 597 str04.assign("noop"); 598 str04.assign(NULL); 599 EXPECT_TRUE(str04 == "noop"); 600 601 str04.assign(str01, str01.size() - 1, 1); 602 EXPECT_TRUE(str04 == "."); 603 604 str04.assign("unchanged"); 605 str04.assign(str01, str01.size(), 1); 606 str04.assign(NULL, 4, 1); 607 str04.assign(NULL, 4); 608 EXPECT_TRUE(str04 == "unchanged"); 609 610 return true; 611} 612 613 614bool testConcat() 615{ 616 string str01("The full"); 617 string str02(" sentence."); 618 string str03; 619 620 str03 = str01 + str02; 621 EXPECT_TRUE(str03 == "The full sentence."); 622 623 str03 = str02 + str01; 624 EXPECT_TRUE(str03 == " sentence.The full"); 625 626 627 str03 = str01 + " sentence."; 628 EXPECT_TRUE(str03 == "The full sentence."); 629 630 str03 = "The full" + str02; 631 EXPECT_TRUE(str03 == "The full sentence."); 632 633 str03 = 'l' + str02; 634 str03 = 'l' + str03; 635 str03 = 'u' + str03; 636 str03 = 'f' + str03; 637 str03 = ' ' + str03; 638 str03 = 'e' + str03; 639 str03 = 'h' + str03; 640 str03 = 'T' + str03; 641 EXPECT_TRUE(str03 == "The full sentence."); 642 643 str03 = "The full "; 644 str03 = str03 + 's'; 645 str03 = str03 + 'e'; 646 str03 = str03 + 'n'; 647 str03 = str03 + 't'; 648 str03 = str03 + 'e'; 649 str03 = str03 + 'n'; 650 str03 = str03 + 'c'; 651 str03 = str03 + 'e'; 652 str03 = str03 + '.'; 653 EXPECT_TRUE(str03 == "The full sentence."); 654 655 // Check the new string buffer is not the same as the original one. 656 string str04("left and"); 657 string str05(" right"); 658 string str06(str04 + str05); 659 660 EXPECT_TRUE(str06 == "left and right"); 661 EXPECT_TRUE(str06.c_str() != str04.c_str()); 662 EXPECT_TRUE(str06.c_str() != str05.c_str()); 663 664 str06 = str04 + str05; 665 EXPECT_TRUE(str06 == "left and right"); 666 EXPECT_TRUE(str06.c_str() != str04.c_str()); 667 EXPECT_TRUE(str06.c_str() != str05.c_str()); 668 return true; 669} 670 671bool testPushBack() 672{ 673 string str01; 674 675 str01.push_back('a'); 676 EXPECT_TRUE(str01 == "a"); 677 EXPECT_TRUE(str01.capacity() == 1); 678 679 str01.reserve(10); 680 str01.push_back('b'); 681 EXPECT_TRUE(str01 == "ab"); 682 EXPECT_TRUE(str01.capacity() == 10); 683 EXPECT_TRUE(str01[2] == '\0'); 684 685 str01.reserve(); 686 EXPECT_TRUE(str01 == "ab"); 687 EXPECT_TRUE(str01.capacity() == 2); 688 EXPECT_TRUE(str01[2] == '\0'); 689 690 return true; 691} 692 693bool testFind() 694{ 695 string haystack("one two three one two three"); 696 697 // Don't die on null strings 698 EXPECT_TRUE(haystack.find(NULL) == string::npos); 699 EXPECT_TRUE(haystack.find(NULL, 10) == string::npos); 700 701 // C strings. 702 EXPECT_TRUE(haystack.find("one") == 0); 703 EXPECT_TRUE(haystack.find("two") == 4); 704 EXPECT_TRUE(haystack.find("t") == 4); 705 EXPECT_TRUE(haystack.find("four") == string::npos); 706 EXPECT_TRUE(haystack.find("one", string::npos) == string::npos); 707 708 // with offset 709 EXPECT_TRUE(haystack.find("one", 13) == 14); 710 EXPECT_TRUE(haystack.find("one", 14) == 14); 711 EXPECT_TRUE(haystack.find("one", 15) == string::npos); 712 EXPECT_TRUE(haystack.find("e", haystack.size() - 1) == haystack.size() - 1); 713 EXPECT_TRUE(haystack.find("e", haystack.size()) == string::npos); 714 EXPECT_TRUE(haystack.find("one", string::npos) == string::npos); 715 716 // std::string 717 EXPECT_TRUE(haystack.find(string("one")) == 0); 718 EXPECT_TRUE(haystack.find(string("two")) == 4); 719 EXPECT_TRUE(haystack.find(string("t")) == 4); 720 EXPECT_TRUE(haystack.find(string("four")) == string::npos); 721 EXPECT_TRUE(haystack.find(string("one"), string::npos) == string::npos); 722 723 // with offset 724 EXPECT_TRUE(haystack.find(string("one"), 13) == 14); 725 EXPECT_TRUE(haystack.find(string("one"), 14) == 14); 726 EXPECT_TRUE(haystack.find(string("one"), 15) == string::npos); 727 EXPECT_TRUE(haystack.find(string("e"), haystack.size() - 1) == haystack.size() - 1); 728 EXPECT_TRUE(haystack.find(string("e"), haystack.size()) == string::npos); 729 EXPECT_TRUE(haystack.find(string("one"), string::npos) == string::npos); 730 731 // Emtpy string should be found at every position in a string except 732 // past the end. 733 EXPECT_TRUE(string().find("", 0) == 0); 734 EXPECT_TRUE(string().find(string(), 0) == 0); 735 EXPECT_TRUE(string().find(string(), 10) == string::npos); 736 737 string foo = "foo"; 738 EXPECT_TRUE(foo.find("", 0) == 0); 739 EXPECT_TRUE(foo.find(string(), 0) == 0); 740 EXPECT_TRUE(foo.find(string(""), 0) == 0); 741 742 EXPECT_TRUE(foo.find("", 1) == 1); 743 EXPECT_TRUE(foo.find(string(), 1) == 1); 744 EXPECT_TRUE(foo.find(string(""), 1) == 1); 745 746 EXPECT_TRUE(foo.find("", foo.size()) == foo.size()); 747 EXPECT_TRUE(foo.find(string(), foo.size()) == foo.size()); 748 EXPECT_TRUE(foo.find(string(""), foo.size()) == foo.size()); 749 750 EXPECT_TRUE(foo.find("", foo.size() + 1) == string::npos); 751 EXPECT_TRUE(foo.find(string(), foo.size() + 1) == string::npos); 752 EXPECT_TRUE(foo.find(string(""), foo.size() + 1) == string::npos); 753 754 // Find on an empty string a non empty one should fail 755 EXPECT_TRUE(string().find("f", 0) == string::npos); 756 EXPECT_TRUE(string().find(string("f"), 0) == string::npos); 757 return true; 758} 759 760bool testCapacity() 761{ 762 string empty_string; 763 764 EXPECT_TRUE(empty_string.capacity() == 0); 765 EXPECT_TRUE(empty_string.size() == 0); 766 767 const char *text = "non empty string"; 768 const size_t len = strlen(text); 769 string str01(text); 770 771 EXPECT_TRUE(str01.capacity() == len); 772 EXPECT_TRUE(str01.size() == len); 773 return true; 774} 775 776bool testClear() 777{ 778 string empty_string; 779 780 empty_string.clear(); 781 EXPECT_TRUE(empty_string.capacity() == 0); 782 EXPECT_TRUE(empty_string.size() == 0); 783 784 string str01("non empty string"); 785 786 str01.clear(); 787 EXPECT_TRUE(str01.capacity() == 0); 788 EXPECT_TRUE(str01.size() == 0); 789 EXPECT_TRUE(str01.empty()); 790 return true; 791} 792 793bool testErase() 794{ 795 { 796 string empty_string; 797 798 empty_string.erase(); 799 EXPECT_TRUE(empty_string.capacity() == 0); 800 EXPECT_TRUE(empty_string.size() == 0); 801 802 empty_string.erase(MAX_SIZE_T); 803 EXPECT_TRUE(empty_string.capacity() == 0); 804 EXPECT_TRUE(empty_string.size() == 0); 805 806 empty_string.erase(MAX_SIZE_T, MAX_SIZE_T); 807 EXPECT_TRUE(empty_string.capacity() == 0); 808 EXPECT_TRUE(empty_string.size() == 0); 809 } 810 811 { 812 string str01("a"); 813 814 str01.erase(); 815 EXPECT_TRUE(str01.capacity() == 1); 816 EXPECT_TRUE(str01.size() == 0); 817 } 818 819 { 820 string str02("a"); 821 822 str02.erase(MAX_SIZE_T); 823 EXPECT_TRUE(str02.capacity() == 1); 824 EXPECT_TRUE(str02.size() == 1); 825 } 826 827 { 828 string str03("a"); 829 830 str03.erase(0, MAX_SIZE_T); 831 EXPECT_TRUE(str03.capacity() == 1); 832 EXPECT_TRUE(str03.size() == 0); 833 } 834 835 { 836 string str04("a"); 837 838 str04.erase(1, MAX_SIZE_T); 839 EXPECT_TRUE(str04.capacity() == 1); 840 EXPECT_TRUE(str04.size() == 1); 841 } 842 843 { 844 string str05("abcd"); 845 846 str05.erase(1, 2); 847 EXPECT_TRUE(str05.capacity() == 4); 848 EXPECT_TRUE(str05.size() == 2); 849 EXPECT_TRUE(str05 == "ad"); 850 } 851 852 { 853 string str06("abcd"); 854 855 str06.erase(0, 1); 856 EXPECT_TRUE(str06.capacity() == 4); 857 EXPECT_TRUE(str06.size() == 3); 858 EXPECT_TRUE(str06 == "bcd"); 859 } 860 861 { 862 // overlap 863 string str07("oh my god (You think I'm in control)"); 864 865 str07.erase(0, strlen("oh my god ")); 866 EXPECT_TRUE(str07.size() == 26); 867 EXPECT_TRUE(str07 == "(You think I'm in control)"); 868 } 869 870 return true; 871} 872 873} // namespace android 874 875int main(int argc, char **argv) 876{ 877 FAIL_UNLESS(testConstructorCString); 878 FAIL_UNLESS(testConstructorString); 879 FAIL_UNLESS(testConstructorRepeatChar); 880 FAIL_UNLESS(testConstructorPointers); 881 FAIL_UNLESS(testConstructorInvalidValues); 882 FAIL_UNLESS(testSize); 883 FAIL_UNLESS(testCString); 884 FAIL_UNLESS(testAppend); 885 FAIL_UNLESS(testAppendOperator); 886 FAIL_UNLESS(testConcat); 887 FAIL_UNLESS(testAssignment); 888 FAIL_UNLESS(testReserve); 889 FAIL_UNLESS(testCompare); 890 FAIL_UNLESS(testAccessor); 891 FAIL_UNLESS(testSwap); 892 FAIL_UNLESS(testPushBack); 893 FAIL_UNLESS(testFind); 894 FAIL_UNLESS(testCapacity); 895 FAIL_UNLESS(testClear); 896 FAIL_UNLESS(testErase); 897 return kPassed; 898} 899