2002-08-04 07:41:48 +00:00
/*
2002-02-23 00:34:39 +00:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2003-06-03 20:58:22 +00:00
* Copyright ( C ) 2002 - 2003 , International Business Machines
2002-02-23 00:34:39 +00:00
* Corporation and others . All Rights Reserved .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* file name : iotest . cpp
* encoding : US - ASCII
* tab size : 8 ( not used )
* indentation : 4
*
* created on : 2002f eb21
* created by : George Rhoten
*/
2002-03-01 23:00:41 +00:00
# include "unicode/ustdio.h"
# include "unicode/ustream.h"
2002-03-26 05:28:54 +00:00
# include "unicode/uclean.h"
2002-02-23 00:34:39 +00:00
2002-03-01 23:00:41 +00:00
# include "unicode/ucnv.h"
2002-08-24 00:00:31 +00:00
# include "unicode/uloc.h"
2002-03-01 23:00:41 +00:00
# include "unicode/unistr.h"
# include "unicode/ustring.h"
# include "unicode/ctest.h"
2002-04-10 23:32:18 +00:00
# include "unicode/utrans.h"
2002-02-23 00:34:39 +00:00
# include "ustr_imp.h"
# if U_IOSTREAM_SOURCE >= 199711
# include <iostream>
# include <strstream>
using namespace std ;
# elif U_IOSTREAM_SOURCE >= 198506
# include <iostream.h>
2002-03-01 23:00:41 +00:00
# include <strstream.h>
2002-02-23 00:34:39 +00:00
# endif
# include <string.h>
static char STANDARD_TEST_FILE [ ] = " iotest-c.txt " ;
static const int32_t STANDARD_TEST_NUM_RANGE = 1000 ;
# ifdef WIN32
static const UChar NEW_LINE [ ] = { 0x0d , 0x0a , 0 } ;
# define C_NEW_LINE "\r\n"
# else
static const UChar NEW_LINE [ ] = { 0x0a , 0 } ;
# define C_NEW_LINE "\n"
# endif
2002-03-15 00:29:57 +00:00
static void TestFileFromICU ( UFILE * myFile ) {
2002-02-23 00:34:39 +00:00
int32_t n [ 1 ] ;
float myFloat = - 1234.0 ;
int32_t newValuePtr [ 1 ] ;
double newDoubleValuePtr [ 1 ] ;
2002-03-15 00:29:57 +00:00
UChar myUString [ 256 ] ;
UChar uStringBuf [ 256 ] ;
char myString [ 256 ] = " " ;
char testBuf [ 256 ] = " " ;
2004-03-16 06:00:02 +00:00
U_STRING_DECL ( myStringOrig , " My-String " , 9 ) ;
2002-02-23 00:34:39 +00:00
2004-03-16 06:00:02 +00:00
U_STRING_INIT ( myStringOrig , " My-String " , 9 ) ;
2002-03-26 20:10:52 +00:00
u_memset ( myUString , 0x2a , sizeof ( myUString ) / sizeof ( * myUString ) ) ;
u_memset ( uStringBuf , 0x2a , sizeof ( uStringBuf ) / sizeof ( * uStringBuf ) ) ;
memset ( myString , 0x2a , sizeof ( myString ) / sizeof ( * myString ) ) ;
memset ( testBuf , 0x2a , sizeof ( testBuf ) / sizeof ( * testBuf ) ) ;
2002-02-23 00:34:39 +00:00
if ( myFile = = NULL ) {
log_err ( " Can't write test file. " ) ;
return ;
}
* n = - 1234 ;
/* Test fprintf */
2002-03-22 00:01:46 +00:00
u_fprintf ( myFile , " Signed decimal integer %%d: %d \n " , * n ) ;
u_fprintf ( myFile , " Signed decimal integer %%i: %i \n " , * n ) ;
u_fprintf ( myFile , " Unsigned octal integer %%o: %o \n " , * n ) ;
u_fprintf ( myFile , " Unsigned decimal integer %%u: %u \n " , * n ) ;
u_fprintf ( myFile , " Lowercase unsigned hexadecimal integer %%x: %x \n " , * n ) ;
u_fprintf ( myFile , " Uppercase unsigned hexadecimal integer %%X: %X \n " , * n ) ;
u_fprintf ( myFile , " Float %%f: %f \n " , myFloat ) ;
u_fprintf ( myFile , " Lowercase float %%e: %e \n " , myFloat ) ;
u_fprintf ( myFile , " Uppercase float %%E: %E \n " , myFloat ) ;
u_fprintf ( myFile , " Lowercase float %%g: %g \n " , myFloat ) ;
u_fprintf ( myFile , " Uppercase float %%G: %G \n " , myFloat ) ;
// u_fprintf(myFile, "Pointer %%p: %p\n", myFile);
u_fprintf ( myFile , " Char %%c: %c \n " , ' A ' ) ;
2004-03-16 06:00:02 +00:00
u_fprintf ( myFile , " UChar %%C: %C \n " , ( UChar ) 0x0041 ) ; /*'A'*/
2002-03-22 00:01:46 +00:00
u_fprintf ( myFile , " String %%s: %s \n " , " My-String " ) ;
2002-03-26 20:10:52 +00:00
u_fprintf ( myFile , " NULL String %%s: %s \n " , NULL ) ;
2004-03-16 06:00:02 +00:00
u_fprintf ( myFile , " Unicode String %%S: %S \n " , myStringOrig ) ;
2003-09-25 21:14:35 +00:00
u_fprintf ( myFile , " NULL Unicode String %%S: %S \n " , NULL ) ;
2002-02-23 00:34:39 +00:00
u_fprintf ( myFile , " Percent %%P (non-ANSI): %P \n " , myFloat ) ;
2002-02-26 21:23:21 +00:00
u_fprintf ( myFile , " Spell Out %%V (non-ANSI): %V \n " , myFloat ) ;
2002-02-23 00:34:39 +00:00
* n = 1 ;
2002-03-27 20:08:42 +00:00
u_fprintf ( myFile , " \t \n Pointer to integer (Count) %%n: n=%d %n n=%d \n " , * n , n , * n ) ;
2002-02-23 00:34:39 +00:00
u_fprintf ( myFile , " Pointer to integer Value: %d \n " , * n ) ;
2003-03-04 19:12:55 +00:00
u_fprintf ( myFile , " This is a long test123456789012345678901234567890123456789012345678901234567890 \n " ) ;
2002-02-23 00:34:39 +00:00
* n = 1 ;
fprintf ( u_fgetfile ( myFile ) , " \t Normal fprintf count: n=%d %n n=%d \n " , * n , n , * n ) ;
fprintf ( u_fgetfile ( myFile ) , " \t Normal fprintf count value: n=%d \n " , * n ) ;
u_fclose ( myFile ) ;
myFile = u_fopen ( STANDARD_TEST_FILE , " r " , NULL , NULL ) ;
if ( myFile = = NULL ) {
log_err ( " Can't read test file. " ) ;
return ;
}
2003-07-12 07:39:13 +00:00
myUString [ 0 ] = u_fgetc ( myFile ) ;
if ( myUString [ 0 ] ! = 0x53 /* S */ ) {
2002-03-26 20:10:52 +00:00
log_err ( " u_fgetc 1 returned %X. Expected 'S'. " , myString [ 0 ] ) ;
}
2003-07-12 07:39:13 +00:00
u_fungetc ( myUString [ 0 ] , myFile ) ;
myUString [ 0 ] = u_fgetc ( myFile ) ;
if ( myUString [ 0 ] ! = 0x53 /* S */ ) {
2002-03-26 20:10:52 +00:00
log_err ( " u_fgetc 2 returned %X. Expected 'S'. " , myString [ 0 ] ) ;
}
2003-07-12 07:39:13 +00:00
u_fungetc ( myUString [ 0 ] , myFile ) ;
myUString [ 0 ] = u_fgetc ( myFile ) ;
if ( myUString [ 0 ] ! = 0x53 /* S */ ) {
2002-03-26 20:10:52 +00:00
log_err ( " u_fgetc 3 returned %X. Expected 'S'. " , myString [ 0 ] ) ;
}
2003-07-12 07:39:13 +00:00
u_fungetc ( myUString [ 0 ] , myFile ) ;
2003-10-11 02:16:12 +00:00
myUString [ 0 ] = u_fgetc ( myFile ) ;
2004-03-16 19:02:00 +00:00
myUString [ 1 ] = ( UChar ) u_fgetcx ( myFile ) ; /* Mix getc and getcx and see what happens. */
2003-10-11 02:16:12 +00:00
myUString [ 2 ] = u_fgetc ( myFile ) ;
2004-03-16 19:02:00 +00:00
if ( myUString [ 0 ] ! = 0x53 /* S */ & & myUString [ 1 ] ! = 0x69 /* i */ & & myUString [ 2 ] ! = 0x6E /* n */ ) {
2003-10-11 02:16:12 +00:00
log_err ( " u_fgetcx returned \\ u%04X \\ u%04X \\ u%04X. Expected 'Sin'. " , myString [ 0 ] , myString [ 1 ] , myString [ 2 ] ) ;
}
u_fungetc ( myUString [ 2 ] , myFile ) ;
u_fungetc ( myUString [ 1 ] , myFile ) ;
u_fungetc ( myUString [ 0 ] , myFile ) ;
* n = - 1234 ;
2002-03-26 20:10:52 +00:00
2002-02-23 00:34:39 +00:00
* newValuePtr = 1 ;
u_fscanf ( myFile , " Signed decimal integer %%d: %d \n " , newValuePtr ) ;
if ( * n ! = * newValuePtr ) {
log_err ( " %%d Got: %d, Expected: %d \n " , * newValuePtr , * n ) ;
}
* newValuePtr = 1 ;
2002-03-22 00:01:46 +00:00
u_fscanf ( myFile , " Signed decimal integer %%i: %i \n " , newValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( * n ! = * newValuePtr ) {
log_err ( " %%i Got: %i, Expected: %i \n " , * newValuePtr , * n ) ;
}
* newValuePtr = 1 ;
2002-03-22 00:01:46 +00:00
u_fscanf ( myFile , " Unsigned octal integer %%o: %o \n " , newValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( * n ! = * newValuePtr ) {
log_err ( " %%o Got: %o, Expected: %o \n " , * newValuePtr , * n ) ;
}
* newValuePtr = 1 ;
2002-03-22 00:01:46 +00:00
u_fscanf ( myFile , " Unsigned decimal integer %%u: %u \n " , newValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( * n ! = * newValuePtr ) {
log_err ( " %%u Got: %u, Expected: %u \n " , * newValuePtr , * n ) ;
}
* newValuePtr = 1 ;
2002-03-22 00:01:46 +00:00
u_fscanf ( myFile , " Lowercase unsigned hexadecimal integer %%x: %x \n " , newValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( * n ! = * newValuePtr ) {
log_err ( " %%x Got: %x, Expected: %x \n " , * newValuePtr , * n ) ;
}
* newValuePtr = 1 ;
2002-03-22 00:01:46 +00:00
u_fscanf ( myFile , " Uppercase unsigned hexadecimal integer %%X: %X \n " , newValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( * n ! = * newValuePtr ) {
log_err ( " %%X Got: %X, Expected: %X \n " , * newValuePtr , * n ) ;
}
* newDoubleValuePtr = - 1.0 ;
2002-03-22 00:01:46 +00:00
u_fscanf ( myFile , " Float %%f: %f \n " , newDoubleValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( myFloat ! = * newDoubleValuePtr ) {
log_err ( " %%f Got: %f, Expected: %f \n " , * newDoubleValuePtr , myFloat ) ;
}
* newDoubleValuePtr = - 1.0 ;
2002-03-22 00:01:46 +00:00
u_fscanf ( myFile , " Lowercase float %%e: %e \n " , newDoubleValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( myFloat ! = * newDoubleValuePtr ) {
log_err ( " %%e Got: %e, Expected: %e \n " , * newDoubleValuePtr , myFloat ) ;
}
* newDoubleValuePtr = - 1.0 ;
2002-03-22 00:01:46 +00:00
u_fscanf ( myFile , " Uppercase float %%E: %E \n " , newDoubleValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( myFloat ! = * newDoubleValuePtr ) {
log_err ( " %%E Got: %E, Expected: %E \n " , * newDoubleValuePtr , myFloat ) ;
}
* newDoubleValuePtr = - 1.0 ;
2002-03-22 00:01:46 +00:00
u_fscanf ( myFile , " Lowercase float %%g: %g \n " , newDoubleValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( myFloat ! = * newDoubleValuePtr ) {
log_err ( " %%g Got: %g, Expected: %g \n " , * newDoubleValuePtr , myFloat ) ;
}
* newDoubleValuePtr = - 1.0 ;
2002-03-22 00:01:46 +00:00
u_fscanf ( myFile , " Uppercase float %%G: %G \n " , newDoubleValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( myFloat ! = * newDoubleValuePtr ) {
log_err ( " %%G Got: %G, Expected: %G \n " , * newDoubleValuePtr , myFloat ) ;
}
2002-03-22 00:01:46 +00:00
// u_fscanf(myFile, "Pointer %%p: %p\n", newDoubleValue);
u_fscanf ( myFile , " Char %%c: %c \n " , myString ) ;
2002-02-23 00:34:39 +00:00
if ( * myString ! = ' A ' ) {
log_err ( " %%c Got: %c, Expected: A \n " , * myString ) ;
}
2003-09-25 21:14:35 +00:00
u_fscanf ( myFile , " UChar %%C: %C \n " , myUString ) ;
2004-03-16 06:00:02 +00:00
if ( * myUString ! = ( UChar ) 0x0041 ) { /*'A'*/
2002-02-23 00:34:39 +00:00
log_err ( " %%C Got: %C, Expected: A \n " , * myUString ) ;
}
2002-03-22 00:01:46 +00:00
u_fscanf ( myFile , " String %%s: %s \n " , myString ) ;
2002-02-23 00:34:39 +00:00
if ( strcmp ( myString , " My-String " ) ) {
log_err ( " %%s Got: %s, Expected: My String \n " , myString ) ;
}
2002-03-26 20:10:52 +00:00
u_fscanf ( myFile , " NULL String %%s: %s \n " , myString ) ;
if ( strcmp ( myString , " (null) " ) ) {
log_err ( " %%s Got: %s, Expected: My String \n " , myString ) ;
}
2003-09-25 21:14:35 +00:00
u_fscanf ( myFile , " Unicode String %%S: %S \n " , myUString ) ;
2002-03-26 20:10:52 +00:00
u_austrncpy ( myString , myUString , sizeof ( myUString ) / sizeof ( * myUString ) ) ;
if ( strcmp ( myString , " My-String " ) ) {
log_err ( " %%S Got: %S, Expected: My String \n " , myUString ) ;
}
2003-09-25 21:14:35 +00:00
u_fscanf ( myFile , " NULL Unicode String %%S: %S \n " , myUString ) ;
2002-03-26 20:10:52 +00:00
u_austrncpy ( myString , myUString , sizeof ( myUString ) / sizeof ( * myUString ) ) ;
if ( strcmp ( myString , " (null) " ) ) {
2002-02-23 00:34:39 +00:00
log_err ( " %%S Got: %S, Expected: My String \n " , myUString ) ;
}
* newDoubleValuePtr = - 1.0 ;
u_fscanf ( myFile , " Percent %%P (non-ANSI): %P \n " , newDoubleValuePtr ) ;
if ( myFloat ! = * newDoubleValuePtr ) {
2002-03-26 05:28:54 +00:00
log_err ( " %%P Got: %f, Expected: %f \n " , * newDoubleValuePtr , myFloat ) ;
2002-02-23 00:34:39 +00:00
}
* newDoubleValuePtr = - 1.0 ;
2002-03-15 00:29:57 +00:00
u_fscanf ( myFile , " Spell Out %%V (non-ANSI): %V \n " , newDoubleValuePtr ) ;
if ( myFloat ! = * newDoubleValuePtr ) {
2002-03-28 19:53:13 +00:00
log_err ( " %%V Got: %f, Expected: %f \n " , * newDoubleValuePtr , myFloat ) ;
2002-03-15 00:29:57 +00:00
}
2002-03-26 20:10:52 +00:00
2003-10-10 20:55:24 +00:00
u_fgets ( myUString , 4 , myFile ) ;
2002-03-27 20:08:42 +00:00
u_austrncpy ( myString , myUString , sizeof ( myUString ) / sizeof ( * myUString ) ) ;
if ( myString = = NULL | | strcmp ( myString , " \t \n " ) ! = 0 ) {
log_err ( " u_fgets got \" %s \" \n " , myString ) ;
}
2003-10-10 20:55:24 +00:00
if ( u_fgets ( myUString , sizeof ( myUString ) / sizeof ( * myUString ) , myFile ) ! = myUString ) {
2002-03-27 21:17:25 +00:00
log_err ( " u_fgets did not return myUString \n " ) ;
}
2002-03-26 20:10:52 +00:00
u_austrncpy ( myString , myUString , sizeof ( myUString ) / sizeof ( * myUString ) ) ;
2002-03-27 20:08:42 +00:00
if ( myString = = NULL | | strcmp ( myString , " Pointer to integer (Count) %n: n=1 n=1 \n " ) ! = 0 ) {
2002-03-27 19:19:52 +00:00
log_err ( " u_fgets got \" %s \" \n " , myString ) ;
2002-03-26 20:10:52 +00:00
}
2003-10-10 20:55:24 +00:00
if ( u_fgets ( myUString , sizeof ( myUString ) / sizeof ( * myUString ) , myFile ) ! = myUString ) {
2002-03-27 21:17:25 +00:00
log_err ( " u_fgets did not return myUString \n " ) ;
}
2002-03-27 19:19:52 +00:00
u_austrncpy ( myString , myUString , sizeof ( myUString ) / sizeof ( * myUString ) ) ;
2002-03-27 20:08:42 +00:00
if ( myString = = NULL | | strcmp ( myString , " Pointer to integer Value: 37 \n " ) ! = 0 ) {
2002-03-27 19:19:52 +00:00
log_err ( " u_fgets got \" %s \" \n " , myString ) ;
}
2002-03-27 21:17:25 +00:00
2003-10-10 20:55:24 +00:00
if ( u_fgets ( myUString , sizeof ( myUString ) / sizeof ( * myUString ) , myFile ) ! = myUString ) {
2003-03-04 19:12:55 +00:00
log_err ( " u_fgets did not return myUString \n " ) ;
}
u_austrncpy ( myString , myUString , sizeof ( myUString ) / sizeof ( * myUString ) ) ;
if ( myString = = NULL | | strcmp ( myString , " This is a long test123456789012345678901234567890123456789012345678901234567890 \n " ) ! = 0 ) {
log_err ( " u_fgets got \" %s \" \n " , myString ) ;
}
2003-10-10 20:55:24 +00:00
if ( u_fgets ( myUString , 0 , myFile ) ! = NULL ) {
2002-03-27 21:17:25 +00:00
log_err ( " u_fgets got \" %s \" and it should have returned NULL \n " , myString ) ;
}
2003-10-10 20:55:24 +00:00
if ( u_fgets ( myUString , 1 , myFile ) ! = myUString ) {
2002-03-27 21:17:25 +00:00
log_err ( " u_fgets did not return myUString \n " ) ;
}
u_austrncpy ( myString , myUString , sizeof ( myUString ) / sizeof ( * myUString ) ) ;
if ( myString = = NULL | | strcmp ( myString , " " ) ! = 0 ) {
log_err ( " u_fgets got \" %s \" \n " , myString ) ;
}
2003-10-10 20:55:24 +00:00
if ( u_fgets ( myUString , 2 , myFile ) ! = myUString ) {
2002-03-27 21:17:25 +00:00
log_err ( " u_fgets did not return myUString \n " ) ;
}
u_austrncpy ( myString , myUString , sizeof ( myUString ) / sizeof ( * myUString ) ) ;
if ( myString = = NULL | | strcmp ( myString , " \t " ) ! = 0 ) {
log_err ( " u_fgets got \" %s \" \n " , myString ) ;
}
2002-02-23 00:34:39 +00:00
u_fclose ( myFile ) ;
}
2002-03-15 00:29:57 +00:00
static void TestFile ( ) {
/* FILE *standardFile;*/
log_verbose ( " Testing u_fopen \n " ) ;
TestFileFromICU ( u_fopen ( STANDARD_TEST_FILE , " w " , NULL , NULL ) ) ;
/* Don't know how to make this work without stdout or stderr */
/*
log_verbose ( " Testing u_finit \n " ) ;
standardFile = fopen ( STANDARD_TEST_FILE , " wb " ) ;
TestFileFromICU ( u_finit ( standardFile , NULL , NULL ) ) ;
fclose ( standardFile ) ;
*/
}
2002-08-24 00:00:31 +00:00
static void TestCodepageAndLocale ( ) {
2002-03-22 00:01:46 +00:00
UFILE * myFile = u_fopen ( STANDARD_TEST_FILE , " w " , NULL , NULL ) ;
if ( u_fgetcodepage ( myFile ) = = NULL
| | strcmp ( u_fgetcodepage ( myFile ) , ucnv_getDefaultName ( ) ) ! = 0 )
{
log_err ( " Didn't get the proper default codepage. Got %s expected: %s \n " ,
u_fgetcodepage ( myFile ) , ucnv_getDefaultName ( ) ) ;
}
2002-08-24 00:00:31 +00:00
if ( u_fgetlocale ( myFile ) = = NULL
| | strcmp ( u_fgetlocale ( myFile ) , uloc_getDefault ( ) ) ! = 0 )
{
log_err ( " Didn't get the proper default locale. Got %s expected: %s \n " ,
u_fgetlocale ( myFile ) , uloc_getDefault ( ) ) ;
}
2002-03-22 00:01:46 +00:00
u_fclose ( myFile ) ;
2002-08-24 00:00:31 +00:00
myFile = u_fopen ( STANDARD_TEST_FILE , " w " , " es " , NULL ) ;
2002-03-22 00:01:46 +00:00
if ( u_fgetcodepage ( myFile ) = = NULL
| | strcmp ( u_fgetcodepage ( myFile ) , " ISO-8859-1 " ) ! = 0 )
{
log_err ( " Didn't get the proper default codepage for \" en \" . Got %s expected: iso-8859-1 \n " ,
u_fgetcodepage ( myFile ) ) ;
}
2002-08-24 00:00:31 +00:00
if ( u_fgetlocale ( myFile ) = = NULL
| | strcmp ( u_fgetlocale ( myFile ) , " es " ) ! = 0 )
{
log_err ( " Didn't get the proper default locale. Got %s expected: %s \n " ,
u_fgetlocale ( myFile ) , " es " ) ;
}
u_fclose ( myFile ) ;
myFile = u_fopen ( STANDARD_TEST_FILE , " w " , NULL , " UTF-16 " ) ;
if ( u_fgetcodepage ( myFile ) = = NULL
| | strcmp ( u_fgetcodepage ( myFile ) , " UTF-16 " ) ! = 0 )
{
log_err ( " Didn't get the proper default codepage for \" en \" . Got %s expected: iso-8859-1 \n " ,
u_fgetcodepage ( myFile ) ) ;
}
if ( u_fgetlocale ( myFile ) = = NULL
| | strcmp ( u_fgetlocale ( myFile ) , uloc_getDefault ( ) ) ! = 0 )
{
log_err ( " Didn't get the proper default locale. Got %s expected: %s \n " ,
u_fgetlocale ( myFile ) , uloc_getDefault ( ) ) ;
}
u_fclose ( myFile ) ;
myFile = u_fopen ( STANDARD_TEST_FILE , " w " , " zh " , " UTF-16 " ) ;
if ( u_fgetcodepage ( myFile ) = = NULL
| | strcmp ( u_fgetcodepage ( myFile ) , " UTF-16 " ) ! = 0 )
{
log_err ( " Didn't get the proper default codepage for \" en \" . Got %s expected: iso-8859-1 \n " ,
u_fgetcodepage ( myFile ) ) ;
}
if ( u_fgetlocale ( myFile ) = = NULL
| | strcmp ( u_fgetlocale ( myFile ) , " zh " ) ! = 0 )
{
log_err ( " Didn't get the proper default locale. Got %s expected: %s \n " ,
u_fgetlocale ( myFile ) , " zh " ) ;
}
2002-03-22 00:01:46 +00:00
u_fclose ( myFile ) ;
2002-08-04 06:47:44 +00:00
}
static void TestfgetsBuffers ( ) {
UChar buffer [ 2048 ] ;
UChar expectedBuffer [ 2048 ] ;
static const char testStr [ ] = " This is a test string that tests u_fgets. It makes sure that we don't try to read too much! " ;
UFILE * myFile = u_fopen ( STANDARD_TEST_FILE , " w " , NULL , " UTF-16 " ) ;
int32_t expectedSize = strlen ( testStr ) ;
int32_t readSize ;
int32_t repetitions ;
u_fputc ( 0x3BC , myFile ) ;
u_fputc ( 0xFF41 , myFile ) ;
u_memset ( buffer , 0xDEAD , sizeof ( buffer ) / sizeof ( buffer [ 0 ] ) ) ;
u_memset ( expectedBuffer , 0 , sizeof ( expectedBuffer ) / sizeof ( expectedBuffer [ 0 ] ) ) ;
2002-08-04 07:10:57 +00:00
u_uastrncpy ( buffer , testStr , expectedSize + 1 ) ;
2002-08-04 06:47:44 +00:00
for ( repetitions = 0 ; repetitions < 16 ; repetitions + + ) {
u_file_write ( buffer , expectedSize , myFile ) ;
u_strcat ( expectedBuffer , buffer ) ;
}
u_fclose ( myFile ) ;
2002-08-04 07:10:57 +00:00
u_memset ( buffer , 0xDEAD , sizeof ( buffer ) / sizeof ( buffer [ 0 ] ) ) ;
2002-08-04 06:47:44 +00:00
myFile = u_fopen ( STANDARD_TEST_FILE , " r " , NULL , " UTF-16 " ) ;
if ( u_fgetc ( myFile ) ! = 0x3BC ) {
log_err ( " The first character is wrong \n " ) ;
}
if ( u_fgetc ( myFile ) ! = 0xFF41 ) {
log_err ( " The second character is wrong \n " ) ;
}
2003-10-10 20:55:24 +00:00
if ( u_fgets ( buffer , sizeof ( buffer ) / sizeof ( buffer [ 0 ] ) , myFile ) ! = buffer ) {
2002-08-04 06:47:44 +00:00
log_err ( " Didn't get the buffer back \n " ) ;
return ;
}
readSize = u_strlen ( buffer ) ;
if ( readSize ! = expectedSize * repetitions ) {
log_err ( " Buffer is the wrong size. Got %d Expected %d \n " , u_strlen ( buffer ) , expectedSize * repetitions ) ;
}
if ( buffer [ ( expectedSize * repetitions ) + 1 ] ! = 0xDEAD ) {
log_err ( " u_fgets wrote too much data \n " ) ;
}
if ( u_strcmp ( buffer , expectedBuffer ) ! = 0 ) {
log_err ( " Did get expected string back \n " ) ;
}
2002-08-24 00:00:31 +00:00
if ( strcmp ( u_fgetcodepage ( myFile ) , " UTF-16 " ) ! = 0 ) {
log_err ( " Got %s instead of UTF-16 \n " , u_fgetcodepage ( myFile ) ) ;
}
2002-08-04 06:47:44 +00:00
u_fclose ( myFile ) ;
log_verbose ( " Now trying a multi-byte encoding (UTF-8). \n " ) ;
myFile = u_fopen ( STANDARD_TEST_FILE , " w " , NULL , " UTF-8 " ) ;
u_fputc ( 0x3BC , myFile ) ;
u_fputc ( 0xFF41 , myFile ) ;
u_memset ( buffer , 0xDEAD , sizeof ( buffer ) / sizeof ( buffer [ 0 ] ) ) ;
u_memset ( expectedBuffer , 0 , sizeof ( expectedBuffer ) / sizeof ( expectedBuffer [ 0 ] ) ) ;
2002-08-04 07:10:57 +00:00
u_uastrncpy ( buffer , testStr , expectedSize + 1 ) ;
2002-08-04 06:47:44 +00:00
for ( repetitions = 0 ; repetitions < 16 ; repetitions + + ) {
u_file_write ( buffer , expectedSize , myFile ) ;
u_strcat ( expectedBuffer , buffer ) ;
}
u_fclose ( myFile ) ;
2002-08-04 07:10:57 +00:00
u_memset ( buffer , 0xDEAD , sizeof ( buffer ) / sizeof ( buffer [ 0 ] ) ) ;
2002-08-04 06:47:44 +00:00
myFile = u_fopen ( STANDARD_TEST_FILE , " r " , NULL , " UTF-8 " ) ;
2002-08-24 00:00:31 +00:00
if ( strcmp ( u_fgetcodepage ( myFile ) , " UTF-8 " ) ! = 0 ) {
log_err ( " Got %s instead of UTF-8 \n " , u_fgetcodepage ( myFile ) ) ;
}
2002-08-04 06:47:44 +00:00
if ( u_fgetc ( myFile ) ! = 0x3BC ) {
log_err ( " The first character is wrong \n " ) ;
}
if ( u_fgetc ( myFile ) ! = 0xFF41 ) {
log_err ( " The second character is wrong \n " ) ;
}
2003-10-10 20:55:24 +00:00
if ( u_fgets ( buffer , sizeof ( buffer ) / sizeof ( buffer [ 0 ] ) , myFile ) ! = buffer ) {
2002-08-04 06:47:44 +00:00
log_err ( " Didn't get the buffer back \n " ) ;
return ;
}
readSize = u_strlen ( buffer ) ;
if ( readSize ! = expectedSize * repetitions ) {
log_err ( " Buffer is the wrong size. Got %d Expected %d \n " , u_strlen ( buffer ) , expectedSize * repetitions ) ;
}
if ( buffer [ ( expectedSize * repetitions ) + 1 ] ! = 0xDEAD ) {
log_err ( " u_fgets wrote too much data \n " ) ;
}
if ( u_strcmp ( buffer , expectedBuffer ) ! = 0 ) {
log_err ( " Did get expected string back \n " ) ;
}
u_fclose ( myFile ) ;
2002-08-04 07:10:57 +00:00
log_verbose ( " Now trying a multi-byte encoding (UTF-8) with a really small buffer. \n " ) ;
2002-08-04 06:47:44 +00:00
2002-08-04 07:10:57 +00:00
myFile = u_fopen ( STANDARD_TEST_FILE , " w " , NULL , " UTF-8 " ) ;
2002-08-04 06:47:44 +00:00
2002-08-04 07:10:57 +00:00
u_fputc ( 0xFF41 , myFile ) ;
u_memset ( buffer , 0xDEAD , sizeof ( buffer ) / sizeof ( buffer [ 0 ] ) ) ;
u_memset ( expectedBuffer , 0 , sizeof ( expectedBuffer ) / sizeof ( expectedBuffer [ 0 ] ) ) ;
u_uastrncpy ( buffer , testStr , expectedSize + 1 ) ;
for ( repetitions = 0 ; repetitions < 1 ; repetitions + + ) {
u_file_write ( buffer , expectedSize , myFile ) ;
u_strcat ( expectedBuffer , buffer ) ;
}
u_fclose ( myFile ) ;
2002-08-04 06:47:44 +00:00
2002-08-04 07:10:57 +00:00
u_memset ( buffer , 0xDEAD , sizeof ( buffer ) / sizeof ( buffer [ 0 ] ) ) ;
myFile = u_fopen ( STANDARD_TEST_FILE , " r " , NULL , " UTF-8 " ) ;
2003-10-10 20:55:24 +00:00
if ( u_fgets ( buffer , 2 , myFile ) ! = buffer ) {
2002-08-04 07:10:57 +00:00
log_err ( " Didn't get the buffer back \n " ) ;
return ;
}
readSize = u_strlen ( buffer ) ;
if ( readSize ! = 1 ) {
log_err ( " Buffer is the wrong size. Got %d Expected %d \n " , u_strlen ( buffer ) , 1 ) ;
}
if ( buffer [ 0 ] ! = 0xFF41 | | buffer [ 1 ] ! = 0 ) {
log_err ( " Did get expected string back \n " ) ;
}
if ( buffer [ 2 ] ! = 0xDEAD ) {
log_err ( " u_fgets wrote too much data \n " ) ;
}
u_fclose ( myFile ) ;
2002-08-04 06:47:44 +00:00
2002-03-22 00:01:46 +00:00
}
2002-03-15 00:29:57 +00:00
2002-08-23 20:58:40 +00:00
static void TestfgetsLineCount ( ) {
UChar buffer [ 2048 ] ;
UChar expectedBuffer [ 2048 ] ;
char charBuffer [ 2048 ] ;
static const char testStr [ ] = " This is a test string that tests u_fgets. It makes sure that we don't try to read too much! " ;
UFILE * myFile = NULL ;
FILE * stdFile = fopen ( STANDARD_TEST_FILE , " w " ) ;
int32_t expectedSize = strlen ( testStr ) ;
int32_t repetitions ;
int32_t nlRepetitions ;
u_memset ( expectedBuffer , 0 , sizeof ( expectedBuffer ) / sizeof ( expectedBuffer [ 0 ] ) ) ;
for ( repetitions = 0 ; repetitions < 16 ; repetitions + + ) {
fwrite ( testStr , sizeof ( testStr [ 0 ] ) , expectedSize , stdFile ) ;
for ( nlRepetitions = 0 ; nlRepetitions < repetitions ; nlRepetitions + + ) {
fwrite ( " \n " , sizeof ( testStr [ 0 ] ) , 1 , stdFile ) ;
}
}
fclose ( stdFile ) ;
myFile = u_fopen ( STANDARD_TEST_FILE , " r " , NULL , NULL ) ;
stdFile = fopen ( STANDARD_TEST_FILE , " r " ) ;
for ( ; ; ) {
2002-08-23 22:13:14 +00:00
u_memset ( buffer , 0xDEAD , sizeof ( buffer ) / sizeof ( buffer [ 0 ] ) ) ;
2002-08-23 20:58:40 +00:00
char * returnedCharBuffer = fgets ( charBuffer , sizeof ( charBuffer ) / sizeof ( charBuffer [ 0 ] ) , stdFile ) ;
2003-10-10 20:55:24 +00:00
UChar * returnedUCharBuffer = u_fgets ( buffer , sizeof ( buffer ) / sizeof ( buffer [ 0 ] ) , myFile ) ;
2002-08-23 20:58:40 +00:00
if ( ! returnedCharBuffer & & ! returnedUCharBuffer ) {
/* Both returned NULL. stop. */
break ;
}
if ( returnedCharBuffer ! = charBuffer ) {
log_err ( " Didn't get the charBuffer back \n " ) ;
continue ;
}
u_uastrncpy ( expectedBuffer , charBuffer , strlen ( charBuffer ) + 1 ) ;
if ( returnedUCharBuffer ! = buffer ) {
log_err ( " Didn't get the buffer back \n " ) ;
continue ;
}
if ( u_strcmp ( buffer , expectedBuffer ) ! = 0 ) {
log_err ( " buffers are different \n " ) ;
}
2002-08-23 22:13:14 +00:00
if ( buffer [ u_strlen ( buffer ) + 1 ] ! = 0xDEAD ) {
log_err ( " u_fgets wrote too much \n " ) ;
}
2002-08-23 20:58:40 +00:00
}
fclose ( stdFile ) ;
u_fclose ( myFile ) ;
}
2003-09-22 23:07:05 +00:00
static void TestfgetsNewLineHandling ( ) {
UChar buffer [ 256 ] ;
static const UChar testUStr [ ] [ 16 ] = {
{ 0x000D , 0 } ,
{ 0x000D , 0x000A , 0 } ,
{ 0x000D , 0 } ,
{ 0x000D , 0 } ,
{ 0x0085 , 0 } ,
{ 0x000A , 0 } ,
{ 0x000D , 0 } ,
{ 0x000B , 0 } ,
{ 0x000C , 0 } ,
{ 0x000C , 0 } ,
{ 0x2028 , 0 } ,
{ 0x0085 , 0 } ,
{ 0x2029 , 0 } ,
{ 0x0085 , 0 } ,
{ 0x008B , 0x000D , 0 } ,
{ 0x00A0 , 0x000D , 0x000A , 0 } ,
{ 0x3000 , 0x000D , 0 } ,
{ 0xd800 , 0xdfff , 0x000D , 0 } ,
{ 0x00AB , 0x0085 , 0 } ,
{ 0x00AC , 0x000A , 0 } ,
{ 0x00AD , 0x000D , 0 } ,
{ 0x00BA , 0x000B , 0 } ,
{ 0x00AB , 0x000C , 0 } ,
{ 0x00B1 , 0x000C , 0 } ,
{ 0x30BB , 0x2028 , 0 } ,
{ 0x00A5 , 0x0085 , 0 } ,
{ 0x0080 , 0x2029 , 0 } ,
{ 0x00AF , 0x0085 , 0 }
} ;
UFILE * myFile = NULL ;
int32_t lineIdx ;
myFile = u_fopen ( STANDARD_TEST_FILE , " w " , NULL , " UTF-8 " ) ;
for ( lineIdx = 0 ; lineIdx < ( int32_t ) ( sizeof ( testUStr ) / sizeof ( testUStr [ 0 ] ) ) ; lineIdx + + ) {
u_file_write ( testUStr [ lineIdx ] , u_strlen ( testUStr [ lineIdx ] ) , myFile ) ;
}
u_fclose ( myFile ) ;
myFile = u_fopen ( STANDARD_TEST_FILE , " r " , NULL , " UTF-8 " ) ;
for ( lineIdx = 0 ; lineIdx < ( int32_t ) ( sizeof ( testUStr ) / sizeof ( testUStr [ 0 ] ) ) ; lineIdx + + ) {
u_memset ( buffer , 0xDEAD , sizeof ( buffer ) / sizeof ( buffer [ 0 ] ) ) ;
2003-10-10 20:55:24 +00:00
UChar * returnedUCharBuffer = u_fgets ( buffer , sizeof ( buffer ) / sizeof ( buffer [ 0 ] ) , myFile ) ;
2003-09-22 23:07:05 +00:00
if ( ! returnedUCharBuffer ) {
/* Returned NULL. stop. */
break ;
}
if ( u_strcmp ( buffer , testUStr [ lineIdx ] ) ! = 0 ) {
log_err ( " buffers are different at index = %d \n " , lineIdx ) ;
}
if ( buffer [ u_strlen ( buffer ) + 1 ] ! = 0xDEAD ) {
log_err ( " u_fgets wrote too much \n " ) ;
}
}
if ( lineIdx ! = ( int32_t ) ( sizeof ( testUStr ) / sizeof ( testUStr [ 0 ] ) ) ) {
log_err ( " u_fgets read too much \n " ) ;
}
2003-10-10 20:55:24 +00:00
if ( u_fgets ( buffer , sizeof ( buffer ) / sizeof ( buffer [ 0 ] ) , myFile ) ! = NULL ) {
2003-09-22 23:07:05 +00:00
log_err ( " u_file_write wrote too much \n " ) ;
}
u_fclose ( myFile ) ;
}
2003-10-11 02:16:12 +00:00
static void TestCodepage ( ) {
UFILE * myFile = NULL ;
static const UChar strABAccentA [ ] = { 0x0041 , 0x0042 , 0x00C1 , 0x0043 , 0 } ;
static const UChar strBadConversion [ ] = { 0x0041 , 0x0042 , 0xfffd , 0x0043 , 0 } ;
UChar testBuf [ sizeof ( strABAccentA ) / sizeof ( strABAccentA [ 0 ] ) * 2 ] ; /* *2 to see if too much was */
char convName [ UCNV_MAX_CONVERTER_NAME_LENGTH ] ;
int32_t retVal ;
UErrorCode status = U_ZERO_ERROR ;
myFile = u_fopen ( STANDARD_TEST_FILE , " w " , NULL , " absurd converter that can't be opened " ) ;
if ( myFile ) {
log_err ( " Recieved a UFILE * with an invalid codepage parameter \n " ) ;
u_fclose ( myFile ) ;
}
myFile = u_fopen ( STANDARD_TEST_FILE , " w " , NULL , " ISO-8859-1 " ) ;
if ( strcmp ( " ISO-8859-1 " , u_fgetcodepage ( myFile ) ) ! = 0 ) {
log_err ( " Couldn't get ISO-8859-1 back as opened codepage \n " ) ;
}
u_file_write ( strABAccentA , u_strlen ( strABAccentA ) , myFile ) ;
u_fclose ( myFile ) ;
/* Now see what we got wrote */
myFile = u_fopen ( STANDARD_TEST_FILE , " r " , NULL , NULL ) ;
if ( u_fsetcodepage ( " ISO-8859-1 " , myFile ) ! = 0 ) {
log_err ( " u_fsetcodepage didn't set the codepage \n " ) ;
}
retVal = u_file_read ( testBuf , u_strlen ( strABAccentA ) , myFile ) ;
if ( u_strncmp ( strABAccentA , testBuf , u_strlen ( strABAccentA ) ) ! = 0 ) {
log_err ( " The test data was read and written differently! \n " ) ;
}
if ( retVal ! = u_strlen ( strABAccentA ) ) {
log_err ( " The test data returned different lengths. Got: %d, Expected %d \n " , retVal , u_strlen ( strABAccentA ) ) ;
}
u_fclose ( myFile ) ;
/* What happens on invalid input? */
myFile = u_fopen ( STANDARD_TEST_FILE , " r " , NULL , " ISO-8859-1 " ) ;
if ( strcmp ( ucnv_getName ( u_fgetConverter ( myFile ) , & status ) , " ISO-8859-1 " ) ! = 0 ) {
log_err ( " u_fgetConverter returned %s \n " , ucnv_getName ( u_fgetConverter ( myFile ) , & status ) ) ;
}
if ( u_fsetcodepage ( " UTF-8 " , myFile ) ! = 0 ) {
log_err ( " u_fsetcodepage didn't set the codepage to UTF-8 \n " ) ;
}
if ( strcmp ( ucnv_getName ( u_fgetConverter ( myFile ) , & status ) , " UTF-8 " ) ! = 0 ) {
log_err ( " u_fgetConverter returned %s \n " , ucnv_getName ( u_fgetConverter ( myFile ) , & status ) ) ;
}
retVal = u_file_read ( testBuf , u_strlen ( strBadConversion ) , myFile ) ;
if ( u_strncmp ( strBadConversion , testBuf , u_strlen ( strBadConversion ) ) ! = 0 ) {
log_err ( " The test data wasn't subsituted as expected \n " ) ;
}
u_fclose ( myFile ) ;
/* Can't currently swap codepages midstream */
myFile = u_fopen ( STANDARD_TEST_FILE , " r " , NULL , " ISO-8859-1 " ) ;
strcpy ( convName , u_fgetcodepage ( myFile ) ) ;
u_file_read ( testBuf , 1 , myFile ) ;
if ( u_fsetcodepage ( " UTF-8 " , myFile ) = = 0 ) {
log_err ( " u_fsetcodepage set the codepage after reading a byte \n " ) ;
}
retVal = u_file_read ( testBuf + 1 , u_strlen ( strABAccentA ) - 1 , myFile ) ;
if ( u_strncmp ( strABAccentA , testBuf , u_strlen ( strABAccentA ) ) ! = 0 ) {
log_err ( " u_fsetcodepage changed the codepages after writing data \n " ) ;
}
if ( ( retVal + 1 ) ! = u_strlen ( strABAccentA ) ) {
log_err ( " The test data returned different lengths. Got: %d, Expected %d \n " , retVal , u_strlen ( strABAccentA ) ) ;
}
u_frewind ( myFile ) ;
retVal = u_file_read ( testBuf , u_strlen ( strABAccentA ) , myFile ) ;
if ( u_strncmp ( strABAccentA , testBuf , u_strlen ( strABAccentA ) ) ! = 0 ) {
log_err ( " The test data was read and written differently! \n " ) ;
}
if ( retVal ! = u_strlen ( strABAccentA ) ) {
log_err ( " The test data returned different lengths. Got: %d, Expected %d \n " , retVal , u_strlen ( strABAccentA ) ) ;
}
u_fclose ( myFile ) ;
}
2002-02-23 00:34:39 +00:00
static void TestFilePrintCompatibility ( ) {
UFILE * myFile = u_fopen ( STANDARD_TEST_FILE , " wb " , " en_US_POSIX " , NULL ) ;
FILE * myCFile ;
int32_t num ;
char cVal ;
static const UChar emptyStr [ ] = { 0 } ;
char readBuf [ 512 ] = " " ;
char testBuf [ 512 ] = " " ;
if ( myFile = = NULL ) {
log_err ( " Can't read test file. " ) ;
return ;
}
2002-08-24 00:00:31 +00:00
if ( strcmp ( u_fgetlocale ( myFile ) , " en_US_POSIX " ) ! = 0 ) {
log_err ( " Got %s instead of en_US_POSIX for locale \n " , u_fgetlocale ( myFile ) ) ;
}
2002-02-23 00:34:39 +00:00
/* Compare against C API compatibility */
for ( num = - STANDARD_TEST_NUM_RANGE ; num < STANDARD_TEST_NUM_RANGE ; num + + ) {
u_fprintf ( myFile , " %x " , num ) ;
u_fprintf ( myFile , " %X " , num ) ;
u_fprintf ( myFile , " %o " , num ) ;
u_fprintf ( myFile , " %d " , num ) ;
u_fprintf ( myFile , " %i " , num ) ;
u_fprintf ( myFile , " %f " , ( double ) num ) ;
u_fprintf ( myFile , " %e " , ( double ) num ) ;
u_fprintf ( myFile , " %E " , ( double ) num ) ;
u_fprintf ( myFile , " %g " , ( double ) num ) ;
u_fprintf ( myFile , " %G " , ( double ) num ) ;
u_fputs ( emptyStr , myFile ) ;
}
u_fprintf_u ( myFile , NEW_LINE ) ;
for ( num = 0 ; num < 0x80 ; num + + ) {
u_fprintf ( myFile , " %c " , num ) ;
}
u_fclose ( myFile ) ;
myCFile = fopen ( STANDARD_TEST_FILE , " rb " ) ;
2002-03-26 05:28:54 +00:00
if ( myCFile = = NULL ) {
2002-02-23 00:34:39 +00:00
log_err ( " Can't read test file. " ) ;
return ;
}
for ( num = - STANDARD_TEST_NUM_RANGE ; num < STANDARD_TEST_NUM_RANGE ; num + + ) {
fscanf ( myCFile , " %s " , readBuf ) ;
sprintf ( testBuf , " %x " , num ) ;
if ( strcmp ( readBuf , testBuf ) ! = 0 ) {
log_err ( " %%x Got: \" %s \" , Expected: \" %s \" \n " , readBuf , testBuf ) ;
}
fscanf ( myCFile , " %s " , readBuf ) ;
sprintf ( testBuf , " %X " , num ) ;
if ( strcmp ( readBuf , testBuf ) ! = 0 ) {
log_err ( " %%X Got: \" %s \" , Expected: \" %s \" \n " , readBuf , testBuf ) ;
}
fscanf ( myCFile , " %s " , readBuf ) ;
sprintf ( testBuf , " %o " , num ) ;
if ( strcmp ( readBuf , testBuf ) ! = 0 ) {
log_err ( " %%o Got: \" %s \" , Expected: \" %s \" \n " , readBuf , testBuf ) ;
}
/* fprintf is not compatible on all platforms e.g. the iSeries */
fscanf ( myCFile , " %s " , readBuf ) ;
sprintf ( testBuf , " %d " , num ) ;
if ( strcmp ( readBuf , testBuf ) ! = 0 ) {
log_err ( " %%d Got: \" %s \" , Expected: \" %s \" \n " , readBuf , testBuf ) ;
}
fscanf ( myCFile , " %s " , readBuf ) ;
sprintf ( testBuf , " %i " , num ) ;
if ( strcmp ( readBuf , testBuf ) ! = 0 ) {
log_err ( " %%i Got: \" %s \" , Expected: \" %s \" \n " , readBuf , testBuf ) ;
}
fscanf ( myCFile , " %s " , readBuf ) ;
sprintf ( testBuf , " %f " , ( double ) num ) ;
if ( strcmp ( readBuf , testBuf ) ! = 0 ) {
log_err ( " %%f Got: \" %s \" , Expected: \" %s \" \n " , readBuf , testBuf ) ;
}
fscanf ( myCFile , " %s " , readBuf ) ;
sprintf ( testBuf , " %e " , ( double ) num ) ;
if ( strcmp ( readBuf , testBuf ) ! = 0 ) {
log_err ( " %%e Got: \" %s \" , Expected: \" %s \" \n " , readBuf , testBuf ) ;
}
fscanf ( myCFile , " %s " , readBuf ) ;
sprintf ( testBuf , " %E " , ( double ) num ) ;
if ( strcmp ( readBuf , testBuf ) ! = 0 ) {
log_err ( " %%E Got: \" %s \" , Expected: \" %s \" \n " , readBuf , testBuf ) ;
}
fscanf ( myCFile , " %s " , readBuf ) ;
sprintf ( testBuf , " %g " , ( double ) num ) ;
if ( strcmp ( readBuf , testBuf ) ! = 0 ) {
log_err ( " %%g Got: \" %s \" , Expected: \" %s \" \n " , readBuf , testBuf ) ;
}
fscanf ( myCFile , " %s " , readBuf ) ;
sprintf ( testBuf , " %G " , ( double ) num ) ;
if ( strcmp ( readBuf , testBuf ) ! = 0 ) {
log_err ( " %%G Got: \" %s \" , Expected: \" %s \" \n " , readBuf , testBuf ) ;
}
}
/* Properly eat the newlines */
for ( num = 0 ; num < u_strlen ( NEW_LINE ) ; num + + ) {
fscanf ( myCFile , " %c " , & cVal ) ;
if ( cVal ! = NEW_LINE [ num ] ) {
log_err ( " OS newline error \n " ) ;
}
}
for ( num = 0 ; num < u_strlen ( NEW_LINE ) ; num + + ) {
fscanf ( myCFile , " %c " , & cVal ) ;
if ( cVal ! = NEW_LINE [ num ] ) {
log_err ( " ustdio newline error \n " ) ;
}
}
for ( num = 0 ; num < 0x80 ; num + + ) {
cVal = - 1 ;
fscanf ( myCFile , " %c " , & cVal ) ;
if ( num ! = cVal ) {
log_err ( " %%c Got: 0x%x, Expected: 0x%x \n " , cVal , num ) ;
}
}
fclose ( myCFile ) ;
}
2002-03-27 19:19:52 +00:00
# define TestFPrintFormat(uFormat, uValue, cFormat, cValue) \
myFile = u_fopen ( STANDARD_TEST_FILE , " w " , " en_US_POSIX " , NULL ) ; \
/* Reinitialize the buffer to verify null termination works. */ \
u_memset ( uBuffer , 0x2a , sizeof ( uBuffer ) / sizeof ( * uBuffer ) ) ; \
memset ( buffer , 0x2a , sizeof ( buffer ) / sizeof ( * buffer ) ) ; \
\
uNumPrinted = u_fprintf ( myFile , uFormat , uValue ) ; \
u_fclose ( myFile ) ; \
myFile = u_fopen ( STANDARD_TEST_FILE , " r " , " en_US_POSIX " , NULL ) ; \
2003-10-10 20:55:24 +00:00
u_fgets ( uBuffer , sizeof ( uBuffer ) / sizeof ( * uBuffer ) , myFile ) ; \
2002-03-27 19:19:52 +00:00
u_fclose ( myFile ) ; \
u_austrncpy ( compBuffer , uBuffer , sizeof ( uBuffer ) / sizeof ( * uBuffer ) ) ; \
cNumPrinted = sprintf ( buffer , cFormat , cValue ) ; \
if ( strcmp ( buffer , compBuffer ) ! = 0 ) { \
log_err ( " % " uFormat " Got: \" %s \" , Expected: \" %s \" \n " , compBuffer , buffer ) ; \
} \
if ( cNumPrinted ! = uNumPrinted ) { \
log_err ( " % " uFormat " number printed Got: %d, Expected: %d \n " , uNumPrinted , cNumPrinted ) ; \
} \
2002-08-26 20:53:53 +00:00
if ( buffer [ uNumPrinted + 1 ] ! = 0x2a ) { \
log_err ( " % " uFormat " too much stored \n " ) ; \
} \
2002-03-27 19:19:52 +00:00
2002-08-24 00:00:31 +00:00
# define TestFPrintFormat2(format, precision, value) \
myFile = u_fopen ( STANDARD_TEST_FILE , " w " , " en_US_POSIX " , NULL ) ; \
/* Reinitialize the buffer to verify null termination works. */ \
u_memset ( uBuffer , 0x2a , sizeof ( uBuffer ) / sizeof ( * uBuffer ) ) ; \
memset ( buffer , 0x2a , sizeof ( buffer ) / sizeof ( * buffer ) ) ; \
\
uNumPrinted = u_fprintf ( myFile , format , precision , value ) ; \
u_fclose ( myFile ) ; \
myFile = u_fopen ( STANDARD_TEST_FILE , " r " , " en_US_POSIX " , NULL ) ; \
2003-10-10 20:55:24 +00:00
u_fgets ( uBuffer , sizeof ( uBuffer ) / sizeof ( * uBuffer ) , myFile ) ; \
2002-08-24 00:00:31 +00:00
u_fclose ( myFile ) ; \
u_austrncpy ( compBuffer , uBuffer , sizeof ( uBuffer ) / sizeof ( * uBuffer ) ) ; \
cNumPrinted = sprintf ( buffer , format , precision , value ) ; \
if ( strcmp ( buffer , compBuffer ) ! = 0 ) { \
log_err ( " % " format " Got: \" %s \" , Expected: \" %s \" \n " , compBuffer , buffer ) ; \
} \
if ( cNumPrinted ! = uNumPrinted ) { \
log_err ( " % " format " number printed Got: %d, Expected: %d \n " , uNumPrinted , cNumPrinted ) ; \
} \
2002-03-27 19:19:52 +00:00
static void TestFprintfFormat ( ) {
static const UChar abcUChars [ ] = { 0x61 , 0x62 , 0x63 , 0 } ;
static const char abcChars [ ] = " abc " ;
UChar uBuffer [ 256 ] ;
char buffer [ 256 ] ;
char compBuffer [ 256 ] ;
int32_t uNumPrinted ;
int32_t cNumPrinted ;
UFILE * myFile ;
TestFPrintFormat ( " %8U " , abcUChars , " %8s " , abcChars ) ;
TestFPrintFormat ( " %-8U " , abcUChars , " %-8s " , abcChars ) ;
2003-10-11 02:16:12 +00:00
TestFPrintFormat ( " %.2U " , abcUChars , " %.2s " , abcChars ) ; /* strlen is 3 */
2002-03-27 19:19:52 +00:00
TestFPrintFormat ( " %8s " , abcChars , " %8s " , abcChars ) ;
TestFPrintFormat ( " %-8s " , abcChars , " %-8s " , abcChars ) ;
2003-10-11 02:16:12 +00:00
TestFPrintFormat ( " %.2s " , abcChars , " %.2s " , abcChars ) ; /* strlen is 3 */
2002-03-27 19:19:52 +00:00
TestFPrintFormat ( " %8c " , 0x65 , " %8c " , 0x65 ) ;
TestFPrintFormat ( " %-8c " , 0x65 , " %-8c " , 0x65 ) ;
2003-10-11 02:16:12 +00:00
TestFPrintFormat ( " %8C " , ( UChar ) 0x65 , " %8c " , ( char ) 0x65 ) ;
TestFPrintFormat ( " %-8C " , ( UChar ) 0x65 , " %-8c " , ( char ) 0x65 ) ;
2002-03-27 19:19:52 +00:00
2002-03-30 00:02:40 +00:00
TestFPrintFormat ( " %10f " , 1.23456789 , " %10f " , 1.23456789 ) ;
TestFPrintFormat ( " %-10f " , 1.23456789 , " %-10f " , 1.23456789 ) ;
TestFPrintFormat ( " %10f " , 123.456789 , " %10f " , 123.456789 ) ;
TestFPrintFormat ( " %-10f " , 123.456789 , " %-10f " , 123.456789 ) ;
2002-03-27 19:19:52 +00:00
2003-03-04 19:12:55 +00:00
TestFPrintFormat ( " %e " , 1234567.89 , " %e " , 1234567.89 ) ;
TestFPrintFormat ( " %E " , 1234567.89 , " %E " , 1234567.89 ) ;
2002-03-30 00:02:40 +00:00
TestFPrintFormat ( " %10e " , 1.23456789 , " %10e " , 1.23456789 ) ;
TestFPrintFormat ( " %-10e " , 1.23456789 , " %-10e " , 1.23456789 ) ;
2003-03-04 19:12:55 +00:00
TestFPrintFormat ( " %10e " , 1234.56789 , " %10e " , 1234.56789 ) ;
TestFPrintFormat ( " %-10e " , 1234.56789 , " %-10e " , 1234.56789 ) ;
2002-03-27 19:19:52 +00:00
2003-03-04 19:12:55 +00:00
TestFPrintFormat ( " %g " , 123456.789 , " %g " , 123456.789 ) ;
TestFPrintFormat ( " %g " , 1234567.89 , " %g " , 1234567.89 ) ;
TestFPrintFormat ( " %G " , 1234567.89 , " %G " , 1234567.89 ) ;
2002-03-30 00:02:40 +00:00
TestFPrintFormat ( " %10g " , 1.23456789 , " %10g " , 1.23456789 ) ;
TestFPrintFormat ( " %-10g " , 1.23456789 , " %-10g " , 1.23456789 ) ;
TestFPrintFormat ( " %10g " , 123.456789 , " %10g " , 123.456789 ) ;
TestFPrintFormat ( " %-10g " , 123.456789 , " %-10g " , 123.456789 ) ;
2002-03-27 19:19:52 +00:00
TestFPrintFormat ( " %8x " , 123456 , " %8x " , 123456 ) ;
TestFPrintFormat ( " %-8x " , 123456 , " %-8x " , 123456 ) ;
2003-10-11 02:16:12 +00:00
TestFPrintFormat ( " %08x " , 123456 , " %08x " , 123456 ) ;
2002-03-27 19:19:52 +00:00
TestFPrintFormat ( " %8X " , 123456 , " %8X " , 123456 ) ;
TestFPrintFormat ( " %-8X " , 123456 , " %-8X " , 123456 ) ;
2003-10-11 02:16:12 +00:00
TestFPrintFormat ( " %08X " , 123456 , " %08X " , 123456 ) ;
2002-03-27 19:19:52 +00:00
TestFPrintFormat ( " %#x " , 123456 , " %#x " , 123456 ) ;
TestFPrintFormat ( " %#x " , - 123456 , " %#x " , - 123456 ) ;
TestFPrintFormat ( " %8o " , 123456 , " %8o " , 123456 ) ;
TestFPrintFormat ( " %-8o " , 123456 , " %-8o " , 123456 ) ;
2003-10-11 02:16:12 +00:00
TestFPrintFormat ( " %08o " , 123456 , " %08o " , 123456 ) ;
2002-03-27 19:19:52 +00:00
TestFPrintFormat ( " %#o " , 123 , " %#o " , 123 ) ;
TestFPrintFormat ( " %#o " , - 123 , " %#o " , - 123 ) ;
TestFPrintFormat ( " %8u " , 123456 , " %8u " , 123456 ) ;
TestFPrintFormat ( " %-8u " , 123456 , " %-8u " , 123456 ) ;
2003-10-11 02:16:12 +00:00
TestFPrintFormat ( " %08u " , 123456 , " %08u " , 123456 ) ;
2002-03-27 19:19:52 +00:00
TestFPrintFormat ( " %8u " , - 123456 , " %8u " , - 123456 ) ;
TestFPrintFormat ( " %-8u " , - 123456 , " %-8u " , - 123456 ) ;
2003-10-11 02:16:12 +00:00
TestFPrintFormat ( " %-08u " , - 123456 , " %-08u " , - 123456 ) ;
2002-03-27 19:19:52 +00:00
TestFPrintFormat ( " %8d " , 123456 , " %8d " , 123456 ) ;
TestFPrintFormat ( " %-8d " , 123456 , " %-8d " , 123456 ) ;
2003-10-11 02:16:12 +00:00
TestFPrintFormat ( " %08d " , 123456 , " %08d " , 123456 ) ;
2002-03-30 00:02:40 +00:00
TestFPrintFormat ( " % d " , 123456 , " % d " , 123456 ) ;
TestFPrintFormat ( " % d " , - 123456 , " % d " , - 123456 ) ;
2002-03-27 19:19:52 +00:00
TestFPrintFormat ( " %8i " , 123456 , " %8i " , 123456 ) ;
TestFPrintFormat ( " %-8i " , 123456 , " %-8i " , 123456 ) ;
2003-10-11 02:16:12 +00:00
TestFPrintFormat ( " %08i " , 123456 , " %08i " , 123456 ) ;
2002-03-27 19:19:52 +00:00
2002-08-26 20:53:53 +00:00
TestFPrintFormat2 ( " %+1.*e " , 4 , 1.2345678 ) ;
TestFPrintFormat2 ( " %+2.*e " , 6 , 1.2345678 ) ;
2003-10-11 02:16:12 +00:00
/* TODO: Figure out what to do here. This is very platform dependent */
/*TestFPrintFormat("%p", (void*)123456, "%p", (void*)123456);
TestFPrintFormat ( " %.2p " , ( void * ) 123456 , " %.2p " , ( void * ) 123456 ) ; */
2002-03-27 19:19:52 +00:00
log_verbose ( " Get really crazy with the formatting. \n " ) ;
2002-03-30 00:02:40 +00:00
TestFPrintFormat ( " %-#12x " , 123 , " %-#12x " , 123 ) ;
TestFPrintFormat ( " %-#12x " , - 123 , " %-#12x " , - 123 ) ;
TestFPrintFormat ( " %#12x " , 123 , " %#12x " , 123 ) ;
TestFPrintFormat ( " %#12x " , - 123 , " %#12x " , - 123 ) ;
TestFPrintFormat ( " %-+12d " , 123 , " %-+12d " , 123 ) ;
TestFPrintFormat ( " %-+12d " , - 123 , " %-+12d " , - 123 ) ;
TestFPrintFormat ( " %- 12d " , 123 , " %- 12d " , 123 ) ;
2002-03-27 19:19:52 +00:00
TestFPrintFormat ( " %- 12d " , - 123 , " %- 12d " , - 123 ) ;
2002-03-30 00:02:40 +00:00
TestFPrintFormat ( " %+12d " , 123 , " %+12d " , 123 ) ;
TestFPrintFormat ( " %+12d " , - 123 , " %+12d " , - 123 ) ;
TestFPrintFormat ( " % 12d " , 123 , " % 12d " , 123 ) ;
TestFPrintFormat ( " % 12d " , - 123 , " % 12d " , - 123 ) ;
TestFPrintFormat ( " %12d " , 123 , " %12d " , 123 ) ;
TestFPrintFormat ( " %12d " , - 123 , " %12d " , - 123 ) ;
2003-10-11 02:16:12 +00:00
TestFPrintFormat ( " %.12d " , 123 , " %.12d " , 123 ) ;
TestFPrintFormat ( " %.12d " , - 123 , " %.12d " , - 123 ) ;
2002-03-30 00:02:40 +00:00
TestFPrintFormat ( " %-+12.1e " , 1.234 , " %-+12.1e " , 1.234 ) ;
TestFPrintFormat ( " %-+12.1e " , - 1.234 , " %-+12.1e " , - 1.234 ) ;
2003-10-11 02:16:12 +00:00
TestFPrintFormat ( " %- 12.10e " , 1.234 , " %- 12.10e " , 1.234 ) ;
2002-03-30 00:02:40 +00:00
TestFPrintFormat ( " %- 12.1e " , - 1.234 , " %- 12.1e " , - 1.234 ) ;
TestFPrintFormat ( " %+12.1e " , 1.234 , " %+12.1e " , 1.234 ) ;
TestFPrintFormat ( " %+12.1e " , - 1.234 , " %+12.1e " , - 1.234 ) ;
TestFPrintFormat ( " % 12.1e " , 1.234 , " % 12.1e " , 1.234 ) ;
TestFPrintFormat ( " % 12.1e " , - 1.234 , " % 12.1e " , - 1.234 ) ;
TestFPrintFormat ( " %12.1e " , 1.234 , " %12.1e " , 1.234 ) ;
TestFPrintFormat ( " %12.1e " , - 1.234 , " %12.1e " , - 1.234 ) ;
TestFPrintFormat ( " %.2e " , 1.234 , " %.2e " , 1.234 ) ;
TestFPrintFormat ( " %.2e " , - 1.234 , " %.2e " , - 1.234 ) ;
TestFPrintFormat ( " %3e " , 1.234 , " %3e " , 1.234 ) ;
TestFPrintFormat ( " %3e " , - 1.234 , " %3e " , - 1.234 ) ;
TestFPrintFormat ( " %-+12.1f " , 1.234 , " %-+12.1f " , 1.234 ) ;
TestFPrintFormat ( " %-+12.1f " , - 1.234 , " %-+12.1f " , - 1.234 ) ;
2003-10-11 02:16:12 +00:00
TestFPrintFormat ( " %- 12.10f " , 1.234 , " %- 12.10f " , 1.234 ) ;
2002-03-30 00:02:40 +00:00
TestFPrintFormat ( " %- 12.1f " , - 1.234 , " %- 12.1f " , - 1.234 ) ;
TestFPrintFormat ( " %+12.1f " , 1.234 , " %+12.1f " , 1.234 ) ;
TestFPrintFormat ( " %+12.1f " , - 1.234 , " %+12.1f " , - 1.234 ) ;
TestFPrintFormat ( " % 12.1f " , 1.234 , " % 12.1f " , 1.234 ) ;
TestFPrintFormat ( " % 12.1f " , - 1.234 , " % 12.1f " , - 1.234 ) ;
TestFPrintFormat ( " %12.1f " , 1.234 , " %12.1f " , 1.234 ) ;
TestFPrintFormat ( " %12.1f " , - 1.234 , " %12.1f " , - 1.234 ) ;
TestFPrintFormat ( " %.2f " , 1.234 , " %.2f " , 1.234 ) ;
TestFPrintFormat ( " %.2f " , - 1.234 , " %.2f " , - 1.234 ) ;
TestFPrintFormat ( " %3f " , 1.234 , " %3f " , 1.234 ) ;
TestFPrintFormat ( " %3f " , - 1.234 , " %3f " , - 1.234 ) ;
2002-08-24 00:00:31 +00:00
2003-09-16 23:36:28 +00:00
myFile = u_fopen ( STANDARD_TEST_FILE , " w " , " en_US_POSIX " , NULL ) ;
/* Reinitialize the buffer to verify null termination works. */
u_memset ( uBuffer , 0x2a , sizeof ( uBuffer ) / sizeof ( * uBuffer ) ) ;
memset ( buffer , 0x2a , sizeof ( buffer ) / sizeof ( * buffer ) ) ;
uNumPrinted = u_fprintf ( myFile , " %d % d %d " , - 1234 , 1234 , 1234 ) ;
u_fclose ( myFile ) ;
myFile = u_fopen ( STANDARD_TEST_FILE , " r " , " en_US_POSIX " , NULL ) ;
2003-10-10 20:55:24 +00:00
u_fgets ( uBuffer , sizeof ( uBuffer ) / sizeof ( * uBuffer ) , myFile ) ;
2003-09-16 23:36:28 +00:00
u_fclose ( myFile ) ;
u_austrncpy ( compBuffer , uBuffer , sizeof ( uBuffer ) / sizeof ( * uBuffer ) ) ;
cNumPrinted = sprintf ( buffer , " %d % d %d " , - 1234 , 1234 , 1234 ) ;
if ( strcmp ( buffer , compBuffer ) ! = 0 ) {
log_err ( " %%d %% d %%d Got: \" %s \" , Expected: \" %s \" \n " , compBuffer , buffer ) ;
}
if ( cNumPrinted ! = uNumPrinted ) {
log_err ( " %%d %% d %%d number printed Got: %d, Expected: %d \n " , uNumPrinted , cNumPrinted ) ;
}
if ( buffer [ uNumPrinted + 1 ] ! = 0x2a ) {
log_err ( " %%d %% d %%d too much stored \n " ) ;
}
2002-03-27 19:19:52 +00:00
}
# undef TestFPrintFormat
2002-02-23 00:34:39 +00:00
static void TestString ( ) {
int32_t n [ 1 ] ;
float myFloat = - 1234.0 ;
UDate myDate = 0.0 ;
UDate dec_31_1969 = - 57600000.000000 ; /* TODO: These are not correct */
UDate midnight = 86400000.000000 ; /* TODO: These are not correct */
UDate myNewDate = - 1.0 ;
int32_t newValuePtr [ 1 ] ;
double newDoubleValuePtr [ 1 ] ;
UChar myUString [ 512 ] ;
UChar uStringBuf [ 512 ] ;
char myString [ 512 ] = " " ;
char testBuf [ 512 ] = " " ;
2003-10-11 02:16:12 +00:00
int32_t retVal ;
2004-03-16 06:00:02 +00:00
U_STRING_DECL ( myStringOrig , " My-String " , 9 ) ;
2002-02-23 00:34:39 +00:00
2004-03-16 06:00:02 +00:00
U_STRING_INIT ( myStringOrig , " My-String " , 9 ) ;
2002-02-23 00:34:39 +00:00
u_memset ( myUString , 0x0a , sizeof ( myUString ) / sizeof ( * myUString ) ) ;
u_memset ( uStringBuf , 0x0a , sizeof ( uStringBuf ) / sizeof ( * uStringBuf ) ) ;
* n = - 1234 ;
/* Test sprintf */
2002-03-26 20:10:52 +00:00
u_sprintf ( uStringBuf , NULL , " Signed decimal integer d: %d " , * n ) ;
2002-02-23 00:34:39 +00:00
* newValuePtr = 1 ;
2002-03-26 20:10:52 +00:00
u_sscanf ( uStringBuf , NULL , " Signed decimal integer d: %d " , newValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( * n ! = * newValuePtr ) {
log_err ( " %%d Got: %d, Expected: %d \n " , * newValuePtr , * n ) ;
}
2002-03-26 20:10:52 +00:00
u_sprintf ( uStringBuf , NULL , " Signed decimal integer i: %i " , * n ) ;
2002-02-23 00:34:39 +00:00
* newValuePtr = 1 ;
2002-03-26 20:10:52 +00:00
u_sscanf ( uStringBuf , NULL , " Signed decimal integer i: %i " , newValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( * n ! = * newValuePtr ) {
log_err ( " %%i Got: %i, Expected: %i \n " , * newValuePtr , * n ) ;
}
2002-03-26 20:10:52 +00:00
u_sprintf ( uStringBuf , NULL , " Unsigned octal integer o: %o " , * n ) ;
2002-02-23 00:34:39 +00:00
* newValuePtr = 1 ;
2002-03-26 20:10:52 +00:00
u_sscanf ( uStringBuf , NULL , " Unsigned octal integer o: %o " , newValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( * n ! = * newValuePtr ) {
log_err ( " %%o Got: %o, Expected: %o \n " , * newValuePtr , * n ) ;
}
2002-03-26 20:10:52 +00:00
u_sprintf ( uStringBuf , NULL , " Unsigned decimal integer %%u: %u " , * n ) ;
2002-02-23 00:34:39 +00:00
* newValuePtr = 1 ;
2002-03-26 20:10:52 +00:00
u_sscanf ( uStringBuf , NULL , " Unsigned decimal integer %%u: %u " , newValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( * n ! = * newValuePtr ) {
log_err ( " %%u Got: %u, Expected: %u \n " , * newValuePtr , * n ) ;
}
2002-03-26 20:10:52 +00:00
u_sprintf ( uStringBuf , NULL , " Lowercase unsigned hexadecimal integer x: %x " , * n ) ;
2002-02-23 00:34:39 +00:00
* newValuePtr = 1 ;
2002-03-26 20:10:52 +00:00
u_sscanf ( uStringBuf , NULL , " Lowercase unsigned hexadecimal integer x: %x " , newValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( * n ! = * newValuePtr ) {
log_err ( " %%x Got: %x, Expected: %x \n " , * newValuePtr , * n ) ;
}
2002-03-26 20:10:52 +00:00
u_sprintf ( uStringBuf , NULL , " Uppercase unsigned hexadecimal integer X: %X " , * n ) ;
2002-02-23 00:34:39 +00:00
* newValuePtr = 1 ;
2002-03-26 20:10:52 +00:00
u_sscanf ( uStringBuf , NULL , " Uppercase unsigned hexadecimal integer X: %X " , newValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( * n ! = * newValuePtr ) {
log_err ( " %%X Got: %X, Expected: %X \n " , * newValuePtr , * n ) ;
}
2002-03-26 20:10:52 +00:00
u_sprintf ( uStringBuf , NULL , " Float f: %f " , myFloat ) ;
2002-02-23 00:34:39 +00:00
* newDoubleValuePtr = - 1.0 ;
2002-03-26 20:10:52 +00:00
u_sscanf ( uStringBuf , NULL , " Float f: %f " , newDoubleValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( myFloat ! = * newDoubleValuePtr ) {
log_err ( " %%f Got: %f, Expected: %f \n " , * newDoubleValuePtr , myFloat ) ;
}
2002-03-26 20:10:52 +00:00
u_sprintf ( uStringBuf , NULL , " Lowercase float e: %e " , myFloat ) ;
2002-02-23 00:34:39 +00:00
* newDoubleValuePtr = - 1.0 ;
2002-03-26 20:10:52 +00:00
u_sscanf ( uStringBuf , NULL , " Lowercase float e: %e " , newDoubleValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( myFloat ! = * newDoubleValuePtr ) {
log_err ( " %%e Got: %e, Expected: %e \n " , * newDoubleValuePtr , myFloat ) ;
}
2002-03-26 20:10:52 +00:00
u_sprintf ( uStringBuf , NULL , " Uppercase float E: %E " , myFloat ) ;
2002-02-23 00:34:39 +00:00
* newDoubleValuePtr = - 1.0 ;
2002-03-26 20:10:52 +00:00
u_sscanf ( uStringBuf , NULL , " Uppercase float E: %E " , newDoubleValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( myFloat ! = * newDoubleValuePtr ) {
log_err ( " %%E Got: %E, Expected: %E \n " , * newDoubleValuePtr , myFloat ) ;
}
2002-03-26 20:10:52 +00:00
u_sprintf ( uStringBuf , NULL , " Lowercase float g: %g " , myFloat ) ;
2002-02-23 00:34:39 +00:00
* newDoubleValuePtr = - 1.0 ;
2002-03-26 20:10:52 +00:00
u_sscanf ( uStringBuf , NULL , " Lowercase float g: %g " , newDoubleValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( myFloat ! = * newDoubleValuePtr ) {
log_err ( " %%g Got: %g, Expected: %g \n " , * newDoubleValuePtr , myFloat ) ;
}
2002-03-26 20:10:52 +00:00
u_sprintf ( uStringBuf , NULL , " Uppercase float G: %G " , myFloat ) ;
2002-02-23 00:34:39 +00:00
* newDoubleValuePtr = - 1.0 ;
2002-03-26 20:10:52 +00:00
u_sscanf ( uStringBuf , NULL , " Uppercase float G: %G " , newDoubleValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( myFloat ! = * newDoubleValuePtr ) {
log_err ( " %%G Got: %G, Expected: %G \n " , * newDoubleValuePtr , myFloat ) ;
}
// u_sprintf(uStringBuf, NULL, "Pointer %%p: %p\n", myFile);
2002-03-26 20:10:52 +00:00
u_sprintf ( uStringBuf , NULL , " Char c: %c " , ' A ' ) ;
u_sscanf ( uStringBuf , NULL , " Char c: %c " , myString ) ;
2002-02-23 00:34:39 +00:00
if ( * myString ! = ' A ' ) {
log_err ( " %%c Got: %c, Expected: A \n " , * myString ) ;
}
2004-03-16 06:00:02 +00:00
u_sprintf ( uStringBuf , NULL , " UChar %%C: %C " , ( UChar ) 0x0041 ) ; /*'A'*/
2003-09-25 21:14:35 +00:00
u_sscanf ( uStringBuf , NULL , " UChar %%C: %C " , myUString ) ;
2004-03-16 06:00:02 +00:00
if ( * myUString ! = ( UChar ) 0x0041 ) { /*'A'*/
2002-02-23 00:34:39 +00:00
log_err ( " %%C Got: %C, Expected: A \n " , * myUString ) ;
}
2002-03-26 20:10:52 +00:00
u_sprintf ( uStringBuf , NULL , " String %%s: %s " , " My-String " ) ;
u_sscanf ( uStringBuf , NULL , " String %%s: %s " , myString ) ;
2002-02-23 00:34:39 +00:00
if ( strcmp ( myString , " My-String " ) ) {
log_err ( " %%s Got: %s, Expected: My-String \n " , myString ) ;
}
2002-03-26 20:10:52 +00:00
if ( uStringBuf [ 20 ] ! = 0 ) {
log_err ( " String not terminated. Got %c \n " , uStringBuf [ 20 ] ) ;
}
u_sprintf ( uStringBuf , NULL , " NULL String %%s: %s " , NULL ) ;
u_sscanf ( uStringBuf , NULL , " NULL String %%s: %s " , myString ) ;
if ( strcmp ( myString , " (null) " ) ) {
log_err ( " %%s Got: %s, Expected: My-String \n " , myString ) ;
2002-02-23 00:34:39 +00:00
}
2004-03-16 06:00:02 +00:00
u_sprintf ( uStringBuf , NULL , " Unicode String %%S: %S " , myStringOrig ) ;
2003-09-25 21:14:35 +00:00
u_sscanf ( uStringBuf , NULL , " Unicode String %%S: %S " , myUString ) ;
2002-04-10 23:32:18 +00:00
u_austrncpy ( myString , myUString , sizeof ( myString ) / sizeof ( * myString ) ) ;
if ( strcmp ( myString , " My-String " ) ) {
2003-09-25 21:14:35 +00:00
log_err ( " %%S Got: %s, Expected: My String \n " , myString ) ;
2002-02-23 00:34:39 +00:00
}
2003-09-25 21:14:35 +00:00
u_sprintf ( uStringBuf , NULL , " NULL Unicode String %%S: %S " , NULL ) ;
u_sscanf ( uStringBuf , NULL , " NULL Unicode String %%S: %S " , myUString ) ;
2002-03-26 20:10:52 +00:00
u_austrncpy ( myString , myUString , sizeof ( myString ) / sizeof ( * myString ) ) ;
if ( strcmp ( myString , " (null) " ) ) {
2003-09-25 21:14:35 +00:00
log_err ( " %%S Got: %s, Expected: (null) \n " , myString ) ;
2002-03-26 20:10:52 +00:00
}
u_sprintf ( uStringBuf , NULL , " Percent %%P (non-ANSI): %P " , myFloat ) ;
2002-02-23 00:34:39 +00:00
* newDoubleValuePtr = - 1.0 ;
2002-03-26 20:10:52 +00:00
u_sscanf ( uStringBuf , NULL , " Percent %%P (non-ANSI): %P " , newDoubleValuePtr ) ;
2002-02-23 00:34:39 +00:00
if ( myFloat ! = * newDoubleValuePtr ) {
log_err ( " %%P Got: %P, Expected: %P \n " , * newDoubleValuePtr , myFloat ) ;
}
2002-03-28 19:53:13 +00:00
u_sprintf ( uStringBuf , NULL , " Spell Out %%V (non-ANSI): %V " , myFloat ) ;
* newDoubleValuePtr = - 1.0 ;
u_sscanf ( uStringBuf , NULL , " Spell Out %%V (non-ANSI): %V " , newDoubleValuePtr ) ;
if ( myFloat ! = * newDoubleValuePtr ) {
log_err ( " %%V Got: %f, Expected: %f \n " , * newDoubleValuePtr , myFloat ) ;
}
* newValuePtr = 1 ;
u_sprintf ( uStringBuf , NULL , " \t \n Pointer to integer (Count) %%n: n=%d %n n=%d \n " , * newValuePtr , newValuePtr , * newValuePtr ) ;
if ( * newValuePtr ! = 37 ) {
log_err ( " %%V Got: %f, Expected: %f \n " , * newDoubleValuePtr , myFloat ) ;
}
2003-10-11 02:16:12 +00:00
// u_sscanf(uStringBuf, NULL, "Pointer %%p: %p\n", myFile);
2003-03-04 19:12:55 +00:00
2003-10-11 02:16:12 +00:00
{
static const char longStr [ ] = " This is a long test12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 " ;
retVal = u_sprintf ( uStringBuf , NULL , longStr ) ;
u_austrncpy ( myString , uStringBuf , sizeof ( uStringBuf ) / sizeof ( * uStringBuf ) ) ;
if ( strcmp ( myString , longStr ) ) {
log_err ( " %%S Got: %s, Expected: %s \n " , myString , longStr ) ;
}
if ( retVal ! = strlen ( longStr ) ) {
log_err ( " %%S returned different sizes. Got: %d Expected: %d \n " , retVal , strlen ( longStr ) ) ;
}
retVal = u_sprintf ( uStringBuf , NULL , " %s " , longStr ) ;
u_austrncpy ( myString , uStringBuf , sizeof ( uStringBuf ) / sizeof ( * uStringBuf ) ) ;
if ( strcmp ( myString , longStr ) ) {
log_err ( " %%S Got: %s, Expected: %s \n " , myString , longStr ) ;
}
if ( retVal ! = strlen ( longStr ) ) {
log_err ( " %%S returned different sizes. Got: %d Expected: %d \n " , retVal , strlen ( longStr ) ) ;
}
u_uastrncpy ( myUString , longStr , sizeof ( longStr ) / sizeof ( * longStr ) ) ;
u_sprintf_u ( uStringBuf , NULL , myUString ) ;
if ( u_strcmp ( myUString , uStringBuf ) ) {
log_err ( " %%S Long strings differ. Expected: %s \n " , longStr ) ;
}
u_uastrncpy ( myUString , longStr , sizeof ( longStr ) / sizeof ( * longStr ) ) ;
retVal = u_sprintf_u ( uStringBuf , NULL , myUString + 10 ) ;
if ( u_strcmp ( myUString + 10 , uStringBuf ) ) {
log_err ( " %%S Long strings differ. Expected: %s \n " , longStr + 10 ) ;
}
if ( retVal ! = strlen ( longStr + 10 ) ) {
log_err ( " %%S returned different sizes. Got: %d Expected: %d \n " , retVal , strlen ( longStr ) ) ;
}
u_memset ( uStringBuf , 1 , sizeof ( longStr ) / sizeof ( * longStr ) ) ;
u_uastrncpy ( myUString , longStr , sizeof ( longStr ) / sizeof ( * longStr ) ) ;
retVal = u_snprintf_u ( uStringBuf , 10 , NULL , myUString ) ;
if ( u_strncmp ( myUString , uStringBuf , 10 ) | | uStringBuf [ 10 ] ! = 1 | | retVal ! = 10 ) {
log_err ( " %%S Long strings differ. Expected the first 10 characters of %s \n " , longStr ) ;
}
}
2002-02-23 00:34:39 +00:00
}
2002-03-27 19:19:52 +00:00
# define Test_u_snprintf(limit, format, value, expectedSize, expectedStr) \
u_uastrncpy ( testStr , " xxxxxxxxxxxxxx " , sizeof ( testStr ) / sizeof ( testStr [ 0 ] ) ) ; \
size = u_snprintf ( testStr , limit , " en_US_POSIX " , format , value ) ; \
u_austrncpy ( cTestResult , testStr , sizeof ( cTestResult ) / sizeof ( cTestResult [ 0 ] ) ) ; \
if ( size ! = expectedSize | | strcmp ( cTestResult , expectedStr ) ! = 0 ) { \
log_err ( " Unexpected formatting. size=%d expectedSize=%d cTestResult=%s expectedStr=%s \n " , \
size , expectedSize , cTestResult , expectedStr ) ; \
} \
else { \
log_verbose ( " Got: %s \n " , cTestResult ) ; \
} \
static void TestSnprintf ( ) {
UChar testStr [ 256 ] ;
char cTestResult [ 256 ] ;
int32_t size ;
Test_u_snprintf ( 0 , " %d " , 123 , 0 , " xxxxxxxxxxxxxx " ) ;
Test_u_snprintf ( 2 , " %d " , 123 , 2 , " 12xxxxxxxxxxxx " ) ;
Test_u_snprintf ( 3 , " %d " , 123 , 3 , " 123xxxxxxxxxxx " ) ;
Test_u_snprintf ( 4 , " %d " , 123 , 3 , " 123 " ) ;
Test_u_snprintf ( 0 , " %s " , " abcd " , 0 , " xxxxxxxxxxxxxx " ) ;
Test_u_snprintf ( 3 , " %s " , " abcd " , 3 , " abcxxxxxxxxxxx " ) ;
Test_u_snprintf ( 4 , " %s " , " abcd " , 4 , " abcdxxxxxxxxxx " ) ;
Test_u_snprintf ( 5 , " %s " , " abcd " , 4 , " abcd " ) ;
Test_u_snprintf ( 0 , " %e " , 12.34 , 0 , " xxxxxxxxxxxxxx " ) ;
Test_u_snprintf ( 1 , " %e " , 12.34 , 1 , " 1xxxxxxxxxxxxx " ) ;
Test_u_snprintf ( 2 , " %e " , 12.34 , 2 , " 1.xxxxxxxxxxxx " ) ;
Test_u_snprintf ( 3 , " %e " , 12.34 , 3 , " 1.2xxxxxxxxxxx " ) ;
Test_u_snprintf ( 5 , " %e " , 12.34 , 5 , " 1.234xxxxxxxxx " ) ;
Test_u_snprintf ( 6 , " %e " , 12.34 , 6 , " 1.2340xxxxxxxx " ) ;
Test_u_snprintf ( 8 , " %e " , 12.34 , 8 , " 1.234000xxxxxx " ) ;
Test_u_snprintf ( 9 , " %e " , 12.34 , 9 , " 1.234000exxxxx " ) ;
Test_u_snprintf ( 10 , " %e " , 12.34 , 10 , " 1.234000e+xxxx " ) ;
Test_u_snprintf ( 11 , " %e " , 12.34 , 11 , " 1.234000e+0xxx " ) ;
Test_u_snprintf ( 13 , " %e " , 12.34 , 13 , " 1.234000e+001x " ) ;
Test_u_snprintf ( 14 , " %e " , 12.34 , 13 , " 1.234000e+001 " ) ;
}
# define TestSPrintFormat(uFormat, uValue, cFormat, cValue) \
/* Reinitialize the buffer to verify null termination works. */ \
u_memset ( uBuffer , 0x2a , sizeof ( uBuffer ) / sizeof ( * uBuffer ) ) ; \
memset ( buffer , 0x2a , sizeof ( buffer ) / sizeof ( * buffer ) ) ; \
\
uNumPrinted = u_sprintf ( uBuffer , " en_US_POSIX " , uFormat , uValue ) ; \
u_austrncpy ( compBuffer , uBuffer , sizeof ( uBuffer ) / sizeof ( uBuffer [ 0 ] ) ) ; \
cNumPrinted = sprintf ( buffer , cFormat , cValue ) ; \
if ( strcmp ( buffer , compBuffer ) ! = 0 ) { \
log_err ( " % " uFormat " Got: \" %s \" , Expected: \" %s \" \n " , compBuffer , buffer ) ; \
} \
if ( cNumPrinted ! = uNumPrinted ) { \
log_err ( " % " uFormat " number printed Got: %d, Expected: %d \n " , uNumPrinted , cNumPrinted ) ; \
} \
2002-08-26 20:53:53 +00:00
if ( buffer [ uNumPrinted + 1 ] ! = 0x2a ) { \
log_err ( " % " uFormat " too much stored \n " ) ; \
} \
2002-03-27 19:19:52 +00:00
2002-08-24 00:00:31 +00:00
# define TestSPrintFormat2(format, precision, value) \
/* Reinitialize the buffer to verify null termination works. */ \
u_memset ( uBuffer , 0x2a , sizeof ( uBuffer ) / sizeof ( * uBuffer ) ) ; \
memset ( buffer , 0x2a , sizeof ( buffer ) / sizeof ( * buffer ) ) ; \
\
uNumPrinted = u_sprintf ( uBuffer , " en_US_POSIX " , format , precision , value ) ; \
u_austrncpy ( compBuffer , uBuffer , sizeof ( uBuffer ) / sizeof ( uBuffer [ 0 ] ) ) ; \
cNumPrinted = sprintf ( buffer , format , precision , value ) ; \
if ( strcmp ( buffer , compBuffer ) ! = 0 ) { \
log_err ( " % " format " Got: \" %s \" , Expected: \" %s \" \n " , compBuffer , buffer ) ; \
} \
if ( cNumPrinted ! = uNumPrinted ) { \
log_err ( " % " format " number printed Got: %d, Expected: %d \n " , uNumPrinted , cNumPrinted ) ; \
} \
2002-03-27 19:19:52 +00:00
static void TestSprintfFormat ( ) {
static const UChar abcUChars [ ] = { 0x61 , 0x62 , 0x63 , 0 } ;
static const char abcChars [ ] = " abc " ;
UChar uBuffer [ 256 ] ;
char buffer [ 256 ] ;
char compBuffer [ 256 ] ;
int32_t uNumPrinted ;
int32_t cNumPrinted ;
TestSPrintFormat ( " %8U " , abcUChars , " %8s " , abcChars ) ;
TestSPrintFormat ( " %-8U " , abcUChars , " %-8s " , abcChars ) ;
2003-10-11 02:16:12 +00:00
TestSPrintFormat ( " %.2U " , abcUChars , " %.2s " , abcChars ) ; /* strlen is 3 */
2002-03-27 19:19:52 +00:00
TestSPrintFormat ( " %8s " , abcChars , " %8s " , abcChars ) ;
TestSPrintFormat ( " %-8s " , abcChars , " %-8s " , abcChars ) ;
2003-10-11 02:16:12 +00:00
TestSPrintFormat ( " %.2s " , abcChars , " %.2s " , abcChars ) ; /* strlen is 3 */
2002-03-27 19:19:52 +00:00
TestSPrintFormat ( " %8c " , 0x65 , " %8c " , 0x65 ) ;
TestSPrintFormat ( " %-8c " , 0x65 , " %-8c " , 0x65 ) ;
2003-10-11 02:16:12 +00:00
TestSPrintFormat ( " %8C " , ( UChar ) 0x65 , " %8c " , ( char ) 0x65 ) ;
TestSPrintFormat ( " %-8C " , ( UChar ) 0x65 , " %-8c " , ( char ) 0x65 ) ;
2002-03-27 19:19:52 +00:00
2002-03-30 00:02:40 +00:00
TestSPrintFormat ( " %10f " , 1.23456789 , " %10f " , 1.23456789 ) ;
TestSPrintFormat ( " %-10f " , 1.23456789 , " %-10f " , 1.23456789 ) ;
TestSPrintFormat ( " %10f " , 123.456789 , " %10f " , 123.456789 ) ;
TestSPrintFormat ( " %-10f " , 123.456789 , " %-10f " , 123.456789 ) ;
2002-03-27 19:19:52 +00:00
2003-03-04 19:12:55 +00:00
TestSPrintFormat ( " %e " , 1234567.89 , " %e " , 1234567.89 ) ;
TestSPrintFormat ( " %E " , 1234567.89 , " %E " , 1234567.89 ) ;
2002-03-30 00:02:40 +00:00
TestSPrintFormat ( " %10e " , 1.23456789 , " %10e " , 1.23456789 ) ;
TestSPrintFormat ( " %-10e " , 1.23456789 , " %-10e " , 1.23456789 ) ;
TestSPrintFormat ( " %10e " , 123.456789 , " %10e " , 123.456789 ) ;
TestSPrintFormat ( " %-10e " , 123.456789 , " %-10e " , 123.456789 ) ;
2002-03-27 19:19:52 +00:00
2003-03-04 19:12:55 +00:00
TestSPrintFormat ( " %g " , 123456.789 , " %g " , 123456.789 ) ;
TestSPrintFormat ( " %g " , 1234567.89 , " %g " , 1234567.89 ) ;
TestSPrintFormat ( " %G " , 1234567.89 , " %G " , 1234567.89 ) ;
2002-03-30 00:02:40 +00:00
TestSPrintFormat ( " %10g " , 1.23456789 , " %10g " , 1.23456789 ) ;
TestSPrintFormat ( " %-10g " , 1.23456789 , " %-10g " , 1.23456789 ) ;
TestSPrintFormat ( " %10g " , 123.456789 , " %10g " , 123.456789 ) ;
TestSPrintFormat ( " %-10g " , 123.456789 , " %-10g " , 123.456789 ) ;
2002-03-27 19:19:52 +00:00
TestSPrintFormat ( " %8x " , 123456 , " %8x " , 123456 ) ;
TestSPrintFormat ( " %-8x " , 123456 , " %-8x " , 123456 ) ;
TestSPrintFormat ( " %8X " , 123456 , " %8X " , 123456 ) ;
TestSPrintFormat ( " %-8X " , 123456 , " %-8X " , 123456 ) ;
TestSPrintFormat ( " %#x " , 123456 , " %#x " , 123456 ) ;
TestSPrintFormat ( " %#x " , - 123456 , " %#x " , - 123456 ) ;
TestSPrintFormat ( " %8o " , 123456 , " %8o " , 123456 ) ;
TestSPrintFormat ( " %-8o " , 123456 , " %-8o " , 123456 ) ;
TestSPrintFormat ( " %#o " , 123 , " %#o " , 123 ) ;
TestSPrintFormat ( " %#o " , - 123 , " %#o " , - 123 ) ;
TestSPrintFormat ( " %8u " , 123456 , " %8u " , 123456 ) ;
TestSPrintFormat ( " %-8u " , 123456 , " %-8u " , 123456 ) ;
TestSPrintFormat ( " %8u " , - 123456 , " %8u " , - 123456 ) ;
TestSPrintFormat ( " %-8u " , - 123456 , " %-8u " , - 123456 ) ;
TestSPrintFormat ( " %8d " , 123456 , " %8d " , 123456 ) ;
TestSPrintFormat ( " %-8d " , 123456 , " %-8d " , 123456 ) ;
2002-03-30 00:02:40 +00:00
TestSPrintFormat ( " % d " , 123456 , " % d " , 123456 ) ;
TestSPrintFormat ( " % d " , - 123456 , " % d " , - 123456 ) ;
2002-03-27 19:19:52 +00:00
TestSPrintFormat ( " %8i " , 123456 , " %8i " , 123456 ) ;
TestSPrintFormat ( " %-8i " , 123456 , " %-8i " , 123456 ) ;
2002-08-26 20:53:53 +00:00
TestSPrintFormat2 ( " %+1.*e " , 4 , 1.2345678 ) ;
TestSPrintFormat2 ( " %+2.*e " , 6 , 1.2345678 ) ;
2003-10-11 02:16:12 +00:00
/* TODO: Figure out what to do here. This is very platform dependent */
/*TestSPrintFormat("%p", (void*)123456, "%p", (void*)123456);
TestSPrintFormat ( " %.2p " , ( void * ) 123456 , " %.2p " , ( void * ) 123456 ) ; */
2002-03-27 19:19:52 +00:00
log_verbose ( " Get really crazy with the formatting. \n " ) ;
2002-03-30 00:02:40 +00:00
TestSPrintFormat ( " %-#12x " , 123 , " %-#12x " , 123 ) ;
TestSPrintFormat ( " %-#12x " , - 123 , " %-#12x " , - 123 ) ;
TestSPrintFormat ( " %#12x " , 123 , " %#12x " , 123 ) ;
TestSPrintFormat ( " %#12x " , - 123 , " %#12x " , - 123 ) ;
TestSPrintFormat ( " %-+12d " , 123 , " %-+12d " , 123 ) ;
TestSPrintFormat ( " %-+12d " , - 123 , " %-+12d " , - 123 ) ;
TestSPrintFormat ( " %- 12d " , 123 , " %- 12d " , 123 ) ;
2002-03-27 19:19:52 +00:00
TestSPrintFormat ( " %- 12d " , - 123 , " %- 12d " , - 123 ) ;
2002-03-30 00:02:40 +00:00
TestSPrintFormat ( " %+12d " , 123 , " %+12d " , 123 ) ;
TestSPrintFormat ( " %+12d " , - 123 , " %+12d " , - 123 ) ;
TestSPrintFormat ( " % 12d " , 123 , " % 12d " , 123 ) ;
TestSPrintFormat ( " % 12d " , - 123 , " % 12d " , - 123 ) ;
TestSPrintFormat ( " %12d " , 123 , " %12d " , 123 ) ;
TestSPrintFormat ( " %12d " , - 123 , " %12d " , - 123 ) ;
2003-10-11 02:16:12 +00:00
TestSPrintFormat ( " %.12d " , 123 , " %.12d " , 123 ) ;
TestSPrintFormat ( " %.12d " , - 123 , " %.12d " , - 123 ) ;
2002-03-30 00:02:40 +00:00
TestSPrintFormat ( " %-+12.1e " , 1.234 , " %-+12.1e " , 1.234 ) ;
TestSPrintFormat ( " %-+12.1e " , - 1.234 , " %-+12.1e " , - 1.234 ) ;
2003-10-11 02:16:12 +00:00
TestSPrintFormat ( " %- 12.10e " , 1.234 , " %- 12.10e " , 1.234 ) ;
2002-03-30 00:02:40 +00:00
TestSPrintFormat ( " %- 12.1e " , - 1.234 , " %- 12.1e " , - 1.234 ) ;
TestSPrintFormat ( " %+12.1e " , 1.234 , " %+12.1e " , 1.234 ) ;
TestSPrintFormat ( " %+12.1e " , - 1.234 , " %+12.1e " , - 1.234 ) ;
TestSPrintFormat ( " % 12.1e " , 1.234 , " % 12.1e " , 1.234 ) ;
TestSPrintFormat ( " % 12.1e " , - 1.234 , " % 12.1e " , - 1.234 ) ;
TestSPrintFormat ( " %12.1e " , 1.234 , " %12.1e " , 1.234 ) ;
TestSPrintFormat ( " %12.1e " , - 1.234 , " %12.1e " , - 1.234 ) ;
TestSPrintFormat ( " %.2e " , 1.234 , " %.2e " , 1.234 ) ;
TestSPrintFormat ( " %.2e " , - 1.234 , " %.2e " , - 1.234 ) ;
TestSPrintFormat ( " %3e " , 1.234 , " %3e " , 1.234 ) ;
TestSPrintFormat ( " %3e " , - 1.234 , " %3e " , - 1.234 ) ;
TestSPrintFormat ( " %-+12.1f " , 1.234 , " %-+12.1f " , 1.234 ) ;
TestSPrintFormat ( " %-+12.1f " , - 1.234 , " %-+12.1f " , - 1.234 ) ;
2003-10-11 02:16:12 +00:00
TestSPrintFormat ( " %- 12.10f " , 1.234 , " %- 12.10f " , 1.234 ) ;
2002-03-30 00:02:40 +00:00
TestSPrintFormat ( " %- 12.1f " , - 1.234 , " %- 12.1f " , - 1.234 ) ;
TestSPrintFormat ( " %+12.1f " , 1.234 , " %+12.1f " , 1.234 ) ;
TestSPrintFormat ( " %+12.1f " , - 1.234 , " %+12.1f " , - 1.234 ) ;
TestSPrintFormat ( " % 12.1f " , 1.234 , " % 12.1f " , 1.234 ) ;
TestSPrintFormat ( " % 12.1f " , - 1.234 , " % 12.1f " , - 1.234 ) ;
TestSPrintFormat ( " %12.1f " , 1.234 , " %12.1f " , 1.234 ) ;
TestSPrintFormat ( " %12.1f " , - 1.234 , " %12.1f " , - 1.234 ) ;
TestSPrintFormat ( " %.2f " , 1.234 , " %.2f " , 1.234 ) ;
TestSPrintFormat ( " %.2f " , - 1.234 , " %.2f " , - 1.234 ) ;
TestSPrintFormat ( " %3f " , 1.234 , " %3f " , 1.234 ) ;
TestSPrintFormat ( " %3f " , - 1.234 , " %3f " , - 1.234 ) ;
2002-08-24 00:00:31 +00:00
2002-03-27 19:19:52 +00:00
}
# undef TestSPrintFormat
2002-02-23 00:34:39 +00:00
static void TestStringCompatibility ( ) {
2002-03-27 19:19:52 +00:00
UChar myUString [ 256 ] ;
UChar uStringBuf [ 256 ] ;
char myString [ 256 ] = " " ;
char testBuf [ 256 ] = " " ;
2002-02-23 00:34:39 +00:00
int32_t num ;
u_memset ( myUString , 0x0a , sizeof ( myUString ) / sizeof ( * myUString ) ) ;
u_memset ( uStringBuf , 0x0a , sizeof ( uStringBuf ) / sizeof ( * uStringBuf ) ) ;
/* Compare against C API compatibility */
for ( num = - STANDARD_TEST_NUM_RANGE ; num < STANDARD_TEST_NUM_RANGE ; num + + ) {
sprintf ( testBuf , " %x " , num ) ;
u_sprintf ( uStringBuf , NULL , " %x " , num ) ;
2002-03-26 20:10:52 +00:00
u_austrncpy ( myString , uStringBuf , sizeof ( myString ) / sizeof ( myString [ 0 ] ) ) ;
2002-02-23 00:34:39 +00:00
if ( strcmp ( myString , testBuf ) ! = 0 ) {
log_err ( " %%x Got: \" %s \" , Expected: \" %s \" \n " , myString , testBuf ) ;
}
sprintf ( testBuf , " %X " , num ) ;
u_sprintf ( uStringBuf , NULL , " %X " , num ) ;
2002-03-26 20:10:52 +00:00
u_austrncpy ( myString , uStringBuf , sizeof ( myString ) / sizeof ( myString [ 0 ] ) ) ;
2002-02-23 00:34:39 +00:00
if ( strcmp ( myString , testBuf ) ! = 0 ) {
log_err ( " %%X Got: \" %s \" , Expected: \" %s \" \n " , myString , testBuf ) ;
}
sprintf ( testBuf , " %o " , num ) ;
u_sprintf ( uStringBuf , NULL , " %o " , num ) ;
2002-03-26 20:10:52 +00:00
u_austrncpy ( myString , uStringBuf , sizeof ( myString ) / sizeof ( myString [ 0 ] ) ) ;
2002-02-23 00:34:39 +00:00
if ( strcmp ( myString , testBuf ) ! = 0 ) {
log_err ( " %%o Got: \" %s \" , Expected: \" %s \" \n " , myString , testBuf ) ;
}
/* sprintf is not compatible on all platforms e.g. the iSeries*/
sprintf ( testBuf , " %d " , num ) ;
u_sprintf ( uStringBuf , " en_US_POSIX " , " %d " , num ) ;
2002-03-26 20:10:52 +00:00
u_austrncpy ( myString , uStringBuf , sizeof ( myString ) / sizeof ( myString [ 0 ] ) ) ;
2002-02-23 00:34:39 +00:00
if ( strcmp ( myString , testBuf ) ! = 0 ) {
log_err ( " %%d Got: \" %s \" , Expected: \" %s \" \n " , myString , testBuf ) ;
}
sprintf ( testBuf , " %i " , num ) ;
u_sprintf ( uStringBuf , " en_US_POSIX " , " %i " , num ) ;
2002-03-26 20:10:52 +00:00
u_austrncpy ( myString , uStringBuf , sizeof ( myString ) / sizeof ( myString [ 0 ] ) ) ;
2002-02-23 00:34:39 +00:00
if ( strcmp ( myString , testBuf ) ! = 0 ) {
log_err ( " %%i Got: \" %s \" , Expected: \" %s \" \n " , myString , testBuf ) ;
}
sprintf ( testBuf , " %f " , ( double ) num ) ;
u_sprintf ( uStringBuf , " en_US_POSIX " , " %f " , ( double ) num ) ;
2002-03-26 20:10:52 +00:00
u_austrncpy ( myString , uStringBuf , sizeof ( myString ) / sizeof ( myString [ 0 ] ) ) ;
2002-02-23 00:34:39 +00:00
if ( strcmp ( myString , testBuf ) ! = 0 ) {
log_err ( " %%f Got: \" %s \" , Expected: \" %s \" \n " , myString , testBuf ) ;
}
sprintf ( testBuf , " %e " , ( double ) num ) ;
u_sprintf ( uStringBuf , " en_US_POSIX " , " %e " , ( double ) num ) ;
2002-03-26 20:10:52 +00:00
u_austrncpy ( myString , uStringBuf , sizeof ( myString ) / sizeof ( myString [ 0 ] ) ) ;
2002-02-23 00:34:39 +00:00
if ( strcmp ( myString , testBuf ) ! = 0 ) {
log_err ( " %%e Got: \" %s \" , Expected: \" %s \" \n " , myString , testBuf ) ;
}
sprintf ( testBuf , " %E " , ( double ) num ) ;
u_sprintf ( uStringBuf , " en_US_POSIX " , " %E " , ( double ) num ) ;
2002-03-26 20:10:52 +00:00
u_austrncpy ( myString , uStringBuf , sizeof ( myString ) / sizeof ( myString [ 0 ] ) ) ;
2002-02-23 00:34:39 +00:00
if ( strcmp ( myString , testBuf ) ! = 0 ) {
log_err ( " %%E Got: \" %s \" , Expected: \" %s \" \n " , myString , testBuf ) ;
}
sprintf ( testBuf , " %g " , ( double ) num ) ;
u_sprintf ( uStringBuf , " en_US_POSIX " , " %g " , ( double ) num ) ;
2002-03-26 20:10:52 +00:00
u_austrncpy ( myString , uStringBuf , sizeof ( myString ) / sizeof ( myString [ 0 ] ) ) ;
2002-02-23 00:34:39 +00:00
if ( strcmp ( myString , testBuf ) ! = 0 ) {
log_err ( " %%g Got: \" %s \" , Expected: \" %s \" \n " , myString , testBuf ) ;
}
sprintf ( testBuf , " %G " , ( double ) num ) ;
u_sprintf ( uStringBuf , " en_US_POSIX " , " %G " , ( double ) num ) ;
2002-03-26 20:10:52 +00:00
u_austrncpy ( myString , uStringBuf , sizeof ( myString ) / sizeof ( myString [ 0 ] ) ) ;
2002-02-23 00:34:39 +00:00
if ( strcmp ( myString , testBuf ) ! = 0 ) {
log_err ( " %%G Got: \" %s \" , Expected: \" %s \" \n " , myString , testBuf ) ;
}
}
for ( num = 0 ; num < 0x80 ; num + + ) {
2002-04-11 00:10:51 +00:00
testBuf [ 0 ] = ( char ) 0xFF ;
uStringBuf [ 0 ] = ( UChar ) 0xfffe ;
2002-02-23 00:34:39 +00:00
sprintf ( testBuf , " %c " , num ) ;
u_sprintf ( uStringBuf , NULL , " %c " , num ) ;
2002-03-26 20:10:52 +00:00
u_austrncpy ( myString , uStringBuf , sizeof ( myString ) / sizeof ( myString [ 0 ] ) ) ;
2002-02-23 00:34:39 +00:00
if ( testBuf [ 0 ] ! = uStringBuf [ 0 ] | | uStringBuf [ 0 ] ! = num ) {
log_err ( " %%c Got: 0x%x, Expected: 0x%x \n " , myString [ 0 ] , testBuf [ 0 ] ) ;
}
}
}
2004-03-16 19:02:00 +00:00
static void TestSScanSetFormat ( const char * format , const UChar * uValue , const char * cValue , UBool expectedToPass ) {
2002-08-26 20:53:53 +00:00
UChar uBuffer [ 256 ] ;
char buffer [ 256 ] ;
char compBuffer [ 256 ] ;
int32_t uNumScanned ;
int32_t cNumScanned ;
2004-03-16 19:02:00 +00:00
/* Reinitialize the buffer to verify null termination works. */
u_memset ( uBuffer , 0x2a , sizeof ( uBuffer ) / sizeof ( * uBuffer ) ) ;
uBuffer [ sizeof ( uBuffer ) / sizeof ( * uBuffer ) - 1 ] = 0 ;
memset ( buffer , 0x2a , sizeof ( buffer ) / sizeof ( * buffer ) ) ;
buffer [ sizeof ( buffer ) / sizeof ( * buffer ) - 1 ] = 0 ;
uNumScanned = u_sscanf ( uValue , NULL , format , uBuffer ) ;
if ( expectedToPass ) {
u_austrncpy ( compBuffer , uBuffer , sizeof ( uBuffer ) / sizeof ( uBuffer [ 0 ] ) ) ;
cNumScanned = sscanf ( cValue , format , buffer ) ;
if ( strncmp ( buffer , compBuffer , sizeof ( uBuffer ) / sizeof ( uBuffer [ 0 ] ) ) ! = 0 ) {
log_err ( " %s Got: \" %s \" , Expected: \" %s \" \n " , format , compBuffer , buffer ) ;
}
if ( cNumScanned ! = uNumScanned ) {
log_err ( " %s number scanned Got: %d, Expected: %d \n " , format , uNumScanned , cNumScanned ) ;
}
if ( uNumScanned > 0 & & uBuffer [ u_strlen ( uBuffer ) + 1 ] ! = 0x2a ) {
log_err ( " %s too much stored \n " , format ) ;
}
}
else {
if ( uNumScanned ! = 0 | | uBuffer [ 0 ] ! = 0x2a | | uBuffer [ 1 ] ! = 0x2a ) {
log_err ( " %s too much stored on a failure \n " , format ) ;
}
}
}
2002-08-26 20:53:53 +00:00
2004-03-16 19:02:00 +00:00
static void TestSScanset ( ) {
static const UChar abcUChars [ ] = { 0x61 , 0x62 , 0x63 , 0x63 , 0x64 , 0x65 , 0x66 , 0x67 , 0 } ;
static const char abcChars [ ] = " abccdefg " ;
2002-08-26 20:53:53 +00:00
2004-03-16 19:02:00 +00:00
TestSScanSetFormat ( " %[bc]S " , abcUChars , abcChars , TRUE ) ;
TestSScanSetFormat ( " %[cb]S " , abcUChars , abcChars , TRUE ) ;
2002-08-26 20:53:53 +00:00
2004-03-16 19:02:00 +00:00
TestSScanSetFormat ( " %[ab]S " , abcUChars , abcChars , TRUE ) ;
TestSScanSetFormat ( " %[ba]S " , abcUChars , abcChars , TRUE ) ;
2002-08-26 20:53:53 +00:00
2004-03-16 19:02:00 +00:00
TestSScanSetFormat ( " %[ab] " , abcUChars , abcChars , TRUE ) ;
TestSScanSetFormat ( " %[ba] " , abcUChars , abcChars , TRUE ) ;
2002-08-26 20:53:53 +00:00
2004-03-16 19:02:00 +00:00
TestSScanSetFormat ( " %[abcdefgh] " , abcUChars , abcChars , TRUE ) ;
TestSScanSetFormat ( " %[;hgfedcba] " , abcUChars , abcChars , TRUE ) ;
2002-08-26 20:53:53 +00:00
2004-03-16 19:02:00 +00:00
TestSScanSetFormat ( " %[a-f] " , abcUChars , abcChars , TRUE ) ;
TestSScanSetFormat ( " %[a-c] " , abcUChars , abcChars , TRUE ) ;
2002-08-26 20:53:53 +00:00
2004-03-16 19:02:00 +00:00
TestSScanSetFormat ( " %[^e-f] " , abcUChars , abcChars , TRUE ) ;
TestSScanSetFormat ( " %[^a] " , abcUChars , abcChars , TRUE ) ;
TestSScanSetFormat ( " %[^e] " , abcUChars , abcChars , TRUE ) ;
TestSScanSetFormat ( " %[^ed] " , abcUChars , abcChars , TRUE ) ;
TestSScanSetFormat ( " %[^dc] " , abcUChars , abcChars , TRUE ) ;
TestSScanSetFormat ( " %[^e] " , abcUChars , abcChars , TRUE ) ;
TestSScanSetFormat ( " %[] " , abcUChars , abcChars , TRUE ) ;
TestSScanSetFormat ( " %1[ab] " , abcUChars , abcChars , TRUE ) ;
TestSScanSetFormat ( " %2[^f] " , abcUChars , abcChars , TRUE ) ;
TestSScanSetFormat ( " %[a-] " , abcUChars , abcChars , TRUE ) ;
2002-08-26 20:53:53 +00:00
/* Bad format */
2004-03-16 19:02:00 +00:00
TestSScanSetFormat ( " %[a " , abcUChars , abcChars , FALSE ) ;
TestSScanSetFormat ( " %[f-a] " , abcUChars , abcChars , FALSE ) ;
TestSScanSetFormat ( " %[c-a] " , abcUChars , abcChars , FALSE ) ;
2002-08-26 20:53:53 +00:00
/* The following is not deterministic on Windows */
/* TestSScanSetFormat("%[a-", abcUChars, abcChars);*/
/* TODO: Need to specify precision with a "*" */
}
2004-03-16 19:02:00 +00:00
static void TestFScanSetFormat ( const char * format , const UChar * uValue , const char * cValue , UBool expectedToPass ) {
2002-08-26 20:53:53 +00:00
UFILE * myFile ;
UChar uBuffer [ 256 ] ;
char buffer [ 256 ] ;
char compBuffer [ 256 ] ;
int32_t uNumScanned ;
int32_t cNumScanned ;
2004-03-16 19:02:00 +00:00
myFile = u_fopen ( STANDARD_TEST_FILE , " w " , NULL , NULL ) ;
/* Reinitialize the buffer to verify null termination works. */
u_memset ( uBuffer , 0x2a , sizeof ( uBuffer ) / sizeof ( * uBuffer ) ) ;
uBuffer [ sizeof ( uBuffer ) / sizeof ( * uBuffer ) - 1 ] = 0 ;
memset ( buffer , 0x2a , sizeof ( buffer ) / sizeof ( * buffer ) ) ;
buffer [ sizeof ( buffer ) / sizeof ( * buffer ) - 1 ] = 0 ;
u_fprintf ( myFile , " %S " , uValue ) ;
u_fclose ( myFile ) ;
myFile = u_fopen ( STANDARD_TEST_FILE , " r " , " en_US_POSIX " , NULL ) ;
uNumScanned = u_fscanf ( myFile , format , uBuffer ) ;
u_fclose ( myFile ) ;
if ( expectedToPass ) {
u_austrncpy ( compBuffer , uBuffer , sizeof ( uBuffer ) / sizeof ( * uBuffer ) ) ;
cNumScanned = sscanf ( cValue , format , buffer ) ;
if ( strncmp ( buffer , compBuffer , sizeof ( uBuffer ) / sizeof ( * uBuffer ) ) ! = 0 ) {
log_err ( " %s Got: \" %s \" , Expected: \" %s \" \n " , format , compBuffer , buffer ) ;
}
if ( cNumScanned ! = uNumScanned ) {
log_err ( " %s number printed Got: %d, Expected: %d \n " , format , uNumScanned , cNumScanned ) ;
}
if ( uNumScanned > 0 & & uBuffer [ u_strlen ( uBuffer ) + 1 ] ! = 0x2a ) {
log_err ( " %s too much stored \n " , format ) ;
}
}
else {
if ( uNumScanned ! = 0 | | uBuffer [ 0 ] ! = 0x2a | | uBuffer [ 1 ] ! = 0x2a ) {
log_err ( " %s too much stored on a failure \n " , format ) ;
}
}
}
static void TestFScanset ( ) {
static const UChar abcUChars [ ] = { 0x61 , 0x62 , 0x63 , 0x63 , 0x64 , 0x65 , 0x66 , 0x67 , 0 } ;
static const char abcChars [ ] = " abccdefg " ;
TestFScanSetFormat ( " %[bc]S " , abcUChars , abcChars , TRUE ) ;
TestFScanSetFormat ( " %[cb]S " , abcUChars , abcChars , TRUE ) ;
2002-08-26 20:53:53 +00:00
2004-03-16 19:02:00 +00:00
TestFScanSetFormat ( " %[ab]S " , abcUChars , abcChars , TRUE ) ;
TestFScanSetFormat ( " %[ba]S " , abcUChars , abcChars , TRUE ) ;
2002-08-26 20:53:53 +00:00
2004-03-16 19:02:00 +00:00
TestFScanSetFormat ( " %[ab] " , abcUChars , abcChars , TRUE ) ;
TestFScanSetFormat ( " %[ba] " , abcUChars , abcChars , TRUE ) ;
2002-08-26 20:53:53 +00:00
2004-03-16 19:02:00 +00:00
TestFScanSetFormat ( " %[abcdefgh] " , abcUChars , abcChars , TRUE ) ;
TestFScanSetFormat ( " %[;hgfedcba] " , abcUChars , abcChars , TRUE ) ;
2002-08-26 20:53:53 +00:00
2004-03-16 19:02:00 +00:00
TestFScanSetFormat ( " %[a-f] " , abcUChars , abcChars , TRUE ) ;
TestFScanSetFormat ( " %[a-c] " , abcUChars , abcChars , TRUE ) ;
2002-08-26 20:53:53 +00:00
2004-03-16 19:02:00 +00:00
TestFScanSetFormat ( " %[^e-f] " , abcUChars , abcChars , TRUE ) ;
2002-08-26 20:53:53 +00:00
2004-03-16 19:02:00 +00:00
TestFScanSetFormat ( " %[^a] " , abcUChars , abcChars , TRUE ) ;
TestFScanSetFormat ( " %[^e] " , abcUChars , abcChars , TRUE ) ;
TestFScanSetFormat ( " %[^ed] " , abcUChars , abcChars , TRUE ) ;
TestFScanSetFormat ( " %[^dc] " , abcUChars , abcChars , TRUE ) ;
TestFScanSetFormat ( " %[^e] " , abcUChars , abcChars , TRUE ) ;
2002-08-26 20:53:53 +00:00
2004-03-16 19:02:00 +00:00
TestFScanSetFormat ( " %[] " , abcUChars , abcChars , TRUE ) ;
TestFScanSetFormat ( " %1[ab] " , abcUChars , abcChars , TRUE ) ;
TestFScanSetFormat ( " %2[^f] " , abcUChars , abcChars , TRUE ) ;
2002-08-26 20:53:53 +00:00
/* Bad format */
2004-03-16 19:02:00 +00:00
TestFScanSetFormat ( " %[f-a] " , abcUChars , abcChars , FALSE ) ;
TestFScanSetFormat ( " %[c-a] " , abcUChars , abcChars , FALSE ) ;
TestFScanSetFormat ( " %[a " , abcUChars , abcChars , FALSE ) ;
2002-08-26 20:53:53 +00:00
/* The following is not deterministic on Windows */
/* TestFScanSetFormat("%[a-", abcUChars, abcChars);*/
/* TODO: Need to specify precision with a "*" */
}
2002-02-23 00:34:39 +00:00
static void TestStream ( ) {
# if U_IOSTREAM_SOURCE >= 198506
char testStreamBuf [ 512 ] ;
const char * testStr = " Beginning of test str1 <<432 1 " C_NEW_LINE " UTF-8 \xCE \xBC \xF0 \x90 \x80 \x81 \xF0 \x90 \x80 \x82 " ;
ostrstream outTestStream ( testStreamBuf , sizeof ( testStreamBuf ) ) ;
2002-02-26 21:23:21 +00:00
istrstream inTestStream ( " tHis \xCE \xBC \xE2 \x80 \x82 mu world " , 0 ) ;
const UChar thisMu [ ] = { 0x74 , 0x48 , 0x69 , 0x73 , 0x3BC , 0 } ;
const UChar mu [ ] = { 0x6D , 0x75 , 0 } ;
2002-02-23 00:34:39 +00:00
UnicodeString str1 = UNICODE_STRING_SIMPLE ( " str1 " ) ;
UnicodeString str2 = UNICODE_STRING_SIMPLE ( " << " ) ;
UnicodeString str3 = UNICODE_STRING_SIMPLE ( " 4 " ) ;
UnicodeString str4 = UNICODE_STRING_SIMPLE ( " UTF-8 " ) ;
UnicodeString inStr = UNICODE_STRING_SIMPLE ( " UTF-8 " ) ;
2002-02-26 21:23:21 +00:00
UnicodeString inStr2 ;
2002-03-26 20:10:52 +00:00
char defConvName [ UCNV_MAX_CONVERTER_NAME_LENGTH * 2 ] ;
2002-02-23 00:34:39 +00:00
char inStrC [ 128 ] ;
UErrorCode status = U_ZERO_ERROR ;
UConverter * defConv ;
str4 . append ( ( UChar32 ) 0x03BC ) ; /* mu */
str4 . append ( ( UChar32 ) 0x10001 ) ;
str4 . append ( ( UChar32 ) 0x10002 ) ;
/* release the default converter and use utf-8 for a bit */
defConv = u_getDefaultConverter ( & status ) ;
if ( U_FAILURE ( status ) ) {
log_err ( " Can't get default converter " ) ;
return ;
}
ucnv_close ( defConv ) ;
2002-03-26 20:10:52 +00:00
strncpy ( defConvName , ucnv_getDefaultName ( ) , sizeof ( defConvName ) / sizeof ( defConvName [ 0 ] ) ) ;
2002-02-23 00:34:39 +00:00
ucnv_setDefaultName ( " UTF-8 " ) ;
outTestStream < < " Beginning of test " ;
2002-03-15 00:29:57 +00:00
outTestStream < < str1 < < " " < < str2 < < str3 < < 3 < < " 2 " < < 1.0 < < C_NEW_LINE < < str4 < < ends ;
2002-02-23 00:34:39 +00:00
if ( strcmp ( testStreamBuf , testStr ) ! = 0 ) {
log_err ( " Got: \" %s \" , Expected: \" %s \" \n " , testStreamBuf , testStr ) ;
}
2002-08-04 07:41:48 +00:00
2002-02-26 21:23:21 +00:00
inTestStream > > inStr > > inStr2 ;
2002-02-23 00:34:39 +00:00
if ( inStr . compare ( thisMu ) ! = 0 ) {
u_austrncpy ( inStrC , inStr . getBuffer ( ) , inStr . length ( ) ) ;
inStrC [ inStr . length ( ) ] = 0 ;
log_err ( " Got: \" %s \" , Expected: \" tHis \\ u03BC \" \n " , inStrC ) ;
}
2002-02-26 21:23:21 +00:00
if ( inStr2 . compare ( mu ) ! = 0 ) {
u_austrncpy ( inStrC , inStr . getBuffer ( ) , inStr . length ( ) ) ;
inStrC [ inStr . length ( ) ] = 0 ;
log_err ( " Got: \" %s \" , Expected: \" mu \" \n " , inStrC ) ;
}
2002-02-23 00:34:39 +00:00
/* return the default converter to the original state. */
ucnv_setDefaultName ( defConvName ) ;
defConv = u_getDefaultConverter ( & status ) ;
if ( U_FAILURE ( status ) ) {
log_err ( " Can't get default converter " ) ;
return ;
}
ucnv_close ( defConv ) ;
# else
2004-03-16 16:59:04 +00:00
log_info ( " U_IOSTREAM_SOURCE is disabled \n " ) ;
2002-02-23 00:34:39 +00:00
# endif
}
2002-04-10 23:32:18 +00:00
static void TestTranslitOps ( )
{
2002-04-11 00:10:51 +00:00
UFILE * f ;
UErrorCode err = U_ZERO_ERROR ;
UTransliterator * a = NULL , * b = NULL , * c = NULL ;
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
log_verbose ( " opening a transliterator and UFILE for testing \n " ) ;
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
f = u_fopen ( STANDARD_TEST_FILE , " w " , " en_US_POSIX " , NULL ) ;
if ( f = = NULL )
{
log_err ( " Couldn't open test file for writing " ) ;
return ;
}
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
a = utrans_open ( " Latin-Greek " , UTRANS_FORWARD , NULL , - 1 , NULL , & err ) ;
if ( U_FAILURE ( err ) )
{
log_err ( " Err opening transliterator %s \n " , u_errorName ( err ) ) ;
u_fclose ( f ) ;
return ;
}
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
log_verbose ( " setting a transliterator \n " ) ;
b = u_fsettransliterator ( f , U_WRITE , a , & err ) ;
2002-08-04 07:41:48 +00:00
if ( U_FAILURE ( err ) )
2002-04-11 00:10:51 +00:00
{
log_err ( " Err setting transliterator %s \n " , u_errorName ( err ) ) ;
u_fclose ( f ) ;
return ;
}
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
if ( b ! = NULL )
{
log_err ( " Err, a transliterator was already set! \n " ) ;
}
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
log_verbose ( " un-setting transliterator (setting to null) \n " ) ;
c = u_fsettransliterator ( f , U_WRITE , NULL , & err ) ;
2002-08-04 07:41:48 +00:00
if ( U_FAILURE ( err ) )
2002-04-11 00:10:51 +00:00
{
log_err ( " Err setting transliterator %s \n " , u_errorName ( err ) ) ;
u_fclose ( f ) ;
return ;
}
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
if ( c ! = a )
{
log_err ( " Err, transliterator that came back was not the original one. \n " ) ;
}
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
log_verbose ( " Trying to set read transliterator (should fail) \n " ) ;
b = u_fsettransliterator ( f , U_READ , NULL , & err ) ;
if ( err ! = U_UNSUPPORTED_ERROR )
{
log_err ( " Should have U_UNSUPPORTED_ERROR setting Read transliterator but got %s - REVISIT AND UPDATE TEST \n " , u_errorName ( err ) ) ;
u_fclose ( f ) ;
return ;
}
else
{
log_verbose ( " Got %s error (expected) setting READ transliterator. \n " , u_errorName ( err ) ) ;
err = U_ZERO_ERROR ;
}
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
utrans_close ( c ) ;
u_fclose ( f ) ;
2002-04-10 23:32:18 +00:00
}
static void TestTranslitOut ( )
{
2002-04-11 00:10:51 +00:00
UFILE * f ;
UErrorCode err = U_ZERO_ERROR ;
UTransliterator * a = NULL , * b = NULL , * c = NULL ;
FILE * infile ;
UChar compare [ ] = { 0xfeff , 0x03a3 , 0x03c4 , 0x03b5 , 0x03c6 , 0x1f00 , 0x03bd , 0x03bf , 0x03c2 , 0x0000 } ;
UChar ubuf [ 256 ] ;
int len ;
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
log_verbose ( " opening a transliterator and UFILE for testing \n " ) ;
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
f = u_fopen ( STANDARD_TEST_FILE , " w " , " en_US_POSIX " , " utf-16 " ) ;
if ( f = = NULL )
{
log_err ( " Couldn't open test file for writing " ) ;
return ;
}
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
a = utrans_open ( " Latin-Greek " , UTRANS_FORWARD , NULL , - 1 , NULL , & err ) ;
if ( U_FAILURE ( err ) )
{
log_err ( " Err opening transliterator %s \n " , u_errorName ( err ) ) ;
u_fclose ( f ) ;
return ;
}
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
log_verbose ( " setting a transliterator \n " ) ;
b = u_fsettransliterator ( f , U_WRITE , a , & err ) ;
2002-08-04 07:41:48 +00:00
if ( U_FAILURE ( err ) )
2002-04-11 00:10:51 +00:00
{
log_err ( " Err setting transliterator %s \n " , u_errorName ( err ) ) ;
u_fclose ( f ) ;
return ;
}
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
if ( b ! = NULL )
{
log_err ( " Err, a transliterator was already set! \n " ) ;
}
2002-08-04 07:41:48 +00:00
u_fprintf ( f , " Stephanos " ) ;
2002-04-11 00:10:51 +00:00
u_fclose ( f ) ;
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
log_verbose ( " Re reading test file to verify transliteration \n " ) ;
infile = fopen ( STANDARD_TEST_FILE , " rb " ) ;
if ( infile = = NULL )
{
log_err ( " Couldn't reopen test file \n " ) ;
return ;
}
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
len = fread ( ubuf , sizeof ( UChar ) , u_strlen ( compare ) , infile ) ;
log_verbose ( " Read %d UChars \n " , len ) ;
if ( len ! = u_strlen ( compare ) )
{
log_err ( " Wanted %d UChars from file, got %d \n " , u_strlen ( compare ) , len ) ;
}
ubuf [ len ] = 0 ;
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
if ( u_strlen ( compare ) ! = u_strlen ( ubuf ) )
{
log_err ( " Wanted %d UChars from file, but u_strlen() returns %d \n " , u_strlen ( compare ) , len ) ;
}
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
if ( u_strcmp ( compare , ubuf ) )
{
log_err ( " Read string doesn't match expected. \n " ) ;
}
else
{
log_verbose ( " Read string matches expected. \n " ) ;
}
2002-08-04 07:41:48 +00:00
2002-04-11 00:10:51 +00:00
fclose ( infile ) ;
2002-08-04 07:41:48 +00:00
2002-04-10 23:32:18 +00:00
}
2002-02-23 00:34:39 +00:00
static void addAllTests ( TestNode * * root ) {
2002-03-27 19:19:52 +00:00
addTest ( root , & TestFile , " file/TestFile " ) ;
2002-08-24 00:00:31 +00:00
addTest ( root , & TestCodepageAndLocale , " file/TestCodepageAndLocale " ) ;
2002-08-04 06:47:44 +00:00
addTest ( root , & TestfgetsBuffers , " file/TestfgetsBuffers " ) ;
2002-08-23 20:58:40 +00:00
addTest ( root , & TestfgetsLineCount , " file/TestfgetsLineCount " ) ;
2003-09-22 23:07:05 +00:00
addTest ( root , & TestfgetsNewLineHandling , " file/TestfgetsNewLineHandling " ) ;
2002-03-27 19:19:52 +00:00
addTest ( root , & TestFprintfFormat , " file/TestFprintfFormat " ) ;
2004-03-16 19:02:00 +00:00
addTest ( root , & TestFScanset , " file/TestFScanset " ) ;
2003-10-11 02:16:12 +00:00
addTest ( root , & TestCodepage , " file/TestCodepage " ) ;
2002-03-27 19:19:52 +00:00
addTest ( root , & TestFilePrintCompatibility , " file/TestFilePrintCompatibility " ) ;
2002-08-04 06:47:44 +00:00
2002-03-27 19:19:52 +00:00
addTest ( root , & TestString , " string/TestString " ) ;
addTest ( root , & TestSprintfFormat , " string/TestSprintfFormat " ) ;
addTest ( root , & TestSnprintf , " string/TestSnprintf " ) ;
2004-03-16 19:02:00 +00:00
addTest ( root , & TestSScanset , " string/TestSScanset " ) ;
2002-03-27 19:19:52 +00:00
addTest ( root , & TestStringCompatibility , " string/TestStringCompatibility " ) ;
addTest ( root , & TestStream , " stream/TestStream " ) ;
2002-04-10 23:32:18 +00:00
addTest ( root , & TestTranslitOps , " translit/ops " ) ;
addTest ( root , & TestTranslitOut , " translit/out " ) ;
2002-02-23 00:34:39 +00:00
}
int main ( int argc , char * argv [ ] )
{
int32_t nerrors = 0 ;
TestNode * root = NULL ;
addAllTests ( & root ) ;
nerrors = processArgs ( root , argc , argv ) ;
2002-03-26 05:28:54 +00:00
cleanUpTestTree ( root ) ;
u_cleanup ( ) ;
2002-02-23 00:34:39 +00:00
return nerrors ;
}