]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/iotest/strtst.c
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / test / iotest / strtst.c
1 /*
2 **********************************************************************
3 * Copyright (C) 2004-2006, 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
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
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
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, 0, "xxxxxxxxxxxxxx");
334 Test_u_snprintf(2, "%d", 123, 2, "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", 0, "xxxxxxxxxxxxxx");
339 Test_u_snprintf(3, "%s", "abcd", 3, "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, 0, "xxxxxxxxxxxxxx");
344 Test_u_snprintf(1, "%e", 12.34, 1, "1xxxxxxxxxxxxx");
345 Test_u_snprintf(2, "%e", 12.34, 2, "1.xxxxxxxxxxxx");
346 Test_u_snprintf(3, "%e", 12.34, 3, "1.2xxxxxxxxxxx");
347 Test_u_snprintf(5, "%e", 12.34, 5, "1.234xxxxxxxxx");
348 Test_u_snprintf(6, "%e", 12.34, 6, "1.2340xxxxxxxx");
349 Test_u_snprintf(8, "%e", 12.34, 8, "1.234000xxxxxx");
350 Test_u_snprintf(9, "%e", 12.34, 9, "1.234000exxxxx");
351 Test_u_snprintf(10, "%e", 12.34, 10, "1.234000e+xxxx");
352 Test_u_snprintf(11, "%e", 12.34, 11, "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
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 UChar uBuffer[256];
381 char buffer[256];
382 char compBuffer[256];
383 int32_t uNumPrinted;
384 int32_t cNumPrinted;
385
386
387 TestSPrintFormat("%8S", abcUChars, "%8s", abcChars);
388 TestSPrintFormat("%-8S", abcUChars, "%-8s", abcChars);
389 TestSPrintFormat("%.2S", abcUChars, "%.2s", abcChars); /* strlen is 3 */
390
391 TestSPrintFormat("%8s", abcChars, "%8s", abcChars);
392 TestSPrintFormat("%-8s", abcChars, "%-8s", abcChars);
393 TestSPrintFormat("%.2s", abcChars, "%.2s", abcChars); /* strlen is 3 */
394
395 TestSPrintFormat("%8c", (char)'e', "%8c", (char)'e');
396 TestSPrintFormat("%-8c", (char)'e', "%-8c", (char)'e');
397
398 TestSPrintFormat("%8C", (UChar)0x65, "%8c", (char)'e');
399 TestSPrintFormat("%-8C", (UChar)0x65, "%-8c", (char)'e');
400
401 TestSPrintFormat("%f", 1.23456789, "%f", 1.23456789);
402 TestSPrintFormat("%f", 12345.6789, "%f", 12345.6789);
403 TestSPrintFormat("%f", 123456.789, "%f", 123456.789);
404 TestSPrintFormat("%f", 1234567.89, "%f", 1234567.89);
405 TestSPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
406 TestSPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
407 TestSPrintFormat("%10f", 123.456789, "%10f", 123.456789);
408 TestSPrintFormat("%10.4f", 123.456789, "%10.4f", 123.456789);
409 TestSPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
410
411 /* TestSPrintFormat("%g", 12345.6789, "%g", 12345.6789);
412 TestSPrintFormat("%g", 123456.789, "%g", 123456.789);
413 TestSPrintFormat("%g", 1234567.89, "%g", 1234567.89);
414 TestSPrintFormat("%G", 123456.789, "%G", 123456.789);
415 TestSPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
416 TestSPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
417 TestSPrintFormat("%10.4g", 1.23456789, "%10.4g", 1.23456789);
418 TestSPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
419 TestSPrintFormat("%10g", 123.456789, "%10g", 123.456789);
420 TestSPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
421
422 TestSPrintFormat("%8x", 123456, "%8x", 123456);
423 TestSPrintFormat("%-8x", 123456, "%-8x", 123456);
424 TestSPrintFormat("%08x", 123456, "%08x", 123456);
425
426 TestSPrintFormat("%8X", 123456, "%8X", 123456);
427 TestSPrintFormat("%-8X", 123456, "%-8X", 123456);
428 TestSPrintFormat("%08X", 123456, "%08X", 123456);
429 TestSPrintFormat("%#x", 123456, "%#x", 123456);
430 TestSPrintFormat("%#x", -123456, "%#x", -123456);
431
432 TestSPrintFormat("%8o", 123456, "%8o", 123456);
433 TestSPrintFormat("%-8o", 123456, "%-8o", 123456);
434 TestSPrintFormat("%08o", 123456, "%08o", 123456);
435 TestSPrintFormat("%#o", 123, "%#o", 123);
436 TestSPrintFormat("%#o", -123, "%#o", -123);
437
438 TestSPrintFormat("%8u", 123456, "%8u", 123456);
439 TestSPrintFormat("%-8u", 123456, "%-8u", 123456);
440 TestSPrintFormat("%08u", 123456, "%08u", 123456);
441 TestSPrintFormat("%8u", -123456, "%8u", -123456);
442 TestSPrintFormat("%-8u", -123456, "%-8u", -123456);
443 TestSPrintFormat("%.5u", 123456, "%.5u", 123456);
444 TestSPrintFormat("%.6u", 123456, "%.6u", 123456);
445 TestSPrintFormat("%.7u", 123456, "%.7u", 123456);
446
447 TestSPrintFormat("%8d", 123456, "%8d", 123456);
448 TestSPrintFormat("%-8d", 123456, "%-8d", 123456);
449 TestSPrintFormat("%08d", 123456, "%08d", 123456);
450 TestSPrintFormat("% d", 123456, "% d", 123456);
451 TestSPrintFormat("% d", -123456, "% d", -123456);
452
453 TestSPrintFormat("%8i", 123456, "%8i", 123456);
454 TestSPrintFormat("%-8i", 123456, "%-8i", 123456);
455 TestSPrintFormat("%08i", 123456, "%08i", 123456);
456
457 log_verbose("Get really crazy with the formatting.\n");
458
459 TestSPrintFormat("%-#12x", 123, "%-#12x", 123);
460 TestSPrintFormat("%-#12x", -123, "%-#12x", -123);
461 TestSPrintFormat("%#12x", 123, "%#12x", 123);
462 TestSPrintFormat("%#12x", -123, "%#12x", -123);
463
464 TestSPrintFormat("%-+12d", 123, "%-+12d", 123);
465 TestSPrintFormat("%-+12d", -123, "%-+12d", -123);
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
477 TestSPrintFormat("%-+12.1f", 1.234, "%-+12.1f", 1.234);
478 TestSPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
479 TestSPrintFormat("%- 12.10f", 1.234, "%- 12.10f", 1.234);
480 TestSPrintFormat("%- 12.1f", -1.234, "%- 12.1f", -1.234);
481 TestSPrintFormat("%+12.1f", 1.234, "%+12.1f", 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("%.2f", 1.234, "%.2f", 1.234);
488 TestSPrintFormat("%.2f", -1.234, "%.2f", -1.234);
489 TestSPrintFormat("%3f", 1.234, "%3f", 1.234);
490 TestSPrintFormat("%3f", -1.234, "%3f", -1.234);
491 #endif
492 }
493
494 #undef TestSPrintFormat
495
496 static void TestStringCompatibility(void) {
497 #if !UCONFIG_NO_FORMATTING
498 UChar myUString[256];
499 UChar uStringBuf[256];
500 char myString[256] = "";
501 char testBuf[256] = "";
502 int32_t num;
503
504 u_memset(myUString, 0x0a, sizeof(myUString)/ sizeof(*myUString));
505 u_memset(uStringBuf, 0x0a, sizeof(uStringBuf) / sizeof(*uStringBuf));
506
507 /* Compare against C API compatibility */
508 for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) {
509 sprintf(testBuf, "%x", (int)num);
510 u_sprintf(uStringBuf, "%x", num);
511 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
512 if (strcmp(myString, testBuf) != 0) {
513 log_err("%%x Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
514 }
515
516 sprintf(testBuf, "%X", (int)num);
517 u_sprintf(uStringBuf, "%X", num);
518 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
519 if (strcmp(myString, testBuf) != 0) {
520 log_err("%%X Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
521 }
522
523 sprintf(testBuf, "%o", (int)num);
524 u_sprintf(uStringBuf, "%o", num);
525 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
526 if (strcmp(myString, testBuf) != 0) {
527 log_err("%%o Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
528 }
529
530 /* sprintf is not compatible on all platforms e.g. the iSeries*/
531 sprintf(testBuf, "%d", (int)num);
532 u_sprintf(uStringBuf, "%d", num);
533 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
534 if (strcmp(myString, testBuf) != 0) {
535 log_err("%%d Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
536 }
537
538 sprintf(testBuf, "%i", (int)num);
539 u_sprintf(uStringBuf, "%i", num);
540 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
541 if (strcmp(myString, testBuf) != 0) {
542 log_err("%%i Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
543 }
544
545 sprintf(testBuf, "%f", (double)num);
546 u_sprintf(uStringBuf, "%f", (double)num);
547 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
548 if (strcmp(myString, testBuf) != 0) {
549 log_err("%%f Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
550 }
551
552 /* sprintf(testBuf, "%e", (double)num);
553 u_sprintf(uStringBuf, "%e", (double)num);
554 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
555 if (strcmp(myString, testBuf) != 0) {
556 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
557 }
558
559 sprintf(testBuf, "%E", (double)num);
560 u_sprintf(uStringBuf, "%E", (double)num);
561 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
562 if (strcmp(myString, testBuf) != 0) {
563 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
564 }*/
565
566 sprintf(testBuf, "%g", (double)num);
567 u_sprintf(uStringBuf, "%g", (double)num);
568 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
569 if (strcmp(myString, testBuf) != 0) {
570 log_err("%%g Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
571 }
572
573 sprintf(testBuf, "%G", (double)num);
574 u_sprintf(uStringBuf, "%G", (double)num);
575 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
576 if (strcmp(myString, testBuf) != 0) {
577 log_err("%%G Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
578 }
579 }
580
581 for (num = 0; num < 0x80; num++) {
582 testBuf[0] = (char)0xFF;
583 uStringBuf[0] = (UChar)0xfffe;
584 sprintf(testBuf, "%c", (char)num);
585 u_sprintf(uStringBuf, "%c", num);
586 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
587 if (testBuf[0] != myString[0] || myString[0] != num) {
588 log_err("%%c Got: 0x%x, Expected: 0x%x\n", myString[0], testBuf[0]);
589 }
590 }
591 #endif
592 }
593
594 static void TestSScanSetFormat(const char *format, const UChar *uValue, const char *cValue, UBool expectedToPass) {
595 #if !UCONFIG_NO_FORMATTING
596 UChar uBuffer[256];
597 char buffer[256];
598 char compBuffer[256];
599 int32_t uNumScanned;
600 int32_t cNumScanned;
601
602 /* Reinitialize the buffer to verify null termination works. */
603 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));
604 uBuffer[sizeof(uBuffer)/sizeof(*uBuffer)-1] = 0;
605 memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));
606 buffer[sizeof(buffer)/sizeof(*buffer)-1] = 0;
607
608 uNumScanned = u_sscanf(uValue, format, uBuffer);
609 if (expectedToPass) {
610 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(uBuffer[0]));
611 cNumScanned = sscanf(cValue, format, buffer);
612 if (strncmp(buffer, compBuffer, sizeof(uBuffer)/sizeof(uBuffer[0])) != 0) {
613 log_err("%s Got: \"%s\", Expected: \"%s\"\n", format, compBuffer, buffer);
614 }
615 if (cNumScanned != uNumScanned) {
616 log_err("%s number scanned Got: %d, Expected: %d\n", format, uNumScanned, cNumScanned);
617 }
618 if (uNumScanned > 0 && uBuffer[u_strlen(uBuffer)+1] != 0x2a) {
619 log_err("%s too much stored\n", format);
620 }
621 }
622 else {
623 if (uNumScanned != 0 || uBuffer[0] != 0x2a || uBuffer[1] != 0x2a) {
624 log_err("%s too much stored on a failure\n", format);
625 }
626 }
627 #endif
628 }
629
630 static void TestSScanset(void) {
631 #if !UCONFIG_NO_FORMATTING
632 static const UChar abcUChars[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
633 static const char abcChars[] = "abccdefg";
634
635 TestSScanSetFormat("%[bc]S", abcUChars, abcChars, TRUE);
636 TestSScanSetFormat("%[cb]S", abcUChars, abcChars, TRUE);
637
638 TestSScanSetFormat("%[ab]S", abcUChars, abcChars, TRUE);
639 TestSScanSetFormat("%[ba]S", abcUChars, abcChars, TRUE);
640
641 TestSScanSetFormat("%[ab]", abcUChars, abcChars, TRUE);
642 TestSScanSetFormat("%[ba]", abcUChars, abcChars, TRUE);
643
644 TestSScanSetFormat("%[abcdefgh]", abcUChars, abcChars, TRUE);
645 TestSScanSetFormat("%[;hgfedcba]", abcUChars, abcChars, TRUE);
646
647 TestSScanSetFormat("%[^a]", abcUChars, abcChars, TRUE);
648 TestSScanSetFormat("%[^e]", abcUChars, abcChars, TRUE);
649 TestSScanSetFormat("%[^ed]", abcUChars, abcChars, TRUE);
650 TestSScanSetFormat("%[^dc]", abcUChars, abcChars, TRUE);
651 TestSScanSetFormat("%[^e] ", abcUChars, abcChars, TRUE);
652
653 TestSScanSetFormat("%1[ab] ", abcUChars, abcChars, TRUE);
654 TestSScanSetFormat("%2[^f]", abcUChars, abcChars, TRUE);
655
656 TestSScanSetFormat("%[qrst]", abcUChars, abcChars, TRUE);
657
658 /* Extra long string for testing */
659 TestSScanSetFormat(" %[qrst]",
660 abcUChars, abcChars, TRUE);
661
662 TestSScanSetFormat("%[a-]", abcUChars, abcChars, TRUE);
663
664 /* Bad format */
665 TestSScanSetFormat("%[a", abcUChars, abcChars, FALSE);
666 TestSScanSetFormat("%[f-a]", abcUChars, abcChars, FALSE);
667 TestSScanSetFormat("%[c-a]", abcUChars, abcChars, FALSE);
668 /* The following is not deterministic on Windows */
669 /* TestSScanSetFormat("%[a-", abcUChars, abcChars);*/
670
671 /* TODO: Need to specify precision with a "*" */
672 #endif
673 }
674
675 static void TestBadSScanfFormat(const char *format, const UChar *uValue, const char *cValue) {
676 #if !UCONFIG_NO_FORMATTING
677 UChar uBuffer[256];
678 int32_t uNumScanned;
679
680 /* Reinitialize the buffer to verify null termination works. */
681 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));
682 uBuffer[sizeof(uBuffer)/sizeof(*uBuffer)-1] = 0;
683
684 uNumScanned = u_sscanf(uValue, format, uBuffer);
685 if (uNumScanned != 0 || uBuffer[0] != 0x2a || uBuffer[1] != 0x2a) {
686 log_err("%s too much stored on a failure\n", format);
687 }
688 #endif
689 }
690
691 static void TestBadScanfFormat(void) {
692 #if !UCONFIG_NO_FORMATTING
693 static const UChar abcUChars[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
694 static const char abcChars[] = "abccdefg";
695
696 TestBadSScanfFormat("%[] ", abcUChars, abcChars);
697 #endif
698 }
699
700 static void Test_u_vfprintf(const char *expectedResult, const char *format, ...) {
701 #if !UCONFIG_NO_FORMATTING
702 UChar uBuffer[256];
703 UChar uBuffer2[256];
704 va_list ap;
705 int32_t count;
706
707 va_start(ap, format);
708 count = u_vsprintf(uBuffer, format, ap);
709 va_end(ap);
710 u_uastrcpy(uBuffer2, expectedResult);
711 if (u_strcmp(uBuffer, uBuffer2) != 0) {
712 log_err("Got two different results for \"%s\" expected \"%s\"\n", format, expectedResult);
713 }
714
715 u_uastrcpy(uBuffer2, format);
716 va_start(ap, format);
717 count = u_vsprintf_u(uBuffer, uBuffer2, ap);
718 va_end(ap);
719 u_uastrcpy(uBuffer2, expectedResult);
720 if (u_strcmp(uBuffer, uBuffer2) != 0) {
721 log_err("Got two different results for \"%s\" expected \"%s\"\n", format, expectedResult);
722 }
723 #endif
724 }
725
726 static void TestVargs(void) {
727 #if !UCONFIG_NO_FORMATTING
728 Test_u_vfprintf("8 9 a B 8.9", "%d %u %x %X %.1f", 8, 9, 10, 11, 8.9);
729 #endif
730 }
731
732 static void TestCount(void) {
733 #if !UCONFIG_NO_FORMATTING
734 static const UChar x15[] = { 0x78, 0x31, 0x35, 0 };
735 UChar testStr[16];
736 UChar character;
737 int16_t i16 = -1;
738 int32_t i32 = -1, actual_count, actual_result;
739 int64_t i64 = -1;
740 u_uastrcpy(testStr, "1233456789");
741 if (u_sscanf(testStr, "%*3[123]%n%*[1-9]", &i32) != 0) {
742 log_err("test 1: scanf did not return 0\n");
743 }
744 if (i32 != 3) {
745 log_err("test 1: scanf returned %hd instead of 3\n", i32);
746 }
747 if (u_sscanf(testStr, "%*4[123]%hn%*[1-9]", &i16) != 0) {
748 log_err("test 2: scanf did not return 0\n");
749 }
750 if (i16 != 4) {
751 log_err("test 2: scanf returned %d instead of 4\n", i16);
752 }
753 if (u_sscanf(testStr, "%*[123]%*[1-9]%lln", &i64) != 0) {
754 log_err("test 3: scanf did not return 0\n");
755 }
756 if (i64 != 10) {
757 log_err("test 3: scanf did not return 10\n", i64);
758 }
759 actual_result = u_sscanf(x15, "%C%d%n", &character, &i32, &actual_count);
760 if (actual_result != 2) {
761 log_err("scanf should return 2, but returned %d\n", actual_result);
762 }
763 if (character != 0x78) {
764 log_err("scanf should return 0x78 for the character, but returned %X\n", character);
765 }
766 if (i32 != 15) {
767 log_err("scanf should return 15 for the number, but returned %d\n", i32);
768 }
769 if (actual_count != 3) {
770 log_err("scanf should return 3 for actual_count, but returned %d\n", actual_count);
771 }
772 #endif
773 }
774
775 U_CFUNC void
776 addStringTest(TestNode** root) {
777 #if !UCONFIG_NO_FORMATTING
778 addTest(root, &TestString, "string/TestString");
779 addTest(root, &TestLocalizedString, "string/TestLocalizedString");
780 addTest(root, &TestSprintfFormat, "string/TestSprintfFormat");
781 addTest(root, &TestSnprintf, "string/TestSnprintf");
782 addTest(root, &TestSScanset, "string/TestSScanset");
783 addTest(root, &TestStringCompatibility, "string/TestStringCompatibility");
784 addTest(root, &TestBadScanfFormat, "string/TestBadScanfFormat");
785 addTest(root, &TestVargs, "string/TestVargs");
786 addTest(root, &TestCount, "string/TestCount");
787 #endif
788 }
789
790