2 * Unit tests for atom functions
4 * Copyright (c) 2002 Alexandre Julliard
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/test.h"
27 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
28 static const WCHAR FOOBARW[] = {'F','O','O','B','A','R',0};
29 static const WCHAR _foobarW[] = {'_','f','o','o','b','a','r',0};
31 static void test_add_atom(void)
36 SetLastError( 0xdeadbeef );
37 atom = GlobalAddAtomA( "foobar" );
38 ok( (atom >= 0xc000) && (atom <= 0xffff), "bad atom id %x", atom );
39 ok( GetLastError() == 0xdeadbeef, "GlobalAddAtomA set last error" );
41 /* Verify that it can be found (or not) appropriately */
42 ok( GlobalFindAtomA( "foobar" ) == atom, "could not find atom foobar" );
43 ok( GlobalFindAtomA( "FOOBAR" ) == atom, "could not find atom FOOBAR" );
44 ok( !GlobalFindAtomA( "_foobar" ), "found _foobar" );
46 /* Add the same atom, specifying string as unicode; should
47 * find the first one, not add a new one */
48 SetLastError( 0xdeadbeef );
49 w_atom = GlobalAddAtomW( foobarW );
50 ok( w_atom == atom, "Unicode atom does not match ASCII" );
51 ok( GetLastError() == 0xdeadbeef, "GlobalAddAtomW set last error" );
53 /* Verify that it can be found (or not) appropriately via unicode name */
54 ok( GlobalFindAtomW( foobarW ) == atom, "could not find atom foobar" );
55 ok( GlobalFindAtomW( FOOBARW ) == atom, "could not find atom FOOBAR" );
56 ok( !GlobalFindAtomW( _foobarW ), "found _foobar" );
59 * (0x0000 .. 0xbfff) should be valid;
60 * (0xc000 .. 0xffff) should be invalid */
63 SetLastError( 0xdeadbeef );
64 ok( GlobalAddAtomA(0) == 0 && GetLastError() == 0xdeadbeef, "failed to add atom 0" );
65 SetLastError( 0xdeadbeef );
66 ok( GlobalAddAtomW(0) == 0 && GetLastError() == 0xdeadbeef, "failed to add atom 0" );
68 SetLastError( 0xdeadbeef );
69 for (i = 1; i <= 0xbfff; i++)
71 SetLastError( 0xdeadbeef );
72 ok( GlobalAddAtomA((LPCSTR)i) == i && GetLastError() == 0xdeadbeef,
73 "failed to add atom %x", i );
74 ok( GlobalAddAtomW((LPCWSTR)i) == i && GetLastError() == 0xdeadbeef,
75 "failed to add atom %x", i );
77 for (i = 0xc000; i <= 0xffff; i++)
79 SetLastError( 0xdeadbeef );
80 ok( !GlobalAddAtomA((LPCSTR)i) && (GetLastError() == ERROR_INVALID_PARAMETER),
81 "succeeded adding %x", i );
82 SetLastError( 0xdeadbeef );
83 ok( !GlobalAddAtomW((LPCWSTR)i) && (GetLastError() == ERROR_INVALID_PARAMETER),
84 "succeeded adding %x", i );
88 static void test_get_atom_name(void)
93 static const char resultW[] = {'f','o','o','b','a','r',0,'.','.','.'};
95 ATOM atom = GlobalAddAtomA( "foobar" );
97 /* Get the name of the atom we added above */
98 memset( buf, '.', sizeof(buf) );
99 len = GlobalGetAtomNameA( atom, buf, 10 );
100 ok( len == strlen("foobar"), "bad length %d", len );
101 ok( !memcmp( buf, "foobar\0...", 10 ), "bad buffer contents" );
102 ok( !GlobalGetAtomNameA( atom, buf, 3 ), "GlobalGetAtomNameA succeeded on short buffer" );
104 /* Repeat, unicode-style */
105 for (i = 0; i < 10; i++) bufW[i] = '.';
106 len = GlobalGetAtomNameW( atom, bufW, 10 );
107 ok( len == lstrlenW(foobarW), "bad length %d", len );
110 ok( !memcmp( bufW, resultW, 10*sizeof(WCHAR) ), "bad buffer contents" );
112 ok( !GlobalGetAtomNameW( atom, bufW, 3 ), "GlobalGetAtomNameW succeeded on short buffer" );
114 /* Check error code returns */
115 SetLastError( 0xdeadbeef );
116 ok( !GlobalGetAtomNameA( atom, buf, 0 ) && GetLastError() == ERROR_MORE_DATA, "succeded" );
117 SetLastError( 0xdeadbeef );
118 ok( !GlobalGetAtomNameA( atom, buf, -1 ) && GetLastError() == ERROR_MORE_DATA, "succeded" );
119 SetLastError( 0xdeadbeef );
120 ok( !GlobalGetAtomNameW( atom, bufW, 0 ) && GetLastError() == ERROR_MORE_DATA, "succeded" );
121 SetLastError( 0xdeadbeef );
122 ok( !GlobalGetAtomNameW( atom, bufW, -1 ) && GetLastError() == ERROR_MORE_DATA, "succeded" );
124 /* Test integer atoms */
125 for (i = 0; i <= 0xbfff; i++)
127 memset( buf, 'a', 10 );
128 len = GlobalGetAtomNameA( i, buf, 10 );
132 ok( (len > 1) && (len < 7), "bad length %d", len );
133 sprintf( res, "#%d", i );
134 memset( res + strlen(res) + 1, 'a', 10 );
135 ok( !memcmp( res, buf, 10 ), "bad buffer contents %s", buf );
140 ok( (len > 1) && (len < 7), "bad length %d", len );
141 sprintf( res, "#%d", i );
142 memset( res + strlen(res) + 1, 'a', 10 );
143 ok( !memcmp( res, buf, 10 ), "bad buffer contents %s", buf );
148 static void test_delete_atom(void)
153 /* First make sure it doesn't exist */
154 atom = GlobalAddAtomA( "foobar" );
155 while (!GlobalDeleteAtom( atom ));
157 /* Now add it a number of times */
158 for (i = 0; i < 10; i++) atom = GlobalAddAtomA( "foobar" );
160 /* Make sure it's here */
161 ok( GlobalFindAtomA( "foobar" ), "foobar not found" );
162 ok( GlobalFindAtomW( foobarW ), "foobarW not found" );
164 /* That many deletions should succeed */
165 for (i = 0; i < 10; i++)
166 ok( !GlobalDeleteAtom( atom ), "delete atom failed" );
168 /* It should be gone now */
169 ok( !GlobalFindAtomA( "foobar" ), "still found it" );
170 ok( !GlobalFindAtomW( foobarW ), "still found it" );
172 /* So this one should fail */
173 SetLastError( 0xdeadbeef );
174 ok( GlobalDeleteAtom( atom ) == atom && GetLastError() == ERROR_INVALID_HANDLE,
178 static void test_error_handling(void)
185 memset( buffer, 'a', 255 );
187 ok( atom = GlobalAddAtomA( buffer ), "add failed" );
188 ok( !GlobalDeleteAtom( atom ), "delete failed" );
191 SetLastError( 0xdeadbeef );
192 ok( !GlobalAddAtomA(buffer) && GetLastError() == ERROR_INVALID_PARAMETER, "add succeded" );
193 SetLastError( 0xdeadbeef );
194 ok( !GlobalFindAtomA(buffer) && GetLastError() == ERROR_INVALID_PARAMETER, "find succeded" );
196 for (i = 0; i < 255; i++) bufferW[i] = 'b';
198 ok( atom = GlobalAddAtomW( bufferW ), "add failed" );
199 ok( !GlobalDeleteAtom( atom ), "delete failed" );
202 SetLastError( 0xdeadbeef );
203 ok( !GlobalAddAtomW(bufferW) && GetLastError() == ERROR_INVALID_PARAMETER, "add succeded" );
204 SetLastError( 0xdeadbeef );
205 ok( !GlobalFindAtomW(bufferW) && GetLastError() == ERROR_INVALID_PARAMETER, "find succeded" );
211 test_get_atom_name();
213 test_error_handling();