2000-08-10 01:33:48 +00:00
/********************************************************************
* COPYRIGHT :
2006-07-24 21:14:00 +00:00
* Copyright ( c ) 2000 - 2006 , International Business Machines Corporation and
2000-08-10 01:33:48 +00:00
* others . All Rights Reserved .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* File stdnmtst . c
*
* Modification History :
*
* Date Name Description
* 08 / 05 / 2000 Yves Creation
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
# include "unicode/ucnv.h"
2002-08-02 23:01:10 +00:00
# include "unicode/ustring.h"
2000-08-10 01:33:48 +00:00
# include "cstring.h"
# include "cintltst.h"
2002-07-13 16:32:16 +00:00
# define ARRAY_SIZE(array) (int32_t)(sizeof array / sizeof array[0])
static void TestStandardName ( void ) ;
2001-01-31 23:43:27 +00:00
static void TestStandardNames ( void ) ;
2002-10-30 22:52:08 +00:00
static void TestCanonicalName ( void ) ;
2000-08-10 01:33:48 +00:00
2001-05-31 23:07:40 +00:00
void addStandardNamesTest ( TestNode * * root ) ;
2000-08-10 01:33:48 +00:00
void
addStandardNamesTest ( TestNode * * root )
{
2003-12-04 01:20:54 +00:00
addTest ( root , & TestStandardName , " tsconv/stdnmtst/TestStandardName " ) ;
addTest ( root , & TestStandardNames , " tsconv/stdnmtst/TestStandardNames " ) ;
addTest ( root , & TestCanonicalName , " tsconv/stdnmtst/TestCanonicalName " ) ;
2000-08-10 01:33:48 +00:00
}
2000-08-11 04:28:42 +00:00
static int dotestname ( const char * name , const char * standard , const char * expected ) {
int res = 1 ;
2000-08-10 01:33:48 +00:00
UErrorCode error ;
const char * tag ;
error = U_ZERO_ERROR ;
tag = ucnv_getStandardName ( name , standard , & error ) ;
2002-07-24 23:09:33 +00:00
if ( ! tag & & expected ) {
2000-08-10 01:33:48 +00:00
log_err ( " FAIL: could not find %s standard name for %s \n " , standard , name ) ;
2000-08-11 19:45:02 +00:00
res = 0 ;
2002-08-02 23:28:03 +00:00
} else if ( expected & & ( name = = tag | | uprv_strcmp ( expected , tag ) ) ) {
2000-08-10 01:33:48 +00:00
log_err ( " FAIL: expected %s for %s standard name for %s, got %s \n " , expected , standard , name , tag ) ;
2000-08-11 19:45:02 +00:00
res = 0 ;
2000-08-10 01:33:48 +00:00
}
2000-08-11 04:28:42 +00:00
return res ;
2000-08-10 01:33:48 +00:00
}
2002-07-13 16:32:16 +00:00
static void TestStandardName ( )
2000-08-10 01:33:48 +00:00
{
2000-08-11 04:28:42 +00:00
int res = 1 ;
2000-08-11 19:45:02 +00:00
uint16_t i , count ;
2000-08-10 21:45:09 +00:00
UErrorCode err ;
/* Iterate over all standards. */
2002-10-30 22:52:08 +00:00
for ( i = 0 , count = ucnv_countStandards ( ) ; i < count - 1 ; + + i ) {
2002-03-22 02:26:29 +00:00
const char * standard ;
2000-08-10 21:45:09 +00:00
err = U_ZERO_ERROR ;
2002-03-22 02:26:29 +00:00
standard = ucnv_getStandard ( i , & err ) ;
2000-08-10 21:45:09 +00:00
if ( U_FAILURE ( err ) ) {
log_err ( " FAIL: ucnv_getStandard(%d), error=%s \n " , i , u_errorName ( err ) ) ;
2000-08-11 04:28:42 +00:00
res = 0 ;
2002-03-22 02:26:29 +00:00
} else if ( ! standard | | ! * standard ) {
log_err ( " FAIL: %s standard name at index %d \n " , ( standard ? " empty " :
2000-08-10 21:45:09 +00:00
" null " ) , i ) ;
2000-08-11 04:28:42 +00:00
res = 0 ;
2000-08-10 21:45:09 +00:00
}
}
err = U_ZERO_ERROR ;
2002-10-30 22:52:08 +00:00
/* "" must be last */
2003-06-04 06:53:23 +00:00
if ( ! count ) {
log_data_err ( " No standards. You probably have no data. \n " ) ;
} else if ( * ucnv_getStandard ( ( uint16_t ) ( count - 1 ) , & err ) ! = 0 ) {
2002-10-30 22:52:08 +00:00
log_err ( " FAIL: ucnv_getStandard(%d) should return " " \n " , count - 1 ) ;
res = 0 ;
}
err = U_ZERO_ERROR ;
if ( ucnv_getStandard ( + + i , & err ) ) {
2000-08-10 21:45:09 +00:00
log_err ( " FAIL: ucnv_getStandard(%d) should return NULL \n " , i ) ;
2000-08-11 04:28:42 +00:00
res = 0 ;
2000-08-10 21:45:09 +00:00
}
2000-08-11 04:28:42 +00:00
if ( res ) {
log_verbose ( " PASS: iterating over standard names works \n " ) ;
}
2000-08-10 21:45:09 +00:00
/* Test for some expected results. */
2002-08-02 23:28:03 +00:00
if ( dotestname ( " ibm-1208 " , " MIME " , " UTF-8 " ) & &
2001-12-03 21:24:15 +00:00
/*dotestname("cp1252", "MIME", "windows-1252") &&*/
2002-08-02 23:28:03 +00:00
dotestname ( " ascii " , " MIME " , " US-ASCII " ) & &
2003-12-04 01:20:54 +00:00
dotestname ( " csiso2022jp2 " , " MIME " , " ISO-2022-JP-2 " ) & &
2006-07-24 21:14:00 +00:00
dotestname ( " Iso20-22__cN " , " IANA " , " ISO-2022-CN " ) & &
2000-08-11 04:28:42 +00:00
dotestname ( " ascii " , " IANA " , " ANSI_X3.4-1968 " ) & &
2002-07-24 23:09:33 +00:00
dotestname ( " cp850 " , " IANA " , " IBM850 " ) & &
dotestname ( " crazy " , " MIME " , NULL ) & &
dotestname ( " ASCII " , " crazy " , NULL ) & &
dotestname ( " LMBCS-1 " , " MIME " , NULL ) )
2002-07-13 16:32:16 +00:00
{
2002-07-24 23:09:33 +00:00
log_verbose ( " PASS: getting IANA and MIME standard names works \n " ) ;
2000-08-11 04:28:42 +00:00
}
2000-08-10 01:33:48 +00:00
}
2002-10-30 22:52:08 +00:00
static int dotestconv ( const char * name , const char * standard , const char * expected ) {
int res = 1 ;
UErrorCode error ;
const char * tag ;
error = U_ZERO_ERROR ;
tag = ucnv_getCanonicalName ( name , standard , & error ) ;
if ( tag & & ! expected ) {
log_err ( " FAIL: Unexpectedly found %s canonical name for %s, got %s \n " , standard , name , tag ) ;
res = 0 ;
2005-04-01 22:58:35 +00:00
}
else if ( ! tag & & expected ) {
2002-10-30 22:52:08 +00:00
log_err ( " FAIL: could not find %s canonical name for %s \n " , ( standard ? " \" \" " : standard ) , name ) ;
res = 0 ;
2005-04-01 22:58:35 +00:00
}
else if ( expected & & ( name = = tag | | uprv_strcmp ( expected , tag ) ! = 0 ) ) {
2002-10-30 22:52:08 +00:00
log_err ( " FAIL: expected %s for %s canonical name for %s, got %s \n " , expected , standard , name , tag ) ;
res = 0 ;
}
2005-04-01 22:58:35 +00:00
else {
log_verbose ( " PASS: ( \" %s \" , \" %s \" ) -> %s == %s \n " , name , standard , tag , expected ) ;
}
2002-10-30 22:52:08 +00:00
return res ;
}
static void TestCanonicalName ( )
{
/* Test for some expected results. */
if ( dotestconv ( " UTF-8 " , " IANA " , " UTF-8 " ) & & /* default name */
dotestconv ( " UTF-8 " , " MIME " , " UTF-8 " ) & & /* default name */
dotestconv ( " ibm-1208 " , " IBM " , " UTF-8 " ) & & /* default name */
dotestconv ( " ibm-5305 " , " IBM " , " UTF-8 " ) & & /* non-default name */
dotestconv ( " ibm-5305 " , " MIME " , NULL ) & & /* mapping does not exist */
dotestconv ( " ascii " , " MIME " , NULL ) & & /* mapping does not exist */
dotestconv ( " ibm-1208 " , " IANA " , NULL ) & & /* mapping does not exist */
dotestconv ( " ibm-5305 " , " IANA " , NULL ) & & /* mapping does not exist */
dotestconv ( " cp1208 " , " " , " UTF-8 " ) & & /* default name due to ordering */
2003-04-05 09:50:53 +00:00
dotestconv ( " UTF16_BigEndian " , " " , " UTF-16BE " ) & & /* non-default name due to ordering */
2006-07-24 21:14:00 +00:00
dotestconv ( " ISO-2022-CN " , " IANA " , " ISO_2022,locale=zh,version=0 " ) & & /* default name */
2005-04-01 22:58:35 +00:00
dotestconv ( " Shift_JIS " , " MIME " , " ibm-943_P15A-2003 " ) & & /* ambiguous alias */
2003-04-05 09:50:53 +00:00
dotestconv ( " Shift_JIS " , " " , " ibm-943_P130-1999 " ) & & /* ambiguous alias */
2005-04-01 22:58:35 +00:00
dotestconv ( " ibm-943 " , " " , " ibm-943_P15A-2003 " ) & & /* ambiguous alias */
2003-04-05 09:50:53 +00:00
dotestconv ( " ibm-943 " , " IBM " , " ibm-943_P130-1999 " ) & & /* ambiguous alias */
dotestconv ( " ibm-1363 " , " " , " ibm-1363_P11B-1998 " ) & & /* ambiguous alias */
dotestconv ( " ibm-1363 " , " IBM " , " ibm-1363_P110-1997 " ) & & /* ambiguous alias */
2002-10-30 22:52:08 +00:00
dotestconv ( " crazy " , " MIME " , NULL ) & &
dotestconv ( " ASCII " , " crazy " , NULL ) )
{
log_verbose ( " PASS: getting IANA and MIME canonical names works \n " ) ;
}
}
2002-07-13 16:32:16 +00:00
static UBool doTestNames ( const char * name , const char * standard , const char * * expected , int32_t size ) {
UErrorCode err = U_ZERO_ERROR ;
UEnumeration * myEnum = ucnv_openStandardNames ( name , standard , & err ) ;
2006-08-06 22:38:31 +00:00
const char * enumName , * testName ;
2002-07-13 16:32:16 +00:00
int32_t enumCount = uenum_count ( myEnum , & err ) ;
2006-08-06 22:38:31 +00:00
int32_t idx , len , repeatTimes = 3 ;
2002-07-13 16:32:16 +00:00
if ( size ! = enumCount ) {
log_err ( " FAIL: different size arrays. Got %d. Expected %d \n " , enumCount , size ) ;
return 0 ;
}
if ( size < 0 & & myEnum ) {
log_err ( " FAIL: size < 0, but recieved an actual object \n " ) ;
return 0 ;
}
log_verbose ( " \n %s %s \n " , name , standard ) ;
2002-07-22 21:05:17 +00:00
while ( repeatTimes - - > 0 ) {
for ( idx = 0 ; idx < enumCount ; idx + + ) {
2006-08-06 22:38:31 +00:00
enumName = uenum_next ( myEnum , & len , & err ) ;
testName = expected [ idx ] ;
2002-08-02 23:01:10 +00:00
if ( uprv_strcmp ( enumName , testName ) ! = 0 | | U_FAILURE ( err )
| | len ! = ( int32_t ) uprv_strlen ( expected [ idx ] ) )
{
log_err ( " FAIL: uenum_next(%d) == \" %s \" . expected \" %s \" , len=%d, error=%s \n " ,
idx , enumName , testName , len , u_errorName ( err ) ) ;
2002-07-22 21:05:17 +00:00
}
log_verbose ( " %s \n " , enumName ) ;
err = U_ZERO_ERROR ;
}
2006-08-07 23:58:13 +00:00
if ( enumCount > = 0 ) {
/* one past the list of all names must return NULL */
enumName = uenum_next ( myEnum , & len , & err ) ;
if ( enumName ! = NULL | | len ! = 0 | | U_FAILURE ( err ) ) {
log_err ( " FAIL: uenum_next(past the list) did not return NULL[0] with U_SUCCESS(). name=%s standard=%s len=%d err=%s \n " , name , standard , len , u_errorName ( err ) ) ;
}
2006-08-06 22:38:31 +00:00
}
2002-08-02 23:01:10 +00:00
log_verbose ( " \n reset \n " ) ;
uenum_reset ( myEnum , & err ) ;
if ( U_FAILURE ( err ) ) {
log_err ( " FAIL: uenum_reset() for %s{%s} failed with %s \n " ,
name , standard , u_errorName ( err ) ) ;
err = U_ZERO_ERROR ;
}
}
uenum_close ( myEnum ) ;
return 1 ;
}
static UBool doTestUCharNames ( const char * name , const char * standard , const char * * expected , int32_t size ) {
UErrorCode err = U_ZERO_ERROR ;
UEnumeration * myEnum = ucnv_openStandardNames ( name , standard , & err ) ;
int32_t enumCount = uenum_count ( myEnum , & err ) ;
int32_t idx , repeatTimes = 3 ;
if ( size ! = enumCount ) {
log_err ( " FAIL: different size arrays. Got %d. Expected %d \n " , enumCount , size ) ;
return 0 ;
}
if ( size < 0 & & myEnum ) {
log_err ( " FAIL: size < 0, but recieved an actual object \n " ) ;
return 0 ;
}
log_verbose ( " \n %s %s \n " , name , standard ) ;
while ( repeatTimes - - > 0 ) {
for ( idx = 0 ; idx < enumCount ; idx + + ) {
UChar testName [ 256 ] ;
int32_t len ;
const UChar * enumName = uenum_unext ( myEnum , & len , & err ) ;
u_uastrncpy ( testName , expected [ idx ] , sizeof ( testName ) / sizeof ( testName [ 0 ] ) ) ;
if ( u_strcmp ( enumName , testName ) ! = 0 | | U_FAILURE ( err )
| | len ! = ( int32_t ) uprv_strlen ( expected [ idx ] ) )
{
log_err ( " FAIL: uenum_next(%d) == \" %s \" . expected \" %s \" , len=%d, error=%s \n " ,
idx , enumName , testName , len , u_errorName ( err ) ) ;
}
log_verbose ( " %s \n " , expected [ idx ] ) ;
err = U_ZERO_ERROR ;
}
log_verbose ( " \n reset \n " ) ;
2002-07-22 21:05:17 +00:00
uenum_reset ( myEnum , & err ) ;
if ( U_FAILURE ( err ) ) {
log_err ( " FAIL: uenum_reset() for %s{%s} failed with %s \n " ,
name , standard , u_errorName ( err ) ) ;
err = U_ZERO_ERROR ;
2002-07-13 16:32:16 +00:00
}
}
uenum_close ( myEnum ) ;
return 1 ;
}
static void TestStandardNames ( )
{
static const char * asciiIANA [ ] = {
" ANSI_X3.4-1968 " ,
" US-ASCII " ,
" ASCII " ,
" ANSI_X3.4-1986 " ,
" ISO_646.irv:1991 " ,
" ISO646-US " ,
" us " ,
" csASCII " ,
" iso-ir-6 " ,
" cp367 " ,
} ;
static const char * asciiMIME [ ] = {
" US-ASCII "
} ;
2002-07-24 23:09:33 +00:00
2002-08-02 23:28:03 +00:00
static const char * iso2022MIME [ ] = {
2003-12-04 01:20:54 +00:00
" ISO-2022-KR " ,
2002-08-02 23:28:03 +00:00
} ;
2002-07-13 16:32:16 +00:00
doTestNames ( " ASCII " , " IANA " , asciiIANA , ARRAY_SIZE ( asciiIANA ) ) ;
doTestNames ( " US-ASCII " , " IANA " , asciiIANA , ARRAY_SIZE ( asciiIANA ) ) ;
doTestNames ( " ASCII " , " MIME " , asciiMIME , ARRAY_SIZE ( asciiMIME ) ) ;
doTestNames ( " ascii " , " mime " , asciiMIME , ARRAY_SIZE ( asciiMIME ) ) ;
doTestNames ( " ASCII " , " crazy " , asciiMIME , - 1 ) ;
doTestNames ( " crazy " , " MIME " , asciiMIME , - 1 ) ;
2002-07-24 23:09:33 +00:00
doTestNames ( " LMBCS-1 " , " MIME " , asciiMIME , 0 ) ;
2002-08-02 23:01:10 +00:00
2003-12-04 01:20:54 +00:00
doTestNames ( " ISO_2022,locale=ko,version=0 " , " MIME " , iso2022MIME , ARRAY_SIZE ( iso2022MIME ) ) ;
doTestNames ( " csiso2022kr " , " MIME " , iso2022MIME , ARRAY_SIZE ( iso2022MIME ) ) ;
2002-08-02 23:28:03 +00:00
2002-08-02 23:01:10 +00:00
log_verbose ( " Testing unext() \n " ) ;
doTestUCharNames ( " ASCII " , " IANA " , asciiIANA , ARRAY_SIZE ( asciiIANA ) ) ;
2002-07-13 16:32:16 +00:00
}