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%lx/%ld (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%lx/%ld 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%lx/%ld 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%lx/%ld 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%lx/%ld 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%lx/%ld (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%lx/%ld (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=%ld (expected 0 with ERROR_INVALID_PARAMETER)\n",
106 /* lstrcmpW is not supported on Win9x! */
107 static int mylstrcmpW(const WCHAR
* str1
, const WCHAR
* str2
)
109 while (*str1
&& *str1
==*str2
) {
116 static void test_negative_source_length(void)
122 /* Test, whether any negative source length works as strlen() + 1 */
123 SetLastError( 0xdeadbeef );
124 memset(buf
,'x',sizeof(buf
));
125 len
= WideCharToMultiByte(CP_ACP
, 0, foobarW
, -2002, buf
, 10, NULL
, NULL
);
126 ok(len
== 7 && !lstrcmpA(buf
, "foobar") && GetLastError() == 0xdeadbeef,
127 "WideCharToMultiByte(-2002): len=%d error=%ld\n",len
,GetLastError());
129 SetLastError( 0xdeadbeef );
130 memset(bufW
,'x',sizeof(bufW
));
131 len
= MultiByteToWideChar(CP_ACP
, 0, "foobar", -2002, bufW
, 10);
132 ok(len
== 7 && !mylstrcmpW(bufW
, foobarW
) && GetLastError() == 0xdeadbeef,
133 "MultiByteToWideChar(-2002): len=%d error=%ld\n",len
,GetLastError());
136 static void test_overlapped_buffers(void)
138 static const WCHAR strW
[] = {'j','u','s','t',' ','a',' ','t','e','s','t',0};
139 static const char strA
[] = "just a test";
143 lstrcpyW((WCHAR
*)(buf
+ 1), strW
);
144 ret
= WideCharToMultiByte(CP_ACP
, 0, (WCHAR
*)(buf
+ 1), -1, buf
, sizeof(buf
), NULL
, NULL
);
145 ok(ret
== sizeof(strA
), "unexpected ret %d != %d\n", ret
, sizeof(strA
));
146 ok(!memcmp(buf
, strA
, sizeof(strA
)), "conversion failed: %s\n", buf
);
151 test_destination_buffer();
153 test_negative_source_length();
154 test_overlapped_buffers();