36ea66dbaa2d8b9b7b1b092f49eb20f0e57bf170
[reactos.git] / rostests / winetests / kernel32 / codepage.c
1 /*
2 * Unit tests for code page to/from unicode translations
3 *
4 * Copyright (c) 2002 Dmitry Timoshkov
5 * Copyright (c) 2008 Colin Finck
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <stdarg.h>
23 #include <limits.h>
24
25 #include "wine/test.h"
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29
30 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
31
32 static void test_destination_buffer(void)
33 {
34 LPSTR buffer;
35 INT maxsize;
36 INT needed;
37 INT len;
38
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());
43
44 maxsize = needed*2;
45 buffer = HeapAlloc(GetProcessHeap(), 0, maxsize);
46 if (buffer == NULL) return;
47
48 maxsize--;
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);
55
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);
62
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);
70
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);
78
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",
82 len, GetLastError());
83
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());
89
90 HeapFree(GetProcessHeap(), 0, buffer);
91 }
92
93
94 static void test_null_source(void)
95 {
96 int len;
97 DWORD GLE;
98
99 SetLastError(0);
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",
104 len, GLE);
105
106 SetLastError(0);
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",
111 len, GLE);
112 }
113
114 static void test_negative_source_length(void)
115 {
116 int len;
117 char buf[10];
118 WCHAR bufW[10];
119
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);
128
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());
134
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());
140 }
141
142 #define LONGBUFLEN 100000
143 static void test_negative_dest_length(void)
144 {
145 int len, i;
146 static char buf[LONGBUFLEN];
147 static WCHAR originalW[LONGBUFLEN];
148 static char originalA[LONGBUFLEN];
149 DWORD theError;
150
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);
155 todo_wine {
156 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
157 "WideCharToMultiByte(destlen -1): len=%d error=%x\n", len, GetLastError());
158 }
159
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);
164 todo_wine {
165 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
166 "WideCharToMultiByte(destlen -1000): len=%d error=%x\n", len, GetLastError());
167 }
168
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());
175
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++) {
180 originalW[i] = 'Q';
181 originalA[i] = 'Q';
182 }
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);
189
190 }
191
192 static void test_other_invalid_parameters(void)
193 {
194 char c_string[] = "Hello World";
195 size_t c_string_len = sizeof(c_string);
196 WCHAR w_string[] = {'H','e','l','l','o',' ','W','o','r','l','d',0};
197 size_t w_string_len = sizeof(w_string) / sizeof(WCHAR);
198 BOOL used;
199 INT len;
200
201 /* srclen=0 => ERROR_INVALID_PARAMETER */
202 SetLastError(0xdeadbeef);
203 len = WideCharToMultiByte(CP_ACP, 0, w_string, 0, c_string, c_string_len, NULL, NULL);
204 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
205
206 SetLastError(0xdeadbeef);
207 len = MultiByteToWideChar(CP_ACP, 0, c_string, 0, w_string, w_string_len);
208 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
209
210
211 /* dst=NULL but dstlen not 0 => ERROR_INVALID_PARAMETER */
212 SetLastError(0xdeadbeef);
213 len = WideCharToMultiByte(CP_ACP, 0, w_string, w_string_len, NULL, c_string_len, NULL, NULL);
214 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
215
216 SetLastError(0xdeadbeef);
217 len = MultiByteToWideChar(CP_ACP, 0, c_string, c_string_len, NULL, w_string_len);
218 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
219
220
221 /* CP_UTF7, CP_UTF8, or CP_SYMBOL and defchar not NULL => ERROR_INVALID_PARAMETER */
222 /* CP_SYMBOL's behavior here is undocumented */
223 SetLastError(0xdeadbeef);
224 len = WideCharToMultiByte(CP_UTF7, 0, w_string, w_string_len, c_string, c_string_len, c_string, NULL);
225 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
226
227 SetLastError(0xdeadbeef);
228 len = WideCharToMultiByte(CP_UTF8, 0, w_string, w_string_len, c_string, c_string_len, c_string, NULL);
229 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
230
231 SetLastError(0xdeadbeef);
232 len = WideCharToMultiByte(CP_SYMBOL, 0, w_string, w_string_len, c_string, c_string_len, c_string, NULL);
233 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
234
235
236 /* CP_UTF7, CP_UTF8, or CP_SYMBOL and used not NULL => ERROR_INVALID_PARAMETER */
237 /* CP_SYMBOL's behavior here is undocumented */
238 SetLastError(0xdeadbeef);
239 len = WideCharToMultiByte(CP_UTF7, 0, w_string, w_string_len, c_string, c_string_len, NULL, &used);
240 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
241
242 SetLastError(0xdeadbeef);
243 len = WideCharToMultiByte(CP_UTF8, 0, w_string, w_string_len, c_string, c_string_len, NULL, &used);
244 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
245
246 SetLastError(0xdeadbeef);
247 len = WideCharToMultiByte(CP_SYMBOL, 0, w_string, w_string_len, c_string, c_string_len, NULL, &used);
248 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
249
250
251 /* CP_UTF7, flags not 0 and used not NULL => ERROR_INVALID_PARAMETER */
252 /* (tests precedence of ERROR_INVALID_PARAMETER over ERROR_INVALID_FLAGS) */
253 /* The same test with CP_SYMBOL instead of CP_UTF7 gives ERROR_INVALID_FLAGS
254 instead except on Windows NT4 */
255 SetLastError(0xdeadbeef);
256 len = WideCharToMultiByte(CP_UTF7, 1, w_string, w_string_len, c_string, c_string_len, NULL, &used);
257 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
258 }
259
260 static void test_overlapped_buffers(void)
261 {
262 static const WCHAR strW[] = {'j','u','s','t',' ','a',' ','t','e','s','t',0};
263 static const char strA[] = "just a test";
264 char buf[256];
265 int ret;
266
267 SetLastError(0xdeadbeef);
268 memcpy(buf + 1, strW, sizeof(strW));
269 ret = WideCharToMultiByte(CP_ACP, 0, (WCHAR *)(buf + 1), -1, buf, sizeof(buf), NULL, NULL);
270 ok(ret == sizeof(strA), "unexpected ret %d\n", ret);
271 ok(!memcmp(buf, strA, sizeof(strA)), "conversion failed: %s\n", buf);
272 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
273 }
274
275 static void test_string_conversion(LPBOOL bUsedDefaultChar)
276 {
277 char mbc;
278 char mbs[5];
279 int ret;
280 WCHAR wc1 = 228; /* Western Windows-1252 character */
281 WCHAR wc2 = 1088; /* Russian Windows-1251 character not displayable for Windows-1252 */
282 static const WCHAR wcs[] = {'T', 'h', 1088, 'i', 0}; /* String with ASCII characters and a Russian character */
283 static const WCHAR dbwcs[] = {28953, 25152, 0}; /* String with Chinese (codepage 950) characters */
284
285 SetLastError(0xdeadbeef);
286 ret = WideCharToMultiByte(1252, 0, &wc1, 1, &mbc, 1, NULL, bUsedDefaultChar);
287 ok(ret == 1, "ret is %d\n", ret);
288 ok(mbc == '\xe4', "mbc is %d\n", mbc);
289 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
290 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
291
292 SetLastError(0xdeadbeef);
293 ret = WideCharToMultiByte(1252, 0, &wc2, 1, &mbc, 1, NULL, bUsedDefaultChar);
294 ok(ret == 1, "ret is %d\n", ret);
295 ok(mbc == 63, "mbc is %d\n", mbc);
296 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
297 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
298
299 if (IsValidCodePage(1251))
300 {
301 SetLastError(0xdeadbeef);
302 ret = WideCharToMultiByte(1251, 0, &wc2, 1, &mbc, 1, NULL, bUsedDefaultChar);
303 ok(ret == 1, "ret is %d\n", ret);
304 ok(mbc == '\xf0', "mbc is %d\n", mbc);
305 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
306 ok(GetLastError() == 0xdeadbeef ||
307 broken(GetLastError() == 0), /* win95 */
308 "GetLastError() is %u\n", GetLastError());
309
310 SetLastError(0xdeadbeef);
311 ret = WideCharToMultiByte(1251, 0, &wc1, 1, &mbc, 1, NULL, bUsedDefaultChar);
312 ok(ret == 1, "ret is %d\n", ret);
313 ok(mbc == 97, "mbc is %d\n", mbc);
314 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
315 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
316 }
317 else
318 skip("Codepage 1251 not available\n");
319
320 /* This call triggers the last Win32 error */
321 SetLastError(0xdeadbeef);
322 ret = WideCharToMultiByte(1252, 0, wcs, -1, &mbc, 1, NULL, bUsedDefaultChar);
323 ok(ret == 0, "ret is %d\n", ret);
324 ok(mbc == 84, "mbc is %d\n", mbc);
325 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
326 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() is %u\n", GetLastError());
327
328 SetLastError(0xdeadbeef);
329 ret = WideCharToMultiByte(1252, 0, wcs, -1, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
330 ok(ret == 5, "ret is %d\n", ret);
331 ok(!strcmp(mbs, "Th?i"), "mbs is %s\n", mbs);
332 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
333 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
334 mbs[0] = 0;
335
336 /* WideCharToMultiByte mustn't add any null character automatically.
337 So in this case, we should get the same string again, even if we only copied the first three bytes. */
338 SetLastError(0xdeadbeef);
339 ret = WideCharToMultiByte(1252, 0, wcs, 3, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
340 ok(ret == 3, "ret is %d\n", ret);
341 ok(!strcmp(mbs, "Th?i"), "mbs is %s\n", mbs);
342 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
343 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
344 ZeroMemory(mbs, 5);
345
346 /* Now this shouldn't be the case like above as we zeroed the complete string buffer. */
347 SetLastError(0xdeadbeef);
348 ret = WideCharToMultiByte(1252, 0, wcs, 3, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
349 ok(ret == 3, "ret is %d\n", ret);
350 ok(!strcmp(mbs, "Th?"), "mbs is %s\n", mbs);
351 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
352 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
353
354 /* Double-byte tests */
355 ret = WideCharToMultiByte(1252, 0, dbwcs, 3, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
356 ok(ret == 3, "ret is %d\n", ret);
357 ok(!strcmp(mbs, "??"), "mbs is %s\n", mbs);
358 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
359
360 /* Length-only tests */
361 SetLastError(0xdeadbeef);
362 ret = WideCharToMultiByte(1252, 0, &wc2, 1, NULL, 0, NULL, bUsedDefaultChar);
363 ok(ret == 1, "ret is %d\n", ret);
364 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
365 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
366
367 SetLastError(0xdeadbeef);
368 ret = WideCharToMultiByte(1252, 0, wcs, -1, NULL, 0, NULL, bUsedDefaultChar);
369 ok(ret == 5, "ret is %d\n", ret);
370 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
371 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
372
373 if (!IsValidCodePage(950))
374 {
375 skip("Codepage 950 not available\n");
376 return;
377 }
378
379 /* Double-byte tests */
380 SetLastError(0xdeadbeef);
381 ret = WideCharToMultiByte(950, 0, dbwcs, -1, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
382 ok(ret == 5, "ret is %d\n", ret);
383 ok(!strcmp(mbs, "\xb5H\xa9\xd2"), "mbs is %s\n", mbs);
384 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
385 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
386
387 SetLastError(0xdeadbeef);
388 ret = WideCharToMultiByte(950, 0, dbwcs, 1, &mbc, 1, NULL, bUsedDefaultChar);
389 ok(ret == 0, "ret is %d\n", ret);
390 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
391 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() is %u\n", GetLastError());
392 ZeroMemory(mbs, 5);
393
394 SetLastError(0xdeadbeef);
395 ret = WideCharToMultiByte(950, 0, dbwcs, 1, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
396 ok(ret == 2, "ret is %d\n", ret);
397 ok(!strcmp(mbs, "\xb5H"), "mbs is %s\n", mbs);
398 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
399 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
400
401 /* Length-only tests */
402 SetLastError(0xdeadbeef);
403 ret = WideCharToMultiByte(950, 0, dbwcs, 1, NULL, 0, NULL, bUsedDefaultChar);
404 ok(ret == 2, "ret is %d\n", ret);
405 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
406 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
407
408 SetLastError(0xdeadbeef);
409 ret = WideCharToMultiByte(950, 0, dbwcs, -1, NULL, 0, NULL, bUsedDefaultChar);
410 ok(ret == 5, "ret is %d\n", ret);
411 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
412 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
413 }
414
415 static void test_undefined_byte_char(void)
416 {
417 static const struct tag_testset {
418 INT codepage;
419 LPCSTR str;
420 BOOL is_error;
421 } testset[] = {
422 { 874, "\xdd", TRUE },
423 { 932, "\xfe", TRUE },
424 { 932, "\x80", FALSE },
425 { 936, "\xff", TRUE },
426 { 949, "\xff", TRUE },
427 { 950, "\xff", TRUE },
428 { 1252, "\x90", FALSE },
429 { 1253, "\xaa", TRUE },
430 { 1255, "\xff", TRUE },
431 { 1257, "\xa5", TRUE },
432 };
433 INT i, ret;
434
435 for (i = 0; i < (sizeof(testset) / sizeof(testset[0])); i++) {
436 if (! IsValidCodePage(testset[i].codepage))
437 {
438 skip("Codepage %d not available\n", testset[i].codepage);
439 continue;
440 }
441
442 SetLastError(0xdeadbeef);
443 ret = MultiByteToWideChar(testset[i].codepage, MB_ERR_INVALID_CHARS,
444 testset[i].str, -1, NULL, 0);
445 if (testset[i].is_error) {
446 ok(ret == 0 && GetLastError() == ERROR_NO_UNICODE_TRANSLATION,
447 "ret is %d, GetLastError is %u (cp %d)\n",
448 ret, GetLastError(), testset[i].codepage);
449 }
450 else {
451 ok(ret == strlen(testset[i].str)+1 && GetLastError() == 0xdeadbeef,
452 "ret is %d, GetLastError is %u (cp %d)\n",
453 ret, GetLastError(), testset[i].codepage);
454 }
455
456 SetLastError(0xdeadbeef);
457 ret = MultiByteToWideChar(testset[i].codepage, 0,
458 testset[i].str, -1, NULL, 0);
459 ok(ret == strlen(testset[i].str)+1 && GetLastError() == 0xdeadbeef,
460 "ret is %d, GetLastError is %u (cp %d)\n",
461 ret, GetLastError(), testset[i].codepage);
462 }
463 }
464
465 START_TEST(codepage)
466 {
467 BOOL bUsedDefaultChar;
468
469 test_destination_buffer();
470 test_null_source();
471 test_negative_source_length();
472 test_negative_dest_length();
473 test_other_invalid_parameters();
474 test_overlapped_buffers();
475
476 /* WideCharToMultiByte has two code paths, test both here */
477 test_string_conversion(NULL);
478 test_string_conversion(&bUsedDefaultChar);
479
480 test_undefined_byte_char();
481 }