]>
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) UPRV_BLOCK_MACRO_BEGIN { \
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);\
327 } UPRV_BLOCK_MACRO_END
331 static void TestSnprintf ( void ) {
332 #if !UCONFIG_NO_FORMATTING
334 char cTestResult
[ 256 ];
337 Test_u_snprintf ( 0 , " %d " , 123 , 3 , "xxxxxxxxxxxxxx" );
338 Test_u_snprintf ( 2 , " %d " , 123 , 3 , "12xxxxxxxxxxxx" );
339 Test_u_snprintf ( 3 , " %d " , 123 , 3 , "123xxxxxxxxxxx" );
340 Test_u_snprintf ( 4 , " %d " , 123 , 3 , "123" );
342 Test_u_snprintf ( 0 , " %s " , "abcd" , 4 , "xxxxxxxxxxxxxx" );
343 Test_u_snprintf ( 3 , " %s " , "abcd" , 4 , "abcxxxxxxxxxxx" );
344 Test_u_snprintf ( 4 , " %s " , "abcd" , 4 , "abcdxxxxxxxxxx" );
345 Test_u_snprintf ( 5 , " %s " , "abcd" , 4 , "abcd" );
347 Test_u_snprintf ( 0 , " %e " , 12.34 , 13 , "xxxxxxxxxxxxxx" );
348 Test_u_snprintf ( 1 , " %e " , 12.34 , 13 , "1xxxxxxxxxxxxx" );
349 Test_u_snprintf ( 2 , " %e " , 12.34 , 13 , "1.xxxxxxxxxxxx" );
350 Test_u_snprintf ( 3 , " %e " , 12.34 , 13 , "1.2xxxxxxxxxxx" );
351 Test_u_snprintf ( 5 , " %e " , 12.34 , 13 , "1.234xxxxxxxxx" );
352 Test_u_snprintf ( 6 , " %e " , 12.34 , 13 , "1.2340xxxxxxxx" );
353 Test_u_snprintf ( 8 , " %e " , 12.34 , 13 , "1.234000xxxxxx" );
354 Test_u_snprintf ( 9 , " %e " , 12.34 , 13 , "1.234000exxxxx" );
355 Test_u_snprintf ( 10 , " %e " , 12.34 , 13 , "1.234000e+xxxx" );
356 Test_u_snprintf ( 11 , " %e " , 12.34 , 13 , "1.234000e+0xxx" );
357 Test_u_snprintf ( 13 , " %e " , 12.34 , 13 , "1.234000e+001x" );
358 Test_u_snprintf ( 14 , " %e " , 12.34 , 13 , "1.234000e+001" );
362 #define TestSPrintFormat(uFormat, uValue, cFormat, cValue) UPRV_BLOCK_MACRO_BEGIN { \
363 /* Reinitialize the buffer to verify null termination works. */ \
364 u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer));\
365 memset(buffer, '*' , UPRV_LENGTHOF(buffer));\
367 uNumPrinted = u_sprintf(uBuffer, uFormat, uValue);\
368 u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer));\
369 cNumPrinted = sprintf(buffer, cFormat, cValue);\
370 if (strcmp(buffer, compBuffer) != 0) {\
371 log_err( "%" uFormat " Got: \" %s \" , Expected: \" %s \"\n " , compBuffer, buffer);\
373 if (cNumPrinted != uNumPrinted) {\
374 log_err( "%" uFormat " number printed Got: %d , Expected: %d \n " , uNumPrinted, cNumPrinted);\
376 if (buffer[uNumPrinted+1] != '*' ) {\
377 log_err( "%" uFormat " too much stored \n " );\
379 } UPRV_BLOCK_MACRO_END
381 static void TestSprintfFormat ( void ) {
382 #if !UCONFIG_NO_FORMATTING
383 static const UChar abcUChars
[] = { 0x61 , 0x62 , 0x63 , 0 };
384 static const char abcChars
[] = "abc" ;
385 const char * reorderFormat
= " %2 $d==> %1 $-10.10s %6 $lld %4 $-10.10s %3 $#x(( %5 $d" ; /* reordering test*/
386 const char * reorderResult
= "99==>truncateif 1311768467463790322 1234567890 0xf1b93((10" ;
389 char compBuffer
[ 256 ];
394 TestSPrintFormat ( " %8 S" , abcUChars
, " %8 s" , abcChars
);
395 TestSPrintFormat ( "%-8S" , abcUChars
, "%-8s" , abcChars
);
396 TestSPrintFormat ( "%.2S" , abcUChars
, "%.2s" , abcChars
); /* strlen is 3 */
398 TestSPrintFormat ( " %8 s" , abcChars
, " %8 s" , abcChars
);
399 TestSPrintFormat ( "%-8s" , abcChars
, "%-8s" , abcChars
);
400 TestSPrintFormat ( "%.2s" , abcChars
, "%.2s" , abcChars
); /* strlen is 3 */
402 TestSPrintFormat ( " %8 c" , ( char ) 'e' , " %8 c" , ( char ) 'e' );
403 TestSPrintFormat ( "%-8c" , ( char ) 'e' , "%-8c" , ( char ) 'e' );
405 TestSPrintFormat ( " %8 C" , ( UChar
) 0x65 , " %8 c" , ( char ) 'e' );
406 TestSPrintFormat ( "%-8C" , ( UChar
) 0x65 , "%-8c" , ( char ) 'e' );
408 TestSPrintFormat ( " %f " , 1.23456789 , " %f " , 1.23456789 );
409 TestSPrintFormat ( " %f " , 12345.6789 , " %f " , 12345.6789 );
410 TestSPrintFormat ( " %f " , 123456.789 , " %f " , 123456.789 );
411 TestSPrintFormat ( " %f " , 1234567.89 , " %f " , 1234567.89 );
412 TestSPrintFormat ( " %1 0f" , 1.23456789 , " %1 0f" , 1.23456789 );
413 TestSPrintFormat ( "%-10f" , 1.23456789 , "%-10f" , 1.23456789 );
414 TestSPrintFormat ( " %1 0f" , 123.456789 , " %1 0f" , 123.456789 );
415 TestSPrintFormat ( " %1 0.4f" , 123.456789 , " %1 0.4f" , 123.456789 );
416 TestSPrintFormat ( "%-10f" , 123.456789 , "%-10f" , 123.456789 );
418 /* TestSPrintFormat("%g", 12345.6789, "%g", 12345.6789);
419 TestSPrintFormat("%g", 123456.789, "%g", 123456.789);
420 TestSPrintFormat("%g", 1234567.89, "%g", 1234567.89);
421 TestSPrintFormat("%G", 123456.789, "%G", 123456.789);
422 TestSPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
423 TestSPrintFormat ( " %1 0g" , 1.23456789 , " %1 0g" , 1.23456789 );
424 TestSPrintFormat ( " %1 0.4g" , 1.23456789 , " %1 0.4g" , 1.23456789 );
425 TestSPrintFormat ( "%-10g" , 1.23456789 , "%-10g" , 1.23456789 );
426 TestSPrintFormat ( " %1 0g" , 123.456789 , " %1 0g" , 123.456789 );
427 TestSPrintFormat ( "%-10g" , 123.456789 , "%-10g" , 123.456789 );
429 TestSPrintFormat ( " %8 x" , 123456 , " %8 x" , 123456 );
430 TestSPrintFormat ( "%-8x" , 123456 , "%-8x" , 123456 );
431 TestSPrintFormat ( " %0 8x" , 123456 , " %0 8x" , 123456 );
433 TestSPrintFormat ( " %8 X" , 123456 , " %8 X" , 123456 );
434 TestSPrintFormat ( "%-8X" , 123456 , "%-8X" , 123456 );
435 TestSPrintFormat ( " %0 8X" , 123456 , " %0 8X" , 123456 );
436 TestSPrintFormat ( "%#x" , 123456 , "%#x" , 123456 );
437 TestSPrintFormat ( "%#x" , - 123456 , "%#x" , - 123456 );
439 TestSPrintFormat ( " %8 o" , 123456 , " %8 o" , 123456 );
440 TestSPrintFormat ( "%-8o" , 123456 , "%-8o" , 123456 );
441 TestSPrintFormat ( " %0 8o" , 123456 , " %0 8o" , 123456 );
442 TestSPrintFormat ( "%#o" , 123 , "%#o" , 123 );
443 TestSPrintFormat ( "%#o" , - 123 , "%#o" , - 123 );
445 TestSPrintFormat ( " %8 u" , 123456 , " %8 u" , 123456 );
446 TestSPrintFormat ( "%-8u" , 123456 , "%-8u" , 123456 );
447 TestSPrintFormat ( " %0 8u" , 123456 , " %0 8u" , 123456 );
448 TestSPrintFormat ( " %8 u" , - 123456 , " %8 u" , - 123456 );
449 TestSPrintFormat ( "%-8u" , - 123456 , "%-8u" , - 123456 );
450 TestSPrintFormat ( "%.5u" , 123456 , "%.5u" , 123456 );
451 TestSPrintFormat ( "%.6u" , 123456 , "%.6u" , 123456 );
452 TestSPrintFormat ( "%.7u" , 123456 , "%.7u" , 123456 );
454 TestSPrintFormat ( " %8 d" , 123456 , " %8 d" , 123456 );
455 TestSPrintFormat ( "%-8d" , 123456 , "%-8d" , 123456 );
456 TestSPrintFormat ( " %0 8d" , 123456 , " %0 8d" , 123456 );
457 TestSPrintFormat ( "% d" , 123456 , "% d" , 123456 );
458 TestSPrintFormat ( "% d" , - 123456 , "% d" , - 123456 );
460 TestSPrintFormat ( " %8 i" , 123456 , " %8 i" , 123456 );
461 TestSPrintFormat ( "%-8i" , 123456 , "%-8i" , 123456 );
462 TestSPrintFormat ( " %0 8i" , 123456 , " %0 8i" , 123456 );
464 log_verbose ( "Get really crazy with the formatting. \n " );
466 TestSPrintFormat ( "%-#12x" , 123 , "%-#12x" , 123 );
467 TestSPrintFormat ( "%-#12x" , - 123 , "%-#12x" , - 123 );
468 TestSPrintFormat ( "%#12x" , 123 , "%#12x" , 123 );
469 TestSPrintFormat ( "%#12x" , - 123 , "%#12x" , - 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 ( "% 12d" , 123 , "% 12d" , 123 );
478 TestSPrintFormat ( "% 12d" , - 123 , "% 12d" , - 123 );
479 TestSPrintFormat ( " %1 2d" , 123 , " %1 2d" , 123 );
480 TestSPrintFormat ( " %1 2d" , - 123 , " %1 2d" , - 123 );
481 TestSPrintFormat ( "%.12d" , 123 , "%.12d" , 123 );
482 TestSPrintFormat ( "%.12d" , - 123 , "%.12d" , - 123 );
484 TestSPrintFormat ( "%-+12.1f" , 1.234 , "%-+12.1f" , 1.234 );
485 TestSPrintFormat ( "%-+12.1f" , - 1.234 , "%-+12.1f" , - 1.234 );
486 TestSPrintFormat ( "%- 12.10f" , 1.234 , "%- 12.10f" , 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 ( "% 12.1f" , 1.234 , "% 12.1f" , 1.234 );
491 TestSPrintFormat ( "% 12.1f" , - 1.234 , "% 12.1f" , - 1.234 );
492 TestSPrintFormat ( " %1 2.1f" , 1.234 , " %1 2.1f" , 1.234 );
493 TestSPrintFormat ( " %1 2.1f" , - 1.234 , " %1 2.1f" , - 1.234 );
494 TestSPrintFormat ( "%.2f" , 1.234 , "%.2f" , 1.234 );
495 TestSPrintFormat ( "%.2f" , - 1.234 , "%.2f" , - 1.234 );
496 TestSPrintFormat ( " %3 f" , 1.234 , " %3 f" , 1.234 );
497 TestSPrintFormat ( " %3 f" , - 1.234 , " %3 f" , - 1.234 );
499 /* Test reordering format */
500 u_sprintf ( uBuffer
, reorderFormat
, "truncateiftoolong" , 99 , 990099 , "12345678901234567890" , 10 , 0x123456789abcdef2 LL
);
501 u_austrncpy ( compBuffer
, uBuffer
, UPRV_LENGTHOF ( uBuffer
));
503 if ( strcmp ( compBuffer
, reorderResult
) != 0 ) {
504 log_err ( " %s Got: \" %s \" , Expected: \" %s \"\n " , reorderFormat
, compBuffer
, buffer
);
509 #undef TestSPrintFormat
511 static void TestStringCompatibility ( void ) {
512 #if !UCONFIG_NO_FORMATTING
513 UChar myUString
[ 256 ];
514 UChar uStringBuf
[ 256 ];
515 char myString
[ 256 ] = "" ;
516 char testBuf
[ 256 ] = "" ;
519 u_memset ( myUString
, 0x0a , UPRV_LENGTHOF ( myUString
));
520 u_memset ( uStringBuf
, 0x0a , UPRV_LENGTHOF ( uStringBuf
));
522 /* Compare against C API compatibility */
523 for ( num
= - STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
524 sprintf ( testBuf
, " %x " , ( int ) num
);
525 u_sprintf ( uStringBuf
, " %x " , num
);
526 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
527 if ( strcmp ( myString
, testBuf
) != 0 ) {
528 log_err ( "% %x Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
531 sprintf ( testBuf
, " %X " , ( int ) num
);
532 u_sprintf ( uStringBuf
, " %X " , num
);
533 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
534 if ( strcmp ( myString
, testBuf
) != 0 ) {
535 log_err ( "% %X Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
538 sprintf ( testBuf
, " %o " , ( int ) num
);
539 u_sprintf ( uStringBuf
, " %o " , num
);
540 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
541 if ( strcmp ( myString
, testBuf
) != 0 ) {
542 log_err ( "% %o Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
545 /* sprintf is not compatible on all platforms e.g. the iSeries*/
546 sprintf ( testBuf
, " %d " , ( int ) num
);
547 u_sprintf ( uStringBuf
, " %d " , num
);
548 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
549 if ( strcmp ( myString
, testBuf
) != 0 ) {
550 log_err ( "% %d Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
553 sprintf ( testBuf
, " %i " , ( int ) num
);
554 u_sprintf ( uStringBuf
, " %i " , num
);
555 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
556 if ( strcmp ( myString
, testBuf
) != 0 ) {
557 log_err ( "% %i Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
560 sprintf ( testBuf
, " %f " , ( double ) num
);
561 u_sprintf ( uStringBuf
, " %f " , ( double ) num
);
562 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
563 if ( strcmp ( myString
, testBuf
) != 0 ) {
564 log_err ( "% %f Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
567 /* sprintf(testBuf, "%e", (double)num);
568 u_sprintf(uStringBuf, "%e", (double)num);
569 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
570 if (strcmp(myString, testBuf) != 0) {
571 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
574 sprintf(testBuf, "%E", (double)num);
575 u_sprintf(uStringBuf, "%E", (double)num);
576 u_austrncpy(myString, uStringBuf, UPRV_LENGTHOF(myString));
577 if (strcmp(myString, testBuf) != 0) {
578 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
581 sprintf ( testBuf
, " %g " , ( double ) num
);
582 u_sprintf ( uStringBuf
, " %g " , ( double ) num
);
583 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
584 if ( strcmp ( myString
, testBuf
) != 0 ) {
585 log_err ( "% %g Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
588 sprintf ( testBuf
, " %G " , ( double ) num
);
589 u_sprintf ( uStringBuf
, " %G " , ( double ) num
);
590 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
591 if ( strcmp ( myString
, testBuf
) != 0 ) {
592 log_err ( "% %G Got: \" %s \" , Expected: \" %s \"\n " , myString
, testBuf
);
596 for ( num
= 0 ; num
< 0x80 ; num
++) {
597 testBuf
[ 0 ] = ( char ) 0xFF ;
598 uStringBuf
[ 0 ] = ( UChar
) 0xfffe ;
599 sprintf ( testBuf
, " %c " , ( char ) num
);
600 u_sprintf ( uStringBuf
, " %c " , num
);
601 u_austrncpy ( myString
, uStringBuf
, UPRV_LENGTHOF ( myString
));
602 if ( testBuf
[ 0 ] != myString
[ 0 ] || myString
[ 0 ] != num
) {
603 log_err ( "% %c Got: 0x %x , Expected: 0x %x \n " , myString
[ 0 ], testBuf
[ 0 ]);
609 static void TestSScanSetFormat ( const char * format
, const UChar
* uValue
, const char * cValue
, UBool expectedToPass
) {
610 #if !UCONFIG_NO_FORMATTING
613 char compBuffer
[ 256 ];
617 /* Reinitialize the buffer to verify null termination works. */
618 u_memset ( uBuffer
, 0x2a , UPRV_LENGTHOF ( uBuffer
));
619 uBuffer
[ UPRV_LENGTHOF ( uBuffer
)- 1 ] = 0 ;
620 memset ( buffer
, '*' , UPRV_LENGTHOF ( buffer
));
621 buffer
[ UPRV_LENGTHOF ( buffer
)- 1 ] = 0 ;
623 uNumScanned
= u_sscanf ( uValue
, format
, uBuffer
);
624 if ( expectedToPass
) {
625 u_austrncpy ( compBuffer
, uBuffer
, UPRV_LENGTHOF ( uBuffer
));
626 cNumScanned
= sscanf ( cValue
, format
, buffer
);
627 if ( strncmp ( buffer
, compBuffer
, UPRV_LENGTHOF ( uBuffer
)) != 0 ) {
628 log_err ( " %s Got: \" %s \" , Expected: \" %s \"\n " , format
, compBuffer
, buffer
);
630 if ( cNumScanned
!= uNumScanned
) {
631 log_err ( " %s number scanned Got: %d , Expected: %d \n " , format
, uNumScanned
, cNumScanned
);
633 if ( uNumScanned
> 0 && uBuffer
[ u_strlen ( uBuffer
)+ 1 ] != 0x2a ) {
634 log_err ( " %s too much stored \n " , format
);
638 if ( uNumScanned
!= 0 || uBuffer
[ 0 ] != 0x2a || uBuffer
[ 1 ] != 0x2a ) {
639 log_err ( " %s too much stored on a failure \n " , format
);
645 static void TestSScanset ( void ) {
646 #if !UCONFIG_NO_FORMATTING
647 static const UChar abcUChars
[] = { 0x61 , 0x62 , 0x63 , 0x63 , 0x64 , 0x65 , 0x66 , 0x67 , 0 };
648 static const char abcChars
[] = "abccdefg" ;
650 TestSScanSetFormat ( "%[bc]S" , abcUChars
, abcChars
, TRUE
);
651 TestSScanSetFormat ( "%[cb]S" , abcUChars
, abcChars
, TRUE
);
653 TestSScanSetFormat ( "%[ab]S" , abcUChars
, abcChars
, TRUE
);
654 TestSScanSetFormat ( "%[ba]S" , abcUChars
, abcChars
, TRUE
);
656 TestSScanSetFormat ( "%[ab]" , abcUChars
, abcChars
, TRUE
);
657 TestSScanSetFormat ( "%[ba]" , abcUChars
, abcChars
, TRUE
);
659 TestSScanSetFormat ( "%[abcdefgh]" , abcUChars
, abcChars
, TRUE
);
660 TestSScanSetFormat ( "%[;hgfedcba]" , abcUChars
, abcChars
, TRUE
);
662 TestSScanSetFormat ( "%[^a]" , abcUChars
, abcChars
, TRUE
);
663 TestSScanSetFormat ( "%[^e]" , abcUChars
, abcChars
, TRUE
);
664 TestSScanSetFormat ( "%[^ed]" , abcUChars
, abcChars
, TRUE
);
665 TestSScanSetFormat ( "%[^dc]" , abcUChars
, abcChars
, TRUE
);
666 TestSScanSetFormat ( "%[^e] " , abcUChars
, abcChars
, TRUE
);
668 TestSScanSetFormat ( " %1 [ab] " , abcUChars
, abcChars
, TRUE
);
669 TestSScanSetFormat ( " %2 [^f]" , abcUChars
, abcChars
, TRUE
);
671 TestSScanSetFormat ( "%[qrst]" , abcUChars
, abcChars
, TRUE
);
673 /* Extra long string for testing */
674 TestSScanSetFormat ( " %[qrst]" ,
675 abcUChars
, abcChars
, TRUE
);
677 TestSScanSetFormat ( "%[a-]" , abcUChars
, abcChars
, TRUE
);
680 TestSScanSetFormat ( "%[a" , abcUChars
, abcChars
, FALSE
);
681 TestSScanSetFormat ( "%[f-a]" , abcUChars
, abcChars
, FALSE
);
682 TestSScanSetFormat ( "%[c-a]" , abcUChars
, abcChars
, FALSE
);
683 /* The following is not deterministic on Windows */
684 /* TestSScanSetFormat("%[a-", abcUChars, abcChars);*/
686 /* TODO: Need to specify precision with a "*" */
690 static void TestBadSScanfFormat ( const char * format
, const UChar
* uValue
, const char * cValue
) {
691 #if !UCONFIG_NO_FORMATTING
692 ( void ) cValue
; // suppress compiler warnings about unused variable
696 /* Reinitialize the buffer to verify null termination works. */
697 u_memset ( uBuffer
, 0x2a , UPRV_LENGTHOF ( uBuffer
));
698 uBuffer
[ UPRV_LENGTHOF ( uBuffer
)- 1 ] = 0 ;
700 uNumScanned
= u_sscanf ( uValue
, format
, uBuffer
);
701 if ( uNumScanned
!= 0 || uBuffer
[ 0 ] != 0x2a || uBuffer
[ 1 ] != 0x2a ) {
702 log_err ( " %s too much stored on a failure \n " , format
);
707 static void TestBadScanfFormat ( void ) {
708 #if !UCONFIG_NO_FORMATTING
709 static const UChar abcUChars
[] = { 0x61 , 0x62 , 0x63 , 0x63 , 0x64 , 0x65 , 0x66 , 0x67 , 0 };
710 static const char abcChars
[] = "abccdefg" ;
712 TestBadSScanfFormat ( "%[] " , abcUChars
, abcChars
);
716 static void Test_u_vfprintf ( const char * expectedResult
, const char * format
, ...) {
717 #if !UCONFIG_NO_FORMATTING
723 va_start ( ap
, format
);
724 count
= u_vsprintf ( uBuffer
, format
, ap
);
725 ( void ) count
; /* Suppress set but not used warning */
727 u_uastrcpy ( uBuffer2
, expectedResult
);
728 if ( u_strcmp ( uBuffer
, uBuffer2
) != 0 ) {
729 log_err ( "Got two different results for \" %s \" expected \" %s \"\n " , format
, expectedResult
);
732 u_uastrcpy ( uBuffer2
, format
);
733 va_start ( ap
, format
);
734 count
= u_vsprintf_u ( uBuffer
, uBuffer2
, ap
);
736 u_uastrcpy ( uBuffer2
, expectedResult
);
737 if ( u_strcmp ( uBuffer
, uBuffer2
) != 0 ) {
738 log_err ( "Got two different results for \" %s \" expected \" %s \"\n " , format
, expectedResult
);
743 static void TestVargs ( void ) {
744 #if !UCONFIG_NO_FORMATTING
745 Test_u_vfprintf ( "8 9 a B 8.9" , " %d %u %x %X %.1f" , 8 , 9 , 10 , 11 , 8.9 );
749 static void TestCount ( void ) {
750 #if !UCONFIG_NO_FORMATTING
751 static const UChar x15
[] = { 0x78 , 0x31 , 0x35 , 0 };
755 int32_t i32
= - 1 , actual_count
, actual_result
;
757 u_uastrcpy ( testStr
, "1233456789" );
758 if ( u_sscanf ( testStr
, "%*3[123] %n %*[1-9]" , & i32
) != 0 ) {
759 log_err ( "test 1: scanf did not return 0 \n " );
762 log_err ( "test 1: scanf returned %h d instead of 3 \n " , i32
);
764 if ( u_sscanf ( testStr
, "%*4[123] %h n%*[1-9]" , & i16
) != 0 ) {
765 log_err ( "test 2: scanf did not return 0 \n " );
768 log_err ( "test 2: scanf returned %d instead of 4 \n " , i16
);
770 if ( u_sscanf ( testStr
, "%*[123]%*[1-9] %l ln" , & i64
) != 0 ) {
771 log_err ( "test 3: scanf did not return 0 \n " );
774 log_err ( "test 3: scanf did not return 10 \n " , i64
);
776 actual_result
= u_sscanf ( x15
, " %C%d%n " , & character
, & i32
, & actual_count
);
777 if ( actual_result
!= 2 ) {
778 log_err ( "scanf should return 2, but returned %d \n " , actual_result
);
780 if ( character
!= 0x78 ) {
781 log_err ( "scanf should return 0x78 for the character, but returned %X \n " , character
);
784 log_err ( "scanf should return 15 for the number, but returned %d \n " , i32
);
786 if ( actual_count
!= 3 ) {
787 log_err ( "scanf should return 3 for actual_count, but returned %d \n " , actual_count
);
793 addStringTest ( TestNode
** root
) {
794 #if !UCONFIG_NO_FORMATTING
795 addTest ( root
, & TestString
, "string/TestString" );
796 addTest ( root
, & TestLocalizedString
, "string/TestLocalizedString" );
797 addTest ( root
, & TestSprintfFormat
, "string/TestSprintfFormat" );
798 addTest ( root
, & TestSnprintf
, "string/TestSnprintf" );
799 addTest ( root
, & TestSScanset
, "string/TestSScanset" );
800 addTest ( root
, & TestStringCompatibility
, "string/TestStringCompatibility" );
801 addTest ( root
, & TestBadScanfFormat
, "string/TestBadScanfFormat" );
802 addTest ( root
, & TestVargs
, "string/TestVargs" );
803 addTest ( root
, & TestCount
, "string/TestCount" );