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
19 #include "unicode/utypes.h"
20 #include "unicode/putil.h"
21 #include "unicode/std_string.h"
22 #include "unicode/stringpiece.h"
23 #include "unicode/unistr.h"
24 #include "unicode/ustring.h"
25 #include "unicode/utf_old.h" // for UTF8_COUNT_TRAIL_BYTES
26 #include "unicode/utf8.h"
32 StringTest::~StringTest() {}
34 void StringTest::TestEndian(void) {
40 if(U_IS_BIG_ENDIAN
!=u
.byte
) {
41 errln("TestEndian: U_IS_BIG_ENDIAN needs to be fixed in platform.h");
45 void StringTest::TestSizeofTypes(void) {
46 if(U_SIZEOF_WCHAR_T
!=sizeof(wchar_t)) {
47 errln("TestSizeofWCharT: U_SIZEOF_WCHAR_T!=sizeof(wchar_t) - U_SIZEOF_WCHAR_T needs to be fixed in platform.h");
49 #ifdef U_INT64_T_UNAVAILABLE
50 errln("int64_t and uint64_t are undefined.");
52 if(8!=sizeof(int64_t)) {
53 errln("TestSizeofTypes: 8!=sizeof(int64_t) - int64_t needs to be fixed in platform.h");
55 if(8!=sizeof(uint64_t)) {
56 errln("TestSizeofTypes: 8!=sizeof(uint64_t) - uint64_t needs to be fixed in platform.h");
59 if(8!=sizeof(double)) {
60 errln("8!=sizeof(double) - putil.c code may not work");
62 if(4!=sizeof(int32_t)) {
63 errln("4!=sizeof(int32_t)");
65 if(4!=sizeof(uint32_t)) {
66 errln("4!=sizeof(uint32_t)");
68 if(2!=sizeof(int16_t)) {
69 errln("2!=sizeof(int16_t)");
71 if(2!=sizeof(uint16_t)) {
72 errln("2!=sizeof(uint16_t)");
74 if(2!=sizeof(UChar
)) {
75 errln("2!=sizeof(UChar)");
77 if(1!=sizeof(int8_t)) {
78 errln("1!=sizeof(int8_t)");
80 if(1!=sizeof(uint8_t)) {
81 errln("1!=sizeof(uint8_t)");
83 if(1!=sizeof(UBool
)) {
84 errln("1!=sizeof(UBool)");
88 void StringTest::TestCharsetFamily(void) {
90 if( (U_CHARSET_FAMILY
==U_ASCII_FAMILY
&& c
!=0x41) ||
91 (U_CHARSET_FAMILY
==U_EBCDIC_FAMILY
&& c
!=0xc1)
93 errln("TestCharsetFamily: U_CHARSET_FAMILY needs to be fixed in platform.h");
97 U_STRING_DECL(ustringVar
, "aZ0 -", 5);
100 StringTest::Test_U_STRING() {
101 U_STRING_INIT(ustringVar
, "aZ0 -", 5);
102 if( u_strlen(ustringVar
)!=5 ||
103 ustringVar
[0]!=0x61 ||
104 ustringVar
[1]!=0x5a ||
105 ustringVar
[2]!=0x30 ||
106 ustringVar
[3]!=0x20 ||
107 ustringVar
[4]!=0x2d ||
110 errln("Test_U_STRING: U_STRING_DECL with U_STRING_INIT does not work right! "
111 "See putil.h and utypes.h with platform.h.");
116 StringTest::Test_UNICODE_STRING() {
117 UnicodeString ustringVar
=UNICODE_STRING("aZ0 -", 5);
118 if( ustringVar
.length()!=5 ||
119 ustringVar
[0]!=0x61 ||
120 ustringVar
[1]!=0x5a ||
121 ustringVar
[2]!=0x30 ||
122 ustringVar
[3]!=0x20 ||
125 errln("Test_UNICODE_STRING: UNICODE_STRING does not work right! "
126 "See unistr.h and utypes.h with platform.h.");
131 StringTest::Test_UNICODE_STRING_SIMPLE() {
132 UnicodeString ustringVar
=UNICODE_STRING_SIMPLE("aZ0 -");
133 if( ustringVar
.length()!=5 ||
134 ustringVar
[0]!=0x61 ||
135 ustringVar
[1]!=0x5a ||
136 ustringVar
[2]!=0x30 ||
137 ustringVar
[3]!=0x20 ||
140 errln("Test_UNICODE_STRING_SIMPLE: UNICODE_STRING_SIMPLE does not work right! "
141 "See unistr.h and utypes.h with platform.h.");
146 StringTest::Test_UTF8_COUNT_TRAIL_BYTES() {
147 if(UTF8_COUNT_TRAIL_BYTES(0x7F) != 0
148 || UTF8_COUNT_TRAIL_BYTES(0xC0) != 1
149 || UTF8_COUNT_TRAIL_BYTES(0xE0) != 2
150 || UTF8_COUNT_TRAIL_BYTES(0xF0) != 3)
152 errln("Test_UTF8_COUNT_TRAIL_BYTES: UTF8_COUNT_TRAIL_BYTES does not work right! "
155 // Note: U8_COUNT_TRAIL_BYTES (current) and UTF8_COUNT_TRAIL_BYTES (deprecated)
156 // have completely different implementations.
157 if (U8_COUNT_TRAIL_BYTES(0x7F) != 0
158 || U8_COUNT_TRAIL_BYTES(0xC0) != 1
159 || U8_COUNT_TRAIL_BYTES(0xE0) != 2
160 || U8_COUNT_TRAIL_BYTES(0xF0) != 3)
162 errln("Test_UTF8_COUNT_TRAIL_BYTES: U8_COUNT_TRAIL_BYTES does not work right! "
167 void StringTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
169 logln("TestSuite Character and String Test: ");
172 TESTCASE_AUTO(TestEndian
);
173 TESTCASE_AUTO(TestSizeofTypes
);
174 TESTCASE_AUTO(TestCharsetFamily
);
175 TESTCASE_AUTO(Test_U_STRING
);
176 TESTCASE_AUTO(Test_UNICODE_STRING
);
177 TESTCASE_AUTO(Test_UNICODE_STRING_SIMPLE
);
178 TESTCASE_AUTO(Test_UTF8_COUNT_TRAIL_BYTES
);
179 TESTCASE_AUTO(TestSTLCompatibility
);
180 TESTCASE_AUTO(TestStringPiece
);
181 TESTCASE_AUTO(TestStringPieceComparisons
);
182 TESTCASE_AUTO(TestByteSink
);
183 TESTCASE_AUTO(TestCheckedArrayByteSink
);
184 TESTCASE_AUTO(TestStringByteSink
);
185 TESTCASE_AUTO(TestCharString
);
186 TESTCASE_AUTO(TestCStr
);
187 TESTCASE_AUTO(Testctou
);
192 StringTest::TestStringPiece() {
193 // Default constructor.
195 if(!empty
.empty() || empty
.data()!=NULL
|| empty
.length()!=0 || empty
.size()!=0) {
196 errln("StringPiece() failed");
198 // Construct from NULL const char * pointer.
199 StringPiece
null(NULL
);
200 if(!null
.empty() || null
.data()!=NULL
|| null
.length()!=0 || null
.size()!=0) {
201 errln("StringPiece(NULL) failed");
203 // Construct from const char * pointer.
204 static const char *abc_chars
="abc";
205 StringPiece
abc(abc_chars
);
206 if(abc
.empty() || abc
.data()!=abc_chars
|| abc
.length()!=3 || abc
.size()!=3) {
207 errln("StringPiece(abc_chars) failed");
209 // Construct from const char * pointer and length.
210 static const char *abcdefg_chars
="abcdefg";
211 StringPiece
abcd(abcdefg_chars
, 4);
212 if(abcd
.empty() || abcd
.data()!=abcdefg_chars
|| abcd
.length()!=4 || abcd
.size()!=4) {
213 errln("StringPiece(abcdefg_chars, 4) failed");
215 // Construct from std::string.
216 std::string
uvwxyz_string("uvwxyz");
217 StringPiece
uvwxyz(uvwxyz_string
);
218 if(uvwxyz
.empty() || uvwxyz
.data()!=uvwxyz_string
.data() || uvwxyz
.length()!=6 || uvwxyz
.size()!=6) {
219 errln("StringPiece(uvwxyz_string) failed");
221 // Substring constructor with pos.
222 StringPiece
sp(abcd
, -1);
223 if(sp
.empty() || sp
.data()!=abcdefg_chars
|| sp
.length()!=4 || sp
.size()!=4) {
224 errln("StringPiece(abcd, -1) failed");
226 sp
=StringPiece(abcd
, 5);
227 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
228 errln("StringPiece(abcd, 5) failed");
230 sp
=StringPiece(abcd
, 2);
231 if(sp
.empty() || sp
.data()!=abcdefg_chars
+2 || sp
.length()!=2 || sp
.size()!=2) {
232 errln("StringPiece(abcd, -1) failed");
234 // Substring constructor with pos and len.
235 sp
=StringPiece(abcd
, -1, 8);
236 if(sp
.empty() || sp
.data()!=abcdefg_chars
|| sp
.length()!=4 || sp
.size()!=4) {
237 errln("StringPiece(abcd, -1, 8) failed");
239 sp
=StringPiece(abcd
, 5, 8);
240 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
241 errln("StringPiece(abcd, 5, 8) failed");
243 sp
=StringPiece(abcd
, 2, 8);
244 if(sp
.empty() || sp
.data()!=abcdefg_chars
+2 || sp
.length()!=2 || sp
.size()!=2) {
245 errln("StringPiece(abcd, -1) failed");
247 sp
=StringPiece(abcd
, 2, -1);
248 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
249 errln("StringPiece(abcd, 5, -1) failed");
252 const int32_t *ptr_npos
=&StringPiece::npos
;
253 if(StringPiece::npos
!=0x7fffffff || *ptr_npos
!=0x7fffffff) {
254 errln("StringPiece::npos!=0x7fffffff");
256 // substr() method with pos, using len=npos.
258 if(sp
.empty() || sp
.data()!=abcdefg_chars
|| sp
.length()!=4 || sp
.size()!=4) {
259 errln("abcd.substr(-1) failed");
262 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
263 errln("abcd.substr(5) failed");
266 if(sp
.empty() || sp
.data()!=abcdefg_chars
+2 || sp
.length()!=2 || sp
.size()!=2) {
267 errln("abcd.substr(-1) failed");
269 // substr() method with pos and len.
270 sp
=abcd
.substr(-1, 8);
271 if(sp
.empty() || sp
.data()!=abcdefg_chars
|| sp
.length()!=4 || sp
.size()!=4) {
272 errln("abcd.substr(-1, 8) failed");
274 sp
=abcd
.substr(5, 8);
275 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
276 errln("abcd.substr(5, 8) failed");
278 sp
=abcd
.substr(2, 8);
279 if(sp
.empty() || sp
.data()!=abcdefg_chars
+2 || sp
.length()!=2 || sp
.size()!=2) {
280 errln("abcd.substr(-1) failed");
282 sp
=abcd
.substr(2, -1);
283 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
284 errln("abcd.substr(5, -1) failed");
289 if(!sp
.empty() || sp
.data()!=NULL
|| sp
.length()!=0 || sp
.size()!=0) {
290 errln("abcd.clear() failed");
294 sp
.remove_prefix(-1);
295 if(sp
.empty() || sp
.data()!=abcdefg_chars
|| sp
.length()!=4 || sp
.size()!=4) {
296 errln("abcd.remove_prefix(-1) failed");
300 if(sp
.empty() || sp
.data()!=abcdefg_chars
+2 || sp
.length()!=2 || sp
.size()!=2) {
301 errln("abcd.remove_prefix(2) failed");
305 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
306 errln("abcd.remove_prefix(5) failed");
310 sp
.remove_suffix(-1);
311 if(sp
.empty() || sp
.data()!=abcdefg_chars
|| sp
.length()!=4 || sp
.size()!=4) {
312 errln("abcd.remove_suffix(-1) failed");
316 if(sp
.empty() || sp
.data()!=abcdefg_chars
|| sp
.length()!=2 || sp
.size()!=2) {
317 errln("abcd.remove_suffix(2) failed");
321 if(!sp
.empty() || sp
.length()!=0 || sp
.size()!=0) {
322 errln("abcd.remove_suffix(5) failed");
327 StringTest::TestStringPieceComparisons() {
329 StringPiece
null(NULL
);
330 StringPiece
abc("abc");
331 StringPiece
abcd("abcdefg", 4);
332 StringPiece
abx("abx");
334 errln("empty!=null");
342 abcd
.remove_suffix(1);
344 errln("abc!=abcd.remove_suffix(1)");
351 // Verify that ByteSink is subclassable and Flush() overridable.
352 class SimpleByteSink
: public ByteSink
{
354 SimpleByteSink(char *outbuf
) : fOutbuf(outbuf
), fLength(0) {}
355 virtual void Append(const char *bytes
, int32_t n
) {
356 if(fOutbuf
!= bytes
) {
357 memcpy(fOutbuf
, bytes
, n
);
362 virtual void Flush() { Append("z", 1); }
363 int32_t length() { return fLength
; }
369 // Test the ByteSink base class.
371 StringTest::TestByteSink() {
374 SimpleByteSink
sink(buffer
);
375 sink
.Append("abc", 3);
377 if(!(sink
.length() == 4 && 0 == memcmp("abcz", buffer
, 4) && buffer
[4] == '!')) {
378 errln("ByteSink (SimpleByteSink) did not Append() or Flush() as expected");
382 int32_t capacity
= -1;
383 char *dest
= sink
.GetAppendBuffer(0, 50, scratch
, (int32_t)sizeof(scratch
), &capacity
);
384 if(dest
!= NULL
|| capacity
!= 0) {
385 errln("ByteSink.GetAppendBuffer(min_capacity<1) did not properly return NULL[0]");
388 dest
= sink
.GetAppendBuffer(10, 50, scratch
, 9, &capacity
);
389 if(dest
!= NULL
|| capacity
!= 0) {
390 errln("ByteSink.GetAppendBuffer(scratch_capacity<min_capacity) did not properly return NULL[0]");
393 dest
= sink
.GetAppendBuffer(5, 50, scratch
, (int32_t)sizeof(scratch
), &capacity
);
394 if(dest
!= scratch
|| capacity
!= (int32_t)sizeof(scratch
)) {
395 errln("ByteSink.GetAppendBuffer() did not properly return the scratch buffer");
400 StringTest::TestCheckedArrayByteSink() {
401 char buffer
[20]; // < 26 for the test code to work
403 CheckedArrayByteSink
sink(buffer
, (int32_t)sizeof(buffer
));
404 sink
.Append("abc", 3);
405 if(!(sink
.NumberOfBytesAppended() == 3 && sink
.NumberOfBytesWritten() == 3 &&
406 0 == memcmp("abc", buffer
, 3) && buffer
[3] == '!') &&
409 errln("CheckedArrayByteSink did not Append() as expected");
413 int32_t capacity
= -1;
414 char *dest
= sink
.GetAppendBuffer(0, 50, scratch
, (int32_t)sizeof(scratch
), &capacity
);
415 if(dest
!= NULL
|| capacity
!= 0) {
416 errln("CheckedArrayByteSink.GetAppendBuffer(min_capacity<1) did not properly return NULL[0]");
419 dest
= sink
.GetAppendBuffer(10, 50, scratch
, 9, &capacity
);
420 if(dest
!= NULL
|| capacity
!= 0) {
421 errln("CheckedArrayByteSink.GetAppendBuffer(scratch_capacity<min_capacity) did not properly return NULL[0]");
424 dest
= sink
.GetAppendBuffer(10, 50, scratch
, (int32_t)sizeof(scratch
), &capacity
);
425 if(dest
!= buffer
+ 3 || capacity
!= (int32_t)sizeof(buffer
) - 3) {
426 errln("CheckedArrayByteSink.GetAppendBuffer() did not properly return its own buffer");
429 memcpy(dest
, "defghijklm", 10);
430 sink
.Append(dest
, 10);
431 if(!(sink
.NumberOfBytesAppended() == 13 && sink
.NumberOfBytesWritten() == 13 &&
432 0 == memcmp("abcdefghijklm", buffer
, 13) &&
435 errln("CheckedArrayByteSink did not Append(its own buffer) as expected");
438 dest
= sink
.GetAppendBuffer(10, 50, scratch
, (int32_t)sizeof(scratch
), &capacity
);
439 if(dest
!= scratch
|| capacity
!= (int32_t)sizeof(scratch
)) {
440 errln("CheckedArrayByteSink.GetAppendBuffer() did not properly return the scratch buffer");
442 memcpy(dest
, "nopqrstuvw", 10);
443 sink
.Append(dest
, 10);
444 if(!(sink
.NumberOfBytesAppended() == 23 &&
445 sink
.NumberOfBytesWritten() == (int32_t)sizeof(buffer
) &&
446 0 == memcmp("abcdefghijklmnopqrstuvwxyz", buffer
, (int32_t)sizeof(buffer
)) &&
449 errln("CheckedArrayByteSink did not Append(scratch buffer) as expected");
452 sink
.Reset().Append("123", 3);
453 if(!(sink
.NumberOfBytesAppended() == 3 && sink
.NumberOfBytesWritten() == 3 &&
454 0 == memcmp("123defghijklmnopqrstuvwxyz", buffer
, (int32_t)sizeof(buffer
)) &&
457 errln("CheckedArrayByteSink did not Reset().Append() as expected");
463 StringTest::TestStringByteSink() {
464 // Not much to test because only the constructor and Append()
465 // are implemented, and trivially so.
466 std::string
result("abc"); // std::string
467 StringByteSink
<std::string
> sink(&result
);
468 sink
.Append("def", 3);
469 if(result
!= "abcdef") {
470 errln("StringByteSink did not Append() as expected");
474 #if defined(_MSC_VER)
479 StringTest::TestSTLCompatibility() {
480 #if defined(_MSC_VER)
481 /* Just make sure that it compiles with STL's placement new usage. */
482 std::vector
<UnicodeString
> myvect
;
483 myvect
.push_back(UnicodeString("blah"));
488 StringTest::TestCharString() {
489 IcuTestErrorCode
errorCode(*this, "TestCharString()");
491 static const char longStr
[] =
492 "This is a long string that is meant to cause reallocation of the internal buffer of CharString.";
493 CharString
chStr(longStr
, errorCode
);
494 if (0 != strcmp(longStr
, chStr
.data()) || (int32_t)strlen(longStr
) != chStr
.length()) {
495 errln("CharString(longStr) failed.");
497 CharString
test("Test", errorCode
);
498 CharString
copy(test
,errorCode
);
499 copy
.copyFrom(chStr
, errorCode
);
500 if (0 != strcmp(longStr
, copy
.data()) || (int32_t)strlen(longStr
) != copy
.length()) {
501 errln("CharString.copyFrom() failed.");
503 StringPiece
sp(chStr
.toStringPiece());
505 chStr
.append(sp
, errorCode
).append(chStr
, errorCode
);
506 strcpy(expected
, longStr
);
507 strcat(expected
, longStr
+4);
508 strcat(expected
, longStr
);
509 strcat(expected
, longStr
+4);
510 if (0 != strcmp(expected
, chStr
.data()) || (int32_t)strlen(expected
) != chStr
.length()) {
511 errln("CharString(longStr).append(substring of self).append(self) failed.");
513 chStr
.clear().append("abc", errorCode
).append("defghij", 3, errorCode
);
514 if (0 != strcmp("abcdef", chStr
.data()) || 6 != chStr
.length()) {
515 errln("CharString.clear().append(abc).append(defghij, 3) failed.");
517 chStr
.appendInvariantChars(UNICODE_STRING_SIMPLE(
518 "This is a long string that is meant to cause reallocation of the internal buffer of CharString."),
520 strcpy(expected
, "abcdef");
521 strcat(expected
, longStr
);
522 if (0 != strcmp(expected
, chStr
.data()) || (int32_t)strlen(expected
) != chStr
.length()) {
523 errln("CharString.appendInvariantChars(longStr) failed.");
525 int32_t appendCapacity
= 0;
526 char *buffer
= chStr
.getAppendBuffer(5, 10, appendCapacity
, errorCode
);
527 if (errorCode
.isFailure()) {
530 memcpy(buffer
, "*****", 5);
531 chStr
.append(buffer
, 5, errorCode
);
532 chStr
.truncate(chStr
.length()-3);
533 strcat(expected
, "**");
534 if (0 != strcmp(expected
, chStr
.data()) || (int32_t)strlen(expected
) != chStr
.length()) {
535 errln("CharString.getAppendBuffer().append(**) failed.");
538 UErrorCode ec
= U_ZERO_ERROR
;
540 chStr
.appendInvariantChars(UnicodeString("The '@' character is not invariant."), ec
);
541 if (ec
!= U_INVARIANT_CONVERSION_ERROR
) {
542 errln("%s:%d expected U_INVARIANT_CONVERSION_ERROR, got %s", __FILE__
, __LINE__
, u_errorName(ec
));
544 if (chStr
.length() != 0) {
545 errln("%s:%d expected length() = 0, got %d", __FILE__
, __LINE__
, chStr
.length());
550 StringTest::TestCStr() {
551 const char *cs
= "This is a test string.";
552 UnicodeString
us(cs
);
553 if (0 != strcmp(CStr(us
)(), cs
)) {
554 errln("%s:%d CStr(s)() failed. Expected \"%s\", got \"%s\"", __FILE__
, __LINE__
, cs
, CStr(us
)());
559 StringTest::Testctou() {
560 const char *cs
= "Fa\\u0127mu";
561 UnicodeString u
= ctou(cs
);
562 assertEquals("Testing unescape@0", (int32_t)0x0046, u
.charAt(0));
563 assertEquals("Testing unescape@2", (int32_t)295, u
.charAt(2));