quartz: Make dwSamplesProcessed a longlong.
[wine] / dlls / kernel32 / tests / codepage.c
1 /*
2  * Unit tests for code page to/from unicode translations
3  *
4  * Copyright (c) 2002 Dmitry Timoshkov
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <limits.h>
23
24 #include "wine/test.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winnls.h"
28
29 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
30
31 static void test_destination_buffer(void)
32 {
33     LPSTR   buffer;
34     INT     maxsize;
35     INT     needed;
36     INT     len;
37
38     SetLastError(0xdeadbeef);
39     needed = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, NULL, 0, NULL, NULL);
40     ok( (needed > 0), "returned %d with %u (expected '> 0')\n",
41         needed, GetLastError());
42
43     maxsize = needed*2;
44     buffer = HeapAlloc(GetProcessHeap(), 0, maxsize);
45     if (buffer == NULL) return;
46
47     maxsize--;
48     memset(buffer, 'x', maxsize);
49     buffer[maxsize] = '\0';
50     SetLastError(0xdeadbeef);
51     len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed+1, NULL, NULL);
52     ok( (len > 0), "returned %d with %u and '%s' (expected '> 0')\n",
53         len, GetLastError(), buffer);
54
55     memset(buffer, 'x', maxsize);
56     buffer[maxsize] = '\0';
57     SetLastError(0xdeadbeef);
58     len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed, NULL, NULL);
59     ok( (len > 0), "returned %d with %u and '%s' (expected '> 0')\n",
60         len, GetLastError(), buffer);
61
62     memset(buffer, 'x', maxsize);
63     buffer[maxsize] = '\0';
64     SetLastError(0xdeadbeef);
65     len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed-1, NULL, NULL);
66     ok( !len && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
67         "returned %d with %u and '%s' (expected '0' with "
68         "ERROR_INSUFFICIENT_BUFFER)\n", len, GetLastError(), buffer);
69
70     memset(buffer, 'x', maxsize);
71     buffer[maxsize] = '\0';
72     SetLastError(0xdeadbeef);
73     len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, 1, NULL, NULL);
74     ok( !len && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
75         "returned %d with %u and '%s' (expected '0' with "
76         "ERROR_INSUFFICIENT_BUFFER)\n", len, GetLastError(), buffer);
77
78     SetLastError(0xdeadbeef);
79     len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, 0, NULL, NULL);
80     ok( (len > 0), "returned %d with %u (expected '> 0')\n",
81         len, GetLastError());
82
83     SetLastError(0xdeadbeef);
84     len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, NULL, needed, NULL, NULL);
85     ok( !len && (GetLastError() == ERROR_INVALID_PARAMETER),
86         "returned %d with %u (expected '0' with "
87         "ERROR_INVALID_PARAMETER)\n", len, GetLastError());
88
89     HeapFree(GetProcessHeap(), 0, buffer);
90 }
91
92
93 static void test_null_source(void)
94 {
95     int len;
96     DWORD GLE;
97
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=%u (expected 0 with ERROR_INVALID_PARAMETER)\n",
103         len, GLE);
104
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=%u (expected 0 with ERROR_INVALID_PARAMETER)\n",
110         len, GLE);
111 }
112
113 /* lstrcmpW is not supported on Win9x! */
114 static int mylstrcmpW(const WCHAR* str1, const WCHAR* str2)
115 {
116     while (*str1 && *str1==*str2) {
117         str1++;
118         str2++;
119     }
120     return *str1-*str2;
121 }
122
123 static void test_negative_source_length(void)
124 {
125     int len;
126     char buf[10];
127     WCHAR bufW[10];
128
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=%u\n", len, GetLastError());
135
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=%u\n", len, GetLastError());
141
142     SetLastError(0xdeadbeef);
143     memset(bufW, 'x', sizeof(bufW));
144     len = MultiByteToWideChar(CP_ACP, 0, "foobar", -1, bufW, 6);
145     ok(len == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
146        "MultiByteToWideChar(-1): len=%d error=%u\n", len, GetLastError());
147 }
148
149 #define LONGBUFLEN 100000
150 static void test_negative_dest_length(void)
151 {
152     int len, i;
153     static char buf[LONGBUFLEN];
154     static WCHAR originalW[LONGBUFLEN];
155     static char originalA[LONGBUFLEN];
156     DWORD theError;
157
158     /* Test return on -1 dest length */
159     SetLastError( 0xdeadbeef );
160     memset(buf,'x',sizeof(buf));
161     len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buf, -1, NULL, NULL);
162     todo_wine {
163       ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
164          "WideCharToMultiByte(destlen -1): len=%d error=%x\n", len, GetLastError());
165     }
166
167     /* Test return on -1000 dest length */
168     SetLastError( 0xdeadbeef );
169     memset(buf,'x',sizeof(buf));
170     len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buf, -1000, NULL, NULL);
171     todo_wine {
172       ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
173          "WideCharToMultiByte(destlen -1000): len=%d error=%x\n", len, GetLastError());
174     }
175
176     /* Test return on INT_MAX dest length */
177     SetLastError( 0xdeadbeef );
178     memset(buf,'x',sizeof(buf));
179     len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buf, INT_MAX, NULL, NULL);
180     ok(len == 7 && !lstrcmpA(buf, "foobar") && GetLastError() == 0xdeadbeef,
181        "WideCharToMultiByte(destlen INT_MAX): len=%d error=%x\n", len, GetLastError());
182
183     /* Test return on INT_MAX dest length and very long input */
184     SetLastError( 0xdeadbeef );
185     memset(buf,'x',sizeof(buf));
186     for (i=0; i < LONGBUFLEN - 1; i++) {
187         originalW[i] = 'Q';
188         originalA[i] = 'Q';
189     }
190     originalW[LONGBUFLEN-1] = 0;
191     originalA[LONGBUFLEN-1] = 0;
192     len = WideCharToMultiByte(CP_ACP, 0, originalW, -1, buf, INT_MAX, NULL, NULL);
193     theError = GetLastError();
194     ok(len == LONGBUFLEN && !lstrcmpA(buf, originalA) && theError == 0xdeadbeef,
195        "WideCharToMultiByte(srclen %d, destlen INT_MAX): len %d error=%x\n", LONGBUFLEN, len, theError);
196
197 }
198
199 static void test_overlapped_buffers(void)
200 {
201     static const WCHAR strW[] = {'j','u','s','t',' ','a',' ','t','e','s','t',0};
202     static const char strA[] = "just a test";
203     char buf[256];
204     int ret;
205
206     memcpy((WCHAR *)(buf + 1), strW, sizeof(strW));
207     ret = WideCharToMultiByte(CP_ACP, 0, (WCHAR *)(buf + 1), -1, buf, sizeof(buf), NULL, NULL);
208     ok(ret == sizeof(strA), "unexpected ret %d\n", ret);
209     ok(!memcmp(buf, strA, sizeof(strA)), "conversion failed: %s\n", buf);
210 }
211
212 START_TEST(codepage)
213 {
214     test_destination_buffer();
215     test_null_source();
216     test_negative_source_length();
217     test_negative_dest_length();
218     test_overlapped_buffers();
219 }