Fixed some issues found by winapi_check.
[wine] / dlls / kernel / tests / atom.c
1 /*
2  * Unit tests for atom functions
3  *
4  * Copyright (c) 2002 Alexandre Julliard
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdio.h>
22
23 #include "wine/test.h"
24 #include "winbase.h"
25 #include "winerror.h"
26
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};
30
31 static void test_add_atom(void)
32 {
33     ATOM atom, w_atom;
34     int i;
35
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" );
40
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" );
45
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" );
52
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" );
57
58     /* Test integer atoms
59      * (0x0000 .. 0xbfff) should be valid;
60      * (0xc000 .. 0xffff) should be invalid */
61     todo_wine
62     {
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" );
67     }
68     SetLastError( 0xdeadbeef );
69     for (i = 1; i <= 0xbfff; i++)
70     {
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 );
76     }
77     for (i = 0xc000; i <= 0xffff; i++)
78     {
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 );
85     }
86 }
87
88 static void test_get_atom_name(void)
89 {
90     char buf[10];
91     WCHAR bufW[10];
92     int i, len;
93     static const char resultW[] = {'f','o','o','b','a','r',0,'.','.','.'};
94
95     ATOM atom = GlobalAddAtomA( "foobar" );
96
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" );
103
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 );
108     todo_wine
109     {
110         ok( !memcmp( bufW, resultW, 10*sizeof(WCHAR) ), "bad buffer contents" );
111     }
112     ok( !GlobalGetAtomNameW( atom, bufW, 3 ), "GlobalGetAtomNameW succeeded on short buffer" );
113
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" );
123
124     /* Test integer atoms */
125     for (i = 0; i <= 0xbfff; i++)
126     {
127         memset( buf, 'a', 10 );
128         len = GlobalGetAtomNameA( i, buf, 10 );
129         if (i)
130         {
131             char res[20];
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 );
136         }
137         else todo_wine
138         {
139             char res[20];
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 );
144         }
145     }
146 }
147
148 static void test_delete_atom(void)
149 {
150     ATOM atom;
151     int i;
152
153     /* First make sure it doesn't exist */
154     atom = GlobalAddAtomA( "foobar" );
155     while (!GlobalDeleteAtom( atom ));
156
157     /* Now add it a number of times */
158     for (i = 0; i < 10; i++) atom = GlobalAddAtomA( "foobar" );
159
160     /* Make sure it's here */
161     ok( GlobalFindAtomA( "foobar" ), "foobar not found" );
162     ok( GlobalFindAtomW( foobarW ), "foobarW not found" );
163
164     /* That many deletions should succeed */
165     for (i = 0; i < 10; i++)
166         ok( !GlobalDeleteAtom( atom ), "delete atom failed" );
167
168     /* It should be gone now */
169     ok( !GlobalFindAtomA( "foobar" ), "still found it" );
170     ok( !GlobalFindAtomW( foobarW ), "still found it" );
171
172     /* So this one should fail */
173     SetLastError( 0xdeadbeef );
174     ok( GlobalDeleteAtom( atom ) == atom && GetLastError() == ERROR_INVALID_HANDLE,
175         "delete failed" );
176 }
177
178 static void test_error_handling(void)
179 {
180     char buffer[260];
181     WCHAR bufferW[260];
182     ATOM atom;
183     int i;
184
185     memset( buffer, 'a', 255 );
186     buffer[255] = 0;
187     ok( atom = GlobalAddAtomA( buffer ), "add failed" );
188     ok( !GlobalDeleteAtom( atom ), "delete failed" );
189     buffer[255] = 'a';
190     buffer[256] = 0;
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" );
195
196     for (i = 0; i < 255; i++) bufferW[i] = 'b';
197     bufferW[255] = 0;
198     ok( atom = GlobalAddAtomW( bufferW ), "add failed" );
199     ok( !GlobalDeleteAtom( atom ), "delete failed" );
200     bufferW[255] = 'b';
201     bufferW[256] = 0;
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" );
206 }
207
208 START_TEST(atom)
209 {
210     test_add_atom();
211     test_get_atom_name();
212     test_delete_atom();
213     test_error_handling();
214 }