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 %x\n", i );
135 SetLastError( 0xdeadbeef );
136 ok( GlobalAddAtomW((LPCWSTR)i) == i && GetLastError() == 0xdeadbeef,
137 "failed to add atom %x\n", i );
141 for (i = 0xc000; i <= 0xffff; i++)
143 ok( !GlobalAddAtomA((LPCSTR)i), "succeeded adding %x\n", i );
145 ok( !GlobalAddAtomW((LPCWSTR)i), "succeeded adding %x\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 );
225 ok( len == 0, "bad length %d\n", len );
226 ok( !memcmp( buf, "fooba\0....", 10 ), "bad buffer contents\n");
229 static const WCHAR resW[] = {'f','o','o','b','a','r','.','.','.','.'};
230 for (len = 0; len < 10; len++) bufW[len] = '.';
231 SetLastError(0xdeadbeef);
232 len = GlobalGetAtomNameW( atom, bufW, 6 );
233 ok( len && GetLastError() == 0xdeadbeef, "GlobalGetAtomNameW failed\n" );
234 ok( len == lstrlenW(foobarW), "bad length %d\n", len );
235 ok( !memcmp( bufW, resW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
238 /* test string limits & overflow */
239 do_initA(in, "abcdefghij", 255);
240 atom = GlobalAddAtomA(in);
241 ok(atom, "couldn't add atom for %s\n", in);
242 len = GlobalGetAtomNameA(atom, out, sizeof(out));
243 ok(len == 255, "length mismatch (%u instead of 255)\n", len);
244 for (i = 0; i < 255; i++)
246 ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
248 ok(out[255] == '\0', "wrong end of string\n");
249 memset(out, '.', sizeof(out));
250 SetLastError(0xdeadbeef);
251 len = GlobalGetAtomNameA(atom, out, 10);
252 if (!len) /* the NT way */
254 ok(GetLastError() == ERROR_MORE_DATA, "wrong error code (%u instead of %u)\n", GetLastError(), ERROR_MORE_DATA);
256 else /* the Win9x way */
258 ok(GetLastError() == 0xdeadbeef, "wrong error code (%u instead of %u)\n", GetLastError(), 0xdeadbeef);
260 for (i = 0; i < 9; i++)
262 ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
264 ok(out[9] == '\0', "wrong end of string\n");
265 ok(out[10] == '.', "wrote after end of buf\n");
266 do_initA(in, "abcdefghij", 256);
267 atom = GlobalAddAtomA(in);
268 ok(!atom, "succeeded\n");
271 /* test integral atoms */
272 for (i = 0; i <= 0xbfff; i++)
274 memset(outW, 'a', sizeof(outW));
275 len = GlobalGetAtomNameW( (ATOM)i, outW, 10 );
280 ok( (len > 1) && (len < 7), "bad length %d\n", len );
281 print_integral( res, i );
282 memset( res + lstrlenW(res) + 1, 'a', 10 * sizeof(WCHAR));
283 ok( !memcmp( res, outW, 10 * sizeof(WCHAR) ), "bad buffer contents for %d\n", i );
286 ok( !len, "bad length %d\n", len );
288 memset(outW, '.', sizeof(outW));
289 SetLastError(0xdeadbeef);
290 len = GlobalGetAtomNameW( (ATOM)i, outW, 1);
293 /* len == 0 with ERROR_MORE_DATA is on NT3.51 */
294 ok(len == 1 || (len == 0 && GetLastError() == ERROR_MORE_DATA),
295 "0x%04x: got %u with %d (excepted '1' or '0' with "
296 "ERROR_MORE_DATA)\n", i, len, GetLastError());
297 ok(outW[1] == DOUBLE('.'), "buffer overwrite\n");
299 else ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "0 badly handled\n");
302 do_initW(inW, "abcdefghij", 255);
303 atom = GlobalAddAtomW(inW);
304 ok(atom, "couldn't add atom for %s\n", in);
305 len = GlobalGetAtomNameW(atom, outW, sizeof(outW));
306 ok(len == 255, "length mismatch (%u instead of 255)\n", len);
307 for (i = 0; i < 255; i++)
309 ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
311 ok(outW[255] == '\0', "wrong end of string\n");
312 memset(outW, '.', sizeof(outW));
313 len = GlobalGetAtomNameW(atom, outW, 10);
314 ok(len == 10, "succeeded\n");
315 for (i = 0; i < 10; i++)
317 ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
319 ok(outW[10] == DOUBLE('.'), "wrote after end of buf\n");
320 do_initW(inW, "abcdefghij", 256);
321 atom = GlobalAddAtomW(inW);
322 ok(!atom, "succeeded\n");
323 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error code\n");
327 static void test_error_handling(void)
333 memset( buffer, 'a', 256 );
335 ok( !GlobalAddAtomA(buffer), "add succeeded\n" );
336 ok( !GlobalFindAtomA(buffer), "find succeeded\n" );
340 for (i = 0; i < 256; i++) bufferW[i] = 'b';
342 ok( !GlobalAddAtomW(bufferW), "add succeeded\n" );
343 ok( !GlobalFindAtomW(bufferW), "find succeeded\n" );
347 static void test_local_add_atom(void)
352 SetLastError( 0xdeadbeef );
353 atom = AddAtomA( "foobar" );
354 ok( atom >= 0xc000, "bad atom id %x\n", atom );
355 ok( GetLastError() == 0xdeadbeef, "AddAtomA set last error\n" );
357 /* Verify that it can be found (or not) appropriately */
358 ok( FindAtomA( "foobar" ) == atom, "could not find atom foobar\n" );
359 ok( FindAtomA( "FOOBAR" ) == atom, "could not find atom FOOBAR\n" );
360 ok( !FindAtomA( "_foobar" ), "found _foobar\n" );
362 /* Add the same atom, specifying string as unicode; should
363 * find the first one, not add a new one */
364 SetLastError( 0xdeadbeef );
365 w_atom = AddAtomW( foobarW );
366 if (w_atom && GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
369 trace("WARNING: Unicode atom APIs are not supported on this platform\n");
373 ok( w_atom == atom, "Unicode atom does not match ASCII\n" );
374 ok( GetLastError() == 0xdeadbeef, "AddAtomW set last error\n" );
377 /* Verify that it can be found (or not) appropriately via unicode name */
380 ok( FindAtomW( foobarW ) == atom, "could not find atom foobar\n" );
381 ok( FindAtomW( FOOBARW ) == atom, "could not find atom FOOBAR\n" );
382 ok( !FindAtomW( _foobarW ), "found _foobar\n" );
385 /* Test integer atoms
386 * (0x0001 .. 0xbfff) should be valid;
387 * (0xc000 .. 0xffff) should be invalid */
389 SetLastError( 0xdeadbeef );
390 ok( AddAtomA(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
393 SetLastError( 0xdeadbeef );
394 ok( AddAtomW(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
397 SetLastError( 0xdeadbeef );
398 for (i = 1; i <= 0xbfff; i++)
400 SetLastError( 0xdeadbeef );
401 ok( AddAtomA((LPCSTR)i) == i && GetLastError() == 0xdeadbeef,
402 "failed to add atom %x\n", i );
405 SetLastError( 0xdeadbeef );
406 ok( AddAtomW((LPCWSTR)i) == i && GetLastError() == 0xdeadbeef,
407 "failed to add atom %x\n", i );
411 for (i = 0xc000; i <= 0xffff; i++)
413 ok( !AddAtomA((LPCSTR)i), "succeeded adding %x\n", i );
415 ok( !AddAtomW((LPCWSTR)i), "succeeded adding %x\n", i );
419 static void test_local_get_atom_name(void)
421 char buf[10], in[257], out[257];
422 WCHAR bufW[10], inW[257], outW[257];
425 static const WCHAR resultW[] = {'f','o','o','b','a','r',0,'.','.','.'};
427 ATOM atom = AddAtomA( "foobar" );
429 /* Get the name of the atom we added above */
430 memset( buf, '.', sizeof(buf) );
431 len = GetAtomNameA( atom, buf, 10 );
432 ok( len == strlen("foobar"), "bad length %d\n", len );
433 ok( !memcmp( buf, "foobar\0...", 10 ), "bad buffer contents\n" );
435 /* Repeat, unicode-style */
438 for (i = 0; i < 10; i++) bufW[i] = '.';
439 SetLastError( 0xdeadbeef );
440 len = GetAtomNameW( atom, bufW, 10 );
441 ok( len && GetLastError() == 0xdeadbeef, "GetAtomNameW failed\n" );
442 ok( len == lstrlenW(foobarW), "bad length %d\n", len );
443 ok( !memcmp( bufW, resultW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
446 /* Get the name of the atom we added above */
447 memset( buf, '.', sizeof(buf) );
448 len = GetAtomNameA( atom, buf, 6 );
449 ok( len == 5, "bad length %d\n", len );
450 ok( !memcmp( buf, "fooba\0....", 10 ), "bad buffer contents\n" );
452 /* Repeat, unicode-style */
455 WCHAR resW[] = {'f','o','o','b','a','\0','.','.','.','.'};
456 for (i = 0; i < 10; i++) bufW[i] = '.';
457 SetLastError( 0xdeadbeef );
458 len = GetAtomNameW( atom, bufW, 6 );
459 ok( len && GetLastError() == 0xdeadbeef, "GlobalGetAtomNameW failed\n" );
460 ok( len == 5, "bad length %d\n", len );
461 ok( !memcmp( bufW, resW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
464 /* Check error code returns */
465 memset(buf, '.', 10);
466 ok( !GetAtomNameA( atom, buf, 0 ), "succeeded\n" );
467 ok( !memcmp( buf, "..........", 10 ), "should not touch buffer\n" );
471 static const WCHAR sampleW[10] = {'.','.','.','.','.','.','.','.','.','.'};
473 for (i = 0; i < 10; i++) bufW[i] = '.';
474 ok( !GetAtomNameW( atom, bufW, 0 ), "succeeded\n" );
475 ok( !memcmp( bufW, sampleW, 10 * sizeof(WCHAR) ), "should not touch buffer\n" );
478 /* Test integer atoms */
479 for (i = 0; i <= 0xbfff; i++)
481 memset( buf, 'a', 10 );
482 len = GetAtomNameA( (ATOM)i, buf, 10 );
486 ok( (len > 1) && (len < 7), "bad length %d for %s\n", len, buf );
487 sprintf( res, "#%d", i );
488 memset( res + strlen(res) + 1, 'a', 10 );
489 ok( !memcmp( res, buf, 10 ), "bad buffer contents %s\n", buf );
492 ok( !len, "bad length %d\n", len );
494 len = GetAtomNameA( (ATOM)i, buf, 1);
495 ok(!len, "succeed with %u for %u\n", len, i);
497 /* ERROR_MORE_DATA is on nt3.51 sp5 */
499 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER ||
500 GetLastError() == ERROR_MORE_DATA ||
501 GetLastError() == 0xdeadbeef, /* the Win 9x way */
502 "wrong error conditions %u for %u\n", GetLastError(), i);
504 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
505 GetLastError() == ERROR_MORE_DATA ||
506 GetLastError() == 0xdeadbeef, /* the Win 9x way */
507 "wrong error conditions %u for %u\n", GetLastError(), i);
509 /* test string limits & overflow */
510 do_initA(in, "abcdefghij", 255);
512 ok(atom, "couldn't add atom for %s\n", in);
513 len = GetAtomNameA(atom, out, sizeof(out));
514 ok(len == 255, "length mismatch (%u instead of 255)\n", len);
515 for (i = 0; i < 255; i++)
517 ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
519 ok(out[255] == '\0', "wrong end of string\n");
520 memset(out, '.', sizeof(out));
521 len = GetAtomNameA(atom, out, 10);
522 ok(len == 9, "succeeded %d\n", len);
523 for (i = 0; i < 9; i++)
525 ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
527 ok(out[9] == '\0', "wrong end of string\n");
528 ok(out[10] == '.', "buffer overwrite\n");
529 do_initA(in, "abcdefghij", 256);
531 ok(!atom, "succeeded\n");
533 /* ERROR_MORE_DATA is on nt3.51 sp5 */
534 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
535 GetLastError() == ERROR_MORE_DATA ||
536 GetLastError() == 0xdeadbeef, /* the Win 9x way */
537 "wrong error code (%u)\n", GetLastError());
541 /* test integral atoms */
542 for (i = 0; i <= 0xbfff; i++)
544 memset(outW, 'a', sizeof(outW));
545 len = GetAtomNameW( (ATOM)i, outW, 10 );
550 ok( (len > 1) && (len < 7), "bad length %d\n", len );
551 print_integral( res, i );
552 memset( res + lstrlenW(res) + 1, 'a', 10 * sizeof(WCHAR));
553 ok( !memcmp( res, outW, 10 * sizeof(WCHAR) ), "bad buffer contents for %d\n", i );
556 ok( !len, "bad length %d\n", len );
558 len = GetAtomNameW( (ATOM)i, outW, 1);
559 ok(!len, "succeed with %u for %u\n", len, i);
561 /* ERROR_MORE_DATA is on nt3.51 sp5 */
562 ok(GetLastError() == ERROR_MORE_DATA ||
563 GetLastError() == (i ? ERROR_INSUFFICIENT_BUFFER : ERROR_INVALID_PARAMETER),
564 "wrong error conditions %u for %u\n", GetLastError(), i);
566 do_initW(inW, "abcdefghij", 255);
567 atom = AddAtomW(inW);
568 ok(atom, "couldn't add atom for %s\n", in);
569 len = GetAtomNameW(atom, outW, sizeof(outW)/sizeof(outW[0]));
570 ok(len == 255, "length mismatch (%u instead of 255)\n", len);
571 for (i = 0; i < 255; i++)
573 ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
575 ok(outW[255] == '\0', "wrong end of string\n");
576 memset(outW, '.', sizeof(outW));
577 len = GetAtomNameW(atom, outW, 10);
578 ok(len == 9, "succeeded %d\n", len);
579 for (i = 0; i < 9; i++)
581 ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
583 ok(outW[9] == '\0', "wrong end of string\n");
584 ok(outW[10] == DOUBLE('.'), "buffer overwrite\n");
585 do_initW(inW, "abcdefghij", 256);
586 atom = AddAtomW(inW);
587 ok(!atom, "succeeded\n");
589 /* ERROR_MORE_DATA is on nt3.51 sp5 */
590 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
591 GetLastError() == ERROR_MORE_DATA,
592 "wrong error code (%u)\n", GetLastError());
596 static void test_local_error_handling(void)
602 memset( buffer, 'a', 256 );
604 ok( !AddAtomA(buffer), "add succeeded\n" );
605 ok( !FindAtomA(buffer), "find succeeded\n" );
609 for (i = 0; i < 256; i++) bufferW[i] = 'b';
611 ok( !AddAtomW(bufferW), "add succeeded\n" );
612 ok( !FindAtomW(bufferW), "find succeeded\n" );
619 test_get_atom_name();
620 test_error_handling();
621 test_local_add_atom();
622 test_local_get_atom_name();
623 test_local_error_handling();