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