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