Debian Codegraph @

Utilities/KWSys/testEncoding.cxx

raw
1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
3 #include "kwsysPrivate.h"
4
5 #if defined(_MSC_VER)
6 # pragma warning(disable : 4786)
7 #endif
8
9 #include KWSYS_HEADER(Encoding.hxx)
10 #include KWSYS_HEADER(Encoding.h)
11
12 #include <algorithm>
13 #include <clocale>
14 #include <cstdlib>
15 #include <cstring>
16 #include <iostream>
17
18 // Work-around CMake dependency scanning limitation. This must
19 // duplicate the above list of headers.
20 #if 0
21 # include "Encoding.h.in"
22 # include "Encoding.hxx.in"
23 #endif
24
25 static const unsigned char helloWorldStrings[][32] = {
26 // English
27 { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', 0 },
28 // Japanese
29 { 0xE3, 0x81, 0x93, 0xE3, 0x82, 0x93, 0xE3, 0x81, 0xAB, 0xE3, 0x81,
30 0xA1, 0xE3, 0x81, 0xAF, 0xE4, 0xB8, 0x96, 0xE7, 0x95, 0x8C, 0 },
31 // Arabic
32 { 0xD9, 0x85, 0xD8, 0xB1, 0xD8, 0xAD, 0xD8, 0xA8, 0xD8, 0xA7, 0x20, 0xD8,
33 0xA7, 0xD9, 0x84, 0xD8, 0xB9, 0xD8, 0xA7, 0xD9, 0x84, 0xD9, 0x85, 0 },
34 // Yiddish
35 { 0xD7, 0x94, 0xD7, 0xA2, 0xD7, 0x9C, 0xD7, 0x90, 0x20, 0xD7,
36 0x95, 0xD7, 0x95, 0xD7, 0xA2, 0xD7, 0x9C, 0xD7, 0x98, 0 },
37 // Russian
38 { 0xD0, 0xBF, 0xD1, 0x80, 0xD0, 0xB8, 0xD0, 0xB2, 0xD0, 0xB5,
39 0xD1, 0x82, 0x20, 0xD0, 0xBC, 0xD0, 0xB8, 0xD1, 0x80, 0 },
40 // Latin
41 { 0x4D, 0x75, 0x6E, 0x64, 0x75, 0x73, 0x20, 0x73, 0x61, 0x6C, 0x76, 0x65,
42 0 },
43 // Swahili
44 { 0x68, 0x75, 0x6A, 0x61, 0x6D, 0x62, 0x6F, 0x20, 0x44, 0x75, 0x6E, 0x69,
45 0x61, 0 },
46 // Icelandic
47 { 0x48, 0x61, 0x6C, 0x6C, 0xC3, 0xB3, 0x20, 0x68, 0x65, 0x69, 0x6D, 0x75,
48 0x72, 0 },
49 { 0 }
50 };
51
52 static int testHelloWorldEncoding()
53 {
54 int ret = 0;
55 for (int i = 0; helloWorldStrings[i][0] != 0; i++) {
56 std::string str = reinterpret_cast<const char*>(helloWorldStrings[i]);
57 std::cout << str << std::endl;
58 std::wstring wstr = kwsys::Encoding::ToWide(str);
59 std::string str2 = kwsys::Encoding::ToNarrow(wstr);
60 wchar_t* c_wstr = kwsysEncoding_DupToWide(str.c_str());
61 char* c_str2 = kwsysEncoding_DupToNarrow(c_wstr);
62 if (!wstr.empty() && (str != str2 || strcmp(c_str2, str.c_str()) != 0)) {
63 std::cout << "converted string was different: " << str2 << std::endl;
64 std::cout << "converted string was different: " << c_str2 << std::endl;
65 ret++;
66 }
67 free(c_wstr);
68 free(c_str2);
69 }
70 return ret;
71 }
72
73 static int testRobustEncoding()
74 {
75 // test that the conversion functions handle invalid
76 // unicode correctly/gracefully
77
78 // we manipulate the format flags of stdout, remember
79 // the original state here to restore before return
80 std::ios::fmtflags const& flags = std::cout.flags();
81
82 int ret = 0;
83 char cstr[] = { static_cast<char>(-1), 0 };
84 // this conversion could fail
85 std::wstring wstr = kwsys::Encoding::ToWide(cstr);
86
87 wstr = kwsys::Encoding::ToWide(nullptr);
88 if (!wstr.empty()) {
89 const wchar_t* wcstr = wstr.c_str();
90 std::cout << "ToWide(NULL) returned";
91 for (size_t i = 0; i < wstr.size(); i++) {
92 std::cout << " " << std::hex << static_cast<int>(wcstr[i]);
93 }
94 std::cout << std::endl;
95 ret++;
96 }
97 wstr = kwsys::Encoding::ToWide("");
98 if (!wstr.empty()) {
99 const wchar_t* wcstr = wstr.c_str();
100 std::cout << "ToWide(\"\") returned";
101 for (size_t i = 0; i < wstr.size(); i++) {
102 std::cout << " " << std::hex << static_cast<int>(wcstr[i]);
103 }
104 std::cout << std::endl;
105 ret++;
106 }
107
108 #ifdef _WIN32
109 // 16 bit wchar_t - we make an invalid surrogate pair
110 wchar_t cwstr[] = { 0xD801, 0xDA00, 0 };
111 // this conversion could fail
112 std::string win_str = kwsys::Encoding::ToNarrow(cwstr);
113 #endif
114
115 std::string str = kwsys::Encoding::ToNarrow(nullptr);
116 if (!str.empty()) {
117 std::cout << "ToNarrow(NULL) returned " << str << std::endl;
118 ret++;
119 }
120
121 str = kwsys::Encoding::ToNarrow(L"");
122 if (!wstr.empty()) {
123 std::cout << "ToNarrow(\"\") returned " << str << std::endl;
124 ret++;
125 }
126
127 std::cout.flags(flags);
128 return ret;
129 }
130
131 static int testWithNulls()
132 {
133 int ret = 0;
134 std::vector<std::string> strings;
135 strings.push_back(std::string("ab") + '\0' + 'c');
136 strings.push_back(std::string("d") + '\0' + '\0' + 'e');
137 strings.push_back(std::string() + '\0' + 'f');
138 strings.push_back(std::string() + '\0' + '\0' + "gh");
139 strings.push_back(std::string("ij") + '\0');
140 strings.push_back(std::string("k") + '\0' + '\0');
141 strings.push_back(std::string("\0\0\0\0", 4) + "lmn" +
142 std::string("\0\0\0\0", 4));
143 for (auto& string : strings) {
144 std::wstring wstr = kwsys::Encoding::ToWide(string);
145 std::string str = kwsys::Encoding::ToNarrow(wstr);
146 std::string s(string);
147 std::replace(s.begin(), s.end(), '\0', ' ');
148 std::cout << "'" << s << "' (" << string.size() << ")" << std::endl;
149 if (str != string) {
150 std::replace(str.begin(), str.end(), '\0', ' ');
151 std::cout << "string with null was different: '" << str << "' ("
152 << str.size() << ")" << std::endl;
153 ret++;
154 }
155 }
156 return ret;
157 }
158
159 static int testCommandLineArguments()
160 {
161 int status = 0;
162
163 char const* argv[2] = {
164 "./app.exe", reinterpret_cast<char const*>(helloWorldStrings[1])
165 };
166
167 kwsys::Encoding::CommandLineArguments args(2, argv);
168 kwsys::Encoding::CommandLineArguments arg2 =
169 kwsys::Encoding::CommandLineArguments(args);
170
171 char const* const* u8_argv = args.argv();
172 for (int i = 0; i < args.argc(); i++) {
173 char const* u8_arg = u8_argv[i];
174 if (strcmp(argv[i], u8_arg) != 0) {
175 std::cout << "argv[" << i << "] " << argv[i] << " != " << u8_arg
176 << std::endl;
177 status++;
178 }
179 }
180
181 kwsys::Encoding::CommandLineArguments args3 =
182 kwsys::Encoding::CommandLineArguments::Main(2, argv);
183
184 return status;
185 }
186
187 static int testToWindowsExtendedPath()
188 {
189 #ifdef _WIN32
190 int ret = 0;
191 if (kwsys::Encoding::ToWindowsExtendedPath(
192 "L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
193 L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
194 std::cout << "Problem with ToWindowsExtendedPath "
195 << "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
196 << std::endl;
197 ++ret;
198 }
199
200 if (kwsys::Encoding::ToWindowsExtendedPath(
201 "L:/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
202 L"\\\\?\\L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
203 std::cout << "Problem with ToWindowsExtendedPath "
204 << "\"L:/Local Mojo/Hex Power Pack/Iffy Voodoo\"" << std::endl;
205 ++ret;
206 }
207
208 if (kwsys::Encoding::ToWindowsExtendedPath(
209 "\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") !=
210 L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
211 std::cout << "Problem with ToWindowsExtendedPath "
212 << "\"\\\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\""
213 << std::endl;
214 ++ret;
215 }
216
217 if (kwsys::Encoding::ToWindowsExtendedPath(
218 "//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
219 L"\\\\?\\UNC\\Foo\\Local Mojo\\Hex Power Pack\\Iffy Voodoo") {
220 std::cout << "Problem with ToWindowsExtendedPath "
221 << "\"//Foo/Local Mojo/Hex Power Pack/Iffy Voodoo\""
222 << std::endl;
223 ++ret;
224 }
225
226 if (kwsys::Encoding::ToWindowsExtendedPath("//") != L"//") {
227 std::cout << "Problem with ToWindowsExtendedPath "
228 << "\"//\"" << std::endl;
229 ++ret;
230 }
231
232 if (kwsys::Encoding::ToWindowsExtendedPath("\\\\.\\") != L"\\\\.\\") {
233 std::cout << "Problem with ToWindowsExtendedPath "
234 << "\"\\\\.\\\"" << std::endl;
235 ++ret;
236 }
237
238 if (kwsys::Encoding::ToWindowsExtendedPath("\\\\.\\X") != L"\\\\.\\X") {
239 std::cout << "Problem with ToWindowsExtendedPath "
240 << "\"\\\\.\\X\"" << std::endl;
241 ++ret;
242 }
243
244 if (kwsys::Encoding::ToWindowsExtendedPath("\\\\.\\X:") != L"\\\\?\\X:") {
245 std::cout << "Problem with ToWindowsExtendedPath "
246 << "\"\\\\.\\X:\"" << std::endl;
247 ++ret;
248 }
249
250 if (kwsys::Encoding::ToWindowsExtendedPath("\\\\.\\X:\\") !=
251 L"\\\\?\\X:\\") {
252 std::cout << "Problem with ToWindowsExtendedPath "
253 << "\"\\\\.\\X:\\\"" << std::endl;
254 ++ret;
255 }
256
257 if (kwsys::Encoding::ToWindowsExtendedPath("NUL") != L"\\\\.\\NUL") {
258 std::cout << "Problem with ToWindowsExtendedPath "
259 << "\"NUL\"" << std::endl;
260 ++ret;
261 }
262
263 return ret;
264 #else
265 return 0;
266 #endif
267 }
268
269 int testEncoding(int, char*[])
270 {
271 const char* loc = setlocale(LC_ALL, "");
272 if (loc) {
273 std::cout << "Locale: " << loc << std::endl;
274 } else {
275 std::cout << "Locale: None" << std::endl;
276 }
277
278 int ret = 0;
279
280 ret |= testHelloWorldEncoding();
281 ret |= testRobustEncoding();
282 ret |= testCommandLineArguments();
283 ret |= testWithNulls();
284 ret |= testToWindowsExtendedPath();
285
286 return ret;
287 }