msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / kernel32 / tests / codepage.c
blob8dc6af949ed5483a604d04406c5bc122ab47f9d7
1 /*
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
22 #include <stdarg.h>
23 #include <limits.h>
25 #include "wine/test.h"
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
30 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
32 static void test_destination_buffer(void)
34 LPSTR buffer;
35 INT maxsize;
36 INT needed;
37 INT len;
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());
44 maxsize = needed*2;
45 buffer = HeapAlloc(GetProcessHeap(), 0, maxsize);
46 if (buffer == NULL) return;
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);
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",
82 len, GetLastError());
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)
96 int len;
97 DWORD GLE;
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);
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);
114 /* lstrcmpW is not supported on Win9x! */
115 static int mylstrcmpW(const WCHAR* str1, const WCHAR* str2)
117 while (*str1 && *str1==*str2) {
118 str1++;
119 str2++;
121 return *str1-*str2;
124 static void test_negative_source_length(void)
126 int len;
127 char buf[10];
128 WCHAR bufW[10];
130 /* Test, whether any negative source length works as strlen() + 1 */
131 SetLastError( 0xdeadbeef );
132 memset(buf,'x',sizeof(buf));
133 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -2002, buf, 10, NULL, NULL);
134 ok(len == 7 && !lstrcmpA(buf, "foobar") && GetLastError() == 0xdeadbeef,
135 "WideCharToMultiByte(-2002): len=%d error=%u\n", len, GetLastError());
137 SetLastError( 0xdeadbeef );
138 memset(bufW,'x',sizeof(bufW));
139 len = MultiByteToWideChar(CP_ACP, 0, "foobar", -2002, bufW, 10);
140 ok(len == 7 && !mylstrcmpW(bufW, foobarW) && GetLastError() == 0xdeadbeef,
141 "MultiByteToWideChar(-2002): len=%d error=%u\n", len, GetLastError());
143 SetLastError(0xdeadbeef);
144 memset(bufW, 'x', sizeof(bufW));
145 len = MultiByteToWideChar(CP_ACP, 0, "foobar", -1, bufW, 6);
146 ok(len == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
147 "MultiByteToWideChar(-1): len=%d error=%u\n", len, GetLastError());
150 #define LONGBUFLEN 100000
151 static void test_negative_dest_length(void)
153 int len, i;
154 static char buf[LONGBUFLEN];
155 static WCHAR originalW[LONGBUFLEN];
156 static char originalA[LONGBUFLEN];
157 DWORD theError;
159 /* Test return on -1 dest length */
160 SetLastError( 0xdeadbeef );
161 memset(buf,'x',sizeof(buf));
162 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buf, -1, NULL, NULL);
163 todo_wine {
164 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
165 "WideCharToMultiByte(destlen -1): len=%d error=%x\n", len, GetLastError());
168 /* Test return on -1000 dest length */
169 SetLastError( 0xdeadbeef );
170 memset(buf,'x',sizeof(buf));
171 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buf, -1000, NULL, NULL);
172 todo_wine {
173 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
174 "WideCharToMultiByte(destlen -1000): len=%d error=%x\n", len, GetLastError());
177 /* Test return on INT_MAX dest length */
178 SetLastError( 0xdeadbeef );
179 memset(buf,'x',sizeof(buf));
180 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buf, INT_MAX, NULL, NULL);
181 ok(len == 7 && !lstrcmpA(buf, "foobar") && GetLastError() == 0xdeadbeef,
182 "WideCharToMultiByte(destlen INT_MAX): len=%d error=%x\n", len, GetLastError());
184 /* Test return on INT_MAX dest length and very long input */
185 SetLastError( 0xdeadbeef );
186 memset(buf,'x',sizeof(buf));
187 for (i=0; i < LONGBUFLEN - 1; i++) {
188 originalW[i] = 'Q';
189 originalA[i] = 'Q';
191 originalW[LONGBUFLEN-1] = 0;
192 originalA[LONGBUFLEN-1] = 0;
193 len = WideCharToMultiByte(CP_ACP, 0, originalW, -1, buf, INT_MAX, NULL, NULL);
194 theError = GetLastError();
195 ok(len == LONGBUFLEN && !lstrcmpA(buf, originalA) && theError == 0xdeadbeef,
196 "WideCharToMultiByte(srclen %d, destlen INT_MAX): len %d error=%x\n", LONGBUFLEN, len, theError);
200 static void test_overlapped_buffers(void)
202 static const WCHAR strW[] = {'j','u','s','t',' ','a',' ','t','e','s','t',0};
203 static const char strA[] = "just a test";
204 char buf[256];
205 int ret;
207 SetLastError(0xdeadbeef);
208 memcpy((WCHAR *)(buf + 1), strW, sizeof(strW));
209 ret = WideCharToMultiByte(CP_ACP, 0, (WCHAR *)(buf + 1), -1, buf, sizeof(buf), NULL, NULL);
210 ok(ret == sizeof(strA), "unexpected ret %d\n", ret);
211 ok(!memcmp(buf, strA, sizeof(strA)), "conversion failed: %s\n", buf);
212 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
215 static void test_string_conversion(LPBOOL bUsedDefaultChar)
217 char mbc;
218 char mbs[5];
219 int ret;
220 WCHAR wc1 = 228; /* Western Windows-1252 character */
221 WCHAR wc2 = 1088; /* Russian Windows-1251 character not displayable for Windows-1252 */
222 WCHAR wcs[5] = {'T', 'h', 1088, 'i', 0}; /* String with ASCII characters and a Russian character */
223 WCHAR dbwcs[3] = {28953, 25152, 0}; /* String with Chinese (codepage 950) characters */
225 SetLastError(0xdeadbeef);
226 ret = WideCharToMultiByte(1252, 0, &wc1, 1, &mbc, 1, NULL, bUsedDefaultChar);
227 ok(ret == 1, "ret is %d\n", ret);
228 ok(mbc == -28, "mbc is %d\n", mbc);
229 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
230 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
232 SetLastError(0xdeadbeef);
233 ret = WideCharToMultiByte(1252, 0, &wc2, 1, &mbc, 1, NULL, bUsedDefaultChar);
234 ok(ret == 1, "ret is %d\n", ret);
235 ok(mbc == 63, "mbc is %d\n", mbc);
236 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
237 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
239 if (IsValidCodePage(1251))
241 SetLastError(0xdeadbeef);
242 ret = WideCharToMultiByte(1251, 0, &wc2, 1, &mbc, 1, NULL, bUsedDefaultChar);
243 ok(ret == 1, "ret is %d\n", ret);
244 ok(mbc == -16, "mbc is %d\n", mbc);
245 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
246 ok(GetLastError() == 0xdeadbeef ||
247 broken(GetLastError() == 0), /* win95 */
248 "GetLastError() is %u\n", GetLastError());
250 SetLastError(0xdeadbeef);
251 ret = WideCharToMultiByte(1251, 0, &wc1, 1, &mbc, 1, NULL, bUsedDefaultChar);
252 ok(ret == 1, "ret is %d\n", ret);
253 ok(mbc == 97, "mbc is %d\n", mbc);
254 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
255 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
257 else
258 skip("Codepage 1251 not available\n");
260 /* This call triggers the last Win32 error */
261 SetLastError(0xdeadbeef);
262 ret = WideCharToMultiByte(1252, 0, wcs, -1, &mbc, 1, NULL, bUsedDefaultChar);
263 ok(ret == 0, "ret is %d\n", ret);
264 ok(mbc == 84, "mbc is %d\n", mbc);
265 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
266 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() is %u\n", GetLastError());
268 SetLastError(0xdeadbeef);
269 ret = WideCharToMultiByte(1252, 0, wcs, -1, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
270 ok(ret == 5, "ret is %d\n", ret);
271 ok(!strcmp(mbs, "Th?i"), "mbs is %s\n", mbs);
272 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
273 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
274 mbs[0] = 0;
276 /* WideCharToMultiByte mustn't add any null character automatically.
277 So in this case, we should get the same string again, even if we only copied the first three bytes. */
278 SetLastError(0xdeadbeef);
279 ret = WideCharToMultiByte(1252, 0, wcs, 3, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
280 ok(ret == 3, "ret is %d\n", ret);
281 ok(!strcmp(mbs, "Th?i"), "mbs is %s\n", mbs);
282 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
283 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
284 ZeroMemory(mbs, 5);
286 /* Now this shouldn't be the case like above as we zeroed the complete string buffer. */
287 SetLastError(0xdeadbeef);
288 ret = WideCharToMultiByte(1252, 0, wcs, 3, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
289 ok(ret == 3, "ret is %d\n", ret);
290 ok(!strcmp(mbs, "Th?"), "mbs is %s\n", mbs);
291 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
292 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
294 /* Double-byte tests */
295 ret = WideCharToMultiByte(1252, 0, dbwcs, 3, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
296 ok(ret == 3, "ret is %d\n", ret);
297 ok(!strcmp(mbs, "??"), "mbs is %s\n", mbs);
298 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
300 /* Length-only tests */
301 SetLastError(0xdeadbeef);
302 ret = WideCharToMultiByte(1252, 0, &wc2, 1, NULL, 0, NULL, bUsedDefaultChar);
303 ok(ret == 1, "ret is %d\n", ret);
304 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
305 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
307 SetLastError(0xdeadbeef);
308 ret = WideCharToMultiByte(1252, 0, wcs, -1, NULL, 0, NULL, bUsedDefaultChar);
309 ok(ret == 5, "ret is %d\n", ret);
310 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
311 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
313 if (!IsValidCodePage(950))
315 skip("Codepage 950 not available\n");
316 return;
319 /* Double-byte tests */
320 SetLastError(0xdeadbeef);
321 ret = WideCharToMultiByte(950, 0, dbwcs, -1, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
322 ok(ret == 5, "ret is %d\n", ret);
323 ok(!strcmp(mbs, "µH©Ò"), "mbs is %s\n", mbs);
324 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
325 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
327 SetLastError(0xdeadbeef);
328 ret = WideCharToMultiByte(950, 0, dbwcs, 1, &mbc, 1, NULL, bUsedDefaultChar);
329 ok(ret == 0, "ret is %d\n", ret);
330 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
331 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() is %u\n", GetLastError());
332 ZeroMemory(mbs, 5);
334 SetLastError(0xdeadbeef);
335 ret = WideCharToMultiByte(950, 0, dbwcs, 1, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
336 ok(ret == 2, "ret is %d\n", ret);
337 ok(!strcmp(mbs, "µH"), "mbs is %s\n", mbs);
338 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
339 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
341 /* Length-only tests */
342 SetLastError(0xdeadbeef);
343 ret = WideCharToMultiByte(950, 0, dbwcs, 1, NULL, 0, NULL, bUsedDefaultChar);
344 ok(ret == 2, "ret is %d\n", ret);
345 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
346 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
348 SetLastError(0xdeadbeef);
349 ret = WideCharToMultiByte(950, 0, dbwcs, -1, NULL, 0, NULL, bUsedDefaultChar);
350 ok(ret == 5, "ret is %d\n", ret);
351 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
352 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
355 START_TEST(codepage)
357 BOOL bUsedDefaultChar;
359 test_destination_buffer();
360 test_null_source();
361 test_negative_source_length();
362 test_negative_dest_length();
363 test_overlapped_buffers();
365 /* WideCharToMultiByte has two code paths, test both here */
366 test_string_conversion(NULL);
367 test_string_conversion(&bUsedDefaultChar);