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
23 #include "wine/test.h"
28 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
30 static void test_destination_buffer(void)
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());
43 buffer = HeapAlloc(GetProcessHeap(), 0, maxsize);
44 if (buffer == NULL) return;
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",
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)
98 len = WideCharToMultiByte(CP_ACP, 0, NULL, 0, NULL, 0, NULL, NULL);
100 ok(!len && GLE == ERROR_INVALID_PARAMETER,
101 "WideCharToMultiByte returned %d with GLE=%u (expected 0 with ERROR_INVALID_PARAMETER)\n",
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",
112 /* lstrcmpW is not supported on Win9x! */
113 static int mylstrcmpW(const WCHAR* str1, const WCHAR* str2)
115 while (*str1 && *str1==*str2) {
122 static void test_negative_source_length(void)
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";
155 SetLastError(0xdeadbeef);
156 lstrcpyW((WCHAR *)(buf + 1), strW);
157 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
159 skip("lstrcypW is not implemented\n");
162 ret = WideCharToMultiByte(CP_ACP, 0, (WCHAR *)(buf + 1), -1, buf, sizeof(buf), NULL, NULL);
163 ok(ret == sizeof(strA), "unexpected ret %d\n", ret);
164 ok(!memcmp(buf, strA, sizeof(strA)), "conversion failed: %s\n", buf);
169 test_destination_buffer();
171 test_negative_source_length();
172 test_overlapped_buffers();