]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/iotest/strtst.c
2 **********************************************************************
3 * Copyright (C) 2004-2006, 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 , 0 , "xxxxxxxxxxxxxx" );
334 Test_u_snprintf ( 2 , " %d " , 123 , 2 , "12xxxxxxxxxxxx" );
335 Test_u_snprintf ( 3 , " %d " , 123 , 3 , "123xxxxxxxxxxx" );
336 Test_u_snprintf ( 4 , " %d " , 123 , 3 , "123" );
338 Test_u_snprintf ( 0 , " %s " , "abcd" , 0 , "xxxxxxxxxxxxxx" );
339 Test_u_snprintf ( 3 , " %s " , "abcd" , 3 , "abcxxxxxxxxxxx" );
340 Test_u_snprintf ( 4 , " %s " , "abcd" , 4 , "abcdxxxxxxxxxx" );
341 Test_u_snprintf ( 5 , " %s " , "abcd" , 4 , "abcd" );
343 Test_u_snprintf ( 0 , " %e " , 12.34 , 0 , "xxxxxxxxxxxxxx" );
344 Test_u_snprintf ( 1 , " %e " , 12.34 , 1 , "1xxxxxxxxxxxxx" );
345 Test_u_snprintf ( 2 , " %e " , 12.34 , 2 , "1.xxxxxxxxxxxx" );
346 Test_u_snprintf ( 3 , " %e " , 12.34 , 3 , "1.2xxxxxxxxxxx" );
347 Test_u_snprintf ( 5 , " %e " , 12.34 , 5 , "1.234xxxxxxxxx" );
348 Test_u_snprintf ( 6 , " %e " , 12.34 , 6 , "1.2340xxxxxxxx" );
349 Test_u_snprintf ( 8 , " %e " , 12.34 , 8 , "1.234000xxxxxx" );
350 Test_u_snprintf ( 9 , " %e " , 12.34 , 9 , "1.234000exxxxx" );
351 Test_u_snprintf ( 10 , " %e " , 12.34 , 10 , "1.234000e+xxxx" );
352 Test_u_snprintf ( 11 , " %e " , 12.34 , 11 , "1.234000e+0xxx" );
353 Test_u_snprintf ( 13 , " %e " , 12.34 , 13 , "1.234000e+001x" );
354 Test_u_snprintf ( 14 , " %e " , 12.34 , 13 , "1.234000e+001" );
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" ;
382 char compBuffer
[ 256 ];
387 TestSPrintFormat ( " %8 S" , abcUChars
, " %8 s" , abcChars
);
388 TestSPrintFormat ( "%-8S" , abcUChars
, "%-8s" , abcChars
);
389 TestSPrintFormat ( "%.2S" , abcUChars
, "%.2s" , abcChars
); /* strlen is 3 */
391 TestSPrintFormat ( " %8 s" , abcChars
, " %8 s" , abcChars
);
392 TestSPrintFormat ( "%-8s" , abcChars
, "%-8s" , abcChars
);
393 TestSPrintFormat ( "%.2s" , abcChars
, "%.2s" , abcChars
); /* strlen is 3 */
395 TestSPrintFormat ( " %8 c" , ( char ) 'e' , " %8 c" , ( char ) 'e' );
396 TestSPrintFormat ( "%-8c" , ( char ) 'e' , "%-8c" , ( char ) 'e' );
398 TestSPrintFormat ( " %8 C" , ( UChar
) 0x65 , " %8 c" , ( char ) 'e' );
399 TestSPrintFormat ( "%-8C" , ( UChar
) 0x65 , "%-8c" , ( char ) 'e' );
401 TestSPrintFormat ( " %f " , 1.23456789 , " %f " , 1.23456789 );
402 TestSPrintFormat ( " %f " , 12345.6789 , " %f " , 12345.6789 );
403 TestSPrintFormat ( " %f " , 123456.789 , " %f " , 123456.789 );
404 TestSPrintFormat ( " %f " , 1234567.89 , " %f " , 1234567.89 );
405 TestSPrintFormat ( " %1 0f" , 1.23456789 , " %1 0f" , 1.23456789 );
406 TestSPrintFormat ( "%-10f" , 1.23456789 , "%-10f" , 1.23456789 );
407 TestSPrintFormat ( " %1 0f" , 123.456789 , " %1 0f" , 123.456789 );
408 TestSPrintFormat ( " %1 0.4f" , 123.456789 , " %1 0.4f" , 123.456789 );
409 TestSPrintFormat ( "%-10f" , 123.456789 , "%-10f" , 123.456789 );
411 /* TestSPrintFormat("%g", 12345.6789, "%g", 12345.6789);
412 TestSPrintFormat("%g", 123456.789, "%g", 123456.789);
413 TestSPrintFormat("%g", 1234567.89, "%g", 1234567.89);
414 TestSPrintFormat("%G", 123456.789, "%G", 123456.789);
415 TestSPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
416 TestSPrintFormat ( " %1 0g" , 1.23456789 , " %1 0g" , 1.23456789 );
417 TestSPrintFormat ( " %1 0.4g" , 1.23456789 , " %1 0.4g" , 1.23456789 );
418 TestSPrintFormat ( "%-10g" , 1.23456789 , "%-10g" , 1.23456789 );
419 TestSPrintFormat ( " %1 0g" , 123.456789 , " %1 0g" , 123.456789 );
420 TestSPrintFormat ( "%-10g" , 123.456789 , "%-10g" , 123.456789 );
422 TestSPrintFormat ( " %8 x" , 123456 , " %8 x" , 123456 );
423 TestSPrintFormat ( "%-8x" , 123456 , "%-8x" , 123456 );
424 TestSPrintFormat ( " %0 8x" , 123456 , " %0 8x" , 123456 );
426 TestSPrintFormat ( " %8 X" , 123456 , " %8 X" , 123456 );
427 TestSPrintFormat ( "%-8X" , 123456 , "%-8X" , 123456 );
428 TestSPrintFormat ( " %0 8X" , 123456 , " %0 8X" , 123456 );
429 TestSPrintFormat ( "%#x" , 123456 , "%#x" , 123456 );
430 TestSPrintFormat ( "%#x" , - 123456 , "%#x" , - 123456 );
432 TestSPrintFormat ( " %8 o" , 123456 , " %8 o" , 123456 );
433 TestSPrintFormat ( "%-8o" , 123456 , "%-8o" , 123456 );
434 TestSPrintFormat ( " %0 8o" , 123456 , " %0 8o" , 123456 );
435 TestSPrintFormat ( "%#o" , 123 , "%#o" , 123 );
436 TestSPrintFormat ( "%#o" , - 123 , "%#o" , - 123 );
438 TestSPrintFormat ( " %8 u" , 123456 , " %8 u" , 123456 );
439 TestSPrintFormat ( "%-8u" , 123456 , "%-8u" , 123456 );
440 TestSPrintFormat ( " %0 8u" , 123456 , " %0 8u" , 123456 );
441 TestSPrintFormat ( " %8 u" , - 123456 , " %8 u" , - 123456 );
442 TestSPrintFormat ( "%-8u" , - 123456 , "%-8u" , - 123456 );
443 TestSPrintFormat ( "%.5u" , 123456 , "%.5u" , 123456 );
444 TestSPrintFormat ( "%.6u" , 123456 , "%.6u" , 123456 );
445 TestSPrintFormat ( "%.7u" , 123456 , "%.7u" , 123456 );
447 TestSPrintFormat ( " %8 d" , 123456 , " %8 d" , 123456 );
448 TestSPrintFormat ( "%-8d" , 123456 , "%-8d" , 123456 );
449 TestSPrintFormat ( " %0 8d" , 123456 , " %0 8d" , 123456 );
450 TestSPrintFormat ( "% d" , 123456 , "% d" , 123456 );
451 TestSPrintFormat ( "% d" , - 123456 , "% d" , - 123456 );
453 TestSPrintFormat ( " %8 i" , 123456 , " %8 i" , 123456 );
454 TestSPrintFormat ( "%-8i" , 123456 , "%-8i" , 123456 );
455 TestSPrintFormat ( " %0 8i" , 123456 , " %0 8i" , 123456 );
457 log_verbose ( "Get really crazy with the formatting. \n " );
459 TestSPrintFormat ( "%-#12x" , 123 , "%-#12x" , 123 );
460 TestSPrintFormat ( "%-#12x" , - 123 , "%-#12x" , - 123 );
461 TestSPrintFormat ( "%#12x" , 123 , "%#12x" , 123 );
462 TestSPrintFormat ( "%#12x" , - 123 , "%#12x" , - 123 );
464 TestSPrintFormat ( "%-+12d" , 123 , "%-+12d" , 123 );
465 TestSPrintFormat ( "%-+12d" , - 123 , "%-+12d" , - 123 );
466 TestSPrintFormat ( "%- 12d" , 123 , "%- 12d" , 123 );
467 TestSPrintFormat ( "%- 12d" , - 123 , "%- 12d" , - 123 );
468 TestSPrintFormat ( "%+12d" , 123 , "%+12d" , 123 );
469 TestSPrintFormat ( "%+12d" , - 123 , "%+12d" , - 123 );
470 TestSPrintFormat ( "% 12d" , 123 , "% 12d" , 123 );
471 TestSPrintFormat ( "% 12d" , - 123 , "% 12d" , - 123 );
472 TestSPrintFormat ( " %1 2d" , 123 , " %1 2d" , 123 );
473 TestSPrintFormat ( " %1 2d" , - 123 , " %1 2d" , - 123 );
474 TestSPrintFormat ( "%.12d" , 123 , "%.12d" , 123 );
475 TestSPrintFormat ( "%.12d" , - 123 , "%.12d" , - 123 );
477 TestSPrintFormat ( "%-+12.1f" , 1.234 , "%-+12.1f" , 1.234 );
478 TestSPrintFormat ( "%-+12.1f" , - 1.234 , "%-+12.1f" , - 1.234 );
479 TestSPrintFormat ( "%- 12.10f" , 1.234 , "%- 12.10f" , 1.234 );
480 TestSPrintFormat ( "%- 12.1f" , - 1.234 , "%- 12.1f" , - 1.234 );
481 TestSPrintFormat ( "%+12.1f" , 1.234 , "%+12.1f" , 1.234 );
482 TestSPrintFormat ( "%+12.1f" , - 1.234 , "%+12.1f" , - 1.234 );
483 TestSPrintFormat ( "% 12.1f" , 1.234 , "% 12.1f" , 1.234 );
484 TestSPrintFormat ( "% 12.1f" , - 1.234 , "% 12.1f" , - 1.234 );
485 TestSPrintFormat ( " %1 2.1f" , 1.234 , " %1 2.1f" , 1.234 );
486 TestSPrintFormat ( " %1 2.1f" , - 1.234 , " %1 2.1f" , - 1.234 );
487 TestSPrintFormat ( "%.2f" , 1.234 , "%.2f" , 1.234 );
488 TestSPrintFormat ( "%.2f" , - 1.234 , "%.2f" , - 1.234 );
489 TestSPrintFormat ( " %3 f" , 1.234 , " %3 f" , 1.234 );
490 TestSPrintFormat ( " %3 f" , - 1.234 , " %3 f" , - 1.234 );
494 #undef TestSPrintFormat
496 static void TestStringCompatibility ( void ) {
497 #if !UCONFIG_NO_FORMATTING
498 UChar myUString
[ 256 ];
499 UChar uStringBuf
[ 256 ];
500 char myString
[ 256 ] = "" ;
501 char testBuf
[ 256 ] = "" ;
504 u_memset ( myUString
, 0x0a , sizeof ( myUString
)/ sizeof (* myUString
));
505 u_memset ( uStringBuf
, 0x0a , sizeof ( uStringBuf
) / sizeof (* uStringBuf
));
507 /* Compare against C API compatibility */
508 for ( num
= - STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
509 sprintf ( testBuf
, " %x " , ( int ) num
);
510 u_sprintf ( uStringBuf
, " %x " , num
);
511 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
512 if ( strcmp ( myString
, testBuf
) != 0 ) {
513 log_err ( "% %x Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
516 sprintf ( testBuf
, " %X " , ( int ) num
);
517 u_sprintf ( uStringBuf
, " %X " , num
);
518 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
519 if ( strcmp ( myString
, testBuf
) != 0 ) {
520 log_err ( "% %X Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
523 sprintf ( testBuf
, " %o " , ( int ) num
);
524 u_sprintf ( uStringBuf
, " %o " , num
);
525 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
526 if ( strcmp ( myString
, testBuf
) != 0 ) {
527 log_err ( "% %o Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
530 /* sprintf is not compatible on all platforms e.g. the iSeries*/
531 sprintf ( testBuf
, " %d " , ( int ) num
);
532 u_sprintf ( uStringBuf
, " %d " , num
);
533 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
534 if ( strcmp ( myString
, testBuf
) != 0 ) {
535 log_err ( "% %d Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
538 sprintf ( testBuf
, " %i " , ( int ) num
);
539 u_sprintf ( uStringBuf
, " %i " , num
);
540 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
541 if ( strcmp ( myString
, testBuf
) != 0 ) {
542 log_err ( "% %i Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
545 sprintf ( testBuf
, " %f " , ( double ) num
);
546 u_sprintf ( uStringBuf
, " %f " , ( double ) num
);
547 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
548 if ( strcmp ( myString
, testBuf
) != 0 ) {
549 log_err ( "% %f Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
552 /* sprintf(testBuf, "%e", (double)num);
553 u_sprintf(uStringBuf, "%e", (double)num);
554 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
555 if (strcmp(myString, testBuf) != 0) {
556 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
559 sprintf(testBuf, "%E", (double)num);
560 u_sprintf(uStringBuf, "%E", (double)num);
561 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
562 if (strcmp(myString, testBuf) != 0) {
563 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
566 sprintf ( testBuf
, " %g " , ( double ) num
);
567 u_sprintf ( uStringBuf
, " %g " , ( double ) num
);
568 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
569 if ( strcmp ( myString
, testBuf
) != 0 ) {
570 log_err ( "% %g Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
573 sprintf ( testBuf
, " %G " , ( double ) num
);
574 u_sprintf ( uStringBuf
, " %G " , ( double ) num
);
575 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
576 if ( strcmp ( myString
, testBuf
) != 0 ) {
577 log_err ( "% %G Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
581 for ( num
= 0 ; num
< 0x80 ; num
++) {
582 testBuf
[ 0 ] = ( char ) 0xFF ;
583 uStringBuf
[ 0 ] = ( UChar
) 0xfffe ;
584 sprintf ( testBuf
, " %c " , ( char ) num
);
585 u_sprintf ( uStringBuf
, " %c " , num
);
586 u_austrncpy ( myString
, uStringBuf
, sizeof ( myString
)/ sizeof ( myString
[ 0 ]));
587 if ( testBuf
[ 0 ] != myString
[ 0 ] || myString
[ 0 ] != num
) {
588 log_err ( "% %c Got: 0x %x , Expected: 0x %x \n " , myString
[ 0 ], testBuf
[ 0 ]);
594 static void TestSScanSetFormat ( const char * format
, const UChar
* uValue
, const char * cValue
, UBool expectedToPass
) {
595 #if !UCONFIG_NO_FORMATTING
598 char compBuffer
[ 256 ];
602 /* Reinitialize the buffer to verify null termination works. */
603 u_memset ( uBuffer
, 0x2a , sizeof ( uBuffer
)/ sizeof (* uBuffer
));
604 uBuffer
[ sizeof ( uBuffer
)/ sizeof (* uBuffer
)- 1 ] = 0 ;
605 memset ( buffer
, '*' , sizeof ( buffer
)/ sizeof (* buffer
));
606 buffer
[ sizeof ( buffer
)/ sizeof (* buffer
)- 1 ] = 0 ;
608 uNumScanned
= u_sscanf ( uValue
, format
, uBuffer
);
609 if ( expectedToPass
) {
610 u_austrncpy ( compBuffer
, uBuffer
, sizeof ( uBuffer
)/ sizeof ( uBuffer
[ 0 ]));
611 cNumScanned
= sscanf ( cValue
, format
, buffer
);
612 if ( strncmp ( buffer
, compBuffer
, sizeof ( uBuffer
)/ sizeof ( uBuffer
[ 0 ])) != 0 ) {
613 log_err ( " %s Got: \" %s \" , Expected: \" %s \"\n " , format
, compBuffer
, buffer
);
615 if ( cNumScanned
!= uNumScanned
) {
616 log_err ( " %s number scanned Got: %d , Expected: %d \n " , format
, uNumScanned
, cNumScanned
);
618 if ( uNumScanned
> 0 && uBuffer
[ u_strlen ( uBuffer
)+ 1 ] != 0x2a ) {
619 log_err ( " %s too much stored \n " , format
);
623 if ( uNumScanned
!= 0 || uBuffer
[ 0 ] != 0x2a || uBuffer
[ 1 ] != 0x2a ) {
624 log_err ( " %s too much stored on a failure \n " , format
);
630 static void TestSScanset ( void ) {
631 #if !UCONFIG_NO_FORMATTING
632 static const UChar abcUChars
[] = { 0x61 , 0x62 , 0x63 , 0x63 , 0x64 , 0x65 , 0x66 , 0x67 , 0 };
633 static const char abcChars
[] = "abccdefg" ;
635 TestSScanSetFormat ( "%[bc]S" , abcUChars
, abcChars
, TRUE
);
636 TestSScanSetFormat ( "%[cb]S" , abcUChars
, abcChars
, TRUE
);
638 TestSScanSetFormat ( "%[ab]S" , abcUChars
, abcChars
, TRUE
);
639 TestSScanSetFormat ( "%[ba]S" , abcUChars
, abcChars
, TRUE
);
641 TestSScanSetFormat ( "%[ab]" , abcUChars
, abcChars
, TRUE
);
642 TestSScanSetFormat ( "%[ba]" , abcUChars
, abcChars
, TRUE
);
644 TestSScanSetFormat ( "%[abcdefgh]" , abcUChars
, abcChars
, TRUE
);
645 TestSScanSetFormat ( "%[;hgfedcba]" , abcUChars
, abcChars
, TRUE
);
647 TestSScanSetFormat ( "%[^a]" , abcUChars
, abcChars
, TRUE
);
648 TestSScanSetFormat ( "%[^e]" , abcUChars
, abcChars
, TRUE
);
649 TestSScanSetFormat ( "%[^ed]" , abcUChars
, abcChars
, TRUE
);
650 TestSScanSetFormat ( "%[^dc]" , abcUChars
, abcChars
, TRUE
);
651 TestSScanSetFormat ( "%[^e] " , abcUChars
, abcChars
, TRUE
);
653 TestSScanSetFormat ( " %1 [ab] " , abcUChars
, abcChars
, TRUE
);
654 TestSScanSetFormat ( " %2 [^f]" , abcUChars
, abcChars
, TRUE
);
656 TestSScanSetFormat ( "%[qrst]" , abcUChars
, abcChars
, TRUE
);
658 /* Extra long string for testing */
659 TestSScanSetFormat ( " %[qrst]" ,
660 abcUChars
, abcChars
, TRUE
);
662 TestSScanSetFormat ( "%[a-]" , abcUChars
, abcChars
, TRUE
);
665 TestSScanSetFormat ( "%[a" , abcUChars
, abcChars
, FALSE
);
666 TestSScanSetFormat ( "%[f-a]" , abcUChars
, abcChars
, FALSE
);
667 TestSScanSetFormat ( "%[c-a]" , abcUChars
, abcChars
, FALSE
);
668 /* The following is not deterministic on Windows */
669 /* TestSScanSetFormat("%[a-", abcUChars, abcChars);*/
671 /* TODO: Need to specify precision with a "*" */
675 static void TestBadSScanfFormat ( const char * format
, const UChar
* uValue
, const char * cValue
) {
676 #if !UCONFIG_NO_FORMATTING
680 /* Reinitialize the buffer to verify null termination works. */
681 u_memset ( uBuffer
, 0x2a , sizeof ( uBuffer
)/ sizeof (* uBuffer
));
682 uBuffer
[ sizeof ( uBuffer
)/ sizeof (* uBuffer
)- 1 ] = 0 ;
684 uNumScanned
= u_sscanf ( uValue
, format
, uBuffer
);
685 if ( uNumScanned
!= 0 || uBuffer
[ 0 ] != 0x2a || uBuffer
[ 1 ] != 0x2a ) {
686 log_err ( " %s too much stored on a failure \n " , format
);
691 static void TestBadScanfFormat ( void ) {
692 #if !UCONFIG_NO_FORMATTING
693 static const UChar abcUChars
[] = { 0x61 , 0x62 , 0x63 , 0x63 , 0x64 , 0x65 , 0x66 , 0x67 , 0 };
694 static const char abcChars
[] = "abccdefg" ;
696 TestBadSScanfFormat ( "%[] " , abcUChars
, abcChars
);
700 static void Test_u_vfprintf ( const char * expectedResult
, const char * format
, ...) {
701 #if !UCONFIG_NO_FORMATTING
707 va_start ( ap
, format
);
708 count
= u_vsprintf ( uBuffer
, format
, ap
);
710 u_uastrcpy ( uBuffer2
, expectedResult
);
711 if ( u_strcmp ( uBuffer
, uBuffer2
) != 0 ) {
712 log_err ( "Got two different results for \" %s \" expected \" %s \"\n " , format
, expectedResult
);
715 u_uastrcpy ( uBuffer2
, format
);
716 va_start ( ap
, format
);
717 count
= u_vsprintf_u ( uBuffer
, uBuffer2
, ap
);
719 u_uastrcpy ( uBuffer2
, expectedResult
);
720 if ( u_strcmp ( uBuffer
, uBuffer2
) != 0 ) {
721 log_err ( "Got two different results for \" %s \" expected \" %s \"\n " , format
, expectedResult
);
726 static void TestVargs ( void ) {
727 #if !UCONFIG_NO_FORMATTING
728 Test_u_vfprintf ( "8 9 a B 8.9" , " %d %u %x %X %.1f" , 8 , 9 , 10 , 11 , 8.9 );
732 static void TestCount ( void ) {
733 #if !UCONFIG_NO_FORMATTING
734 static const UChar x15
[] = { 0x78 , 0x31 , 0x35 , 0 };
738 int32_t i32
= - 1 , actual_count
, actual_result
;
740 u_uastrcpy ( testStr
, "1233456789" );
741 if ( u_sscanf ( testStr
, "%*3[123] %n %*[1-9]" , & i32
) != 0 ) {
742 log_err ( "test 1: scanf did not return 0 \n " );
745 log_err ( "test 1: scanf returned %h d instead of 3 \n " , i32
);
747 if ( u_sscanf ( testStr
, "%*4[123] %h n%*[1-9]" , & i16
) != 0 ) {
748 log_err ( "test 2: scanf did not return 0 \n " );
751 log_err ( "test 2: scanf returned %d instead of 4 \n " , i16
);
753 if ( u_sscanf ( testStr
, "%*[123]%*[1-9] %l ln" , & i64
) != 0 ) {
754 log_err ( "test 3: scanf did not return 0 \n " );
757 log_err ( "test 3: scanf did not return 10 \n " , i64
);
759 actual_result
= u_sscanf ( x15
, " %C%d%n " , & character
, & i32
, & actual_count
);
760 if ( actual_result
!= 2 ) {
761 log_err ( "scanf should return 2, but returned %d \n " , actual_result
);
763 if ( character
!= 0x78 ) {
764 log_err ( "scanf should return 0x78 for the character, but returned %X \n " , character
);
767 log_err ( "scanf should return 15 for the number, but returned %d \n " , i32
);
769 if ( actual_count
!= 3 ) {
770 log_err ( "scanf should return 3 for actual_count, but returned %d \n " , actual_count
);
776 addStringTest ( TestNode
** root
) {
777 #if !UCONFIG_NO_FORMATTING
778 addTest ( root
, & TestString
, "string/TestString" );
779 addTest ( root
, & TestLocalizedString
, "string/TestLocalizedString" );
780 addTest ( root
, & TestSprintfFormat
, "string/TestSprintfFormat" );
781 addTest ( root
, & TestSnprintf
, "string/TestSnprintf" );
782 addTest ( root
, & TestSScanset
, "string/TestSScanset" );
783 addTest ( root
, & TestStringCompatibility
, "string/TestStringCompatibility" );
784 addTest ( root
, & TestBadScanfFormat
, "string/TestBadScanfFormat" );
785 addTest ( root
, & TestVargs
, "string/TestVargs" );
786 addTest ( root
, & TestCount
, "string/TestCount" );