]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /******************************************************************** |
2 | * COPYRIGHT: | |
46f4442e | 3 | * Copyright (c) 2000-2007, International Business Machines Corporation and |
b75a7d8f A |
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 | |
46f4442e | 13 | ****************************************************************************** |
b75a7d8f A |
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 | { | |
374ca955 A |
33 | addTest(root, &TestStandardName, "tsconv/stdnmtst/TestStandardName"); |
34 | addTest(root, &TestStandardNames, "tsconv/stdnmtst/TestStandardNames"); | |
35 | addTest(root, &TestCanonicalName, "tsconv/stdnmtst/TestCanonicalName"); | |
b75a7d8f A |
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") && | |
374ca955 | 102 | dotestname("csiso2022jp2", "MIME", "ISO-2022-JP-2") && |
73c04bcf | 103 | dotestname("Iso20-22__cN", "IANA", "ISO-2022-CN") && |
b75a7d8f A |
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; | |
73c04bcf A |
125 | } |
126 | else if (!tag && expected) { | |
b75a7d8f A |
127 | log_err("FAIL: could not find %s canonical name for %s\n", (standard ? "\"\"" : standard), name); |
128 | res = 0; | |
73c04bcf A |
129 | } |
130 | else if (expected && (name == tag || uprv_strcmp(expected, tag) != 0)) { | |
b75a7d8f A |
131 | log_err("FAIL: expected %s for %s canonical name for %s, got %s\n", expected, standard, name, tag); |
132 | res = 0; | |
133 | } | |
73c04bcf A |
134 | else { |
135 | log_verbose("PASS: (\"%s\", \"%s\") -> %s == %s \n", name, standard, tag, expected); | |
136 | } | |
b75a7d8f A |
137 | |
138 | return res; | |
139 | } | |
140 | ||
141 | static void TestCanonicalName() | |
142 | { | |
143 | /* Test for some expected results. */ | |
144 | ||
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 */ | |
73c04bcf A |
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 */ | |
b75a7d8f | 157 | dotestconv("Shift_JIS", "", "ibm-943_P130-1999") &&/* ambiguous alias */ |
73c04bcf | 158 | dotestconv("ibm-943", "", "ibm-943_P15A-2003") &&/* ambiguous alias */ |
b75a7d8f A |
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)) | |
164 | { | |
165 | log_verbose("PASS: getting IANA and MIME canonical names works\n"); | |
166 | } | |
167 | } | |
168 | ||
169 | ||
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); | |
73c04bcf | 173 | const char *enumName, *testName; |
b75a7d8f | 174 | int32_t enumCount = uenum_count(myEnum, &err); |
73c04bcf | 175 | int32_t idx, len, repeatTimes = 3; |
b75a7d8f | 176 | if (size != enumCount) { |
46f4442e | 177 | log_err("FAIL: different size arrays for %s. Got %d. Expected %d\n", name, enumCount, size); |
b75a7d8f A |
178 | return 0; |
179 | } | |
180 | if (size < 0 && myEnum) { | |
181 | log_err("FAIL: size < 0, but recieved an actual object\n"); | |
182 | return 0; | |
183 | } | |
184 | log_verbose("\n%s %s\n", name, standard); | |
185 | while (repeatTimes-- > 0) { | |
186 | for (idx = 0; idx < enumCount; idx++) { | |
73c04bcf A |
187 | enumName = uenum_next(myEnum, &len, &err); |
188 | testName = expected[idx]; | |
b75a7d8f A |
189 | if (uprv_strcmp(enumName, testName) != 0 || U_FAILURE(err) |
190 | || len != (int32_t)uprv_strlen(expected[idx])) | |
191 | { | |
192 | log_err("FAIL: uenum_next(%d) == \"%s\". expected \"%s\", len=%d, error=%s\n", | |
193 | idx, enumName, testName, len, u_errorName(err)); | |
194 | } | |
195 | log_verbose("%s\n", enumName); | |
196 | err = U_ZERO_ERROR; | |
197 | } | |
73c04bcf A |
198 | if (enumCount >= 0) { |
199 | /* one past the list of all names must return NULL */ | |
200 | enumName = uenum_next(myEnum, &len, &err); | |
201 | if (enumName != NULL || len != 0 || U_FAILURE(err)) { | |
202 | 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)); | |
203 | } | |
204 | } | |
b75a7d8f A |
205 | log_verbose("\n reset\n"); |
206 | uenum_reset(myEnum, &err); | |
207 | if (U_FAILURE(err)) { | |
208 | log_err("FAIL: uenum_reset() for %s{%s} failed with %s\n", | |
209 | name, standard, u_errorName(err)); | |
210 | err = U_ZERO_ERROR; | |
211 | } | |
212 | } | |
213 | uenum_close(myEnum); | |
214 | return 1; | |
215 | } | |
216 | ||
217 | static UBool doTestUCharNames(const char *name, const char *standard, const char **expected, int32_t size) { | |
218 | UErrorCode err = U_ZERO_ERROR; | |
219 | UEnumeration *myEnum = ucnv_openStandardNames(name, standard, &err); | |
220 | int32_t enumCount = uenum_count(myEnum, &err); | |
221 | int32_t idx, repeatTimes = 3; | |
222 | if (size != enumCount) { | |
223 | log_err("FAIL: different size arrays. Got %d. Expected %d\n", enumCount, size); | |
224 | return 0; | |
225 | } | |
226 | if (size < 0 && myEnum) { | |
227 | log_err("FAIL: size < 0, but recieved an actual object\n"); | |
228 | return 0; | |
229 | } | |
230 | log_verbose("\n%s %s\n", name, standard); | |
231 | while (repeatTimes-- > 0) { | |
232 | for (idx = 0; idx < enumCount; idx++) { | |
233 | UChar testName[256]; | |
234 | int32_t len; | |
235 | const UChar *enumName = uenum_unext(myEnum, &len, &err); | |
236 | u_uastrncpy(testName, expected[idx], sizeof(testName)/sizeof(testName[0])); | |
237 | if (u_strcmp(enumName, testName) != 0 || U_FAILURE(err) | |
238 | || len != (int32_t)uprv_strlen(expected[idx])) | |
239 | { | |
240 | log_err("FAIL: uenum_next(%d) == \"%s\". expected \"%s\", len=%d, error=%s\n", | |
241 | idx, enumName, testName, len, u_errorName(err)); | |
242 | } | |
243 | log_verbose("%s\n", expected[idx]); | |
244 | err = U_ZERO_ERROR; | |
245 | } | |
246 | log_verbose("\n reset\n"); | |
247 | uenum_reset(myEnum, &err); | |
248 | if (U_FAILURE(err)) { | |
249 | log_err("FAIL: uenum_reset() for %s{%s} failed with %s\n", | |
250 | name, standard, u_errorName(err)); | |
251 | err = U_ZERO_ERROR; | |
252 | } | |
253 | } | |
254 | uenum_close(myEnum); | |
255 | return 1; | |
256 | } | |
257 | ||
258 | static void TestStandardNames() | |
259 | { | |
260 | static const char *asciiIANA[] = { | |
261 | "ANSI_X3.4-1968", | |
262 | "US-ASCII", | |
263 | "ASCII", | |
264 | "ANSI_X3.4-1986", | |
265 | "ISO_646.irv:1991", | |
266 | "ISO646-US", | |
267 | "us", | |
268 | "csASCII", | |
269 | "iso-ir-6", | |
270 | "cp367", | |
46f4442e | 271 | "IBM367", |
b75a7d8f A |
272 | }; |
273 | static const char *asciiMIME[] = { | |
274 | "US-ASCII" | |
275 | }; | |
276 | ||
277 | static const char *iso2022MIME[] = { | |
374ca955 | 278 | "ISO-2022-KR", |
b75a7d8f A |
279 | }; |
280 | ||
281 | doTestNames("ASCII", "IANA", asciiIANA, ARRAY_SIZE(asciiIANA)); | |
282 | doTestNames("US-ASCII", "IANA", asciiIANA, ARRAY_SIZE(asciiIANA)); | |
283 | doTestNames("ASCII", "MIME", asciiMIME, ARRAY_SIZE(asciiMIME)); | |
284 | doTestNames("ascii", "mime", asciiMIME, ARRAY_SIZE(asciiMIME)); | |
285 | ||
286 | doTestNames("ASCII", "crazy", asciiMIME, -1); | |
287 | doTestNames("crazy", "MIME", asciiMIME, -1); | |
288 | ||
289 | doTestNames("LMBCS-1", "MIME", asciiMIME, 0); | |
290 | ||
374ca955 A |
291 | doTestNames("ISO_2022,locale=ko,version=0", "MIME", iso2022MIME, ARRAY_SIZE(iso2022MIME)); |
292 | doTestNames("csiso2022kr", "MIME", iso2022MIME, ARRAY_SIZE(iso2022MIME)); | |
b75a7d8f A |
293 | |
294 | log_verbose(" Testing unext()\n"); | |
295 | doTestUCharNames("ASCII", "IANA", asciiIANA, ARRAY_SIZE(asciiIANA)); | |
296 | ||
297 | } |