2 * Unit tests for atom functions
4 * Copyright (c) 2002 Alexandre Julliard
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.
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.
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
24 #include "wine/test.h"
30 #define DOUBLE(x) (WCHAR)((x<<8)|(x))
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};
36 static void do_initA(char* tmp, const char* pattern, int len)
38 const char* p = pattern;
48 static void do_initW(WCHAR* tmp, const char* pattern, int len)
50 const char* p = pattern;
60 static void print_integral( WCHAR* buffer, int atom )
64 #define X(v) { if (atom >= v) {*buffer++ = '0' + atom / v; first = FALSE; } else if (!first || v == 1) *buffer++ = '0'; atom %= v; }
75 static BOOL unicode_OS;
77 static void test_add_atom(void)
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" );
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" );
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)
99 trace("WARNING: Unicode atom APIs are not supported on this platform\n");
103 ok( w_atom == atom, "Unicode atom does not match ASCII\n" );
104 ok( GetLastError() == 0xdeadbeef, "GlobalAddAtomW set last error\n" );
107 /* Verify that it can be found (or not) appropriately via unicode name */
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" );
115 /* Test integer atoms
116 * (0x0001 .. 0xbfff) should be valid;
117 * (0xc000 .. 0xffff) should be invalid */
119 SetLastError( 0xdeadbeef );
120 ok( GlobalAddAtomA(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
123 SetLastError( 0xdeadbeef );
124 ok( GlobalAddAtomW(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
127 SetLastError( 0xdeadbeef );
128 for (i = 1; i <= 0xbfff; i++)
130 SetLastError( 0xdeadbeef );
131 ok( GlobalAddAtomA((LPCSTR)i) == i && GetLastError() == 0xdeadbeef,
132 "failed to add atom %lx\n", i );
135 SetLastError( 0xdeadbeef );
136 ok( GlobalAddAtomW((LPCWSTR)i) == i && GetLastError() == 0xdeadbeef,
137 "failed to add atom %lx\n", i );
141 for (i = 0xc000; i <= 0xffff; i++)
143 ok( !GlobalAddAtomA((LPCSTR)i), "succeeded adding %lx\n", i );
145 ok( !GlobalAddAtomW((LPCWSTR)i), "succeeded adding %lx\n", i );
149 static void test_get_atom_name(void)
155 static const WCHAR resultW[] = {'f','o','o','b','a','r',0,'.','.','.'};
156 char in[257], out[257];
157 WCHAR inW[257], outW[257];
159 ATOM atom = GlobalAddAtomA( "foobar" );
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" );
167 /* Repeat, unicode-style */
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" );
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" );
185 static const WCHAR sampleW[10] = {'.','.','.','.','.','.','.','.','.','.'};
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" );
192 /* Test integer atoms */
193 for (i = 0; i <= 0xbfff; i++)
195 memset( buf, 'a', 10 );
196 len = GlobalGetAtomNameA( (ATOM)i, buf, 10 );
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 );
206 ok( !len, "bad length %d\n", len );
208 SetLastError(0xdeadbeef);
209 len = GlobalGetAtomNameA( (ATOM)i, buf, 2);
210 if (!len) /* the NT way */
212 ok(GetLastError() == (i ? ERROR_MORE_DATA : ERROR_INVALID_PARAMETER) ||
213 GetLastError() == 0xdeadbeef, /* the Win 9x way */
214 "wrong error conditions %u for %u\n", GetLastError(), i);
216 else /* the Win 9x way */
218 ok(GetLastError() == 0xdeadbeef,
219 "wrong error conditions %u for %u\n", GetLastError(), i);
223 memset( buf, '.', sizeof(buf) );
224 len = GlobalGetAtomNameA( atom, buf, 6 );
226 len == 5, /* win9x */
227 "bad length %d\n", len );
228 ok( !memcmp( buf, "fooba\0....", 10 ), "bad buffer contents\n");
231 static const WCHAR resW[] = {'f','o','o','b','a','r','.','.','.','.'};
232 for (len = 0; len < 10; len++) bufW[len] = '.';
233 SetLastError(0xdeadbeef);
234 len = GlobalGetAtomNameW( atom, bufW, 6 );
235 ok( len && GetLastError() == 0xdeadbeef, "GlobalGetAtomNameW failed\n" );
236 ok( len == lstrlenW(foobarW), "bad length %d\n", len );
237 ok( !memcmp( bufW, resW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
240 /* test string limits & overflow */
241 do_initA(in, "abcdefghij", 255);
242 atom = GlobalAddAtomA(in);
243 ok(atom, "couldn't add atom for %s\n", in);
244 len = GlobalGetAtomNameA(atom, out, sizeof(out));
245 ok(len == 255, "length mismatch (%u instead of 255)\n", len);
246 for (i = 0; i < 255; i++)
248 ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
250 ok(out[255] == '\0', "wrong end of string\n");
251 memset(out, '.', sizeof(out));
252 SetLastError(0xdeadbeef);
253 len = GlobalGetAtomNameA(atom, out, 10);
254 if (!len) /* the NT way */
256 ok(GetLastError() == ERROR_MORE_DATA, "wrong error code (%u instead of %u)\n", GetLastError(), ERROR_MORE_DATA);
258 else /* the Win9x way */
260 ok(GetLastError() == 0xdeadbeef, "wrong error code (%u instead of %u)\n", GetLastError(), 0xdeadbeef);
262 for (i = 0; i < 9; i++)
264 ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
266 ok(out[9] == '\0', "wrong end of string\n");
267 ok(out[10] == '.', "wrote after end of buf\n");
268 do_initA(in, "abcdefghij", 256);
269 atom = GlobalAddAtomA(in);
270 ok(!atom, "succeeded\n");
273 /* test integral atoms */
274 for (i = 0; i <= 0xbfff; i++)
276 memset(outW, 'a', sizeof(outW));
277 len = GlobalGetAtomNameW( (ATOM)i, outW, 10 );
282 ok( (len > 1) && (len < 7), "bad length %d\n", len );
283 print_integral( res, i );
284 memset( res + lstrlenW(res) + 1, 'a', 10 * sizeof(WCHAR));
285 ok( !memcmp( res, outW, 10 * sizeof(WCHAR) ), "bad buffer contents for %d\n", i );
288 ok( !len, "bad length %d\n", len );
290 memset(outW, '.', sizeof(outW));
291 SetLastError(0xdeadbeef);
292 len = GlobalGetAtomNameW( (ATOM)i, outW, 1);
295 /* len == 0 with ERROR_MORE_DATA is on NT3.51 */
296 ok(len == 1 || (len == 0 && GetLastError() == ERROR_MORE_DATA),
297 "0x%04x: got %u with %d (excepted '1' or '0' with "
298 "ERROR_MORE_DATA)\n", i, len, GetLastError());
299 ok(outW[1] == DOUBLE('.'), "buffer overwrite\n");
301 else ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "0 badly handled\n");
304 do_initW(inW, "abcdefghij", 255);
305 atom = GlobalAddAtomW(inW);
306 ok(atom, "couldn't add atom for %s\n", in);
307 len = GlobalGetAtomNameW(atom, outW, sizeof(outW)/sizeof(outW[0]));
308 ok(len == 255, "length mismatch (%u instead of 255)\n", len);
309 for (i = 0; i < 255; i++)
311 ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
313 ok(outW[255] == '\0', "wrong end of string\n");
314 memset(outW, '.', sizeof(outW));
315 len = GlobalGetAtomNameW(atom, outW, 10);
316 ok(len == 10, "succeeded\n");
317 for (i = 0; i < 10; i++)
319 ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
321 ok(outW[10] == DOUBLE('.'), "wrote after end of buf\n");
322 do_initW(inW, "abcdefghij", 256);
323 atom = GlobalAddAtomW(inW);
324 ok(!atom, "succeeded\n");
325 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error code\n");
329 static void test_error_handling(void)
335 memset( buffer, 'a', 256 );
337 ok( !GlobalAddAtomA(buffer), "add succeeded\n" );
338 ok( !GlobalFindAtomA(buffer), "find succeeded\n" );
342 for (i = 0; i < 256; i++) bufferW[i] = 'b';
344 ok( !GlobalAddAtomW(bufferW), "add succeeded\n" );
345 ok( !GlobalFindAtomW(bufferW), "find succeeded\n" );
349 static void test_local_add_atom(void)
354 SetLastError( 0xdeadbeef );
355 atom = AddAtomA( "foobar" );
356 ok( atom >= 0xc000, "bad atom id %x\n", atom );
357 ok( GetLastError() == 0xdeadbeef, "AddAtomA set last error\n" );
359 /* Verify that it can be found (or not) appropriately */
360 ok( FindAtomA( "foobar" ) == atom, "could not find atom foobar\n" );
361 ok( FindAtomA( "FOOBAR" ) == atom, "could not find atom FOOBAR\n" );
362 ok( !FindAtomA( "_foobar" ), "found _foobar\n" );
364 /* Add the same atom, specifying string as unicode; should
365 * find the first one, not add a new one */
366 SetLastError( 0xdeadbeef );
367 w_atom = AddAtomW( foobarW );
368 if (w_atom && GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
371 trace("WARNING: Unicode atom APIs are not supported on this platform\n");
375 ok( w_atom == atom, "Unicode atom does not match ASCII\n" );
376 ok( GetLastError() == 0xdeadbeef, "AddAtomW set last error\n" );
379 /* Verify that it can be found (or not) appropriately via unicode name */
382 ok( FindAtomW( foobarW ) == atom, "could not find atom foobar\n" );
383 ok( FindAtomW( FOOBARW ) == atom, "could not find atom FOOBAR\n" );
384 ok( !FindAtomW( _foobarW ), "found _foobar\n" );
387 /* Test integer atoms
388 * (0x0001 .. 0xbfff) should be valid;
389 * (0xc000 .. 0xffff) should be invalid */
391 SetLastError( 0xdeadbeef );
392 ok( AddAtomA(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
395 SetLastError( 0xdeadbeef );
396 ok( AddAtomW(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
399 SetLastError( 0xdeadbeef );
400 for (i = 1; i <= 0xbfff; i++)
402 SetLastError( 0xdeadbeef );
403 ok( AddAtomA((LPCSTR)i) == i && GetLastError() == 0xdeadbeef,
404 "failed to add atom %lx\n", i );
407 SetLastError( 0xdeadbeef );
408 ok( AddAtomW((LPCWSTR)i) == i && GetLastError() == 0xdeadbeef,
409 "failed to add atom %lx\n", i );
413 for (i = 0xc000; i <= 0xffff; i++)
415 ok( !AddAtomA((LPCSTR)i), "succeeded adding %lx\n", i );
417 ok( !AddAtomW((LPCWSTR)i), "succeeded adding %lx\n", i );
421 static void test_local_get_atom_name(void)
423 char buf[10], in[257], out[257];
424 WCHAR bufW[10], inW[257], outW[257];
427 static const WCHAR resultW[] = {'f','o','o','b','a','r',0,'.','.','.'};
429 ATOM atom = AddAtomA( "foobar" );
431 /* Get the name of the atom we added above */
432 memset( buf, '.', sizeof(buf) );
433 len = GetAtomNameA( atom, buf, 10 );
434 ok( len == strlen("foobar"), "bad length %d\n", len );
435 ok( !memcmp( buf, "foobar\0...", 10 ), "bad buffer contents\n" );
437 /* Repeat, unicode-style */
440 for (i = 0; i < 10; i++) bufW[i] = '.';
441 SetLastError( 0xdeadbeef );
442 len = GetAtomNameW( atom, bufW, 10 );
443 ok( len && GetLastError() == 0xdeadbeef, "GetAtomNameW failed\n" );
444 ok( len == lstrlenW(foobarW), "bad length %d\n", len );
445 ok( !memcmp( bufW, resultW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
448 /* Get the name of the atom we added above */
449 memset( buf, '.', sizeof(buf) );
450 len = GetAtomNameA( atom, buf, 6 );
451 ok( len == 5, "bad length %d\n", len );
452 ok( !memcmp( buf, "fooba\0....", 10 ), "bad buffer contents\n" );
454 /* Repeat, unicode-style */
457 WCHAR resW[] = {'f','o','o','b','a','\0','.','.','.','.'};
458 for (i = 0; i < 10; i++) bufW[i] = '.';
459 SetLastError( 0xdeadbeef );
460 len = GetAtomNameW( atom, bufW, 6 );
461 ok( len && GetLastError() == 0xdeadbeef, "GlobalGetAtomNameW failed\n" );
462 ok( len == 5, "bad length %d\n", len );
463 ok( !memcmp( bufW, resW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
466 /* Check error code returns */
467 memset(buf, '.', 10);
468 ok( !GetAtomNameA( atom, buf, 0 ), "succeeded\n" );
469 ok( !memcmp( buf, "..........", 10 ), "should not touch buffer\n" );
473 static const WCHAR sampleW[10] = {'.','.','.','.','.','.','.','.','.','.'};
475 for (i = 0; i < 10; i++) bufW[i] = '.';
476 ok( !GetAtomNameW( atom, bufW, 0 ), "succeeded\n" );
477 ok( !memcmp( bufW, sampleW, 10 * sizeof(WCHAR) ), "should not touch buffer\n" );
480 /* Test integer atoms */
481 for (i = 0; i <= 0xbfff; i++)
483 memset( buf, 'a', 10 );
484 len = GetAtomNameA( (ATOM)i, buf, 10 );
488 ok( (len > 1) && (len < 7), "bad length %d for %s\n", len, buf );
489 sprintf( res, "#%d", i );
490 memset( res + strlen(res) + 1, 'a', 10 );
491 ok( !memcmp( res, buf, 10 ), "bad buffer contents %s\n", buf );
494 ok( !len, "bad length %d\n", len );
496 len = GetAtomNameA( (ATOM)i, buf, 1);
497 ok(!len, "succeed with %u for %u\n", len, i);
499 /* ERROR_MORE_DATA is on nt3.51 sp5 */
501 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER ||
502 GetLastError() == ERROR_MORE_DATA ||
503 GetLastError() == 0xdeadbeef, /* the Win 9x way */
504 "wrong error conditions %u for %u\n", GetLastError(), i);
506 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
507 GetLastError() == ERROR_MORE_DATA ||
508 GetLastError() == 0xdeadbeef, /* the Win 9x way */
509 "wrong error conditions %u for %u\n", GetLastError(), i);
511 /* test string limits & overflow */
512 do_initA(in, "abcdefghij", 255);
514 ok(atom, "couldn't add atom for %s\n", in);
515 len = GetAtomNameA(atom, out, sizeof(out));
516 ok(len == 255, "length mismatch (%u instead of 255)\n", len);
517 for (i = 0; i < 255; i++)
519 ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
521 ok(out[255] == '\0', "wrong end of string\n");
522 memset(out, '.', sizeof(out));
523 len = GetAtomNameA(atom, out, 10);
524 ok(len == 9, "succeeded %d\n", len);
525 for (i = 0; i < 9; i++)
527 ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
529 ok(out[9] == '\0', "wrong end of string\n");
530 ok(out[10] == '.', "buffer overwrite\n");
531 do_initA(in, "abcdefghij", 256);
533 ok(!atom, "succeeded\n");
535 /* ERROR_MORE_DATA is on nt3.51 sp5 */
536 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
537 GetLastError() == ERROR_MORE_DATA ||
538 GetLastError() == 0xdeadbeef, /* the Win 9x way */
539 "wrong error code (%u)\n", GetLastError());
543 /* test integral atoms */
544 for (i = 0; i <= 0xbfff; i++)
546 memset(outW, 'a', sizeof(outW));
547 len = GetAtomNameW( (ATOM)i, outW, 10 );
552 ok( (len > 1) && (len < 7), "bad length %d\n", len );
553 print_integral( res, i );
554 memset( res + lstrlenW(res) + 1, 'a', 10 * sizeof(WCHAR));
555 ok( !memcmp( res, outW, 10 * sizeof(WCHAR) ), "bad buffer contents for %d\n", i );
558 ok( !len, "bad length %d\n", len );
560 len = GetAtomNameW( (ATOM)i, outW, 1);
561 ok(!len, "succeed with %u for %u\n", len, i);
563 /* ERROR_MORE_DATA is on nt3.51 sp5 */
564 ok(GetLastError() == ERROR_MORE_DATA ||
565 GetLastError() == (i ? ERROR_INSUFFICIENT_BUFFER : ERROR_INVALID_PARAMETER),
566 "wrong error conditions %u for %u\n", GetLastError(), i);
568 do_initW(inW, "abcdefghij", 255);
569 atom = AddAtomW(inW);
570 ok(atom, "couldn't add atom for %s\n", in);
571 len = GetAtomNameW(atom, outW, sizeof(outW)/sizeof(outW[0]));
572 ok(len == 255, "length mismatch (%u instead of 255)\n", len);
573 for (i = 0; i < 255; i++)
575 ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
577 ok(outW[255] == '\0', "wrong end of string\n");
578 memset(outW, '.', sizeof(outW));
579 len = GetAtomNameW(atom, outW, 10);
580 ok(len == 9, "succeeded %d\n", len);
581 for (i = 0; i < 9; i++)
583 ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
585 ok(outW[9] == '\0', "wrong end of string\n");
586 ok(outW[10] == DOUBLE('.'), "buffer overwrite\n");
587 do_initW(inW, "abcdefghij", 256);
588 atom = AddAtomW(inW);
589 ok(!atom, "succeeded\n");
591 /* ERROR_MORE_DATA is on nt3.51 sp5 */
592 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
593 GetLastError() == ERROR_MORE_DATA,
594 "wrong error code (%u)\n", GetLastError());
598 static void test_local_error_handling(void)
604 memset( buffer, 'a', 256 );
606 ok( !AddAtomA(buffer), "add succeeded\n" );
607 ok( !FindAtomA(buffer), "find succeeded\n" );
611 for (i = 0; i < 256; i++) bufferW[i] = 'b';
613 ok( !AddAtomW(bufferW), "add succeeded\n" );
614 ok( !FindAtomW(bufferW), "find succeeded\n" );
621 test_get_atom_name();
622 test_error_handling();
623 test_local_add_atom();
624 test_local_get_atom_name();
625 test_local_error_handling();