]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/stdnmtst.c
1 /********************************************************************
3 * Copyright (c) 2000-2009, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
9 * Modification History:
11 * Date Name Description
12 * 08/05/2000 Yves Creation
13 ******************************************************************************
16 #include "unicode/ucnv.h"
17 #include "unicode/ustring.h"
21 #define ARRAY_SIZE(array) (int32_t)(sizeof array / sizeof array[0])
23 static void TestStandardName ( void );
24 static void TestStandardNames ( void );
25 static void TestCanonicalName ( void );
27 void addStandardNamesTest ( TestNode
** root
);
31 addStandardNamesTest ( TestNode
** root
)
33 addTest ( root
, & TestStandardName
, "tsconv/stdnmtst/TestStandardName" );
34 addTest ( root
, & TestStandardNames
, "tsconv/stdnmtst/TestStandardNames" );
35 addTest ( root
, & TestCanonicalName
, "tsconv/stdnmtst/TestCanonicalName" );
38 static int dotestname ( const char * name
, const char * standard
, const char * expected
) {
45 tag
= ucnv_getStandardName ( name
, standard
, & error
);
46 if (! tag
&& expected
) {
47 log_err_status ( error
, "FAIL: could not find %s standard name for %s \n " , standard
, name
);
49 } else if ( expected
&& ( name
== tag
|| uprv_strcmp ( expected
, tag
))) {
50 log_err ( "FAIL: expected %s for %s standard name for %s , got %s \n " , expected
, standard
, name
, tag
);
57 static void TestStandardName ()
64 /* Iterate over all standards. */
65 for ( i
= 0 , count
= ucnv_countStandards (); i
< count
- 1 ; ++ i
) {
69 standard
= ucnv_getStandard ( i
, & err
);
71 log_err ( "FAIL: ucnv_getStandard( %d ), error= %s \n " , i
, u_errorName ( err
));
73 } else if (! standard
|| !* standard
) {
74 log_err ( "FAIL: %s standard name at index %d \n " , ( standard
? "empty" :
82 log_data_err ( "No standards. You probably have no data. \n " );
83 } else if (* ucnv_getStandard (( uint16_t )( count
- 1 ), & err
) != 0 ) {
84 log_err ( "FAIL: ucnv_getStandard( %d ) should return " " \n " , count
- 1 );
88 if ( ucnv_getStandard (++ i
, & err
)) {
89 log_err ( "FAIL: ucnv_getStandard( %d ) should return NULL \n " , i
);
94 log_verbose ( "PASS: iterating over standard names works \n " );
97 /* Test for some expected results. */
99 if ( dotestname ( "ibm-1208" , "MIME" , "UTF-8" ) &&
100 /*dotestname("cp1252", "MIME", "windows-1252") &&*/
101 dotestname ( "ascii" , "MIME" , "US-ASCII" ) &&
102 dotestname ( "csiso2022jp2" , "MIME" , "ISO-2022-JP-2" ) &&
103 dotestname ( "Iso20-22__cN" , "IANA" , "ISO-2022-CN" ) &&
104 dotestname ( "ascii" , "IANA" , "ANSI_X3.4-1968" ) &&
105 dotestname ( "cp850" , "IANA" , "IBM850" ) &&
106 dotestname ( "crazy" , "MIME" , NULL
) &&
107 dotestname ( "ASCII" , "crazy" , NULL
) &&
108 dotestname ( "LMBCS-1" , "MIME" , NULL
))
110 log_verbose ( "PASS: getting IANA and MIME standard names works \n " );
114 static int dotestconv ( const char * name
, const char * standard
, const char * expected
) {
120 error
= U_ZERO_ERROR
;
121 tag
= ucnv_getCanonicalName ( name
, standard
, & error
);
122 if ( tag
&& ! expected
) {
123 log_err ( "FAIL: Unexpectedly found %s canonical name for %s , got %s \n " , standard
, name
, tag
);
126 else if (! tag
&& expected
) {
127 log_err_status ( error
, "FAIL: could not find %s canonical name for %s \n " , ( standard
? " \"\" " : standard
), name
);
130 else if ( expected
&& ( name
== tag
|| uprv_strcmp ( expected
, tag
) != 0 )) {
131 log_err ( "FAIL: expected %s for %s canonical name for %s , got %s \n " , expected
, standard
, name
, tag
);
135 log_verbose ( "PASS: ( \" %s \" , \" %s \" ) -> %s == %s \n " , name
, standard
, tag
, expected
);
141 static void TestCanonicalName ()
143 /* Test for some expected results. */
145 if ( dotestconv ( "UTF-8" , "IANA" , "UTF-8" ) && /* default name */
146 dotestconv ( "UTF-8" , "MIME" , "UTF-8" ) && /* default name */
147 dotestconv ( "ibm-1208" , "IBM" , "UTF-8" ) && /* default name */
148 dotestconv ( "ibm-5305" , "IBM" , "UTF-8" ) && /* non-default name */
149 dotestconv ( "ibm-5305" , "MIME" , NULL
) && /* mapping does not exist */
150 dotestconv ( "ascii" , "MIME" , NULL
) && /* mapping does not exist */
151 dotestconv ( "ibm-1208" , "IANA" , NULL
) && /* mapping does not exist */
152 dotestconv ( "ibm-5305" , "IANA" , NULL
) && /* mapping does not exist */
153 dotestconv ( "cp1208" , "" , "UTF-8" ) && /* default name due to ordering */
154 dotestconv ( "UTF16_BigEndian" , "" , "UTF-16BE" ) && /* non-default name due to ordering */
155 dotestconv ( "ISO-2022-CN" , "IANA" , "ISO_2022,locale=zh,version=0" ) && /* default name */
156 dotestconv ( "Shift_JIS" , "MIME" , "ibm-943_P15A-2003" ) && /* ambiguous alias */
157 dotestconv ( "Shift_JIS" , "" , "ibm-943_P130-1999" ) && /* ambiguous alias */
158 dotestconv ( "ibm-943" , "" , "ibm-943_P15A-2003" ) && /* ambiguous alias */
159 dotestconv ( "ibm-943" , "IBM" , "ibm-943_P130-1999" ) && /* ambiguous alias */
160 dotestconv ( "ibm-1363" , "" , "ibm-1363_P11B-1998" ) && /* ambiguous alias */
161 dotestconv ( "ibm-1363" , "IBM" , "ibm-1363_P110-1997" ) && /* ambiguous alias */
162 dotestconv ( "crazy" , "MIME" , NULL
) &&
163 dotestconv ( "ASCII" , "crazy" , NULL
))
165 log_verbose ( "PASS: getting IANA and MIME canonical names works \n " );
170 static UBool
doTestNames ( const char * name
, const char * standard
, const char ** expected
, int32_t size
) {
171 UErrorCode err
= U_ZERO_ERROR
;
172 UEnumeration
* myEnum
= ucnv_openStandardNames ( name
, standard
, & err
);
173 const char * enumName
, * testName
;
174 int32_t enumCount
= uenum_count ( myEnum
, & err
);
175 int32_t idx
, len
, repeatTimes
= 3 ;
177 if ( err
== U_FILE_ACCESS_ERROR
) {
178 log_data_err ( "Unable to open standard names for %s of standard: %s \n " , name
, standard
);
181 if ( size
!= enumCount
) {
182 log_err ( "FAIL: different size arrays for %s . Got %d . Expected %d \n " , name
, enumCount
, size
);
185 if ( size
< 0 && myEnum
) {
186 log_err ( "FAIL: size < 0, but recieved an actual object \n " );
189 log_verbose ( " \n %s %s \n " , name
, standard
);
190 while ( repeatTimes
-- > 0 ) {
191 for ( idx
= 0 ; idx
< enumCount
; idx
++) {
192 enumName
= uenum_next ( myEnum
, & len
, & err
);
193 testName
= expected
[ idx
];
194 if ( uprv_strcmp ( enumName
, testName
) != 0 || U_FAILURE ( err
)
195 || len
!= ( int32_t ) uprv_strlen ( expected
[ idx
]))
197 log_err ( "FAIL: uenum_next( %d ) == \" %s \" . expected \" %s \" , len= %d , error= %s \n " ,
198 idx
, enumName
, testName
, len
, u_errorName ( err
));
200 log_verbose ( " %s \n " , enumName
);
203 if ( enumCount
>= 0 ) {
204 /* one past the list of all names must return NULL */
205 enumName
= uenum_next ( myEnum
, & len
, & err
);
206 if ( enumName
!= NULL
|| len
!= 0 || U_FAILURE ( err
)) {
207 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
));
210 log_verbose ( " \n reset \n " );
211 uenum_reset ( myEnum
, & err
);
212 if ( U_FAILURE ( err
)) {
213 log_err ( "FAIL: uenum_reset() for %s { %s } failed with %s \n " ,
214 name
, standard
, u_errorName ( err
));
222 static UBool
doTestUCharNames ( const char * name
, const char * standard
, const char ** expected
, int32_t size
) {
223 UErrorCode err
= U_ZERO_ERROR
;
224 UEnumeration
* myEnum
= ucnv_openStandardNames ( name
, standard
, & err
);
225 int32_t enumCount
= uenum_count ( myEnum
, & err
);
226 int32_t idx
, repeatTimes
= 3 ;
228 if ( err
== U_FILE_ACCESS_ERROR
) {
229 log_data_err ( "Unable to open standard names for %s of standard: %s \n " , name
, standard
);
233 if ( size
!= enumCount
) {
234 log_err ( "FAIL: different size arrays. Got %d . Expected %d \n " , enumCount
, size
);
237 if ( size
< 0 && myEnum
) {
238 log_err ( "FAIL: size < 0, but recieved an actual object \n " );
241 log_verbose ( " \n %s %s \n " , name
, standard
);
242 while ( repeatTimes
-- > 0 ) {
243 for ( idx
= 0 ; idx
< enumCount
; idx
++) {
246 const UChar
* enumName
= uenum_unext ( myEnum
, & len
, & err
);
247 u_uastrncpy ( testName
, expected
[ idx
], sizeof ( testName
)/ sizeof ( testName
[ 0 ]));
248 if ( u_strcmp ( enumName
, testName
) != 0 || U_FAILURE ( err
)
249 || len
!= ( int32_t ) uprv_strlen ( expected
[ idx
]))
251 log_err ( "FAIL: uenum_next( %d ) == \" %s \" . expected \" %s \" , len= %d , error= %s \n " ,
252 idx
, enumName
, testName
, len
, u_errorName ( err
));
254 log_verbose ( " %s \n " , expected
[ idx
]);
257 log_verbose ( " \n reset \n " );
258 uenum_reset ( myEnum
, & err
);
259 if ( U_FAILURE ( err
)) {
260 log_err ( "FAIL: uenum_reset() for %s { %s } failed with %s \n " ,
261 name
, standard
, u_errorName ( err
));
269 static void TestStandardNames ()
271 static const char * asciiIANA
[] = {
284 static const char * asciiMIME
[] = {
288 static const char * iso2022MIME
[] = {
292 doTestNames ( "ASCII" , "IANA" , asciiIANA
, ARRAY_SIZE ( asciiIANA
));
293 doTestNames ( "US-ASCII" , "IANA" , asciiIANA
, ARRAY_SIZE ( asciiIANA
));
294 doTestNames ( "ASCII" , "MIME" , asciiMIME
, ARRAY_SIZE ( asciiMIME
));
295 doTestNames ( "ascii" , "mime" , asciiMIME
, ARRAY_SIZE ( asciiMIME
));
297 doTestNames ( "ASCII" , "crazy" , asciiMIME
, - 1 );
298 doTestNames ( "crazy" , "MIME" , asciiMIME
, - 1 );
300 doTestNames ( "LMBCS-1" , "MIME" , asciiMIME
, 0 );
302 doTestNames ( "ISO_2022,locale=ko,version=0" , "MIME" , iso2022MIME
, ARRAY_SIZE ( iso2022MIME
));
303 doTestNames ( "csiso2022kr" , "MIME" , iso2022MIME
, ARRAY_SIZE ( iso2022MIME
));
305 log_verbose ( " Testing unext() \n " );
306 doTestUCharNames ( "ASCII" , "IANA" , asciiIANA
, ARRAY_SIZE ( asciiIANA
));