2 * Unit tests for code page to/from unicode translations
4 * Copyright (c) 2002 Dmitry Timoshkov
5 * Copyright (c) 2008 Colin Finck
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/test.h"
30 static const WCHAR foobarW
[] = {'f','o','o','b','a','r',0};
32 static void test_destination_buffer(void)
39 SetLastError(0xdeadbeef);
40 needed
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, NULL
, 0, NULL
, NULL
);
41 ok( (needed
> 0), "returned %d with %u (expected '> 0')\n",
42 needed
, GetLastError());
45 buffer
= HeapAlloc(GetProcessHeap(), 0, maxsize
);
46 if (buffer
== NULL
) return;
49 memset(buffer
, 'x', maxsize
);
50 buffer
[maxsize
] = '\0';
51 SetLastError(0xdeadbeef);
52 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, buffer
, needed
+1, NULL
, NULL
);
53 ok( (len
> 0), "returned %d with %u and '%s' (expected '> 0')\n",
54 len
, GetLastError(), buffer
);
56 memset(buffer
, 'x', maxsize
);
57 buffer
[maxsize
] = '\0';
58 SetLastError(0xdeadbeef);
59 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, buffer
, needed
, NULL
, NULL
);
60 ok( (len
> 0), "returned %d with %u and '%s' (expected '> 0')\n",
61 len
, GetLastError(), buffer
);
63 memset(buffer
, 'x', maxsize
);
64 buffer
[maxsize
] = '\0';
65 SetLastError(0xdeadbeef);
66 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, buffer
, needed
-1, NULL
, NULL
);
67 ok( !len
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
68 "returned %d with %u and '%s' (expected '0' with "
69 "ERROR_INSUFFICIENT_BUFFER)\n", len
, GetLastError(), buffer
);
71 memset(buffer
, 'x', maxsize
);
72 buffer
[maxsize
] = '\0';
73 SetLastError(0xdeadbeef);
74 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, buffer
, 1, NULL
, NULL
);
75 ok( !len
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
76 "returned %d with %u and '%s' (expected '0' with "
77 "ERROR_INSUFFICIENT_BUFFER)\n", len
, GetLastError(), buffer
);
79 SetLastError(0xdeadbeef);
80 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, buffer
, 0, NULL
, NULL
);
81 ok( (len
> 0), "returned %d with %u (expected '> 0')\n",
84 SetLastError(0xdeadbeef);
85 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, NULL
, needed
, NULL
, NULL
);
86 ok( !len
&& (GetLastError() == ERROR_INVALID_PARAMETER
),
87 "returned %d with %u (expected '0' with "
88 "ERROR_INVALID_PARAMETER)\n", len
, GetLastError());
90 HeapFree(GetProcessHeap(), 0, buffer
);
94 static void test_null_source(void)
100 len
= WideCharToMultiByte(CP_ACP
, 0, NULL
, 0, NULL
, 0, NULL
, NULL
);
101 GLE
= GetLastError();
102 ok(!len
&& GLE
== ERROR_INVALID_PARAMETER
,
103 "WideCharToMultiByte returned %d with GLE=%u (expected 0 with ERROR_INVALID_PARAMETER)\n",
107 len
= WideCharToMultiByte(CP_ACP
, 0, NULL
, -1, NULL
, 0, NULL
, NULL
);
108 GLE
= GetLastError();
109 ok(!len
&& GLE
== ERROR_INVALID_PARAMETER
,
110 "WideCharToMultiByte returned %d with GLE=%u (expected 0 with ERROR_INVALID_PARAMETER)\n",
114 static void test_negative_source_length(void)
120 /* Test, whether any negative source length works as strlen() + 1 */
121 SetLastError( 0xdeadbeef );
122 memset(buf
,'x',sizeof(buf
));
123 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -2002, buf
, 10, NULL
, NULL
);
124 ok(len
== 7 && GetLastError() == 0xdeadbeef,
125 "WideCharToMultiByte(-2002): len=%d error=%u\n", len
, GetLastError());
126 ok(!lstrcmpA(buf
, "foobar"),
127 "WideCharToMultiByte(-2002): expected \"foobar\" got \"%s\"\n", buf
);
129 SetLastError( 0xdeadbeef );
130 memset(bufW
,'x',sizeof(bufW
));
131 len
= MultiByteToWideChar(CP_ACP
, 0, "foobar", -2002, bufW
, 10);
132 ok(len
== 7 && !lstrcmpW(bufW
, foobarW
) && GetLastError() == 0xdeadbeef,
133 "MultiByteToWideChar(-2002): len=%d error=%u\n", len
, GetLastError());
135 SetLastError(0xdeadbeef);
136 memset(bufW
, 'x', sizeof(bufW
));
137 len
= MultiByteToWideChar(CP_ACP
, 0, "foobar", -1, bufW
, 6);
138 ok(len
== 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
139 "MultiByteToWideChar(-1): len=%d error=%u\n", len
, GetLastError());
142 #define LONGBUFLEN 100000
143 static void test_negative_dest_length(void)
146 static char buf
[LONGBUFLEN
];
147 static WCHAR originalW
[LONGBUFLEN
];
148 static char originalA
[LONGBUFLEN
];
151 /* Test return on -1 dest length */
152 SetLastError( 0xdeadbeef );
153 memset(buf
,'x',sizeof(buf
));
154 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, buf
, -1, NULL
, NULL
);
156 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
,
157 "WideCharToMultiByte(destlen -1): len=%d error=%x\n", len
, GetLastError());
160 /* Test return on -1000 dest length */
161 SetLastError( 0xdeadbeef );
162 memset(buf
,'x',sizeof(buf
));
163 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, buf
, -1000, NULL
, NULL
);
165 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
,
166 "WideCharToMultiByte(destlen -1000): len=%d error=%x\n", len
, GetLastError());
169 /* Test return on INT_MAX dest length */
170 SetLastError( 0xdeadbeef );
171 memset(buf
,'x',sizeof(buf
));
172 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, buf
, INT_MAX
, NULL
, NULL
);
173 ok(len
== 7 && !lstrcmpA(buf
, "foobar") && GetLastError() == 0xdeadbeef,
174 "WideCharToMultiByte(destlen INT_MAX): len=%d error=%x\n", len
, GetLastError());
176 /* Test return on INT_MAX dest length and very long input */
177 SetLastError( 0xdeadbeef );
178 memset(buf
,'x',sizeof(buf
));
179 for (i
=0; i
< LONGBUFLEN
- 1; i
++) {
183 originalW
[LONGBUFLEN
-1] = 0;
184 originalA
[LONGBUFLEN
-1] = 0;
185 len
= WideCharToMultiByte(CP_ACP
, 0, originalW
, -1, buf
, INT_MAX
, NULL
, NULL
);
186 theError
= GetLastError();
187 ok(len
== LONGBUFLEN
&& !lstrcmpA(buf
, originalA
) && theError
== 0xdeadbeef,
188 "WideCharToMultiByte(srclen %d, destlen INT_MAX): len %d error=%x\n", LONGBUFLEN
, len
, theError
);
192 static void test_overlapped_buffers(void)
194 static const WCHAR strW
[] = {'j','u','s','t',' ','a',' ','t','e','s','t',0};
195 static const char strA
[] = "just a test";
199 SetLastError(0xdeadbeef);
200 memcpy(buf
+ 1, strW
, sizeof(strW
));
201 ret
= WideCharToMultiByte(CP_ACP
, 0, (WCHAR
*)(buf
+ 1), -1, buf
, sizeof(buf
), NULL
, NULL
);
202 ok(ret
== sizeof(strA
), "unexpected ret %d\n", ret
);
203 ok(!memcmp(buf
, strA
, sizeof(strA
)), "conversion failed: %s\n", buf
);
204 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
207 static void test_string_conversion(LPBOOL bUsedDefaultChar
)
212 WCHAR wc1
= 228; /* Western Windows-1252 character */
213 WCHAR wc2
= 1088; /* Russian Windows-1251 character not displayable for Windows-1252 */
214 static const WCHAR wcs
[] = {'T', 'h', 1088, 'i', 0}; /* String with ASCII characters and a Russian character */
215 static const WCHAR dbwcs
[] = {28953, 25152, 0}; /* String with Chinese (codepage 950) characters */
217 SetLastError(0xdeadbeef);
218 ret
= WideCharToMultiByte(1252, 0, &wc1
, 1, &mbc
, 1, NULL
, bUsedDefaultChar
);
219 ok(ret
== 1, "ret is %d\n", ret
);
220 ok(mbc
== -28, "mbc is %d\n", mbc
);
221 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
222 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
224 SetLastError(0xdeadbeef);
225 ret
= WideCharToMultiByte(1252, 0, &wc2
, 1, &mbc
, 1, NULL
, bUsedDefaultChar
);
226 ok(ret
== 1, "ret is %d\n", ret
);
227 ok(mbc
== 63, "mbc is %d\n", mbc
);
228 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== TRUE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
229 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
231 if (IsValidCodePage(1251))
233 SetLastError(0xdeadbeef);
234 ret
= WideCharToMultiByte(1251, 0, &wc2
, 1, &mbc
, 1, NULL
, bUsedDefaultChar
);
235 ok(ret
== 1, "ret is %d\n", ret
);
236 ok(mbc
== -16, "mbc is %d\n", mbc
);
237 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
238 ok(GetLastError() == 0xdeadbeef ||
239 broken(GetLastError() == 0), /* win95 */
240 "GetLastError() is %u\n", GetLastError());
242 SetLastError(0xdeadbeef);
243 ret
= WideCharToMultiByte(1251, 0, &wc1
, 1, &mbc
, 1, NULL
, bUsedDefaultChar
);
244 ok(ret
== 1, "ret is %d\n", ret
);
245 ok(mbc
== 97, "mbc is %d\n", mbc
);
246 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
247 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
250 skip("Codepage 1251 not available\n");
252 /* This call triggers the last Win32 error */
253 SetLastError(0xdeadbeef);
254 ret
= WideCharToMultiByte(1252, 0, wcs
, -1, &mbc
, 1, NULL
, bUsedDefaultChar
);
255 ok(ret
== 0, "ret is %d\n", ret
);
256 ok(mbc
== 84, "mbc is %d\n", mbc
);
257 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
258 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetLastError() is %u\n", GetLastError());
260 SetLastError(0xdeadbeef);
261 ret
= WideCharToMultiByte(1252, 0, wcs
, -1, mbs
, sizeof(mbs
), NULL
, bUsedDefaultChar
);
262 ok(ret
== 5, "ret is %d\n", ret
);
263 ok(!strcmp(mbs
, "Th?i"), "mbs is %s\n", mbs
);
264 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== TRUE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
265 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
268 /* WideCharToMultiByte mustn't add any null character automatically.
269 So in this case, we should get the same string again, even if we only copied the first three bytes. */
270 SetLastError(0xdeadbeef);
271 ret
= WideCharToMultiByte(1252, 0, wcs
, 3, mbs
, sizeof(mbs
), NULL
, bUsedDefaultChar
);
272 ok(ret
== 3, "ret is %d\n", ret
);
273 ok(!strcmp(mbs
, "Th?i"), "mbs is %s\n", mbs
);
274 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== TRUE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
275 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
278 /* Now this shouldn't be the case like above as we zeroed the complete string buffer. */
279 SetLastError(0xdeadbeef);
280 ret
= WideCharToMultiByte(1252, 0, wcs
, 3, mbs
, sizeof(mbs
), NULL
, bUsedDefaultChar
);
281 ok(ret
== 3, "ret is %d\n", ret
);
282 ok(!strcmp(mbs
, "Th?"), "mbs is %s\n", mbs
);
283 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== TRUE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
284 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
286 /* Double-byte tests */
287 ret
= WideCharToMultiByte(1252, 0, dbwcs
, 3, mbs
, sizeof(mbs
), NULL
, bUsedDefaultChar
);
288 ok(ret
== 3, "ret is %d\n", ret
);
289 ok(!strcmp(mbs
, "??"), "mbs is %s\n", mbs
);
290 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== TRUE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
292 /* Length-only tests */
293 SetLastError(0xdeadbeef);
294 ret
= WideCharToMultiByte(1252, 0, &wc2
, 1, NULL
, 0, NULL
, bUsedDefaultChar
);
295 ok(ret
== 1, "ret is %d\n", ret
);
296 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== TRUE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
297 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
299 SetLastError(0xdeadbeef);
300 ret
= WideCharToMultiByte(1252, 0, wcs
, -1, NULL
, 0, NULL
, bUsedDefaultChar
);
301 ok(ret
== 5, "ret is %d\n", ret
);
302 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== TRUE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
303 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
305 if (!IsValidCodePage(950))
307 skip("Codepage 950 not available\n");
311 /* Double-byte tests */
312 SetLastError(0xdeadbeef);
313 ret
= WideCharToMultiByte(950, 0, dbwcs
, -1, mbs
, sizeof(mbs
), NULL
, bUsedDefaultChar
);
314 ok(ret
== 5, "ret is %d\n", ret
);
315 ok(!strcmp(mbs
, "\xb5H\xa9\xd2"), "mbs is %s\n", mbs
);
316 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
317 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
319 SetLastError(0xdeadbeef);
320 ret
= WideCharToMultiByte(950, 0, dbwcs
, 1, &mbc
, 1, NULL
, bUsedDefaultChar
);
321 ok(ret
== 0, "ret is %d\n", ret
);
322 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
323 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetLastError() is %u\n", GetLastError());
326 SetLastError(0xdeadbeef);
327 ret
= WideCharToMultiByte(950, 0, dbwcs
, 1, mbs
, sizeof(mbs
), NULL
, bUsedDefaultChar
);
328 ok(ret
== 2, "ret is %d\n", ret
);
329 ok(!strcmp(mbs
, "\xb5H"), "mbs is %s\n", mbs
);
330 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
331 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
333 /* Length-only tests */
334 SetLastError(0xdeadbeef);
335 ret
= WideCharToMultiByte(950, 0, dbwcs
, 1, NULL
, 0, NULL
, bUsedDefaultChar
);
336 ok(ret
== 2, "ret is %d\n", ret
);
337 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
338 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
340 SetLastError(0xdeadbeef);
341 ret
= WideCharToMultiByte(950, 0, dbwcs
, -1, NULL
, 0, NULL
, bUsedDefaultChar
);
342 ok(ret
== 5, "ret is %d\n", ret
);
343 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
344 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
347 static void test_undefined_byte_char(void)
349 static const struct tag_testset
{
354 { 874, "\xdd", TRUE
},
355 { 932, "\xfe", TRUE
},
356 { 932, "\x80", FALSE
},
357 { 936, "\xff", TRUE
},
358 { 949, "\xff", TRUE
},
359 { 950, "\xff", TRUE
},
360 { 1252, "\x90", FALSE
},
361 { 1253, "\xaa", TRUE
},
362 { 1255, "\xff", TRUE
},
363 { 1257, "\xa5", TRUE
},
367 for (i
= 0; i
< (sizeof(testset
) / sizeof(testset
[0])); i
++) {
368 if (! IsValidCodePage(testset
[i
].codepage
))
370 skip("Codepage %d not available\n", testset
[i
].codepage
);
374 SetLastError(0xdeadbeef);
375 ret
= MultiByteToWideChar(testset
[i
].codepage
, MB_ERR_INVALID_CHARS
,
376 testset
[i
].str
, -1, NULL
, 0);
377 if (testset
[i
].is_error
) {
378 ok(ret
== 0 && GetLastError() == ERROR_NO_UNICODE_TRANSLATION
,
379 "ret is %d, GetLastError is %u (cp %d)\n",
380 ret
, GetLastError(), testset
[i
].codepage
);
383 ok(ret
== strlen(testset
[i
].str
)+1 && GetLastError() == 0xdeadbeef,
384 "ret is %d, GetLastError is %u (cp %d)\n",
385 ret
, GetLastError(), testset
[i
].codepage
);
388 SetLastError(0xdeadbeef);
389 ret
= MultiByteToWideChar(testset
[i
].codepage
, 0,
390 testset
[i
].str
, -1, NULL
, 0);
391 ok(ret
== strlen(testset
[i
].str
)+1 && GetLastError() == 0xdeadbeef,
392 "ret is %d, GetLastError is %u (cp %d)\n",
393 ret
, GetLastError(), testset
[i
].codepage
);
399 BOOL bUsedDefaultChar
;
401 test_destination_buffer();
403 test_negative_source_length();
404 test_negative_dest_length();
405 test_overlapped_buffers();
407 /* WideCharToMultiByte has two code paths, test both here */
408 test_string_conversion(NULL
);
409 test_string_conversion(&bUsedDefaultChar
);
411 test_undefined_byte_char();