kernel32: Make the test compatible with win9x.
[wine.git] / dlls / kernel32 / tests / codepage.c
blob6f1582667764baffa35e67c8ac96196b4f06b50c
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 %u (expected '> 0')\n",
40 needed, 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 %u and '%s' (expected '> 0')\n",
52 len, 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 %u and '%s' (expected '> 0')\n",
59 len, 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 %u and '%s' (expected '0' with "
67 "ERROR_INSUFFICIENT_BUFFER)\n", len, 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 %u and '%s' (expected '0' with "
75 "ERROR_INSUFFICIENT_BUFFER)\n", len, GetLastError(), buffer);
77 SetLastError(0xdeadbeef);
78 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, 0, NULL, NULL);
79 ok( (len > 0), "returned %d with %u (expected '> 0')\n",
80 len, 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 %u (expected '0' with "
86 "ERROR_INVALID_PARAMETER)\n", len, GetLastError());
88 HeapFree(GetProcessHeap(), 0, buffer);
92 static void test_null_source(void)
94 int len;
95 DWORD GLE;
97 SetLastError(0);
98 len = WideCharToMultiByte(CP_ACP, 0, NULL, 0, NULL, 0, NULL, NULL);
99 GLE = GetLastError();
100 ok(!len && GLE == ERROR_INVALID_PARAMETER,
101 "WideCharToMultiByte returned %d with GLE=%u (expected 0 with ERROR_INVALID_PARAMETER)\n",
102 len, GLE);
104 SetLastError(0);
105 len = WideCharToMultiByte(CP_ACP, 0, NULL, -1, NULL, 0, NULL, NULL);
106 GLE = GetLastError();
107 ok(!len && GLE == ERROR_INVALID_PARAMETER,
108 "WideCharToMultiByte returned %d with GLE=%u (expected 0 with ERROR_INVALID_PARAMETER)\n",
109 len, GLE);
112 /* lstrcmpW is not supported on Win9x! */
113 static int mylstrcmpW(const WCHAR* str1, const WCHAR* str2)
115 while (*str1 && *str1==*str2) {
116 str1++;
117 str2++;
119 return *str1-*str2;
122 static void test_negative_source_length(void)
124 int len;
125 char buf[10];
126 WCHAR bufW[10];
128 /* Test, whether any negative source length works as strlen() + 1 */
129 SetLastError( 0xdeadbeef );
130 memset(buf,'x',sizeof(buf));
131 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -2002, buf, 10, NULL, NULL);
132 ok(len == 7 && !lstrcmpA(buf, "foobar") && GetLastError() == 0xdeadbeef,
133 "WideCharToMultiByte(-2002): len=%d error=%u\n", len, GetLastError());
135 SetLastError( 0xdeadbeef );
136 memset(bufW,'x',sizeof(bufW));
137 len = MultiByteToWideChar(CP_ACP, 0, "foobar", -2002, bufW, 10);
138 ok(len == 7 && !mylstrcmpW(bufW, foobarW) && GetLastError() == 0xdeadbeef,
139 "MultiByteToWideChar(-2002): len=%d error=%u\n", len, GetLastError());
141 SetLastError(0xdeadbeef);
142 memset(bufW, 'x', sizeof(bufW));
143 len = MultiByteToWideChar(CP_ACP, 0, "foobar", -1, bufW, 6);
144 ok(len == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
145 "MultiByteToWideChar(-1): len=%d error=%u\n", len, GetLastError());
148 static void test_overlapped_buffers(void)
150 static const WCHAR strW[] = {'j','u','s','t',' ','a',' ','t','e','s','t',0};
151 static const char strA[] = "just a test";
152 char buf[256];
153 int ret;
155 memcpy((WCHAR *)(buf + 1), strW, sizeof(strW));
156 ret = WideCharToMultiByte(CP_ACP, 0, (WCHAR *)(buf + 1), -1, buf, sizeof(buf), NULL, NULL);
157 ok(ret == sizeof(strA), "unexpected ret %d\n", ret);
158 ok(!memcmp(buf, strA, sizeof(strA)), "conversion failed: %s\n", buf);
161 START_TEST(codepage)
163 test_destination_buffer();
164 test_null_source();
165 test_negative_source_length();
166 test_overlapped_buffers();