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