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 0x%x/%d (expected '> 0')\n",
40 needed, GetLastError(), 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 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)
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",
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",
113 /* lstrcmpW is not supported on Win9x! */
114 static int mylstrcmpW(const WCHAR* str1, const WCHAR* str2)
116 while (*str1 && *str1==*str2) {
123 static void test_negative_source_length(void)
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";
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);
158 test_destination_buffer();
160 test_negative_source_length();
161 test_overlapped_buffers();