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 #include "wine/unicode.h"
27
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};
31
32 static void test_add_atom(void)
33 {
34     ATOM atom, w_atom;
35     int i;
36
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" );
41
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" );
46
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" );
53
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" );
58
59     /* Test integer atoms
60      * (0x0000 .. 0xbfff) should be valid;
61      * (0xc000 .. 0xffff) should be invalid */
62     todo_wine
63     {
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" );
68     }
69     SetLastError( 0xdeadbeef );
70     for (i = 1; i <= 0xbfff; i++)
71     {
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 );
77     }
78     for (i = 0xc000; i <= 0xffff; i++)
79     {
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 );
86     }
87 }
88
89 static void test_get_atom_name(void)
90 {
91     char buf[10];
92     WCHAR bufW[10];
93     int i, len;
94     static const char resultW[] = {'f','o','o','b','a','r',0,'.','.','.'};
95
96     ATOM atom = GlobalAddAtomA( "foobar" );
97
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" );
104
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 );
109     todo_wine
110     {
111         ok( !memcmp( bufW, resultW, 10*sizeof(WCHAR) ), "bad buffer contents" );
112     }
113     ok( !GlobalGetAtomNameW( atom, bufW, 3 ), "GlobalGetAtomNameW succeeded on short buffer" );
114
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" );
124
125     /* Test integer atoms */
126     for (i = 0; i <= 0xbfff; i++)
127     {
128         memset( buf, 'a', 10 );
129         len = GlobalGetAtomNameA( i, buf, 10 );
130         if (i)
131         {
132             char res[20];
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 );
137         }
138         else todo_wine
139         {
140             char res[20];
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 );
145         }
146     }
147 }
148
149 static void test_delete_atom(void)
150 {
151     ATOM atom;
152     int i;
153
154     /* First make sure it doesn't exist */
155     atom = GlobalAddAtomA( "foobar" );
156     while (!GlobalDeleteAtom( atom ));
157
158     /* Now add it a number of times */
159     for (i = 0; i < 10; i++) atom = GlobalAddAtomA( "foobar" );
160
161     /* Make sure it's here */
162     ok( GlobalFindAtomA( "foobar" ), "foobar not found" );
163     ok( GlobalFindAtomW( foobarW ), "foobarW not found" );
164
165     /* That many deletions should succeed */
166     for (i = 0; i < 10; i++)
167         ok( !GlobalDeleteAtom( atom ), "delete atom failed" );
168
169     /* It should be gone now */
170     ok( !GlobalFindAtomA( "foobar" ), "still found it" );
171     ok( !GlobalFindAtomW( foobarW ), "still found it" );
172
173     /* So this one should fail */
174     SetLastError( 0xdeadbeef );
175     ok( GlobalDeleteAtom( atom ) == atom && GetLastError() == ERROR_INVALID_HANDLE,
176         "delete failed" );
177 }
178
179 static void test_error_handling(void)
180 {
181     char buffer[260];
182     WCHAR bufferW[260];
183     ATOM atom;
184     int i;
185
186     memset( buffer, 'a', 255 );
187     buffer[255] = 0;
188     ok( atom = GlobalAddAtomA( buffer ), "add failed" );
189     ok( !GlobalDeleteAtom( atom ), "delete failed" );
190     buffer[255] = 'a';
191     buffer[256] = 0;
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" );
196
197     for (i = 0; i < 255; i++) bufferW[i] = 'b';
198     bufferW[255] = 0;
199     ok( atom = GlobalAddAtomW( bufferW ), "add failed" );
200     ok( !GlobalDeleteAtom( atom ), "delete failed" );
201     bufferW[255] = 'b';
202     bufferW[256] = 0;
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" );
207 }
208
209 START_TEST(atom)
210 {
211     test_add_atom();
212     test_get_atom_name();
213     test_delete_atom();
214     test_error_handling();
215 }