1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2015, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8 /* file name: strtest.cpp
10 * tab size: 8 (not used)
13 * created on: 1999nov22
14 * created by: Markus W. Scherer
17 #ifdef U_HAVE_STRING_VIEW
18 #include <string_view>
24 #include "unicode/utypes.h"
25 #include "unicode/putil.h"
26 #include "unicode/std_string.h"
27 #include "unicode/stringpiece.h"
28 #include "unicode/unistr.h"
29 #include "unicode/ustring.h"
30 #include "unicode/utf_old.h" // for UTF8_COUNT_TRAIL_BYTES
31 #include "unicode/utf8.h"
38 StringTest::~StringTest() {}
40 void StringTest::TestEndian(void) {
46 if(U_IS_BIG_ENDIAN
!=u
.byte
) {
47 errln("TestEndian: U_IS_BIG_ENDIAN needs to be fixed in platform.h");
51 void StringTest::TestSizeofTypes(void) {
52 if(U_SIZEOF_WCHAR_T
!=sizeof(wchar_t)) {
53 errln("TestSizeofWCharT: U_SIZEOF_WCHAR_T!=sizeof(wchar_t) - U_SIZEOF_WCHAR_T needs to be fixed in platform.h");
55 #ifdef U_INT64_T_UNAVAILABLE
56 errln("int64_t and uint64_t are undefined.");
58 if(8!=sizeof(int64_t)) {
59 errln("TestSizeofTypes: 8!=sizeof(int64_t) - int64_t needs to be fixed in platform.h");
61 if(8!=sizeof(uint64_t)) {
62 errln("TestSizeofTypes: 8!=sizeof(uint64_t) - uint64_t needs to be fixed in platform.h");
65 if(8!=sizeof(double)) {
66 errln("8!=sizeof(double) - putil.c code may not work");
68 if(4!=sizeof(int32_t)) {
69 errln("4!=sizeof(int32_t)");
71 if(4!=sizeof(uint32_t)) {
72 errln("4!=sizeof(uint32_t)");
74 if(2!=sizeof(int16_t)) {
75 errln("2!=sizeof(int16_t)");
77 if(2!=sizeof(uint16_t)) {
78 errln("2!=sizeof(uint16_t)");
80 if(2!=sizeof(UChar
)) {
81 errln("2!=sizeof(UChar)");
83 if(1!=sizeof(int8_t)) {
84 errln("1!=sizeof(int8_t)");
86 if(1!=sizeof(uint8_t)) {
87 errln("1!=sizeof(uint8_t)");
89 if(1!=sizeof(UBool
)) {
90 errln("1!=sizeof(UBool)");
94 void StringTest::TestCharsetFamily(void) {
96 if( (U_CHARSET_FAMILY
==U_ASCII_FAMILY
&& c
!=0x41) ||
97 (U_CHARSET_FAMILY
==U_EBCDIC_FAMILY
&& c
!=0xc1)
99 errln("TestCharsetFamily: U_CHARSET_FAMILY needs to be fixed in platform.h");
103 U_STRING_DECL(ustringVar
, "aZ0 -", 5);
106 StringTest::Test_U_STRING() {
107 U_STRING_INIT(ustringVar
, "aZ0 -", 5);
108 if( u_strlen(ustringVar
)!=5 ||
109 ustringVar
[0]!=0x61 ||
110 ustringVar
[1]!=0x5a ||
111 ustringVar
[2]!=0x30 ||
112 ustringVar
[3]!=0x20 ||
113 ustringVar
[4]!=0x2d ||
116 errln("Test_U_STRING: U_STRING_DECL with U_STRING_INIT does not work right! "
117 "See putil.h and utypes.h with platform.h.");
122 StringTest::Test_UNICODE_STRING() {
123 UnicodeString ustringVar
=UNICODE_STRING("aZ0 -", 5);
124 if( ustringVar
.length()!=5 ||
125 ustringVar
[0]!=0x61 ||
126 ustringVar
[1]!=0x5a ||
127 ustringVar
[2]!=0x30 ||
128 ustringVar
[3]!=0x20 ||
131 errln("Test_UNICODE_STRING: UNICODE_STRING does not work right! "
132 "See unistr.h and utypes.h with platform.h.");
137 StringTest::Test_UNICODE_STRING_SIMPLE() {
138 UnicodeString ustringVar
=UNICODE_STRING_SIMPLE("aZ0 -");
139 if( ustringVar
.length()!=5 ||
140 ustringVar
[0]!=0x61 ||
141 ustringVar
[1]!=0x5a ||
142 ustringVar
[2]!=0x30 ||
143 ustringVar
[3]!=0x20 ||
146 errln("Test_UNICODE_STRING_SIMPLE: UNICODE_STRING_SIMPLE does not work right! "
147 "See unistr.h and utypes.h with platform.h.");
153 // See U_CHARSET_FAMILY in unicode/platform.h.
154 const char *nativeInvChars
=
155 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
156 "abcdefghijklmnopqrstuvwxyz"
157 "0123456789 \"%&'()*+,-./:;<=>?_";
158 const char16_t *asciiInvChars
=
159 u
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
160 u
"abcdefghijklmnopqrstuvwxyz"
161 u
"0123456789 \"%&'()*+,-./:;<=>?_";
166 StringTest::TestUpperOrdinal() {
167 for (int32_t i
= 0;; ++i
) {
168 char ic
= nativeInvChars
[i
];
169 uint8_t ac
= asciiInvChars
[i
];
170 int32_t expected
= ac
- 'A';
171 int32_t actual
= uprv_upperOrdinal(ic
);
172 if (0 <= expected
&& expected
<= 25) {
173 if (actual
!= expected
) {
174 errln("uprv_upperOrdinal('%c')=%d != expected %d",
175 ic
, (int)actual
, (int)expected
);
178 if (0 <= actual
&& actual
<= 25) {
179 errln("uprv_upperOrdinal('%c')=%d should have been outside 0..25",
183 if (ic
== 0) { break; }
188 StringTest::TestLowerOrdinal() {
189 for (int32_t i
= 0;; ++i
) {
190 char ic
= nativeInvChars
[i
];
191 uint8_t ac
= asciiInvChars
[i
];
192 int32_t expected
= ac
- 'a';
193 int32_t actual
= uprv_lowerOrdinal(ic
);
194 if (0 <= expected
&& expected
<= 25) {
195 if (actual
!= expected
) {
196 errln("uprv_lowerOrdinal('%c')=%d != expected %d",
197 ic
, (int)actual
, (int)expected
);
200 if (0 <= actual
&& actual
<= 25) {
201 errln("uprv_lowerOrdinal('%c')=%d should have been outside 0..25",
205 if (ic
== 0) { break; }
210 StringTest::Test_UTF8_COUNT_TRAIL_BYTES() {
211 #if !U_HIDE_OBSOLETE_UTF_OLD_H
212 if(UTF8_COUNT_TRAIL_BYTES(0x7F) != 0
213 || UTF8_COUNT_TRAIL_BYTES(0xC2) != 1
214 || UTF8_COUNT_TRAIL_BYTES(0xE0) != 2
215 || UTF8_COUNT_TRAIL_BYTES(0xF0) != 3) {
216 errln("UTF8_COUNT_TRAIL_BYTES does not work right! See utf_old.h.");
219 // Note: U8_COUNT_TRAIL_BYTES (current) and UTF8_COUNT_TRAIL_BYTES (deprecated)
220 // have completely different implementations.
221 if (U8_COUNT_TRAIL_BYTES(0x7F) != 0
222 || U8_COUNT_TRAIL_BYTES(0xC2) != 1
223 || U8_COUNT_TRAIL_BYTES(0xE0) != 2
224 || U8_COUNT_TRAIL_BYTES(0xF0) != 3) {
225 errln("U8_COUNT_TRAIL_BYTES does not work right! See utf8.h.");
229 void StringTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
231 logln("TestSuite Character and String Test: ");
234 TESTCASE_AUTO(TestEndian
);
235 TESTCASE_AUTO(TestSizeofTypes
);
236 TESTCASE_AUTO(TestCharsetFamily
);
237 TESTCASE_AUTO(Test_U_STRING
);
238 TESTCASE_AUTO(Test_UNICODE_STRING
);
239 TESTCASE_AUTO(Test_UNICODE_STRING_SIMPLE
);
240 TESTCASE_AUTO(TestUpperOrdinal
);
241 TESTCASE_AUTO(TestLowerOrdinal
);
242 TESTCASE_AUTO(Test_UTF8_COUNT_TRAIL_BYTES
);
243 TESTCASE_AUTO(TestSTLCompatibility
);
244 TESTCASE_AUTO(TestStringPiece
);
245 TESTCASE_AUTO(TestStringPieceComparisons
);
246 TESTCASE_AUTO(TestStringPieceOther
);
247 #ifdef U_HAVE_STRING_VIEW
248 TESTCASE_AUTO(TestStringPieceStringView
);
250 TESTCASE_AUTO(TestByteSink
);
251 TESTCASE_AUTO(TestCheckedArrayByteSink
);
252 TESTCASE_AUTO(TestStringByteSink
);
253 TESTCASE_AUTO(TestCharString
);
254 TESTCASE_AUTO(TestCStr
);
255 TESTCASE_AUTO(Testctou
);
260 StringTest::TestStringPiece() {
261 // Default constructor.
263 if(!empty
.empty() || empty
.data()!=NULL
|| empty
.length()!=0 || empty
.size()!=0) {
264 errln("StringPiece() failed");
266 // Construct from NULL const char * pointer.
267 StringPiece
null(NULL
);
268 if(!null
.empty() || null
.data()!=NULL
|| null
.length()!=0 || null
.size()!=0) {
269 errln("StringPiece(NULL) failed");
271 // Construct from const char * pointer.
272 static const char *abc_chars
="abc";
273 StringPiece
abc(abc_chars
);
274 if(abc
.empty() || abc
.data()!=abc_chars
|| abc
.length()!=3 || abc
.size()!=3) {
275 errln("StringPiece(abc_chars) failed");
277 // Construct from const char * pointer and length.
278 static const char *abcdefg_chars
="abcdefg";
279 StringPiece
abcd(abcdefg_chars
, 4);
280 if(abcd
.empty() || abcd
.data()!=abcdefg_chars
|| abcd
.length()!=4 || abcd
.size()!=4) {
281 errln("StringPiece(abcdefg_chars, 4) failed");
283 // Construct from std::string.
284 std::string
uvwxyz_string("uvwxyz");
285 StringPiece
uvwxyz(uvwxyz_string
);
286 if(uvwxyz
.empty() || uvwxyz
.data()!=uvwxyz_string
.data() || uvwxyz
.length()!=6 || uvwxyz
.size()!=6) {
287 errln("StringPiece(uvwxyz_string) failed");
289 // Substring constructor with pos.
290 StringPiece
sp(abcd
, -1);
291 if(sp
.empty() || sp
.data()!=abcdefg_chars
|| sp
.length()!=4 || sp
.size()!=4) {
292 errln("StringPiece(abcd, -1) failed");
294 sp
=StringPiece(abcd
, 5);
295 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
296 errln("StringPiece(abcd, 5) failed");
298 sp
=StringPiece(abcd
, 2);
299 if(sp
.empty() || sp
.data()!=abcdefg_chars
+2 || sp
.length()!=2 || sp
.size()!=2) {
300 errln("StringPiece(abcd, -1) failed");
302 // Substring constructor with pos and len.
303 sp
=StringPiece(abcd
, -1, 8);
304 if(sp
.empty() || sp
.data()!=abcdefg_chars
|| sp
.length()!=4 || sp
.size()!=4) {
305 errln("StringPiece(abcd, -1, 8) failed");
307 sp
=StringPiece(abcd
, 5, 8);
308 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
309 errln("StringPiece(abcd, 5, 8) failed");
311 sp
=StringPiece(abcd
, 2, 8);
312 if(sp
.empty() || sp
.data()!=abcdefg_chars
+2 || sp
.length()!=2 || sp
.size()!=2) {
313 errln("StringPiece(abcd, -1) failed");
315 sp
=StringPiece(abcd
, 2, -1);
316 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
317 errln("StringPiece(abcd, 5, -1) failed");
320 const int32_t *ptr_npos
=&StringPiece::npos
;
321 if(StringPiece::npos
!=0x7fffffff || *ptr_npos
!=0x7fffffff) {
322 errln("StringPiece::npos!=0x7fffffff");
324 // substr() method with pos, using len=npos.
326 if(sp
.empty() || sp
.data()!=abcdefg_chars
|| sp
.length()!=4 || sp
.size()!=4) {
327 errln("abcd.substr(-1) failed");
330 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
331 errln("abcd.substr(5) failed");
334 if(sp
.empty() || sp
.data()!=abcdefg_chars
+2 || sp
.length()!=2 || sp
.size()!=2) {
335 errln("abcd.substr(-1) failed");
337 // substr() method with pos and len.
338 sp
=abcd
.substr(-1, 8);
339 if(sp
.empty() || sp
.data()!=abcdefg_chars
|| sp
.length()!=4 || sp
.size()!=4) {
340 errln("abcd.substr(-1, 8) failed");
342 sp
=abcd
.substr(5, 8);
343 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
344 errln("abcd.substr(5, 8) failed");
346 sp
=abcd
.substr(2, 8);
347 if(sp
.empty() || sp
.data()!=abcdefg_chars
+2 || sp
.length()!=2 || sp
.size()!=2) {
348 errln("abcd.substr(-1) failed");
350 sp
=abcd
.substr(2, -1);
351 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
352 errln("abcd.substr(5, -1) failed");
357 if(!sp
.empty() || sp
.data()!=NULL
|| sp
.length()!=0 || sp
.size()!=0) {
358 errln("abcd.clear() failed");
362 sp
.remove_prefix(-1);
363 if(sp
.empty() || sp
.data()!=abcdefg_chars
|| sp
.length()!=4 || sp
.size()!=4) {
364 errln("abcd.remove_prefix(-1) failed");
368 if(sp
.empty() || sp
.data()!=abcdefg_chars
+2 || sp
.length()!=2 || sp
.size()!=2) {
369 errln("abcd.remove_prefix(2) failed");
373 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
374 errln("abcd.remove_prefix(5) failed");
378 sp
.remove_suffix(-1);
379 if(sp
.empty() || sp
.data()!=abcdefg_chars
|| sp
.length()!=4 || sp
.size()!=4) {
380 errln("abcd.remove_suffix(-1) failed");
384 if(sp
.empty() || sp
.data()!=abcdefg_chars
|| sp
.length()!=2 || sp
.size()!=2) {
385 errln("abcd.remove_suffix(2) failed");
389 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
390 errln("abcd.remove_suffix(5) failed");
395 StringTest::TestStringPieceComparisons() {
397 StringPiece
null(NULL
);
398 StringPiece
abc("abc");
399 StringPiece
abcd("abcdefg", 4);
400 StringPiece
abx("abx");
402 errln("empty!=null");
410 abcd
.remove_suffix(1);
412 errln("abc!=abcd.remove_suffix(1)");
420 StringTest::TestStringPieceOther() {
421 static constexpr char msg
[] = "Kapow!";
423 // Another string piece implementation.
425 const char* data() { return msg
; }
426 size_t size() { return sizeof msg
- 1; }
430 StringPiece
piece(other
);
432 assertEquals("size()", piece
.size(), other
.size());
433 assertEquals("data()", piece
.data(), other
.data());
436 #ifdef U_HAVE_STRING_VIEW
438 StringTest::TestStringPieceStringView() {
439 static constexpr char msg
[] = "Kapow!";
441 std::string_view
view(msg
); // C++17
442 StringPiece
piece(view
);
444 assertEquals("size()", piece
.size(), view
.size());
445 assertEquals("data()", piece
.data(), view
.data());
449 // Verify that ByteSink is subclassable and Flush() overridable.
450 class SimpleByteSink
: public ByteSink
{
452 SimpleByteSink(char *outbuf
) : fOutbuf(outbuf
), fLength(0) {}
453 virtual void Append(const char *bytes
, int32_t n
) {
454 if(fOutbuf
!= bytes
) {
455 memcpy(fOutbuf
, bytes
, n
);
460 virtual void Flush() { Append("z", 1); }
461 int32_t length() { return fLength
; }
467 // Test the ByteSink base class.
469 StringTest::TestByteSink() {
472 SimpleByteSink
sink(buffer
);
473 sink
.Append("abc", 3);
475 if(!(sink
.length() == 4 && 0 == memcmp("abcz", buffer
, 4) && buffer
[4] == '!')) {
476 errln("ByteSink (SimpleByteSink) did not Append() or Flush() as expected");
480 int32_t capacity
= -1;
481 char *dest
= sink
.GetAppendBuffer(0, 50, scratch
, (int32_t)sizeof(scratch
), &capacity
);
482 if(dest
!= NULL
|| capacity
!= 0) {
483 errln("ByteSink.GetAppendBuffer(min_capacity<1) did not properly return NULL[0]");
486 dest
= sink
.GetAppendBuffer(10, 50, scratch
, 9, &capacity
);
487 if(dest
!= NULL
|| capacity
!= 0) {
488 errln("ByteSink.GetAppendBuffer(scratch_capacity<min_capacity) did not properly return NULL[0]");
491 dest
= sink
.GetAppendBuffer(5, 50, scratch
, (int32_t)sizeof(scratch
), &capacity
);
492 if(dest
!= scratch
|| capacity
!= (int32_t)sizeof(scratch
)) {
493 errln("ByteSink.GetAppendBuffer() did not properly return the scratch buffer");
498 StringTest::TestCheckedArrayByteSink() {
499 char buffer
[20]; // < 26 for the test code to work
501 CheckedArrayByteSink
sink(buffer
, (int32_t)sizeof(buffer
));
502 sink
.Append("abc", 3);
503 if(!(sink
.NumberOfBytesAppended() == 3 && sink
.NumberOfBytesWritten() == 3 &&
504 0 == memcmp("abc", buffer
, 3) && buffer
[3] == '!') &&
507 errln("CheckedArrayByteSink did not Append() as expected");
511 int32_t capacity
= -1;
512 char *dest
= sink
.GetAppendBuffer(0, 50, scratch
, (int32_t)sizeof(scratch
), &capacity
);
513 if(dest
!= NULL
|| capacity
!= 0) {
514 errln("CheckedArrayByteSink.GetAppendBuffer(min_capacity<1) did not properly return NULL[0]");
517 dest
= sink
.GetAppendBuffer(10, 50, scratch
, 9, &capacity
);
518 if(dest
!= NULL
|| capacity
!= 0) {
519 errln("CheckedArrayByteSink.GetAppendBuffer(scratch_capacity<min_capacity) did not properly return NULL[0]");
522 dest
= sink
.GetAppendBuffer(10, 50, scratch
, (int32_t)sizeof(scratch
), &capacity
);
523 if(dest
!= buffer
+ 3 || capacity
!= (int32_t)sizeof(buffer
) - 3) {
524 errln("CheckedArrayByteSink.GetAppendBuffer() did not properly return its own buffer");
527 memcpy(dest
, "defghijklm", 10);
528 sink
.Append(dest
, 10);
529 if(!(sink
.NumberOfBytesAppended() == 13 && sink
.NumberOfBytesWritten() == 13 &&
530 0 == memcmp("abcdefghijklm", buffer
, 13) &&
533 errln("CheckedArrayByteSink did not Append(its own buffer) as expected");
536 dest
= sink
.GetAppendBuffer(10, 50, scratch
, (int32_t)sizeof(scratch
), &capacity
);
537 if(dest
!= scratch
|| capacity
!= (int32_t)sizeof(scratch
)) {
538 errln("CheckedArrayByteSink.GetAppendBuffer() did not properly return the scratch buffer");
540 memcpy(dest
, "nopqrstuvw", 10);
541 sink
.Append(dest
, 10);
542 if(!(sink
.NumberOfBytesAppended() == 23 &&
543 sink
.NumberOfBytesWritten() == (int32_t)sizeof(buffer
) &&
544 0 == memcmp("abcdefghijklmnopqrstuvwxyz", buffer
, (int32_t)sizeof(buffer
)) &&
547 errln("CheckedArrayByteSink did not Append(scratch buffer) as expected");
550 sink
.Reset().Append("123", 3);
551 if(!(sink
.NumberOfBytesAppended() == 3 && sink
.NumberOfBytesWritten() == 3 &&
552 0 == memcmp("123defghijklmnopqrstuvwxyz", buffer
, (int32_t)sizeof(buffer
)) &&
555 errln("CheckedArrayByteSink did not Reset().Append() as expected");
561 StringTest::TestStringByteSink() {
562 // Not much to test because only the constructors and Append()
563 // are implemented, and trivially so.
564 std::string
result("abc"); // std::string
565 StringByteSink
<std::string
> sink(&result
);
566 sink
.Append("def", 3);
567 if(result
!= "abcdef") {
568 errln("StringByteSink did not Append() as expected");
570 StringByteSink
<std::string
> sink2(&result
, 20);
571 if(result
.capacity() < (result
.length() + 20)) {
572 errln("StringByteSink should have 20 append capacity, has only %d",
573 (int)(result
.capacity() - result
.length()));
575 sink
.Append("ghi", 3);
576 if(result
!= "abcdefghi") {
577 errln("StringByteSink did not Append() as expected");
581 #if defined(_MSC_VER)
586 StringTest::TestSTLCompatibility() {
587 #if defined(_MSC_VER)
588 /* Just make sure that it compiles with STL's placement new usage. */
589 std::vector
<UnicodeString
> myvect
;
590 myvect
.push_back(UnicodeString("blah"));
595 StringTest::TestCharString() {
596 IcuTestErrorCode
errorCode(*this, "TestCharString()");
598 static const char longStr
[] =
599 "This is a long string that is meant to cause reallocation of the internal buffer of CharString.";
600 CharString
chStr(longStr
, errorCode
);
601 if (0 != strcmp(longStr
, chStr
.data()) || (int32_t)strlen(longStr
) != chStr
.length()) {
602 errln("CharString(longStr) failed.");
604 CharString
test("Test", errorCode
);
605 CharString
copy(test
,errorCode
);
606 copy
.copyFrom(chStr
, errorCode
);
607 if (0 != strcmp(longStr
, copy
.data()) || (int32_t)strlen(longStr
) != copy
.length()) {
608 errln("CharString.copyFrom() failed.");
610 StringPiece
sp(chStr
.toStringPiece());
612 chStr
.append(sp
, errorCode
).append(chStr
, errorCode
);
613 strcpy(expected
, longStr
);
614 strcat(expected
, longStr
+4);
615 strcat(expected
, longStr
);
616 strcat(expected
, longStr
+4);
617 if (0 != strcmp(expected
, chStr
.data()) || (int32_t)strlen(expected
) != chStr
.length()) {
618 errln("CharString(longStr).append(substring of self).append(self) failed.");
620 chStr
.clear().append("abc", errorCode
).append("defghij", 3, errorCode
);
621 if (0 != strcmp("abcdef", chStr
.data()) || 6 != chStr
.length()) {
622 errln("CharString.clear().append(abc).append(defghij, 3) failed.");
624 chStr
.appendInvariantChars(UNICODE_STRING_SIMPLE(
625 "This is a long string that is meant to cause reallocation of the internal buffer of CharString."),
627 strcpy(expected
, "abcdef");
628 strcat(expected
, longStr
);
629 if (0 != strcmp(expected
, chStr
.data()) || (int32_t)strlen(expected
) != chStr
.length()) {
630 errln("CharString.appendInvariantChars(longStr) failed.");
632 int32_t appendCapacity
= 0;
633 char *buffer
= chStr
.getAppendBuffer(5, 10, appendCapacity
, errorCode
);
634 if (errorCode
.isFailure()) {
637 memcpy(buffer
, "*****", 5);
638 chStr
.append(buffer
, 5, errorCode
);
639 chStr
.truncate(chStr
.length()-3);
640 strcat(expected
, "**");
641 if (0 != strcmp(expected
, chStr
.data()) || (int32_t)strlen(expected
) != chStr
.length()) {
642 errln("CharString.getAppendBuffer().append(**) failed.");
645 UErrorCode ec
= U_ZERO_ERROR
;
647 chStr
.appendInvariantChars(UnicodeString("The '@' character is not invariant."), ec
);
648 if (ec
!= U_INVARIANT_CONVERSION_ERROR
) {
649 errln("%s:%d expected U_INVARIANT_CONVERSION_ERROR, got %s", __FILE__
, __LINE__
, u_errorName(ec
));
651 if (chStr
.length() != 0) {
652 errln("%s:%d expected length() = 0, got %d", __FILE__
, __LINE__
, chStr
.length());
656 CharString
s1("Short string", errorCode
);
657 CharString
s2(std::move(s1
));
658 assertEquals("s2 should have content of s1", "Short string", s2
.data());
659 CharString
s3("Dummy", errorCode
);
661 assertEquals("s3 should have content of s2", "Short string", s3
.data());
665 CharString
s1("Long string over 40 characters to trigger heap allocation", errorCode
);
666 CharString
s2(std::move(s1
));
667 assertEquals("s2 should have content of s1",
668 "Long string over 40 characters to trigger heap allocation",
670 CharString
s3("Dummy string with over 40 characters to trigger heap allocation", errorCode
);
672 assertEquals("s3 should have content of s2",
673 "Long string over 40 characters to trigger heap allocation",
679 StringTest::TestCStr() {
680 const char *cs
= "This is a test string.";
681 UnicodeString
us(cs
);
682 if (0 != strcmp(CStr(us
)(), cs
)) {
683 errln("%s:%d CStr(s)() failed. Expected \"%s\", got \"%s\"", __FILE__
, __LINE__
, cs
, CStr(us
)());
688 StringTest::Testctou() {
689 const char *cs
= "Fa\\u0127mu";
690 UnicodeString u
= ctou(cs
);
691 assertEquals("Testing unescape@0", (int32_t)0x0046, u
.charAt(0));
692 assertEquals("Testing unescape@2", (int32_t)295, u
.charAt(2));