kernel32/tests: In multiline strings there is no need for '\' at the end of the lines.
[wine/wine-kai.git] / dlls / kernel32 / tests / codepage.c
blob2cd0557b4f66266e5bdacb89f3642550c8e4890e
1 /*
2 * Unit tests for code page to/from unicode translations
4 * Copyright (c) 2002 Dmitry Timoshkov
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #include "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
28 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
30 static void test_destination_buffer(void)
32 LPSTR buffer;
33 INT maxsize;
34 INT needed;
35 INT len;
37 SetLastError(0xdeadbeef);
38 needed = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, NULL, 0, NULL, NULL);
39 ok( (needed > 0), "returned %d with 0x%x/%d (expected '> 0')\n",
40 needed, GetLastError(), GetLastError());
42 maxsize = needed*2;
43 buffer = HeapAlloc(GetProcessHeap(), 0, maxsize);
44 if (buffer == NULL) return;
46 maxsize--;
47 memset(buffer, 'x', maxsize);
48 buffer[maxsize] = '\0';
49 SetLastError(0xdeadbeef);
50 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed+1, NULL, NULL);
51 ok( (len > 0), "returned %d with 0x%x/%d and '%s' (expected '> 0')\n",
52 len, GetLastError(), GetLastError(), buffer);
54 memset(buffer, 'x', maxsize);
55 buffer[maxsize] = '\0';
56 SetLastError(0xdeadbeef);
57 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed, NULL, NULL);
58 ok( (len > 0), "returned %d with 0x%x/%d and '%s' (expected '> 0')\n",
59 len, GetLastError(), GetLastError(), buffer);
61 memset(buffer, 'x', maxsize);
62 buffer[maxsize] = '\0';
63 SetLastError(0xdeadbeef);
64 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed-1, NULL, NULL);
65 ok( !len && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
66 "returned %d with 0x%x/%d and '%s' (expected '0' with "
67 "ERROR_INSUFFICIENT_BUFFER)\n", len, GetLastError(), GetLastError(), buffer);
69 memset(buffer, 'x', maxsize);
70 buffer[maxsize] = '\0';
71 SetLastError(0xdeadbeef);
72 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, 1, NULL, NULL);
73 ok( !len && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
74 "returned %d with 0x%x/%d and '%s' (expected '0' with "
75 "ERROR_INSUFFICIENT_BUFFER)\n", len, GetLastError(), GetLastError(), buffer);
77 SetLastError(0xdeadbeef);
78 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, 0, NULL, NULL);
79 ok( (len > 0), "returned %d with 0x%x/%d (expected '> 0')\n",
80 len, GetLastError(), GetLastError());
82 SetLastError(0xdeadbeef);
83 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, NULL, needed, NULL, NULL);
84 ok( !len && (GetLastError() == ERROR_INVALID_PARAMETER),
85 "returned %d with 0x%x/%d (expected '0' with "
86 "ERROR_INVALID_PARAMETER)\n", len, GetLastError(), GetLastError());
88 HeapFree(GetProcessHeap(), 0, buffer);
93 static void test_null_source(void)
95 int len;
96 DWORD GLE;
98 SetLastError(0);
99 len = WideCharToMultiByte(CP_ACP, 0, NULL, 0, NULL, 0, NULL, NULL);
100 GLE = GetLastError();
101 ok(!len && GLE == ERROR_INVALID_PARAMETER,
102 "WideCharToMultiByte returned %d with GLE=%d (expected 0 with ERROR_INVALID_PARAMETER)\n",
103 len, GLE);
105 SetLastError(0);
106 len = WideCharToMultiByte(CP_ACP, 0, NULL, -1, NULL, 0, NULL, NULL);
107 GLE = GetLastError();
108 ok(!len && GLE == ERROR_INVALID_PARAMETER,
109 "WideCharToMultiByte returned %d with GLE=%d (expected 0 with ERROR_INVALID_PARAMETER)\n",
110 len, GLE);
113 /* lstrcmpW is not supported on Win9x! */
114 static int mylstrcmpW(const WCHAR* str1, const WCHAR* str2)
116 while (*str1 && *str1==*str2) {
117 str1++;
118 str2++;
120 return *str1-*str2;
123 static void test_negative_source_length(void)
125 int len;
126 char buf[10];
127 WCHAR bufW[10];
129 /* Test, whether any negative source length works as strlen() + 1 */
130 SetLastError( 0xdeadbeef );
131 memset(buf,'x',sizeof(buf));
132 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -2002, buf, 10, NULL, NULL);
133 ok(len == 7 && !lstrcmpA(buf, "foobar") && GetLastError() == 0xdeadbeef,
134 "WideCharToMultiByte(-2002): len=%d error=%d\n",len,GetLastError());
136 SetLastError( 0xdeadbeef );
137 memset(bufW,'x',sizeof(bufW));
138 len = MultiByteToWideChar(CP_ACP, 0, "foobar", -2002, bufW, 10);
139 ok(len == 7 && !mylstrcmpW(bufW, foobarW) && GetLastError() == 0xdeadbeef,
140 "MultiByteToWideChar(-2002): len=%d error=%d\n",len,GetLastError());
143 static void test_overlapped_buffers(void)
145 static const WCHAR strW[] = {'j','u','s','t',' ','a',' ','t','e','s','t',0};
146 static const char strA[] = "just a test";
147 char buf[256];
148 int ret;
150 lstrcpyW((WCHAR *)(buf + 1), strW);
151 ret = WideCharToMultiByte(CP_ACP, 0, (WCHAR *)(buf + 1), -1, buf, sizeof(buf), NULL, NULL);
152 ok(ret == sizeof(strA), "unexpected ret %d\n", ret);
153 ok(!memcmp(buf, strA, sizeof(strA)), "conversion failed: %s\n", buf);
156 START_TEST(codepage)
158 test_destination_buffer();
159 test_null_source();
160 test_negative_source_length();
161 test_overlapped_buffers();