]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/iotest/strtst.c
2 **********************************************************************
3 * Copyright (C) 2004-2007, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
8 * tab size: 8 (not used)
11 * created on: 2004apr06
12 * created by: George Rhoten
15 #include "unicode/ustdio.h"
16 #include "unicode/ustring.h"
21 static void TestString ( void ) {
22 #if !UCONFIG_NO_FORMATTING
24 float myFloat
= - 1234.0 ;
25 int32_t newValuePtr
[ 1 ];
26 double newDoubleValuePtr
[ 1 ];
28 UChar uStringBuf
[ 512 ];
29 char myString
[ 512 ] = "" ;
32 U_STRING_DECL ( myStringOrig
, "My-String" , 9 );
34 U_STRING_INIT ( myStringOrig
, "My-String" , 9 );
35 u_memset ( myUString
, 0x0a , sizeof ( myUString
)/ sizeof (* myUString
));
36 u_memset ( uStringBuf
, 0x0a , sizeof ( uStringBuf
) / sizeof (* uStringBuf
));
39 if ( sizeof ( void *) == 4 ) {
40 origPtr
= ( void *) 0xdeadbeef ;
41 } else if ( sizeof ( void *) == 8 ) {
42 origPtr
= ( void *) INT64_C ( 0x1000200030004000 );
43 } else if ( sizeof ( void *) == 16 ) {
48 } massiveBigEndianPtr
= {{ 0x10002000 , 0x30004000 , 0x50006000 , 0x70008000 }};
49 origPtr
= massiveBigEndianPtr
. ptr
;
51 log_err ( "sizeof(void*)= %d hasn't been tested before" , ( int ) sizeof ( void *));
55 u_sprintf ( uStringBuf
, "Signed decimal integer d: %d " , * n
);
57 u_sscanf ( uStringBuf
, "Signed decimal integer d: %d " , newValuePtr
);
58 if (* n
!= * newValuePtr
) {
59 log_err ( "% %d Got: %d , Expected: %d \n " , * newValuePtr
, * n
);
62 u_sprintf ( uStringBuf
, "Signed decimal integer i: %i " , * n
);
64 u_sscanf ( uStringBuf
, "Signed decimal integer i: %i " , newValuePtr
);
65 if (* n
!= * newValuePtr
) {
66 log_err ( "% %i Got: %i , Expected: %i \n " , * newValuePtr
, * n
);
69 u_sprintf ( uStringBuf
, "Unsigned octal integer o: %o " , * n
);
71 u_sscanf ( uStringBuf
, "Unsigned octal integer o: %o " , newValuePtr
);
72 if (* n
!= * newValuePtr
) {
73 log_err ( "% %o Got: %o , Expected: %o \n " , * newValuePtr
, * n
);
76 u_sprintf ( uStringBuf
, "Unsigned decimal integer % %u : %u " , * n
);
78 u_sscanf ( uStringBuf
, "Unsigned decimal integer % %u : %u " , newValuePtr
);
79 if (* n
!= * newValuePtr
) {
80 log_err ( "% %u Got: %u , Expected: %u \n " , * newValuePtr
, * n
);
83 u_sprintf ( uStringBuf
, "Lowercase unsigned hexadecimal integer x: %x " , * n
);
85 u_sscanf ( uStringBuf
, "Lowercase unsigned hexadecimal integer x: %x " , newValuePtr
);
86 if (* n
!= * newValuePtr
) {
87 log_err ( "% %x Got: %x , Expected: %x \n " , * newValuePtr
, * n
);
90 u_sprintf ( uStringBuf
, "Uppercase unsigned hexadecimal integer X: %X " , * n
);
92 u_sscanf ( uStringBuf
, "Uppercase unsigned hexadecimal integer X: %X " , newValuePtr
);
93 if (* n
!= * newValuePtr
) {
94 log_err ( "% %X Got: %X , Expected: %X \n " , * newValuePtr
, * n
);
97 u_sprintf ( uStringBuf
, "Float f: %f " , myFloat
);
98 * newDoubleValuePtr
= - 1.0 ;
99 u_sscanf ( uStringBuf
, "Float f: %l f" , newDoubleValuePtr
);
100 if ( myFloat
!= * newDoubleValuePtr
) {
101 log_err ( "% %f Got: %f , Expected: %f \n " , * newDoubleValuePtr
, myFloat
);
104 u_sprintf ( uStringBuf
, "Lowercase float e: %e " , myFloat
);
105 * newDoubleValuePtr
= - 1.0 ;
106 u_sscanf ( uStringBuf
, "Lowercase float e: %l e" , newDoubleValuePtr
);
107 if ( myFloat
!= * newDoubleValuePtr
) {
108 log_err ( "% %e Got: %e , Expected: %e \n " , * newDoubleValuePtr
, myFloat
);
111 u_sprintf ( uStringBuf
, "Uppercase float E: %E " , myFloat
);
112 * newDoubleValuePtr
= - 1.0 ;
113 u_sscanf ( uStringBuf
, "Uppercase float E: %l E" , newDoubleValuePtr
);
114 if ( myFloat
!= * newDoubleValuePtr
) {
115 log_err ( "% %E Got: %E , Expected: %E \n " , * newDoubleValuePtr
, myFloat
);
118 u_sprintf ( uStringBuf
, "Lowercase float g: %g " , myFloat
);
119 * newDoubleValuePtr
= - 1.0 ;
120 u_sscanf ( uStringBuf
, "Lowercase float g: %l g" , newDoubleValuePtr
);
121 if ( myFloat
!= * newDoubleValuePtr
) {
122 log_err ( "% %g Got: %g , Expected: %g \n " , * newDoubleValuePtr
, myFloat
);
125 u_sprintf ( uStringBuf
, "Uppercase float G: %G " , myFloat
);
126 * newDoubleValuePtr
= - 1.0 ;
127 u_sscanf ( uStringBuf
, "Uppercase float G: %l G" , newDoubleValuePtr
);
128 if ( myFloat
!= * newDoubleValuePtr
) {
129 log_err ( "% %G Got: %G , Expected: %G \n " , * newDoubleValuePtr
, myFloat
);
133 u_sprintf ( uStringBuf
, "Pointer % %p : %p \n " , origPtr
);
134 u_sscanf ( uStringBuf
, "Pointer % %p : %p \n " , & ptr
);
135 if ( ptr
!= origPtr
|| u_strlen ( uStringBuf
) != 13 +( sizeof ( void *)* 2 )) {
136 log_err ( "% %p Got: %p , Expected: %p \n " , ptr
, origPtr
);
139 u_sprintf ( uStringBuf
, "Char c: %c " , 'A' );
140 u_sscanf ( uStringBuf
, "Char c: %c " , myString
);
141 if (* myString
!= 'A' ) {
142 log_err ( "% %c Got: %c , Expected: A \n " , * myString
);
145 u_sprintf ( uStringBuf
, "UChar % %C : %C " , ( UChar
) 0x0041 ); /*'A'*/
146 u_sscanf ( uStringBuf
, "UChar % %C : %C " , myUString
);
147 if (* myUString
!= ( UChar
) 0x0041 ) { /*'A'*/
148 log_err ( "% %C Got: %C , Expected: A \n " , * myUString
);
151 u_sprintf ( uStringBuf
, "String % %s : %s " , "My-String" );
152 u_sscanf ( uStringBuf
, "String % %s : %s " , myString
);
153 if ( strcmp ( myString
, "My-String" )) {
154 log_err ( "% %s Got: %s , Expected: My-String \n " , myString
);
156 if ( uStringBuf
[ 20 ] != 0 ) {
157 log_err ( "String not terminated. Got %c \n " , uStringBuf
[ 20 ] );
159 u_sprintf ( uStringBuf
, "NULL String % %s : %s " , NULL
);
160 u_sscanf ( uStringBuf
, "NULL String % %s : %s " , myString
);
161 if ( strcmp ( myString
, "(null)" )) {
162 log_err ( "% %s Got: %s , Expected: My-String \n " , myString
);
165 u_sprintf ( uStringBuf
, "Unicode String % %S : %S " , myStringOrig
);
166 u_sscanf ( uStringBuf
, "Unicode String % %S : %S " , myUString
);
167 u_austrncpy ( myString
, myUString
, sizeof ( myString
)/ sizeof (* myString
));
168 if ( strcmp ( myString
, "My-String" )) {
169 log_err ( "% %S Got: %s , Expected: My String \n " , myString
);
172 u_sprintf ( uStringBuf
, "NULL Unicode String % %S : %S " , NULL
);
173 u_sscanf ( uStringBuf
, "NULL Unicode String % %S : %S " , myUString
);
174 u_austrncpy ( myString
, myUString
, sizeof ( myString
)/ sizeof (* myString
));
175 if ( strcmp ( myString
, "(null)" )) {
176 log_err ( "% %S Got: %s , Expected: (null) \n " , myString
);
179 u_sprintf ( uStringBuf
, "Percent % %P (non-ANSI): %P " , myFloat
);
180 * newDoubleValuePtr
= - 1.0 ;
181 u_sscanf ( uStringBuf
, "Percent % %P (non-ANSI): %P " , newDoubleValuePtr
);
182 if ( myFloat
!= * newDoubleValuePtr
) {
183 log_err ( "% %P Got: %P , Expected: %P \n " , * newDoubleValuePtr
, myFloat
);
186 u_sprintf ( uStringBuf
, "Spell Out % %V (non-ANSI): %V " , myFloat
);
187 * newDoubleValuePtr
= - 1.0 ;
188 u_sscanf ( uStringBuf
, "Spell Out % %V (non-ANSI): %V " , newDoubleValuePtr
);
189 if ( myFloat
!= * newDoubleValuePtr
) {
190 log_err ( "% %V Got: %f , Expected: %f \n " , * newDoubleValuePtr
, myFloat
);
194 u_sprintf ( uStringBuf
, " \t\n Pointer to integer (Count) % %n : n= %d %n n= %d \n " , * newValuePtr
, newValuePtr
, * newValuePtr
);
195 if (* newValuePtr
!= 37 ) {
196 log_err ( "% %V Got: %f , Expected: %f \n " , * newDoubleValuePtr
, myFloat
);
199 /* u_sscanf(uStringBuf, "Pointer %%p: %p\n", myFile);*/
202 static const char longStr
[] = "This is a long test12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" ;
204 retVal
= u_sprintf ( uStringBuf
, longStr
);
205 u_austrncpy ( myString
, uStringBuf
, sizeof ( uStringBuf
)/ sizeof (* uStringBuf
));
206 if ( strcmp ( myString
, longStr
)) {
207 log_err ( "% %S Got: %s , Expected: %s \n " , myString
, longStr
);
209 if ( retVal
!= ( int32_t ) strlen ( longStr
)) {
210 log_err ( "% %S returned different sizes. Got: %d Expected: %d \n " , retVal
, strlen ( longStr
));
213 retVal
= u_sprintf ( uStringBuf
, " %s " , longStr
);
214 u_austrncpy ( myString
, uStringBuf
, sizeof ( uStringBuf
)/ sizeof (* uStringBuf
));
215 if ( strcmp ( myString
, longStr
)) {
216 log_err ( "% %S Got: %s , Expected: %s \n " , myString
, longStr
);
218 if ( retVal
!= ( int32_t ) strlen ( longStr
)) {
219 log_err ( "% %S returned different sizes. Got: %d Expected: %d \n " , retVal
, strlen ( longStr
));
222 u_uastrncpy ( myUString
, longStr
, sizeof ( longStr
)/ sizeof (* longStr
));
223 u_sprintf_u ( uStringBuf
, myUString
);
224 if ( u_strcmp ( myUString
, uStringBuf
)) {
225 log_err ( "% %S Long strings differ. Expected: %s \n " , longStr
);
228 u_uastrncpy ( myUString
, longStr
, sizeof ( longStr
)/ sizeof (* longStr
));
229 retVal
= u_sprintf_u ( uStringBuf
, myUString
+ 10 );
230 if ( u_strcmp ( myUString
+ 10 , uStringBuf
)) {
231 log_err ( "% %S Long strings differ. Expected: %s \n " , longStr
+ 10 );
233 if ( retVal
!= ( int32_t ) strlen ( longStr
+ 10 )) {
234 log_err ( "% %S returned different sizes. Got: %d Expected: %d \n " , retVal
, strlen ( longStr
));
237 u_memset ( uStringBuf
, 1 , sizeof ( longStr
)/ sizeof (* longStr
));
238 u_uastrncpy ( myUString
, longStr
, sizeof ( longStr
)/ sizeof (* longStr
));
239 retVal
= u_snprintf_u ( uStringBuf
, 10 , myUString
);
240 if ( u_strncmp ( myUString
, uStringBuf
, 10 ) || uStringBuf
[ 10 ] != 1 || retVal
!= 10 ) {
241 log_err ( "% %S Long strings differ. Expected the first 10 characters of %s \n " , longStr
);
247 static void TestLocalizedString ( void ) {
248 #if !UCONFIG_NO_FORMATTING
252 int32_t numResult
= - 1 ;
254 UFILE
* strFile
= u_fstropen ( testStr
, sizeof ( testStr
)/ sizeof ( testStr
[ 0 ]), "en_US" );
257 log_err ( "u_fstropen failed to work \n " );
260 u_fprintf ( strFile
, " %d " , 1234 );
262 u_fscanf ( strFile
, " %d " , & numResult
);
263 u_uastrcpy ( uBuffer
, "1,234" );
264 u_austrcpy ( cBuffer
, testStr
);
265 if ( u_strcmp ( testStr
, uBuffer
) != 0 ) {
266 log_err ( "u_fprintf failed to work on an en string Got: %s \n " , cBuffer
);
268 if ( numResult
!= 1234 ) {
269 log_err ( "u_fscanf failed to work on an en string Got: %d \n " , numResult
);
273 locale
= u_fgetlocale ( strFile
);
274 if ( locale
== NULL
|| strcmp ( locale
, "en_US" ) != 0 ) {
275 log_err ( "u_fgetlocale didn't return \" en \" Got: %d \n " , u_fgetlocale ( strFile
));
277 u_fsetlocale ( strFile
, "de_DE" );
278 locale
= u_fgetlocale ( strFile
);
279 if ( locale
== NULL
|| strcmp ( locale
, "de_DE" ) != 0 ) {
280 log_err ( "u_fgetlocale didn't return \" de \" Got: %d \n " , u_fgetlocale ( strFile
));
283 u_fprintf ( strFile
, " %d " , 1234 );
286 u_fscanf ( strFile
, " %d " , & numResult
);
288 u_uastrcpy ( uBuffer
, "1.234" );
289 u_austrcpy ( cBuffer
, testStr
);
290 if ( u_strcmp ( testStr
, uBuffer
) != 0 ) {
291 log_err ( "u_fprintf failed to work on a de string Got: %s \n " , cBuffer
);
293 if ( numResult
!= 1234 ) {
294 log_err ( "u_fscanf failed to work on a de string Got: %d \n " , numResult
);
297 strFile
= u_fstropen ( testStr
, sizeof ( testStr
)/ sizeof ( testStr
[ 0 ]), NULL
);
298 u_fprintf ( strFile
, " %d " , 1234 );
301 u_fscanf ( strFile
, " %d " , & numResult
);
303 if ( numResult
!= 1234 ) {
304 log_err ( "u_fscanf failed to work on a default locale string Got: %d , Expected: 1234 \n " , numResult
);
306 if ( u_fstropen ( testStr
, - 1 , NULL
) != NULL
) {
307 log_err ( "u_fstropen returned a UFILE* on a negative buffer size \n " , numResult
);
312 #if !UCONFIG_NO_FORMATTING
313 #define Test_u_snprintf(limit, format, value, expectedSize, expectedStr) \
314 u_uastrncpy(testStr, "xxxxxxxxxxxxxx" , sizeof(testStr)/sizeof(testStr[0]));\
315 size = u_snprintf(testStr, limit, format, value);\
316 u_austrncpy(cTestResult, testStr, sizeof(cTestResult)/sizeof(cTestResult[0]));\
317 if (size != expectedSize || strcmp(cTestResult, expectedStr) != 0) {\
318 log_err( "Unexpected formatting. size= %d expectedSize= %d cTestResult= %s expectedStr= %s \n " ,\
319 size, expectedSize, cTestResult, expectedStr);\
322 log_verbose( "Got: %s \n " , cTestResult);\
327 static void TestSnprintf ( void ) {
328 #if !UCONFIG_NO_FORMATTING
330 char cTestResult
[ 256 ];
333 Test_u_snprintf ( 0 , " %d " , 123 , 3 , "xxxxxxxxxxxxxx" );
334 Test_u_snprintf ( 2 , " %d " , 123 , 3 , "12xxxxxxxxxxxx" );
335 Test_u_snprintf ( 3 , " %d " , 123 , 3 , "123xxxxxxxxxxx" );
336 Test_u_snprintf ( 4 , " %d " , 123 , 3 , "123" );
338 Test_u_snprintf ( 0 , " %s " , "abcd" , 4 , "xxxxxxxxxxxxxx" );
339 Test_u_snprintf ( 3 , " %s " , "abcd" , 4 , "abcxxxxxxxxxxx" );
340 Test_u_snprintf ( 4 , " %s " , "abcd" , 4 , "abcdxxxxxxxxxx" );
341 Test_u_snprintf ( 5 , " %s " , "abcd" , 4 , "abcd" );
343 Test_u_snprintf ( 0 , " %e " , 12.34 , 13 , "xxxxxxxxxxxxxx" );
344 Test_u_snprintf ( 1 , " %e " , 12.34 , 13 , "1xxxxxxxxxxxxx" );
345 Test_u_snprintf ( 2 , " %e " , 12.34 , 13 , "1.xxxxxxxxxxxx" );
346 Test_u_snprintf ( 3 , " %e " , 12.34 , 13 , "1.2xxxxxxxxxxx" );
347 Test_u_snprintf ( 5 , " %e " , 12.34 , 13 , "1.234xxxxxxxxx" );
348 Test_u_snprintf ( 6 , " %e " , 12.34 , 13 , "1.2340xxxxxxxx" );
349 Test_u_snprintf ( 8 , " %e " , 12.34 , 13 , "1.234000xxxxxx" );
350 Test_u_snprintf ( 9 , " %e " , 12.34 , 13 , "1.234000exxxxx" );
351 Test_u_snprintf ( 10 , " %e " , 12.34 , 13 , "1.234000e+xxxx" );
352 Test_u_snprintf ( 11 , " %e " , 12.34 , 13 , "1.234000e+0xxx" );
353 Test_u_snprintf ( 13 , " %e " , 12.34 , 13 , "1.234000e+001x" );
354 Test_u_snprintf ( 14 , " %e " , 12.34 , 13 , "1.234000e+001" );
358 #define TestSPrintFormat(uFormat, uValue, cFormat, cValue) \
359 /* Reinitialize the buffer to verify null termination works. */ \
360 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
361 memset(buffer, '*' , sizeof(buffer)/sizeof(*buffer));\
363 uNumPrinted = u_sprintf(uBuffer, uFormat, uValue);\
364 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(uBuffer[0]));\
365 cNumPrinted = sprintf(buffer, cFormat, cValue);\
366 if (strcmp(buffer, compBuffer) != 0) {\
367 log_err( "%" uFormat " Got: \" %s \" , Expected: \" %s \"\n " , compBuffer, buffer);\
369 if (cNumPrinted != uNumPrinted) {\
370 log_err( "%" uFormat " number printed Got: %d , Expected: %d \n " , uNumPrinted, cNumPrinted);\
372 if (buffer[uNumPrinted+1] != '*' ) {\
373 log_err( "%" uFormat " too much stored \n " );\
376 static void TestSprintfFormat(void) {
377 #if !UCONFIG_NO_FORMATTING
378 static const UChar abcUChars
[] = { 0x61 , 0x62 , 0x63 , 0 };
379 static const char abcChars
[] = "abc" ;
380 const char * reorderFormat
= " %2 $d==> %1 $-10.10s %6 $lld %4 $-10.10s %3 $#x(( %5 $d" ; /* reordering test*/
381 const char * reorderResult
= "99==>truncateif 1311768467463790322 1234567890 0xf1b93((10" ;
384 char compBuffer
[ 256 ];
389 TestSPrintFormat ( " %8 S" , abcUChars
, " %8 s" , abcChars
);
390 TestSPrintFormat ( "%-8S" , abcUChars
, "%-8s" , abcChars
);
391 TestSPrintFormat ( "%.2S" , abcUChars
, "%.2s" , abcChars
); /* strlen is 3 */
393 TestSPrintFormat ( " %8 s" , abcChars
, " %8 s" , abcChars
);
394 TestSPrintFormat ( "%-8s" , abcChars
, "%-8s" , abcChars
);
395 TestSPrintFormat ( "%.2s" , abcChars
, "%.2s" , abcChars
); /* strlen is 3 */
397 TestSPrintFormat ( " %8 c" , ( char ) 'e' , " %8 c" , ( char ) 'e' );
398 TestSPrintFormat ( "%-8c" , ( char ) 'e' , "%-8c" , ( char ) 'e' );
400 TestSPrintFormat ( " %8 C" , ( UChar
) 0x65 , " %8 c" , ( char ) 'e' );
401 TestSPrintFormat ( "%-8C" , ( UChar
) 0x65 , "%-8c" , ( char ) 'e' );
403 TestSPrintFormat ( " %f " , 1.23456789 , " %f " , 1.23456789 );
404 TestSPrintFormat ( " %f " , 12345.6789 , " %f " , 12345.6789 );
405 TestSPrintFormat ( " %f " , 123456.789 , " %f " , 123456.789 );
406 TestSPrintFormat ( " %f " , 1234567.89 , " %f " , 1234567.89 );
407 TestSPrintFormat ( " %1 0f" , 1.23456789 , " %1 0f" , 1.23456789 );
408 TestSPrintFormat ( "%-10f" , 1.23456789 , "%-10f" , 1.23456789 );
409 TestSPrintFormat ( " %1 0f" , 123.456789 , " %1 0f" , 123.456789 );
410 TestSPrintFormat ( " %1 0.4f" , 123.456789 , " %1 0.4f" , 123.456789 );
411 TestSPrintFormat ( "%-10f" , 123.456789 , "%-10f" , 123.456789 );
413 /* TestSPrintFormat("%g", 12345.6789, "%g", 12345.6789);
414 TestSPrintFormat("%g", 123456.789, "%g", 123456.789);
415 TestSPrintFormat("%g", 1234567.89, "%g", 1234567.89);
416 TestSPrintFormat("%G", 123456.789, "%G", 123456.789);
417 TestSPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
418 TestSPrintFormat ( " %1 0g" , 1.23456789 , " %1 0g" , 1.23456789 );
419 TestSPrintFormat ( " %1 0.4g" , 1.23456789 , " %1 0.4g" , 1.23456789 );
420 TestSPrintFormat ( "%-10g" , 1.23456789 , "%-10g" , 1.23456789 );
421 TestSPrintFormat ( " %1 0g" , 123.456789 , " %1 0g" , 123.456789 );
422 TestSPrintFormat ( "%-10g" , 123.456789 , "%-10g" , 123.456789 );
424 TestSPrintFormat ( " %8 x" , 123456 , " %8 x" , 123456 );
425 TestSPrintFormat ( "%-8x" , 123456 , "%-8x" , 123456 );
426 TestSPrintFormat ( " %0 8x" , 123456 , " %0 8x" , 123456 );
428 TestSPrintFormat ( " %8 X" , 123456 , " %8 X" , 123456 );
429 TestSPrintFormat ( "%-8X" , 123456 , "%-8X" , 123456 );
430 TestSPrintFormat ( " %0 8X" , 123456 , " %0 8X" , 123456 );
431 TestSPrintFormat ( "%#x" , 123456 , "%#x" , 123456 );
432 TestSPrintFormat ( "%#x" , - 123456 , "%#x" , - 123456 );
434 TestSPrintFormat ( " %8 o" , 123456 , " %8 o" , 123456 );
435 TestSPrintFormat ( "%-8o" , 123456 , "%-8o" , 123456 );
436 TestSPrintFormat ( " %0 8o" , 123456 , " %0 8o" , 123456 );
437 TestSPrintFormat ( "%#o" , 123 , "%#o" , 123 );
438 TestSPrintFormat ( "%#o" , - 123 , "%#o" , - 123 );
440 TestSPrintFormat ( " %8 u" , 123456 , " %8 u" , 123456 );
441 TestSPrintFormat ( "%-8u" , 123456 , "%-8u" , 123456 );
442 TestSPrintFormat ( " %0 8u" , 123456 , " %0 8u" , 123456 );
443 TestSPrintFormat ( " %8 u" , - 123456 , " %8 u" , - 123456 );
444 TestSPrintFormat ( "%-8u" , - 123456 , "%-8u" , - 123456 );
445 TestSPrintFormat ( "%.5u" , 123456 , "%.5u" , 123456 );
446 TestSPrintFormat ( "%.6u" , 123456 , "%.6u" , 123456 );
447 TestSPrintFormat ( "%.7u" , 123456 , "%.7u" , 123456 );
449 TestSPrintFormat ( " %8 d" , 123456 , " %8 d" , 123456 );
450 TestSPrintFormat ( "%-8d" , 123456 , "%-8d" , 123456 );
451 TestSPrintFormat ( " %0 8d" , 123456 , " %0 8d" , 123456 );
452 TestSPrintFormat ( "% d" , 123456 , "% d" , 123456 );
453 TestSPrintFormat ( "% d" , - 123456 , "% d" , - 123456 );
455 TestSPrintFormat ( " %8 i" , 123456 , " %8 i" , 123456 );
456 TestSPrintFormat ( "%-8i" , 123456 , "%-8i" , 123456 );
457 TestSPrintFormat ( " %0 8i" , 123456 , " %0 8i" , 123456 );
459 log_verbose ( "Get really crazy with the formatting. \n " );
461 TestSPrintFormat ( "%-#12x" , 123 , "%-#12x" , 123 );
462 TestSPrintFormat ( "%-#12x" , - 123 , "%-#12x" , - 123 );
463 TestSPrintFormat ( "%#12x" , 123 , "%#12x" , 123 );
464 TestSPrintFormat ( "%#12x" , - 123 , "%#12x" , - 123 );
466 TestSPrintFormat ( "%-+12d" , 123 , "%-+12d" , 123 );
467 TestSPrintFormat ( "%-+12d" , - 123 , "%-+12d" , - 123 );
468 TestSPrintFormat ( "%- 12d" , 123 , "%- 12d" , 123 );
469 TestSPrintFormat ( "%- 12d" , - 123 , "%- 12d" , - 123 );
470 TestSPrintFormat ( "%+12d" , 123 , "%+12d" , 123 );
471 TestSPrintFormat ( "%+12d" , - 123 , "%+12d" , - 123 );
472 TestSPrintFormat ( "% 12d" , 123 , "% 12d" , 123 );
473 TestSPrintFormat ( "% 12d" , - 123 , "% 12d" , - 123 );
474 TestSPrintFormat ( " %1 2d" , 123 , " %1 2d" , 123 );
475 TestSPrintFormat ( " %1 2d" , - 123 , " %1 2d" , - 123 );
476 TestSPrintFormat ( "%.12d" , 123 , "%.12d" , 123 );
477 TestSPrintFormat ( "%.12d" , - 123 , "%.12d" , - 123 );
479 TestSPrintFormat ( "%-+12.1f" , 1.234 , "%-+12.1f" , 1.234 );
480 TestSPrintFormat ( "%-+12.1f" , - 1.234 , "%-+12.1f" , - 1.234 );
481 TestSPrintFormat ( "%- 12.10f" , 1.234 , "%- 12.10f" , 1.234 );
482 TestSPrintFormat ( "%- 12.1f" , - 1.234 , "%- 12.1f" , - 1.234 );
483 TestSPrintFormat ( "%+12.1f" , 1.234 , "%+12.1f" , 1.234 );
484 TestSPrintFormat ( "%+12.1f" , - 1.234 , "%+12.1f" , - 1.234 );
485 TestSPrintFormat ( "% 12.1f" , 1.234 , "% 12.1f" , 1.234 );
486 TestSPrintFormat ( "% 12.1f" , - 1.234 , "% 12.1f" , - 1.234 );
487 TestSPrintFormat ( " %1 2.1f" , 1.234 , " %1 2.1f" , 1.234 );
488 TestSPrintFormat ( " %1 2.1f" , - 1.234 , " %1 2.1f" , - 1.234 );
489 TestSPrintFormat ( "%.2f" , 1.234 , "%.2f" , 1.234 );
490 TestSPrintFormat ( "%.2f" , - 1.234 , "%.2f" , - 1.234 );
491 TestSPrintFormat ( " %3 f" , 1.234 , " %3 f" , 1.234 );
492 TestSPrintFormat ( " %3 f" , - 1.234 , " %3 f" , - 1.234 );
494 /* Test reordering format */
495 u_sprintf ( uBuffer
, reorderFormat
, "truncateiftoolong" , 99 , 990099 , "12345678901234567890" , 10 , 0x123456789abcdef2 LL
);
496 u_austrncpy ( compBuffer
, uBuffer
, sizeof ( uBuffer
)/ sizeof ( uBuffer
[ 0 ]));
498 if ( strcmp ( compBuffer
, reorderResult
) != 0 ) {
499 log_err ( " %s Got: \" %s \" , Expected: \" %s \"\n " , reorderFormat
, compBuffer
, buffer
);
504 #undef TestSPrintFormat
506 static void TestStringCompatibility ( void ) {
507 #if !UCONFIG_NO_FORMATTING
508 UChar myUString
[ 256 ];
509 UChar uStringBuf
[ 256 ];
510 char myString
[ 256 ] = "" ;
511 char testBuf
[ 256 ] = "" ;
514 u_memset ( myUString
, 0x0a , sizeof ( myUString
)/ sizeof (* myUString
));
515 u_memset ( uStringBuf
, 0x0a , sizeof ( uStringBuf
) / sizeof (* uStringBuf
));
517 /* Compare against C API compatibility */
518 for ( num
= - STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
519 sprintf ( testBuf
, " %x " , ( int ) num
);
520 u_sprintf ( uStringBuf
, " %x " , num
);
521 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
522 if ( strcmp ( myString
, testBuf
) != 0 ) {
523 log_err ( "% %x Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
526 sprintf ( testBuf
, " %X " , ( int ) num
);
527 u_sprintf ( uStringBuf
, " %X " , num
);
528 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
529 if ( strcmp ( myString
, testBuf
) != 0 ) {
530 log_err ( "% %X Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
533 sprintf ( testBuf
, " %o " , ( int ) num
);
534 u_sprintf ( uStringBuf
, " %o " , num
);
535 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
536 if ( strcmp ( myString
, testBuf
) != 0 ) {
537 log_err ( "% %o Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
540 /* sprintf is not compatible on all platforms e.g. the iSeries*/
541 sprintf ( testBuf
, " %d " , ( int ) num
);
542 u_sprintf ( uStringBuf
, " %d " , num
);
543 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
544 if ( strcmp ( myString
, testBuf
) != 0 ) {
545 log_err ( "% %d Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
548 sprintf ( testBuf
, " %i " , ( int ) num
);
549 u_sprintf ( uStringBuf
, " %i " , num
);
550 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
551 if ( strcmp ( myString
, testBuf
) != 0 ) {
552 log_err ( "% %i Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
555 sprintf ( testBuf
, " %f " , ( double ) num
);
556 u_sprintf ( uStringBuf
, " %f " , ( double ) num
);
557 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
558 if ( strcmp ( myString
, testBuf
) != 0 ) {
559 log_err ( "% %f Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
562 /* sprintf(testBuf, "%e", (double)num);
563 u_sprintf(uStringBuf, "%e", (double)num);
564 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
565 if (strcmp(myString, testBuf) != 0) {
566 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
569 sprintf(testBuf, "%E", (double)num);
570 u_sprintf(uStringBuf, "%E", (double)num);
571 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
572 if (strcmp(myString, testBuf) != 0) {
573 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
576 sprintf ( testBuf
, " %g " , ( double ) num
);
577 u_sprintf ( uStringBuf
, " %g " , ( double ) num
);
578 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
579 if ( strcmp ( myString
, testBuf
) != 0 ) {
580 log_err ( "% %g Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
583 sprintf ( testBuf
, " %G " , ( double ) num
);
584 u_sprintf ( uStringBuf
, " %G " , ( double ) num
);
585 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
586 if ( strcmp ( myString
, testBuf
) != 0 ) {
587 log_err ( "% %G Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
591 for ( num
= 0 ; num
< 0x80 ; num
++) {
592 testBuf
[ 0 ] = ( char ) 0xFF ;
593 uStringBuf
[ 0 ] = ( UChar
) 0xfffe ;
594 sprintf ( testBuf
, " %c " , ( char ) num
);
595 u_sprintf ( uStringBuf
, " %c " , num
);
596 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
597 if ( testBuf
[ 0 ] != myString
[ 0 ] || myString
[ 0 ] != num
) {
598 log_err ( "% %c Got: 0x %x , Expected: 0x %x \n " , myString
[ 0 ], testBuf
[ 0 ]);
604 static void TestSScanSetFormat ( const char * format
, const UChar
* uValue
, const char * cValue
, UBool expectedToPass
) {
605 #if !UCONFIG_NO_FORMATTING
608 char compBuffer
[ 256 ];
612 /* Reinitialize the buffer to verify null termination works. */
613 u_memset ( uBuffer
, 0x2a , sizeof ( uBuffer
)/ sizeof (* uBuffer
));
614 uBuffer
[ sizeof ( uBuffer
)/ sizeof (* uBuffer
)- 1 ] = 0 ;
615 memset ( buffer
, '*' , sizeof ( buffer
)/ sizeof (* buffer
));
616 buffer
[ sizeof ( buffer
)/ sizeof (* buffer
)- 1 ] = 0 ;
618 uNumScanned
= u_sscanf ( uValue
, format
, uBuffer
);
619 if ( expectedToPass
) {
620 u_austrncpy ( compBuffer
, uBuffer
, sizeof ( uBuffer
)/ sizeof ( uBuffer
[ 0 ]));
621 cNumScanned
= sscanf ( cValue
, format
, buffer
);
622 if ( strncmp ( buffer
, compBuffer
, sizeof ( uBuffer
)/ sizeof ( uBuffer
[ 0 ])) != 0 ) {
623 log_err ( " %s Got: \" %s \" , Expected: \" %s \"\n " , format
, compBuffer
, buffer
);
625 if ( cNumScanned
!= uNumScanned
) {
626 log_err ( " %s number scanned Got: %d , Expected: %d \n " , format
, uNumScanned
, cNumScanned
);
628 if ( uNumScanned
> 0 && uBuffer
[ u_strlen ( uBuffer
)+ 1 ] != 0x2a ) {
629 log_err ( " %s too much stored \n " , format
);
633 if ( uNumScanned
!= 0 || uBuffer
[ 0 ] != 0x2a || uBuffer
[ 1 ] != 0x2a ) {
634 log_err ( " %s too much stored on a failure \n " , format
);
640 static void TestSScanset ( void ) {
641 #if !UCONFIG_NO_FORMATTING
642 static const UChar abcUChars
[] = { 0x61 , 0x62 , 0x63 , 0x63 , 0x64 , 0x65 , 0x66 , 0x67 , 0 };
643 static const char abcChars
[] = "abccdefg" ;
645 TestSScanSetFormat ( "%[bc]S" , abcUChars
, abcChars
, TRUE
);
646 TestSScanSetFormat ( "%[cb]S" , abcUChars
, abcChars
, TRUE
);
648 TestSScanSetFormat ( "%[ab]S" , abcUChars
, abcChars
, TRUE
);
649 TestSScanSetFormat ( "%[ba]S" , abcUChars
, abcChars
, TRUE
);
651 TestSScanSetFormat ( "%[ab]" , abcUChars
, abcChars
, TRUE
);
652 TestSScanSetFormat ( "%[ba]" , abcUChars
, abcChars
, TRUE
);
654 TestSScanSetFormat ( "%[abcdefgh]" , abcUChars
, abcChars
, TRUE
);
655 TestSScanSetFormat ( "%[;hgfedcba]" , abcUChars
, abcChars
, TRUE
);
657 TestSScanSetFormat ( "%[^a]" , abcUChars
, abcChars
, TRUE
);
658 TestSScanSetFormat ( "%[^e]" , abcUChars
, abcChars
, TRUE
);
659 TestSScanSetFormat ( "%[^ed]" , abcUChars
, abcChars
, TRUE
);
660 TestSScanSetFormat ( "%[^dc]" , abcUChars
, abcChars
, TRUE
);
661 TestSScanSetFormat ( "%[^e] " , abcUChars
, abcChars
, TRUE
);
663 TestSScanSetFormat ( " %1 [ab] " , abcUChars
, abcChars
, TRUE
);
664 TestSScanSetFormat ( " %2 [^f]" , abcUChars
, abcChars
, TRUE
);
666 TestSScanSetFormat ( "%[qrst]" , abcUChars
, abcChars
, TRUE
);
668 /* Extra long string for testing */
669 TestSScanSetFormat ( " %[qrst]" ,
670 abcUChars
, abcChars
, TRUE
);
672 TestSScanSetFormat ( "%[a-]" , abcUChars
, abcChars
, TRUE
);
675 TestSScanSetFormat ( "%[a" , abcUChars
, abcChars
, FALSE
);
676 TestSScanSetFormat ( "%[f-a]" , abcUChars
, abcChars
, FALSE
);
677 TestSScanSetFormat ( "%[c-a]" , abcUChars
, abcChars
, FALSE
);
678 /* The following is not deterministic on Windows */
679 /* TestSScanSetFormat("%[a-", abcUChars, abcChars);*/
681 /* TODO: Need to specify precision with a "*" */
685 static void TestBadSScanfFormat ( const char * format
, const UChar
* uValue
, const char * cValue
) {
686 #if !UCONFIG_NO_FORMATTING
690 /* Reinitialize the buffer to verify null termination works. */
691 u_memset ( uBuffer
, 0x2a , sizeof ( uBuffer
)/ sizeof (* uBuffer
));
692 uBuffer
[ sizeof ( uBuffer
)/ sizeof (* uBuffer
)- 1 ] = 0 ;
694 uNumScanned
= u_sscanf ( uValue
, format
, uBuffer
);
695 if ( uNumScanned
!= 0 || uBuffer
[ 0 ] != 0x2a || uBuffer
[ 1 ] != 0x2a ) {
696 log_err ( " %s too much stored on a failure \n " , format
);
701 static void TestBadScanfFormat ( void ) {
702 #if !UCONFIG_NO_FORMATTING
703 static const UChar abcUChars
[] = { 0x61 , 0x62 , 0x63 , 0x63 , 0x64 , 0x65 , 0x66 , 0x67 , 0 };
704 static const char abcChars
[] = "abccdefg" ;
706 TestBadSScanfFormat ( "%[] " , abcUChars
, abcChars
);
710 static void Test_u_vfprintf ( const char * expectedResult
, const char * format
, ...) {
711 #if !UCONFIG_NO_FORMATTING
717 va_start ( ap
, format
);
718 count
= u_vsprintf ( uBuffer
, format
, ap
);
720 u_uastrcpy ( uBuffer2
, expectedResult
);
721 if ( u_strcmp ( uBuffer
, uBuffer2
) != 0 ) {
722 log_err ( "Got two different results for \" %s \" expected \" %s \"\n " , format
, expectedResult
);
725 u_uastrcpy ( uBuffer2
, format
);
726 va_start ( ap
, format
);
727 count
= u_vsprintf_u ( uBuffer
, uBuffer2
, 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
);
736 static void TestVargs ( void ) {
737 #if !UCONFIG_NO_FORMATTING
738 Test_u_vfprintf ( "8 9 a B 8.9" , " %d %u %x %X %.1f" , 8 , 9 , 10 , 11 , 8.9 );
742 static void TestCount ( void ) {
743 #if !UCONFIG_NO_FORMATTING
744 static const UChar x15
[] = { 0x78 , 0x31 , 0x35 , 0 };
748 int32_t i32
= - 1 , actual_count
, actual_result
;
750 u_uastrcpy ( testStr
, "1233456789" );
751 if ( u_sscanf ( testStr
, "%*3[123] %n %*[1-9]" , & i32
) != 0 ) {
752 log_err ( "test 1: scanf did not return 0 \n " );
755 log_err ( "test 1: scanf returned %h d instead of 3 \n " , i32
);
757 if ( u_sscanf ( testStr
, "%*4[123] %h n%*[1-9]" , & i16
) != 0 ) {
758 log_err ( "test 2: scanf did not return 0 \n " );
761 log_err ( "test 2: scanf returned %d instead of 4 \n " , i16
);
763 if ( u_sscanf ( testStr
, "%*[123]%*[1-9] %l ln" , & i64
) != 0 ) {
764 log_err ( "test 3: scanf did not return 0 \n " );
767 log_err ( "test 3: scanf did not return 10 \n " , i64
);
769 actual_result
= u_sscanf ( x15
, " %C%d%n " , & character
, & i32
, & actual_count
);
770 if ( actual_result
!= 2 ) {
771 log_err ( "scanf should return 2, but returned %d \n " , actual_result
);
773 if ( character
!= 0x78 ) {
774 log_err ( "scanf should return 0x78 for the character, but returned %X \n " , character
);
777 log_err ( "scanf should return 15 for the number, but returned %d \n " , i32
);
779 if ( actual_count
!= 3 ) {
780 log_err ( "scanf should return 3 for actual_count, but returned %d \n " , actual_count
);
786 addStringTest ( TestNode
** root
) {
787 #if !UCONFIG_NO_FORMATTING
788 addTest ( root
, & TestString
, "string/TestString" );
789 addTest ( root
, & TestLocalizedString
, "string/TestLocalizedString" );
790 addTest ( root
, & TestSprintfFormat
, "string/TestSprintfFormat" );
791 addTest ( root
, & TestSnprintf
, "string/TestSnprintf" );
792 addTest ( root
, & TestSScanset
, "string/TestSScanset" );
793 addTest ( root
, & TestStringCompatibility
, "string/TestStringCompatibility" );
794 addTest ( root
, & TestBadScanfFormat
, "string/TestBadScanfFormat" );
795 addTest ( root
, & TestVargs
, "string/TestVargs" );
796 addTest ( root
, & TestCount
, "string/TestCount" );