]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/iotest/strtst.c
2 **********************************************************************
3 * Copyright (C) 2004-2016, 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"
22 static void TestString ( void ) {
23 #if !UCONFIG_NO_FORMATTING
25 float myFloat
= - 1234.0 ;
26 int32_t newValuePtr
[ 1 ];
27 double newDoubleValuePtr
[ 1 ];
29 UChar uStringBuf
[ 512 ];
30 char myString
[ 512 ] = "" ;
33 U_STRING_DECL ( myStringOrig
, "My-String" , 9 );
35 U_STRING_INIT ( myStringOrig
, "My-String" , 9 );
36 u_memset ( myUString
, 0x0a , UPRV_LENGTHOF ( myUString
));
37 u_memset ( uStringBuf
, 0x0a , UPRV_LENGTHOF ( uStringBuf
));
40 if ( sizeof ( void *) == 4 ) {
41 origPtr
= ( void *) 0xdeadbeef ;
42 } else if ( sizeof ( void *) == 8 ) {
43 origPtr
= ( void *) INT64_C ( 0x1000200030004000 );
44 } else if ( sizeof ( void *) == 16 ) {
49 } massiveBigEndianPtr
= {{ 0x10002000 , 0x30004000 , 0x50006000 , 0x70008000 }};
50 origPtr
= massiveBigEndianPtr
. ptr
;
52 log_err ( "sizeof(void*)= %d hasn't been tested before" , ( int ) sizeof ( void *));
56 u_sprintf ( uStringBuf
, "Signed decimal integer d: %d " , * n
);
58 u_sscanf ( uStringBuf
, "Signed decimal integer d: %d " , newValuePtr
);
59 if (* n
!= * newValuePtr
) {
60 log_err ( "% %d Got: %d , Expected: %d \n " , * newValuePtr
, * n
);
63 u_sprintf ( uStringBuf
, "Signed decimal integer i: %i " , * n
);
65 u_sscanf ( uStringBuf
, "Signed decimal integer i: %i " , newValuePtr
);
66 if (* n
!= * newValuePtr
) {
67 log_err ( "% %i Got: %i , Expected: %i \n " , * newValuePtr
, * n
);
70 u_sprintf ( uStringBuf
, "Unsigned octal integer o: %o " , * n
);
72 u_sscanf ( uStringBuf
, "Unsigned octal integer o: %o " , newValuePtr
);
73 if (* n
!= * newValuePtr
) {
74 log_err ( "% %o Got: %o , Expected: %o \n " , * newValuePtr
, * n
);
77 u_sprintf ( uStringBuf
, "Unsigned decimal integer % %u : %u " , * n
);
79 u_sscanf ( uStringBuf
, "Unsigned decimal integer % %u : %u " , newValuePtr
);
80 if (* n
!= * newValuePtr
) {
81 log_err ( "% %u Got: %u , Expected: %u \n " , * newValuePtr
, * n
);
84 u_sprintf ( uStringBuf
, "Lowercase unsigned hexadecimal integer x: %x " , * n
);
86 u_sscanf ( uStringBuf
, "Lowercase unsigned hexadecimal integer x: %x " , newValuePtr
);
87 if (* n
!= * newValuePtr
) {
88 log_err ( "% %x Got: %x , Expected: %x \n " , * newValuePtr
, * n
);
91 u_sprintf ( uStringBuf
, "Uppercase unsigned hexadecimal integer X: %X " , * n
);
93 u_sscanf ( uStringBuf
, "Uppercase unsigned hexadecimal integer X: %X " , newValuePtr
);
94 if (* n
!= * newValuePtr
) {
95 log_err ( "% %X Got: %X , Expected: %X \n " , * newValuePtr
, * n
);
98 u_sprintf ( uStringBuf
, "Float f: %f " , myFloat
);
99 * newDoubleValuePtr
= - 1.0 ;
100 u_sscanf ( uStringBuf
, "Float f: %l f" , newDoubleValuePtr
);
101 if ( myFloat
!= * newDoubleValuePtr
) {
102 log_err ( "% %f Got: %f , Expected: %f \n " , * newDoubleValuePtr
, myFloat
);
105 u_sprintf ( uStringBuf
, "Lowercase float e: %e " , myFloat
);
106 * newDoubleValuePtr
= - 1.0 ;
107 u_sscanf ( uStringBuf
, "Lowercase float e: %l e" , newDoubleValuePtr
);
108 if ( myFloat
!= * newDoubleValuePtr
) {
109 log_err ( "% %e Got: %e , Expected: %e \n " , * newDoubleValuePtr
, myFloat
);
112 u_sprintf ( uStringBuf
, "Uppercase float E: %E " , myFloat
);
113 * newDoubleValuePtr
= - 1.0 ;
114 u_sscanf ( uStringBuf
, "Uppercase float E: %l E" , newDoubleValuePtr
);
115 if ( myFloat
!= * newDoubleValuePtr
) {
116 log_err ( "% %E Got: %E , Expected: %E \n " , * newDoubleValuePtr
, myFloat
);
119 u_sprintf ( uStringBuf
, "Lowercase float g: %g " , myFloat
);
120 * newDoubleValuePtr
= - 1.0 ;
121 u_sscanf ( uStringBuf
, "Lowercase float g: %l g" , newDoubleValuePtr
);
122 if ( myFloat
!= * newDoubleValuePtr
) {
123 log_err ( "% %g Got: %g , Expected: %g \n " , * newDoubleValuePtr
, myFloat
);
126 u_sprintf ( uStringBuf
, "Uppercase float G: %G " , myFloat
);
127 * newDoubleValuePtr
= - 1.0 ;
128 u_sscanf ( uStringBuf
, "Uppercase float G: %l G" , newDoubleValuePtr
);
129 if ( myFloat
!= * newDoubleValuePtr
) {
130 log_err ( "% %G Got: %G , Expected: %G \n " , * newDoubleValuePtr
, myFloat
);
134 u_sprintf ( uStringBuf
, "Pointer % %p : %p \n " , origPtr
);
135 u_sscanf ( uStringBuf
, "Pointer % %p : %p \n " , & ptr
);
136 if ( ptr
!= origPtr
|| u_strlen ( uStringBuf
) != 13 +( sizeof ( void *)* 2 )) {
137 log_err ( "% %p Got: %p , Expected: %p \n " , ptr
, origPtr
);
140 u_sprintf ( uStringBuf
, "Char c: %c " , 'A' );
141 u_sscanf ( uStringBuf
, "Char c: %c " , myString
);
142 if (* myString
!= 'A' ) {
143 log_err ( "% %c Got: %c , Expected: A \n " , * myString
);
146 u_sprintf ( uStringBuf
, "UChar % %C : %C " , ( UChar
) 0x0041 ); /*'A'*/
147 u_sscanf ( uStringBuf
, "UChar % %C : %C " , myUString
);
148 if (* myUString
!= ( UChar
) 0x0041 ) { /*'A'*/
149 log_err ( "% %C Got: %C , Expected: A \n " , * myUString
);
152 u_sprintf ( uStringBuf
, "String % %s : %s " , "My-String" );
153 u_sscanf ( uStringBuf
, "String % %s : %s " , myString
);
154 if ( strcmp ( myString
, "My-String" )) {
155 log_err ( "% %s Got: %s , Expected: My-String \n " , myString
);
157 if ( uStringBuf
[ 20 ] != 0 ) {
158 log_err ( "String not terminated. Got %c \n " , uStringBuf
[ 20 ] );
160 u_sprintf ( uStringBuf
, "NULL String % %s : %s " , NULL
);
161 u_sscanf ( uStringBuf
, "NULL String % %s : %s " , myString
);
162 if ( strcmp ( myString
, "(null)" )) {
163 log_err ( "% %s Got: %s , Expected: My-String \n " , myString
);
166 u_sprintf ( uStringBuf
, "Unicode String % %S : %S " , myStringOrig
);
167 u_sscanf ( uStringBuf
, "Unicode String % %S : %S " , myUString
);
168 u_austrncpy ( myString
, myUString
, UPRV_LENGTHOF ( myString
));
169 if ( strcmp ( myString
, "My-String" )) {
170 log_err ( "% %S Got: %s , Expected: My String \n " , myString
);
173 u_sprintf ( uStringBuf
, "NULL Unicode String % %S : %S " , NULL
);
174 u_sscanf ( uStringBuf
, "NULL Unicode String % %S : %S " , myUString
);
175 u_austrncpy ( myString
, myUString
, UPRV_LENGTHOF ( myString
));
176 if ( strcmp ( myString
, "(null)" )) {
177 log_err ( "% %S Got: %s , Expected: (null) \n " , myString
);
180 u_sprintf ( uStringBuf
, "Percent % %P (non-ANSI): %P " , myFloat
);
181 * newDoubleValuePtr
= - 1.0 ;
182 u_sscanf ( uStringBuf
, "Percent % %P (non-ANSI): %P " , newDoubleValuePtr
);
183 if ( myFloat
!= * newDoubleValuePtr
) {
184 log_err ( "% %P Got: %P , Expected: %P \n " , * newDoubleValuePtr
, myFloat
);
187 u_sprintf ( uStringBuf
, "Spell Out % %V (non-ANSI): %V " , myFloat
);
188 * newDoubleValuePtr
= - 1.0 ;
189 u_sscanf ( uStringBuf
, "Spell Out % %V (non-ANSI): %V " , newDoubleValuePtr
);
190 if ( myFloat
!= * newDoubleValuePtr
) {
191 log_err ( "% %V Got: %f , Expected: %f \n " , * newDoubleValuePtr
, myFloat
);
195 u_sprintf ( uStringBuf
, " \t\n Pointer to integer (Count) % %n : n= %d %n n= %d \n " , * newValuePtr
, newValuePtr
, * newValuePtr
);
196 if (* newValuePtr
!= 37 ) {
197 log_err ( "% %V Got: %f , Expected: %f \n " , * newDoubleValuePtr
, myFloat
);
200 /* u_sscanf(uStringBuf, "Pointer %%p: %p\n", myFile);*/
203 static const char longStr
[] = "This is a long test12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" ;
205 retVal
= u_sprintf ( uStringBuf
, longStr
);
206 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( uStringBuf
));
207 if ( strcmp ( myString
, longStr
)) {
208 log_err ( "% %S Got: %s , Expected: %s \n " , myString
, longStr
);
210 if ( retVal
!= ( int32_t ) strlen ( longStr
)) {
211 log_err ( "% %S returned different sizes. Got: %d Expected: %d \n " , retVal
, strlen ( longStr
));
214 retVal
= u_sprintf ( uStringBuf
, " %s " , longStr
);
215 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( uStringBuf
));
216 if ( strcmp ( myString
, longStr
)) {
217 log_err ( "% %S Got: %s , Expected: %s \n " , myString
, longStr
);
219 if ( retVal
!= ( int32_t ) strlen ( longStr
)) {
220 log_err ( "% %S returned different sizes. Got: %d Expected: %d \n " , retVal
, strlen ( longStr
));
223 u_uastrncpy ( myUString
, longStr
, UPRV_LENGTHOF ( longStr
));
224 u_sprintf_u ( uStringBuf
, myUString
);
225 if ( u_strcmp ( myUString
, uStringBuf
)) {
226 log_err ( "% %S Long strings differ. Expected: %s \n " , longStr
);
229 u_uastrncpy ( myUString
, longStr
, UPRV_LENGTHOF ( longStr
));
230 retVal
= u_sprintf_u ( uStringBuf
, myUString
+ 10 );
231 if ( u_strcmp ( myUString
+ 10 , uStringBuf
)) {
232 log_err ( "% %S Long strings differ. Expected: %s \n " , longStr
+ 10 );
234 if ( retVal
!= ( int32_t ) strlen ( longStr
+ 10 )) {
235 log_err ( "% %S returned different sizes. Got: %d Expected: %d \n " , retVal
, strlen ( longStr
));
238 u_memset ( uStringBuf
, 1 , UPRV_LENGTHOF ( longStr
));
239 u_uastrncpy ( myUString
, longStr
, UPRV_LENGTHOF ( longStr
));
240 retVal
= u_snprintf_u ( uStringBuf
, 10 , myUString
);
241 if ( u_strncmp ( myUString
, uStringBuf
, 10 ) || uStringBuf
[ 10 ] != 1 || retVal
!= 10 ) {
242 log_err ( "% %S Long strings differ. Expected the first 10 characters of %s \n " , longStr
);
248 static void TestLocalizedString ( void ) {
249 #if !UCONFIG_NO_FORMATTING
253 int32_t numResult
= - 1 ;
255 UFILE
* strFile
= u_fstropen ( testStr
, UPRV_LENGTHOF ( testStr
), "en_US" );
258 log_err ( "u_fstropen failed to work \n " );
261 u_fprintf ( strFile
, " %d " , 1234 );
263 u_fscanf ( strFile
, " %d " , & numResult
);
264 u_uastrcpy ( uBuffer
, "1,234" );
265 u_austrcpy ( cBuffer
, testStr
);
266 if ( u_strcmp ( testStr
, uBuffer
) != 0 ) {
267 log_err ( "u_fprintf failed to work on an en string Got: %s \n " , cBuffer
);
269 if ( numResult
!= 1234 ) {
270 log_err ( "u_fscanf failed to work on an en string Got: %d \n " , numResult
);
274 locale
= u_fgetlocale ( strFile
);
275 if ( locale
== NULL
|| strcmp ( locale
, "en_US" ) != 0 ) {
276 log_err ( "u_fgetlocale didn't return \" en \" Got: %d \n " , u_fgetlocale ( strFile
));
278 u_fsetlocale ( strFile
, "de_DE" );
279 locale
= u_fgetlocale ( strFile
);
280 if ( locale
== NULL
|| strcmp ( locale
, "de_DE" ) != 0 ) {
281 log_err ( "u_fgetlocale didn't return \" de \" Got: %d \n " , u_fgetlocale ( strFile
));
284 u_fprintf ( strFile
, " %d " , 1234 );
287 u_fscanf ( strFile
, " %d " , & numResult
);
289 u_uastrcpy ( uBuffer
, "1.234" );
290 u_austrcpy ( cBuffer
, testStr
);
291 if ( u_strcmp ( testStr
, uBuffer
) != 0 ) {
292 log_err ( "u_fprintf failed to work on a de string Got: %s \n " , cBuffer
);
294 if ( numResult
!= 1234 ) {
295 log_err ( "u_fscanf failed to work on a de string Got: %d \n " , numResult
);
298 strFile
= u_fstropen ( testStr
, UPRV_LENGTHOF ( testStr
), NULL
);
299 u_fprintf ( strFile
, " %d " , 1234 );
302 u_fscanf ( strFile
, " %d " , & numResult
);
304 if ( numResult
!= 1234 ) {
305 log_err ( "u_fscanf failed to work on a default locale string Got: %d , Expected: 1234 \n " , numResult
);
307 if ( u_fstropen ( testStr
, - 1 , NULL
) != NULL
) {
308 log_err ( "u_fstropen returned a UFILE* on a negative buffer size \n " , numResult
);
313 #if !UCONFIG_NO_FORMATTING
314 #define Test_u_snprintf(limit, format, value, expectedSize, expectedStr) \
315 u_uastrncpy(testStr, "xxxxxxxxxxxxxx" , UPRV_LENGTHOF(testStr));\
316 size = u_snprintf(testStr, limit, format, value);\
317 u_austrncpy(cTestResult, testStr, UPRV_LENGTHOF(cTestResult));\
318 if (size != expectedSize || strcmp(cTestResult, expectedStr) != 0) {\
319 log_err( "Unexpected formatting. size= %d expectedSize= %d cTestResult= %s expectedStr= %s \n " ,\
320 size, expectedSize, cTestResult, expectedStr);\
323 log_verbose( "Got: %s \n " , cTestResult);\
328 static void TestSnprintf ( void ) {
329 #if !UCONFIG_NO_FORMATTING
331 char cTestResult
[ 256 ];
334 Test_u_snprintf ( 0 , " %d " , 123 , 3 , "xxxxxxxxxxxxxx" );
335 Test_u_snprintf ( 2 , " %d " , 123 , 3 , "12xxxxxxxxxxxx" );
336 Test_u_snprintf ( 3 , " %d " , 123 , 3 , "123xxxxxxxxxxx" );
337 Test_u_snprintf ( 4 , " %d " , 123 , 3 , "123" );
339 Test_u_snprintf ( 0 , " %s " , "abcd" , 4 , "xxxxxxxxxxxxxx" );
340 Test_u_snprintf ( 3 , " %s " , "abcd" , 4 , "abcxxxxxxxxxxx" );
341 Test_u_snprintf ( 4 , " %s " , "abcd" , 4 , "abcdxxxxxxxxxx" );
342 Test_u_snprintf ( 5 , " %s " , "abcd" , 4 , "abcd" );
344 Test_u_snprintf ( 0 , " %e " , 12.34 , 13 , "xxxxxxxxxxxxxx" );
345 Test_u_snprintf ( 1 , " %e " , 12.34 , 13 , "1xxxxxxxxxxxxx" );
346 Test_u_snprintf ( 2 , " %e " , 12.34 , 13 , "1.xxxxxxxxxxxx" );
347 Test_u_snprintf ( 3 , " %e " , 12.34 , 13 , "1.2xxxxxxxxxxx" );
348 Test_u_snprintf ( 5 , " %e " , 12.34 , 13 , "1.234xxxxxxxxx" );
349 Test_u_snprintf ( 6 , " %e " , 12.34 , 13 , "1.2340xxxxxxxx" );
350 Test_u_snprintf ( 8 , " %e " , 12.34 , 13 , "1.234000xxxxxx" );
351 Test_u_snprintf ( 9 , " %e " , 12.34 , 13 , "1.234000exxxxx" );
352 Test_u_snprintf ( 10 , " %e " , 12.34 , 13 , "1.234000e+xxxx" );
353 Test_u_snprintf ( 11 , " %e " , 12.34 , 13 , "1.234000e+0xxx" );
354 Test_u_snprintf ( 13 , " %e " , 12.34 , 13 , "1.234000e+001x" );
355 Test_u_snprintf ( 14 , " %e " , 12.34 , 13 , "1.234000e+001" );
359 #define TestSPrintFormat(uFormat, uValue, cFormat, cValue) \
360 /* Reinitialize the buffer to verify null termination works. */ \
361 u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer));\
362 memset(buffer, '*' , UPRV_LENGTHOF(buffer));\
364 uNumPrinted = u_sprintf(uBuffer, uFormat, uValue);\
365 u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer));\
366 cNumPrinted = sprintf(buffer, cFormat, cValue);\
367 if (strcmp(buffer, compBuffer) != 0) {\
368 log_err( "%" uFormat " Got: \" %s \" , Expected: \" %s \"\n " , compBuffer, buffer);\
370 if (cNumPrinted != uNumPrinted) {\
371 log_err( "%" uFormat " number printed Got: %d , Expected: %d \n " , uNumPrinted, cNumPrinted);\
373 if (buffer[uNumPrinted+1] != '*' ) {\
374 log_err( "%" uFormat " too much stored \n " );\
377 static void TestSprintfFormat(void) {
378 #if !UCONFIG_NO_FORMATTING
379 static const UChar abcUChars
[] = { 0x61 , 0x62 , 0x63 , 0 };
380 static const char abcChars
[] = "abc" ;
381 const char * reorderFormat
= " %2 $d==> %1 $-10.10s %6 $lld %4 $-10.10s %3 $#x(( %5 $d" ; /* reordering test*/
382 const char * reorderResult
= "99==>truncateif 1311768467463790322 1234567890 0xf1b93((10" ;
385 char compBuffer
[ 256 ];
390 TestSPrintFormat ( " %8 S" , abcUChars
, " %8 s" , abcChars
);
391 TestSPrintFormat ( "%-8S" , abcUChars
, "%-8s" , abcChars
);
392 TestSPrintFormat ( "%.2S" , abcUChars
, "%.2s" , abcChars
); /* strlen is 3 */
394 TestSPrintFormat ( " %8 s" , abcChars
, " %8 s" , abcChars
);
395 TestSPrintFormat ( "%-8s" , abcChars
, "%-8s" , abcChars
);
396 TestSPrintFormat ( "%.2s" , abcChars
, "%.2s" , abcChars
); /* strlen is 3 */
398 TestSPrintFormat ( " %8 c" , ( char ) 'e' , " %8 c" , ( char ) 'e' );
399 TestSPrintFormat ( "%-8c" , ( char ) 'e' , "%-8c" , ( char ) 'e' );
401 TestSPrintFormat ( " %8 C" , ( UChar
) 0x65 , " %8 c" , ( char ) 'e' );
402 TestSPrintFormat ( "%-8C" , ( UChar
) 0x65 , "%-8c" , ( char ) 'e' );
404 TestSPrintFormat ( " %f " , 1.23456789 , " %f " , 1.23456789 );
405 TestSPrintFormat ( " %f " , 12345.6789 , " %f " , 12345.6789 );
406 TestSPrintFormat ( " %f " , 123456.789 , " %f " , 123456.789 );
407 TestSPrintFormat ( " %f " , 1234567.89 , " %f " , 1234567.89 );
408 TestSPrintFormat ( " %1 0f" , 1.23456789 , " %1 0f" , 1.23456789 );
409 TestSPrintFormat ( "%-10f" , 1.23456789 , "%-10f" , 1.23456789 );
410 TestSPrintFormat ( " %1 0f" , 123.456789 , " %1 0f" , 123.456789 );
411 TestSPrintFormat ( " %1 0.4f" , 123.456789 , " %1 0.4f" , 123.456789 );
412 TestSPrintFormat ( "%-10f" , 123.456789 , "%-10f" , 123.456789 );
414 /* TestSPrintFormat("%g", 12345.6789, "%g", 12345.6789);
415 TestSPrintFormat("%g", 123456.789, "%g", 123456.789);
416 TestSPrintFormat("%g", 1234567.89, "%g", 1234567.89);
417 TestSPrintFormat("%G", 123456.789, "%G", 123456.789);
418 TestSPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
419 TestSPrintFormat ( " %1 0g" , 1.23456789 , " %1 0g" , 1.23456789 );
420 TestSPrintFormat ( " %1 0.4g" , 1.23456789 , " %1 0.4g" , 1.23456789 );
421 TestSPrintFormat ( "%-10g" , 1.23456789 , "%-10g" , 1.23456789 );
422 TestSPrintFormat ( " %1 0g" , 123.456789 , " %1 0g" , 123.456789 );
423 TestSPrintFormat ( "%-10g" , 123.456789 , "%-10g" , 123.456789 );
425 TestSPrintFormat ( " %8 x" , 123456 , " %8 x" , 123456 );
426 TestSPrintFormat ( "%-8x" , 123456 , "%-8x" , 123456 );
427 TestSPrintFormat ( " %0 8x" , 123456 , " %0 8x" , 123456 );
429 TestSPrintFormat ( " %8 X" , 123456 , " %8 X" , 123456 );
430 TestSPrintFormat ( "%-8X" , 123456 , "%-8X" , 123456 );
431 TestSPrintFormat ( " %0 8X" , 123456 , " %0 8X" , 123456 );
432 TestSPrintFormat ( "%#x" , 123456 , "%#x" , 123456 );
433 TestSPrintFormat ( "%#x" , - 123456 , "%#x" , - 123456 );
435 TestSPrintFormat ( " %8 o" , 123456 , " %8 o" , 123456 );
436 TestSPrintFormat ( "%-8o" , 123456 , "%-8o" , 123456 );
437 TestSPrintFormat ( " %0 8o" , 123456 , " %0 8o" , 123456 );
438 TestSPrintFormat ( "%#o" , 123 , "%#o" , 123 );
439 TestSPrintFormat ( "%#o" , - 123 , "%#o" , - 123 );
441 TestSPrintFormat ( " %8 u" , 123456 , " %8 u" , 123456 );
442 TestSPrintFormat ( "%-8u" , 123456 , "%-8u" , 123456 );
443 TestSPrintFormat ( " %0 8u" , 123456 , " %0 8u" , 123456 );
444 TestSPrintFormat ( " %8 u" , - 123456 , " %8 u" , - 123456 );
445 TestSPrintFormat ( "%-8u" , - 123456 , "%-8u" , - 123456 );
446 TestSPrintFormat ( "%.5u" , 123456 , "%.5u" , 123456 );
447 TestSPrintFormat ( "%.6u" , 123456 , "%.6u" , 123456 );
448 TestSPrintFormat ( "%.7u" , 123456 , "%.7u" , 123456 );
450 TestSPrintFormat ( " %8 d" , 123456 , " %8 d" , 123456 );
451 TestSPrintFormat ( "%-8d" , 123456 , "%-8d" , 123456 );
452 TestSPrintFormat ( " %0 8d" , 123456 , " %0 8d" , 123456 );
453 TestSPrintFormat ( "% d" , 123456 , "% d" , 123456 );
454 TestSPrintFormat ( "% d" , - 123456 , "% d" , - 123456 );
456 TestSPrintFormat ( " %8 i" , 123456 , " %8 i" , 123456 );
457 TestSPrintFormat ( "%-8i" , 123456 , "%-8i" , 123456 );
458 TestSPrintFormat ( " %0 8i" , 123456 , " %0 8i" , 123456 );
460 log_verbose ( "Get really crazy with the formatting. \n " );
462 TestSPrintFormat ( "%-#12x" , 123 , "%-#12x" , 123 );
463 TestSPrintFormat ( "%-#12x" , - 123 , "%-#12x" , - 123 );
464 TestSPrintFormat ( "%#12x" , 123 , "%#12x" , 123 );
465 TestSPrintFormat ( "%#12x" , - 123 , "%#12x" , - 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 ( "% 12d" , - 123 , "% 12d" , - 123 );
475 TestSPrintFormat ( " %1 2d" , 123 , " %1 2d" , 123 );
476 TestSPrintFormat ( " %1 2d" , - 123 , " %1 2d" , - 123 );
477 TestSPrintFormat ( "%.12d" , 123 , "%.12d" , 123 );
478 TestSPrintFormat ( "%.12d" , - 123 , "%.12d" , - 123 );
480 TestSPrintFormat ( "%-+12.1f" , 1.234 , "%-+12.1f" , 1.234 );
481 TestSPrintFormat ( "%-+12.1f" , - 1.234 , "%-+12.1f" , - 1.234 );
482 TestSPrintFormat ( "%- 12.10f" , 1.234 , "%- 12.10f" , 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 ( "% 12.1f" , - 1.234 , "% 12.1f" , - 1.234 );
488 TestSPrintFormat ( " %1 2.1f" , 1.234 , " %1 2.1f" , 1.234 );
489 TestSPrintFormat ( " %1 2.1f" , - 1.234 , " %1 2.1f" , - 1.234 );
490 TestSPrintFormat ( "%.2f" , 1.234 , "%.2f" , 1.234 );
491 TestSPrintFormat ( "%.2f" , - 1.234 , "%.2f" , - 1.234 );
492 TestSPrintFormat ( " %3 f" , 1.234 , " %3 f" , 1.234 );
493 TestSPrintFormat ( " %3 f" , - 1.234 , " %3 f" , - 1.234 );
495 /* Test reordering format */
496 u_sprintf ( uBuffer
, reorderFormat
, "truncateiftoolong" , 99 , 990099 , "12345678901234567890" , 10 , 0x123456789abcdef2 LL
);
497 u_austrncpy ( compBuffer
, uBuffer
, UPRV_LENGTHOF ( uBuffer
));
499 if ( strcmp ( compBuffer
, reorderResult
) != 0 ) {
500 log_err ( " %s Got: \" %s \" , Expected: \" %s \"\n " , reorderFormat
, compBuffer
, buffer
);
505 #undef TestSPrintFormat
507 static void TestStringCompatibility ( void ) {
508 #if !UCONFIG_NO_FORMATTING
509 UChar myUString
[ 256 ];
510 UChar uStringBuf
[ 256 ];
511 char myString
[ 256 ] = "" ;
512 char testBuf
[ 256 ] = "" ;
515 u_memset ( myUString
, 0x0a , UPRV_LENGTHOF ( myUString
));
516 u_memset ( uStringBuf
, 0x0a , UPRV_LENGTHOF ( uStringBuf
));
518 /* Compare against C API compatibility */
519 for ( num
= - STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
520 sprintf ( testBuf
, " %x " , ( int ) num
);
521 u_sprintf ( uStringBuf
, " %x " , num
);
522 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
523 if ( strcmp ( myString
, testBuf
) != 0 ) {
524 log_err ( "% %x Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
527 sprintf ( testBuf
, " %X " , ( int ) num
);
528 u_sprintf ( uStringBuf
, " %X " , num
);
529 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
530 if ( strcmp ( myString
, testBuf
) != 0 ) {
531 log_err ( "% %X Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
534 sprintf ( testBuf
, " %o " , ( int ) num
);
535 u_sprintf ( uStringBuf
, " %o " , num
);
536 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
537 if ( strcmp ( myString
, testBuf
) != 0 ) {
538 log_err ( "% %o Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
541 /* sprintf is not compatible on all platforms e.g. the iSeries*/
542 sprintf ( testBuf
, " %d " , ( int ) num
);
543 u_sprintf ( uStringBuf
, " %d " , num
);
544 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
545 if ( strcmp ( myString
, testBuf
) != 0 ) {
546 log_err ( "% %d Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
549 sprintf ( testBuf
, " %i " , ( int ) num
);
550 u_sprintf ( uStringBuf
, " %i " , num
);
551 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
552 if ( strcmp ( myString
, testBuf
) != 0 ) {
553 log_err ( "% %i Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
556 sprintf ( testBuf
, " %f " , ( double ) num
);
557 u_sprintf ( uStringBuf
, " %f " , ( double ) num
);
558 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
559 if ( strcmp ( myString
, testBuf
) != 0 ) {
560 log_err ( "% %f Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
563 /* sprintf(testBuf, "%e", (double)num);
564 u_sprintf(uStringBuf, "%e", (double)num);
565 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
566 if (strcmp(myString, testBuf) != 0) {
567 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
570 sprintf(testBuf, "%E", (double)num);
571 u_sprintf(uStringBuf, "%E", (double)num);
572 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
573 if (strcmp(myString, testBuf) != 0) {
574 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
577 sprintf ( testBuf
, " %g " , ( double ) num
);
578 u_sprintf ( uStringBuf
, " %g " , ( double ) num
);
579 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
580 if ( strcmp ( myString
, testBuf
) != 0 ) {
581 log_err ( "% %g Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
584 sprintf ( testBuf
, " %G " , ( double ) num
);
585 u_sprintf ( uStringBuf
, " %G " , ( double ) num
);
586 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
587 if ( strcmp ( myString
, testBuf
) != 0 ) {
588 log_err ( "% %G Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
592 for ( num
= 0 ; num
< 0x80 ; num
++) {
593 testBuf
[ 0 ] = ( char ) 0xFF ;
594 uStringBuf
[ 0 ] = ( UChar
) 0xfffe ;
595 sprintf ( testBuf
, " %c " , ( char ) num
);
596 u_sprintf ( uStringBuf
, " %c " , num
);
597 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
598 if ( testBuf
[ 0 ] != myString
[ 0 ] || myString
[ 0 ] != num
) {
599 log_err ( "% %c Got: 0x %x , Expected: 0x %x \n " , myString
[ 0 ], testBuf
[ 0 ]);
605 static void TestSScanSetFormat ( const char * format
, const UChar
* uValue
, const char * cValue
, UBool expectedToPass
) {
606 #if !UCONFIG_NO_FORMATTING
609 char compBuffer
[ 256 ];
613 /* Reinitialize the buffer to verify null termination works. */
614 u_memset ( uBuffer
, 0x2a , UPRV_LENGTHOF ( uBuffer
));
615 uBuffer
[ UPRV_LENGTHOF ( uBuffer
)- 1 ] = 0 ;
616 memset ( buffer
, '*' , UPRV_LENGTHOF ( buffer
));
617 buffer
[ UPRV_LENGTHOF ( buffer
)- 1 ] = 0 ;
619 uNumScanned
= u_sscanf ( uValue
, format
, uBuffer
);
620 if ( expectedToPass
) {
621 u_austrncpy ( compBuffer
, uBuffer
, UPRV_LENGTHOF ( uBuffer
));
622 cNumScanned
= sscanf ( cValue
, format
, buffer
);
623 if ( strncmp ( buffer
, compBuffer
, UPRV_LENGTHOF ( uBuffer
)) != 0 ) {
624 log_err ( " %s Got: \" %s \" , Expected: \" %s \"\n " , format
, compBuffer
, buffer
);
626 if ( cNumScanned
!= uNumScanned
) {
627 log_err ( " %s number scanned Got: %d , Expected: %d \n " , format
, uNumScanned
, cNumScanned
);
629 if ( uNumScanned
> 0 && uBuffer
[ u_strlen ( uBuffer
)+ 1 ] != 0x2a ) {
630 log_err ( " %s too much stored \n " , format
);
634 if ( uNumScanned
!= 0 || uBuffer
[ 0 ] != 0x2a || uBuffer
[ 1 ] != 0x2a ) {
635 log_err ( " %s too much stored on a failure \n " , format
);
641 static void TestSScanset ( void ) {
642 #if !UCONFIG_NO_FORMATTING
643 static const UChar abcUChars
[] = { 0x61 , 0x62 , 0x63 , 0x63 , 0x64 , 0x65 , 0x66 , 0x67 , 0 };
644 static const char abcChars
[] = "abccdefg" ;
646 TestSScanSetFormat ( "%[bc]S" , abcUChars
, abcChars
, TRUE
);
647 TestSScanSetFormat ( "%[cb]S" , abcUChars
, abcChars
, TRUE
);
649 TestSScanSetFormat ( "%[ab]S" , abcUChars
, abcChars
, TRUE
);
650 TestSScanSetFormat ( "%[ba]S" , abcUChars
, abcChars
, TRUE
);
652 TestSScanSetFormat ( "%[ab]" , abcUChars
, abcChars
, TRUE
);
653 TestSScanSetFormat ( "%[ba]" , abcUChars
, abcChars
, TRUE
);
655 TestSScanSetFormat ( "%[abcdefgh]" , abcUChars
, abcChars
, TRUE
);
656 TestSScanSetFormat ( "%[;hgfedcba]" , abcUChars
, abcChars
, TRUE
);
658 TestSScanSetFormat ( "%[^a]" , abcUChars
, abcChars
, TRUE
);
659 TestSScanSetFormat ( "%[^e]" , abcUChars
, abcChars
, TRUE
);
660 TestSScanSetFormat ( "%[^ed]" , abcUChars
, abcChars
, TRUE
);
661 TestSScanSetFormat ( "%[^dc]" , abcUChars
, abcChars
, TRUE
);
662 TestSScanSetFormat ( "%[^e] " , abcUChars
, abcChars
, TRUE
);
664 TestSScanSetFormat ( " %1 [ab] " , abcUChars
, abcChars
, TRUE
);
665 TestSScanSetFormat ( " %2 [^f]" , abcUChars
, abcChars
, TRUE
);
667 TestSScanSetFormat ( "%[qrst]" , abcUChars
, abcChars
, TRUE
);
669 /* Extra long string for testing */
670 TestSScanSetFormat ( " %[qrst]" ,
671 abcUChars
, abcChars
, TRUE
);
673 TestSScanSetFormat ( "%[a-]" , abcUChars
, abcChars
, TRUE
);
676 TestSScanSetFormat ( "%[a" , abcUChars
, abcChars
, FALSE
);
677 TestSScanSetFormat ( "%[f-a]" , abcUChars
, abcChars
, FALSE
);
678 TestSScanSetFormat ( "%[c-a]" , abcUChars
, abcChars
, FALSE
);
679 /* The following is not deterministic on Windows */
680 /* TestSScanSetFormat("%[a-", abcUChars, abcChars);*/
682 /* TODO: Need to specify precision with a "*" */
686 static void TestBadSScanfFormat ( const char * format
, const UChar
* uValue
, const char * cValue
) {
687 #if !UCONFIG_NO_FORMATTING
691 /* Reinitialize the buffer to verify null termination works. */
692 u_memset ( uBuffer
, 0x2a , UPRV_LENGTHOF ( uBuffer
));
693 uBuffer
[ UPRV_LENGTHOF ( uBuffer
)- 1 ] = 0 ;
695 uNumScanned
= u_sscanf ( uValue
, format
, uBuffer
);
696 if ( uNumScanned
!= 0 || uBuffer
[ 0 ] != 0x2a || uBuffer
[ 1 ] != 0x2a ) {
697 log_err ( " %s too much stored on a failure \n " , format
);
702 static void TestBadScanfFormat ( void ) {
703 #if !UCONFIG_NO_FORMATTING
704 static const UChar abcUChars
[] = { 0x61 , 0x62 , 0x63 , 0x63 , 0x64 , 0x65 , 0x66 , 0x67 , 0 };
705 static const char abcChars
[] = "abccdefg" ;
707 TestBadSScanfFormat ( "%[] " , abcUChars
, abcChars
);
711 static void Test_u_vfprintf ( const char * expectedResult
, const char * format
, ...) {
712 #if !UCONFIG_NO_FORMATTING
718 va_start ( ap
, format
);
719 count
= u_vsprintf ( uBuffer
, format
, ap
);
720 ( void ) count
; /* Suppress set but not used warning */
722 u_uastrcpy ( uBuffer2
, expectedResult
);
723 if ( u_strcmp ( uBuffer
, uBuffer2
) != 0 ) {
724 log_err ( "Got two different results for \" %s \" expected \" %s \"\n " , format
, expectedResult
);
727 u_uastrcpy ( uBuffer2
, format
);
728 va_start ( ap
, format
);
729 count
= u_vsprintf_u ( uBuffer
, uBuffer2
, ap
);
731 u_uastrcpy ( uBuffer2
, expectedResult
);
732 if ( u_strcmp ( uBuffer
, uBuffer2
) != 0 ) {
733 log_err ( "Got two different results for \" %s \" expected \" %s \"\n " , format
, expectedResult
);
738 static void TestVargs ( void ) {
739 #if !UCONFIG_NO_FORMATTING
740 Test_u_vfprintf ( "8 9 a B 8.9" , " %d %u %x %X %.1f" , 8 , 9 , 10 , 11 , 8.9 );
744 static void TestCount ( void ) {
745 #if !UCONFIG_NO_FORMATTING
746 static const UChar x15
[] = { 0x78 , 0x31 , 0x35 , 0 };
750 int32_t i32
= - 1 , actual_count
, actual_result
;
752 u_uastrcpy ( testStr
, "1233456789" );
753 if ( u_sscanf ( testStr
, "%*3[123] %n %*[1-9]" , & i32
) != 0 ) {
754 log_err ( "test 1: scanf did not return 0 \n " );
757 log_err ( "test 1: scanf returned %h d instead of 3 \n " , i32
);
759 if ( u_sscanf ( testStr
, "%*4[123] %h n%*[1-9]" , & i16
) != 0 ) {
760 log_err ( "test 2: scanf did not return 0 \n " );
763 log_err ( "test 2: scanf returned %d instead of 4 \n " , i16
);
765 if ( u_sscanf ( testStr
, "%*[123]%*[1-9] %l ln" , & i64
) != 0 ) {
766 log_err ( "test 3: scanf did not return 0 \n " );
769 log_err ( "test 3: scanf did not return 10 \n " , i64
);
771 actual_result
= u_sscanf ( x15
, " %C%d%n " , & character
, & i32
, & actual_count
);
772 if ( actual_result
!= 2 ) {
773 log_err ( "scanf should return 2, but returned %d \n " , actual_result
);
775 if ( character
!= 0x78 ) {
776 log_err ( "scanf should return 0x78 for the character, but returned %X \n " , character
);
779 log_err ( "scanf should return 15 for the number, but returned %d \n " , i32
);
781 if ( actual_count
!= 3 ) {
782 log_err ( "scanf should return 3 for actual_count, but returned %d \n " , actual_count
);
788 addStringTest ( TestNode
** root
) {
789 #if !UCONFIG_NO_FORMATTING
790 addTest ( root
, & TestString
, "string/TestString" );
791 addTest ( root
, & TestLocalizedString
, "string/TestLocalizedString" );
792 addTest ( root
, & TestSprintfFormat
, "string/TestSprintfFormat" );
793 addTest ( root
, & TestSnprintf
, "string/TestSnprintf" );
794 addTest ( root
, & TestSScanset
, "string/TestSScanset" );
795 addTest ( root
, & TestStringCompatibility
, "string/TestStringCompatibility" );
796 addTest ( root
, & TestBadScanfFormat
, "string/TestBadScanfFormat" );
797 addTest ( root
, & TestVargs
, "string/TestVargs" );
798 addTest ( root
, & TestCount
, "string/TestCount" );