]>
Commit | Line | Data |
---|---|---|
1 | // © 2016 and later: Unicode, Inc. and others. | |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
3 | /******************************************************************** | |
4 | * COPYRIGHT: | |
5 | * Copyright (c) 1997-2015, International Business Machines Corporation and | |
6 | * others. All Rights Reserved. | |
7 | ********************************************************************/ | |
8 | /* file name: strtest.cpp | |
9 | * encoding: UTF-8 | |
10 | * tab size: 8 (not used) | |
11 | * indentation:4 | |
12 | * | |
13 | * created on: 1999nov22 | |
14 | * created by: Markus W. Scherer | |
15 | */ | |
16 | ||
17 | #ifdef U_HAVE_STRING_VIEW | |
18 | #include <string_view> | |
19 | #endif | |
20 | ||
21 | #include <cstddef> | |
22 | #include <string.h> | |
23 | ||
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" | |
32 | #include "charstr.h" | |
33 | #include "cstr.h" | |
34 | #include "intltest.h" | |
35 | #include "strtest.h" | |
36 | #include "uinvchar.h" | |
37 | ||
38 | StringTest::~StringTest() {} | |
39 | ||
40 | void StringTest::TestEndian(void) { | |
41 | union { | |
42 | uint8_t byte; | |
43 | uint16_t word; | |
44 | } u; | |
45 | u.word=0x0100; | |
46 | if(U_IS_BIG_ENDIAN!=u.byte) { | |
47 | errln("TestEndian: U_IS_BIG_ENDIAN needs to be fixed in platform.h"); | |
48 | } | |
49 | } | |
50 | ||
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"); | |
54 | } | |
55 | #ifdef U_INT64_T_UNAVAILABLE | |
56 | errln("int64_t and uint64_t are undefined."); | |
57 | #else | |
58 | if(8!=sizeof(int64_t)) { | |
59 | errln("TestSizeofTypes: 8!=sizeof(int64_t) - int64_t needs to be fixed in platform.h"); | |
60 | } | |
61 | if(8!=sizeof(uint64_t)) { | |
62 | errln("TestSizeofTypes: 8!=sizeof(uint64_t) - uint64_t needs to be fixed in platform.h"); | |
63 | } | |
64 | #endif | |
65 | if(8!=sizeof(double)) { | |
66 | errln("8!=sizeof(double) - putil.c code may not work"); | |
67 | } | |
68 | if(4!=sizeof(int32_t)) { | |
69 | errln("4!=sizeof(int32_t)"); | |
70 | } | |
71 | if(4!=sizeof(uint32_t)) { | |
72 | errln("4!=sizeof(uint32_t)"); | |
73 | } | |
74 | if(2!=sizeof(int16_t)) { | |
75 | errln("2!=sizeof(int16_t)"); | |
76 | } | |
77 | if(2!=sizeof(uint16_t)) { | |
78 | errln("2!=sizeof(uint16_t)"); | |
79 | } | |
80 | if(2!=sizeof(UChar)) { | |
81 | errln("2!=sizeof(UChar)"); | |
82 | } | |
83 | if(1!=sizeof(int8_t)) { | |
84 | errln("1!=sizeof(int8_t)"); | |
85 | } | |
86 | if(1!=sizeof(uint8_t)) { | |
87 | errln("1!=sizeof(uint8_t)"); | |
88 | } | |
89 | if(1!=sizeof(UBool)) { | |
90 | errln("1!=sizeof(UBool)"); | |
91 | } | |
92 | } | |
93 | ||
94 | void StringTest::TestCharsetFamily(void) { | |
95 | unsigned char c='A'; | |
96 | if( (U_CHARSET_FAMILY==U_ASCII_FAMILY && c!=0x41) || | |
97 | (U_CHARSET_FAMILY==U_EBCDIC_FAMILY && c!=0xc1) | |
98 | ) { | |
99 | errln("TestCharsetFamily: U_CHARSET_FAMILY needs to be fixed in platform.h"); | |
100 | } | |
101 | } | |
102 | ||
103 | U_STRING_DECL(ustringVar, "aZ0 -", 5); | |
104 | ||
105 | void | |
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 || | |
114 | ustringVar[5]!=0 | |
115 | ) { | |
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."); | |
118 | } | |
119 | } | |
120 | ||
121 | void | |
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 || | |
129 | ustringVar[4]!=0x2d | |
130 | ) { | |
131 | errln("Test_UNICODE_STRING: UNICODE_STRING does not work right! " | |
132 | "See unistr.h and utypes.h with platform.h."); | |
133 | } | |
134 | } | |
135 | ||
136 | void | |
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 || | |
144 | ustringVar[4]!=0x2d | |
145 | ) { | |
146 | errln("Test_UNICODE_STRING_SIMPLE: UNICODE_STRING_SIMPLE does not work right! " | |
147 | "See unistr.h and utypes.h with platform.h."); | |
148 | } | |
149 | } | |
150 | ||
151 | namespace { | |
152 | ||
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 \"%&'()*+,-./:;<=>?_"; | |
162 | ||
163 | } // namespace | |
164 | ||
165 | void | |
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); | |
176 | } | |
177 | } else { | |
178 | if (0 <= actual && actual <= 25) { | |
179 | errln("uprv_upperOrdinal('%c')=%d should have been outside 0..25", | |
180 | ic, (int)actual); | |
181 | } | |
182 | } | |
183 | if (ic == 0) { break; } | |
184 | } | |
185 | } | |
186 | ||
187 | void | |
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); | |
198 | } | |
199 | } else { | |
200 | if (0 <= actual && actual <= 25) { | |
201 | errln("uprv_lowerOrdinal('%c')=%d should have been outside 0..25", | |
202 | ic, (int)actual); | |
203 | } | |
204 | } | |
205 | if (ic == 0) { break; } | |
206 | } | |
207 | } | |
208 | ||
209 | void | |
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."); | |
217 | } | |
218 | #endif | |
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."); | |
226 | } | |
227 | } | |
228 | ||
229 | void StringTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) { | |
230 | if(exec) { | |
231 | logln("TestSuite Character and String Test: "); | |
232 | } | |
233 | TESTCASE_AUTO_BEGIN; | |
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); | |
249 | #endif | |
250 | TESTCASE_AUTO(TestByteSink); | |
251 | TESTCASE_AUTO(TestCheckedArrayByteSink); | |
252 | TESTCASE_AUTO(TestStringByteSink); | |
253 | TESTCASE_AUTO(TestCharString); | |
254 | TESTCASE_AUTO(TestCStr); | |
255 | TESTCASE_AUTO(Testctou); | |
256 | TESTCASE_AUTO_END; | |
257 | } | |
258 | ||
259 | void | |
260 | StringTest::TestStringPiece() { | |
261 | // Default constructor. | |
262 | StringPiece empty; | |
263 | if(!empty.empty() || empty.data()!=NULL || empty.length()!=0 || empty.size()!=0) { | |
264 | errln("StringPiece() failed"); | |
265 | } | |
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"); | |
270 | } | |
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"); | |
276 | } | |
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"); | |
282 | } | |
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"); | |
288 | } | |
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"); | |
293 | } | |
294 | sp=StringPiece(abcd, 5); | |
295 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
296 | errln("StringPiece(abcd, 5) failed"); | |
297 | } | |
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"); | |
301 | } | |
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"); | |
306 | } | |
307 | sp=StringPiece(abcd, 5, 8); | |
308 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
309 | errln("StringPiece(abcd, 5, 8) failed"); | |
310 | } | |
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"); | |
314 | } | |
315 | sp=StringPiece(abcd, 2, -1); | |
316 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
317 | errln("StringPiece(abcd, 5, -1) failed"); | |
318 | } | |
319 | // static const npos | |
320 | const int32_t *ptr_npos=&StringPiece::npos; | |
321 | if(StringPiece::npos!=0x7fffffff || *ptr_npos!=0x7fffffff) { | |
322 | errln("StringPiece::npos!=0x7fffffff"); | |
323 | } | |
324 | // substr() method with pos, using len=npos. | |
325 | sp=abcd.substr(-1); | |
326 | if(sp.empty() || sp.data()!=abcdefg_chars || sp.length()!=4 || sp.size()!=4) { | |
327 | errln("abcd.substr(-1) failed"); | |
328 | } | |
329 | sp=abcd.substr(5); | |
330 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
331 | errln("abcd.substr(5) failed"); | |
332 | } | |
333 | sp=abcd.substr(2); | |
334 | if(sp.empty() || sp.data()!=abcdefg_chars+2 || sp.length()!=2 || sp.size()!=2) { | |
335 | errln("abcd.substr(-1) failed"); | |
336 | } | |
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"); | |
341 | } | |
342 | sp=abcd.substr(5, 8); | |
343 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
344 | errln("abcd.substr(5, 8) failed"); | |
345 | } | |
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"); | |
349 | } | |
350 | sp=abcd.substr(2, -1); | |
351 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
352 | errln("abcd.substr(5, -1) failed"); | |
353 | } | |
354 | // clear() | |
355 | sp=abcd; | |
356 | sp.clear(); | |
357 | if(!sp.empty() || sp.data()!=NULL || sp.length()!=0 || sp.size()!=0) { | |
358 | errln("abcd.clear() failed"); | |
359 | } | |
360 | // remove_prefix() | |
361 | sp=abcd; | |
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"); | |
365 | } | |
366 | sp=abcd; | |
367 | sp.remove_prefix(2); | |
368 | if(sp.empty() || sp.data()!=abcdefg_chars+2 || sp.length()!=2 || sp.size()!=2) { | |
369 | errln("abcd.remove_prefix(2) failed"); | |
370 | } | |
371 | sp=abcd; | |
372 | sp.remove_prefix(5); | |
373 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
374 | errln("abcd.remove_prefix(5) failed"); | |
375 | } | |
376 | // remove_suffix() | |
377 | sp=abcd; | |
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"); | |
381 | } | |
382 | sp=abcd; | |
383 | sp.remove_suffix(2); | |
384 | if(sp.empty() || sp.data()!=abcdefg_chars || sp.length()!=2 || sp.size()!=2) { | |
385 | errln("abcd.remove_suffix(2) failed"); | |
386 | } | |
387 | sp=abcd; | |
388 | sp.remove_suffix(5); | |
389 | if(!sp.empty() || sp.length()!=0 || sp.size()!=0) { | |
390 | errln("abcd.remove_suffix(5) failed"); | |
391 | } | |
392 | } | |
393 | ||
394 | void | |
395 | StringTest::TestStringPieceComparisons() { | |
396 | StringPiece empty; | |
397 | StringPiece null(NULL); | |
398 | StringPiece abc("abc"); | |
399 | StringPiece abcd("abcdefg", 4); | |
400 | StringPiece abx("abx"); | |
401 | if(empty!=null) { | |
402 | errln("empty!=null"); | |
403 | } | |
404 | if(empty==abc) { | |
405 | errln("empty==abc"); | |
406 | } | |
407 | if(abc==abcd) { | |
408 | errln("abc==abcd"); | |
409 | } | |
410 | abcd.remove_suffix(1); | |
411 | if(abc!=abcd) { | |
412 | errln("abc!=abcd.remove_suffix(1)"); | |
413 | } | |
414 | if(abc==abx) { | |
415 | errln("abc==abx"); | |
416 | } | |
417 | } | |
418 | ||
419 | void | |
420 | StringTest::TestStringPieceOther() { | |
421 | static constexpr char msg[] = "Kapow!"; | |
422 | ||
423 | // Another string piece implementation. | |
424 | struct Other { | |
425 | const char* data() { return msg; } | |
426 | size_t size() { return sizeof msg - 1; } | |
427 | }; | |
428 | ||
429 | Other other; | |
430 | StringPiece piece(other); | |
431 | ||
432 | assertEquals("size()", piece.size(), other.size()); | |
433 | assertEquals("data()", piece.data(), other.data()); | |
434 | } | |
435 | ||
436 | #ifdef U_HAVE_STRING_VIEW | |
437 | void | |
438 | StringTest::TestStringPieceStringView() { | |
439 | static constexpr char msg[] = "Kapow!"; | |
440 | ||
441 | std::string_view view(msg); // C++17 | |
442 | StringPiece piece(view); | |
443 | ||
444 | assertEquals("size()", piece.size(), view.size()); | |
445 | assertEquals("data()", piece.data(), view.data()); | |
446 | } | |
447 | #endif | |
448 | ||
449 | // Verify that ByteSink is subclassable and Flush() overridable. | |
450 | class SimpleByteSink : public ByteSink { | |
451 | public: | |
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); | |
456 | } | |
457 | fOutbuf += n; | |
458 | fLength += n; | |
459 | } | |
460 | virtual void Flush() { Append("z", 1); } | |
461 | int32_t length() { return fLength; } | |
462 | private: | |
463 | char *fOutbuf; | |
464 | int32_t fLength; | |
465 | }; | |
466 | ||
467 | // Test the ByteSink base class. | |
468 | void | |
469 | StringTest::TestByteSink() { | |
470 | char buffer[20]; | |
471 | buffer[4] = '!'; | |
472 | SimpleByteSink sink(buffer); | |
473 | sink.Append("abc", 3); | |
474 | sink.Flush(); | |
475 | if(!(sink.length() == 4 && 0 == memcmp("abcz", buffer, 4) && buffer[4] == '!')) { | |
476 | errln("ByteSink (SimpleByteSink) did not Append() or Flush() as expected"); | |
477 | return; | |
478 | } | |
479 | char scratch[20]; | |
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]"); | |
484 | return; | |
485 | } | |
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]"); | |
489 | return; | |
490 | } | |
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"); | |
494 | } | |
495 | } | |
496 | ||
497 | void | |
498 | StringTest::TestCheckedArrayByteSink() { | |
499 | char buffer[20]; // < 26 for the test code to work | |
500 | buffer[3] = '!'; | |
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] == '!') && | |
505 | !sink.Overflowed() | |
506 | ) { | |
507 | errln("CheckedArrayByteSink did not Append() as expected"); | |
508 | return; | |
509 | } | |
510 | char scratch[10]; | |
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]"); | |
515 | return; | |
516 | } | |
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]"); | |
520 | return; | |
521 | } | |
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"); | |
525 | return; | |
526 | } | |
527 | memcpy(dest, "defghijklm", 10); | |
528 | sink.Append(dest, 10); | |
529 | if(!(sink.NumberOfBytesAppended() == 13 && sink.NumberOfBytesWritten() == 13 && | |
530 | 0 == memcmp("abcdefghijklm", buffer, 13) && | |
531 | !sink.Overflowed()) | |
532 | ) { | |
533 | errln("CheckedArrayByteSink did not Append(its own buffer) as expected"); | |
534 | return; | |
535 | } | |
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"); | |
539 | } | |
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)) && | |
545 | sink.Overflowed()) | |
546 | ) { | |
547 | errln("CheckedArrayByteSink did not Append(scratch buffer) as expected"); | |
548 | return; | |
549 | } | |
550 | sink.Reset().Append("123", 3); | |
551 | if(!(sink.NumberOfBytesAppended() == 3 && sink.NumberOfBytesWritten() == 3 && | |
552 | 0 == memcmp("123defghijklmnopqrstuvwxyz", buffer, (int32_t)sizeof(buffer)) && | |
553 | !sink.Overflowed()) | |
554 | ) { | |
555 | errln("CheckedArrayByteSink did not Reset().Append() as expected"); | |
556 | return; | |
557 | } | |
558 | } | |
559 | ||
560 | void | |
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"); | |
569 | } | |
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())); | |
574 | } | |
575 | sink.Append("ghi", 3); | |
576 | if(result != "abcdefghi") { | |
577 | errln("StringByteSink did not Append() as expected"); | |
578 | } | |
579 | } | |
580 | ||
581 | #if defined(_MSC_VER) | |
582 | #include <vector> | |
583 | #endif | |
584 | ||
585 | void | |
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")); | |
591 | #endif | |
592 | } | |
593 | ||
594 | void | |
595 | StringTest::TestCharString() { | |
596 | IcuTestErrorCode errorCode(*this, "TestCharString()"); | |
597 | char expected[400]; | |
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."); | |
603 | } | |
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."); | |
609 | } | |
610 | StringPiece sp(chStr.toStringPiece()); | |
611 | sp.remove_prefix(4); | |
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."); | |
619 | } | |
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."); | |
623 | } | |
624 | chStr.appendInvariantChars(UNICODE_STRING_SIMPLE( | |
625 | "This is a long string that is meant to cause reallocation of the internal buffer of CharString."), | |
626 | errorCode); | |
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."); | |
631 | } | |
632 | int32_t appendCapacity = 0; | |
633 | char *buffer = chStr.getAppendBuffer(5, 10, appendCapacity, errorCode); | |
634 | if (errorCode.isFailure()) { | |
635 | return; | |
636 | } | |
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."); | |
643 | } | |
644 | ||
645 | UErrorCode ec = U_ZERO_ERROR; | |
646 | chStr.clear(); | |
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)); | |
650 | } | |
651 | if (chStr.length() != 0) { | |
652 | errln("%s:%d expected length() = 0, got %d", __FILE__, __LINE__, chStr.length()); | |
653 | } | |
654 | ||
655 | { | |
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); | |
660 | s3 = std::move(s2); | |
661 | assertEquals("s3 should have content of s2", "Short string", s3.data()); | |
662 | } | |
663 | ||
664 | { | |
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", | |
669 | s2.data()); | |
670 | CharString s3("Dummy string with over 40 characters to trigger heap allocation", errorCode); | |
671 | s3 = std::move(s2); | |
672 | assertEquals("s3 should have content of s2", | |
673 | "Long string over 40 characters to trigger heap allocation", | |
674 | s3.data()); | |
675 | } | |
676 | } | |
677 | ||
678 | void | |
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)()); | |
684 | } | |
685 | } | |
686 | ||
687 | void | |
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)); | |
693 | } |