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) 1999-2013, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8
9 #include <stdbool.h>
10
11 #include "unicode/utypes.h"
12 #include "unicode/ustring.h"
13 #include "unicode/ctest.h"
14 #include "unicode/ucnv.h"
15
16 void TestEuroRegression(void);
17 void addTestEuroRegression(TestNode** root);
18
19 #if !UCONFIG_NO_LEGACY_CONVERSION
addTestEuroRegression(TestNode ** root)20 void addTestEuroRegression(TestNode** root)
21 {
22 addTest(root, &TestEuroRegression, "tsconv/eurocreg/TestEuroRegression");
23 }
24
25 /*
26 * The table below lists codepages that are supposed to have roundtrip mappings for
27 * the U+20AC Euro sign.
28 *
29 * Changes made 2000nov28 and marked as such are due to the following:
30 *
31 * After updating all ibm-*.ucm files with precise fallback indicators (|0, |1, |3),
32 * some of these codepages failed the Euro regression test.
33 * This means that the actual mappings changed when only the preciseness of fallback
34 * mappings should have changed.
35 * My (Markus) suspicion is that some files got Euro sign mappings added manually,
36 * changing their contents compared to the NLTC (IBM Toronto codepage database) definition.
37 * Such changes are highly undesirable because they effectively define new codepages.
38 * Codepage mapping files with "ibm-*.ucm" should always exactly match the files
39 * from the IBM codepage database.
40 * (If there are several mappings with the same number, then we choose the
41 * default mappings with Private-Use Area assignments.)
42 *
43 * Also, in the past, some aliases were set such that e.g. cp850 became an alias for ibm-858.
44 * This followed the practice of OS/2 that uses the old codepage number 850 for the new
45 * codepage 858, with the main difference being the additional Euro sign.
46 * However, we have documented that the "cp" prefix should be used for Microsoft-compatible
47 * codepages, and Microsoft Windows 2000's codepage 850 does not contain a Euro sign mapping.
48 * Therefore, cp850 must not support the Euro sign.
49 * In these cases, I have changed the codepage name here to point to a newer codepage with the
50 * Euro sign, using its new name.
51 * I could not find such "updates" for codepages 1362 and 1363 - we might want to supply them later.
52 */
53
54 static const char convertersToCheck[][15] = {
55 "cp1250",
56 "cp1251",
57 "cp1252",
58 "cp1254",
59 "cp1255",
60 "cp1256",
61 "cp1257",
62 "cp1258",
63 "ibm1140",
64 "ibm1142",
65 "ibm1143",
66 "ibm1144",
67 "ibm1145",
68 "ibm1146",
69 "ibm1147",
70 "ibm1148",
71 "ibm1149",
72 "ibm1153",
73 "ibm1154",
74 "ibm1155",
75 "ibm1156",
76 "ibm1157",
77 "ibm1158",
78 /*"ibm-1159",*/ /* removed 2003Apr17 */
79 "ibm12712",
80 "ibm16804",
81 "ibm-1160",
82 "ibm-1162",
83 "ibm-1164",
84
85 "ibm-858", /* was "cp850" changed 2000nov28 */
86 /* duplicate "cp850" removed 2000nov28 */
87 /*"ibm-9049",*/ /* was "cp857" changed 2002nov25 */
88 "ibm-12712", /* was "cp424" changed 2000nov28 */
89 "ibm-4899", /* was "cp803" changed 2000nov28 */
90 "ibm-867", /* was "cp862" changed 2002nov25 */
91 "cp1258",
92 "windows-950",
93 "cp1253",
94 /* "cp819",
95 "cp13488",*/
96 "ibm-4971",
97 /*"ibm-9061",*/ /* was "cp869" changed 2002nov25 */
98 /* "cp813",*/
99 /*"ibm-9044",*/ /* was "cp852" changed 2002nov25 */
100 /*"ibm-872",*/ /* was "cp855" changed 2002nov25 */
101 /*"ibm-808",*/ /* was "cp866" changed 2002nov25 */
102 /* "cp1131",
103 "cp1125",*/
104 "ibm-902", /* was "cp922" changed 2003jan08 */
105 "ibm-901", /* was "cp921" changed 2003jan09 */
106 /*"ibm-17248",*/ /* was "cp864" changed 2002nov25 */
107 /*"cp1008",
108 "cp1046",*/
109 /* "cp9066",
110 "cp1129",*/
111 "ibm-5123", /* was "cp1027" changed 2003jan08 */
112 /* "cp300",*/
113 /* "cp4930",*/
114 "ibm-1364",
115 /* "cp1362" removed 2000nov28 */
116 "cp1363",
117 /* "cp1114", removed 2002jul3
118 "cp947", removed 2002jul3 */
119 "gb18030",
120 ""};
121
122 UBool isEuroAware(UConverter*);
123
TestEuroRegression()124 void TestEuroRegression()
125 {
126 int32_t i=0;
127
128 do
129 {
130 UErrorCode err = U_ZERO_ERROR;
131 UConverter* myConv = ucnv_open(convertersToCheck[i], &err);
132 if (U_FAILURE(err)&&convertersToCheck[i][0])
133 log_data_err("%s \tMISSING [%s]\n", convertersToCheck[i], u_errorName(err));
134 else
135 {
136 if (isEuroAware(myConv))
137 log_verbose("%s \tsupports euro\n", convertersToCheck[i]);
138 else
139 log_err("%s \tDOES NOT support euro\n", convertersToCheck[i]);
140 ucnv_close(myConv);
141 }
142 } while (convertersToCheck[++i][0]);
143 }
144
isEuroAware(UConverter * myConv)145 UBool isEuroAware(UConverter* myConv)
146 {
147 static const UChar euroString[2] = { 0x20AC, 0x0000 };
148 char target[20];
149 UChar euroBack[2];
150 int32_t targetSize, euroBackSize;
151 UErrorCode err = U_ZERO_ERROR;
152 /*const char* myName = ucnv_getName(myConv, &err);*/
153
154 targetSize = ucnv_fromUChars(myConv,
155 target,
156 sizeof(target),
157 euroString,
158 -1,
159 &err);
160 if (U_FAILURE(err))
161 {
162 log_err("Failure occurred in ucnv_fromUChars euro roundtrip test\n");
163 return false;
164 }
165 euroBackSize = ucnv_toUChars(myConv,
166 euroBack,
167 2,
168 target,
169 targetSize,
170 &err);
171 (void)euroBackSize; /* Suppress set but not used warning. */
172 if (U_FAILURE(err))
173 {
174 log_err("Failure occurred in ucnv_toUChars euro roundtrip test\n");
175 return false;
176 }
177 if (u_strcmp(euroString, euroBack))
178 {
179 /* log_err("%s FAILED Euro roundtrip\n", myName);*/
180 return false;
181 }
182 else
183 {
184 /* log_verbose("%s PASSED Euro roundtrip\n", myName);*/
185 return true;
186 }
187
188 }
189 #else
addTestEuroRegression(TestNode ** root)190 void addTestEuroRegression(TestNode** root)
191 {
192 /* test nothing... */
193 }
194 #endif
195