]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/stdnmtst.c
ICU-6.2.22.tar.gz
[apple/icu.git] / icuSources / test / cintltst / stdnmtst.c
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2000-2003, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /*
7 * File stdnmtst.c
8 *
9 * Modification History:
10 *
11 * Date Name Description
12 * 08/05/2000 Yves Creation
13 *******************************************************************************
14 */
15
16 #include "unicode/ucnv.h"
17 #include "unicode/ustring.h"
18 #include "cstring.h"
19 #include "cintltst.h"
20
21 #define ARRAY_SIZE(array) (int32_t)(sizeof array / sizeof array[0])
22
23 static void TestStandardName(void);
24 static void TestStandardNames(void);
25 static void TestCanonicalName(void);
26
27 void addStandardNamesTest(TestNode** root);
28
29
30 void
31 addStandardNamesTest(TestNode** root)
32 {
33 addTest(root, &TestStandardName, "tsconv/stdnmtst/TestStandardName");
34 addTest(root, &TestStandardNames, "tsconv/stdnmtst/TestStandardNames");
35 addTest(root, &TestCanonicalName, "tsconv/stdnmtst/TestCanonicalName");
36 }
37
38 static int dotestname(const char *name, const char *standard, const char *expected) {
39 int res = 1;
40
41 UErrorCode error;
42 const char *tag;
43
44 error = U_ZERO_ERROR;
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);
48 res = 0;
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);
51 res = 0;
52 }
53
54 return res;
55 }
56
57 static void TestStandardName()
58 {
59 int res = 1;
60
61 uint16_t i, count;
62 UErrorCode err;
63
64 /* Iterate over all standards. */
65 for (i = 0, count = ucnv_countStandards(); i < count-1; ++i) {
66 const char *standard;
67
68 err = U_ZERO_ERROR;
69 standard = ucnv_getStandard(i, &err);
70 if (U_FAILURE(err)) {
71 log_err("FAIL: ucnv_getStandard(%d), error=%s\n", i, u_errorName(err));
72 res = 0;
73 } else if (!standard || !*standard) {
74 log_err("FAIL: %s standard name at index %d\n", (standard ? "empty" :
75 "null"), i);
76 res = 0;
77 }
78 }
79 err = U_ZERO_ERROR;
80 /* "" must be last */
81 if(!count) {
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);
85 res = 0;
86 }
87 err = U_ZERO_ERROR;
88 if (ucnv_getStandard(++i, &err)) {
89 log_err("FAIL: ucnv_getStandard(%d) should return NULL\n", i);
90 res = 0;
91 }
92
93 if (res) {
94 log_verbose("PASS: iterating over standard names works\n");
95 }
96
97 /* Test for some expected results. */
98
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))
109 {
110 log_verbose("PASS: getting IANA and MIME standard names works\n");
111 }
112 }
113
114 static int dotestconv(const char *name, const char *standard, const char *expected) {
115 int res = 1;
116
117 UErrorCode error;
118 const char *tag;
119
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);
124 res = 0;
125 } else if (!tag && expected) {
126 log_err("FAIL: could not find %s canonical name for %s\n", (standard ? "\"\"" : standard), name);
127 res = 0;
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);
130 res = 0;
131 }
132
133 return res;
134 }
135
136 static void TestCanonicalName()
137 {
138 /* Test for some expected results. */
139
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))
159 {
160 log_verbose("PASS: getting IANA and MIME canonical names works\n");
161 }
162 }
163
164
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);
172 return 0;
173 }
174 if (size < 0 && myEnum) {
175 log_err("FAIL: size < 0, but recieved an actual object\n");
176 return 0;
177 }
178 log_verbose("\n%s %s\n", name, standard);
179 while (repeatTimes-- > 0) {
180 for (idx = 0; idx < enumCount; idx++) {
181 int32_t len;
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]))
186 {
187 log_err("FAIL: uenum_next(%d) == \"%s\". expected \"%s\", len=%d, error=%s\n",
188 idx, enumName, testName, len, u_errorName(err));
189 }
190 log_verbose("%s\n", enumName);
191 err = U_ZERO_ERROR;
192 }
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));
198 err = U_ZERO_ERROR;
199 }
200 }
201 uenum_close(myEnum);
202 return 1;
203 }
204
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);
212 return 0;
213 }
214 if (size < 0 && myEnum) {
215 log_err("FAIL: size < 0, but recieved an actual object\n");
216 return 0;
217 }
218 log_verbose("\n%s %s\n", name, standard);
219 while (repeatTimes-- > 0) {
220 for (idx = 0; idx < enumCount; idx++) {
221 UChar testName[256];
222 int32_t len;
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]))
227 {
228 log_err("FAIL: uenum_next(%d) == \"%s\". expected \"%s\", len=%d, error=%s\n",
229 idx, enumName, testName, len, u_errorName(err));
230 }
231 log_verbose("%s\n", expected[idx]);
232 err = U_ZERO_ERROR;
233 }
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));
239 err = U_ZERO_ERROR;
240 }
241 }
242 uenum_close(myEnum);
243 return 1;
244 }
245
246 static void TestStandardNames()
247 {
248 static const char *asciiIANA[] = {
249 "ANSI_X3.4-1968",
250 "US-ASCII",
251 "ASCII",
252 "ANSI_X3.4-1986",
253 "ISO_646.irv:1991",
254 "ISO646-US",
255 "us",
256 "csASCII",
257 "iso-ir-6",
258 "cp367",
259 };
260 static const char *asciiMIME[] = {
261 "US-ASCII"
262 };
263
264 static const char *iso2022MIME[] = {
265 "ISO-2022-KR",
266 };
267
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));
272
273 doTestNames("ASCII", "crazy", asciiMIME, -1);
274 doTestNames("crazy", "MIME", asciiMIME, -1);
275
276 doTestNames("LMBCS-1", "MIME", asciiMIME, 0);
277
278 doTestNames("ISO_2022,locale=ko,version=0", "MIME", iso2022MIME, ARRAY_SIZE(iso2022MIME));
279 doTestNames("csiso2022kr", "MIME", iso2022MIME, ARRAY_SIZE(iso2022MIME));
280
281 log_verbose(" Testing unext()\n");
282 doTestUCharNames("ASCII", "IANA", asciiIANA, ARRAY_SIZE(asciiIANA));
283
284 }