-sync kernel32_winetest with wine 1.1.32
[reactos.git] / rostests / winetests / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #include "winuser.h"
29
30 #define DOUBLE(x) (WCHAR)((x<<8)|(x))
31
32 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
33 static const WCHAR FOOBARW[] = {'F','O','O','B','A','R',0};
34 static const WCHAR _foobarW[] = {'_','f','o','o','b','a','r',0};
35
36 static void do_initA(char* tmp, const char* pattern, int len)
37 {
38 const char* p = pattern;
39
40 while (len--)
41 {
42 *tmp++ = *p++;
43 if (!*p) p = pattern;
44 }
45 *tmp = '\0';
46 }
47
48 static void do_initW(WCHAR* tmp, const char* pattern, int len)
49 {
50 const char* p = pattern;
51
52 while (len--)
53 {
54 *tmp++ = *p++;
55 if (!*p) p = pattern;
56 }
57 *tmp = '\0';
58 }
59
60 static void print_integral( WCHAR* buffer, int atom )
61 {
62 BOOL first = TRUE;
63
64 #define X(v) { if (atom >= v) {*buffer++ = '0' + atom / v; first = FALSE; } else if (!first || v == 1) *buffer++ = '0'; atom %= v; }
65 *buffer++ = '#';
66 X(10000);
67 X(1000);
68 X(100);
69 X(10);
70 X(1);
71 *buffer = '\0';
72 #undef X
73 }
74
75 static BOOL unicode_OS;
76
77 static void test_add_atom(void)
78 {
79 ATOM atom, w_atom;
80 INT_PTR i;
81
82 SetLastError( 0xdeadbeef );
83 atom = GlobalAddAtomA( "foobar" );
84 ok( atom >= 0xc000, "bad atom id %x\n", atom );
85 ok( GetLastError() == 0xdeadbeef, "GlobalAddAtomA set last error\n" );
86
87 /* Verify that it can be found (or not) appropriately */
88 ok( GlobalFindAtomA( "foobar" ) == atom, "could not find atom foobar\n" );
89 ok( GlobalFindAtomA( "FOOBAR" ) == atom, "could not find atom FOOBAR\n" );
90 ok( !GlobalFindAtomA( "_foobar" ), "found _foobar\n" );
91
92 /* Add the same atom, specifying string as unicode; should
93 * find the first one, not add a new one */
94 SetLastError( 0xdeadbeef );
95 w_atom = GlobalAddAtomW( foobarW );
96 if (w_atom && GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
97 unicode_OS = TRUE;
98 else
99 trace("WARNING: Unicode atom APIs are not supported on this platform\n");
100
101 if (unicode_OS)
102 {
103 ok( w_atom == atom, "Unicode atom does not match ASCII\n" );
104 ok( GetLastError() == 0xdeadbeef, "GlobalAddAtomW set last error\n" );
105 }
106
107 /* Verify that it can be found (or not) appropriately via unicode name */
108 if (unicode_OS)
109 {
110 ok( GlobalFindAtomW( foobarW ) == atom, "could not find atom foobar\n" );
111 ok( GlobalFindAtomW( FOOBARW ) == atom, "could not find atom FOOBAR\n" );
112 ok( !GlobalFindAtomW( _foobarW ), "found _foobar\n" );
113 }
114
115 /* Test integer atoms
116 * (0x0001 .. 0xbfff) should be valid;
117 * (0xc000 .. 0xffff) should be invalid */
118
119 SetLastError( 0xdeadbeef );
120 ok( GlobalAddAtomA(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
121 if (unicode_OS)
122 {
123 SetLastError( 0xdeadbeef );
124 ok( GlobalAddAtomW(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
125 }
126
127 SetLastError( 0xdeadbeef );
128 for (i = 1; i <= 0xbfff; i++)
129 {
130 SetLastError( 0xdeadbeef );
131 ok( GlobalAddAtomA((LPCSTR)i) == i && GetLastError() == 0xdeadbeef,
132 "failed to add atom %lx\n", i );
133 if (unicode_OS)
134 {
135 SetLastError( 0xdeadbeef );
136 ok( GlobalAddAtomW((LPCWSTR)i) == i && GetLastError() == 0xdeadbeef,
137 "failed to add atom %lx\n", i );
138 }
139 }
140
141 for (i = 0xc000; i <= 0xffff; i++)
142 {
143 ok( !GlobalAddAtomA((LPCSTR)i), "succeeded adding %lx\n", i );
144 if (unicode_OS)
145 ok( !GlobalAddAtomW((LPCWSTR)i), "succeeded adding %lx\n", i );
146 }
147 }
148
149 static void test_get_atom_name(void)
150 {
151 char buf[10];
152 WCHAR bufW[10];
153 int i;
154 UINT len;
155 static const WCHAR resultW[] = {'f','o','o','b','a','r',0,'.','.','.'};
156 char in[257], out[257];
157 WCHAR inW[257], outW[257];
158
159 ATOM atom = GlobalAddAtomA( "foobar" );
160
161 /* Get the name of the atom we added above */
162 memset( buf, '.', sizeof(buf) );
163 len = GlobalGetAtomNameA( atom, buf, 10 );
164 ok( len == strlen("foobar"), "bad length %d\n", len );
165 ok( !memcmp( buf, "foobar\0...", 10 ), "bad buffer contents\n" );
166
167 /* Repeat, unicode-style */
168 if (unicode_OS)
169 {
170 for (i = 0; i < 10; i++) bufW[i] = '.';
171 SetLastError( 0xdeadbeef );
172 len = GlobalGetAtomNameW( atom, bufW, 10 );
173 ok( len && GetLastError() == 0xdeadbeef, "GlobalGetAtomNameW failed\n" );
174 ok( len == lstrlenW(foobarW), "bad length %d\n", len );
175 ok( !memcmp( bufW, resultW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
176 }
177
178 /* Check error code returns */
179 memset(buf, '.', 10);
180 ok( !GlobalGetAtomNameA( atom, buf, 0 ), "succeeded\n" );
181 ok( !memcmp( buf, "..........", 10 ), "should not touch buffer\n" );
182
183 if (unicode_OS)
184 {
185 static const WCHAR sampleW[10] = {'.','.','.','.','.','.','.','.','.','.'};
186
187 for (i = 0; i < 10; i++) bufW[i] = '.';
188 ok( !GlobalGetAtomNameW( atom, bufW, 0 ), "succeeded\n" );
189 ok( !memcmp( bufW, sampleW, 10 * sizeof(WCHAR) ), "should not touch buffer\n" );
190 }
191
192 /* Test integer atoms */
193 for (i = 0; i <= 0xbfff; i++)
194 {
195 memset( buf, 'a', 10 );
196 len = GlobalGetAtomNameA( (ATOM)i, buf, 10 );
197 if (i)
198 {
199 char res[20];
200 ok( (len > 1) && (len < 7), "bad length %d\n", len );
201 sprintf( res, "#%d", i );
202 memset( res + strlen(res) + 1, 'a', 10 );
203 ok( !memcmp( res, buf, 10 ), "bad buffer contents %s\n", buf );
204 if (len <= 1 || len >= 7) break; /* don't bother testing all of them */
205 }
206 else
207 ok( !len, "bad length %d\n", len );
208
209 SetLastError(0xdeadbeef);
210 len = GlobalGetAtomNameA( (ATOM)i, buf, 2);
211 if (!len) /* the NT way */
212 {
213 ok(GetLastError() == (i ? ERROR_MORE_DATA : ERROR_INVALID_PARAMETER) ||
214 GetLastError() == 0xdeadbeef, /* the Win 9x way */
215 "wrong error conditions %u for %u\n", GetLastError(), i);
216 }
217 else /* the Win 9x way */
218 {
219 ok(GetLastError() == 0xdeadbeef,
220 "wrong error conditions %u for %u\n", GetLastError(), i);
221 }
222 }
223
224 memset( buf, '.', sizeof(buf) );
225 len = GlobalGetAtomNameA( atom, buf, 6 );
226 ok( len == 0 ||
227 len == 5, /* win9x */
228 "bad length %d\n", len );
229 ok( !memcmp( buf, "fooba\0....", 10 ), "bad buffer contents\n");
230 if (unicode_OS)
231 {
232 static const WCHAR resW[] = {'f','o','o','b','a','r','.','.','.','.'};
233 for (len = 0; len < 10; len++) bufW[len] = '.';
234 SetLastError(0xdeadbeef);
235 len = GlobalGetAtomNameW( atom, bufW, 6 );
236 ok( len && GetLastError() == 0xdeadbeef, "GlobalGetAtomNameW failed\n" );
237 ok( len == lstrlenW(foobarW), "bad length %d\n", len );
238 ok( !memcmp( bufW, resW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
239 }
240
241 /* test string limits & overflow */
242 do_initA(in, "abcdefghij", 255);
243 atom = GlobalAddAtomA(in);
244 ok(atom, "couldn't add atom for %s\n", in);
245 len = GlobalGetAtomNameA(atom, out, sizeof(out));
246 ok(len == 255, "length mismatch (%u instead of 255)\n", len);
247 for (i = 0; i < 255; i++)
248 {
249 ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
250 }
251 ok(out[255] == '\0', "wrong end of string\n");
252 memset(out, '.', sizeof(out));
253 SetLastError(0xdeadbeef);
254 len = GlobalGetAtomNameA(atom, out, 10);
255 if (!len) /* the NT way */
256 {
257 ok(GetLastError() == ERROR_MORE_DATA, "wrong error code (%u instead of %u)\n", GetLastError(), ERROR_MORE_DATA);
258 }
259 else /* the Win9x way */
260 {
261 ok(GetLastError() == 0xdeadbeef, "wrong error code (%u instead of %u)\n", GetLastError(), 0xdeadbeef);
262 }
263 for (i = 0; i < 9; i++)
264 {
265 ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
266 }
267 ok(out[9] == '\0', "wrong end of string\n");
268 ok(out[10] == '.', "wrote after end of buf\n");
269 do_initA(in, "abcdefghij", 256);
270 atom = GlobalAddAtomA(in);
271 ok(!atom, "succeeded\n");
272 if (unicode_OS)
273 {
274 /* test integral atoms */
275 for (i = 0; i <= 0xbfff; i++)
276 {
277 memset(outW, 'a', sizeof(outW));
278 len = GlobalGetAtomNameW( (ATOM)i, outW, 10 );
279 if (i)
280 {
281 WCHAR res[20];
282
283 ok( (len > 1) && (len < 7), "bad length %d\n", len );
284 print_integral( res, i );
285 memset( res + lstrlenW(res) + 1, 'a', 10 * sizeof(WCHAR));
286 ok( !memcmp( res, outW, 10 * sizeof(WCHAR) ), "bad buffer contents for %d\n", i );
287 if (len <= 1 || len >= 7) break; /* don't bother testing all of them */
288 }
289 else
290 ok( !len, "bad length %d\n", len );
291
292 memset(outW, '.', sizeof(outW));
293 SetLastError(0xdeadbeef);
294 len = GlobalGetAtomNameW( (ATOM)i, outW, 1);
295 if (i)
296 {
297 /* len == 0 with ERROR_MORE_DATA is on NT3.51 */
298 ok(len == 1 || (len == 0 && GetLastError() == ERROR_MORE_DATA),
299 "0x%04x: got %u with %d (excepted '1' or '0' with "
300 "ERROR_MORE_DATA)\n", i, len, GetLastError());
301 ok(outW[1] == DOUBLE('.'), "buffer overwrite\n");
302 }
303 else ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "0 badly handled\n");
304 }
305
306 do_initW(inW, "abcdefghij", 255);
307 atom = GlobalAddAtomW(inW);
308 ok(atom, "couldn't add atom for %s\n", in);
309 len = GlobalGetAtomNameW(atom, outW, sizeof(outW)/sizeof(outW[0]));
310 ok(len == 255, "length mismatch (%u instead of 255)\n", len);
311 for (i = 0; i < 255; i++)
312 {
313 ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
314 }
315 ok(outW[255] == '\0', "wrong end of string\n");
316 memset(outW, '.', sizeof(outW));
317 len = GlobalGetAtomNameW(atom, outW, 10);
318 ok(len == 10, "succeeded\n");
319 for (i = 0; i < 10; i++)
320 {
321 ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
322 }
323 ok(outW[10] == DOUBLE('.'), "wrote after end of buf\n");
324 do_initW(inW, "abcdefghij", 256);
325 atom = GlobalAddAtomW(inW);
326 ok(!atom, "succeeded\n");
327 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error code\n");
328 }
329 }
330
331 static void test_error_handling(void)
332 {
333 char buffer[260];
334 WCHAR bufferW[260];
335 int i;
336
337 memset( buffer, 'a', 256 );
338 buffer[256] = 0;
339 ok( !GlobalAddAtomA(buffer), "add succeeded\n" );
340 ok( !GlobalFindAtomA(buffer), "find succeeded\n" );
341
342 if (unicode_OS)
343 {
344 for (i = 0; i < 256; i++) bufferW[i] = 'b';
345 bufferW[256] = 0;
346 ok( !GlobalAddAtomW(bufferW), "add succeeded\n" );
347 ok( !GlobalFindAtomW(bufferW), "find succeeded\n" );
348 }
349 }
350
351 static void test_local_add_atom(void)
352 {
353 ATOM atom, w_atom;
354 INT_PTR i;
355
356 SetLastError( 0xdeadbeef );
357 atom = AddAtomA( "foobar" );
358 ok( atom >= 0xc000, "bad atom id %x\n", atom );
359 ok( GetLastError() == 0xdeadbeef, "AddAtomA set last error\n" );
360
361 /* Verify that it can be found (or not) appropriately */
362 ok( FindAtomA( "foobar" ) == atom, "could not find atom foobar\n" );
363 ok( FindAtomA( "FOOBAR" ) == atom, "could not find atom FOOBAR\n" );
364 ok( !FindAtomA( "_foobar" ), "found _foobar\n" );
365
366 /* Add the same atom, specifying string as unicode; should
367 * find the first one, not add a new one */
368 SetLastError( 0xdeadbeef );
369 w_atom = AddAtomW( foobarW );
370 if (w_atom && GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
371 unicode_OS = TRUE;
372 else
373 trace("WARNING: Unicode atom APIs are not supported on this platform\n");
374
375 if (unicode_OS)
376 {
377 ok( w_atom == atom, "Unicode atom does not match ASCII\n" );
378 ok( GetLastError() == 0xdeadbeef, "AddAtomW set last error\n" );
379 }
380
381 /* Verify that it can be found (or not) appropriately via unicode name */
382 if (unicode_OS)
383 {
384 ok( FindAtomW( foobarW ) == atom, "could not find atom foobar\n" );
385 ok( FindAtomW( FOOBARW ) == atom, "could not find atom FOOBAR\n" );
386 ok( !FindAtomW( _foobarW ), "found _foobar\n" );
387 }
388
389 /* Test integer atoms
390 * (0x0001 .. 0xbfff) should be valid;
391 * (0xc000 .. 0xffff) should be invalid */
392
393 SetLastError( 0xdeadbeef );
394 ok( AddAtomA(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
395 if (unicode_OS)
396 {
397 SetLastError( 0xdeadbeef );
398 ok( AddAtomW(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
399 }
400
401 SetLastError( 0xdeadbeef );
402 for (i = 1; i <= 0xbfff; i++)
403 {
404 SetLastError( 0xdeadbeef );
405 ok( AddAtomA((LPCSTR)i) == i && GetLastError() == 0xdeadbeef,
406 "failed to add atom %lx\n", i );
407 if (unicode_OS)
408 {
409 SetLastError( 0xdeadbeef );
410 ok( AddAtomW((LPCWSTR)i) == i && GetLastError() == 0xdeadbeef,
411 "failed to add atom %lx\n", i );
412 }
413 }
414
415 for (i = 0xc000; i <= 0xffff; i++)
416 {
417 ok( !AddAtomA((LPCSTR)i), "succeeded adding %lx\n", i );
418 if (unicode_OS)
419 ok( !AddAtomW((LPCWSTR)i), "succeeded adding %lx\n", i );
420 }
421 }
422
423 static void test_local_get_atom_name(void)
424 {
425 char buf[10], in[257], out[257];
426 WCHAR bufW[10], inW[257], outW[257];
427 int i;
428 UINT len;
429 static const WCHAR resultW[] = {'f','o','o','b','a','r',0,'.','.','.'};
430
431 ATOM atom = AddAtomA( "foobar" );
432
433 /* Get the name of the atom we added above */
434 memset( buf, '.', sizeof(buf) );
435 len = GetAtomNameA( atom, buf, 10 );
436 ok( len == strlen("foobar"), "bad length %d\n", len );
437 ok( !memcmp( buf, "foobar\0...", 10 ), "bad buffer contents\n" );
438
439 /* Repeat, unicode-style */
440 if (unicode_OS)
441 {
442 for (i = 0; i < 10; i++) bufW[i] = '.';
443 SetLastError( 0xdeadbeef );
444 len = GetAtomNameW( atom, bufW, 10 );
445 ok( len && GetLastError() == 0xdeadbeef, "GetAtomNameW failed\n" );
446 ok( len == lstrlenW(foobarW), "bad length %d\n", len );
447 ok( !memcmp( bufW, resultW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
448 }
449
450 /* Get the name of the atom we added above */
451 memset( buf, '.', sizeof(buf) );
452 len = GetAtomNameA( atom, buf, 6 );
453 ok( len == 5, "bad length %d\n", len );
454 ok( !memcmp( buf, "fooba\0....", 10 ), "bad buffer contents\n" );
455
456 /* Repeat, unicode-style */
457 if (unicode_OS)
458 {
459 WCHAR resW[] = {'f','o','o','b','a','\0','.','.','.','.'};
460 for (i = 0; i < 10; i++) bufW[i] = '.';
461 SetLastError( 0xdeadbeef );
462 len = GetAtomNameW( atom, bufW, 6 );
463 ok( len && GetLastError() == 0xdeadbeef, "GlobalGetAtomNameW failed\n" );
464 ok( len == 5, "bad length %d\n", len );
465 ok( !memcmp( bufW, resW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
466 }
467
468 /* Check error code returns */
469 memset(buf, '.', 10);
470 ok( !GetAtomNameA( atom, buf, 0 ), "succeeded\n" );
471 ok( !memcmp( buf, "..........", 10 ), "should not touch buffer\n" );
472
473 if (unicode_OS)
474 {
475 static const WCHAR sampleW[10] = {'.','.','.','.','.','.','.','.','.','.'};
476
477 for (i = 0; i < 10; i++) bufW[i] = '.';
478 ok( !GetAtomNameW( atom, bufW, 0 ), "succeeded\n" );
479 ok( !memcmp( bufW, sampleW, 10 * sizeof(WCHAR) ), "should not touch buffer\n" );
480 }
481
482 /* Test integer atoms */
483 for (i = 0; i <= 0xbfff; i++)
484 {
485 memset( buf, 'a', 10 );
486 len = GetAtomNameA( (ATOM)i, buf, 10 );
487 if (i)
488 {
489 char res[20];
490 ok( (len > 1) && (len < 7), "bad length %d for %s\n", len, buf );
491 sprintf( res, "#%d", i );
492 memset( res + strlen(res) + 1, 'a', 10 );
493 ok( !memcmp( res, buf, 10 ), "bad buffer contents %s\n", buf );
494 }
495 else
496 ok( !len, "bad length %d\n", len );
497
498 len = GetAtomNameA( (ATOM)i, buf, 1);
499 ok(!len, "succeed with %u for %u\n", len, i);
500
501 /* ERROR_MORE_DATA is on nt3.51 sp5 */
502 if (i)
503 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER ||
504 GetLastError() == ERROR_MORE_DATA ||
505 GetLastError() == 0xdeadbeef, /* the Win 9x way */
506 "wrong error conditions %u for %u\n", GetLastError(), i);
507 else
508 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
509 GetLastError() == ERROR_MORE_DATA ||
510 GetLastError() == 0xdeadbeef, /* the Win 9x way */
511 "wrong error conditions %u for %u\n", GetLastError(), i);
512 }
513 /* test string limits & overflow */
514 do_initA(in, "abcdefghij", 255);
515 atom = AddAtomA(in);
516 ok(atom, "couldn't add atom for %s\n", in);
517 len = GetAtomNameA(atom, out, sizeof(out));
518 ok(len == 255, "length mismatch (%u instead of 255)\n", len);
519 for (i = 0; i < 255; i++)
520 {
521 ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
522 }
523 ok(out[255] == '\0', "wrong end of string\n");
524 memset(out, '.', sizeof(out));
525 len = GetAtomNameA(atom, out, 10);
526 ok(len == 9, "succeeded %d\n", len);
527 for (i = 0; i < 9; i++)
528 {
529 ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
530 }
531 ok(out[9] == '\0', "wrong end of string\n");
532 ok(out[10] == '.', "buffer overwrite\n");
533 do_initA(in, "abcdefghij", 256);
534 atom = AddAtomA(in);
535 ok(!atom, "succeeded\n");
536
537 /* ERROR_MORE_DATA is on nt3.51 sp5 */
538 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
539 GetLastError() == ERROR_MORE_DATA ||
540 GetLastError() == 0xdeadbeef, /* the Win 9x way */
541 "wrong error code (%u)\n", GetLastError());
542
543 if (unicode_OS)
544 {
545 /* test integral atoms */
546 for (i = 0; i <= 0xbfff; i++)
547 {
548 memset(outW, 'a', sizeof(outW));
549 len = GetAtomNameW( (ATOM)i, outW, 10 );
550 if (i)
551 {
552 WCHAR res[20];
553
554 ok( (len > 1) && (len < 7), "bad length %d\n", len );
555 print_integral( res, i );
556 memset( res + lstrlenW(res) + 1, 'a', 10 * sizeof(WCHAR));
557 ok( !memcmp( res, outW, 10 * sizeof(WCHAR) ), "bad buffer contents for %d\n", i );
558 }
559 else
560 ok( !len, "bad length %d\n", len );
561
562 len = GetAtomNameW( (ATOM)i, outW, 1);
563 ok(!len, "succeed with %u for %u\n", len, i);
564
565 /* ERROR_MORE_DATA is on nt3.51 sp5 */
566 ok(GetLastError() == ERROR_MORE_DATA ||
567 GetLastError() == (i ? ERROR_INSUFFICIENT_BUFFER : ERROR_INVALID_PARAMETER),
568 "wrong error conditions %u for %u\n", GetLastError(), i);
569 }
570 do_initW(inW, "abcdefghij", 255);
571 atom = AddAtomW(inW);
572 ok(atom, "couldn't add atom for %s\n", in);
573 len = GetAtomNameW(atom, outW, sizeof(outW)/sizeof(outW[0]));
574 ok(len == 255, "length mismatch (%u instead of 255)\n", len);
575 for (i = 0; i < 255; i++)
576 {
577 ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
578 }
579 ok(outW[255] == '\0', "wrong end of string\n");
580 memset(outW, '.', sizeof(outW));
581 len = GetAtomNameW(atom, outW, 10);
582 ok(len == 9, "succeeded %d\n", len);
583 for (i = 0; i < 9; i++)
584 {
585 ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
586 }
587 ok(outW[9] == '\0', "wrong end of string\n");
588 ok(outW[10] == DOUBLE('.'), "buffer overwrite\n");
589 do_initW(inW, "abcdefghij", 256);
590 atom = AddAtomW(inW);
591 ok(!atom, "succeeded\n");
592
593 /* ERROR_MORE_DATA is on nt3.51 sp5 */
594 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
595 GetLastError() == ERROR_MORE_DATA,
596 "wrong error code (%u)\n", GetLastError());
597 }
598 }
599
600 static void test_local_error_handling(void)
601 {
602 char buffer[260];
603 WCHAR bufferW[260];
604 int i;
605
606 memset( buffer, 'a', 256 );
607 buffer[256] = 0;
608 ok( !AddAtomA(buffer), "add succeeded\n" );
609 ok( !FindAtomA(buffer), "find succeeded\n" );
610
611 if (unicode_OS)
612 {
613 for (i = 0; i < 256; i++) bufferW[i] = 'b';
614 bufferW[256] = 0;
615 ok( !AddAtomW(bufferW), "add succeeded\n" );
616 ok( !FindAtomW(bufferW), "find succeeded\n" );
617 }
618 }
619
620 START_TEST(atom)
621 {
622 /* Global atom table seems to be available to GUI apps only in
623 Win7, so let's turn this app into a GUI app */
624 GetDesktopWindow();
625
626 test_add_atom();
627 test_get_atom_name();
628 test_error_handling();
629 test_local_add_atom();
630 test_local_get_atom_name();
631 test_local_error_handling();
632 }