Don't try to access floppy if it's not mounted.
[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 <stdarg.h>
22 #include <stdio.h>
23
24 #include "wine/test.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28
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 static const WCHAR _foobarW[] = {'_','f','o','o','b','a','r',0};
32
33 static BOOL unicode_OS;
34
35 static void test_add_atom(void)
36 {
37     ATOM atom, w_atom;
38     int i;
39
40     SetLastError( 0xdeadbeef );
41     atom = GlobalAddAtomA( "foobar" );
42     ok( atom >= 0xc000, "bad atom id %x\n", atom );
43     ok( GetLastError() == 0xdeadbeef, "GlobalAddAtomA set last error\n" );
44
45     /* Verify that it can be found (or not) appropriately */
46     ok( GlobalFindAtomA( "foobar" ) == atom, "could not find atom foobar\n" );
47     ok( GlobalFindAtomA( "FOOBAR" ) == atom, "could not find atom FOOBAR\n" );
48     ok( !GlobalFindAtomA( "_foobar" ), "found _foobar\n" );
49
50     /* Add the same atom, specifying string as unicode; should
51      * find the first one, not add a new one */
52     SetLastError( 0xdeadbeef );
53     w_atom = GlobalAddAtomW( foobarW );
54     if (w_atom && GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
55         unicode_OS = TRUE;
56     else
57         trace("WARNING: Unicode atom APIs are not supported on this platform\n");
58
59     if (unicode_OS)
60     {
61         ok( w_atom == atom, "Unicode atom does not match ASCII\n" );
62         ok( GetLastError() == 0xdeadbeef, "GlobalAddAtomW set last error\n" );
63     }
64
65     /* Verify that it can be found (or not) appropriately via unicode name */
66     if (unicode_OS)
67     {
68         ok( GlobalFindAtomW( foobarW ) == atom, "could not find atom foobar\n" );
69         ok( GlobalFindAtomW( FOOBARW ) == atom, "could not find atom FOOBAR\n" );
70         ok( !GlobalFindAtomW( _foobarW ), "found _foobar\n" );
71     }
72
73     /* Test integer atoms
74      * (0x0001 .. 0xbfff) should be valid;
75      * (0xc000 .. 0xffff) should be invalid */
76
77     SetLastError( 0xdeadbeef );
78     ok( GlobalAddAtomA(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
79     if (unicode_OS)
80     {
81         SetLastError( 0xdeadbeef );
82         ok( GlobalAddAtomW(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
83     }
84
85     SetLastError( 0xdeadbeef );
86     for (i = 1; i <= 0xbfff; i++)
87     {
88         SetLastError( 0xdeadbeef );
89         ok( GlobalAddAtomA((LPCSTR)i) == i && GetLastError() == 0xdeadbeef,
90             "failed to add atom %x\n", i );
91         if (unicode_OS)
92         {
93             SetLastError( 0xdeadbeef );
94             ok( GlobalAddAtomW((LPCWSTR)i) == i && GetLastError() == 0xdeadbeef,
95                 "failed to add atom %x\n", i );
96         }
97     }
98
99     for (i = 0xc000; i <= 0xffff; i++)
100     {
101         ok( !GlobalAddAtomA((LPCSTR)i), "succeeded adding %x\n", i );
102         if (unicode_OS)
103             ok( !GlobalAddAtomW((LPCWSTR)i), "succeeded adding %x\n", i );
104     }
105 }
106
107 static void test_get_atom_name(void)
108 {
109     char buf[10];
110     WCHAR bufW[10];
111     int i;
112     UINT len;
113     static const WCHAR resultW[] = {'f','o','o','b','a','r',0,'.','.','.'};
114
115     ATOM atom = GlobalAddAtomA( "foobar" );
116
117     /* Get the name of the atom we added above */
118     memset( buf, '.', sizeof(buf) );
119     len = GlobalGetAtomNameA( atom, buf, 10 );
120     ok( len == strlen("foobar"), "bad length %d\n", len );
121     ok( !memcmp( buf, "foobar\0...", 10 ), "bad buffer contents\n" );
122
123     /* Repeat, unicode-style */
124     if (unicode_OS)
125     {
126         for (i = 0; i < 10; i++) bufW[i] = '.';
127         SetLastError( 0xdeadbeef );
128         len = GlobalGetAtomNameW( atom, bufW, 10 );
129         ok( len && GetLastError() == 0xdeadbeef, "GlobalGetAtomNameW failed\n" );
130         ok( len == lstrlenW(foobarW), "bad length %d\n", len );
131         ok( !memcmp( bufW, resultW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
132     }
133
134     /* Check error code returns */
135     memset(buf, '.', 10);
136     ok( !GlobalGetAtomNameA( atom, buf,  0 ), "succeeded\n" );
137     ok( !memcmp( buf, "..........", 10 ), "should not touch buffer\n" );
138
139     if (unicode_OS)
140     {
141         static const WCHAR sampleW[10] = {'.','.','.','.','.','.','.','.','.','.'};
142
143         for (i = 0; i < 10; i++) bufW[i] = '.';
144         ok( !GlobalGetAtomNameW( atom, bufW, 0 ), "succeeded\n" );
145         ok( !memcmp( bufW, sampleW, 10 * sizeof(WCHAR) ), "should not touch buffer\n" );
146     }
147
148     /* Test integer atoms */
149     for (i = 0; i <= 0xbfff; i++)
150     {
151         memset( buf, 'a', 10 );
152         len = GlobalGetAtomNameA( (ATOM)i, buf, 10 );
153         if (i)
154         {
155             char res[20];
156             ok( (len > 1) && (len < 7), "bad length %d\n", len );
157             sprintf( res, "#%d", i );
158             memset( res + strlen(res) + 1, 'a', 10 );
159             ok( !memcmp( res, buf, 10 ), "bad buffer contents %s\n", buf );
160         }
161         else
162             ok( !len, "bad length %d\n", len );
163     }
164 }
165
166 static void test_error_handling(void)
167 {
168     char buffer[260];
169     WCHAR bufferW[260];
170     int i;
171
172     memset( buffer, 'a', 256 );
173     buffer[256] = 0;
174     ok( !GlobalAddAtomA(buffer), "add succeeded\n" );
175     ok( !GlobalFindAtomA(buffer), "find succeeded\n" );
176
177     if (unicode_OS)
178     {
179         for (i = 0; i < 256; i++) bufferW[i] = 'b';
180         bufferW[256] = 0;
181         ok( !GlobalAddAtomW(bufferW), "add succeeded\n" );
182         ok( !GlobalFindAtomW(bufferW), "find succeeded\n" );
183     }
184 }
185
186 static void test_local_add_atom(void)
187 {
188     ATOM atom, w_atom;
189     int i;
190
191     SetLastError( 0xdeadbeef );
192     atom = AddAtomA( "foobar" );
193     ok( atom >= 0xc000, "bad atom id %x\n", atom );
194     ok( GetLastError() == 0xdeadbeef, "AddAtomA set last error\n" );
195
196     /* Verify that it can be found (or not) appropriately */
197     ok( FindAtomA( "foobar" ) == atom, "could not find atom foobar\n" );
198     ok( FindAtomA( "FOOBAR" ) == atom, "could not find atom FOOBAR\n" );
199     ok( !FindAtomA( "_foobar" ), "found _foobar\n" );
200
201     /* Add the same atom, specifying string as unicode; should
202      * find the first one, not add a new one */
203     SetLastError( 0xdeadbeef );
204     w_atom = AddAtomW( foobarW );
205     if (w_atom && GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
206         unicode_OS = TRUE;
207     else
208         trace("WARNING: Unicode atom APIs are not supported on this platform\n");
209
210     if (unicode_OS)
211     {
212         ok( w_atom == atom, "Unicode atom does not match ASCII\n" );
213         ok( GetLastError() == 0xdeadbeef, "AddAtomW set last error\n" );
214     }
215
216     /* Verify that it can be found (or not) appropriately via unicode name */
217     if (unicode_OS)
218     {
219         ok( FindAtomW( foobarW ) == atom, "could not find atom foobar\n" );
220         ok( FindAtomW( FOOBARW ) == atom, "could not find atom FOOBAR\n" );
221         ok( !FindAtomW( _foobarW ), "found _foobar\n" );
222     }
223
224     /* Test integer atoms
225      * (0x0001 .. 0xbfff) should be valid;
226      * (0xc000 .. 0xffff) should be invalid */
227
228     SetLastError( 0xdeadbeef );
229     ok( AddAtomA(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
230     if (unicode_OS)
231     {
232         SetLastError( 0xdeadbeef );
233         ok( AddAtomW(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
234     }
235
236     SetLastError( 0xdeadbeef );
237     for (i = 1; i <= 0xbfff; i++)
238     {
239         SetLastError( 0xdeadbeef );
240         ok( AddAtomA((LPCSTR)i) == i && GetLastError() == 0xdeadbeef,
241             "failed to add atom %x\n", i );
242         if (unicode_OS)
243         {
244             SetLastError( 0xdeadbeef );
245             ok( AddAtomW((LPCWSTR)i) == i && GetLastError() == 0xdeadbeef,
246                 "failed to add atom %x\n", i );
247         }
248     }
249
250     for (i = 0xc000; i <= 0xffff; i++)
251     {
252         ok( !AddAtomA((LPCSTR)i), "succeeded adding %x\n", i );
253         if (unicode_OS)
254             ok( !AddAtomW((LPCWSTR)i), "succeeded adding %x\n", i );
255     }
256 }
257
258 static void test_local_get_atom_name(void)
259 {
260     char buf[10];
261     WCHAR bufW[10];
262     int i;
263     UINT len;
264     static const WCHAR resultW[] = {'f','o','o','b','a','r',0,'.','.','.'};
265
266     ATOM atom = AddAtomA( "foobar" );
267
268     /* Get the name of the atom we added above */
269     memset( buf, '.', sizeof(buf) );
270     len = GetAtomNameA( atom, buf, 10 );
271     ok( len == strlen("foobar"), "bad length %d\n", len );
272     ok( !memcmp( buf, "foobar\0...", 10 ), "bad buffer contents\n" );
273
274     /* Repeat, unicode-style */
275     if (unicode_OS)
276     {
277         for (i = 0; i < 10; i++) bufW[i] = '.';
278         SetLastError( 0xdeadbeef );
279         len = GetAtomNameW( atom, bufW, 10 );
280         ok( len && GetLastError() == 0xdeadbeef, "GetAtomNameW failed\n" );
281         ok( len == lstrlenW(foobarW), "bad length %d\n", len );
282         ok( !memcmp( bufW, resultW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
283     }
284
285     /* Check error code returns */
286     memset(buf, '.', 10);
287     ok( !GetAtomNameA( atom, buf,  0 ), "succeeded\n" );
288     ok( !memcmp( buf, "..........", 10 ), "should not touch buffer\n" );
289
290     if (unicode_OS)
291     {
292         static const WCHAR sampleW[10] = {'.','.','.','.','.','.','.','.','.','.'};
293
294         for (i = 0; i < 10; i++) bufW[i] = '.';
295         ok( !GetAtomNameW( atom, bufW, 0 ), "succeeded\n" );
296         ok( !memcmp( bufW, sampleW, 10 * sizeof(WCHAR) ), "should not touch buffer\n" );
297     }
298
299     /* Test integer atoms */
300     for (i = 0; i <= 0xbfff; i++)
301     {
302         memset( buf, 'a', 10 );
303         len = GetAtomNameA( (ATOM)i, buf, 10 );
304         if (i)
305         {
306             char res[20];
307             ok( (len > 1) && (len < 7), "bad length %d\n", len );
308             sprintf( res, "#%d", i );
309             memset( res + strlen(res) + 1, 'a', 10 );
310             ok( !memcmp( res, buf, 10 ), "bad buffer contents %s\n", buf );
311         }
312         else
313             ok( !len, "bad length %d\n", len );
314     }
315 }
316
317 static void test_local_error_handling(void)
318 {
319     char buffer[260];
320     WCHAR bufferW[260];
321     int i;
322
323     memset( buffer, 'a', 256 );
324     buffer[256] = 0;
325     ok( !AddAtomA(buffer), "add succeeded\n" );
326     ok( !FindAtomA(buffer), "find succeeded\n" );
327
328     if (unicode_OS)
329     {
330         for (i = 0; i < 256; i++) bufferW[i] = 'b';
331         bufferW[256] = 0;
332         ok( !AddAtomW(bufferW), "add succeeded\n" );
333         ok( !FindAtomW(bufferW), "find succeeded\n" );
334     }
335 }
336
337
338 START_TEST(atom)
339 {
340     test_add_atom();
341     test_get_atom_name();
342     test_error_handling();
343     test_local_add_atom();
344     test_local_get_atom_name();
345     test_local_error_handling();
346 }