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