1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2000-2009, 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
23static void TestStandardName(void);
24static void TestStandardNames(void);
25static void TestCanonicalName(void);
26
27void addStandardNamesTest(TestNode** root);
28
29
30void
31addStandardNamesTest(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
38static 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_status(error, "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
57static 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", "IANA", "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
114static 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    }
126    else if (!tag && expected) {
127        log_err_status(error, "FAIL: could not find %s canonical name for %s\n", (standard ? "\"\"" : standard), name);
128        res = 0;
129    }
130    else if (expected && (name == tag || uprv_strcmp(expected, tag) != 0)) {
131        log_err("FAIL: expected %s for %s canonical name for %s, got %s\n", expected, standard, name, tag);
132        res = 0;
133    }
134    else {
135        log_verbose("PASS: (\"%s\", \"%s\") -> %s == %s \n", name, standard, tag, expected);
136    }
137
138    return res;
139}
140
141static 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 */
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 */
157        dotestconv("Shift_JIS", "", "ibm-943_P130-1999") &&/* ambiguous alias */
158        dotestconv("ibm-943", "", "ibm-943_P15A-2003") &&/* ambiguous alias */
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
170static 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);
173    const char *enumName, *testName;
174    int32_t enumCount = uenum_count(myEnum, &err);
175    int32_t idx, len, repeatTimes = 3;
176
177    if (err == U_FILE_ACCESS_ERROR) {
178        log_data_err("Unable to open standard names for %s of standard: %s\n", name, standard);
179        return 0;
180    }
181    if (size != enumCount) {
182        log_err("FAIL: different size arrays for %s. Got %d. Expected %d\n", name, enumCount, size);
183        return 0;
184    }
185    if (size < 0 && myEnum) {
186        log_err("FAIL: size < 0, but recieved an actual object\n");
187        return 0;
188    }
189    log_verbose("\n%s %s\n", name, standard);
190    while (repeatTimes-- > 0) {
191        for (idx = 0; idx < enumCount; idx++) {
192            enumName = uenum_next(myEnum, &len, &err);
193            testName = expected[idx];
194            if (uprv_strcmp(enumName, testName) != 0 || U_FAILURE(err)
195                || len != (int32_t)uprv_strlen(expected[idx]))
196            {
197                log_err("FAIL: uenum_next(%d) == \"%s\". expected \"%s\", len=%d, error=%s\n",
198                    idx, enumName, testName, len, u_errorName(err));
199            }
200            log_verbose("%s\n", enumName);
201            err = U_ZERO_ERROR;
202        }
203        if (enumCount >= 0) {
204            /* one past the list of all names must return NULL */
205            enumName = uenum_next(myEnum, &len, &err);
206            if (enumName != NULL || len != 0 || U_FAILURE(err)) {
207                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));
208            }
209        }
210        log_verbose("\n    reset\n");
211        uenum_reset(myEnum, &err);
212        if (U_FAILURE(err)) {
213            log_err("FAIL: uenum_reset() for %s{%s} failed with %s\n",
214                name, standard, u_errorName(err));
215            err = U_ZERO_ERROR;
216        }
217    }
218    uenum_close(myEnum);
219    return 1;
220}
221
222static UBool doTestUCharNames(const char *name, const char *standard, const char **expected, int32_t size) {
223    UErrorCode err = U_ZERO_ERROR;
224    UEnumeration *myEnum = ucnv_openStandardNames(name, standard, &err);
225    int32_t enumCount = uenum_count(myEnum, &err);
226    int32_t idx, repeatTimes = 3;
227
228    if (err == U_FILE_ACCESS_ERROR) {
229        log_data_err("Unable to open standard names for %s of standard: %s\n", name, standard);
230        return 0;
231    }
232
233    if (size != enumCount) {
234        log_err("FAIL: different size arrays. Got %d. Expected %d\n", enumCount, size);
235        return 0;
236    }
237    if (size < 0 && myEnum) {
238        log_err("FAIL: size < 0, but recieved an actual object\n");
239        return 0;
240    }
241    log_verbose("\n%s %s\n", name, standard);
242    while (repeatTimes-- > 0) {
243        for (idx = 0; idx < enumCount; idx++) {
244            UChar testName[256];
245            int32_t len;
246            const UChar *enumName = uenum_unext(myEnum, &len, &err);
247            u_uastrncpy(testName, expected[idx], sizeof(testName)/sizeof(testName[0]));
248            if (u_strcmp(enumName, testName) != 0 || U_FAILURE(err)
249                || len != (int32_t)uprv_strlen(expected[idx]))
250            {
251                log_err("FAIL: uenum_next(%d) == \"%s\". expected \"%s\", len=%d, error=%s\n",
252                    idx, enumName, testName, len, u_errorName(err));
253            }
254            log_verbose("%s\n", expected[idx]);
255            err = U_ZERO_ERROR;
256        }
257        log_verbose("\n    reset\n");
258        uenum_reset(myEnum, &err);
259        if (U_FAILURE(err)) {
260            log_err("FAIL: uenum_reset() for %s{%s} failed with %s\n",
261                name, standard, u_errorName(err));
262            err = U_ZERO_ERROR;
263        }
264    }
265    uenum_close(myEnum);
266    return 1;
267}
268
269static void TestStandardNames()
270{
271    static const char *asciiIANA[] = {
272        "ANSI_X3.4-1968",
273        "US-ASCII",
274        "ASCII",
275        "ANSI_X3.4-1986",
276        "ISO_646.irv:1991",
277        "ISO646-US",
278        "us",
279        "csASCII",
280        "iso-ir-6",
281        "cp367",
282        "IBM367",
283    };
284    static const char *asciiMIME[] = {
285        "US-ASCII"
286    };
287
288    static const char *iso2022MIME[] = {
289        "ISO-2022-KR",
290    };
291
292    doTestNames("ASCII", "IANA", asciiIANA, ARRAY_SIZE(asciiIANA));
293    doTestNames("US-ASCII", "IANA", asciiIANA, ARRAY_SIZE(asciiIANA));
294    doTestNames("ASCII", "MIME", asciiMIME, ARRAY_SIZE(asciiMIME));
295    doTestNames("ascii", "mime", asciiMIME, ARRAY_SIZE(asciiMIME));
296
297    doTestNames("ASCII", "crazy", asciiMIME, -1);
298    doTestNames("crazy", "MIME", asciiMIME, -1);
299
300    doTestNames("LMBCS-1", "MIME", asciiMIME, 0);
301
302    doTestNames("ISO_2022,locale=ko,version=0", "MIME", iso2022MIME, ARRAY_SIZE(iso2022MIME));
303    doTestNames("csiso2022kr", "MIME", iso2022MIME, ARRAY_SIZE(iso2022MIME));
304
305    log_verbose(" Testing unext()\n");
306    doTestUCharNames("ASCII", "IANA", asciiIANA, ARRAY_SIZE(asciiIANA));
307
308}
309