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
26 #include "wine/test.h"
31 static const char foobarA
[] = "foobar";
32 static const WCHAR foobarW
[] = {'f','o','o','b','a','r',0};
34 static void test_destination_buffer(void)
41 SetLastError(0xdeadbeef);
42 needed
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, NULL
, 0, NULL
, NULL
);
43 ok( (needed
> 0), "returned %d with %u (expected '> 0')\n",
44 needed
, GetLastError());
47 buffer
= HeapAlloc(GetProcessHeap(), 0, maxsize
);
48 if (buffer
== NULL
) return;
51 memset(buffer
, 'x', maxsize
);
52 buffer
[maxsize
] = '\0';
53 SetLastError(0xdeadbeef);
54 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, buffer
, needed
+1, NULL
, NULL
);
55 ok( (len
> 0), "returned %d with %u and '%s' (expected '> 0')\n",
56 len
, GetLastError(), buffer
);
58 memset(buffer
, 'x', maxsize
);
59 buffer
[maxsize
] = '\0';
60 SetLastError(0xdeadbeef);
61 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, buffer
, needed
, NULL
, NULL
);
62 ok( (len
> 0), "returned %d with %u and '%s' (expected '> 0')\n",
63 len
, GetLastError(), buffer
);
65 memset(buffer
, 'x', maxsize
);
66 buffer
[maxsize
] = '\0';
67 SetLastError(0xdeadbeef);
68 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, buffer
, needed
-1, NULL
, NULL
);
69 ok( !len
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
70 "returned %d with %u and '%s' (expected '0' with "
71 "ERROR_INSUFFICIENT_BUFFER)\n", len
, GetLastError(), buffer
);
73 memset(buffer
, 'x', maxsize
);
74 buffer
[maxsize
] = '\0';
75 SetLastError(0xdeadbeef);
76 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, buffer
, 1, NULL
, NULL
);
77 ok( !len
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
78 "returned %d with %u and '%s' (expected '0' with "
79 "ERROR_INSUFFICIENT_BUFFER)\n", len
, GetLastError(), buffer
);
81 SetLastError(0xdeadbeef);
82 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, buffer
, 0, NULL
, NULL
);
83 ok( (len
> 0), "returned %d with %u (expected '> 0')\n",
86 SetLastError(0xdeadbeef);
87 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, NULL
, needed
, NULL
, NULL
);
88 ok( !len
&& (GetLastError() == ERROR_INVALID_PARAMETER
),
89 "returned %d with %u (expected '0' with "
90 "ERROR_INVALID_PARAMETER)\n", len
, GetLastError());
92 HeapFree(GetProcessHeap(), 0, buffer
);
96 static void test_null_source(void)
102 len
= WideCharToMultiByte(CP_ACP
, 0, NULL
, 0, NULL
, 0, NULL
, NULL
);
103 GLE
= GetLastError();
104 ok(!len
&& GLE
== ERROR_INVALID_PARAMETER
,
105 "WideCharToMultiByte returned %d with GLE=%u (expected 0 with ERROR_INVALID_PARAMETER)\n",
109 len
= WideCharToMultiByte(CP_ACP
, 0, NULL
, -1, NULL
, 0, NULL
, NULL
);
110 GLE
= GetLastError();
111 ok(!len
&& GLE
== ERROR_INVALID_PARAMETER
,
112 "WideCharToMultiByte returned %d with GLE=%u (expected 0 with ERROR_INVALID_PARAMETER)\n",
116 static void test_negative_source_length(void)
122 /* Test, whether any negative source length works as strlen() + 1 */
123 SetLastError( 0xdeadbeef );
124 memset(buf
,'x',sizeof(buf
));
125 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -2002, buf
, 10, NULL
, NULL
);
126 ok(len
== 7 && GetLastError() == 0xdeadbeef,
127 "WideCharToMultiByte(-2002): len=%d error=%u\n", len
, GetLastError());
128 ok(!lstrcmpA(buf
, "foobar"),
129 "WideCharToMultiByte(-2002): expected \"foobar\" got \"%s\"\n", buf
);
131 SetLastError( 0xdeadbeef );
132 memset(bufW
,'x',sizeof(bufW
));
133 len
= MultiByteToWideChar(CP_ACP
, 0, "foobar", -2002, bufW
, 10);
134 ok(len
== 7 && !lstrcmpW(bufW
, foobarW
) && GetLastError() == 0xdeadbeef,
135 "MultiByteToWideChar(-2002): len=%d error=%u\n", len
, GetLastError());
137 SetLastError(0xdeadbeef);
138 memset(bufW
, 'x', sizeof(bufW
));
139 len
= MultiByteToWideChar(CP_ACP
, 0, "foobar", -1, bufW
, 6);
140 ok(len
== 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
141 "MultiByteToWideChar(-1): len=%d error=%u\n", len
, GetLastError());
144 #define LONGBUFLEN 100000
145 static void test_negative_dest_length(void)
148 static WCHAR bufW
[LONGBUFLEN
];
149 static char bufA
[LONGBUFLEN
];
150 static WCHAR originalW
[LONGBUFLEN
];
151 static char originalA
[LONGBUFLEN
];
154 /* Test return on -1 dest length */
155 SetLastError( 0xdeadbeef );
156 memset(bufA
,'x',sizeof(bufA
));
157 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, bufA
, -1, NULL
, NULL
);
158 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
,
159 "WideCharToMultiByte(destlen -1): len=%d error=%x\n", len
, GetLastError());
161 SetLastError( 0xdeadbeef );
162 memset(bufW
,'x',sizeof(bufW
));
163 len
= MultiByteToWideChar(CP_ACP
, 0, foobarA
, -1, bufW
, -1);
164 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
,
165 "MultiByteToWideChar(destlen -1): len=%d error=%x\n", len
, GetLastError());
167 /* Test return on -1000 dest length */
168 SetLastError( 0xdeadbeef );
169 memset(bufA
,'x',sizeof(bufA
));
170 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, bufA
, -1000, NULL
, NULL
);
171 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
,
172 "WideCharToMultiByte(destlen -1000): len=%d error=%x\n", len
, GetLastError());
174 SetLastError( 0xdeadbeef );
175 memset(bufW
,'x',sizeof(bufW
));
176 len
= MultiByteToWideChar(CP_ACP
, 0, foobarA
, -1000, bufW
, -1);
177 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
,
178 "MultiByteToWideChar(destlen -1000): len=%d error=%x\n", len
, GetLastError());
180 /* Test return on INT_MAX dest length */
181 SetLastError( 0xdeadbeef );
182 memset(bufA
,'x',sizeof(bufA
));
183 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, bufA
, INT_MAX
, NULL
, NULL
);
184 ok(len
== 7 && !lstrcmpA(bufA
, "foobar") && GetLastError() == 0xdeadbeef,
185 "WideCharToMultiByte(destlen INT_MAX): len=%d error=%x\n", len
, GetLastError());
187 /* Test return on INT_MAX dest length and very long input */
188 SetLastError( 0xdeadbeef );
189 memset(bufA
,'x',sizeof(bufA
));
190 for (i
=0; i
< LONGBUFLEN
- 1; i
++) {
194 originalW
[LONGBUFLEN
-1] = 0;
195 originalA
[LONGBUFLEN
-1] = 0;
196 len
= WideCharToMultiByte(CP_ACP
, 0, originalW
, -1, bufA
, INT_MAX
, NULL
, NULL
);
197 theError
= GetLastError();
198 ok(len
== LONGBUFLEN
&& !lstrcmpA(bufA
, originalA
) && theError
== 0xdeadbeef,
199 "WideCharToMultiByte(srclen %d, destlen INT_MAX): len %d error=%x\n", LONGBUFLEN
, len
, theError
);
203 static void test_other_invalid_parameters(void)
205 char c_string
[] = "Hello World";
206 size_t c_string_len
= sizeof(c_string
);
207 WCHAR w_string
[] = {'H','e','l','l','o',' ','W','o','r','l','d',0};
208 size_t w_string_len
= sizeof(w_string
) / sizeof(WCHAR
);
212 /* srclen=0 => ERROR_INVALID_PARAMETER */
213 SetLastError(0xdeadbeef);
214 len
= WideCharToMultiByte(CP_ACP
, 0, w_string
, 0, c_string
, c_string_len
, NULL
, NULL
);
215 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
, "len=%d error=%x\n", len
, GetLastError());
217 SetLastError(0xdeadbeef);
218 len
= MultiByteToWideChar(CP_ACP
, 0, c_string
, 0, w_string
, w_string_len
);
219 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
, "len=%d error=%x\n", len
, GetLastError());
222 /* dst=NULL but dstlen not 0 => ERROR_INVALID_PARAMETER */
223 SetLastError(0xdeadbeef);
224 len
= WideCharToMultiByte(CP_ACP
, 0, w_string
, w_string_len
, NULL
, c_string_len
, NULL
, NULL
);
225 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
, "len=%d error=%x\n", len
, GetLastError());
227 SetLastError(0xdeadbeef);
228 len
= MultiByteToWideChar(CP_ACP
, 0, c_string
, c_string_len
, NULL
, w_string_len
);
229 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
, "len=%d error=%x\n", len
, GetLastError());
232 /* CP_UTF7, CP_UTF8, or CP_SYMBOL and defchar not NULL => ERROR_INVALID_PARAMETER */
233 /* CP_SYMBOL's behavior here is undocumented */
234 SetLastError(0xdeadbeef);
235 len
= WideCharToMultiByte(CP_UTF7
, 0, w_string
, w_string_len
, c_string
, c_string_len
, c_string
, NULL
);
236 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
, "len=%d error=%x\n", len
, GetLastError());
238 SetLastError(0xdeadbeef);
239 len
= WideCharToMultiByte(CP_UTF8
, 0, w_string
, w_string_len
, c_string
, c_string_len
, c_string
, NULL
);
240 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
, "len=%d error=%x\n", len
, GetLastError());
242 SetLastError(0xdeadbeef);
243 len
= WideCharToMultiByte(CP_SYMBOL
, 0, w_string
, w_string_len
, c_string
, c_string_len
, c_string
, NULL
);
244 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
, "len=%d error=%x\n", len
, GetLastError());
247 /* CP_UTF7, CP_UTF8, or CP_SYMBOL and used not NULL => ERROR_INVALID_PARAMETER */
248 /* CP_SYMBOL's behavior here is undocumented */
249 SetLastError(0xdeadbeef);
250 len
= WideCharToMultiByte(CP_UTF7
, 0, w_string
, w_string_len
, c_string
, c_string_len
, NULL
, &used
);
251 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
, "len=%d error=%x\n", len
, GetLastError());
253 SetLastError(0xdeadbeef);
254 len
= WideCharToMultiByte(CP_UTF8
, 0, w_string
, w_string_len
, c_string
, c_string_len
, NULL
, &used
);
255 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
, "len=%d error=%x\n", len
, GetLastError());
257 SetLastError(0xdeadbeef);
258 len
= WideCharToMultiByte(CP_SYMBOL
, 0, w_string
, w_string_len
, c_string
, c_string_len
, NULL
, &used
);
259 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
, "len=%d error=%x\n", len
, GetLastError());
262 /* CP_UTF7, flags not 0 and used not NULL => ERROR_INVALID_PARAMETER */
263 /* (tests precedence of ERROR_INVALID_PARAMETER over ERROR_INVALID_FLAGS) */
264 /* The same test with CP_SYMBOL instead of CP_UTF7 gives ERROR_INVALID_FLAGS
265 instead except on Windows NT4 */
266 SetLastError(0xdeadbeef);
267 len
= WideCharToMultiByte(CP_UTF7
, 1, w_string
, w_string_len
, c_string
, c_string_len
, NULL
, &used
);
268 ok(len
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
, "len=%d error=%x\n", len
, GetLastError());
271 static void test_overlapped_buffers(void)
273 static const WCHAR strW
[] = {'j','u','s','t',' ','a',' ','t','e','s','t',0};
274 static const char strA
[] = "just a test";
278 SetLastError(0xdeadbeef);
279 memcpy(buf
+ 1, strW
, sizeof(strW
));
280 ret
= WideCharToMultiByte(CP_ACP
, 0, (WCHAR
*)(buf
+ 1), -1, buf
, sizeof(buf
), NULL
, NULL
);
281 ok(ret
== sizeof(strA
), "unexpected ret %d\n", ret
);
282 ok(!memcmp(buf
, strA
, sizeof(strA
)), "conversion failed: %s\n", buf
);
283 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
286 static void test_string_conversion(LPBOOL bUsedDefaultChar
)
291 WCHAR wc1
= 228; /* Western Windows-1252 character */
292 WCHAR wc2
= 1088; /* Russian Windows-1251 character not displayable for Windows-1252 */
293 static const WCHAR wcs
[] = {'T', 'h', 1088, 'i', 0}; /* String with ASCII characters and a Russian character */
294 static const WCHAR dbwcs
[] = {28953, 25152, 0}; /* String with Chinese (codepage 950) characters */
295 static const WCHAR dbwcs2
[] = {0x7bb8, 0x3d, 0xc813, 0xac00, 0xb77d, 0};
296 static const char default_char
[] = {0xa3, 0xbf, 0};
298 SetLastError(0xdeadbeef);
299 ret
= WideCharToMultiByte(1252, 0, &wc1
, 1, &mbc
, 1, NULL
, bUsedDefaultChar
);
300 ok(ret
== 1, "ret is %d\n", ret
);
301 ok(mbc
== '\xe4', "mbc is %d\n", mbc
);
302 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
303 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
305 SetLastError(0xdeadbeef);
306 ret
= WideCharToMultiByte(1252, 0, &wc2
, 1, &mbc
, 1, NULL
, bUsedDefaultChar
);
307 ok(ret
== 1, "ret is %d\n", ret
);
308 ok(mbc
== 63, "mbc is %d\n", mbc
);
309 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== TRUE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
310 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
312 if (IsValidCodePage(1251))
314 SetLastError(0xdeadbeef);
315 ret
= WideCharToMultiByte(1251, 0, &wc2
, 1, &mbc
, 1, NULL
, bUsedDefaultChar
);
316 ok(ret
== 1, "ret is %d\n", ret
);
317 ok(mbc
== '\xf0', "mbc is %d\n", mbc
);
318 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
319 ok(GetLastError() == 0xdeadbeef ||
320 broken(GetLastError() == 0), /* win95 */
321 "GetLastError() is %u\n", GetLastError());
323 SetLastError(0xdeadbeef);
324 ret
= WideCharToMultiByte(1251, 0, &wc1
, 1, &mbc
, 1, NULL
, bUsedDefaultChar
);
325 ok(ret
== 1, "ret is %d\n", ret
);
326 ok(mbc
== 97, "mbc is %d\n", mbc
);
327 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
328 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
331 skip("Codepage 1251 not available\n");
333 /* This call triggers the last Win32 error */
334 SetLastError(0xdeadbeef);
335 ret
= WideCharToMultiByte(1252, 0, wcs
, -1, &mbc
, 1, NULL
, bUsedDefaultChar
);
336 ok(ret
== 0, "ret is %d\n", ret
);
337 ok(mbc
== 84, "mbc is %d\n", mbc
);
338 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
339 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetLastError() is %u\n", GetLastError());
341 SetLastError(0xdeadbeef);
342 ret
= WideCharToMultiByte(1252, 0, wcs
, -1, mbs
, sizeof(mbs
), NULL
, bUsedDefaultChar
);
343 ok(ret
== 5, "ret is %d\n", ret
);
344 ok(!strcmp(mbs
, "Th?i"), "mbs is %s\n", mbs
);
345 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== TRUE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
346 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
349 /* WideCharToMultiByte mustn't add any null character automatically.
350 So in this case, we should get the same string again, even if we only copied the first three bytes. */
351 SetLastError(0xdeadbeef);
352 ret
= WideCharToMultiByte(1252, 0, wcs
, 3, mbs
, sizeof(mbs
), NULL
, bUsedDefaultChar
);
353 ok(ret
== 3, "ret is %d\n", ret
);
354 ok(!strcmp(mbs
, "Th?i"), "mbs is %s\n", mbs
);
355 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== TRUE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
356 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
359 /* Now this shouldn't be the case like above as we zeroed the complete string buffer. */
360 SetLastError(0xdeadbeef);
361 ret
= WideCharToMultiByte(1252, 0, wcs
, 3, mbs
, sizeof(mbs
), NULL
, bUsedDefaultChar
);
362 ok(ret
== 3, "ret is %d\n", ret
);
363 ok(!strcmp(mbs
, "Th?"), "mbs is %s\n", mbs
);
364 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== TRUE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
365 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
367 /* Double-byte tests */
368 ret
= WideCharToMultiByte(1252, 0, dbwcs
, 3, mbs
, sizeof(mbs
), NULL
, bUsedDefaultChar
);
369 ok(ret
== 3, "ret is %d\n", ret
);
370 ok(!strcmp(mbs
, "??"), "mbs is %s\n", mbs
);
371 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== TRUE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
373 ret
= WideCharToMultiByte(936, WC_COMPOSITECHECK
, dbwcs2
, -1, mbs
, sizeof(mbs
), (const char *)default_char
, bUsedDefaultChar
);
374 ok(ret
== 10, "ret is %d\n", ret
);
375 ok(!strcmp(mbs
, "\xf3\xe7\x3d\xa3\xbf\xa3\xbf\xa3\xbf"), "mbs is %s\n", mbs
);
376 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== TRUE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
378 /* Length-only tests */
379 SetLastError(0xdeadbeef);
380 ret
= WideCharToMultiByte(1252, 0, &wc2
, 1, NULL
, 0, NULL
, bUsedDefaultChar
);
381 ok(ret
== 1, "ret is %d\n", ret
);
382 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== TRUE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
383 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
385 SetLastError(0xdeadbeef);
386 ret
= WideCharToMultiByte(1252, 0, wcs
, -1, NULL
, 0, NULL
, bUsedDefaultChar
);
387 ok(ret
== 5, "ret is %d\n", ret
);
388 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== TRUE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
389 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
391 if (!IsValidCodePage(950))
393 skip("Codepage 950 not available\n");
397 /* Double-byte tests */
398 SetLastError(0xdeadbeef);
399 ret
= WideCharToMultiByte(950, 0, dbwcs
, -1, mbs
, sizeof(mbs
), NULL
, bUsedDefaultChar
);
400 ok(ret
== 5, "ret is %d\n", ret
);
401 ok(!strcmp(mbs
, "\xb5H\xa9\xd2"), "mbs is %s\n", mbs
);
402 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
403 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
405 SetLastError(0xdeadbeef);
406 ret
= WideCharToMultiByte(950, 0, dbwcs
, 1, &mbc
, 1, NULL
, bUsedDefaultChar
);
407 ok(ret
== 0, "ret is %d\n", ret
);
408 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
409 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetLastError() is %u\n", GetLastError());
412 SetLastError(0xdeadbeef);
413 ret
= WideCharToMultiByte(950, 0, dbwcs
, 1, mbs
, sizeof(mbs
), NULL
, bUsedDefaultChar
);
414 ok(ret
== 2, "ret is %d\n", ret
);
415 ok(!strcmp(mbs
, "\xb5H"), "mbs is %s\n", mbs
);
416 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
417 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
419 /* Length-only tests */
420 SetLastError(0xdeadbeef);
421 ret
= WideCharToMultiByte(950, 0, dbwcs
, 1, NULL
, 0, NULL
, bUsedDefaultChar
);
422 ok(ret
== 2, "ret is %d\n", ret
);
423 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
424 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
426 SetLastError(0xdeadbeef);
427 ret
= WideCharToMultiByte(950, 0, dbwcs
, -1, NULL
, 0, NULL
, bUsedDefaultChar
);
428 ok(ret
== 5, "ret is %d\n", ret
);
429 if(bUsedDefaultChar
) ok(*bUsedDefaultChar
== FALSE
, "bUsedDefaultChar is %d\n", *bUsedDefaultChar
);
430 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
433 static void test_utf7_encoding(void)
436 char output
[16], expected
[16];
437 int i
, len
, expected_len
;
439 static const BOOL directly_encodable_table
[] =
441 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, /* 0x00 - 0x0F */
442 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1F */
443 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* 0x20 - 0x2F */
444 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x30 - 0x3F */
445 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4F */
446 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50 - 0x5F */
447 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6F */
448 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 - 0x7F */
450 static const char base64_encoding_table
[] =
451 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
460 /* expected outputs */
461 char expected_dst
[16];
467 /* tests string conversion with srclen=-1 */
469 {0x4F60,0x597D,0x5417,0}, -1, output
, sizeof(output
) - 1,
472 /* tests string conversion with srclen=-2 */
474 {0x4F60,0x597D,0x5417,0}, -2, output
, sizeof(output
) - 1,
477 /* tests string conversion with dstlen=strlen(expected_dst) */
479 {0x4F60,0x597D,0x5417,0}, -1, output
, 10,
482 /* tests string conversion with dstlen=strlen(expected_dst)+1 */
484 {0x4F60,0x597D,0x5417,0}, -1, output
, 11,
487 /* tests string conversion with dstlen=strlen(expected_dst)+2 */
489 {0x4F60,0x597D,0x5417,0}, -1, output
, 12,
492 /* tests dry run with dst=NULL and dstlen=0 */
494 {0x4F60,0x597D,0x5417,0}, -1, NULL
, 0,
497 /* tests dry run with dst!=NULL and dstlen=0 */
499 {0x4F60,0x597D,0x5417,0}, -1, output
, 0,
502 /* tests srclen < strlenW(src) with directly encodable chars */
504 {'h','e','l','l','o',0}, 2, output
, sizeof(output
) - 1,
507 /* tests srclen < strlenW(src) with non-directly encodable chars */
509 {0x4F60,0x597D,0x5417,0}, 2, output
, sizeof(output
) - 1,
512 /* tests a single null char */
514 {0}, -1, output
, sizeof(output
) - 1,
517 /* tests a buffer that runs out while not encoding a UTF-7 sequence */
519 {'h','e','l','l','o',0}, -1, output
, 2,
522 /* tests a buffer that runs out after writing 1 base64 character */
524 {0x4F60,0x0001,0}, -1, output
, 2,
527 /* tests a buffer that runs out after writing 2 base64 characters */
529 {0x4F60,0x0001,0}, -1, output
, 3,
532 /* tests a buffer that runs out after writing 3 base64 characters */
534 {0x4F60,0x0001,0}, -1, output
, 4,
537 /* tests a buffer that runs out just after writing the + sign */
539 {0x4F60,0}, -1, output
, 1,
542 /* tests a buffer that runs out just before writing the - sign
543 * the number of bits to encode here is evenly divisible by 6 */
545 {0x4F60,0x597D,0x5417,0}, -1, output
, 9,
548 /* tests a buffer that runs out just before writing the - sign
549 * the number of bits to encode here is NOT evenly divisible by 6 */
551 {0x4F60,0}, -1, output
, 4,
554 /* tests a buffer that runs out in the middle of escaping a + sign */
556 {'+',0}, -1, output
, 1,
561 /* test which characters are encoded if surrounded by non-encoded characters */
562 for (i
= 0; i
<= 0xFFFF; i
++)
569 memset(output
, '#', sizeof(output
) - 1);
570 output
[sizeof(output
) - 1] = 0;
572 len
= WideCharToMultiByte(CP_UTF7
, 0, input
, 4, output
, sizeof(output
) - 1, NULL
, NULL
);
576 /* '+' is a special case and is encoded as "+-" */
578 strcpy(expected
, " +- ");
580 else if (i
<= 0x7F && directly_encodable_table
[i
])
582 /* encodes directly */
584 sprintf(expected
, " %c ", i
);
590 sprintf(expected
, " +%c%c%c- ",
591 base64_encoding_table
[(i
& 0xFC00) >> 10],
592 base64_encoding_table
[(i
& 0x03F0) >> 4],
593 base64_encoding_table
[(i
& 0x000F) << 2]);
596 ok(len
== expected_len
, "i=0x%04x: expected len=%i, got len=%i\n", i
, expected_len
, len
);
597 ok(memcmp(output
, expected
, expected_len
) == 0,
598 "i=0x%04x: expected output='%s', got output='%s'\n", i
, expected
, output
);
599 ok(output
[expected_len
] == '#', "i=0x%04x: expected output[%i]='#', got output[%i]=%i\n",
600 i
, expected_len
, expected_len
, output
[expected_len
]);
603 /* test which one-byte characters are absorbed into surrounding base64 blocks
604 * (Windows always ends the base64 block when it encounters a directly encodable character) */
605 for (i
= 0; i
<= 0xFFFF; i
++)
612 memset(output
, '#', sizeof(output
) - 1);
613 output
[sizeof(output
) - 1] = 0;
615 len
= WideCharToMultiByte(CP_UTF7
, 0, input
, 4, output
, sizeof(output
) - 1, NULL
, NULL
);
619 /* '+' is a special case and is encoded as "+-" */
621 strcpy(expected
, "+JnI-+-+JnI-");
623 else if (i
<= 0x7F && directly_encodable_table
[i
])
625 /* encodes directly */
627 sprintf(expected
, "+JnI-%c+JnI-", i
);
633 sprintf(expected
, "+Jn%c%c%c%cZy-",
634 base64_encoding_table
[8 | ((i
& 0xC000) >> 14)],
635 base64_encoding_table
[(i
& 0x3F00) >> 8],
636 base64_encoding_table
[(i
& 0x00FC) >> 2],
637 base64_encoding_table
[((i
& 0x0003) << 4) | 2]);
640 ok(len
== expected_len
, "i=0x%04x: expected len=%i, got len=%i\n", i
, expected_len
, len
);
641 ok(memcmp(output
, expected
, expected_len
) == 0,
642 "i=0x%04x: expected output='%s', got output='%s'\n", i
, expected
, output
);
643 ok(output
[expected_len
] == '#', "i=0x%04x: expected output[%i]='#', got output[%i]=%i\n",
644 i
, expected_len
, expected_len
, output
[expected_len
]);
647 for (i
= 0; i
< sizeof(tests
) / sizeof(tests
[0]); i
++)
649 memset(output
, '#', sizeof(output
) - 1);
650 output
[sizeof(output
) - 1] = 0;
651 SetLastError(0xdeadbeef);
653 len
= WideCharToMultiByte(CP_UTF7
, 0, tests
[i
].src
, tests
[i
].srclen
,
654 tests
[i
].dst
, tests
[i
].dstlen
, NULL
, NULL
);
658 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
659 "tests[%i]: expected error=0x%x, got error=0x%x\n",
660 i
, ERROR_INSUFFICIENT_BUFFER
, GetLastError());
662 ok(len
== tests
[i
].len
, "tests[%i]: expected len=%i, got len=%i\n", i
, tests
[i
].len
, len
);
666 ok(memcmp(tests
[i
].dst
, tests
[i
].expected_dst
, tests
[i
].chars_written
) == 0,
667 "tests[%i]: expected dst='%s', got dst='%s'\n",
668 i
, tests
[i
].expected_dst
, tests
[i
].dst
);
669 ok(tests
[i
].dst
[tests
[i
].chars_written
] == '#',
670 "tests[%i]: expected dst[%i]='#', got dst[%i]=%i\n",
671 i
, tests
[i
].chars_written
, tests
[i
].chars_written
, tests
[i
].dst
[tests
[i
].chars_written
]);
676 static void test_utf7_decoding(void)
679 WCHAR output
[32], expected
[32];
680 int i
, len
, expected_len
;
682 static const signed char base64_decoding_table
[] =
684 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0F */
685 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1F */
686 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20-0x2F */
687 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30-0x3F */
688 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40-0x4F */
689 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50-0x5F */
690 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60-0x6F */
691 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70-0x7F */
701 /* expected outputs */
702 WCHAR expected_dst
[32];
708 /* tests string conversion with srclen=-1 */
710 "+T2BZfQ-", -1, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
711 {0x4F60,0x597D,0}, 3, 3
713 /* tests string conversion with srclen=-2 */
715 "+T2BZfQ-", -2, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
716 {0x4F60,0x597D,0}, 3, 3
718 /* tests string conversion with dstlen=strlen(expected_dst) */
720 "+T2BZfQ-", -1, output
, 2,
721 {0x4F60,0x597D}, 2, 0
723 /* tests string conversion with dstlen=strlen(expected_dst)+1 */
725 "+T2BZfQ-", -1, output
, 3,
726 {0x4F60,0x597D,0}, 3, 3
728 /* tests string conversion with dstlen=strlen(expected_dst)+2 */
730 "+T2BZfQ-", -1, output
, 4,
731 {0x4F60,0x597D,0}, 3, 3
733 /* tests dry run with dst=NULL and dstlen=0 */
735 "+T2BZfQ-", -1, NULL
, 0,
738 /* tests dry run with dst!=NULL and dstlen=0 */
740 "+T2BZfQ-", -1, output
, 0,
743 /* tests ill-formed UTF-7: 6 bits, not enough for a byte pair */
745 "+T-+T-+T-hello", -1, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
746 {'h','e','l','l','o',0}, 6, 6
748 /* tests ill-formed UTF-7: 12 bits, not enough for a byte pair */
750 "+T2-+T2-+T2-hello", -1, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
751 {'h','e','l','l','o',0}, 6, 6
753 /* tests ill-formed UTF-7: 18 bits, not a multiple of 16 and the last bit is a 1 */
755 "+T2B-+T2B-+T2B-hello", -1, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
756 {0x4F60,0x4F60,0x4F60,'h','e','l','l','o',0}, 9, 9
758 /* tests ill-formed UTF-7: 24 bits, a multiple of 8 but not a multiple of 16 */
760 "+T2BZ-+T2BZ-+T2BZ-hello", -1, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
761 {0x4F60,0x4F60,0x4F60,'h','e','l','l','o',0}, 9, 9
763 /* tests UTF-7 followed by characters that should be encoded but aren't */
765 "+T2BZ-\x82\xFE", -1, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
766 {0x4F60,0x0082,0x00FE,0}, 4, 4
768 /* tests srclen > strlen(src) */
770 "a\0b", 4, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
773 /* tests srclen < strlen(src) outside of a UTF-7 sequence */
775 "hello", 2, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
778 /* tests srclen < strlen(src) inside of a UTF-7 sequence */
780 "+T2BZfQ-", 4, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
783 /* tests srclen < strlen(src) right at the beginning of a UTF-7 sequence */
785 "hi+T2A-", 3, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
788 /* tests srclen < strlen(src) right at the end of a UTF-7 sequence */
790 "+T2A-hi", 5, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
793 /* tests srclen < strlen(src) at the beginning of an escaped + sign */
795 "hi+-", 3, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
798 /* tests srclen < strlen(src) at the end of an escaped + sign */
800 "+-hi", 2, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
803 /* tests len=0 but no error */
805 "+", 1, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
808 /* tests a single null char */
810 "", -1, output
, sizeof(output
) / sizeof(WCHAR
) - 1,
813 /* tests a buffer that runs out while not decoding a UTF-7 sequence */
815 "hello", -1, output
, 2,
818 /* tests a buffer that runs out in the middle of decoding a UTF-7 sequence */
820 "+T2BZfQ-", -1, output
, 1,
825 /* test which one-byte characters remove stray + signs */
826 for (i
= 0; i
< 256; i
++)
828 sprintf(input
, "+%c+AAA", i
);
830 memset(output
, 0x23, sizeof(output
) - sizeof(WCHAR
));
831 output
[sizeof(output
) / sizeof(WCHAR
) - 1] = 0;
833 len
= MultiByteToWideChar(CP_UTF7
, 0, input
, 7, output
, sizeof(output
) / sizeof(WCHAR
) - 1);
837 /* removes the - sign */
839 expected
[0] = 0x002B;
843 else if (i
<= 0x7F && base64_decoding_table
[i
] != -1)
845 /* absorbs the character into the base64 sequence */
847 expected
[0] = (base64_decoding_table
[i
] << 10) | 0x03E0;
852 /* removes the + sign */
858 expected
[expected_len
] = 0x2323;
860 ok(len
== expected_len
, "i=0x%02x: expected len=%i, got len=%i\n", i
, expected_len
, len
);
861 ok(memcmp(output
, expected
, (expected_len
+ 1) * sizeof(WCHAR
)) == 0,
862 "i=0x%02x: expected output=%s, got output=%s\n",
863 i
, wine_dbgstr_wn(expected
, expected_len
+ 1), wine_dbgstr_wn(output
, expected_len
+ 1));
866 /* test which one-byte characters terminate a sequence
867 * also test whether the unfinished byte pair is discarded or not */
868 for (i
= 0; i
< 256; i
++)
870 sprintf(input
, "+B%c+AAA", i
);
872 memset(output
, 0x23, sizeof(output
) - sizeof(WCHAR
));
873 output
[sizeof(output
) / sizeof(WCHAR
) - 1] = 0;
875 len
= MultiByteToWideChar(CP_UTF7
, 0, input
, 8, output
, sizeof(output
) / sizeof(WCHAR
) - 1);
879 /* explicitly terminates */
886 if (base64_decoding_table
[i
] != -1)
888 /* absorbs the character into the base64 sequence */
890 expected
[0] = 0x0400 | (base64_decoding_table
[i
] << 4) | 0x000F;
891 expected
[1] = 0x8000;
896 /* implicitly terminates and discards the unfinished byte pair */
905 /* implicitly terminates but does not the discard unfinished byte pair */
908 expected
[1] = 0x0400;
911 expected
[expected_len
] = 0x2323;
913 ok(len
== expected_len
, "i=0x%02x: expected len=%i, got len=%i\n", i
, expected_len
, len
);
914 ok(memcmp(output
, expected
, (expected_len
+ 1) * sizeof(WCHAR
)) == 0,
915 "i=0x%02x: expected output=%s, got output=%s\n",
916 i
, wine_dbgstr_wn(expected
, expected_len
+ 1), wine_dbgstr_wn(output
, expected_len
+ 1));
919 for (i
= 0; i
< sizeof(tests
) / sizeof(tests
[0]); i
++)
921 memset(output
, 0x23, sizeof(output
) - sizeof(WCHAR
));
922 output
[sizeof(output
) / sizeof(WCHAR
) - 1] = 0;
923 SetLastError(0xdeadbeef);
925 len
= MultiByteToWideChar(CP_UTF7
, 0, tests
[i
].src
, tests
[i
].srclen
,
926 tests
[i
].dst
, tests
[i
].dstlen
);
928 tests
[i
].expected_dst
[tests
[i
].chars_written
] = 0x2323;
930 if (!tests
[i
].len
&& tests
[i
].chars_written
)
932 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
933 "tests[%i]: expected error=0x%x, got error=0x%x\n",
934 i
, ERROR_INSUFFICIENT_BUFFER
, GetLastError());
936 ok(len
== tests
[i
].len
, "tests[%i]: expected len=%i, got len=%i\n", i
, tests
[i
].len
, len
);
940 ok(memcmp(tests
[i
].dst
, tests
[i
].expected_dst
, (tests
[i
].chars_written
+ 1) * sizeof(WCHAR
)) == 0,
941 "tests[%i]: expected dst=%s, got dst=%s\n",
942 i
, wine_dbgstr_wn(tests
[i
].expected_dst
, tests
[i
].chars_written
+ 1),
943 wine_dbgstr_wn(tests
[i
].dst
, tests
[i
].chars_written
+ 1));
948 static void test_undefined_byte_char(void)
950 static const struct tag_testset
{
955 { 874, "\xdd", TRUE
},
956 { 932, "\xfe", TRUE
},
957 { 932, "\x80", FALSE
},
958 { 936, "\xff", TRUE
},
959 { 949, "\xff", TRUE
},
960 { 950, "\xff", TRUE
},
961 { 1252, "\x90", FALSE
},
962 { 1253, "\xaa", TRUE
},
963 { 1255, "\xff", TRUE
},
964 { 1257, "\xa5", TRUE
},
968 for (i
= 0; i
< (sizeof(testset
) / sizeof(testset
[0])); i
++) {
969 if (! IsValidCodePage(testset
[i
].codepage
))
971 skip("Codepage %d not available\n", testset
[i
].codepage
);
975 SetLastError(0xdeadbeef);
976 ret
= MultiByteToWideChar(testset
[i
].codepage
, MB_ERR_INVALID_CHARS
,
977 testset
[i
].str
, -1, NULL
, 0);
978 if (testset
[i
].is_error
) {
979 ok(ret
== 0 && GetLastError() == ERROR_NO_UNICODE_TRANSLATION
,
980 "ret is %d, GetLastError is %u (cp %d)\n",
981 ret
, GetLastError(), testset
[i
].codepage
);
984 ok(ret
== strlen(testset
[i
].str
)+1 && GetLastError() == 0xdeadbeef,
985 "ret is %d, GetLastError is %u (cp %d)\n",
986 ret
, GetLastError(), testset
[i
].codepage
);
989 SetLastError(0xdeadbeef);
990 ret
= MultiByteToWideChar(testset
[i
].codepage
, 0,
991 testset
[i
].str
, -1, NULL
, 0);
992 ok(ret
== strlen(testset
[i
].str
)+1 && GetLastError() == 0xdeadbeef,
993 "ret is %d, GetLastError is %u (cp %d)\n",
994 ret
, GetLastError(), testset
[i
].codepage
);
998 static void test_threadcp(void)
1000 static const LCID ENGLISH
= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
);
1001 static const LCID HINDI
= MAKELCID(MAKELANGID(LANG_HINDI
, SUBLANG_HINDI_INDIA
), SORT_DEFAULT
);
1002 static const LCID GEORGIAN
= MAKELCID(MAKELANGID(LANG_GEORGIAN
, SUBLANG_GEORGIAN_GEORGIA
), SORT_DEFAULT
);
1003 static const LCID RUSSIAN
= MAKELCID(MAKELANGID(LANG_RUSSIAN
, SUBLANG_RUSSIAN_RUSSIA
), SORT_DEFAULT
);
1004 static const LCID JAPANESE
= MAKELCID(MAKELANGID(LANG_JAPANESE
, SUBLANG_JAPANESE_JAPAN
), SORT_DEFAULT
);
1005 static const LCID CHINESE
= MAKELCID(MAKELANGID(LANG_CHINESE
, SUBLANG_CHINESE_SIMPLIFIED
), SORT_DEFAULT
);
1007 BOOL islead
, islead_acp
;
1026 struct test_islead_nocp
{
1029 } isleads_nocp
[] = {
1041 struct test_islead
{
1046 { ENGLISH
, 0x00, FALSE
},
1047 { ENGLISH
, 0x81, FALSE
},
1048 { ENGLISH
, 0xa0, FALSE
},
1049 { ENGLISH
, 0xe0, FALSE
},
1051 { RUSSIAN
, 0x00, FALSE
},
1052 { RUSSIAN
, 0x81, FALSE
},
1053 { RUSSIAN
, 0xa0, FALSE
},
1054 { RUSSIAN
, 0xe0, FALSE
},
1056 { JAPANESE
, 0x00, FALSE
},
1057 { JAPANESE
, 0x81, TRUE
},
1058 { JAPANESE
, 0xa0, FALSE
},
1059 { JAPANESE
, 0xe0, TRUE
},
1061 { CHINESE
, 0x00, FALSE
},
1062 { CHINESE
, 0x81, TRUE
},
1063 { CHINESE
, 0xa0, TRUE
},
1064 { CHINESE
, 0xe0, TRUE
},
1067 last
= GetThreadLocale();
1070 for (i
= 0; i
< sizeof(lcids
)/sizeof(lcids
[0]); i
++)
1072 SetThreadLocale(lcids
[i
].lcid
);
1075 GetLocaleInfoA(lcids
[i
].lcid
, LOCALE_IDEFAULTANSICODEPAGE
|LOCALE_RETURN_NUMBER
, (LPSTR
)&cp
, sizeof(cp
));
1076 ok(cp
== lcids
[i
].threadcp
, "wrong codepage %u for lcid %04x, should be %u\n", cp
, lcids
[i
].threadcp
, cp
);
1078 /* GetCPInfoEx/GetCPInfo - CP_ACP */
1079 SetLastError(0xdeadbeef);
1080 memset(&cpi
, 0, sizeof(cpi
));
1081 ret
= GetCPInfoExA(CP_ACP
, 0, &cpi
);
1082 ok(ret
, "GetCPInfoExA failed for lcid %04x, error %d\n", lcids
[i
].lcid
, GetLastError());
1083 ok(cpi
.CodePage
== acp
, "wrong codepage %u for lcid %04x, should be %u\n", cpi
.CodePage
, lcids
[i
].lcid
, acp
);
1085 /* WideCharToMultiByte - CP_ACP */
1086 num
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -1, NULL
, 0, NULL
, NULL
);
1087 ok(num
== 7, "ret is %d (%04x)\n", num
, lcids
[i
].lcid
);
1089 /* MultiByteToWideChar - CP_ACP */
1090 num
= MultiByteToWideChar(CP_ACP
, 0, "foobar", -1, NULL
, 0);
1091 ok(num
== 7, "ret is %d (%04x)\n", num
, lcids
[i
].lcid
);
1093 /* GetCPInfoEx/GetCPInfo - CP_THREAD_ACP */
1094 SetLastError(0xdeadbeef);
1095 memset(&cpi
, 0, sizeof(cpi
));
1096 ret
= GetCPInfoExA(CP_THREAD_ACP
, 0, &cpi
);
1097 ok(ret
, "GetCPInfoExA failed for lcid %04x, error %d\n", lcids
[i
].lcid
, GetLastError());
1098 if (lcids
[i
].threadcp
)
1099 ok(cpi
.CodePage
== lcids
[i
].threadcp
, "wrong codepage %u for lcid %04x, should be %u\n",
1100 cpi
.CodePage
, lcids
[i
].lcid
, lcids
[i
].threadcp
);
1102 ok(cpi
.CodePage
== acp
, "wrong codepage %u for lcid %04x, should be %u\n",
1103 cpi
.CodePage
, lcids
[i
].lcid
, acp
);
1105 /* WideCharToMultiByte - CP_THREAD_ACP */
1106 num
= WideCharToMultiByte(CP_THREAD_ACP
, 0, foobarW
, -1, NULL
, 0, NULL
, NULL
);
1107 ok(num
== 7, "ret is %d (%04x)\n", num
, lcids
[i
].lcid
);
1109 /* MultiByteToWideChar - CP_THREAD_ACP */
1110 num
= MultiByteToWideChar(CP_THREAD_ACP
, 0, "foobar", -1, NULL
, 0);
1111 ok(num
== 7, "ret is %d (%04x)\n", num
, lcids
[i
].lcid
);
1114 /* IsDBCSLeadByteEx - locales without codepage */
1115 for (i
= 0; i
< sizeof(isleads_nocp
)/sizeof(isleads_nocp
[0]); i
++)
1117 SetThreadLocale(isleads_nocp
[i
].lcid
);
1119 islead_acp
= IsDBCSLeadByteEx(CP_ACP
, isleads_nocp
[i
].testchar
);
1120 islead
= IsDBCSLeadByteEx(CP_THREAD_ACP
, isleads_nocp
[i
].testchar
);
1122 ok(islead
== islead_acp
, "wrong islead %i for test char %x in lcid %04x. should be %i\n",
1123 islead
, isleads_nocp
[i
].testchar
, isleads_nocp
[i
].lcid
, islead_acp
);
1126 /* IsDBCSLeadByteEx - locales with codepage */
1127 for (i
= 0; i
< sizeof(isleads
)/sizeof(isleads
[0]); i
++)
1129 SetThreadLocale(isleads
[i
].lcid
);
1131 islead
= IsDBCSLeadByteEx(CP_THREAD_ACP
, isleads
[i
].testchar
);
1132 ok(islead
== isleads
[i
].islead
, "wrong islead %i for test char %x in lcid %04x. should be %i\n",
1133 islead
, isleads
[i
].testchar
, isleads
[i
].lcid
, isleads
[i
].islead
);
1136 SetThreadLocale(last
);
1139 static void test_dbcs_to_widechar(void)
1141 int i
, count
, count2
;
1143 unsigned char buf
[] = {0xbf, 0xb4, 0xc7, '\0', 'x'};
1144 static const DWORD flags
[] = {
1148 MB_PRECOMPOSED
|MB_USEGLYPHCHARS
,
1149 MB_COMPOSITE
|MB_USEGLYPHCHARS
,
1151 MB_PRECOMPOSED
|MB_ERR_INVALID_CHARS
,
1152 MB_COMPOSITE
|MB_ERR_INVALID_CHARS
,
1154 MB_PRECOMPOSED
|MB_ERR_INVALID_CHARS
|MB_USEGLYPHCHARS
,
1155 MB_COMPOSITE
|MB_ERR_INVALID_CHARS
|MB_USEGLYPHCHARS
,
1158 for (i
= 0; i
< sizeof(flags
)/sizeof(DWORD
); ++i
)
1160 memset(wbuf
, 0xff, sizeof(wbuf
));
1161 count
= MultiByteToWideChar(936, flags
[i
], (char*)&buf
[0], 2, NULL
, 0);
1162 count2
= MultiByteToWideChar(936, flags
[i
], (char*)&buf
[0], 2, wbuf
, count
);
1164 ok(count
== 1, "%04x: returned %d (expected 1)\n", flags
[i
], count
);
1165 ok(count2
== 1, "%04x: returned %d (expected 1)\n", flags
[i
], count2
);
1166 ok(wbuf
[0] == 0x770b, "%04x: returned %04x (expected 770b)\n", flags
[i
], wbuf
[0]);
1167 ok(wbuf
[1] == 0xffff, "%04x: returned %04x (expected ffff)\n", flags
[i
], wbuf
[1]);
1170 for (i
= 0; i
< sizeof(flags
)/sizeof(DWORD
); ++i
)
1172 memset(wbuf
, 0xff, sizeof(wbuf
));
1173 count
= MultiByteToWideChar(936, flags
[i
], (char*)&buf
[0], 3, NULL
, 0);
1174 SetLastError( 0xdeadbeef );
1175 count2
= MultiByteToWideChar(936, flags
[i
], (char*)&buf
[0], 3, wbuf
, count
);
1177 if (flags
[i
] & MB_ERR_INVALID_CHARS
)
1179 ok(count
== 0, "%04x: returned %d (expected 0)\n", flags
[i
], count
);
1180 ok(count2
== 0, "%04x: returned %d (expected 0)\n", flags
[i
], count2
);
1181 ok(GetLastError() == ERROR_NO_UNICODE_TRANSLATION
, "%04x: returned %d (expected %d)\n",
1182 flags
[i
], GetLastError(), ERROR_NO_UNICODE_TRANSLATION
);
1186 ok(count
== 2, "%04x: returned %d (expected 2)\n", flags
[i
], count
);
1187 ok(count2
== 2, "%04x: returned %d (expected 2)\n", flags
[i
], count2
);
1188 ok(wbuf
[0] == 0x770b, "%04x: returned %04x (expected 770b)\n", flags
[i
], wbuf
[0]);
1189 ok(wbuf
[1] == 0x003f || broken(wbuf
[1] == 0), /*windows xp*/
1190 "%04x: wrong wide char: %04x\n", flags
[i
], wbuf
[1]);
1191 ok(wbuf
[2] == 0xffff, "%04x: returned %04x (expected ffff)\n", flags
[i
], wbuf
[2]);
1195 /* src ends with null character */
1196 for (i
= 0; i
< sizeof(flags
)/sizeof(DWORD
); ++i
)
1198 memset(wbuf
, 0xff, sizeof(wbuf
));
1199 count
= MultiByteToWideChar(936, flags
[i
], (char*)&buf
[0], 4, NULL
, 0);
1200 SetLastError( 0xdeadbeef );
1201 count2
= MultiByteToWideChar(936, flags
[i
], (char*)&buf
[0], 4, wbuf
, count
);
1202 ok(count
== count2
, "%04x: returned %d (expected %d)\n", flags
[i
], count2
, count
);
1204 if (flags
[i
] & MB_ERR_INVALID_CHARS
)
1206 ok(count
== 0, "%04x: returned %d (expected 0)\n", flags
[i
], count
);
1207 ok(GetLastError() == ERROR_NO_UNICODE_TRANSLATION
, "%04x: returned %d (expected %d)\n",
1208 flags
[i
], GetLastError(), ERROR_NO_UNICODE_TRANSLATION
);
1212 WCHAR wbuf_ok
[] = { 0x770b, 0x003f, '\0', 0xffff };
1213 WCHAR wbuf_broken
[] = { 0x770b, '\0', 0xffff, 0xffff };
1214 ok(count
== 3 || broken(count
== 2 /*windows xp*/),
1215 "%04x: returned %d (expected 3)\n", flags
[i
], count
);
1216 ok(!memcmp(wbuf
, wbuf_ok
, sizeof(wbuf_ok
))
1217 || broken(!memcmp(wbuf
, wbuf_broken
, sizeof(wbuf_broken
))),
1218 "%04x: returned %04x %04x %04x %04x (expected %04x %04x %04x %04x)\n",
1219 flags
[i
], wbuf
[0], wbuf
[1], wbuf
[2], wbuf
[3],
1220 wbuf_ok
[0], wbuf_ok
[1], wbuf_ok
[2], wbuf_ok
[3]);
1224 /* src has null character, but not ends with it */
1225 for (i
= 0; i
< sizeof(flags
)/sizeof(DWORD
); ++i
)
1227 memset(wbuf
, 0xff, sizeof(wbuf
));
1228 count
= MultiByteToWideChar(936, flags
[i
], (char*)&buf
[0], 5, NULL
, 0);
1229 SetLastError( 0xdeadbeef );
1230 count2
= MultiByteToWideChar(936, flags
[i
], (char*)&buf
[0], 5, wbuf
, count
);
1231 ok(count
== count2
, "%04x: returned %d (expected %d)\n", flags
[i
], count2
, count
);
1233 if (flags
[i
] & MB_ERR_INVALID_CHARS
)
1235 ok(count
== 0, "%04x: returned %d (expected 0)\n", flags
[i
], count
);
1236 ok(GetLastError() == ERROR_NO_UNICODE_TRANSLATION
, "%04x: returned %d (expected %d)\n",
1237 flags
[i
], GetLastError(), ERROR_NO_UNICODE_TRANSLATION
);
1241 WCHAR wbuf_ok
[] = { 0x770b, 0x003f, '\0', 'x', 0xffff };
1242 WCHAR wbuf_broken
[] = { 0x770b, '\0', 'x', 0xffff, 0xffff };
1243 ok(count
== 4 || broken(count
== 3),
1244 "%04x: returned %d (expected 4)\n", flags
[i
], count
);
1245 ok(!memcmp(wbuf
, wbuf_ok
, sizeof(wbuf_ok
))
1246 || broken(!memcmp(wbuf
, wbuf_broken
, sizeof(wbuf_broken
))),
1247 "%04x: returned %04x %04x %04x %04x %04x (expected %04x %04x %04x %04x %04x)\n",
1248 flags
[i
], wbuf
[0], wbuf
[1], wbuf
[2], wbuf
[3], wbuf
[4],
1249 wbuf_ok
[0], wbuf_ok
[1], wbuf_ok
[2], wbuf_ok
[3], wbuf_ok
[4]);
1254 START_TEST(codepage
)
1256 BOOL bUsedDefaultChar
;
1258 test_destination_buffer();
1260 test_negative_source_length();
1261 test_negative_dest_length();
1262 test_other_invalid_parameters();
1263 test_overlapped_buffers();
1265 /* WideCharToMultiByte has two code paths, test both here */
1266 test_string_conversion(NULL
);
1267 test_string_conversion(&bUsedDefaultChar
);
1269 test_utf7_encoding();
1270 test_utf7_decoding();
1272 test_undefined_byte_char();
1275 test_dbcs_to_widechar();