]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/iotest/strtst.c
ICU-57166.0.1.tar.gz
[apple/icu.git] / icuSources / test / iotest / strtst.c
1 /*
2 **********************************************************************
3 * Copyright (C) 2004-2016, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * file name: strtst.c
7 * encoding: US-ASCII
8 * tab size: 8 (not used)
9 * indentation:4
10 *
11 * created on: 2004apr06
12 * created by: George Rhoten
13 */
14
15 #include "unicode/ustdio.h"
16 #include "unicode/ustring.h"
17 #include "cmemory.h"
18 #include "iotest.h"
19
20 #include <string.h>
21
22 static void TestString(void) {
23 #if !UCONFIG_NO_FORMATTING
24 int32_t n[1];
25 float myFloat = -1234.0;
26 int32_t newValuePtr[1];
27 double newDoubleValuePtr[1];
28 UChar myUString[512];
29 UChar uStringBuf[512];
30 char myString[512] = "";
31 int32_t retVal;
32 void *origPtr, *ptr;
33 U_STRING_DECL(myStringOrig, "My-String", 9);
34
35 U_STRING_INIT(myStringOrig, "My-String", 9);
36 u_memset(myUString, 0x0a, UPRV_LENGTHOF(myUString));
37 u_memset(uStringBuf, 0x0a, UPRV_LENGTHOF(uStringBuf));
38
39 *n = -1234;
40 if (sizeof(void *) == 4) {
41 origPtr = (void *)0xdeadbeef;
42 } else if (sizeof(void *) == 8) {
43 origPtr = (void *) INT64_C(0x1000200030004000);
44 } else if (sizeof(void *) == 16) {
45 /* iSeries */
46 union {
47 int32_t arr[4];
48 void *ptr;
49 } massiveBigEndianPtr = {{ 0x10002000, 0x30004000, 0x50006000, 0x70008000 }};
50 origPtr = massiveBigEndianPtr.ptr;
51 } else {
52 log_err("sizeof(void*)=%d hasn't been tested before", (int)sizeof(void*));
53 }
54
55 /* Test sprintf */
56 u_sprintf(uStringBuf, "Signed decimal integer d: %d", *n);
57 *newValuePtr = 1;
58 u_sscanf(uStringBuf, "Signed decimal integer d: %d", newValuePtr);
59 if (*n != *newValuePtr) {
60 log_err("%%d Got: %d, Expected: %d\n", *newValuePtr, *n);
61 }
62
63 u_sprintf(uStringBuf, "Signed decimal integer i: %i", *n);
64 *newValuePtr = 1;
65 u_sscanf(uStringBuf, "Signed decimal integer i: %i", newValuePtr);
66 if (*n != *newValuePtr) {
67 log_err("%%i Got: %i, Expected: %i\n", *newValuePtr, *n);
68 }
69
70 u_sprintf(uStringBuf, "Unsigned octal integer o: %o", *n);
71 *newValuePtr = 1;
72 u_sscanf(uStringBuf, "Unsigned octal integer o: %o", newValuePtr);
73 if (*n != *newValuePtr) {
74 log_err("%%o Got: %o, Expected: %o\n", *newValuePtr, *n);
75 }
76
77 u_sprintf(uStringBuf, "Unsigned decimal integer %%u: %u", *n);
78 *newValuePtr = 1;
79 u_sscanf(uStringBuf, "Unsigned decimal integer %%u: %u", newValuePtr);
80 if (*n != *newValuePtr) {
81 log_err("%%u Got: %u, Expected: %u\n", *newValuePtr, *n);
82 }
83
84 u_sprintf(uStringBuf, "Lowercase unsigned hexadecimal integer x: %x", *n);
85 *newValuePtr = 1;
86 u_sscanf(uStringBuf, "Lowercase unsigned hexadecimal integer x: %x", newValuePtr);
87 if (*n != *newValuePtr) {
88 log_err("%%x Got: %x, Expected: %x\n", *newValuePtr, *n);
89 }
90
91 u_sprintf(uStringBuf, "Uppercase unsigned hexadecimal integer X: %X", *n);
92 *newValuePtr = 1;
93 u_sscanf(uStringBuf, "Uppercase unsigned hexadecimal integer X: %X", newValuePtr);
94 if (*n != *newValuePtr) {
95 log_err("%%X Got: %X, Expected: %X\n", *newValuePtr, *n);
96 }
97
98 u_sprintf(uStringBuf, "Float f: %f", myFloat);
99 *newDoubleValuePtr = -1.0;
100 u_sscanf(uStringBuf, "Float f: %lf", newDoubleValuePtr);
101 if (myFloat != *newDoubleValuePtr) {
102 log_err("%%f Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
103 }
104
105 u_sprintf(uStringBuf, "Lowercase float e: %e", myFloat);
106 *newDoubleValuePtr = -1.0;
107 u_sscanf(uStringBuf, "Lowercase float e: %le", newDoubleValuePtr);
108 if (myFloat != *newDoubleValuePtr) {
109 log_err("%%e Got: %e, Expected: %e\n", *newDoubleValuePtr, myFloat);
110 }
111
112 u_sprintf(uStringBuf, "Uppercase float E: %E", myFloat);
113 *newDoubleValuePtr = -1.0;
114 u_sscanf(uStringBuf, "Uppercase float E: %lE", newDoubleValuePtr);
115 if (myFloat != *newDoubleValuePtr) {
116 log_err("%%E Got: %E, Expected: %E\n", *newDoubleValuePtr, myFloat);
117 }
118
119 u_sprintf(uStringBuf, "Lowercase float g: %g", myFloat);
120 *newDoubleValuePtr = -1.0;
121 u_sscanf(uStringBuf, "Lowercase float g: %lg", newDoubleValuePtr);
122 if (myFloat != *newDoubleValuePtr) {
123 log_err("%%g Got: %g, Expected: %g\n", *newDoubleValuePtr, myFloat);
124 }
125
126 u_sprintf(uStringBuf, "Uppercase float G: %G", myFloat);
127 *newDoubleValuePtr = -1.0;
128 u_sscanf(uStringBuf, "Uppercase float G: %lG", newDoubleValuePtr);
129 if (myFloat != *newDoubleValuePtr) {
130 log_err("%%G Got: %G, Expected: %G\n", *newDoubleValuePtr, myFloat);
131 }
132
133 ptr = NULL;
134 u_sprintf(uStringBuf, "Pointer %%p: %p\n", origPtr);
135 u_sscanf(uStringBuf, "Pointer %%p: %p\n", &ptr);
136 if (ptr != origPtr || u_strlen(uStringBuf) != 13+(sizeof(void*)*2)) {
137 log_err("%%p Got: %p, Expected: %p\n", ptr, origPtr);
138 }
139
140 u_sprintf(uStringBuf, "Char c: %c", 'A');
141 u_sscanf(uStringBuf, "Char c: %c", myString);
142 if (*myString != 'A') {
143 log_err("%%c Got: %c, Expected: A\n", *myString);
144 }
145
146 u_sprintf(uStringBuf, "UChar %%C: %C", (UChar)0x0041); /*'A'*/
147 u_sscanf(uStringBuf, "UChar %%C: %C", myUString);
148 if (*myUString != (UChar)0x0041) { /*'A'*/
149 log_err("%%C Got: %C, Expected: A\n", *myUString);
150 }
151
152 u_sprintf(uStringBuf, "String %%s: %s", "My-String");
153 u_sscanf(uStringBuf, "String %%s: %s", myString);
154 if (strcmp(myString, "My-String")) {
155 log_err("%%s Got: %s, Expected: My-String\n", myString);
156 }
157 if (uStringBuf[20] != 0) {
158 log_err("String not terminated. Got %c\n", uStringBuf[20] );
159 }
160 u_sprintf(uStringBuf, "NULL String %%s: %s", NULL);
161 u_sscanf(uStringBuf, "NULL String %%s: %s", myString);
162 if (strcmp(myString, "(null)")) {
163 log_err("%%s Got: %s, Expected: My-String\n", myString);
164 }
165
166 u_sprintf(uStringBuf, "Unicode String %%S: %S", myStringOrig);
167 u_sscanf(uStringBuf, "Unicode String %%S: %S", myUString);
168 u_austrncpy(myString, myUString, UPRV_LENGTHOF(myString));
169 if (strcmp(myString, "My-String")) {
170 log_err("%%S Got: %s, Expected: My String\n", myString);
171 }
172
173 u_sprintf(uStringBuf, "NULL Unicode String %%S: %S", NULL);
174 u_sscanf(uStringBuf, "NULL Unicode String %%S: %S", myUString);
175 u_austrncpy(myString, myUString, UPRV_LENGTHOF(myString));
176 if (strcmp(myString, "(null)")) {
177 log_err("%%S Got: %s, Expected: (null)\n", myString);
178 }
179
180 u_sprintf(uStringBuf, "Percent %%P (non-ANSI): %P", myFloat);
181 *newDoubleValuePtr = -1.0;
182 u_sscanf(uStringBuf, "Percent %%P (non-ANSI): %P", newDoubleValuePtr);
183 if (myFloat != *newDoubleValuePtr) {
184 log_err("%%P Got: %P, Expected: %P\n", *newDoubleValuePtr, myFloat);
185 }
186
187 u_sprintf(uStringBuf, "Spell Out %%V (non-ANSI): %V", myFloat);
188 *newDoubleValuePtr = -1.0;
189 u_sscanf(uStringBuf, "Spell Out %%V (non-ANSI): %V", newDoubleValuePtr);
190 if (myFloat != *newDoubleValuePtr) {
191 log_err("%%V Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
192 }
193
194 *newValuePtr = 1;
195 u_sprintf(uStringBuf, "\t\nPointer to integer (Count) %%n: n=%d %n n=%d\n", *newValuePtr, newValuePtr, *newValuePtr);
196 if (*newValuePtr != 37) {
197 log_err("%%V Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
198 }
199
200 /* u_sscanf(uStringBuf, "Pointer %%p: %p\n", myFile);*/
201
202 {
203 static const char longStr[] = "This is a long test12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
204
205 retVal = u_sprintf(uStringBuf, longStr);
206 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(uStringBuf));
207 if (strcmp(myString, longStr)) {
208 log_err("%%S Got: %s, Expected: %s\n", myString, longStr);
209 }
210 if (retVal != (int32_t)strlen(longStr)) {
211 log_err("%%S returned different sizes. Got: %d Expected: %d\n", retVal, strlen(longStr));
212 }
213
214 retVal = u_sprintf(uStringBuf, "%s", longStr);
215 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(uStringBuf));
216 if (strcmp(myString, longStr)) {
217 log_err("%%S Got: %s, Expected: %s\n", myString, longStr);
218 }
219 if (retVal != (int32_t)strlen(longStr)) {
220 log_err("%%S returned different sizes. Got: %d Expected: %d\n", retVal, strlen(longStr));
221 }
222
223 u_uastrncpy(myUString, longStr, UPRV_LENGTHOF(longStr));
224 u_sprintf_u(uStringBuf, myUString);
225 if (u_strcmp(myUString, uStringBuf)) {
226 log_err("%%S Long strings differ. Expected: %s\n", longStr);
227 }
228
229 u_uastrncpy(myUString, longStr, UPRV_LENGTHOF(longStr));
230 retVal = u_sprintf_u(uStringBuf, myUString+10);
231 if (u_strcmp(myUString+10, uStringBuf)) {
232 log_err("%%S Long strings differ. Expected: %s\n", longStr + 10);
233 }
234 if (retVal != (int32_t)strlen(longStr + 10)) {
235 log_err("%%S returned different sizes. Got: %d Expected: %d\n", retVal, strlen(longStr));
236 }
237
238 u_memset(uStringBuf, 1, UPRV_LENGTHOF(longStr));
239 u_uastrncpy(myUString, longStr, UPRV_LENGTHOF(longStr));
240 retVal = u_snprintf_u(uStringBuf, 10, myUString);
241 if (u_strncmp(myUString, uStringBuf, 10) || uStringBuf[10] != 1 || retVal != 10) {
242 log_err("%%S Long strings differ. Expected the first 10 characters of %s\n", longStr);
243 }
244 }
245 #endif
246 }
247
248 static void TestLocalizedString(void) {
249 #if !UCONFIG_NO_FORMATTING
250 UChar testStr[256];
251 UChar uBuffer[256];
252 char cBuffer[256];
253 int32_t numResult = -1;
254 const char *locale;
255 UFILE *strFile = u_fstropen(testStr, UPRV_LENGTHOF(testStr), "en_US");
256
257 if (!strFile) {
258 log_err("u_fstropen failed to work\n");
259 return;
260 }
261 u_fprintf(strFile, "%d", 1234);
262 u_frewind(strFile);
263 u_fscanf(strFile, "%d", &numResult);
264 u_uastrcpy(uBuffer,"1,234");
265 u_austrcpy(cBuffer,testStr);
266 if (u_strcmp(testStr, uBuffer) != 0) {
267 log_err("u_fprintf failed to work on an en string Got: %s\n", cBuffer);
268 }
269 if (numResult != 1234) {
270 log_err("u_fscanf failed to work on an en string Got: %d\n", numResult);
271 }
272
273 u_frewind(strFile);
274 locale = u_fgetlocale(strFile);
275 if (locale == NULL || strcmp(locale, "en_US") != 0) {
276 log_err("u_fgetlocale didn't return \"en\" Got: %d\n", u_fgetlocale(strFile));
277 }
278 u_fsetlocale(strFile, "de_DE");
279 locale = u_fgetlocale(strFile);
280 if (locale == NULL || strcmp(locale, "de_DE") != 0) {
281 log_err("u_fgetlocale didn't return \"de\" Got: %d\n", u_fgetlocale(strFile));
282 }
283
284 u_fprintf(strFile, "%d", 1234);
285 u_frewind(strFile);
286 numResult = -1;
287 u_fscanf(strFile, "%d", &numResult);
288 u_fclose(strFile);
289 u_uastrcpy(uBuffer,"1.234");
290 u_austrcpy(cBuffer,testStr);
291 if (u_strcmp(testStr, uBuffer) != 0) {
292 log_err("u_fprintf failed to work on a de string Got: %s\n", cBuffer);
293 }
294 if (numResult != 1234) {
295 log_err("u_fscanf failed to work on a de string Got: %d\n", numResult);
296 }
297
298 strFile = u_fstropen(testStr, UPRV_LENGTHOF(testStr), NULL);
299 u_fprintf(strFile, "%d", 1234);
300 u_frewind(strFile);
301 numResult = -1;
302 u_fscanf(strFile, "%d", &numResult);
303 u_fclose(strFile);
304 if (numResult != 1234) {
305 log_err("u_fscanf failed to work on a default locale string Got: %d, Expected: 1234\n", numResult);
306 }
307 if (u_fstropen(testStr, -1, NULL) != NULL) {
308 log_err("u_fstropen returned a UFILE* on a negative buffer size\n", numResult);
309 }
310 #endif
311 }
312
313 #if !UCONFIG_NO_FORMATTING
314 #define Test_u_snprintf(limit, format, value, expectedSize, expectedStr) \
315 u_uastrncpy(testStr, "xxxxxxxxxxxxxx", UPRV_LENGTHOF(testStr));\
316 size = u_snprintf(testStr, limit, format, value);\
317 u_austrncpy(cTestResult, testStr, UPRV_LENGTHOF(cTestResult));\
318 if (size != expectedSize || strcmp(cTestResult, expectedStr) != 0) {\
319 log_err("Unexpected formatting. size=%d expectedSize=%d cTestResult=%s expectedStr=%s\n",\
320 size, expectedSize, cTestResult, expectedStr);\
321 }\
322 else {\
323 log_verbose("Got: %s\n", cTestResult);\
324 }\
325
326 #endif
327
328 static void TestSnprintf(void) {
329 #if !UCONFIG_NO_FORMATTING
330 UChar testStr[256];
331 char cTestResult[256];
332 int32_t size;
333
334 Test_u_snprintf(0, "%d", 123, 3, "xxxxxxxxxxxxxx");
335 Test_u_snprintf(2, "%d", 123, 3, "12xxxxxxxxxxxx");
336 Test_u_snprintf(3, "%d", 123, 3, "123xxxxxxxxxxx");
337 Test_u_snprintf(4, "%d", 123, 3, "123");
338
339 Test_u_snprintf(0, "%s", "abcd", 4, "xxxxxxxxxxxxxx");
340 Test_u_snprintf(3, "%s", "abcd", 4, "abcxxxxxxxxxxx");
341 Test_u_snprintf(4, "%s", "abcd", 4, "abcdxxxxxxxxxx");
342 Test_u_snprintf(5, "%s", "abcd", 4, "abcd");
343
344 Test_u_snprintf(0, "%e", 12.34, 13, "xxxxxxxxxxxxxx");
345 Test_u_snprintf(1, "%e", 12.34, 13, "1xxxxxxxxxxxxx");
346 Test_u_snprintf(2, "%e", 12.34, 13, "1.xxxxxxxxxxxx");
347 Test_u_snprintf(3, "%e", 12.34, 13, "1.2xxxxxxxxxxx");
348 Test_u_snprintf(5, "%e", 12.34, 13, "1.234xxxxxxxxx");
349 Test_u_snprintf(6, "%e", 12.34, 13, "1.2340xxxxxxxx");
350 Test_u_snprintf(8, "%e", 12.34, 13, "1.234000xxxxxx");
351 Test_u_snprintf(9, "%e", 12.34, 13, "1.234000exxxxx");
352 Test_u_snprintf(10, "%e", 12.34, 13, "1.234000e+xxxx");
353 Test_u_snprintf(11, "%e", 12.34, 13, "1.234000e+0xxx");
354 Test_u_snprintf(13, "%e", 12.34, 13, "1.234000e+001x");
355 Test_u_snprintf(14, "%e", 12.34, 13, "1.234000e+001");
356 #endif
357 }
358
359 #define TestSPrintFormat(uFormat, uValue, cFormat, cValue) \
360 /* Reinitialize the buffer to verify null termination works. */\
361 u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer));\
362 memset(buffer, '*', UPRV_LENGTHOF(buffer));\
363 \
364 uNumPrinted = u_sprintf(uBuffer, uFormat, uValue);\
365 u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer));\
366 cNumPrinted = sprintf(buffer, cFormat, cValue);\
367 if (strcmp(buffer, compBuffer) != 0) {\
368 log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
369 }\
370 if (cNumPrinted != uNumPrinted) {\
371 log_err("%" uFormat " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
372 }\
373 if (buffer[uNumPrinted+1] != '*') {\
374 log_err("%" uFormat " too much stored\n");\
375 }\
376
377 static void TestSprintfFormat(void) {
378 #if !UCONFIG_NO_FORMATTING
379 static const UChar abcUChars[] = {0x61,0x62,0x63,0};
380 static const char abcChars[] = "abc";
381 const char *reorderFormat = "%2$d==>%1$-10.10s %6$lld %4$-10.10s %3$#x((%5$d"; /* reordering test*/
382 const char *reorderResult = "99==>truncateif 1311768467463790322 1234567890 0xf1b93((10";
383 UChar uBuffer[256];
384 char buffer[256];
385 char compBuffer[256];
386 int32_t uNumPrinted;
387 int32_t cNumPrinted;
388
389
390 TestSPrintFormat("%8S", abcUChars, "%8s", abcChars);
391 TestSPrintFormat("%-8S", abcUChars, "%-8s", abcChars);
392 TestSPrintFormat("%.2S", abcUChars, "%.2s", abcChars); /* strlen is 3 */
393
394 TestSPrintFormat("%8s", abcChars, "%8s", abcChars);
395 TestSPrintFormat("%-8s", abcChars, "%-8s", abcChars);
396 TestSPrintFormat("%.2s", abcChars, "%.2s", abcChars); /* strlen is 3 */
397
398 TestSPrintFormat("%8c", (char)'e', "%8c", (char)'e');
399 TestSPrintFormat("%-8c", (char)'e', "%-8c", (char)'e');
400
401 TestSPrintFormat("%8C", (UChar)0x65, "%8c", (char)'e');
402 TestSPrintFormat("%-8C", (UChar)0x65, "%-8c", (char)'e');
403
404 TestSPrintFormat("%f", 1.23456789, "%f", 1.23456789);
405 TestSPrintFormat("%f", 12345.6789, "%f", 12345.6789);
406 TestSPrintFormat("%f", 123456.789, "%f", 123456.789);
407 TestSPrintFormat("%f", 1234567.89, "%f", 1234567.89);
408 TestSPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
409 TestSPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
410 TestSPrintFormat("%10f", 123.456789, "%10f", 123.456789);
411 TestSPrintFormat("%10.4f", 123.456789, "%10.4f", 123.456789);
412 TestSPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
413
414 /* TestSPrintFormat("%g", 12345.6789, "%g", 12345.6789);
415 TestSPrintFormat("%g", 123456.789, "%g", 123456.789);
416 TestSPrintFormat("%g", 1234567.89, "%g", 1234567.89);
417 TestSPrintFormat("%G", 123456.789, "%G", 123456.789);
418 TestSPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
419 TestSPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
420 TestSPrintFormat("%10.4g", 1.23456789, "%10.4g", 1.23456789);
421 TestSPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
422 TestSPrintFormat("%10g", 123.456789, "%10g", 123.456789);
423 TestSPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
424
425 TestSPrintFormat("%8x", 123456, "%8x", 123456);
426 TestSPrintFormat("%-8x", 123456, "%-8x", 123456);
427 TestSPrintFormat("%08x", 123456, "%08x", 123456);
428
429 TestSPrintFormat("%8X", 123456, "%8X", 123456);
430 TestSPrintFormat("%-8X", 123456, "%-8X", 123456);
431 TestSPrintFormat("%08X", 123456, "%08X", 123456);
432 TestSPrintFormat("%#x", 123456, "%#x", 123456);
433 TestSPrintFormat("%#x", -123456, "%#x", -123456);
434
435 TestSPrintFormat("%8o", 123456, "%8o", 123456);
436 TestSPrintFormat("%-8o", 123456, "%-8o", 123456);
437 TestSPrintFormat("%08o", 123456, "%08o", 123456);
438 TestSPrintFormat("%#o", 123, "%#o", 123);
439 TestSPrintFormat("%#o", -123, "%#o", -123);
440
441 TestSPrintFormat("%8u", 123456, "%8u", 123456);
442 TestSPrintFormat("%-8u", 123456, "%-8u", 123456);
443 TestSPrintFormat("%08u", 123456, "%08u", 123456);
444 TestSPrintFormat("%8u", -123456, "%8u", -123456);
445 TestSPrintFormat("%-8u", -123456, "%-8u", -123456);
446 TestSPrintFormat("%.5u", 123456, "%.5u", 123456);
447 TestSPrintFormat("%.6u", 123456, "%.6u", 123456);
448 TestSPrintFormat("%.7u", 123456, "%.7u", 123456);
449
450 TestSPrintFormat("%8d", 123456, "%8d", 123456);
451 TestSPrintFormat("%-8d", 123456, "%-8d", 123456);
452 TestSPrintFormat("%08d", 123456, "%08d", 123456);
453 TestSPrintFormat("% d", 123456, "% d", 123456);
454 TestSPrintFormat("% d", -123456, "% d", -123456);
455
456 TestSPrintFormat("%8i", 123456, "%8i", 123456);
457 TestSPrintFormat("%-8i", 123456, "%-8i", 123456);
458 TestSPrintFormat("%08i", 123456, "%08i", 123456);
459
460 log_verbose("Get really crazy with the formatting.\n");
461
462 TestSPrintFormat("%-#12x", 123, "%-#12x", 123);
463 TestSPrintFormat("%-#12x", -123, "%-#12x", -123);
464 TestSPrintFormat("%#12x", 123, "%#12x", 123);
465 TestSPrintFormat("%#12x", -123, "%#12x", -123);
466
467 TestSPrintFormat("%-+12d", 123, "%-+12d", 123);
468 TestSPrintFormat("%-+12d", -123, "%-+12d", -123);
469 TestSPrintFormat("%- 12d", 123, "%- 12d", 123);
470 TestSPrintFormat("%- 12d", -123, "%- 12d", -123);
471 TestSPrintFormat("%+12d", 123, "%+12d", 123);
472 TestSPrintFormat("%+12d", -123, "%+12d", -123);
473 TestSPrintFormat("% 12d", 123, "% 12d", 123);
474 TestSPrintFormat("% 12d", -123, "% 12d", -123);
475 TestSPrintFormat("%12d", 123, "%12d", 123);
476 TestSPrintFormat("%12d", -123, "%12d", -123);
477 TestSPrintFormat("%.12d", 123, "%.12d", 123);
478 TestSPrintFormat("%.12d", -123, "%.12d", -123);
479
480 TestSPrintFormat("%-+12.1f", 1.234, "%-+12.1f", 1.234);
481 TestSPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
482 TestSPrintFormat("%- 12.10f", 1.234, "%- 12.10f", 1.234);
483 TestSPrintFormat("%- 12.1f", -1.234, "%- 12.1f", -1.234);
484 TestSPrintFormat("%+12.1f", 1.234, "%+12.1f", 1.234);
485 TestSPrintFormat("%+12.1f", -1.234, "%+12.1f", -1.234);
486 TestSPrintFormat("% 12.1f", 1.234, "% 12.1f", 1.234);
487 TestSPrintFormat("% 12.1f", -1.234, "% 12.1f", -1.234);
488 TestSPrintFormat("%12.1f", 1.234, "%12.1f", 1.234);
489 TestSPrintFormat("%12.1f", -1.234, "%12.1f", -1.234);
490 TestSPrintFormat("%.2f", 1.234, "%.2f", 1.234);
491 TestSPrintFormat("%.2f", -1.234, "%.2f", -1.234);
492 TestSPrintFormat("%3f", 1.234, "%3f", 1.234);
493 TestSPrintFormat("%3f", -1.234, "%3f", -1.234);
494
495 /* Test reordering format */
496 u_sprintf(uBuffer, reorderFormat,"truncateiftoolong", 99, 990099, "12345678901234567890", 10, 0x123456789abcdef2LL);
497 u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer));
498
499 if (strcmp(compBuffer, reorderResult) != 0) {
500 log_err("%s Got: \"%s\", Expected: \"%s\"\n", reorderFormat, compBuffer, buffer);
501 }
502 #endif
503 }
504
505 #undef TestSPrintFormat
506
507 static void TestStringCompatibility(void) {
508 #if !UCONFIG_NO_FORMATTING
509 UChar myUString[256];
510 UChar uStringBuf[256];
511 char myString[256] = "";
512 char testBuf[256] = "";
513 int32_t num;
514
515 u_memset(myUString, 0x0a, UPRV_LENGTHOF(myUString));
516 u_memset(uStringBuf, 0x0a, UPRV_LENGTHOF(uStringBuf));
517
518 /* Compare against C API compatibility */
519 for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) {
520 sprintf(testBuf, "%x", (int)num);
521 u_sprintf(uStringBuf, "%x", num);
522 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
523 if (strcmp(myString, testBuf) != 0) {
524 log_err("%%x Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
525 }
526
527 sprintf(testBuf, "%X", (int)num);
528 u_sprintf(uStringBuf, "%X", num);
529 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
530 if (strcmp(myString, testBuf) != 0) {
531 log_err("%%X Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
532 }
533
534 sprintf(testBuf, "%o", (int)num);
535 u_sprintf(uStringBuf, "%o", num);
536 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
537 if (strcmp(myString, testBuf) != 0) {
538 log_err("%%o Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
539 }
540
541 /* sprintf is not compatible on all platforms e.g. the iSeries*/
542 sprintf(testBuf, "%d", (int)num);
543 u_sprintf(uStringBuf, "%d", num);
544 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
545 if (strcmp(myString, testBuf) != 0) {
546 log_err("%%d Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
547 }
548
549 sprintf(testBuf, "%i", (int)num);
550 u_sprintf(uStringBuf, "%i", num);
551 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
552 if (strcmp(myString, testBuf) != 0) {
553 log_err("%%i Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
554 }
555
556 sprintf(testBuf, "%f", (double)num);
557 u_sprintf(uStringBuf, "%f", (double)num);
558 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
559 if (strcmp(myString, testBuf) != 0) {
560 log_err("%%f Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
561 }
562
563 /* sprintf(testBuf, "%e", (double)num);
564 u_sprintf(uStringBuf, "%e", (double)num);
565 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
566 if (strcmp(myString, testBuf) != 0) {
567 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
568 }
569
570 sprintf(testBuf, "%E", (double)num);
571 u_sprintf(uStringBuf, "%E", (double)num);
572 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
573 if (strcmp(myString, testBuf) != 0) {
574 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
575 }*/
576
577 sprintf(testBuf, "%g", (double)num);
578 u_sprintf(uStringBuf, "%g", (double)num);
579 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
580 if (strcmp(myString, testBuf) != 0) {
581 log_err("%%g Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
582 }
583
584 sprintf(testBuf, "%G", (double)num);
585 u_sprintf(uStringBuf, "%G", (double)num);
586 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
587 if (strcmp(myString, testBuf) != 0) {
588 log_err("%%G Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
589 }
590 }
591
592 for (num = 0; num < 0x80; num++) {
593 testBuf[0] = (char)0xFF;
594 uStringBuf[0] = (UChar)0xfffe;
595 sprintf(testBuf, "%c", (char)num);
596 u_sprintf(uStringBuf, "%c", num);
597 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
598 if (testBuf[0] != myString[0] || myString[0] != num) {
599 log_err("%%c Got: 0x%x, Expected: 0x%x\n", myString[0], testBuf[0]);
600 }
601 }
602 #endif
603 }
604
605 static void TestSScanSetFormat(const char *format, const UChar *uValue, const char *cValue, UBool expectedToPass) {
606 #if !UCONFIG_NO_FORMATTING
607 UChar uBuffer[256];
608 char buffer[256];
609 char compBuffer[256];
610 int32_t uNumScanned;
611 int32_t cNumScanned;
612
613 /* Reinitialize the buffer to verify null termination works. */
614 u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer));
615 uBuffer[UPRV_LENGTHOF(uBuffer)-1] = 0;
616 memset(buffer, '*', UPRV_LENGTHOF(buffer));
617 buffer[UPRV_LENGTHOF(buffer)-1] = 0;
618
619 uNumScanned = u_sscanf(uValue, format, uBuffer);
620 if (expectedToPass) {
621 u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer));
622 cNumScanned = sscanf(cValue, format, buffer);
623 if (strncmp(buffer, compBuffer, UPRV_LENGTHOF(uBuffer)) != 0) {
624 log_err("%s Got: \"%s\", Expected: \"%s\"\n", format, compBuffer, buffer);
625 }
626 if (cNumScanned != uNumScanned) {
627 log_err("%s number scanned Got: %d, Expected: %d\n", format, uNumScanned, cNumScanned);
628 }
629 if (uNumScanned > 0 && uBuffer[u_strlen(uBuffer)+1] != 0x2a) {
630 log_err("%s too much stored\n", format);
631 }
632 }
633 else {
634 if (uNumScanned != 0 || uBuffer[0] != 0x2a || uBuffer[1] != 0x2a) {
635 log_err("%s too much stored on a failure\n", format);
636 }
637 }
638 #endif
639 }
640
641 static void TestSScanset(void) {
642 #if !UCONFIG_NO_FORMATTING
643 static const UChar abcUChars[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
644 static const char abcChars[] = "abccdefg";
645
646 TestSScanSetFormat("%[bc]S", abcUChars, abcChars, TRUE);
647 TestSScanSetFormat("%[cb]S", abcUChars, abcChars, TRUE);
648
649 TestSScanSetFormat("%[ab]S", abcUChars, abcChars, TRUE);
650 TestSScanSetFormat("%[ba]S", abcUChars, abcChars, TRUE);
651
652 TestSScanSetFormat("%[ab]", abcUChars, abcChars, TRUE);
653 TestSScanSetFormat("%[ba]", abcUChars, abcChars, TRUE);
654
655 TestSScanSetFormat("%[abcdefgh]", abcUChars, abcChars, TRUE);
656 TestSScanSetFormat("%[;hgfedcba]", abcUChars, abcChars, TRUE);
657
658 TestSScanSetFormat("%[^a]", abcUChars, abcChars, TRUE);
659 TestSScanSetFormat("%[^e]", abcUChars, abcChars, TRUE);
660 TestSScanSetFormat("%[^ed]", abcUChars, abcChars, TRUE);
661 TestSScanSetFormat("%[^dc]", abcUChars, abcChars, TRUE);
662 TestSScanSetFormat("%[^e] ", abcUChars, abcChars, TRUE);
663
664 TestSScanSetFormat("%1[ab] ", abcUChars, abcChars, TRUE);
665 TestSScanSetFormat("%2[^f]", abcUChars, abcChars, TRUE);
666
667 TestSScanSetFormat("%[qrst]", abcUChars, abcChars, TRUE);
668
669 /* Extra long string for testing */
670 TestSScanSetFormat(" %[qrst]",
671 abcUChars, abcChars, TRUE);
672
673 TestSScanSetFormat("%[a-]", abcUChars, abcChars, TRUE);
674
675 /* Bad format */
676 TestSScanSetFormat("%[a", abcUChars, abcChars, FALSE);
677 TestSScanSetFormat("%[f-a]", abcUChars, abcChars, FALSE);
678 TestSScanSetFormat("%[c-a]", abcUChars, abcChars, FALSE);
679 /* The following is not deterministic on Windows */
680 /* TestSScanSetFormat("%[a-", abcUChars, abcChars);*/
681
682 /* TODO: Need to specify precision with a "*" */
683 #endif
684 }
685
686 static void TestBadSScanfFormat(const char *format, const UChar *uValue, const char *cValue) {
687 #if !UCONFIG_NO_FORMATTING
688 UChar uBuffer[256];
689 int32_t uNumScanned;
690
691 /* Reinitialize the buffer to verify null termination works. */
692 u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer));
693 uBuffer[UPRV_LENGTHOF(uBuffer)-1] = 0;
694
695 uNumScanned = u_sscanf(uValue, format, uBuffer);
696 if (uNumScanned != 0 || uBuffer[0] != 0x2a || uBuffer[1] != 0x2a) {
697 log_err("%s too much stored on a failure\n", format);
698 }
699 #endif
700 }
701
702 static void TestBadScanfFormat(void) {
703 #if !UCONFIG_NO_FORMATTING
704 static const UChar abcUChars[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
705 static const char abcChars[] = "abccdefg";
706
707 TestBadSScanfFormat("%[] ", abcUChars, abcChars);
708 #endif
709 }
710
711 static void Test_u_vfprintf(const char *expectedResult, const char *format, ...) {
712 #if !UCONFIG_NO_FORMATTING
713 UChar uBuffer[256];
714 UChar uBuffer2[256];
715 va_list ap;
716 int32_t count;
717
718 va_start(ap, format);
719 count = u_vsprintf(uBuffer, format, ap);
720 (void)count; /* Suppress set but not used warning */
721 va_end(ap);
722 u_uastrcpy(uBuffer2, expectedResult);
723 if (u_strcmp(uBuffer, uBuffer2) != 0) {
724 log_err("Got two different results for \"%s\" expected \"%s\"\n", format, expectedResult);
725 }
726
727 u_uastrcpy(uBuffer2, format);
728 va_start(ap, format);
729 count = u_vsprintf_u(uBuffer, uBuffer2, ap);
730 va_end(ap);
731 u_uastrcpy(uBuffer2, expectedResult);
732 if (u_strcmp(uBuffer, uBuffer2) != 0) {
733 log_err("Got two different results for \"%s\" expected \"%s\"\n", format, expectedResult);
734 }
735 #endif
736 }
737
738 static void TestVargs(void) {
739 #if !UCONFIG_NO_FORMATTING
740 Test_u_vfprintf("8 9 a B 8.9", "%d %u %x %X %.1f", 8, 9, 10, 11, 8.9);
741 #endif
742 }
743
744 static void TestCount(void) {
745 #if !UCONFIG_NO_FORMATTING
746 static const UChar x15[] = { 0x78, 0x31, 0x35, 0 };
747 UChar testStr[16];
748 UChar character;
749 int16_t i16 = -1;
750 int32_t i32 = -1, actual_count, actual_result;
751 int64_t i64 = -1;
752 u_uastrcpy(testStr, "1233456789");
753 if (u_sscanf(testStr, "%*3[123]%n%*[1-9]", &i32) != 0) {
754 log_err("test 1: scanf did not return 0\n");
755 }
756 if (i32 != 3) {
757 log_err("test 1: scanf returned %hd instead of 3\n", i32);
758 }
759 if (u_sscanf(testStr, "%*4[123]%hn%*[1-9]", &i16) != 0) {
760 log_err("test 2: scanf did not return 0\n");
761 }
762 if (i16 != 4) {
763 log_err("test 2: scanf returned %d instead of 4\n", i16);
764 }
765 if (u_sscanf(testStr, "%*[123]%*[1-9]%lln", &i64) != 0) {
766 log_err("test 3: scanf did not return 0\n");
767 }
768 if (i64 != 10) {
769 log_err("test 3: scanf did not return 10\n", i64);
770 }
771 actual_result = u_sscanf(x15, "%C%d%n", &character, &i32, &actual_count);
772 if (actual_result != 2) {
773 log_err("scanf should return 2, but returned %d\n", actual_result);
774 }
775 if (character != 0x78) {
776 log_err("scanf should return 0x78 for the character, but returned %X\n", character);
777 }
778 if (i32 != 15) {
779 log_err("scanf should return 15 for the number, but returned %d\n", i32);
780 }
781 if (actual_count != 3) {
782 log_err("scanf should return 3 for actual_count, but returned %d\n", actual_count);
783 }
784 #endif
785 }
786
787 U_CFUNC void
788 addStringTest(TestNode** root) {
789 #if !UCONFIG_NO_FORMATTING
790 addTest(root, &TestString, "string/TestString");
791 addTest(root, &TestLocalizedString, "string/TestLocalizedString");
792 addTest(root, &TestSprintfFormat, "string/TestSprintfFormat");
793 addTest(root, &TestSnprintf, "string/TestSnprintf");
794 addTest(root, &TestSScanset, "string/TestSScanset");
795 addTest(root, &TestStringCompatibility, "string/TestStringCompatibility");
796 addTest(root, &TestBadScanfFormat, "string/TestBadScanfFormat");
797 addTest(root, &TestVargs, "string/TestVargs");
798 addTest(root, &TestCount, "string/TestCount");
799 #endif
800 }
801
802