]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/stdnmtst.c
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 2000-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
11 * Modification History:
13 * Date Name Description
14 * 08/05/2000 Yves Creation
15 ******************************************************************************
18 #include "unicode/ucnv.h"
19 #include "unicode/ustring.h"
24 static void TestStandardName ( void );
25 static void TestStandardNames ( void );
26 static void TestCanonicalName ( void );
28 void addStandardNamesTest ( TestNode
** root
);
32 addStandardNamesTest ( TestNode
** root
)
34 addTest ( root
, & TestStandardName
, "tsconv/stdnmtst/TestStandardName" );
35 addTest ( root
, & TestStandardNames
, "tsconv/stdnmtst/TestStandardNames" );
36 addTest ( root
, & TestCanonicalName
, "tsconv/stdnmtst/TestCanonicalName" );
39 static int dotestname ( const char * name
, const char * standard
, const char * expected
) {
46 tag
= ucnv_getStandardName ( name
, standard
, & error
);
47 if (! tag
&& expected
) {
48 log_err_status ( error
, "FAIL: could not find %s standard name for %s \n " , standard
, name
);
50 } else if ( expected
&& ( name
== tag
|| uprv_strcmp ( expected
, tag
))) {
51 log_err ( "FAIL: expected %s for %s standard name for %s , got %s \n " , expected
, standard
, name
, tag
);
58 static void TestStandardName ()
65 /* Iterate over all standards. */
66 for ( i
= 0 , count
= ucnv_countStandards (); i
< count
- 1 ; ++ i
) {
70 standard
= ucnv_getStandard ( i
, & err
);
72 log_err ( "FAIL: ucnv_getStandard( %d ), error= %s \n " , i
, u_errorName ( err
));
74 } else if (! standard
|| !* standard
) {
75 log_err ( "FAIL: %s standard name at index %d \n " , ( standard
? "empty" :
83 log_data_err ( "No standards. You probably have no data. \n " );
84 } else if (* ucnv_getStandard (( uint16_t )( count
- 1 ), & err
) != 0 ) {
85 log_err ( "FAIL: ucnv_getStandard( %d ) should return " " \n " , count
- 1 );
89 if ( ucnv_getStandard (++ i
, & err
)) {
90 log_err ( "FAIL: ucnv_getStandard( %d ) should return NULL \n " , i
);
95 log_verbose ( "PASS: iterating over standard names works \n " );
98 /* Test for some expected results. */
100 if ( dotestname ( "ibm-1208" , "MIME" , "UTF-8" ) &&
101 /*dotestname("cp1252", "MIME", "windows-1252") &&*/
102 dotestname ( "ascii" , "MIME" , "US-ASCII" ) &&
103 dotestname ( "csiso2022jp2" , "MIME" , "ISO-2022-JP-2" ) &&
104 dotestname ( "Iso20-22__cN" , "IANA" , "ISO-2022-CN" ) &&
105 dotestname ( "ascii" , "IANA" , "ANSI_X3.4-1968" ) &&
106 dotestname ( "cp850" , "IANA" , "IBM850" ) &&
107 dotestname ( "crazy" , "MIME" , NULL
) &&
108 dotestname ( "ASCII" , "crazy" , NULL
) &&
109 dotestname ( "LMBCS-1" , "MIME" , NULL
))
111 log_verbose ( "PASS: getting IANA and MIME standard names works \n " );
115 static int dotestconv ( const char * name
, const char * standard
, const char * expected
) {
121 error
= U_ZERO_ERROR
;
122 tag
= ucnv_getCanonicalName ( name
, standard
, & error
);
123 if ( tag
&& ! expected
) {
124 log_err ( "FAIL: Unexpectedly found %s canonical name for %s , got %s \n " , standard
, name
, tag
);
127 else if (! tag
&& expected
) {
128 log_err_status ( error
, "FAIL: could not find %s canonical name for %s \n " , ( standard
? " \"\" " : standard
), name
);
131 else if ( expected
&& ( name
== tag
|| uprv_strcmp ( expected
, tag
) != 0 )) {
132 log_err ( "FAIL: expected %s for %s canonical name for %s , got %s \n " , expected
, standard
, name
, tag
);
136 log_verbose ( "PASS: ( \" %s \" , \" %s \" ) -> %s == %s \n " , name
, standard
, tag
, expected
);
142 static void TestCanonicalName ()
144 /* Test for some expected results. */
146 if ( dotestconv ( "UTF-8" , "IANA" , "UTF-8" ) && /* default name */
147 dotestconv ( "UTF-8" , "MIME" , "UTF-8" ) && /* default name */
148 dotestconv ( "ibm-1208" , "IBM" , "UTF-8" ) && /* default name */
149 dotestconv ( "ibm-5305" , "IBM" , "UTF-8" ) && /* non-default name */
150 dotestconv ( "ibm-5305" , "MIME" , NULL
) && /* mapping does not exist */
151 dotestconv ( "ascii" , "MIME" , NULL
) && /* mapping does not exist */
152 dotestconv ( "ibm-1208" , "IANA" , NULL
) && /* mapping does not exist */
153 dotestconv ( "ibm-5305" , "IANA" , NULL
) && /* mapping does not exist */
154 dotestconv ( "cp1208" , "" , "UTF-8" ) && /* default name due to ordering */
155 dotestconv ( "UTF16_BigEndian" , "" , "UTF-16BE" ) && /* non-default name due to ordering */
156 dotestconv ( "ISO-2022-CN" , "IANA" , "ISO_2022,locale=zh,version=0" ) && /* default name */
157 dotestconv ( "Shift_JIS" , "MIME" , "ibm-943_P15A-2003" ) && /* ambiguous alias */
158 dotestconv ( "Shift_JIS" , "" , "ibm-943_P130-1999" ) && /* ambiguous alias */
159 dotestconv ( "ibm-943" , "" , "ibm-943_P15A-2003" ) && /* ambiguous alias */
160 dotestconv ( "ibm-943" , "IBM" , "ibm-943_P130-1999" ) && /* ambiguous alias */
161 dotestconv ( "ibm-1363" , "" , "ibm-1363_P11B-1998" ) && /* ambiguous alias */
162 dotestconv ( "ibm-1363" , "IBM" , "ibm-1363_P110-1997" ) && /* ambiguous alias */
163 dotestconv ( "crazy" , "MIME" , NULL
) &&
164 dotestconv ( "ASCII" , "crazy" , NULL
))
166 log_verbose ( "PASS: getting IANA and MIME canonical names works \n " );
171 static UBool
doTestNames ( const char * name
, const char * standard
, const char ** expected
, int32_t size
) {
172 UErrorCode err
= U_ZERO_ERROR
;
173 UEnumeration
* myEnum
= ucnv_openStandardNames ( name
, standard
, & err
);
174 const char * enumName
, * testName
;
175 int32_t enumCount
= uenum_count ( myEnum
, & err
);
176 int32_t idx
, len
, repeatTimes
= 3 ;
178 if ( err
== U_FILE_ACCESS_ERROR
) {
179 log_data_err ( "Unable to open standard names for %s of standard: %s \n " , name
, standard
);
182 if ( size
!= enumCount
) {
183 log_err ( "FAIL: different size arrays for %s . Got %d . Expected %d \n " , name
, enumCount
, size
);
186 if ( size
< 0 && myEnum
) {
187 log_err ( "FAIL: size < 0, but recieved an actual object \n " );
190 log_verbose ( " \n %s %s \n " , name
, standard
);
191 while ( repeatTimes
-- > 0 ) {
192 for ( idx
= 0 ; idx
< enumCount
; idx
++) {
193 enumName
= uenum_next ( myEnum
, & len
, & err
);
194 testName
= expected
[ idx
];
195 if ( uprv_strcmp ( enumName
, testName
) != 0 || U_FAILURE ( err
)
196 || len
!= ( int32_t ) uprv_strlen ( expected
[ idx
]))
198 log_err ( "FAIL: uenum_next( %d ) == \" %s \" . expected \" %s \" , len= %d , error= %s \n " ,
199 idx
, enumName
, testName
, len
, u_errorName ( err
));
201 log_verbose ( " %s \n " , enumName
);
204 if ( enumCount
>= 0 ) {
205 /* one past the list of all names must return NULL */
206 enumName
= uenum_next ( myEnum
, & len
, & err
);
207 if ( enumName
!= NULL
|| len
!= 0 || U_FAILURE ( err
)) {
208 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
));
211 log_verbose ( " \n reset \n " );
212 uenum_reset ( myEnum
, & err
);
213 if ( U_FAILURE ( err
)) {
214 log_err ( "FAIL: uenum_reset() for %s { %s } failed with %s \n " ,
215 name
, standard
, u_errorName ( err
));
223 static UBool
doTestUCharNames ( const char * name
, const char * standard
, const char ** expected
, int32_t size
) {
224 UErrorCode err
= U_ZERO_ERROR
;
225 UEnumeration
* myEnum
= ucnv_openStandardNames ( name
, standard
, & err
);
226 int32_t enumCount
= uenum_count ( myEnum
, & err
);
227 int32_t idx
, repeatTimes
= 3 ;
229 if ( err
== U_FILE_ACCESS_ERROR
) {
230 log_data_err ( "Unable to open standard names for %s of standard: %s \n " , name
, standard
);
234 if ( size
!= enumCount
) {
235 log_err ( "FAIL: different size arrays. Got %d . Expected %d \n " , enumCount
, size
);
238 if ( size
< 0 && myEnum
) {
239 log_err ( "FAIL: size < 0, but recieved an actual object \n " );
242 log_verbose ( " \n %s %s \n " , name
, standard
);
243 while ( repeatTimes
-- > 0 ) {
244 for ( idx
= 0 ; idx
< enumCount
; idx
++) {
247 const UChar
* enumName
= uenum_unext ( myEnum
, & len
, & err
);
248 u_uastrncpy ( testName
, expected
[ idx
], UPRV_LENGTHOF ( testName
));
249 if ( u_strcmp ( enumName
, testName
) != 0 || U_FAILURE ( err
)
250 || len
!= ( int32_t ) uprv_strlen ( expected
[ idx
]))
252 log_err ( "FAIL: uenum_next( %d ) == \" %s \" . expected \" %s \" , len= %d , error= %s \n " ,
253 idx
, enumName
, testName
, len
, u_errorName ( err
));
255 log_verbose ( " %s \n " , expected
[ idx
]);
258 log_verbose ( " \n reset \n " );
259 uenum_reset ( myEnum
, & err
);
260 if ( U_FAILURE ( err
)) {
261 log_err ( "FAIL: uenum_reset() for %s { %s } failed with %s \n " ,
262 name
, standard
, u_errorName ( err
));
270 static void TestStandardNames ()
272 static const char * asciiIANA
[] = {
285 static const char * asciiMIME
[] = {
289 static const char * iso2022MIME
[] = {
293 doTestNames ( "ASCII" , "IANA" , asciiIANA
, UPRV_LENGTHOF ( asciiIANA
));
294 doTestNames ( "US-ASCII" , "IANA" , asciiIANA
, UPRV_LENGTHOF ( asciiIANA
));
295 doTestNames ( "ASCII" , "MIME" , asciiMIME
, UPRV_LENGTHOF ( asciiMIME
));
296 doTestNames ( "ascii" , "mime" , asciiMIME
, UPRV_LENGTHOF ( asciiMIME
));
298 doTestNames ( "ASCII" , "crazy" , asciiMIME
, - 1 );
299 doTestNames ( "crazy" , "MIME" , asciiMIME
, - 1 );
301 doTestNames ( "LMBCS-1" , "MIME" , asciiMIME
, 0 );
303 doTestNames ( "ISO_2022,locale=ko,version=0" , "MIME" , iso2022MIME
, UPRV_LENGTHOF ( iso2022MIME
));
304 doTestNames ( "csiso2022kr" , "MIME" , iso2022MIME
, UPRV_LENGTHOF ( iso2022MIME
));
306 log_verbose ( " Testing unext() \n " );
307 doTestUCharNames ( "ASCII" , "IANA" , asciiIANA
, UPRV_LENGTHOF ( asciiIANA
));