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"
26 #include "wine/unicode.h"
28 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
29 static const WCHAR FOOBARW[] = {'F','O','O','B','A','R',0};
30 static const WCHAR _foobarW[] = {'_','f','o','o','b','a','r',0};
32 static void test_add_atom(void)
37 SetLastError( 0xdeadbeef );
38 atom = GlobalAddAtomA( "foobar" );
39 ok( (atom >= 0xc000) && (atom <= 0xffff), "bad atom id %x", atom );
40 ok( GetLastError() == 0xdeadbeef, "GlobalAddAtomA set last error" );
42 /* Verify that it can be found (or not) appropriately */
43 ok( GlobalFindAtomA( "foobar" ) == atom, "could not find atom foobar" );
44 ok( GlobalFindAtomA( "FOOBAR" ) == atom, "could not find atom FOOBAR" );
45 ok( !GlobalFindAtomA( "_foobar" ), "found _foobar" );
47 /* Add the same atom, specifying string as unicode; should
48 * find the first one, not add a new one */
49 SetLastError( 0xdeadbeef );
50 w_atom = GlobalAddAtomW( foobarW );
51 ok( w_atom == atom, "Unicode atom does not match ASCII" );
52 ok( GetLastError() == 0xdeadbeef, "GlobalAddAtomW set last error" );
54 /* Verify that it can be found (or not) appropriately via unicode name */
55 ok( GlobalFindAtomW( foobarW ) == atom, "could not find atom foobar" );
56 ok( GlobalFindAtomW( FOOBARW ) == atom, "could not find atom FOOBAR" );
57 ok( !GlobalFindAtomW( _foobarW ), "found _foobar" );
60 * (0x0000 .. 0xbfff) should be valid;
61 * (0xc000 .. 0xffff) should be invalid */
64 SetLastError( 0xdeadbeef );
65 ok( GlobalAddAtomA(0) == 0 && GetLastError() == 0xdeadbeef, "failed to add atom 0" );
66 SetLastError( 0xdeadbeef );
67 ok( GlobalAddAtomW(0) == 0 && GetLastError() == 0xdeadbeef, "failed to add atom 0" );
69 SetLastError( 0xdeadbeef );
70 for (i = 1; i <= 0xbfff; i++)
72 SetLastError( 0xdeadbeef );
73 ok( GlobalAddAtomA((LPCSTR)i) == i && GetLastError() == 0xdeadbeef,
74 "failed to add atom %x", i );
75 ok( GlobalAddAtomW((LPCWSTR)i) == i && GetLastError() == 0xdeadbeef,
76 "failed to add atom %x", i );
78 for (i = 0xc000; i <= 0xffff; i++)
80 SetLastError( 0xdeadbeef );
81 ok( !GlobalAddAtomA((LPCSTR)i) && (GetLastError() == ERROR_INVALID_PARAMETER),
82 "succeeded adding %x", i );
83 SetLastError( 0xdeadbeef );
84 ok( !GlobalAddAtomW((LPCWSTR)i) && (GetLastError() == ERROR_INVALID_PARAMETER),
85 "succeeded adding %x", i );
89 static void test_get_atom_name(void)
94 static const char resultW[] = {'f','o','o','b','a','r',0,'.','.','.'};
96 ATOM atom = GlobalAddAtomA( "foobar" );
98 /* Get the name of the atom we added above */
99 memset( buf, '.', sizeof(buf) );
100 len = GlobalGetAtomNameA( atom, buf, 10 );
101 ok( len == strlen("foobar"), "bad length %d", len );
102 ok( !memcmp( buf, "foobar\0...", 10 ), "bad buffer contents" );
103 ok( !GlobalGetAtomNameA( atom, buf, 3 ), "GlobalGetAtomNameA succeeded on short buffer" );
105 /* Repeat, unicode-style */
106 for (i = 0; i < 10; i++) bufW[i] = '.';
107 len = GlobalGetAtomNameW( atom, bufW, 10 );
108 ok( len == strlenW(foobarW), "bad length %d", len );
111 ok( !memcmp( bufW, resultW, 10*sizeof(WCHAR) ), "bad buffer contents" );
113 ok( !GlobalGetAtomNameW( atom, bufW, 3 ), "GlobalGetAtomNameW succeeded on short buffer" );
115 /* Check error code returns */
116 SetLastError( 0xdeadbeef );
117 ok( !GlobalGetAtomNameA( atom, buf, 0 ) && GetLastError() == ERROR_MORE_DATA, "succeded" );
118 SetLastError( 0xdeadbeef );
119 ok( !GlobalGetAtomNameA( atom, buf, -1 ) && GetLastError() == ERROR_MORE_DATA, "succeded" );
120 SetLastError( 0xdeadbeef );
121 ok( !GlobalGetAtomNameW( atom, bufW, 0 ) && GetLastError() == ERROR_MORE_DATA, "succeded" );
122 SetLastError( 0xdeadbeef );
123 ok( !GlobalGetAtomNameW( atom, bufW, -1 ) && GetLastError() == ERROR_MORE_DATA, "succeded" );
125 /* Test integer atoms */
126 for (i = 0; i <= 0xbfff; i++)
128 memset( buf, 'a', 10 );
129 len = GlobalGetAtomNameA( i, buf, 10 );
133 ok( (len > 1) && (len < 7), "bad length %d", len );
134 sprintf( res, "#%d", i );
135 memset( res + strlen(res) + 1, 'a', 10 );
136 ok( !memcmp( res, buf, 10 ), "bad buffer contents %s", buf );
141 ok( (len > 1) && (len < 7), "bad length %d", len );
142 sprintf( res, "#%d", i );
143 memset( res + strlen(res) + 1, 'a', 10 );
144 ok( !memcmp( res, buf, 10 ), "bad buffer contents %s", buf );
149 static void test_delete_atom(void)
154 /* First make sure it doesn't exist */
155 atom = GlobalAddAtomA( "foobar" );
156 while (!GlobalDeleteAtom( atom ));
158 /* Now add it a number of times */
159 for (i = 0; i < 10; i++) atom = GlobalAddAtomA( "foobar" );
161 /* Make sure it's here */
162 ok( GlobalFindAtomA( "foobar" ), "foobar not found" );
163 ok( GlobalFindAtomW( foobarW ), "foobarW not found" );
165 /* That many deletions should succeed */
166 for (i = 0; i < 10; i++)
167 ok( !GlobalDeleteAtom( atom ), "delete atom failed" );
169 /* It should be gone now */
170 ok( !GlobalFindAtomA( "foobar" ), "still found it" );
171 ok( !GlobalFindAtomW( foobarW ), "still found it" );
173 /* So this one should fail */
174 SetLastError( 0xdeadbeef );
175 ok( GlobalDeleteAtom( atom ) == atom && GetLastError() == ERROR_INVALID_HANDLE,
179 static void test_error_handling(void)
186 memset( buffer, 'a', 255 );
188 ok( atom = GlobalAddAtomA( buffer ), "add failed" );
189 ok( !GlobalDeleteAtom( atom ), "delete failed" );
192 SetLastError( 0xdeadbeef );
193 ok( !GlobalAddAtomA(buffer) && GetLastError() == ERROR_INVALID_PARAMETER, "add succeded" );
194 SetLastError( 0xdeadbeef );
195 ok( !GlobalFindAtomA(buffer) && GetLastError() == ERROR_INVALID_PARAMETER, "find succeded" );
197 for (i = 0; i < 255; i++) bufferW[i] = 'b';
199 ok( atom = GlobalAddAtomW( bufferW ), "add failed" );
200 ok( !GlobalDeleteAtom( atom ), "delete failed" );
203 SetLastError( 0xdeadbeef );
204 ok( !GlobalAddAtomW(bufferW) && GetLastError() == ERROR_INVALID_PARAMETER, "add succeded" );
205 SetLastError( 0xdeadbeef );
206 ok( !GlobalFindAtomW(bufferW) && GetLastError() == ERROR_INVALID_PARAMETER, "find succeded" );
212 test_get_atom_name();
214 test_error_handling();