]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/stdnmtst.c
1 /********************************************************************
3 * Copyright (c) 2000-2003, 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("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", "MIME", "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
);
125 } else if (!tag
&& expected
) {
126 log_err("FAIL: could not find %s canonical name for %s\n", (standard
? "\"\"" : standard
), name
);
128 } else if (expected
&& (name
== tag
|| uprv_strcmp(expected
, tag
)) && error
== U_ZERO_ERROR
) {
129 log_err("FAIL: expected %s for %s canonical name for %s, got %s\n", expected
, standard
, name
, tag
);
136 static void TestCanonicalName()
138 /* Test for some expected results. */
140 if (dotestconv("UTF-8", "IANA", "UTF-8") && /* default name */
141 dotestconv("UTF-8", "MIME", "UTF-8") && /* default name */
142 dotestconv("ibm-1208", "IBM", "UTF-8") && /* default name */
143 dotestconv("ibm-5305", "IBM", "UTF-8") && /* non-default name */
144 dotestconv("ibm-5305", "MIME", NULL
) && /* mapping does not exist */
145 dotestconv("ascii", "MIME", NULL
) && /* mapping does not exist */
146 dotestconv("ibm-1208", "IANA", NULL
) && /* mapping does not exist */
147 dotestconv("ibm-5305", "IANA", NULL
) && /* mapping does not exist */
148 dotestconv("cp1208", "", "UTF-8") && /* default name due to ordering */
149 dotestconv("UTF16_BigEndian", "", "UTF-16BE") && /* non-default name due to ordering */
150 dotestconv("ISO-2022-CN", "MIME", "ISO_2022,locale=zh,version=0") &&/* default name */
151 dotestconv("Shift_JIS", "MIME", "ibm-943_P14A-1999") &&/* ambiguous alias */
152 dotestconv("Shift_JIS", "", "ibm-943_P130-1999") &&/* ambiguous alias */
153 dotestconv("ibm-943", "", "ibm-943_P14A-1999") &&/* ambiguous alias */
154 dotestconv("ibm-943", "IBM", "ibm-943_P130-1999") &&/* ambiguous alias */
155 dotestconv("ibm-1363", "", "ibm-1363_P11B-1998") &&/* ambiguous alias */
156 dotestconv("ibm-1363", "IBM", "ibm-1363_P110-1997") &&/* ambiguous alias */
157 dotestconv("crazy", "MIME", NULL
) &&
158 dotestconv("ASCII", "crazy", NULL
))
160 log_verbose("PASS: getting IANA and MIME canonical names works\n");
165 static UBool
doTestNames(const char *name
, const char *standard
, const char **expected
, int32_t size
) {
166 UErrorCode err
= U_ZERO_ERROR
;
167 UEnumeration
*myEnum
= ucnv_openStandardNames(name
, standard
, &err
);
168 int32_t enumCount
= uenum_count(myEnum
, &err
);
169 int32_t idx
, repeatTimes
= 3;
170 if (size
!= enumCount
) {
171 log_err("FAIL: different size arrays. Got %d. Expected %d\n", enumCount
, size
);
174 if (size
< 0 && myEnum
) {
175 log_err("FAIL: size < 0, but recieved an actual object\n");
178 log_verbose("\n%s %s\n", name
, standard
);
179 while (repeatTimes
-- > 0) {
180 for (idx
= 0; idx
< enumCount
; idx
++) {
182 const char *enumName
= uenum_next(myEnum
, &len
, &err
);
183 const char *testName
= expected
[idx
];
184 if (uprv_strcmp(enumName
, testName
) != 0 || U_FAILURE(err
)
185 || len
!= (int32_t)uprv_strlen(expected
[idx
]))
187 log_err("FAIL: uenum_next(%d) == \"%s\". expected \"%s\", len=%d, error=%s\n",
188 idx
, enumName
, testName
, len
, u_errorName(err
));
190 log_verbose("%s\n", enumName
);
193 log_verbose("\n reset\n");
194 uenum_reset(myEnum
, &err
);
195 if (U_FAILURE(err
)) {
196 log_err("FAIL: uenum_reset() for %s{%s} failed with %s\n",
197 name
, standard
, u_errorName(err
));
205 static UBool
doTestUCharNames(const char *name
, const char *standard
, const char **expected
, int32_t size
) {
206 UErrorCode err
= U_ZERO_ERROR
;
207 UEnumeration
*myEnum
= ucnv_openStandardNames(name
, standard
, &err
);
208 int32_t enumCount
= uenum_count(myEnum
, &err
);
209 int32_t idx
, repeatTimes
= 3;
210 if (size
!= enumCount
) {
211 log_err("FAIL: different size arrays. Got %d. Expected %d\n", enumCount
, size
);
214 if (size
< 0 && myEnum
) {
215 log_err("FAIL: size < 0, but recieved an actual object\n");
218 log_verbose("\n%s %s\n", name
, standard
);
219 while (repeatTimes
-- > 0) {
220 for (idx
= 0; idx
< enumCount
; idx
++) {
223 const UChar
*enumName
= uenum_unext(myEnum
, &len
, &err
);
224 u_uastrncpy(testName
, expected
[idx
], sizeof(testName
)/sizeof(testName
[0]));
225 if (u_strcmp(enumName
, testName
) != 0 || U_FAILURE(err
)
226 || len
!= (int32_t)uprv_strlen(expected
[idx
]))
228 log_err("FAIL: uenum_next(%d) == \"%s\". expected \"%s\", len=%d, error=%s\n",
229 idx
, enumName
, testName
, len
, u_errorName(err
));
231 log_verbose("%s\n", expected
[idx
]);
234 log_verbose("\n reset\n");
235 uenum_reset(myEnum
, &err
);
236 if (U_FAILURE(err
)) {
237 log_err("FAIL: uenum_reset() for %s{%s} failed with %s\n",
238 name
, standard
, u_errorName(err
));
246 static void TestStandardNames()
248 static const char *asciiIANA
[] = {
260 static const char *asciiMIME
[] = {
264 static const char *iso2022MIME
[] = {
268 doTestNames("ASCII", "IANA", asciiIANA
, ARRAY_SIZE(asciiIANA
));
269 doTestNames("US-ASCII", "IANA", asciiIANA
, ARRAY_SIZE(asciiIANA
));
270 doTestNames("ASCII", "MIME", asciiMIME
, ARRAY_SIZE(asciiMIME
));
271 doTestNames("ascii", "mime", asciiMIME
, ARRAY_SIZE(asciiMIME
));
273 doTestNames("ASCII", "crazy", asciiMIME
, -1);
274 doTestNames("crazy", "MIME", asciiMIME
, -1);
276 doTestNames("LMBCS-1", "MIME", asciiMIME
, 0);
278 doTestNames("ISO_2022,locale=ko,version=0", "MIME", iso2022MIME
, ARRAY_SIZE(iso2022MIME
));
279 doTestNames("csiso2022kr", "MIME", iso2022MIME
, ARRAY_SIZE(iso2022MIME
));
281 log_verbose(" Testing unext()\n");
282 doTestUCharNames("ASCII", "IANA", asciiIANA
, ARRAY_SIZE(asciiIANA
));