smallstring-test.cpp 25.1 KB
Newer Older
Marco Bubke's avatar
Marco Bubke committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

Marco Bubke's avatar
Marco Bubke committed
26
#include "googletest.h"
Marco Bubke's avatar
Marco Bubke committed
27 28 29 30

#include <QString>

#include <utils/smallstring.h>
31 32
#include <utils/smallstringio.h>
#include <utils/smallstringvector.h>
Marco Bubke's avatar
Marco Bubke committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

#ifndef __cpp_constexpr
#define constexpr
#endif

using namespace ::testing;

using Utils::SmallString;
using Utils::SmallStringLiteral;
using Utils::SmallStringView;

TEST(SmallString, BasicStringEqual)
{
    ASSERT_THAT(SmallString("text"), Eq(SmallString("text")));
}

TEST(SmallString, BasicSmallStringUnequal)
{
    ASSERT_THAT(SmallString("text"), Ne(SmallString("other text")));
}

TEST(SmallString, NullSmallStringIsEqualToEmptySmallString)
{
    ASSERT_THAT(SmallString(), Eq(SmallString("")));
}

TEST(SmallString, ShortSmallStringLiteralIsShortSmallString)
{
    constexpr SmallStringLiteral shortText("short string");

#if __cpp_constexpr >= 201304
    ASSERT_TRUE(shortText.isShortString());
#else
66
    ASSERT_TRUE(shortText.isReadOnlyReference());
Marco Bubke's avatar
Marco Bubke committed
67 68 69 70 71 72 73 74 75 76
#endif
}

TEST(SmallString, ShortSmallStringIsShortSmallString)
{
    SmallString shortText("short string");

#if __cpp_constexpr >= 201304
    ASSERT_TRUE(shortText.isShortString());
#else
77
    ASSERT_TRUE(shortText.isReadOnlyReference());
Marco Bubke's avatar
Marco Bubke committed
78 79 80
#endif
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
TEST(SmallString, CreateFromCStringIterators)
{
    char sourceText[] = "this is very very very very very much text";

    SmallString text(sourceText, &sourceText[sizeof(sourceText) - 1]);

    ASSERT_THAT(text, SmallString("this is very very very very very much text"));
}

TEST(SmallString, CreateFromQByteArrayIterators)
{
    QByteArray sourceText = "this is very very very very very much text";

    SmallString text(sourceText.begin(), sourceText.end());

    ASSERT_THAT(text, SmallString("this is very very very very very much text"));
}

TEST(SmallString, CreateFromSmallStringIterators)
{
    SmallString sourceText = "this is very very very very very much text";

    SmallString text(sourceText.begin(), sourceText.end());

    ASSERT_THAT(text, SmallString("this is very very very very very much text"));
}

Marco Bubke's avatar
Marco Bubke committed
108 109 110 111
TEST(SmallString, ShortSmallStringIsReference)
{
    SmallString longText("very very very very very long text");

112
    ASSERT_TRUE(longText.isReadOnlyReference());
Marco Bubke's avatar
Marco Bubke committed
113 114
}

115 116 117 118 119 120 121 122
TEST(SmallString, SmallStringContructorIsNotReference)
{
    const char *shortCSmallString = "short string";
    auto shortText = SmallString(shortCSmallString);

    ASSERT_TRUE(shortText.isShortString());
}

Marco Bubke's avatar
Marco Bubke committed
123 124 125 126 127
TEST(SmallString, ShortSmallStringIsNotReference)
{
    const char *shortCSmallString = "short string";
    auto shortText = SmallString::fromUtf8(shortCSmallString);

128
    ASSERT_FALSE(shortText.isReadOnlyReference());
Marco Bubke's avatar
Marco Bubke committed
129 130
}

131 132 133 134 135 136 137 138
TEST(SmallString, LongSmallStringConstrutorIsAllocated)
{
    const char *longCSmallString = "very very very very very long text";
    auto longText = SmallString(longCSmallString);

    ASSERT_TRUE(longText.hasAllocatedMemory());
}

Marco Bubke's avatar
Marco Bubke committed
139 140 141 142 143 144 145 146 147 148 149
TEST(SmallString, MaximumShortSmallString)
{
    SmallString maximumShortText("very very very very short text", 30);

    ASSERT_THAT(maximumShortText.constData(), StrEq("very very very very short text"));
}

TEST(SmallString, LongConstExpressionSmallStringIsReference)
{
    SmallString longText("very very very very very very very very very very very long string");

150
    ASSERT_TRUE(longText.isReadOnlyReference());
Marco Bubke's avatar
Marco Bubke committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
}

TEST(SmallString, CloneShortSmallString)
{
    SmallString shortText("short string");

    auto clonedText = shortText.clone();

    ASSERT_THAT(clonedText, Eq("short string"));
}

TEST(SmallString, CloneLongSmallString)
{
    SmallString longText = SmallString::fromUtf8("very very very very very very very very very very very long string");

    auto clonedText = longText.clone();

    ASSERT_THAT(clonedText, Eq("very very very very very very very very very very very long string"));
}

TEST(SmallString, ClonedLongSmallStringDataPointerIsDifferent)
{
    SmallString longText = SmallString::fromUtf8("very very very very very very very very very very very long string");

    auto clonedText = longText.clone();

    ASSERT_THAT(clonedText.data(), Ne(longText.data()));
}

TEST(SmallString, CopyShortConstExpressionSmallStringIsShortSmallString)
{
    SmallString shortText("short string");

    auto shortTextCopy = shortText;

#if __cpp_constexpr >= 201304
    ASSERT_TRUE(shortTextCopy.isShortString());
#else
189
    ASSERT_TRUE(shortTextCopy.isReadOnlyReference());
Marco Bubke's avatar
Marco Bubke committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
#endif
}

TEST(SmallString, CopyLongConstExpressionSmallStringIsLongSmallString)
{
    SmallString longText("very very very very very very very very very very very long string");

    auto longTextCopy = longText;

    ASSERT_FALSE(longTextCopy.isShortString());
}

TEST(SmallString, SmallStringFromCharacterArrayIsReference)
{
    const char longCString[] = "very very very very very very very very very very very long string";

    SmallString longString(longCString);

208
    ASSERT_TRUE(longString.isReadOnlyReference());
Marco Bubke's avatar
Marco Bubke committed
209 210 211 212 213 214 215 216
}

TEST(SmallString, SmallStringFromCharacterPointerIsNotReference)
{
    const char *longCString = "very very very very very very very very very very very long string";

    SmallString longString = SmallString::fromUtf8(longCString);

217
    ASSERT_FALSE(longString.isReadOnlyReference());
Marco Bubke's avatar
Marco Bubke committed
218 219 220 221 222 223 224 225 226
}

TEST(SmallString, CopyStringFromReference)
{
    SmallString longText("very very very very very very very very very very very long string");
    SmallString longTextCopy;

    longTextCopy = longText;

227
    ASSERT_TRUE(longTextCopy.isReadOnlyReference());
Marco Bubke's avatar
Marco Bubke committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
}

TEST(SmallString, SmallStringLiteralShortSmallStringDataAccess)
{
    SmallStringLiteral literalText("very very very very very very very very very very very long string");

    ASSERT_THAT(literalText.data(), StrEq("very very very very very very very very very very very long string"));
}

TEST(SmallString, SmallStringLiteralLongSmallStringDataAccess)
{
    SmallStringLiteral literalText("short string");

    ASSERT_THAT(literalText.data(), StrEq("short string"));
}

TEST(SmallString, ReferenceDataAccess)
{
    SmallString literalText("short string");

    ASSERT_THAT(literalText.constData(), StrEq("short string"));
}

TEST(SmallString, ShortDataAccess)
{
    const char *shortCString = "short string";
    auto shortText = SmallString::fromUtf8(shortCString);

    ASSERT_THAT(shortText.constData(), StrEq("short string"));
}

TEST(SmallString, LongDataAccess)
{
    const char *longCString = "very very very very very very very very very very very long string";
    auto longText = SmallString::fromUtf8(longCString);

    ASSERT_THAT(longText.constData(), StrEq(longCString));
}

TEST(SmallString, LongSmallStringHasShortSmallStringSizeZero)
{
    auto longText = SmallString::fromUtf8("very very very very very very very very very very very long string");

    ASSERT_THAT(longText.shortStringSize(), 0);
}

TEST(SmallString, BeginIsEqualEndForEmptySmallString)
{
    SmallString text;

    ASSERT_THAT(text.begin(), Eq(text.end()));
}

TEST(SmallString, BeginIsNotEqualEndForNonEmptySmallString)
{
    SmallString text("x");

    ASSERT_THAT(text.begin(), Ne(text.end()));
}

TEST(SmallString, BeginPlusOneIsEqualEndForSmallStringWidthSizeOne)
{
    SmallString text("x");

Marco Bubke's avatar
Marco Bubke committed
292
    auto beginPlusOne = text.begin() + std::size_t(1);
Marco Bubke's avatar
Marco Bubke committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

    ASSERT_THAT(beginPlusOne, Eq(text.end()));
}

TEST(SmallString, RBeginIsEqualREndForEmptySmallString)
{
    SmallString text;

    ASSERT_THAT(text.rbegin(), Eq(text.rend()));
}

TEST(SmallString, RBeginIsNotEqualREndForNonEmptySmallString)
{
    SmallString text("x");

    ASSERT_THAT(text.rbegin(), Ne(text.rend()));
}

TEST(SmallString, RBeginPlusOneIsEqualREndForSmallStringWidthSizeOne)
{
    SmallString text("x");

    auto beginPlusOne = text.rbegin() + 1l;

    ASSERT_THAT(beginPlusOne, Eq(text.rend()));
}

320 321 322 323 324 325 326 327 328
TEST(SmallString, ConstructorStandardString)
{
    std::string stdStringText = "short string";

    auto text = SmallString(stdStringText);

    ASSERT_THAT(text, SmallString("short string"));
}

Marco Bubke's avatar
Marco Bubke committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
TEST(SmallString, ToQString)
{
    SmallString text("short string");

    auto qStringText = text;

    ASSERT_THAT(qStringText, QStringLiteral("short string"));
}

TEST(SmallString, FromQString)
{
    QString qStringText = QStringLiteral("short string");

    auto text = SmallString::fromQString(qStringText);

    ASSERT_THAT(text, SmallString("short string"));
}


TEST(SmallString, FromQByteArray)
{
    QByteArray qByteArray = QByteArrayLiteral("short string");

    auto text = SmallString::fromQByteArray(qByteArray);

    ASSERT_THAT(text, SmallString("short string"));
}

TEST(SmallString, MidOneParameter)
{
    SmallString text("some text");

    auto midString = text.mid(5);

    ASSERT_THAT(midString, Eq(SmallString("text")));
}

TEST(SmallString, MidTwoParameter)
{
    SmallString text("some text and more");

    auto midString = text.mid(5, 4);

    ASSERT_THAT(midString, Eq(SmallString("text")));
}

TEST(SmallString, SizeOfEmptyStringl)
{
    SmallString emptyString;

    auto size = emptyString.size();

    ASSERT_THAT(size, 0);
}

TEST(SmallString, SizeShortSmallStringLiteral)
{
    SmallStringLiteral shortText("text");

    auto size = shortText.size();

    ASSERT_THAT(size, 4);
}

TEST(SmallString, SizeLongSmallStringLiteral)
{
    auto longText = SmallStringLiteral("very very very very very very very very very very very long string");

    auto size = longText.size();

    ASSERT_THAT(size, 66);
}

TEST(SmallString, SizeReference)
{
    SmallString shortText("text");

    auto size = shortText.size();

    ASSERT_THAT(size, 4);
}

TEST(SmallString, SizeShortSmallString)
{
    SmallString shortText("text", 4);

    auto size = shortText.size();

    ASSERT_THAT(size, 4);
}

TEST(SmallString, SizeLongSmallString)
{
    auto longText = SmallString::fromUtf8("very very very very very very very very very very very long string");

    auto size = longText.size();

    ASSERT_THAT(size, 66);
}

TEST(SmallString, CapacityReference)
{
    SmallString shortText("very very very very very very very long string");

    auto capacity = shortText.capacity();

    ASSERT_THAT(capacity, 0);
}

TEST(SmallString, CapacityShortSmallString)
{
    SmallString shortText("text", 4);

    auto capacity = shortText.capacity();

    ASSERT_THAT(capacity, 30);
}

TEST(SmallString, CapacityLongSmallString)
{
    auto longText = SmallString::fromUtf8("very very very very very very very very very very very long string");

    auto capacity = longText.capacity();

    ASSERT_THAT(capacity, 66);
}

TEST(SmallString, FitsNotInCapacityBecauseNullSmallStringIsAShortSmallString)
{
    SmallString text;

    ASSERT_FALSE(text.fitsNotInCapacity(30));
}

TEST(SmallString, FitsNotInCapacityBecauseItIsReference)
{
    SmallString text("very very very very very very very long string");

    ASSERT_TRUE(text.fitsNotInCapacity(1));
}

TEST(SmallString, FitsInShortSmallStringCapacity)
{
    SmallString text("text", 4);

    ASSERT_FALSE(text.fitsNotInCapacity(30));
}

TEST(SmallString, FitsInNotShortSmallStringCapacity)
{
    SmallString text("text", 4);

    ASSERT_TRUE(text.fitsNotInCapacity(31));
}

TEST(SmallString, FitsInLongSmallStringCapacity)
{
    SmallString text = SmallString::fromUtf8("very very very very very very long string");

    ASSERT_FALSE(text.fitsNotInCapacity(33)) << text.capacity();
}

TEST(SmallString, FitsNotInLongSmallStringCapacity)
{
    SmallString text = SmallString::fromUtf8("very very very very very very long string");

    ASSERT_TRUE(text.fitsNotInCapacity(65)) << text.capacity();
}

TEST(SmallString, AppendNullSmallString)
{
    SmallString text("text");

    text.append(SmallString());

    ASSERT_THAT(text, SmallString("text"));
}

TEST(SmallString, AppendEmptySmallString)
{
    SmallString text("text");

    text.append(SmallString(""));

    ASSERT_THAT(text, SmallString("text"));
}


TEST(SmallString, AppendShortSmallString)
{
    SmallString text("some ");

    text.append(SmallString("text"));

    ASSERT_THAT(text, SmallString("some text"));
}

TEST(SmallString, AppendLongSmallString)
{
    SmallString longText("some very very very very very very very very very very very long string");
    longText.append(SmallString(" text"));

    ASSERT_THAT(longText, SmallString("some very very very very very very very very very very very long string text"));
}

TEST(SmallString, ToByteArray)
{
    SmallString text("some text");

    ASSERT_THAT(text.toQByteArray(), QByteArrayLiteral("some text"));
}

TEST(SmallString, Contains)
{
    SmallString text("some text");

    ASSERT_TRUE(text.contains(SmallString("text")));
    ASSERT_TRUE(text.contains("text"));
    ASSERT_TRUE(text.contains('x'));
}

TEST(SmallString, NotContains)
{
    SmallString text("some text");

    ASSERT_FALSE(text.contains(SmallString("textTwo")));
    ASSERT_FALSE(text.contains("foo"));
    ASSERT_FALSE(text.contains('q'));
}

TEST(SmallString, EqualSmallStringOperator)
{
    ASSERT_TRUE(SmallString() == SmallString(""));
    ASSERT_FALSE(SmallString() == SmallString("text"));
    ASSERT_TRUE(SmallString("text") == SmallString("text"));
    ASSERT_FALSE(SmallString("text") == SmallString("text2"));
}

TEST(SmallString, EqualCStringArrayOperator)
{
    ASSERT_TRUE(SmallString() == "");
    ASSERT_FALSE(SmallString() == "text");
    ASSERT_TRUE(SmallString("text") == "text");
    ASSERT_FALSE(SmallString("text") == "text2");
}

TEST(SmallString, EqualCStringPointerOperator)
{
    ASSERT_TRUE(SmallString("text") == SmallString("text").data());
    ASSERT_FALSE(SmallString("text") == SmallString("text2").data());
}

TEST(SmallString, EqualSmallStringViewOperator)
{
    ASSERT_TRUE(SmallString("text") == SmallStringView("text"));
    ASSERT_FALSE(SmallString("text") == SmallStringView("text2"));
}

TEST(SmallString, EqualSmallStringViewsOperator)
{
    ASSERT_TRUE(SmallStringView("text") == SmallStringView("text"));
    ASSERT_FALSE(SmallStringView("text") == SmallStringView("text2"));
}

TEST(SmallString, UnequalOperator)
{
    ASSERT_FALSE(SmallString("text") != SmallString("text"));
    ASSERT_TRUE(SmallString("text") != SmallString("text2"));
}

TEST(SmallString, UnequalCStringArrayOperator)
{
    ASSERT_FALSE(SmallString("text") != "text");
    ASSERT_TRUE(SmallString("text") != "text2");
}

TEST(SmallString, UnequalCStringPointerOperator)
{
    ASSERT_FALSE(SmallString("text") != SmallString("text").data());
    ASSERT_TRUE(SmallString("text") != SmallString("text2").data());
}

TEST(SmallString, UnequalSmallStringViewArrayOperator)
{
    ASSERT_FALSE(SmallString("text") != SmallStringView("text"));
    ASSERT_TRUE(SmallString("text") != SmallStringView("text2"));
}

TEST(SmallString, UnequalSmallStringViewsArrayOperator)
{
    ASSERT_FALSE(SmallStringView("text") != SmallStringView("text"));
    ASSERT_TRUE(SmallStringView("text") != SmallStringView("text2"));
}

TEST(SmallString, SmallerOperator)
{
    ASSERT_TRUE(SmallString() < SmallString("text"));
    ASSERT_TRUE(SmallString("some") < SmallString("text"));
    ASSERT_TRUE(SmallString("text") < SmallString("texta"));
    ASSERT_FALSE(SmallString("texta") < SmallString("text"));
    ASSERT_FALSE(SmallString("text") < SmallString("some"));
    ASSERT_FALSE(SmallString("text") < SmallString("text"));
}

TEST(SmallString, IsEmpty)
{
    ASSERT_FALSE(SmallString("text").isEmpty());
    ASSERT_TRUE(SmallString("").isEmpty());
    ASSERT_TRUE(SmallString().isEmpty());
}

TEST(SmallString, HasContent)
{
    ASSERT_TRUE(SmallString("text").hasContent());
    ASSERT_FALSE(SmallString("").hasContent());
    ASSERT_FALSE(SmallString().hasContent());
}

TEST(SmallString, Clear)
{
    SmallString text("text");

    text.clear();

    ASSERT_TRUE(text.isEmpty());
}

TEST(SmallString, ReplaceWithEqualSizedText)
{
    SmallString text("here is some text");

    text.replace("some", "much");

    ASSERT_THAT(text, SmallString("here is much text"));
}

TEST(SmallString, ReplaceWithEqualSizedTextOnEmptyText)
{
    SmallString text;

    text.replace("some", "much");

    ASSERT_THAT(text, SmallString());
}

TEST(SmallString, ReplaceWithShorterText)
{
    SmallString text("here is some text");

    text.replace("some", "any");

    ASSERT_THAT(text, SmallString("here is any text"));
}

TEST(SmallString, ReplaceWithShorterTextOnEmptyText)
{
    SmallString text;

    text.replace("some", "any");

    ASSERT_THAT(text, SmallString());
}

TEST(SmallString, ReplaceWithLongerText)
{
    SmallString text("here is some text");

    text.replace("some", "much more");

    ASSERT_THAT(text, SmallString("here is much more text"));
}

TEST(SmallString, ReplaceWithLongerTextOnEmptyText)
{
    SmallString text;

    text.replace("some", "much more");

    ASSERT_THAT(text, SmallString());
}

TEST(SmallString, ReplaceShortSmallStringWithLongerText)
{
    SmallString text = SmallString::fromUtf8("here is some text");

    text.replace("some", "much more");

    ASSERT_THAT(text, SmallString("here is much more text"));
}

TEST(SmallString, ReplaceLongSmallStringWithLongerText)
{
    SmallString text = SmallString::fromUtf8("some very very very very very very very very very very very long string");

    text.replace("some", "much more");

    ASSERT_THAT(text, Eq(SmallString("much more very very very very very very very very very very very long string")));
}

TEST(SmallString, MultipleReplaceSmallStringWithLongerText)
{
    SmallString text = SmallString("here is some text with some longer text");

    text.replace("some", "much more");

    ASSERT_THAT(text, SmallString("here is much more text with much more longer text"));
}

TEST(SmallString, MultipleReplaceSmallStringWithShorterText)
{
    SmallString text = SmallString("here is some text with some longer text");

    text.replace("some", "a");

    ASSERT_THAT(text, SmallString("here is a text with a longer text"));
}

TEST(SmallString, DontReplaceReplacedText)
{
    SmallString text("here is some foo text");

    text.replace("foo", "foofoo");

    ASSERT_THAT(text, SmallString("here is some foofoo text"));
}


TEST(SmallString, StartsWith)
{
    SmallString text("$column");

    ASSERT_TRUE(text.startsWith("$column"));
    ASSERT_TRUE(text.startsWith("$col"));
    ASSERT_FALSE(text.startsWith("col"));
    ASSERT_TRUE(text.startsWith('$'));
    ASSERT_FALSE(text.startsWith('@'));
}

TEST(SmallString, EndsWith)
{
    SmallString text("/my/path");

    ASSERT_TRUE(text.endsWith("/my/path"));
    ASSERT_TRUE(text.endsWith("path"));
    ASSERT_FALSE(text.endsWith("paths"));
    ASSERT_TRUE(text.endsWith('h'));
    ASSERT_FALSE(text.endsWith('x'));
}

TEST(SmallString, EndsWithSmallString)
{
    SmallString text("/my/path");

    ASSERT_TRUE(text.endsWith(SmallString("path")));
    ASSERT_TRUE(text.endsWith('h'));
}

TEST(SmallString, ReserveSmallerThanReference)
{
    SmallString text("text");

    text.reserve(2);

    ASSERT_THAT(text.capacity(), 30);
}

TEST(SmallString, ReserveBiggerThanReference)
{
    SmallString text("text");

    text.reserve(10);

    ASSERT_THAT(text.capacity(), 30);
}

TEST(SmallString, ReserveMuchBiggerThanReference)
{
    SmallString text("text");

    text.reserve(100);

    ASSERT_THAT(text.capacity(), 100);
}

TEST(SmallString, ReserveSmallerThanShortSmallString)
{
    SmallString text = SmallString::fromUtf8("text");

    text.reserve(10);

    ASSERT_THAT(text.capacity(), 30);
}

TEST(SmallString, ReserveBiggerThanShortSmallString)
{
    SmallString text = SmallString::fromUtf8("text");

    text.reserve(100);

    ASSERT_THAT(text.capacity(), 100);
}

TEST(SmallString, ReserveBiggerThanLongSmallString)
{
    SmallString text = SmallString::fromUtf8("some very very very very very very very very very very very long string");

    text.reserve(100);

    ASSERT_THAT(text.capacity(), 100);
}

TEST(SmallString, OptimalHeapCacheLineForSize)
{
    ASSERT_THAT(SmallString::optimalHeapCapacity(64), 64);
    ASSERT_THAT(SmallString::optimalHeapCapacity(65), 128);
    ASSERT_THAT(SmallString::optimalHeapCapacity(127), 128);
    ASSERT_THAT(SmallString::optimalHeapCapacity(128), 128);
    ASSERT_THAT(SmallString::optimalHeapCapacity(129), 192);
    ASSERT_THAT(SmallString::optimalHeapCapacity(191), 192);
    ASSERT_THAT(SmallString::optimalHeapCapacity(193), 256);
    ASSERT_THAT(SmallString::optimalHeapCapacity(255), 256);
    ASSERT_THAT(SmallString::optimalHeapCapacity(257), 384);
    ASSERT_THAT(SmallString::optimalHeapCapacity(257), 384);
    ASSERT_THAT(SmallString::optimalHeapCapacity(383), 384);
    ASSERT_THAT(SmallString::optimalHeapCapacity(385), 512);
    ASSERT_THAT(SmallString::optimalHeapCapacity(4095), 4096);
    ASSERT_THAT(SmallString::optimalHeapCapacity(4096), 4096);
    ASSERT_THAT(SmallString::optimalHeapCapacity(4097), 6144);
}

TEST(SmallString, OptimalCapacityForSize)
{
    SmallString text;

    ASSERT_THAT(text.optimalCapacity(0), 0);
    ASSERT_THAT(text.optimalCapacity(30), 30);
    ASSERT_THAT(text.optimalCapacity(31), 63);
    ASSERT_THAT(text.optimalCapacity(63), 63);
    ASSERT_THAT(text.optimalCapacity(64), 127);
    ASSERT_THAT(text.optimalCapacity(128), 191);
}

TEST(SmallString, DataStreamData)
{
    SmallString inputText("foo");
    QByteArray byteArray;
    QDataStream out(&byteArray, QIODevice::ReadWrite);

    out << inputText;

    ASSERT_TRUE(byteArray.endsWith("foo"));
}

TEST(SmallString, ReadDataStreamSize)
{
    SmallString outputText("foo");
    QByteArray byteArray;
    quint32 size;
    {
        QDataStream out(&byteArray, QIODevice::WriteOnly);
        out << outputText;
    }
    QDataStream in(&byteArray, QIODevice::ReadOnly);

    in >> size;

    ASSERT_THAT(size, 3);
}

TEST(SmallString, ReadDataStreamData)
{
    SmallString outputText("foo");
    QByteArray byteArray;
    SmallString outputString;
    {
        QDataStream out(&byteArray, QIODevice::WriteOnly);
        out << outputText;
    }
    QDataStream in(&byteArray, QIODevice::ReadOnly);

    in >> outputString;

    ASSERT_THAT(outputString, SmallString("foo"));
}

TEST(SmallString, ShortSmallStringCopyConstuctor)
{
    SmallString text("text");

    auto copy = text;

    ASSERT_THAT(copy, text);
}

TEST(SmallString, LongSmallStringCopyConstuctor)
{
    SmallString text("this is a very very very very long text");

    auto copy = text;

    ASSERT_THAT(copy, text);
}

TEST(SmallString, ShortSmallStringMoveConstuctor)
{
    SmallString text("text");

    auto copy = std::move(text);

    ASSERT_TRUE(text.isEmpty());
    ASSERT_THAT(copy, SmallString("text"));
}

TEST(SmallString, LongSmallStringMoveConstuctor)
{
    SmallString text("this is a very very very very long text");

    auto copy = std::move(text);

    ASSERT_TRUE(text.isEmpty());
    ASSERT_THAT(copy, SmallString("this is a very very very very long text"));
}

TEST(SmallString, ShortSmallStringCopyAssignment)
{
    SmallString text("text");
    SmallString copy("more text");

    copy = text;

    ASSERT_THAT(copy, text);
}

TEST(SmallString, LongSmallStringCopyAssignment)
{
    SmallString text("this is a very very very very long text");
    SmallString copy("more text");

    copy = text;

    ASSERT_THAT(copy, text);
}

TEST(SmallString, LongSmallStringCopySelfAssignment)
{
    SmallString text("this is a very very very very long text");

    text = text;

    ASSERT_THAT(text, SmallString("this is a very very very very long text"));
}

TEST(SmallString, ShortSmallStringMoveAssignment)
{
    SmallString text("text");
    SmallString copy("more text");

    copy = std::move(text);

    ASSERT_THAT(text, SmallString("more text"));
    ASSERT_THAT(copy, SmallString("text"));
}

TEST(SmallString, LongSmallStringMoveAssignment)
{
    SmallString text("this is a very very very very long text");
    SmallString copy("more text");

    copy = std::move(text);

    ASSERT_THAT(text, SmallString("more text"));
    ASSERT_THAT(copy, SmallString("this is a very very very very long text"));
}

TEST(SmallString, ReplaceByPositionShorterWithLongerText)
{
    SmallString text("this is a very very very very long text");

    text.replace(8, 1, "some");

    ASSERT_THAT(text, SmallString("this is some very very very very long text"));
}

TEST(SmallString, ReplaceByPositionLongerWithShortText)
{
    SmallString text("this is some very very very very long text");

    text.replace(8, 4, "a");

    ASSERT_THAT(text, SmallString("this is a very very very very long text"));
}

TEST(SmallString, ReplaceByPositionEqualSizedTexts)
{
    SmallString text("this is very very very very very long text");

    text.replace(33, 4, "much");

    ASSERT_THAT(text, SmallString("this is very very very very very much text"));
}