]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/iotest/strtst.c
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 2004-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
10 * tab size: 8 (not used)
13 * created on: 2004apr06
14 * created by: George Rhoten
17 #include "unicode/ustdio.h"
18 #include "unicode/ustring.h"
24 static void TestString ( void ) {
25 #if !UCONFIG_NO_FORMATTING
27 float myFloat
= - 1234.0 ;
28 int32_t newValuePtr
[ 1 ];
29 double newDoubleValuePtr
[ 1 ];
31 UChar uStringBuf
[ 512 ];
32 char myString
[ 512 ] = "" ;
35 U_STRING_DECL ( myStringOrig
, "My-String" , 9 );
37 U_STRING_INIT ( myStringOrig
, "My-String" , 9 );
38 u_memset ( myUString
, 0x0a , UPRV_LENGTHOF ( myUString
));
39 u_memset ( uStringBuf
, 0x0a , UPRV_LENGTHOF ( uStringBuf
));
42 if ( sizeof ( void *) == 4 ) {
43 origPtr
= ( void *) 0xdeadbeef ;
44 } else if ( sizeof ( void *) == 8 ) {
45 origPtr
= ( void *) INT64_C ( 0x1000200030004000 );
46 } else if ( sizeof ( void *) == 16 ) {
51 } massiveBigEndianPtr
= {{ 0x10002000 , 0x30004000 , 0x50006000 , 0x70008000 }};
52 origPtr
= massiveBigEndianPtr
. ptr
;
54 log_err ( "sizeof(void*)= %d hasn't been tested before" , ( int ) sizeof ( void *));
58 u_sprintf ( uStringBuf
, "Signed decimal integer d: %d " , * n
);
60 u_sscanf ( uStringBuf
, "Signed decimal integer d: %d " , newValuePtr
);
61 if (* n
!= * newValuePtr
) {
62 log_err ( "% %d Got: %d , Expected: %d \n " , * newValuePtr
, * n
);
65 u_sprintf ( uStringBuf
, "Signed decimal integer i: %i " , * n
);
67 u_sscanf ( uStringBuf
, "Signed decimal integer i: %i " , newValuePtr
);
68 if (* n
!= * newValuePtr
) {
69 log_err ( "% %i Got: %i , Expected: %i \n " , * newValuePtr
, * n
);
72 u_sprintf ( uStringBuf
, "Unsigned octal integer o: %o " , * n
);
74 u_sscanf ( uStringBuf
, "Unsigned octal integer o: %o " , newValuePtr
);
75 if (* n
!= * newValuePtr
) {
76 log_err ( "% %o Got: %o , Expected: %o \n " , * newValuePtr
, * n
);
79 u_sprintf ( uStringBuf
, "Unsigned decimal integer % %u : %u " , * n
);
81 u_sscanf ( uStringBuf
, "Unsigned decimal integer % %u : %u " , newValuePtr
);
82 if (* n
!= * newValuePtr
) {
83 log_err ( "% %u Got: %u , Expected: %u \n " , * newValuePtr
, * n
);
86 u_sprintf ( uStringBuf
, "Lowercase unsigned hexadecimal integer x: %x " , * n
);
88 u_sscanf ( uStringBuf
, "Lowercase unsigned hexadecimal integer x: %x " , newValuePtr
);
89 if (* n
!= * newValuePtr
) {
90 log_err ( "% %x Got: %x , Expected: %x \n " , * newValuePtr
, * n
);
93 u_sprintf ( uStringBuf
, "Uppercase unsigned hexadecimal integer X: %X " , * n
);
95 u_sscanf ( uStringBuf
, "Uppercase unsigned hexadecimal integer X: %X " , newValuePtr
);
96 if (* n
!= * newValuePtr
) {
97 log_err ( "% %X Got: %X , Expected: %X \n " , * newValuePtr
, * n
);
100 u_sprintf ( uStringBuf
, "Float f: %f " , myFloat
);
101 * newDoubleValuePtr
= - 1.0 ;
102 u_sscanf ( uStringBuf
, "Float f: %l f" , newDoubleValuePtr
);
103 if ( myFloat
!= * newDoubleValuePtr
) {
104 log_err ( "% %f Got: %f , Expected: %f \n " , * newDoubleValuePtr
, myFloat
);
107 u_sprintf ( uStringBuf
, "Lowercase float e: %e " , myFloat
);
108 * newDoubleValuePtr
= - 1.0 ;
109 u_sscanf ( uStringBuf
, "Lowercase float e: %l e" , newDoubleValuePtr
);
110 if ( myFloat
!= * newDoubleValuePtr
) {
111 log_err ( "% %e Got: %e , Expected: %e \n " , * newDoubleValuePtr
, myFloat
);
114 u_sprintf ( uStringBuf
, "Uppercase float E: %E " , myFloat
);
115 * newDoubleValuePtr
= - 1.0 ;
116 u_sscanf ( uStringBuf
, "Uppercase float E: %l E" , newDoubleValuePtr
);
117 if ( myFloat
!= * newDoubleValuePtr
) {
118 log_err ( "% %E Got: %E , Expected: %E \n " , * newDoubleValuePtr
, myFloat
);
121 u_sprintf ( uStringBuf
, "Lowercase float g: %g " , myFloat
);
122 * newDoubleValuePtr
= - 1.0 ;
123 u_sscanf ( uStringBuf
, "Lowercase float g: %l g" , newDoubleValuePtr
);
124 if ( myFloat
!= * newDoubleValuePtr
) {
125 log_err ( "% %g Got: %g , Expected: %g \n " , * newDoubleValuePtr
, myFloat
);
128 u_sprintf ( uStringBuf
, "Uppercase float G: %G " , myFloat
);
129 * newDoubleValuePtr
= - 1.0 ;
130 u_sscanf ( uStringBuf
, "Uppercase float G: %l G" , newDoubleValuePtr
);
131 if ( myFloat
!= * newDoubleValuePtr
) {
132 log_err ( "% %G Got: %G , Expected: %G \n " , * newDoubleValuePtr
, myFloat
);
136 u_sprintf ( uStringBuf
, "Pointer % %p : %p \n " , origPtr
);
137 u_sscanf ( uStringBuf
, "Pointer % %p : %p \n " , & ptr
);
138 if ( ptr
!= origPtr
|| u_strlen ( uStringBuf
) != 13 +( sizeof ( void *)* 2 )) {
139 log_err ( "% %p Got: %p , Expected: %p \n " , ptr
, origPtr
);
142 u_sprintf ( uStringBuf
, "Char c: %c " , 'A' );
143 u_sscanf ( uStringBuf
, "Char c: %c " , myString
);
144 if (* myString
!= 'A' ) {
145 log_err ( "% %c Got: %c , Expected: A \n " , * myString
);
148 u_sprintf ( uStringBuf
, "UChar % %C : %C " , ( UChar
) 0x0041 ); /*'A'*/
149 u_sscanf ( uStringBuf
, "UChar % %C : %C " , myUString
);
150 if (* myUString
!= ( UChar
) 0x0041 ) { /*'A'*/
151 log_err ( "% %C Got: %C , Expected: A \n " , * myUString
);
154 u_sprintf ( uStringBuf
, "String % %s : %s " , "My-String" );
155 u_sscanf ( uStringBuf
, "String % %s : %s " , myString
);
156 if ( strcmp ( myString
, "My-String" )) {
157 log_err ( "% %s Got: %s , Expected: My-String \n " , myString
);
159 if ( uStringBuf
[ 20 ] != 0 ) {
160 log_err ( "String not terminated. Got %c \n " , uStringBuf
[ 20 ] );
162 u_sprintf ( uStringBuf
, "NULL String % %s : %s " , NULL
);
163 u_sscanf ( uStringBuf
, "NULL String % %s : %s " , myString
);
164 if ( strcmp ( myString
, "(null)" )) {
165 log_err ( "% %s Got: %s , Expected: My-String \n " , myString
);
168 u_sprintf ( uStringBuf
, "Unicode String % %S : %S " , myStringOrig
);
169 u_sscanf ( uStringBuf
, "Unicode String % %S : %S " , myUString
);
170 u_austrncpy ( myString
, myUString
, UPRV_LENGTHOF ( myString
));
171 if ( strcmp ( myString
, "My-String" )) {
172 log_err ( "% %S Got: %s , Expected: My String \n " , myString
);
175 u_sprintf ( uStringBuf
, "NULL Unicode String % %S : %S " , NULL
);
176 u_sscanf ( uStringBuf
, "NULL Unicode String % %S : %S " , myUString
);
177 u_austrncpy ( myString
, myUString
, UPRV_LENGTHOF ( myString
));
178 if ( strcmp ( myString
, "(null)" )) {
179 log_err ( "% %S Got: %s , Expected: (null) \n " , myString
);
182 u_sprintf ( uStringBuf
, "Percent % %P (non-ANSI): %P " , myFloat
);
183 * newDoubleValuePtr
= - 1.0 ;
184 u_sscanf ( uStringBuf
, "Percent % %P (non-ANSI): %P " , newDoubleValuePtr
);
185 if ( myFloat
!= * newDoubleValuePtr
) {
186 log_err ( "% %P Got: %P , Expected: %P \n " , * newDoubleValuePtr
, myFloat
);
189 u_sprintf ( uStringBuf
, "Spell Out % %V (non-ANSI): %V " , myFloat
);
190 * newDoubleValuePtr
= - 1.0 ;
191 u_sscanf ( uStringBuf
, "Spell Out % %V (non-ANSI): %V " , newDoubleValuePtr
);
192 if ( myFloat
!= * newDoubleValuePtr
) {
193 log_err ( "% %V Got: %f , Expected: %f \n " , * newDoubleValuePtr
, myFloat
);
197 u_sprintf ( uStringBuf
, " \t\n Pointer to integer (Count) % %n : n= %d %n n= %d \n " , * newValuePtr
, newValuePtr
, * newValuePtr
);
198 if (* newValuePtr
!= 37 ) {
199 log_err ( "% %V Got: %f , Expected: %f \n " , * newDoubleValuePtr
, myFloat
);
202 /* u_sscanf(uStringBuf, "Pointer %%p: %p\n", myFile);*/
205 static const char longStr
[] = "This is a long test12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" ;
207 retVal
= u_sprintf ( uStringBuf
, longStr
);
208 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( uStringBuf
));
209 if ( strcmp ( myString
, longStr
)) {
210 log_err ( "% %S Got: %s , Expected: %s \n " , myString
, longStr
);
212 if ( retVal
!= ( int32_t ) strlen ( longStr
)) {
213 log_err ( "% %S returned different sizes. Got: %d Expected: %d \n " , retVal
, strlen ( longStr
));
216 retVal
= u_sprintf ( uStringBuf
, " %s " , longStr
);
217 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( uStringBuf
));
218 if ( strcmp ( myString
, longStr
)) {
219 log_err ( "% %S Got: %s , Expected: %s \n " , myString
, longStr
);
221 if ( retVal
!= ( int32_t ) strlen ( longStr
)) {
222 log_err ( "% %S returned different sizes. Got: %d Expected: %d \n " , retVal
, strlen ( longStr
));
225 u_uastrncpy ( myUString
, longStr
, UPRV_LENGTHOF ( longStr
));
226 u_sprintf_u ( uStringBuf
, myUString
);
227 if ( u_strcmp ( myUString
, uStringBuf
)) {
228 log_err ( "% %S Long strings differ. Expected: %s \n " , longStr
);
231 u_uastrncpy ( myUString
, longStr
, UPRV_LENGTHOF ( longStr
));
232 retVal
= u_sprintf_u ( uStringBuf
, myUString
+ 10 );
233 if ( u_strcmp ( myUString
+ 10 , uStringBuf
)) {
234 log_err ( "% %S Long strings differ. Expected: %s \n " , longStr
+ 10 );
236 if ( retVal
!= ( int32_t ) strlen ( longStr
+ 10 )) {
237 log_err ( "% %S returned different sizes. Got: %d Expected: %d \n " , retVal
, strlen ( longStr
));
240 u_memset ( uStringBuf
, 1 , UPRV_LENGTHOF ( longStr
));
241 u_uastrncpy ( myUString
, longStr
, UPRV_LENGTHOF ( longStr
));
242 retVal
= u_snprintf_u ( uStringBuf
, 10 , myUString
);
243 if ( u_strncmp ( myUString
, uStringBuf
, 10 ) || uStringBuf
[ 10 ] != 1 || retVal
!= 10 ) {
244 log_err ( "% %S Long strings differ. Expected the first 10 characters of %s \n " , longStr
);
250 static void TestLocalizedString ( void ) {
251 #if !UCONFIG_NO_FORMATTING
255 int32_t numResult
= - 1 ;
257 UFILE
* strFile
= u_fstropen ( testStr
, UPRV_LENGTHOF ( testStr
), "en_US" );
260 log_err ( "u_fstropen failed to work \n " );
263 u_fprintf ( strFile
, " %d " , 1234 );
265 u_fscanf ( strFile
, " %d " , & numResult
);
266 u_uastrcpy ( uBuffer
, "1,234" );
267 u_austrcpy ( cBuffer
, testStr
);
268 if ( u_strcmp ( testStr
, uBuffer
) != 0 ) {
269 log_err ( "u_fprintf failed to work on an en string Got: %s \n " , cBuffer
);
271 if ( numResult
!= 1234 ) {
272 log_err ( "u_fscanf failed to work on an en string Got: %d \n " , numResult
);
276 locale
= u_fgetlocale ( strFile
);
277 if ( locale
== NULL
|| strcmp ( locale
, "en_US" ) != 0 ) {
278 log_err ( "u_fgetlocale didn't return \" en \" Got: %d \n " , u_fgetlocale ( strFile
));
280 u_fsetlocale ( strFile
, "de_DE" );
281 locale
= u_fgetlocale ( strFile
);
282 if ( locale
== NULL
|| strcmp ( locale
, "de_DE" ) != 0 ) {
283 log_err ( "u_fgetlocale didn't return \" de \" Got: %d \n " , u_fgetlocale ( strFile
));
286 u_fprintf ( strFile
, " %d " , 1234 );
289 u_fscanf ( strFile
, " %d " , & numResult
);
291 u_uastrcpy ( uBuffer
, "1.234" );
292 u_austrcpy ( cBuffer
, testStr
);
293 if ( u_strcmp ( testStr
, uBuffer
) != 0 ) {
294 log_err ( "u_fprintf failed to work on a de string Got: %s \n " , cBuffer
);
296 if ( numResult
!= 1234 ) {
297 log_err ( "u_fscanf failed to work on a de string Got: %d \n " , numResult
);
300 strFile
= u_fstropen ( testStr
, UPRV_LENGTHOF ( testStr
), NULL
);
301 u_fprintf ( strFile
, " %d " , 1234 );
304 u_fscanf ( strFile
, " %d " , & numResult
);
306 if ( numResult
!= 1234 ) {
307 log_err ( "u_fscanf failed to work on a default locale string Got: %d , Expected: 1234 \n " , numResult
);
309 if ( u_fstropen ( testStr
, - 1 , NULL
) != NULL
) {
310 log_err ( "u_fstropen returned a UFILE* on a negative buffer size \n " , numResult
);
315 #if !UCONFIG_NO_FORMATTING
316 #define Test_u_snprintf(limit, format, value, expectedSize, expectedStr) \
317 u_uastrncpy(testStr, "xxxxxxxxxxxxxx" , UPRV_LENGTHOF(testStr));\
318 size = u_snprintf(testStr, limit, format, value);\
319 u_austrncpy(cTestResult, testStr, UPRV_LENGTHOF(cTestResult));\
320 if (size != expectedSize || strcmp(cTestResult, expectedStr) != 0) {\
321 log_err( "Unexpected formatting. size= %d expectedSize= %d cTestResult= %s expectedStr= %s \n " ,\
322 size, expectedSize, cTestResult, expectedStr);\
325 log_verbose( "Got: %s \n " , cTestResult);\
330 static void TestSnprintf ( void ) {
331 #if !UCONFIG_NO_FORMATTING
333 char cTestResult
[ 256 ];
336 Test_u_snprintf ( 0 , " %d " , 123 , 3 , "xxxxxxxxxxxxxx" );
337 Test_u_snprintf ( 2 , " %d " , 123 , 3 , "12xxxxxxxxxxxx" );
338 Test_u_snprintf ( 3 , " %d " , 123 , 3 , "123xxxxxxxxxxx" );
339 Test_u_snprintf ( 4 , " %d " , 123 , 3 , "123" );
341 Test_u_snprintf ( 0 , " %s " , "abcd" , 4 , "xxxxxxxxxxxxxx" );
342 Test_u_snprintf ( 3 , " %s " , "abcd" , 4 , "abcxxxxxxxxxxx" );
343 Test_u_snprintf ( 4 , " %s " , "abcd" , 4 , "abcdxxxxxxxxxx" );
344 Test_u_snprintf ( 5 , " %s " , "abcd" , 4 , "abcd" );
346 Test_u_snprintf ( 0 , " %e " , 12.34 , 13 , "xxxxxxxxxxxxxx" );
347 Test_u_snprintf ( 1 , " %e " , 12.34 , 13 , "1xxxxxxxxxxxxx" );
348 Test_u_snprintf ( 2 , " %e " , 12.34 , 13 , "1.xxxxxxxxxxxx" );
349 Test_u_snprintf ( 3 , " %e " , 12.34 , 13 , "1.2xxxxxxxxxxx" );
350 Test_u_snprintf ( 5 , " %e " , 12.34 , 13 , "1.234xxxxxxxxx" );
351 Test_u_snprintf ( 6 , " %e " , 12.34 , 13 , "1.2340xxxxxxxx" );
352 Test_u_snprintf ( 8 , " %e " , 12.34 , 13 , "1.234000xxxxxx" );
353 Test_u_snprintf ( 9 , " %e " , 12.34 , 13 , "1.234000exxxxx" );
354 Test_u_snprintf ( 10 , " %e " , 12.34 , 13 , "1.234000e+xxxx" );
355 Test_u_snprintf ( 11 , " %e " , 12.34 , 13 , "1.234000e+0xxx" );
356 Test_u_snprintf ( 13 , " %e " , 12.34 , 13 , "1.234000e+001x" );
357 Test_u_snprintf ( 14 , " %e " , 12.34 , 13 , "1.234000e+001" );
361 #define TestSPrintFormat(uFormat, uValue, cFormat, cValue) \
362 /* Reinitialize the buffer to verify null termination works. */ \
363 u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer));\
364 memset(buffer, '*' , UPRV_LENGTHOF(buffer));\
366 uNumPrinted = u_sprintf(uBuffer, uFormat, uValue);\
367 u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer));\
368 cNumPrinted = sprintf(buffer, cFormat, cValue);\
369 if (strcmp(buffer, compBuffer) != 0) {\
370 log_err( "%" uFormat " Got: \" %s \" , Expected: \" %s \"\n " , compBuffer, buffer);\
372 if (cNumPrinted != uNumPrinted) {\
373 log_err( "%" uFormat " number printed Got: %d , Expected: %d \n " , uNumPrinted, cNumPrinted);\
375 if (buffer[uNumPrinted+1] != '*' ) {\
376 log_err( "%" uFormat " too much stored \n " );\
379 static void TestSprintfFormat(void) {
380 #if !UCONFIG_NO_FORMATTING
381 static const UChar abcUChars
[] = { 0x61 , 0x62 , 0x63 , 0 };
382 static const char abcChars
[] = "abc" ;
383 const char * reorderFormat
= " %2 $d==> %1 $-10.10s %6 $lld %4 $-10.10s %3 $#x(( %5 $d" ; /* reordering test*/
384 const char * reorderResult
= "99==>truncateif 1311768467463790322 1234567890 0xf1b93((10" ;
387 char compBuffer
[ 256 ];
392 TestSPrintFormat ( " %8 S" , abcUChars
, " %8 s" , abcChars
);
393 TestSPrintFormat ( "%-8S" , abcUChars
, "%-8s" , abcChars
);
394 TestSPrintFormat ( "%.2S" , abcUChars
, "%.2s" , abcChars
); /* strlen is 3 */
396 TestSPrintFormat ( " %8 s" , abcChars
, " %8 s" , abcChars
);
397 TestSPrintFormat ( "%-8s" , abcChars
, "%-8s" , abcChars
);
398 TestSPrintFormat ( "%.2s" , abcChars
, "%.2s" , abcChars
); /* strlen is 3 */
400 TestSPrintFormat ( " %8 c" , ( char ) 'e' , " %8 c" , ( char ) 'e' );
401 TestSPrintFormat ( "%-8c" , ( char ) 'e' , "%-8c" , ( char ) 'e' );
403 TestSPrintFormat ( " %8 C" , ( UChar
) 0x65 , " %8 c" , ( char ) 'e' );
404 TestSPrintFormat ( "%-8C" , ( UChar
) 0x65 , "%-8c" , ( char ) 'e' );
406 TestSPrintFormat ( " %f " , 1.23456789 , " %f " , 1.23456789 );
407 TestSPrintFormat ( " %f " , 12345.6789 , " %f " , 12345.6789 );
408 TestSPrintFormat ( " %f " , 123456.789 , " %f " , 123456.789 );
409 TestSPrintFormat ( " %f " , 1234567.89 , " %f " , 1234567.89 );
410 TestSPrintFormat ( " %1 0f" , 1.23456789 , " %1 0f" , 1.23456789 );
411 TestSPrintFormat ( "%-10f" , 1.23456789 , "%-10f" , 1.23456789 );
412 TestSPrintFormat ( " %1 0f" , 123.456789 , " %1 0f" , 123.456789 );
413 TestSPrintFormat ( " %1 0.4f" , 123.456789 , " %1 0.4f" , 123.456789 );
414 TestSPrintFormat ( "%-10f" , 123.456789 , "%-10f" , 123.456789 );
416 /* TestSPrintFormat("%g", 12345.6789, "%g", 12345.6789);
417 TestSPrintFormat("%g", 123456.789, "%g", 123456.789);
418 TestSPrintFormat("%g", 1234567.89, "%g", 1234567.89);
419 TestSPrintFormat("%G", 123456.789, "%G", 123456.789);
420 TestSPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
421 TestSPrintFormat ( " %1 0g" , 1.23456789 , " %1 0g" , 1.23456789 );
422 TestSPrintFormat ( " %1 0.4g" , 1.23456789 , " %1 0.4g" , 1.23456789 );
423 TestSPrintFormat ( "%-10g" , 1.23456789 , "%-10g" , 1.23456789 );
424 TestSPrintFormat ( " %1 0g" , 123.456789 , " %1 0g" , 123.456789 );
425 TestSPrintFormat ( "%-10g" , 123.456789 , "%-10g" , 123.456789 );
427 TestSPrintFormat ( " %8 x" , 123456 , " %8 x" , 123456 );
428 TestSPrintFormat ( "%-8x" , 123456 , "%-8x" , 123456 );
429 TestSPrintFormat ( " %0 8x" , 123456 , " %0 8x" , 123456 );
431 TestSPrintFormat ( " %8 X" , 123456 , " %8 X" , 123456 );
432 TestSPrintFormat ( "%-8X" , 123456 , "%-8X" , 123456 );
433 TestSPrintFormat ( " %0 8X" , 123456 , " %0 8X" , 123456 );
434 TestSPrintFormat ( "%#x" , 123456 , "%#x" , 123456 );
435 TestSPrintFormat ( "%#x" , - 123456 , "%#x" , - 123456 );
437 TestSPrintFormat ( " %8 o" , 123456 , " %8 o" , 123456 );
438 TestSPrintFormat ( "%-8o" , 123456 , "%-8o" , 123456 );
439 TestSPrintFormat ( " %0 8o" , 123456 , " %0 8o" , 123456 );
440 TestSPrintFormat ( "%#o" , 123 , "%#o" , 123 );
441 TestSPrintFormat ( "%#o" , - 123 , "%#o" , - 123 );
443 TestSPrintFormat ( " %8 u" , 123456 , " %8 u" , 123456 );
444 TestSPrintFormat ( "%-8u" , 123456 , "%-8u" , 123456 );
445 TestSPrintFormat ( " %0 8u" , 123456 , " %0 8u" , 123456 );
446 TestSPrintFormat ( " %8 u" , - 123456 , " %8 u" , - 123456 );
447 TestSPrintFormat ( "%-8u" , - 123456 , "%-8u" , - 123456 );
448 TestSPrintFormat ( "%.5u" , 123456 , "%.5u" , 123456 );
449 TestSPrintFormat ( "%.6u" , 123456 , "%.6u" , 123456 );
450 TestSPrintFormat ( "%.7u" , 123456 , "%.7u" , 123456 );
452 TestSPrintFormat ( " %8 d" , 123456 , " %8 d" , 123456 );
453 TestSPrintFormat ( "%-8d" , 123456 , "%-8d" , 123456 );
454 TestSPrintFormat ( " %0 8d" , 123456 , " %0 8d" , 123456 );
455 TestSPrintFormat ( "% d" , 123456 , "% d" , 123456 );
456 TestSPrintFormat ( "% d" , - 123456 , "% d" , - 123456 );
458 TestSPrintFormat ( " %8 i" , 123456 , " %8 i" , 123456 );
459 TestSPrintFormat ( "%-8i" , 123456 , "%-8i" , 123456 );
460 TestSPrintFormat ( " %0 8i" , 123456 , " %0 8i" , 123456 );
462 log_verbose ( "Get really crazy with the formatting. \n " );
464 TestSPrintFormat ( "%-#12x" , 123 , "%-#12x" , 123 );
465 TestSPrintFormat ( "%-#12x" , - 123 , "%-#12x" , - 123 );
466 TestSPrintFormat ( "%#12x" , 123 , "%#12x" , 123 );
467 TestSPrintFormat ( "%#12x" , - 123 , "%#12x" , - 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 ( "% 12d" , 123 , "% 12d" , 123 );
476 TestSPrintFormat ( "% 12d" , - 123 , "% 12d" , - 123 );
477 TestSPrintFormat ( " %1 2d" , 123 , " %1 2d" , 123 );
478 TestSPrintFormat ( " %1 2d" , - 123 , " %1 2d" , - 123 );
479 TestSPrintFormat ( "%.12d" , 123 , "%.12d" , 123 );
480 TestSPrintFormat ( "%.12d" , - 123 , "%.12d" , - 123 );
482 TestSPrintFormat ( "%-+12.1f" , 1.234 , "%-+12.1f" , 1.234 );
483 TestSPrintFormat ( "%-+12.1f" , - 1.234 , "%-+12.1f" , - 1.234 );
484 TestSPrintFormat ( "%- 12.10f" , 1.234 , "%- 12.10f" , 1.234 );
485 TestSPrintFormat ( "%- 12.1f" , - 1.234 , "%- 12.1f" , - 1.234 );
486 TestSPrintFormat ( "%+12.1f" , 1.234 , "%+12.1f" , 1.234 );
487 TestSPrintFormat ( "%+12.1f" , - 1.234 , "%+12.1f" , - 1.234 );
488 TestSPrintFormat ( "% 12.1f" , 1.234 , "% 12.1f" , 1.234 );
489 TestSPrintFormat ( "% 12.1f" , - 1.234 , "% 12.1f" , - 1.234 );
490 TestSPrintFormat ( " %1 2.1f" , 1.234 , " %1 2.1f" , 1.234 );
491 TestSPrintFormat ( " %1 2.1f" , - 1.234 , " %1 2.1f" , - 1.234 );
492 TestSPrintFormat ( "%.2f" , 1.234 , "%.2f" , 1.234 );
493 TestSPrintFormat ( "%.2f" , - 1.234 , "%.2f" , - 1.234 );
494 TestSPrintFormat ( " %3 f" , 1.234 , " %3 f" , 1.234 );
495 TestSPrintFormat ( " %3 f" , - 1.234 , " %3 f" , - 1.234 );
497 /* Test reordering format */
498 u_sprintf ( uBuffer
, reorderFormat
, "truncateiftoolong" , 99 , 990099 , "12345678901234567890" , 10 , 0x123456789abcdef2 LL
);
499 u_austrncpy ( compBuffer
, uBuffer
, UPRV_LENGTHOF ( uBuffer
));
501 if ( strcmp ( compBuffer
, reorderResult
) != 0 ) {
502 log_err ( " %s Got: \" %s \" , Expected: \" %s \"\n " , reorderFormat
, compBuffer
, buffer
);
507 #undef TestSPrintFormat
509 static void TestStringCompatibility ( void ) {
510 #if !UCONFIG_NO_FORMATTING
511 UChar myUString
[ 256 ];
512 UChar uStringBuf
[ 256 ];
513 char myString
[ 256 ] = "" ;
514 char testBuf
[ 256 ] = "" ;
517 u_memset ( myUString
, 0x0a , UPRV_LENGTHOF ( myUString
));
518 u_memset ( uStringBuf
, 0x0a , UPRV_LENGTHOF ( uStringBuf
));
520 /* Compare against C API compatibility */
521 for ( num
= - STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
522 sprintf ( testBuf
, " %x " , ( int ) num
);
523 u_sprintf ( uStringBuf
, " %x " , num
);
524 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
525 if ( strcmp ( myString
, testBuf
) != 0 ) {
526 log_err ( "% %x Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
529 sprintf ( testBuf
, " %X " , ( int ) num
);
530 u_sprintf ( uStringBuf
, " %X " , num
);
531 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
532 if ( strcmp ( myString
, testBuf
) != 0 ) {
533 log_err ( "% %X Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
536 sprintf ( testBuf
, " %o " , ( int ) num
);
537 u_sprintf ( uStringBuf
, " %o " , num
);
538 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
539 if ( strcmp ( myString
, testBuf
) != 0 ) {
540 log_err ( "% %o Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
543 /* sprintf is not compatible on all platforms e.g. the iSeries*/
544 sprintf ( testBuf
, " %d " , ( int ) num
);
545 u_sprintf ( uStringBuf
, " %d " , num
);
546 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
547 if ( strcmp ( myString
, testBuf
) != 0 ) {
548 log_err ( "% %d Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
551 sprintf ( testBuf
, " %i " , ( int ) num
);
552 u_sprintf ( uStringBuf
, " %i " , num
);
553 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
554 if ( strcmp ( myString
, testBuf
) != 0 ) {
555 log_err ( "% %i Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
558 sprintf ( testBuf
, " %f " , ( double ) num
);
559 u_sprintf ( uStringBuf
, " %f " , ( double ) num
);
560 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
561 if ( strcmp ( myString
, testBuf
) != 0 ) {
562 log_err ( "% %f Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
565 /* sprintf(testBuf, "%e", (double)num);
566 u_sprintf(uStringBuf, "%e", (double)num);
567 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
568 if (strcmp(myString, testBuf) != 0) {
569 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
572 sprintf(testBuf, "%E", (double)num);
573 u_sprintf(uStringBuf, "%E", (double)num);
574 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
575 if (strcmp(myString, testBuf) != 0) {
576 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
579 sprintf ( testBuf
, " %g " , ( double ) num
);
580 u_sprintf ( uStringBuf
, " %g " , ( double ) num
);
581 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
582 if ( strcmp ( myString
, testBuf
) != 0 ) {
583 log_err ( "% %g Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
586 sprintf ( testBuf
, " %G " , ( double ) num
);
587 u_sprintf ( uStringBuf
, " %G " , ( double ) num
);
588 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
589 if ( strcmp ( myString
, testBuf
) != 0 ) {
590 log_err ( "% %G Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
594 for ( num
= 0 ; num
< 0x80 ; num
++) {
595 testBuf
[ 0 ] = ( char ) 0xFF ;
596 uStringBuf
[ 0 ] = ( UChar
) 0xfffe ;
597 sprintf ( testBuf
, " %c " , ( char ) num
);
598 u_sprintf ( uStringBuf
, " %c " , num
);
599 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
600 if ( testBuf
[ 0 ] != myString
[ 0 ] || myString
[ 0 ] != num
) {
601 log_err ( "% %c Got: 0x %x , Expected: 0x %x \n " , myString
[ 0 ], testBuf
[ 0 ]);
607 static void TestSScanSetFormat ( const char * format
, const UChar
* uValue
, const char * cValue
, UBool expectedToPass
) {
608 #if !UCONFIG_NO_FORMATTING
611 char compBuffer
[ 256 ];
615 /* Reinitialize the buffer to verify null termination works. */
616 u_memset ( uBuffer
, 0x2a , UPRV_LENGTHOF ( uBuffer
));
617 uBuffer
[ UPRV_LENGTHOF ( uBuffer
)- 1 ] = 0 ;
618 memset ( buffer
, '*' , UPRV_LENGTHOF ( buffer
));
619 buffer
[ UPRV_LENGTHOF ( buffer
)- 1 ] = 0 ;
621 uNumScanned
= u_sscanf ( uValue
, format
, uBuffer
);
622 if ( expectedToPass
) {
623 u_austrncpy ( compBuffer
, uBuffer
, UPRV_LENGTHOF ( uBuffer
));
624 cNumScanned
= sscanf ( cValue
, format
, buffer
);
625 if ( strncmp ( buffer
, compBuffer
, UPRV_LENGTHOF ( uBuffer
)) != 0 ) {
626 log_err ( " %s Got: \" %s \" , Expected: \" %s \"\n " , format
, compBuffer
, buffer
);
628 if ( cNumScanned
!= uNumScanned
) {
629 log_err ( " %s number scanned Got: %d , Expected: %d \n " , format
, uNumScanned
, cNumScanned
);
631 if ( uNumScanned
> 0 && uBuffer
[ u_strlen ( uBuffer
)+ 1 ] != 0x2a ) {
632 log_err ( " %s too much stored \n " , format
);
636 if ( uNumScanned
!= 0 || uBuffer
[ 0 ] != 0x2a || uBuffer
[ 1 ] != 0x2a ) {
637 log_err ( " %s too much stored on a failure \n " , format
);
643 static void TestSScanset ( void ) {
644 #if !UCONFIG_NO_FORMATTING
645 static const UChar abcUChars
[] = { 0x61 , 0x62 , 0x63 , 0x63 , 0x64 , 0x65 , 0x66 , 0x67 , 0 };
646 static const char abcChars
[] = "abccdefg" ;
648 TestSScanSetFormat ( "%[bc]S" , abcUChars
, abcChars
, TRUE
);
649 TestSScanSetFormat ( "%[cb]S" , abcUChars
, abcChars
, TRUE
);
651 TestSScanSetFormat ( "%[ab]S" , abcUChars
, abcChars
, TRUE
);
652 TestSScanSetFormat ( "%[ba]S" , abcUChars
, abcChars
, TRUE
);
654 TestSScanSetFormat ( "%[ab]" , abcUChars
, abcChars
, TRUE
);
655 TestSScanSetFormat ( "%[ba]" , abcUChars
, abcChars
, TRUE
);
657 TestSScanSetFormat ( "%[abcdefgh]" , abcUChars
, abcChars
, TRUE
);
658 TestSScanSetFormat ( "%[;hgfedcba]" , abcUChars
, abcChars
, TRUE
);
660 TestSScanSetFormat ( "%[^a]" , abcUChars
, abcChars
, TRUE
);
661 TestSScanSetFormat ( "%[^e]" , abcUChars
, abcChars
, TRUE
);
662 TestSScanSetFormat ( "%[^ed]" , abcUChars
, abcChars
, TRUE
);
663 TestSScanSetFormat ( "%[^dc]" , abcUChars
, abcChars
, TRUE
);
664 TestSScanSetFormat ( "%[^e] " , abcUChars
, abcChars
, TRUE
);
666 TestSScanSetFormat ( " %1 [ab] " , abcUChars
, abcChars
, TRUE
);
667 TestSScanSetFormat ( " %2 [^f]" , abcUChars
, abcChars
, TRUE
);
669 TestSScanSetFormat ( "%[qrst]" , abcUChars
, abcChars
, TRUE
);
671 /* Extra long string for testing */
672 TestSScanSetFormat ( " %[qrst]" ,
673 abcUChars
, abcChars
, TRUE
);
675 TestSScanSetFormat ( "%[a-]" , abcUChars
, abcChars
, TRUE
);
678 TestSScanSetFormat ( "%[a" , abcUChars
, abcChars
, FALSE
);
679 TestSScanSetFormat ( "%[f-a]" , abcUChars
, abcChars
, FALSE
);
680 TestSScanSetFormat ( "%[c-a]" , abcUChars
, abcChars
, FALSE
);
681 /* The following is not deterministic on Windows */
682 /* TestSScanSetFormat("%[a-", abcUChars, abcChars);*/
684 /* TODO: Need to specify precision with a "*" */
688 static void TestBadSScanfFormat ( const char * format
, const UChar
* uValue
, const char * cValue
) {
689 #if !UCONFIG_NO_FORMATTING
693 /* Reinitialize the buffer to verify null termination works. */
694 u_memset ( uBuffer
, 0x2a , UPRV_LENGTHOF ( uBuffer
));
695 uBuffer
[ UPRV_LENGTHOF ( uBuffer
)- 1 ] = 0 ;
697 uNumScanned
= u_sscanf ( uValue
, format
, uBuffer
);
698 if ( uNumScanned
!= 0 || uBuffer
[ 0 ] != 0x2a || uBuffer
[ 1 ] != 0x2a ) {
699 log_err ( " %s too much stored on a failure \n " , format
);
704 static void TestBadScanfFormat ( void ) {
705 #if !UCONFIG_NO_FORMATTING
706 static const UChar abcUChars
[] = { 0x61 , 0x62 , 0x63 , 0x63 , 0x64 , 0x65 , 0x66 , 0x67 , 0 };
707 static const char abcChars
[] = "abccdefg" ;
709 TestBadSScanfFormat ( "%[] " , abcUChars
, abcChars
);
713 static void Test_u_vfprintf ( const char * expectedResult
, const char * format
, ...) {
714 #if !UCONFIG_NO_FORMATTING
720 va_start ( ap
, format
);
721 count
= u_vsprintf ( uBuffer
, format
, ap
);
722 ( void ) count
; /* Suppress set but not used warning */
724 u_uastrcpy ( uBuffer2
, expectedResult
);
725 if ( u_strcmp ( uBuffer
, uBuffer2
) != 0 ) {
726 log_err ( "Got two different results for \" %s \" expected \" %s \"\n " , format
, expectedResult
);
729 u_uastrcpy ( uBuffer2
, format
);
730 va_start ( ap
, format
);
731 count
= u_vsprintf_u ( uBuffer
, uBuffer2
, ap
);
733 u_uastrcpy ( uBuffer2
, expectedResult
);
734 if ( u_strcmp ( uBuffer
, uBuffer2
) != 0 ) {
735 log_err ( "Got two different results for \" %s \" expected \" %s \"\n " , format
, expectedResult
);
740 static void TestVargs ( void ) {
741 #if !UCONFIG_NO_FORMATTING
742 Test_u_vfprintf ( "8 9 a B 8.9" , " %d %u %x %X %.1f" , 8 , 9 , 10 , 11 , 8.9 );
746 static void TestCount ( void ) {
747 #if !UCONFIG_NO_FORMATTING
748 static const UChar x15
[] = { 0x78 , 0x31 , 0x35 , 0 };
752 int32_t i32
= - 1 , actual_count
, actual_result
;
754 u_uastrcpy ( testStr
, "1233456789" );
755 if ( u_sscanf ( testStr
, "%*3[123] %n %*[1-9]" , & i32
) != 0 ) {
756 log_err ( "test 1: scanf did not return 0 \n " );
759 log_err ( "test 1: scanf returned %h d instead of 3 \n " , i32
);
761 if ( u_sscanf ( testStr
, "%*4[123] %h n%*[1-9]" , & i16
) != 0 ) {
762 log_err ( "test 2: scanf did not return 0 \n " );
765 log_err ( "test 2: scanf returned %d instead of 4 \n " , i16
);
767 if ( u_sscanf ( testStr
, "%*[123]%*[1-9] %l ln" , & i64
) != 0 ) {
768 log_err ( "test 3: scanf did not return 0 \n " );
771 log_err ( "test 3: scanf did not return 10 \n " , i64
);
773 actual_result
= u_sscanf ( x15
, " %C%d%n " , & character
, & i32
, & actual_count
);
774 if ( actual_result
!= 2 ) {
775 log_err ( "scanf should return 2, but returned %d \n " , actual_result
);
777 if ( character
!= 0x78 ) {
778 log_err ( "scanf should return 0x78 for the character, but returned %X \n " , character
);
781 log_err ( "scanf should return 15 for the number, but returned %d \n " , i32
);
783 if ( actual_count
!= 3 ) {
784 log_err ( "scanf should return 3 for actual_count, but returned %d \n " , actual_count
);
790 addStringTest ( TestNode
** root
) {
791 #if !UCONFIG_NO_FORMATTING
792 addTest ( root
, & TestString
, "string/TestString" );
793 addTest ( root
, & TestLocalizedString
, "string/TestLocalizedString" );
794 addTest ( root
, & TestSprintfFormat
, "string/TestSprintfFormat" );
795 addTest ( root
, & TestSnprintf
, "string/TestSnprintf" );
796 addTest ( root
, & TestSScanset
, "string/TestSScanset" );
797 addTest ( root
, & TestStringCompatibility
, "string/TestStringCompatibility" );
798 addTest ( root
, & TestBadScanfFormat
, "string/TestBadScanfFormat" );
799 addTest ( root
, & TestVargs
, "string/TestVargs" );
800 addTest ( root
, & TestCount
, "string/TestCount" );