]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
b75a7d8f A |
3 | /******************************************************************** |
4 | * COPYRIGHT: | |
2ca993e8 | 5 | * Copyright (c) 1997-2015, International Business Machines Corporation and |
b75a7d8f A |
6 | * others. All Rights Reserved. |
7 | ********************************************************************/ | |
8 | /* file name: strtest.cpp | |
f3c0d7a5 | 9 | * encoding: UTF-8 |
b75a7d8f A |
10 | * tab size: 8 (not used) |
11 | * indentation:4 | |
12 | * | |
13 | * created on: 1999nov22 | |
14 | * created by: Markus W. Scherer | |
15 | */ | |
16 | ||
729e4ab9 A |
17 | #include <string.h> |
18 | ||
b75a7d8f A |
19 | #include "unicode/utypes.h" |
20 | #include "unicode/putil.h" | |
729e4ab9 A |
21 | #include "unicode/std_string.h" |
22 | #include "unicode/stringpiece.h" | |
23 | #include "unicode/unistr.h" | |
24 | #include "unicode/ustring.h" | |
f3c0d7a5 A |
25 | #include "unicode/utf_old.h" // for UTF8_COUNT_TRAIL_BYTES |
26 | #include "unicode/utf8.h" | |
729e4ab9 | 27 | #include "charstr.h" |
2ca993e8 | 28 | #include "cstr.h" |
b75a7d8f A |
29 | #include "intltest.h" |
30 | #include "strtest.h" | |
374ca955 A |
31 | |
32 | StringTest::~StringTest() {} | |
33 | ||
b75a7d8f A |
34 | void StringTest::TestEndian(void) { |
35 | union { | |
36 | uint8_t byte; | |
37 | uint16_t word; | |
38 | } u; | |
39 | u.word=0x0100; | |
40 | if(U_IS_BIG_ENDIAN!=u.byte) { | |
41 | errln("TestEndian: U_IS_BIG_ENDIAN needs to be fixed in platform.h"); | |
42 | } | |
43 | } | |
44 | ||
374ca955 | 45 | void StringTest::TestSizeofTypes(void) { |
b75a7d8f A |
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"); | |
48 | } | |
b75a7d8f A |
49 | #ifdef U_INT64_T_UNAVAILABLE |
50 | errln("int64_t and uint64_t are undefined."); | |
51 | #else | |
52 | if(8!=sizeof(int64_t)) { | |
374ca955 | 53 | errln("TestSizeofTypes: 8!=sizeof(int64_t) - int64_t needs to be fixed in platform.h"); |
b75a7d8f A |
54 | } |
55 | if(8!=sizeof(uint64_t)) { | |
374ca955 | 56 | errln("TestSizeofTypes: 8!=sizeof(uint64_t) - uint64_t needs to be fixed in platform.h"); |
b75a7d8f A |
57 | } |
58 | #endif | |
59 | if(8!=sizeof(double)) { | |
60 | errln("8!=sizeof(double) - putil.c code may not work"); | |
61 | } | |
374ca955 A |
62 | if(4!=sizeof(int32_t)) { |
63 | errln("4!=sizeof(int32_t)"); | |
64 | } | |
65 | if(4!=sizeof(uint32_t)) { | |
66 | errln("4!=sizeof(uint32_t)"); | |
67 | } | |
68 | if(2!=sizeof(int16_t)) { | |
69 | errln("2!=sizeof(int16_t)"); | |
70 | } | |
71 | if(2!=sizeof(uint16_t)) { | |
72 | errln("2!=sizeof(uint16_t)"); | |
73 | } | |
74 | if(2!=sizeof(UChar)) { | |
75 | errln("2!=sizeof(UChar)"); | |
76 | } | |
77 | if(1!=sizeof(int8_t)) { | |
78 | errln("1!=sizeof(int8_t)"); | |
79 | } | |
80 | if(1!=sizeof(uint8_t)) { | |
81 | errln("1!=sizeof(uint8_t)"); | |
82 | } | |
83 | if(1!=sizeof(UBool)) { | |
84 | errln("1!=sizeof(UBool)"); | |
85 | } | |
b75a7d8f A |
86 | } |
87 | ||
88 | void StringTest::TestCharsetFamily(void) { | |
89 | unsigned char c='A'; | |
729e4ab9 A |
90 | if( (U_CHARSET_FAMILY==U_ASCII_FAMILY && c!=0x41) || |
91 | (U_CHARSET_FAMILY==U_EBCDIC_FAMILY && c!=0xc1) | |
b75a7d8f A |
92 | ) { |
93 | errln("TestCharsetFamily: U_CHARSET_FAMILY needs to be fixed in platform.h"); | |
94 | } | |
95 | } | |
96 | ||
97 | U_STRING_DECL(ustringVar, "aZ0 -", 5); | |
98 | ||
729e4ab9 A |
99 | void |
100 | StringTest::Test_U_STRING() { | |
101 | U_STRING_INIT(ustringVar, "aZ0 -", 5); | |
51004dcb | 102 | if( u_strlen(ustringVar)!=5 || |
729e4ab9 A |
103 | ustringVar[0]!=0x61 || |
104 | ustringVar[1]!=0x5a || | |
105 | ustringVar[2]!=0x30 || | |
106 | ustringVar[3]!=0x20 || | |
107 | ustringVar[4]!=0x2d || | |
108 | ustringVar[5]!=0 | |
109 | ) { | |
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."); | |
112 | } | |
113 | } | |
114 | ||
115 | void | |
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 || | |
123 | ustringVar[4]!=0x2d | |
124 | ) { | |
125 | errln("Test_UNICODE_STRING: UNICODE_STRING does not work right! " | |
126 | "See unistr.h and utypes.h with platform.h."); | |
127 | } | |
128 | } | |
129 | ||
130 | void | |
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 || | |
138 | ustringVar[4]!=0x2d | |
139 | ) { | |
140 | errln("Test_UNICODE_STRING_SIMPLE: UNICODE_STRING_SIMPLE does not work right! " | |
141 | "See unistr.h and utypes.h with platform.h."); | |
142 | } | |
143 | } | |
144 | ||
145 | void | |
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) | |
151 | { | |
152 | errln("Test_UTF8_COUNT_TRAIL_BYTES: UTF8_COUNT_TRAIL_BYTES does not work right! " | |
f3c0d7a5 A |
153 | "See utf_old.h."); |
154 | } | |
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) | |
161 | { | |
162 | errln("Test_UTF8_COUNT_TRAIL_BYTES: U8_COUNT_TRAIL_BYTES does not work right! " | |
163 | "See utf8.h."); | |
164 | } | |
729e4ab9 A |
165 | } |
166 | ||
b75a7d8f A |
167 | void StringTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) { |
168 | if(exec) { | |
169 | logln("TestSuite Character and String Test: "); | |
170 | } | |
729e4ab9 A |
171 | TESTCASE_AUTO_BEGIN; |
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); | |
729e4ab9 A |
180 | TESTCASE_AUTO(TestStringPiece); |
181 | TESTCASE_AUTO(TestStringPieceComparisons); | |
182 | TESTCASE_AUTO(TestByteSink); | |
183 | TESTCASE_AUTO(TestCheckedArrayByteSink); | |
184 | TESTCASE_AUTO(TestStringByteSink); | |
185 | TESTCASE_AUTO(TestCharString); | |
2ca993e8 | 186 | TESTCASE_AUTO(TestCStr); |
f3c0d7a5 | 187 | TESTCASE_AUTO(Testctou); |
729e4ab9 A |
188 | TESTCASE_AUTO_END; |
189 | } | |
190 | ||
729e4ab9 A |
191 | void |
192 | StringTest::TestStringPiece() { | |
193 | // Default constructor. | |
194 | StringPiece empty; | |
195 | if(!empty.empty() || empty.data()!=NULL || empty.length()!=0 || empty.size()!=0) { | |
196 | errln("StringPiece() failed"); | |
197 | } | |
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"); | |
202 | } | |
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"); | |
208 | } | |
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"); | |
214 | } | |
729e4ab9 | 215 | // Construct from std::string. |
4388f060 | 216 | std::string uvwxyz_string("uvwxyz"); |
729e4ab9 A |
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"); | |
220 | } | |
729e4ab9 A |
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"); | |
225 | } | |
226 | sp=StringPiece(abcd, 5); | |
227 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
228 | errln("StringPiece(abcd, 5) failed"); | |
229 | } | |
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"); | |
233 | } | |
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"); | |
238 | } | |
239 | sp=StringPiece(abcd, 5, 8); | |
240 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
241 | errln("StringPiece(abcd, 5, 8) failed"); | |
242 | } | |
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"); | |
246 | } | |
247 | sp=StringPiece(abcd, 2, -1); | |
248 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
249 | errln("StringPiece(abcd, 5, -1) failed"); | |
250 | } | |
251 | // static const npos | |
252 | const int32_t *ptr_npos=&StringPiece::npos; | |
253 | if(StringPiece::npos!=0x7fffffff || *ptr_npos!=0x7fffffff) { | |
254 | errln("StringPiece::npos!=0x7fffffff"); | |
255 | } | |
256 | // substr() method with pos, using len=npos. | |
257 | sp=abcd.substr(-1); | |
258 | if(sp.empty() || sp.data()!=abcdefg_chars || sp.length()!=4 || sp.size()!=4) { | |
259 | errln("abcd.substr(-1) failed"); | |
260 | } | |
261 | sp=abcd.substr(5); | |
262 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
263 | errln("abcd.substr(5) failed"); | |
264 | } | |
265 | sp=abcd.substr(2); | |
266 | if(sp.empty() || sp.data()!=abcdefg_chars+2 || sp.length()!=2 || sp.size()!=2) { | |
267 | errln("abcd.substr(-1) failed"); | |
268 | } | |
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"); | |
273 | } | |
274 | sp=abcd.substr(5, 8); | |
275 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
276 | errln("abcd.substr(5, 8) failed"); | |
277 | } | |
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"); | |
281 | } | |
282 | sp=abcd.substr(2, -1); | |
283 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
284 | errln("abcd.substr(5, -1) failed"); | |
285 | } | |
286 | // clear() | |
287 | sp=abcd; | |
288 | sp.clear(); | |
289 | if(!sp.empty() || sp.data()!=NULL || sp.length()!=0 || sp.size()!=0) { | |
290 | errln("abcd.clear() failed"); | |
291 | } | |
292 | // remove_prefix() | |
293 | sp=abcd; | |
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"); | |
297 | } | |
298 | sp=abcd; | |
299 | sp.remove_prefix(2); | |
300 | if(sp.empty() || sp.data()!=abcdefg_chars+2 || sp.length()!=2 || sp.size()!=2) { | |
301 | errln("abcd.remove_prefix(2) failed"); | |
302 | } | |
303 | sp=abcd; | |
304 | sp.remove_prefix(5); | |
305 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
306 | errln("abcd.remove_prefix(5) failed"); | |
307 | } | |
308 | // remove_suffix() | |
309 | sp=abcd; | |
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"); | |
313 | } | |
314 | sp=abcd; | |
315 | sp.remove_suffix(2); | |
316 | if(sp.empty() || sp.data()!=abcdefg_chars || sp.length()!=2 || sp.size()!=2) { | |
317 | errln("abcd.remove_suffix(2) failed"); | |
318 | } | |
319 | sp=abcd; | |
320 | sp.remove_suffix(5); | |
321 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
322 | errln("abcd.remove_suffix(5) failed"); | |
323 | } | |
324 | } | |
325 | ||
326 | void | |
327 | StringTest::TestStringPieceComparisons() { | |
328 | StringPiece empty; | |
329 | StringPiece null(NULL); | |
330 | StringPiece abc("abc"); | |
331 | StringPiece abcd("abcdefg", 4); | |
332 | StringPiece abx("abx"); | |
333 | if(empty!=null) { | |
334 | errln("empty!=null"); | |
335 | } | |
336 | if(empty==abc) { | |
337 | errln("empty==abc"); | |
338 | } | |
339 | if(abc==abcd) { | |
340 | errln("abc==abcd"); | |
341 | } | |
342 | abcd.remove_suffix(1); | |
343 | if(abc!=abcd) { | |
344 | errln("abc!=abcd.remove_suffix(1)"); | |
345 | } | |
346 | if(abc==abx) { | |
347 | errln("abc==abx"); | |
348 | } | |
349 | } | |
350 | ||
351 | // Verify that ByteSink is subclassable and Flush() overridable. | |
352 | class SimpleByteSink : public ByteSink { | |
353 | public: | |
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); | |
b75a7d8f | 358 | } |
729e4ab9 A |
359 | fOutbuf += n; |
360 | fLength += n; | |
361 | } | |
362 | virtual void Flush() { Append("z", 1); } | |
363 | int32_t length() { return fLength; } | |
364 | private: | |
365 | char *fOutbuf; | |
366 | int32_t fLength; | |
367 | }; | |
368 | ||
369 | // Test the ByteSink base class. | |
370 | void | |
371 | StringTest::TestByteSink() { | |
372 | char buffer[20]; | |
373 | buffer[4] = '!'; | |
374 | SimpleByteSink sink(buffer); | |
375 | sink.Append("abc", 3); | |
376 | sink.Flush(); | |
377 | if(!(sink.length() == 4 && 0 == memcmp("abcz", buffer, 4) && buffer[4] == '!')) { | |
378 | errln("ByteSink (SimpleByteSink) did not Append() or Flush() as expected"); | |
379 | return; | |
380 | } | |
381 | char scratch[20]; | |
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]"); | |
386 | return; | |
387 | } | |
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]"); | |
391 | return; | |
392 | } | |
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"); | |
396 | } | |
397 | } | |
398 | ||
399 | void | |
400 | StringTest::TestCheckedArrayByteSink() { | |
401 | char buffer[20]; // < 26 for the test code to work | |
402 | buffer[3] = '!'; | |
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] == '!') && | |
407 | !sink.Overflowed() | |
408 | ) { | |
409 | errln("CheckedArrayByteSink did not Append() as expected"); | |
410 | return; | |
411 | } | |
412 | char scratch[10]; | |
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]"); | |
417 | return; | |
418 | } | |
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]"); | |
422 | return; | |
423 | } | |
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"); | |
427 | return; | |
428 | } | |
429 | memcpy(dest, "defghijklm", 10); | |
430 | sink.Append(dest, 10); | |
431 | if(!(sink.NumberOfBytesAppended() == 13 && sink.NumberOfBytesWritten() == 13 && | |
432 | 0 == memcmp("abcdefghijklm", buffer, 13) && | |
433 | !sink.Overflowed()) | |
434 | ) { | |
435 | errln("CheckedArrayByteSink did not Append(its own buffer) as expected"); | |
436 | return; | |
437 | } | |
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"); | |
441 | } | |
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)) && | |
447 | sink.Overflowed()) | |
448 | ) { | |
449 | errln("CheckedArrayByteSink did not Append(scratch buffer) as expected"); | |
450 | return; | |
451 | } | |
452 | sink.Reset().Append("123", 3); | |
453 | if(!(sink.NumberOfBytesAppended() == 3 && sink.NumberOfBytesWritten() == 3 && | |
454 | 0 == memcmp("123defghijklmnopqrstuvwxyz", buffer, (int32_t)sizeof(buffer)) && | |
455 | !sink.Overflowed()) | |
456 | ) { | |
457 | errln("CheckedArrayByteSink did not Reset().Append() as expected"); | |
458 | return; | |
459 | } | |
460 | } | |
461 | ||
462 | void | |
463 | StringTest::TestStringByteSink() { | |
729e4ab9 A |
464 | // Not much to test because only the constructor and Append() |
465 | // are implemented, and trivially so. | |
4388f060 A |
466 | std::string result("abc"); // std::string |
467 | StringByteSink<std::string> sink(&result); | |
729e4ab9 A |
468 | sink.Append("def", 3); |
469 | if(result != "abcdef") { | |
470 | errln("StringByteSink did not Append() as expected"); | |
471 | } | |
729e4ab9 A |
472 | } |
473 | ||
4388f060 | 474 | #if defined(_MSC_VER) |
729e4ab9 | 475 | #include <vector> |
374ca955 | 476 | #endif |
729e4ab9 A |
477 | |
478 | void | |
479 | StringTest::TestSTLCompatibility() { | |
4388f060 | 480 | #if defined(_MSC_VER) |
729e4ab9 A |
481 | /* Just make sure that it compiles with STL's placement new usage. */ |
482 | std::vector<UnicodeString> myvect; | |
483 | myvect.push_back(UnicodeString("blah")); | |
484 | #endif | |
485 | } | |
486 | ||
487 | void | |
488 | StringTest::TestCharString() { | |
489 | IcuTestErrorCode errorCode(*this, "TestCharString()"); | |
490 | char expected[400]; | |
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."); | |
496 | } | |
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."); | |
502 | } | |
503 | StringPiece sp(chStr.toStringPiece()); | |
504 | sp.remove_prefix(4); | |
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."); | |
512 | } | |
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."); | |
516 | } | |
517 | chStr.appendInvariantChars(UNICODE_STRING_SIMPLE( | |
518 | "This is a long string that is meant to cause reallocation of the internal buffer of CharString."), | |
519 | errorCode); | |
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."); | |
524 | } | |
525 | int32_t appendCapacity = 0; | |
526 | char *buffer = chStr.getAppendBuffer(5, 10, appendCapacity, errorCode); | |
527 | if (errorCode.isFailure()) { | |
528 | return; | |
529 | } | |
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."); | |
b75a7d8f | 536 | } |
2ca993e8 A |
537 | |
538 | UErrorCode ec = U_ZERO_ERROR; | |
539 | chStr.clear(); | |
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)); | |
543 | } | |
544 | if (chStr.length() != 0) { | |
545 | errln("%s:%d expected length() = 0, got %d", __FILE__, __LINE__, chStr.length()); | |
546 | } | |
547 | } | |
548 | ||
549 | void | |
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)()); | |
555 | } | |
b75a7d8f | 556 | } |
f3c0d7a5 A |
557 | |
558 | void | |
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)); | |
564 | } |