scroll mode for very long start menus
[reactos.git] / reactos / apps / tests / kernel32 / 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 START_TEST(atom)
187 {
188 test_add_atom();
189 test_get_atom_name();
190 test_error_handling();
191 }