• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4  * COPYRIGHT:
5  * Copyright (c) 2000-2016, International Business Machines Corporation and
6  * others. All Rights Reserved.
7  ********************************************************************/
8 /*
9 * File stdnmtst.c
10 *
11 * Modification History:
12 *
13 *   Date          Name        Description
14 *   08/05/2000    Yves       Creation
15 ******************************************************************************
16 */
17 
18 #include "unicode/ucnv.h"
19 #include "unicode/ustring.h"
20 #include "cmemory.h"
21 #include "cstring.h"
22 #include "cintltst.h"
23 
24 static void TestStandardName(void);
25 static void TestStandardNames(void);
26 static void TestCanonicalName(void);
27 
28 void addStandardNamesTest(TestNode** root);
29 
30 
31 void
addStandardNamesTest(TestNode ** root)32 addStandardNamesTest(TestNode** root)
33 {
34   addTest(root, &TestStandardName,  "tsconv/stdnmtst/TestStandardName");
35   addTest(root, &TestStandardNames, "tsconv/stdnmtst/TestStandardNames");
36   addTest(root, &TestCanonicalName, "tsconv/stdnmtst/TestCanonicalName");
37 }
38 
dotestname(const char * name,const char * standard,const char * expected)39 static int dotestname(const char *name, const char *standard, const char *expected) {
40     int res = 1;
41 
42     UErrorCode error;
43     const char *tag;
44 
45     error = U_ZERO_ERROR;
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);
49         res = 0;
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);
52         res = 0;
53     }
54 
55     return res;
56 }
57 
TestStandardName()58 static void TestStandardName()
59 {
60     int res = 1;
61 
62     uint16_t i, count;
63     UErrorCode err;
64 
65     /* Iterate over all standards. */
66     for (i = 0, count = ucnv_countStandards(); i < count-1; ++i) {
67         const char *standard;
68 
69         err = U_ZERO_ERROR;
70         standard = ucnv_getStandard(i, &err);
71         if (U_FAILURE(err)) {
72             log_err("FAIL: ucnv_getStandard(%d), error=%s\n", i, u_errorName(err));
73             res = 0;
74         } else if (!standard || !*standard) {
75             log_err("FAIL: %s standard name at index %d\n", (standard ? "empty" :
76                 "null"), i);
77             res = 0;
78         }
79     }
80     err = U_ZERO_ERROR;
81     /* "" must be last */
82     if(!count) {
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);
86         res = 0;
87     }
88     err = U_ZERO_ERROR;
89     if (ucnv_getStandard(++i, &err)) {
90         log_err("FAIL: ucnv_getStandard(%d) should return NULL\n", i);
91         res = 0;
92     }
93 
94     if (res) {
95         log_verbose("PASS: iterating over standard names works\n");
96     }
97 
98     /* Test for some expected results. */
99 
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))
110     {
111         log_verbose("PASS: getting IANA and MIME standard names works\n");
112     }
113 }
114 
dotestconv(const char * name,const char * standard,const char * expected)115 static int dotestconv(const char *name, const char *standard, const char *expected) {
116     int res = 1;
117 
118     UErrorCode error;
119     const char *tag;
120 
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);
125         res = 0;
126     }
127     else if (!tag && expected) {
128         log_err_status(error, "FAIL: could not find %s canonical name for %s\n", (standard ? "\"\"" : standard), name);
129         res = 0;
130     }
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);
133         res = 0;
134     }
135     else {
136         log_verbose("PASS: (\"%s\", \"%s\") -> %s == %s \n", name, standard, tag, expected);
137     }
138 
139     return res;
140 }
141 
TestCanonicalName()142 static void TestCanonicalName()
143 {
144     /* Test for some expected results. */
145 
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))
165     {
166         log_verbose("PASS: getting IANA and MIME canonical names works\n");
167     }
168 }
169 
170 
doTestNames(const char * name,const char * standard,const char ** expected,int32_t size)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;
177 
178     if (err == U_FILE_ACCESS_ERROR) {
179         log_data_err("Unable to open standard names for %s of standard: %s\n", name, standard);
180         return 0;
181     }
182     if (size != enumCount) {
183         log_err("FAIL: different size arrays for %s. Got %d. Expected %d\n", name, enumCount, size);
184         return 0;
185     }
186     if (size < 0 && myEnum) {
187         log_err("FAIL: size < 0, but received an actual object\n");
188         return 0;
189     }
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]))
197             {
198                 log_err("FAIL: uenum_next(%d) == \"%s\". expected \"%s\", len=%d, error=%s\n",
199                     idx, enumName, testName, len, u_errorName(err));
200             }
201             log_verbose("%s\n", enumName);
202             err = U_ZERO_ERROR;
203         }
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));
209             }
210         }
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));
216             err = U_ZERO_ERROR;
217         }
218     }
219     uenum_close(myEnum);
220     return 1;
221 }
222 
doTestUCharNames(const char * name,const char * standard,const char ** expected,int32_t size)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;
228 
229     if (err == U_FILE_ACCESS_ERROR) {
230         log_data_err("Unable to open standard names for %s of standard: %s\n", name, standard);
231         return 0;
232     }
233 
234     if (size != enumCount) {
235         log_err("FAIL: different size arrays. Got %d. Expected %d\n", enumCount, size);
236         return 0;
237     }
238     if (size < 0 && myEnum) {
239         log_err("FAIL: size < 0, but received an actual object\n");
240         return 0;
241     }
242     log_verbose("\n%s %s\n", name, standard);
243     while (repeatTimes-- > 0) {
244         for (idx = 0; idx < enumCount; idx++) {
245             UChar testName[256];
246             int32_t len;
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]))
251             {
252                 log_err("FAIL: uenum_next(%d) == \"%s\". expected \"%s\", len=%d, error=%s\n",
253                     idx, enumName, testName, len, u_errorName(err));
254             }
255             log_verbose("%s\n", expected[idx]);
256             err = U_ZERO_ERROR;
257         }
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));
263             err = U_ZERO_ERROR;
264         }
265     }
266     uenum_close(myEnum);
267     return 1;
268 }
269 
TestStandardNames()270 static void TestStandardNames()
271 {
272     static const char *asciiIANA[] = {
273         "ANSI_X3.4-1968",
274         "US-ASCII",
275         "ASCII",
276         "ANSI_X3.4-1986",
277         "ISO_646.irv:1991",
278         "ISO646-US",
279         "us",
280         "csASCII",
281         "iso-ir-6",
282         "cp367",
283         "IBM367",
284     };
285     static const char *asciiMIME[] = {
286         "US-ASCII"
287     };
288 
289     static const char *iso2022MIME[] = {
290         "ISO-2022-KR",
291     };
292 
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));
297 
298     doTestNames("ASCII", "crazy", asciiMIME, -1);
299     doTestNames("crazy", "MIME", asciiMIME, -1);
300 
301     doTestNames("LMBCS-1", "MIME", asciiMIME, 0);
302 
303     doTestNames("ISO_2022,locale=ko,version=0", "MIME", iso2022MIME, UPRV_LENGTHOF(iso2022MIME));
304     doTestNames("csiso2022kr", "MIME", iso2022MIME, UPRV_LENGTHOF(iso2022MIME));
305 
306     log_verbose(" Testing unext()\n");
307     doTestUCharNames("ASCII", "IANA", asciiIANA, UPRV_LENGTHOF(asciiIANA));
308 
309 }
310