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 26 27 28 29 30 31 32 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 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 292 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 320 321 322 323 324 325 326 327 328 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
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2015, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/* file name: strtest.cpp
* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* created on: 1999nov22
* created by: Markus W. Scherer
*/
#include <string_view>
#include <cstddef>
#include <string.h>
#include <limits>
#include <type_traits>
#include <utility>
#include "unicode/utypes.h"
#include "unicode/putil.h"
#include "unicode/std_string.h"
#include "unicode/stringpiece.h"
#include "unicode/unistr.h"
#include "unicode/ustring.h"
#include "unicode/utf_old.h" // for UTF8_COUNT_TRAIL_BYTES
#include "unicode/utf8.h"
#include "charstr.h"
#include "cstr.h"
#include "fixedstring.h"
#include "intltest.h"
#include "strtest.h"
#include "uinvchar.h"
StringTest::~StringTest() {}
void StringTest::TestEndian() {
union {
uint8_t byte;
uint16_t word;
} u;
u.word=0x0100;
if(U_IS_BIG_ENDIAN!=u.byte) {
errln("TestEndian: U_IS_BIG_ENDIAN needs to be fixed in platform.h");
}
}
void StringTest::TestSizeofTypes() {
if(U_SIZEOF_WCHAR_T!=sizeof(wchar_t)) {
errln("TestSizeofWCharT: U_SIZEOF_WCHAR_T!=sizeof(wchar_t) - U_SIZEOF_WCHAR_T needs to be fixed in platform.h");
}
#ifdef U_INT64_T_UNAVAILABLE
errln("int64_t and uint64_t are undefined.");
#else
if(8!=sizeof(int64_t)) {
errln("TestSizeofTypes: 8!=sizeof(int64_t) - int64_t needs to be fixed in platform.h");
}
if(8!=sizeof(uint64_t)) {
errln("TestSizeofTypes: 8!=sizeof(uint64_t) - uint64_t needs to be fixed in platform.h");
}
#endif
if(8!=sizeof(double)) {
errln("8!=sizeof(double) - putil.c code may not work");
}
if(4!=sizeof(int32_t)) {
errln("4!=sizeof(int32_t)");
}
if(4!=sizeof(uint32_t)) {
errln("4!=sizeof(uint32_t)");
}
if(2!=sizeof(int16_t)) {
errln("2!=sizeof(int16_t)");
}
if(2!=sizeof(uint16_t)) {
errln("2!=sizeof(uint16_t)");
}
if(2!=sizeof(char16_t)) {
errln("2!=sizeof(char16_t)");
}
if(1!=sizeof(int8_t)) {
errln("1!=sizeof(int8_t)");
}
if(1!=sizeof(uint8_t)) {
errln("1!=sizeof(uint8_t)");
}
if(1!=sizeof(UBool)) {
errln("1!=sizeof(UBool)");
}
}
void StringTest::TestCharsetFamily() {
unsigned char c='A';
if( (U_CHARSET_FAMILY==U_ASCII_FAMILY && c!=0x41) ||
(U_CHARSET_FAMILY==U_EBCDIC_FAMILY && c!=0xc1)
) {
errln("TestCharsetFamily: U_CHARSET_FAMILY needs to be fixed in platform.h");
}
}
U_STRING_DECL(ustringVar, "aZ0 -", 5);
void
StringTest::Test_U_STRING() {
U_STRING_INIT(ustringVar, "aZ0 -", 5);
if( u_strlen(ustringVar)!=5 ||
ustringVar[0]!=0x61 ||
ustringVar[1]!=0x5a ||
ustringVar[2]!=0x30 ||
ustringVar[3]!=0x20 ||
ustringVar[4]!=0x2d ||
ustringVar[5]!=0
) {
errln("Test_U_STRING: U_STRING_DECL with U_STRING_INIT does not work right! "
"See putil.h and utypes.h with platform.h.");
}
}
void
StringTest::Test_UNICODE_STRING() {
UnicodeString ustringVar=UNICODE_STRING("aZ0 -", 5);
if( ustringVar.length()!=5 ||
ustringVar[0]!=0x61 ||
ustringVar[1]!=0x5a ||
ustringVar[2]!=0x30 ||
ustringVar[3]!=0x20 ||
ustringVar[4]!=0x2d
) {
errln("Test_UNICODE_STRING: UNICODE_STRING does not work right! "
"See unistr.h and utypes.h with platform.h.");
}
}
void
StringTest::Test_UNICODE_STRING_SIMPLE() {
UnicodeString ustringVar=UNICODE_STRING_SIMPLE("aZ0 -");
if( ustringVar.length()!=5 ||
ustringVar[0]!=0x61 ||
ustringVar[1]!=0x5a ||
ustringVar[2]!=0x30 ||
ustringVar[3]!=0x20 ||
ustringVar[4]!=0x2d
) {
errln("Test_UNICODE_STRING_SIMPLE: UNICODE_STRING_SIMPLE does not work right! "
"See unistr.h and utypes.h with platform.h.");
}
}
namespace {
// See U_CHARSET_FAMILY in unicode/platform.h.
const char *nativeInvChars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789 \"%&'()*+,-./:;<=>?_";
const char16_t *asciiInvChars =
u"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
u"abcdefghijklmnopqrstuvwxyz"
u"0123456789 \"%&'()*+,-./:;<=>?_";
} // namespace
void
StringTest::TestUpperOrdinal() {
for (int32_t i = 0;; ++i) {
char ic = nativeInvChars[i];
uint8_t ac = static_cast<uint8_t>(asciiInvChars[i]);
int32_t expected = ac - 'A';
int32_t actual = uprv_upperOrdinal(ic);
if (0 <= expected && expected <= 25) {
if (actual != expected) {
errln("uprv_upperOrdinal('%c')=%d != expected %d",
ic, static_cast<int>(actual), static_cast<int>(expected));
}
} else {
if (0 <= actual && actual <= 25) {
errln("uprv_upperOrdinal('%c')=%d should have been outside 0..25",
ic, static_cast<int>(actual));
}
}
if (ic == 0) { break; }
}
}
void
StringTest::TestLowerOrdinal() {
for (int32_t i = 0;; ++i) {
char ic = nativeInvChars[i];
uint8_t ac = static_cast<uint8_t>(asciiInvChars[i]);
int32_t expected = ac - 'a';
int32_t actual = uprv_lowerOrdinal(ic);
if (0 <= expected && expected <= 25) {
if (actual != expected) {
errln("uprv_lowerOrdinal('%c')=%d != expected %d",
ic, static_cast<int>(actual), static_cast<int>(expected));
}
} else {
if (0 <= actual && actual <= 25) {
errln("uprv_lowerOrdinal('%c')=%d should have been outside 0..25",
ic, static_cast<int>(actual));
}
}
if (ic == 0) { break; }
}
}
void
StringTest::Test_UTF8_COUNT_TRAIL_BYTES() {
#if !U_HIDE_OBSOLETE_UTF_OLD_H
if(UTF8_COUNT_TRAIL_BYTES(0x7F) != 0
|| UTF8_COUNT_TRAIL_BYTES(0xC2) != 1
|| UTF8_COUNT_TRAIL_BYTES(0xE0) != 2
|| UTF8_COUNT_TRAIL_BYTES(0xF0) != 3) {
errln("UTF8_COUNT_TRAIL_BYTES does not work right! See utf_old.h.");
}
#endif
// Note: U8_COUNT_TRAIL_BYTES (current) and UTF8_COUNT_TRAIL_BYTES (deprecated)
// have completely different implementations.
if (U8_COUNT_TRAIL_BYTES(0x7F) != 0
|| U8_COUNT_TRAIL_BYTES(0xC2) != 1
|| U8_COUNT_TRAIL_BYTES(0xE0) != 2
|| U8_COUNT_TRAIL_BYTES(0xF0) != 3) {
errln("U8_COUNT_TRAIL_BYTES does not work right! See utf8.h.");
}
}
void StringTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
if(exec) {
logln("TestSuite Character and String Test: ");
}
TESTCASE_AUTO_BEGIN;
TESTCASE_AUTO(TestEndian);
TESTCASE_AUTO(TestSizeofTypes);
TESTCASE_AUTO(TestCharsetFamily);
TESTCASE_AUTO(Test_U_STRING);
TESTCASE_AUTO(Test_UNICODE_STRING);
TESTCASE_AUTO(Test_UNICODE_STRING_SIMPLE);
TESTCASE_AUTO(TestUpperOrdinal);
TESTCASE_AUTO(TestLowerOrdinal);
TESTCASE_AUTO(Test_UTF8_COUNT_TRAIL_BYTES);
TESTCASE_AUTO(TestSTLCompatibility);
TESTCASE_AUTO(TestStringPiece);
TESTCASE_AUTO(TestStringPieceComparisons);
TESTCASE_AUTO(TestStringPieceFind);
TESTCASE_AUTO(TestStringPieceOther);
TESTCASE_AUTO(TestStringPieceStringView);
TESTCASE_AUTO(TestStringPieceU8);
TESTCASE_AUTO(TestByteSink);
TESTCASE_AUTO(TestCheckedArrayByteSink);
TESTCASE_AUTO(TestStringByteSink);
TESTCASE_AUTO(TestStringByteSinkAppendU8);
TESTCASE_AUTO(TestCharString);
TESTCASE_AUTO(TestCStr);
TESTCASE_AUTO(TestCharStrAppendNumber);
TESTCASE_AUTO(Testctou);
TESTCASE_AUTO(TestFixedString);
TESTCASE_AUTO(TestCopyInvariantChars);
TESTCASE_AUTO_END;
}
void
StringTest::TestStringPiece() {
// Default constructor.
StringPiece empty;
if(!empty.empty() || empty.data()!=nullptr || empty.length()!=0 || empty.size()!=0) {
errln("StringPiece() failed");
}
// Construct from nullptr const char * pointer.
StringPiece null((const char *)nullptr);
if(!null.empty() || null.data()!=nullptr || null.length()!=0 || null.size()!=0) {
errln("StringPiece(nullptr) failed");
}
// Construct from const char * pointer.
static const char *abc_chars="abc";
StringPiece abc(abc_chars);
if(abc.empty() || abc.data()!=abc_chars || abc.length()!=3 || abc.size()!=3) {
errln("StringPiece(abc_chars) failed");
}
// Construct from const char * pointer and length.
static const char *abcdefg_chars="abcdefg";
StringPiece abcd(abcdefg_chars, 4);
if(abcd.empty() || abcd.data()!=abcdefg_chars || abcd.length()!=4 || abcd.size()!=4) {
errln("StringPiece(abcdefg_chars, 4) failed");
}
// Construct from std::string.
std::string uvwxyz_string("uvwxyz");
StringPiece uvwxyz(uvwxyz_string);
if(uvwxyz.empty() || uvwxyz.data()!=uvwxyz_string.data() || uvwxyz.length()!=6 || uvwxyz.size()!=6) {
errln("StringPiece(uvwxyz_string) failed");
}
// Substring constructor with pos.
StringPiece sp(abcd, -1);
if(sp.empty() || sp.data()!=abcdefg_chars || sp.length()!=4 || sp.size()!=4) {
errln("StringPiece(abcd, -1) failed");
}
sp=StringPiece(abcd, 5);
if(!sp.empty() || sp.length()!=0 || sp.size()!=0) {
errln("StringPiece(abcd, 5) failed");
}
sp=StringPiece(abcd, 2);
if(sp.empty() || sp.data()!=abcdefg_chars+2 || sp.length()!=2 || sp.size()!=2) {
errln("StringPiece(abcd, -1) failed");
}
// Substring constructor with pos and len.
sp=StringPiece(abcd, -1, 8);
if(sp.empty() || sp.data()!=abcdefg_chars || sp.length()!=4 || sp.size()!=4) {
errln("StringPiece(abcd, -1, 8) failed");
}
sp=StringPiece(abcd, 5, 8);
if(!sp.empty() || sp.length()!=0 || sp.size()!=0) {
errln("StringPiece(abcd, 5, 8) failed");
}
sp=StringPiece(abcd, 2, 8);
if(sp.empty() || sp.data()!=abcdefg_chars+2 || sp.length()!=2 || sp.size()!=2) {
errln("StringPiece(abcd, -1) failed");
}
sp=StringPiece(abcd, 2, -1);
if(!sp.empty() || sp.length()!=0 || sp.size()!=0) {
errln("StringPiece(abcd, 5, -1) failed");
}
// static const npos
const int32_t *ptr_npos=&StringPiece::npos;
if(StringPiece::npos!=0x7fffffff || *ptr_npos!=0x7fffffff) {
errln("StringPiece::npos!=0x7fffffff");
}
// substr() method with pos, using len=npos.
sp=abcd.substr(-1);
if(sp.empty() || sp.data()!=abcdefg_chars || sp.length()!=4 || sp.size()!=4) {
errln("abcd.substr(-1) failed");
}
sp=abcd.substr(5);
if(!sp.empty() || sp.length()!=0 || sp.size()!=0) {
errln("abcd.substr(5) failed");
}
sp=abcd.substr(2);
if(sp.empty() || sp.data()!=abcdefg_chars+2 || sp.length()!=2 || sp.size()!=2) {
errln("abcd.substr(-1) failed");
}
// substr() method with pos and len.
sp=abcd.substr(-1, 8);
if(sp.empty() || sp.data()!=abcdefg_chars || sp.length()!=4 || sp.size()!=4) {
errln("abcd.substr(-1, 8) failed");
}
sp=abcd.substr(5, 8);
if(!sp.empty() || sp.length()!=0 || sp.size()!=0) {
errln("abcd.substr(5, 8) failed");
}
sp=abcd.substr(2, 8);
if(sp.empty() || sp.data()!=abcdefg_chars+2 || sp.length()!=2 || sp.size()!=2) {
errln("abcd.substr(-1) failed");
}
sp=abcd.substr(2, -1);
if(!sp.empty() || sp.length()!=0 || sp.size()!=0) {
errln("abcd.substr(5, -1) failed");
}
// clear()
sp=abcd;
sp.clear();
if(!sp.empty() || sp.data()!=nullptr || sp.length()!=0 || sp.size()!=0) {
errln("abcd.clear() failed");
}
// remove_prefix()
sp=abcd;
sp.remove_prefix(-1);
if(sp.empty() || sp.data()!=abcdefg_chars || sp.length()!=4 || sp.size()!=4) {
errln("abcd.remove_prefix(-1) failed");
}
sp=abcd;
sp.remove_prefix(2);
if(sp.empty() || sp.data()!=abcdefg_chars+2 || sp.length()!=2 || sp.size()!=2) {
errln("abcd.remove_prefix(2) failed");
}
sp=abcd;
sp.remove_prefix(5);
if(!sp.empty() || sp.length()!=0 || sp.size()!=0) {
errln("abcd.remove_prefix(5) failed");
}
// remove_suffix()
sp=abcd;
sp.remove_suffix(-1);
if(sp.empty() || sp.data()!=abcdefg_chars || sp.length()!=4 || sp.size()!=4) {
errln("abcd.remove_suffix(-1) failed");
}
sp=abcd;
sp.remove_suffix(2);
if(sp.empty() || sp.data()!=abcdefg_chars || sp.length()!=2 || sp.size()!=2) {
errln("abcd.remove_suffix(2) failed");
}
sp=abcd;
sp.remove_suffix(5);
if(!sp.empty() || sp.length()!=0 || sp.size()!=0) {
errln("abcd.remove_suffix(5) failed");
}
}
void
StringTest::TestStringPieceComparisons() {
StringPiece empty;
StringPiece null(nullptr);
StringPiece abc("abc");
StringPiece abcd("abcdefg", 4);
StringPiece abx("abx");
if(empty!=null) {
errln("empty!=null");
}
if(empty==abc) {
errln("empty==abc");
}
if(abc==abcd) {
errln("abc==abcd");
}
assertTrue("null<abc", null.compare(abc) < 0);
assertTrue("abc>null", abc.compare(null) > 0);
assertTrue("abc<abcd", abc.compare(abcd) < 0);
assertTrue("abcd>abc", abcd.compare(abc) > 0);
assertTrue("abc<abx", abc.compare(abx) < 0);
assertTrue("abx>abc", abx.compare(abc) > 0);
assertTrue("abx>abcd", abx.compare(abcd) > 0);
assertTrue("abcd<abx", abcd.compare(abx) < 0);
assertTrue("abx==abx", abx.compare(abx) == 0);
// Behavior should be the same as std::string::compare
{
std::string null("");
std::string abc("abc");
std::string abcd("abcdefg", 4);
std::string abx("abx");
assertTrue("std: null<abc", null.compare(abc) < 0);
assertTrue("std: abc>null", abc.compare(null) > 0);
assertTrue("std: abc<abcd", abc.compare(abcd) < 0);
assertTrue("std: abcd>abc", abcd.compare(abc) > 0);
assertTrue("std: abc<abx", abc.compare(abx) < 0);
assertTrue("std: abx>abc", abx.compare(abc) > 0);
assertTrue("std: abx>abcd", abx.compare(abcd) > 0);
assertTrue("std: abcd<abx", abcd.compare(abx) < 0);
assertTrue("std: abx==abx", abx.compare(abx) == 0);
}
abcd.remove_suffix(1);
if(abc!=abcd) {
errln("abc!=abcd.remove_suffix(1)");
}
if(abc==abx) {
errln("abc==abx");
}
}
void
StringTest::TestStringPieceFind() {
struct TestCase {
const char* haystack;
const char* needle;
int32_t expected;
} cases[] = {
{ "", "", 0 },
{ "", "x", -1 },
{ "x", "", 0 },
{ "x", "x", 0 },
{ "xy", "x", 0 },
{ "xy", "y", 1 },
{ "xy", "xy", 0 },
{ "xy", "xyz", -1 },
{ "qwerty", "qqw", -1 },
{ "qwerty", "qw", 0 },
{ "qwerty", "er", 2 },
{ "qwerty", "err", -1 },
{ "qwerty", "ert", 2 },
{ "qwerty", "ty", 4 },
{ "qwerty", "tyy", -1 },
{ "qwerty", "a", -1 },
{ "qwerty", "abc", -1 }
};
int32_t caseNumber = 0;
for (auto& cas : cases) {
StringPiece haystack(cas.haystack);
StringPiece needle(cas.needle);
assertEquals(Int64ToUnicodeString(caseNumber),
cas.expected, haystack.find(needle, 0));
// Should be same as std::string::find
std::string stdhaystack(cas.haystack);
std::string stdneedle(cas.needle);
assertEquals(Int64ToUnicodeString(caseNumber) + u" (std)",
cas.expected, static_cast<int32_t>(stdhaystack.find(stdneedle, 0)));
// Test offsets against std::string::find
for (int32_t offset = 0; offset < haystack.length(); offset++) {
assertEquals(Int64ToUnicodeString(caseNumber) + "u @ " + Int64ToUnicodeString(offset),
static_cast<int32_t>(stdhaystack.find(stdneedle, offset)), haystack.find(needle, offset));
}
caseNumber++;
}
}
void
StringTest::TestStringPieceOther() {
static constexpr char msg[] = "Kapow!";
// Another string piece implementation.
struct Other {
const char* data() { return msg; }
size_t size() { return sizeof msg - 1; }
};
Other other;
StringPiece piece(other);
assertEquals("size()", piece.size(), other.size());
assertEquals("data()", piece.data(), other.data());
}
void
StringTest::TestStringPieceStringView() {
static constexpr char msg[] = "Kapow!";
std::string_view view(msg); // C++17
StringPiece piece(view);
assertEquals("size()", piece.size(), view.size());
assertEquals("data()", piece.data(), view.data());
std::string_view v2 = piece; // Internal implicit conversion.
assertEquals("size()", piece.size(), v2.size());
assertEquals("data()", piece.data(), v2.data());
}
void
StringTest::TestStringPieceU8() {
// ICU-20984 "mitigate some C++20 char8_t breakages"
// For the following APIs there are overloads for both
// const char * and const char8_t *.
// A u8"string literal" has one type or the other
// depending on C++ version and compiler settings.
StringPiece abc(u8"abc");
assertEquals("abc.length", 3, abc.length());
assertEquals("abc", "\x61\x62\x63", abc.data());
StringPiece abc3(u8"abcdef", 3);
assertEquals("abc3.length", 3, abc3.length());
assertEquals("abc3[0]", 0x61, abc3.data()[0]);
assertEquals("abc3[1]", 0x62, abc3.data()[1]);
assertEquals("abc3[2]", 0x63, abc3.data()[2]);
StringPiece uvw("q");
uvw.set(u8"uvw");
assertEquals("uvw.length", 3, uvw.length());
assertEquals("uvw", "\x75\x76\x77", uvw.data());
StringPiece xyz("r");
xyz.set(u8"xyzXYZ", 3);
assertEquals("xyz.length", 3, xyz.length());
assertEquals("xyz[0]", 0x78, xyz.data()[0]);
assertEquals("xyz[1]", 0x79, xyz.data()[1]);
assertEquals("xyz[2]", 0x7a, xyz.data()[2]);
StringPiece null(nullptr);
assertTrue("null is empty", null.empty());
assertTrue("null is null", null.data() == nullptr);
#ifdef __cpp_lib_char8_t
std::u8string_view u8sv(u8"sv"); // C++20
StringPiece u8svsp(u8sv);
assertEquals("u8svsp.length", 2, u8svsp.length());
assertEquals("u8svsp", "\x73\x76", u8svsp.data());
std::u8string u8str(u8"str"); // C++20
StringPiece u8strsp(u8str);
assertEquals("u8strsp.length", 3, u8strsp.length());
assertEquals("u8strsp", "\x73\x74\x72", u8strsp.data());
#endif // __cpp_lib_char8_t
}
// Verify that ByteSink is subclassable and Flush() overridable.
class SimpleByteSink : public ByteSink {
public:
SimpleByteSink(char *outbuf) : fOutbuf(outbuf), fLength(0) {}
virtual void Append(const char *bytes, int32_t n) override {
if(fOutbuf != bytes) {
memcpy(fOutbuf, bytes, n);
}
fOutbuf += n;
fLength += n;
}
virtual void Flush() override { Append("z", 1); }
int32_t length() { return fLength; }
private:
char *fOutbuf;
int32_t fLength;
};
// Test the ByteSink base class.
void
StringTest::TestByteSink() {
char buffer[20];
buffer[4] = '!';
SimpleByteSink sink(buffer);
sink.Append("abc", 3);
sink.Flush();
if(!(sink.length() == 4 && 0 == memcmp("abcz", buffer, 4) && buffer[4] == '!')) {
errln("ByteSink (SimpleByteSink) did not Append() or Flush() as expected");
return;
}
char scratch[20];
int32_t capacity = -1;
char* dest = sink.GetAppendBuffer(0, 50, scratch, static_cast<int32_t>(sizeof(scratch)), &capacity);
if(dest != nullptr || capacity != 0) {
errln("ByteSink.GetAppendBuffer(min_capacity<1) did not properly return nullptr[0]");
return;
}
dest = sink.GetAppendBuffer(10, 50, scratch, 9, &capacity);
if(dest != nullptr || capacity != 0) {
errln("ByteSink.GetAppendBuffer(scratch_capacity<min_capacity) did not properly return nullptr[0]");
return;
}
dest = sink.GetAppendBuffer(5, 50, scratch, static_cast<int32_t>(sizeof(scratch)), &capacity);
if (dest != scratch || capacity != static_cast<int32_t>(sizeof(scratch))) {
errln("ByteSink.GetAppendBuffer() did not properly return the scratch buffer");
}
}
void
StringTest::TestCheckedArrayByteSink() {
char buffer[20]; // < 26 for the test code to work
buffer[3] = '!';
CheckedArrayByteSink sink(buffer, static_cast<int32_t>(sizeof(buffer)));
sink.Append("abc", 3);
if(!(sink.NumberOfBytesAppended() == 3 && sink.NumberOfBytesWritten() == 3 &&
0 == memcmp("abc", buffer, 3) && buffer[3] == '!') &&
!sink.Overflowed()
) {
errln("CheckedArrayByteSink did not Append() as expected");
return;
}
char scratch[10];
int32_t capacity = -1;
char* dest = sink.GetAppendBuffer(0, 50, scratch, static_cast<int32_t>(sizeof(scratch)), &capacity);
if(dest != nullptr || capacity != 0) {
errln("CheckedArrayByteSink.GetAppendBuffer(min_capacity<1) did not properly return nullptr[0]");
return;
}
dest = sink.GetAppendBuffer(10, 50, scratch, 9, &capacity);
if(dest != nullptr || capacity != 0) {
errln("CheckedArrayByteSink.GetAppendBuffer(scratch_capacity<min_capacity) did not properly return nullptr[0]");
return;
}
dest = sink.GetAppendBuffer(10, 50, scratch, static_cast<int32_t>(sizeof(scratch)), &capacity);
if (dest != buffer + 3 || capacity != static_cast<int32_t>(sizeof(buffer)) - 3) {
errln("CheckedArrayByteSink.GetAppendBuffer() did not properly return its own buffer");
return;
}
memcpy(dest, "defghijklm", 10);
sink.Append(dest, 10);
if(!(sink.NumberOfBytesAppended() == 13 && sink.NumberOfBytesWritten() == 13 &&
0 == memcmp("abcdefghijklm", buffer, 13) &&
!sink.Overflowed())
) {
errln("CheckedArrayByteSink did not Append(its own buffer) as expected");
return;
}
dest = sink.GetAppendBuffer(10, 50, scratch, static_cast<int32_t>(sizeof(scratch)), &capacity);
if (dest != scratch || capacity != static_cast<int32_t>(sizeof(scratch))) {
errln("CheckedArrayByteSink.GetAppendBuffer() did not properly return the scratch buffer");
}
memcpy(dest, "nopqrstuvw", 10);
sink.Append(dest, 10);
if(!(sink.NumberOfBytesAppended() == 23 &&
sink.NumberOfBytesWritten() == static_cast<int32_t>(sizeof(buffer)) &&
0 == memcmp("abcdefghijklmnopqrstuvwxyz", buffer, static_cast<int32_t>(sizeof(buffer))) &&
sink.Overflowed())
) {
errln("CheckedArrayByteSink did not Append(scratch buffer) as expected");
return;
}
sink.Reset().Append("123", 3);
if(!(sink.NumberOfBytesAppended() == 3 && sink.NumberOfBytesWritten() == 3 &&
0 == memcmp("123defghijklmnopqrstuvwxyz", buffer, static_cast<int32_t>(sizeof(buffer))) &&
!sink.Overflowed())
) {
errln("CheckedArrayByteSink did not Reset().Append() as expected");
return;
}
}
void
StringTest::TestStringByteSink() {
// Not much to test because only the constructors and Append()
// are implemented, and trivially so.
std::string result("abc"); // std::string
StringByteSink<std::string> sink(&result);
sink.Append("def", 3);
if(result != "abcdef") {
errln("StringByteSink did not Append() as expected");
}
StringByteSink<std::string> sink2(&result, 20);
if(result.capacity() < (result.length() + 20)) {
errln("StringByteSink should have 20 append capacity, has only %d",
static_cast<int>(result.capacity() - result.length()));
}
sink.Append("ghi", 3);
if(result != "abcdefghi") {
errln("StringByteSink did not Append() as expected");
}
}
void
StringTest::TestStringByteSinkAppendU8() {
// ICU-20984 "mitigate some C++20 char8_t breakages"
// For the following APIs there are overloads for both
// const char * and const char8_t *.
// A u8"string literal" has one type or the other
// depending on C++ version and compiler settings.
std::string result("abc");
StringByteSink<std::string> sink(&result);
sink.AppendU8("def", 3);
sink.AppendU8(u8"ghijkl", 4);
assertEquals("abcdefghij", "abcdef\x67\x68\x69\x6a", result.c_str());
}
#if defined(_MSC_VER)
#include <vector>
#endif
void
StringTest::TestSTLCompatibility() {
#if defined(_MSC_VER)
/* Just make sure that it compiles with STL's placement new usage. */
std::vector<UnicodeString> myvect;
myvect.push_back(UnicodeString("blah"));
#endif
}
void
StringTest::TestCharString() {
IcuTestErrorCode errorCode(*this, "TestCharString()");
char expected[400];
static const char longStr[] =
"This is a long string that is meant to cause reallocation of the internal buffer of CharString.";
CharString chStr(longStr, errorCode);
if (0 != strcmp(longStr, chStr.data()) || static_cast<int32_t>(strlen(longStr)) != chStr.length()) {
errln("CharString(longStr) failed.");
}
CharString test("Test", errorCode);
CharString copy(test,errorCode);
copy.copyFrom(chStr, errorCode);
if (0 != strcmp(longStr, copy.data()) || static_cast<int32_t>(strlen(longStr)) != copy.length()) {
errln("CharString.copyFrom() failed.");
}
StringPiece sp(chStr.toStringPiece());
sp.remove_prefix(4);
chStr.append(sp, errorCode).append(chStr, errorCode);
strcpy(expected, longStr);
strcat(expected, longStr+4);
strcat(expected, longStr);
strcat(expected, longStr+4);
if (0 != strcmp(expected, chStr.data()) || static_cast<int32_t>(strlen(expected)) != chStr.length()) {
errln("CharString(longStr).append(substring of self).append(self) failed.");
}
chStr.clear().append("abc", errorCode).append("defghij", 3, errorCode);
if (0 != strcmp("abcdef", chStr.data()) || 6 != chStr.length()) {
errln("CharString.clear().append(abc).append(defghij, 3) failed.");
}
chStr.appendInvariantChars(UNICODE_STRING_SIMPLE(
"This is a long string that is meant to cause reallocation of the internal buffer of CharString."),
errorCode);
strcpy(expected, "abcdef");
strcat(expected, longStr);
if (0 != strcmp(expected, chStr.data()) || static_cast<int32_t>(strlen(expected)) != chStr.length()) {
errln("CharString.appendInvariantChars(longStr) failed.");
}
int32_t appendCapacity = 0;
char *buffer = chStr.getAppendBuffer(5, 10, appendCapacity, errorCode);
if (errorCode.isFailure()) {
return;
}
memcpy(buffer, "*****", 5);
chStr.append(buffer, 5, errorCode);
chStr.truncate(chStr.length()-3);
strcat(expected, "**");
if (0 != strcmp(expected, chStr.data()) || static_cast<int32_t>(strlen(expected)) != chStr.length()) {
errln("CharString.getAppendBuffer().append(**) failed.");
}
UErrorCode ec = U_ZERO_ERROR;
chStr.clear();
chStr.appendInvariantChars(UnicodeString("The '@' character is not invariant."), ec);
if (ec != U_INVARIANT_CONVERSION_ERROR) {
errln("%s:%d expected U_INVARIANT_CONVERSION_ERROR, got %s", __FILE__, __LINE__, u_errorName(ec));
}
if (chStr.length() != 0) {
errln("%s:%d expected length() = 0, got %d", __FILE__, __LINE__, chStr.length());
}
{
CharString s1("Short string", errorCode);
CharString s2(std::move(s1));
assertEquals("s2 should have content of s1", "Short string", s2.data());
CharString s3("Dummy", errorCode);
s3 = std::move(s2);
assertEquals("s3 should have content of s2", "Short string", s3.data());
}
{
CharString s1("Long string over 40 characters to trigger heap allocation", errorCode);
CharString s2(std::move(s1));
assertEquals("s2 should have content of s1",
"Long string over 40 characters to trigger heap allocation",
s2.data());
CharString s3("Dummy string with over 40 characters to trigger heap allocation", errorCode);
s3 = std::move(s2);
assertEquals("s3 should have content of s2",
"Long string over 40 characters to trigger heap allocation",
s3.data());
}
{
// extract()
errorCode.reset();
CharString s("abc", errorCode);
char buffer[10];
s.extract(buffer, 10, errorCode);
assertEquals("abc.extract(10) success", U_ZERO_ERROR, errorCode.get());
assertEquals("abc.extract(10) output", "abc", buffer);
strcpy(buffer, "012345");
s.extract(buffer, 3, errorCode);
assertEquals("abc.extract(3) not terminated",
U_STRING_NOT_TERMINATED_WARNING, errorCode.reset());
assertEquals("abc.extract(3) output", "abc345", buffer);
strcpy(buffer, "012345");
s.extract(buffer, 2, errorCode);
assertEquals("abc.extract(2) overflow", U_BUFFER_OVERFLOW_ERROR, errorCode.reset());
}
}
void
StringTest::TestCStr() {
const char *cs = "This is a test string.";
UnicodeString us(cs);
if (0 != strcmp(CStr(us)(), cs)) {
errln("%s:%d CStr(s)() failed. Expected \"%s\", got \"%s\"", __FILE__, __LINE__, cs, CStr(us)());
}
}
void StringTest::TestCharStrAppendNumber() {
IcuTestErrorCode errorCode(*this, "TestCharStrAppendNumber()");
CharString testString;
testString.appendNumber(1, errorCode);
assertEquals("TestAppendNumber 1", "1", testString.data());
testString.clear();
testString.appendNumber(-1, errorCode);
assertEquals("TestAppendNumber -1", "-1", testString.data());
testString.clear();
testString.appendNumber(12345, errorCode);
assertEquals("TestAppendNumber 12345", "12345", testString.data());
testString.appendNumber(123, errorCode);
assertEquals("TestAppendNumber 12345 and then 123", "12345123", testString.data());
testString.clear();
testString.appendNumber(std::numeric_limits<int32_t>::max(), errorCode);
assertEquals("TestAppendNumber when appending the biggest int32", "2147483647", testString.data());
testString.clear();
testString.appendNumber(std::numeric_limits<int32_t>::min(), errorCode);
assertEquals("TestAppendNumber when appending the smallest int32", "-2147483648", testString.data());
testString.clear();
testString.appendNumber(0, errorCode);
assertEquals("TestAppendNumber when appending zero", "0", testString.data());
}
void
StringTest::Testctou() {
const char *cs = "Fa\\u0127mu";
UnicodeString u = ctou(cs);
assertEquals("Testing unescape@0", 0x0046, u.charAt(0));
assertEquals("Testing unescape@2", 295, u.charAt(2));
}
void
StringTest::TestFixedString() {
static_assert(std::is_default_constructible_v<FixedString>);
static_assert(std::is_copy_constructible_v<FixedString>);
static_assert(std::is_constructible_v<FixedString, std::string_view>);
static_assert(std::is_copy_assignable_v<FixedString>);
static_assert(std::is_assignable_v<FixedString, std::string_view>);
static_assert(std::is_move_constructible_v<FixedString>);
static_assert(std::is_move_assignable_v<FixedString>);
FixedString s;
assertTrue("default is empty", s.isEmpty());
assertTrue("default alias is nullptr", s.getAlias() == nullptr);
assertEquals("default data is empty", "", s.data());
FixedString empty("");
assertTrue("empty is empty", empty.isEmpty());
assertTrue("empty alias is nullptr", empty.getAlias() == nullptr);
assertEquals("empty data is empty", "", empty.data());
bool success = s.reserve(1);
assertTrue("reserve success", success);
assertFalse("reserved is empty", s.isEmpty());
assertFalse("reserved alias is nullptr", s.getAlias() == nullptr);
static constexpr char text[] = "foo";
FixedString init(text);
assertFalse("initialized is empty", init.isEmpty());
assertFalse("initialized alias is nullptr", init.getAlias() == nullptr);
assertEquals("initialized data is text", text, init.data());
FixedString copied(init);
assertFalse("copied is empty", copied.isEmpty());
assertFalse("copied alias is nullptr", copied.getAlias() == nullptr);
assertFalse("copied alias is same", copied.getAlias() == init.getAlias());
assertEquals("copied data is text", text, copied.data());
FixedString moved(std::move(copied));
assertFalse("moved is empty", moved.isEmpty());
assertFalse("moved alias is nullptr", moved.getAlias() == nullptr);
assertEquals("moved data is text", text, moved.data());
assertTrue("copied is empty after move", copied.isEmpty());
assertTrue("copied alias is nullptr after move", copied.getAlias() == nullptr);
assertEquals("copied data is empty after move", "", copied.data());
moved.clear();
assertTrue("cleared is empty", moved.isEmpty());
assertTrue("cleared alias is nullptr", moved.getAlias() == nullptr);
assertEquals("cleared data is empty", "", moved.data());
s = text;
assertFalse("assigned is empty", s.isEmpty());
assertFalse("assigned alias is nullptr", s.getAlias() == nullptr);
assertEquals("assigned data is text", text, s.data());
copied = s;
assertFalse("assign copied is empty", copied.isEmpty());
assertFalse("assign copied alias is nullptr", copied.getAlias() == nullptr);
assertFalse("assign copied alias is same", copied.getAlias() == s.getAlias());
assertEquals("assign copied data is text", text, copied.data());
moved = std::move(copied);
assertFalse("assign moved is empty", moved.isEmpty());
assertFalse("assign moved alias is nullptr", moved.getAlias() == nullptr);
assertEquals("assign moved data is text", text, moved.data());
assertTrue("copied is empty after move assign", copied.isEmpty());
assertTrue("copied alias is nullptr after move assign", copied.getAlias() == nullptr);
assertEquals("copied data is empty after move assign", "", copied.data());
}
void
StringTest::TestCopyInvariantChars() {
IcuTestErrorCode status(*this, "TestCopyInvariantChars()");
static constexpr char text[] = "bar";
UnicodeString src(text);
FixedString dst;
copyInvariantChars(src, dst, status);
status.errIfFailureAndReset();
assertFalse("copied is empty", dst.isEmpty());
assertFalse("copied alias is nullptr", dst.getAlias() == nullptr);
assertEquals("copied data is text", text, dst.data());
src = "fubar";
status.set(U_INTERNAL_PROGRAM_ERROR);
copyInvariantChars(src, dst, status);
status.expectErrorAndReset(U_INTERNAL_PROGRAM_ERROR);
assertFalse("copied is empty", dst.isEmpty());
assertFalse("copied alias is nullptr", dst.getAlias() == nullptr);
assertEquals("copied data is text", text, dst.data());
src.remove();
copyInvariantChars(src, dst, status);
status.errIfFailureAndReset();
assertTrue("copied is empty", dst.isEmpty());
assertTrue("copied alias is nullptr", dst.getAlias() == nullptr);
assertEquals("copied data is empty", "", dst.data());
}
|