2002-10-04 19:42:27 +02:00
|
|
|
/*
|
|
|
|
* Unit tests for code page to/from unicode translations
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002 Dmitry Timoshkov
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-10-04 19:42:27 +02:00
|
|
|
*/
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2002-10-04 19:42:27 +02:00
|
|
|
#include "wine/test.h"
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2002-10-04 19:42:27 +02:00
|
|
|
#include "winbase.h"
|
|
|
|
#include "winnls.h"
|
|
|
|
|
2006-06-05 12:30:41 +02:00
|
|
|
static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
|
|
|
|
|
|
|
|
static void test_destination_buffer(void)
|
|
|
|
{
|
|
|
|
LPSTR buffer;
|
|
|
|
INT maxsize;
|
|
|
|
INT needed;
|
|
|
|
INT len;
|
|
|
|
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
needed = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, NULL, 0, NULL, NULL);
|
2007-05-18 13:18:56 +02:00
|
|
|
ok( (needed > 0), "returned %d with %u (expected '> 0')\n",
|
|
|
|
needed, GetLastError());
|
2006-06-05 12:30:41 +02:00
|
|
|
|
|
|
|
maxsize = needed*2;
|
|
|
|
buffer = HeapAlloc(GetProcessHeap(), 0, maxsize);
|
|
|
|
if (buffer == NULL) return;
|
|
|
|
|
|
|
|
maxsize--;
|
|
|
|
memset(buffer, 'x', maxsize);
|
|
|
|
buffer[maxsize] = '\0';
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed+1, NULL, NULL);
|
2007-05-18 13:18:56 +02:00
|
|
|
ok( (len > 0), "returned %d with %u and '%s' (expected '> 0')\n",
|
|
|
|
len, GetLastError(), buffer);
|
2006-06-05 12:30:41 +02:00
|
|
|
|
|
|
|
memset(buffer, 'x', maxsize);
|
|
|
|
buffer[maxsize] = '\0';
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed, NULL, NULL);
|
2007-05-18 13:18:56 +02:00
|
|
|
ok( (len > 0), "returned %d with %u and '%s' (expected '> 0')\n",
|
|
|
|
len, GetLastError(), buffer);
|
2006-06-05 12:30:41 +02:00
|
|
|
|
|
|
|
memset(buffer, 'x', maxsize);
|
|
|
|
buffer[maxsize] = '\0';
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed-1, NULL, NULL);
|
|
|
|
ok( !len && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
|
2007-05-18 13:18:56 +02:00
|
|
|
"returned %d with %u and '%s' (expected '0' with "
|
|
|
|
"ERROR_INSUFFICIENT_BUFFER)\n", len, GetLastError(), buffer);
|
2006-06-05 12:30:41 +02:00
|
|
|
|
|
|
|
memset(buffer, 'x', maxsize);
|
|
|
|
buffer[maxsize] = '\0';
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, 1, NULL, NULL);
|
|
|
|
ok( !len && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
|
2007-05-18 13:18:56 +02:00
|
|
|
"returned %d with %u and '%s' (expected '0' with "
|
|
|
|
"ERROR_INSUFFICIENT_BUFFER)\n", len, GetLastError(), buffer);
|
2006-06-05 12:30:41 +02:00
|
|
|
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, 0, NULL, NULL);
|
2007-05-18 13:18:56 +02:00
|
|
|
ok( (len > 0), "returned %d with %u (expected '> 0')\n",
|
|
|
|
len, GetLastError());
|
2006-06-05 12:30:41 +02:00
|
|
|
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, NULL, needed, NULL, NULL);
|
|
|
|
ok( !len && (GetLastError() == ERROR_INVALID_PARAMETER),
|
2007-05-18 13:18:56 +02:00
|
|
|
"returned %d with %u (expected '0' with "
|
|
|
|
"ERROR_INVALID_PARAMETER)\n", len, GetLastError());
|
2006-06-05 12:30:41 +02:00
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-18 12:50:46 +02:00
|
|
|
static void test_null_source(void)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
DWORD GLE;
|
|
|
|
|
|
|
|
SetLastError(0);
|
|
|
|
len = WideCharToMultiByte(CP_ACP, 0, NULL, 0, NULL, 0, NULL, NULL);
|
|
|
|
GLE = GetLastError();
|
|
|
|
ok(!len && GLE == ERROR_INVALID_PARAMETER,
|
2007-05-18 13:18:56 +02:00
|
|
|
"WideCharToMultiByte returned %d with GLE=%u (expected 0 with ERROR_INVALID_PARAMETER)\n",
|
2005-08-18 12:50:46 +02:00
|
|
|
len, GLE);
|
2006-11-23 21:24:33 +01:00
|
|
|
|
|
|
|
SetLastError(0);
|
|
|
|
len = WideCharToMultiByte(CP_ACP, 0, NULL, -1, NULL, 0, NULL, NULL);
|
|
|
|
GLE = GetLastError();
|
|
|
|
ok(!len && GLE == ERROR_INVALID_PARAMETER,
|
2007-05-18 13:18:56 +02:00
|
|
|
"WideCharToMultiByte returned %d with GLE=%u (expected 0 with ERROR_INVALID_PARAMETER)\n",
|
2006-11-23 21:24:33 +01:00
|
|
|
len, GLE);
|
2005-08-18 12:50:46 +02:00
|
|
|
}
|
|
|
|
|
2002-12-12 03:16:29 +01:00
|
|
|
/* lstrcmpW is not supported on Win9x! */
|
|
|
|
static int mylstrcmpW(const WCHAR* str1, const WCHAR* str2)
|
|
|
|
{
|
|
|
|
while (*str1 && *str1==*str2) {
|
|
|
|
str1++;
|
|
|
|
str2++;
|
|
|
|
}
|
|
|
|
return *str1-*str2;
|
|
|
|
}
|
|
|
|
|
2002-10-04 19:42:27 +02:00
|
|
|
static void test_negative_source_length(void)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char buf[10];
|
|
|
|
WCHAR bufW[10];
|
|
|
|
|
|
|
|
/* Test, whether any negative source length works as strlen() + 1 */
|
|
|
|
SetLastError( 0xdeadbeef );
|
2002-12-12 03:16:29 +01:00
|
|
|
memset(buf,'x',sizeof(buf));
|
2002-10-04 19:42:27 +02:00
|
|
|
len = WideCharToMultiByte(CP_ACP, 0, foobarW, -2002, buf, 10, NULL, NULL);
|
|
|
|
ok(len == 7 && !lstrcmpA(buf, "foobar") && GetLastError() == 0xdeadbeef,
|
2007-05-18 13:18:56 +02:00
|
|
|
"WideCharToMultiByte(-2002): len=%d error=%u\n", len, GetLastError());
|
2002-10-04 19:42:27 +02:00
|
|
|
|
|
|
|
SetLastError( 0xdeadbeef );
|
2002-12-12 03:16:29 +01:00
|
|
|
memset(bufW,'x',sizeof(bufW));
|
2002-10-04 19:42:27 +02:00
|
|
|
len = MultiByteToWideChar(CP_ACP, 0, "foobar", -2002, bufW, 10);
|
2002-12-12 03:16:29 +01:00
|
|
|
ok(len == 7 && !mylstrcmpW(bufW, foobarW) && GetLastError() == 0xdeadbeef,
|
2007-05-18 13:18:56 +02:00
|
|
|
"MultiByteToWideChar(-2002): len=%d error=%u\n", len, GetLastError());
|
2007-05-18 13:21:35 +02:00
|
|
|
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
memset(bufW, 'x', sizeof(bufW));
|
|
|
|
len = MultiByteToWideChar(CP_ACP, 0, "foobar", -1, bufW, 6);
|
|
|
|
ok(len == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
|
|
|
|
"MultiByteToWideChar(-1): len=%d error=%u\n", len, GetLastError());
|
2002-10-04 19:42:27 +02:00
|
|
|
}
|
|
|
|
|
2005-07-15 18:33:10 +02:00
|
|
|
static void test_overlapped_buffers(void)
|
|
|
|
{
|
|
|
|
static const WCHAR strW[] = {'j','u','s','t',' ','a',' ','t','e','s','t',0};
|
|
|
|
static const char strA[] = "just a test";
|
|
|
|
char buf[256];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
lstrcpyW((WCHAR *)(buf + 1), strW);
|
|
|
|
ret = WideCharToMultiByte(CP_ACP, 0, (WCHAR *)(buf + 1), -1, buf, sizeof(buf), NULL, NULL);
|
2006-06-13 14:02:31 +02:00
|
|
|
ok(ret == sizeof(strA), "unexpected ret %d\n", ret);
|
2005-07-15 18:33:10 +02:00
|
|
|
ok(!memcmp(buf, strA, sizeof(strA)), "conversion failed: %s\n", buf);
|
|
|
|
}
|
|
|
|
|
2002-10-04 19:42:27 +02:00
|
|
|
START_TEST(codepage)
|
|
|
|
{
|
2006-06-05 12:30:41 +02:00
|
|
|
test_destination_buffer();
|
2005-08-18 12:50:46 +02:00
|
|
|
test_null_source();
|
2002-10-04 19:42:27 +02:00
|
|
|
test_negative_source_length();
|
2005-07-15 18:33:10 +02:00
|
|
|
test_overlapped_buffers();
|
2002-10-04 19:42:27 +02:00
|
|
|
}
|