Sweden-Number/dlls/kernel/tests/atom.c

192 lines
6.1 KiB
C
Raw Normal View History

2002-07-31 19:45:00 +02:00
/*
* Unit tests for atom functions
*
* Copyright (c) 2002 Alexandre Julliard
*
* 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
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdarg.h>
2002-07-31 19:45:00 +02:00
#include <stdio.h>
#include "wine/test.h"
#include "windef.h"
2002-07-31 19:45:00 +02:00
#include "winbase.h"
#include "winerror.h"
static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
static const WCHAR FOOBARW[] = {'F','O','O','B','A','R',0};
static const WCHAR _foobarW[] = {'_','f','o','o','b','a','r',0};
2002-10-04 19:45:00 +02:00
static BOOL unicode_OS;
2002-07-31 19:45:00 +02:00
static void test_add_atom(void)
{
ATOM atom, w_atom;
int i;
SetLastError( 0xdeadbeef );
atom = GlobalAddAtomA( "foobar" );
2004-01-23 03:10:33 +01:00
ok( atom >= 0xc000, "bad atom id %x\n", atom );
ok( GetLastError() == 0xdeadbeef, "GlobalAddAtomA set last error\n" );
2002-07-31 19:45:00 +02:00
/* Verify that it can be found (or not) appropriately */
2004-01-23 03:10:33 +01:00
ok( GlobalFindAtomA( "foobar" ) == atom, "could not find atom foobar\n" );
ok( GlobalFindAtomA( "FOOBAR" ) == atom, "could not find atom FOOBAR\n" );
ok( !GlobalFindAtomA( "_foobar" ), "found _foobar\n" );
2002-07-31 19:45:00 +02:00
/* Add the same atom, specifying string as unicode; should
* find the first one, not add a new one */
SetLastError( 0xdeadbeef );
w_atom = GlobalAddAtomW( foobarW );
2002-10-04 19:45:00 +02:00
if (w_atom && GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
unicode_OS = TRUE;
else
trace("WARNING: Unicode atom APIs are not supported on this platform\n");
if (unicode_OS)
{
2004-01-23 03:10:33 +01:00
ok( w_atom == atom, "Unicode atom does not match ASCII\n" );
ok( GetLastError() == 0xdeadbeef, "GlobalAddAtomW set last error\n" );
2002-10-04 19:45:00 +02:00
}
2002-07-31 19:45:00 +02:00
/* Verify that it can be found (or not) appropriately via unicode name */
2002-10-04 19:45:00 +02:00
if (unicode_OS)
{
2004-01-23 03:10:33 +01:00
ok( GlobalFindAtomW( foobarW ) == atom, "could not find atom foobar\n" );
ok( GlobalFindAtomW( FOOBARW ) == atom, "could not find atom FOOBAR\n" );
ok( !GlobalFindAtomW( _foobarW ), "found _foobar\n" );
2002-10-04 19:45:00 +02:00
}
2002-07-31 19:45:00 +02:00
/* Test integer atoms
2002-10-04 19:45:00 +02:00
* (0x0001 .. 0xbfff) should be valid;
2002-07-31 19:45:00 +02:00
* (0xc000 .. 0xffff) should be invalid */
2002-10-04 19:45:00 +02:00
SetLastError( 0xdeadbeef );
2004-01-23 03:10:33 +01:00
ok( GlobalAddAtomA(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
2002-10-04 19:45:00 +02:00
if (unicode_OS)
2002-07-31 19:45:00 +02:00
{
SetLastError( 0xdeadbeef );
2004-01-23 03:10:33 +01:00
ok( GlobalAddAtomW(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
2002-07-31 19:45:00 +02:00
}
2002-10-04 19:45:00 +02:00
2002-07-31 19:45:00 +02:00
SetLastError( 0xdeadbeef );
for (i = 1; i <= 0xbfff; i++)
{
SetLastError( 0xdeadbeef );
ok( GlobalAddAtomA((LPCSTR)i) == i && GetLastError() == 0xdeadbeef,
2004-01-23 03:10:33 +01:00
"failed to add atom %x\n", i );
2002-10-04 19:45:00 +02:00
if (unicode_OS)
{
SetLastError( 0xdeadbeef );
ok( GlobalAddAtomW((LPCWSTR)i) == i && GetLastError() == 0xdeadbeef,
2004-01-23 03:10:33 +01:00
"failed to add atom %x\n", i );
2002-10-04 19:45:00 +02:00
}
2002-07-31 19:45:00 +02:00
}
2002-10-04 19:45:00 +02:00
2002-07-31 19:45:00 +02:00
for (i = 0xc000; i <= 0xffff; i++)
{
2004-01-23 03:10:33 +01:00
ok( !GlobalAddAtomA((LPCSTR)i), "succeeded adding %x\n", i );
2002-10-04 19:45:00 +02:00
if (unicode_OS)
2004-01-23 03:10:33 +01:00
ok( !GlobalAddAtomW((LPCWSTR)i), "succeeded adding %x\n", i );
2002-07-31 19:45:00 +02:00
}
}
static void test_get_atom_name(void)
{
char buf[10];
WCHAR bufW[10];
int i;
UINT len;
2002-10-04 19:45:00 +02:00
static const WCHAR resultW[] = {'f','o','o','b','a','r',0,'.','.','.'};
2002-07-31 19:45:00 +02:00
ATOM atom = GlobalAddAtomA( "foobar" );
/* Get the name of the atom we added above */
memset( buf, '.', sizeof(buf) );
len = GlobalGetAtomNameA( atom, buf, 10 );
2004-01-23 03:10:33 +01:00
ok( len == strlen("foobar"), "bad length %d\n", len );
ok( !memcmp( buf, "foobar\0...", 10 ), "bad buffer contents\n" );
2002-07-31 19:45:00 +02:00
/* Repeat, unicode-style */
2002-10-04 19:45:00 +02:00
if (unicode_OS)
2002-07-31 19:45:00 +02:00
{
2002-10-04 19:45:00 +02:00
for (i = 0; i < 10; i++) bufW[i] = '.';
SetLastError( 0xdeadbeef );
len = GlobalGetAtomNameW( atom, bufW, 10 );
2004-01-23 03:10:33 +01:00
ok( len && GetLastError() == 0xdeadbeef, "GlobalGetAtomNameW failed\n" );
ok( len == lstrlenW(foobarW), "bad length %d\n", len );
ok( !memcmp( bufW, resultW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
2002-07-31 19:45:00 +02:00
}
/* Check error code returns */
2002-10-04 19:45:00 +02:00
memset(buf, '.', 10);
2004-01-23 03:10:33 +01:00
ok( !GlobalGetAtomNameA( atom, buf, 0 ), "succeeded\n" );
ok( !memcmp( buf, "..........", 10 ), "should not touch buffer\n" );
2002-10-04 19:45:00 +02:00
if (unicode_OS)
{
static const WCHAR sampleW[10] = {'.','.','.','.','.','.','.','.','.','.'};
for (i = 0; i < 10; i++) bufW[i] = '.';
2004-01-23 03:10:33 +01:00
ok( !GlobalGetAtomNameW( atom, bufW, 0 ), "succeeded\n" );
ok( !memcmp( bufW, sampleW, 10 * sizeof(WCHAR) ), "should not touch buffer\n" );
2002-10-04 19:45:00 +02:00
}
2002-07-31 19:45:00 +02:00
/* Test integer atoms */
for (i = 0; i <= 0xbfff; i++)
{
memset( buf, 'a', 10 );
2002-10-04 19:45:00 +02:00
len = GlobalGetAtomNameA( (ATOM)i, buf, 10 );
2002-07-31 19:45:00 +02:00
if (i)
{
char res[20];
2004-01-23 03:10:33 +01:00
ok( (len > 1) && (len < 7), "bad length %d\n", len );
2002-07-31 19:45:00 +02:00
sprintf( res, "#%d", i );
memset( res + strlen(res) + 1, 'a', 10 );
2004-01-23 03:10:33 +01:00
ok( !memcmp( res, buf, 10 ), "bad buffer contents %s\n", buf );
2002-07-31 19:45:00 +02:00
}
2002-10-04 19:45:00 +02:00
else
2004-01-23 03:10:33 +01:00
ok( !len, "bad length %d\n", len );
2002-07-31 19:45:00 +02:00
}
}
static void test_error_handling(void)
{
char buffer[260];
WCHAR bufferW[260];
int i;
2002-10-04 19:45:00 +02:00
memset( buffer, 'a', 256 );
2002-07-31 19:45:00 +02:00
buffer[256] = 0;
2004-01-23 03:10:33 +01:00
ok( !GlobalAddAtomA(buffer), "add succeeded\n" );
ok( !GlobalFindAtomA(buffer), "find succeeded\n" );
2002-10-04 19:45:00 +02:00
if (unicode_OS)
{
for (i = 0; i < 256; i++) bufferW[i] = 'b';
bufferW[256] = 0;
2004-01-23 03:10:33 +01:00
ok( !GlobalAddAtomW(bufferW), "add succeeded\n" );
ok( !GlobalFindAtomW(bufferW), "find succeeded\n" );
2002-10-04 19:45:00 +02:00
}
2002-07-31 19:45:00 +02:00
}
START_TEST(atom)
{
test_add_atom();
test_get_atom_name();
test_error_handling();
}