atom.c 20.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Unit tests for atom functions
 *
 * Copyright (c) 2002 Alexandre Julliard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 23 24
#include <stdio.h>

#include "wine/test.h"
25
#include "windef.h"
26 27
#include "winbase.h"
#include "winerror.h"
28 29 30
#include "winuser.h"

#define DOUBLE(x)       (WCHAR)((x<<8)|(x))
31 32 33 34

static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
static const WCHAR FOOBARW[] = {'F','O','O','B','A','R',0};
static const WCHAR _foobarW[] = {'_','f','o','o','b','a','r',0};
35
static const WCHAR integfmt[] = {'#','%','d',0};
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static void do_initA(char* tmp, const char* pattern, int len)
{
    const char* p = pattern;

    while (len--)
    {
        *tmp++ = *p++;
        if (!*p) p = pattern;
    }
    *tmp = '\0';
}

static void do_initW(WCHAR* tmp, const char* pattern, int len)
{
    const char* p = pattern;

    while (len--)
    {
        *tmp++ = *p++;
        if (!*p) p = pattern;
    }
    *tmp = '\0';
}

61 62
static BOOL unicode_OS;

63 64 65
static void test_add_atom(void)
{
    ATOM atom, w_atom;
66
    INT_PTR i;
67 68 69

    SetLastError( 0xdeadbeef );
    atom = GlobalAddAtomA( "foobar" );
70 71
    ok( atom >= 0xc000, "bad atom id %x\n", atom );
    ok( GetLastError() == 0xdeadbeef, "GlobalAddAtomA set last error\n" );
72 73

    /* Verify that it can be found (or not) appropriately */
74 75 76
    ok( GlobalFindAtomA( "foobar" ) == atom, "could not find atom foobar\n" );
    ok( GlobalFindAtomA( "FOOBAR" ) == atom, "could not find atom FOOBAR\n" );
    ok( !GlobalFindAtomA( "_foobar" ), "found _foobar\n" );
77 78 79 80 81

    /* Add the same atom, specifying string as unicode; should
     * find the first one, not add a new one */
    SetLastError( 0xdeadbeef );
    w_atom = GlobalAddAtomW( foobarW );
82 83 84 85 86 87 88
    if (w_atom && GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
        unicode_OS = TRUE;
    else
        trace("WARNING: Unicode atom APIs are not supported on this platform\n");

    if (unicode_OS)
    {
89 90
        ok( w_atom == atom, "Unicode atom does not match ASCII\n" );
        ok( GetLastError() == 0xdeadbeef, "GlobalAddAtomW set last error\n" );
91
    }
92 93

    /* Verify that it can be found (or not) appropriately via unicode name */
94 95
    if (unicode_OS)
    {
96 97 98
        ok( GlobalFindAtomW( foobarW ) == atom, "could not find atom foobar\n" );
        ok( GlobalFindAtomW( FOOBARW ) == atom, "could not find atom FOOBAR\n" );
        ok( !GlobalFindAtomW( _foobarW ), "found _foobar\n" );
99
    }
100 101

    /* Test integer atoms
102
     * (0x0001 .. 0xbfff) should be valid;
103
     * (0xc000 .. 0xffff) should be invalid */
104 105

    SetLastError( 0xdeadbeef );
106
    ok( GlobalAddAtomA(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
107
    if (unicode_OS)
108 109
    {
        SetLastError( 0xdeadbeef );
110
        ok( GlobalAddAtomW(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
111
    }
112

113 114 115 116 117
    SetLastError( 0xdeadbeef );
    for (i = 1; i <= 0xbfff; i++)
    {
        SetLastError( 0xdeadbeef );
        ok( GlobalAddAtomA((LPCSTR)i) == i && GetLastError() == 0xdeadbeef,
118
            "failed to add atom %lx\n", i );
119 120 121 122
        if (unicode_OS)
        {
            SetLastError( 0xdeadbeef );
            ok( GlobalAddAtomW((LPCWSTR)i) == i && GetLastError() == 0xdeadbeef,
123
                "failed to add atom %lx\n", i );
124
        }
125
    }
126

127 128
    for (i = 0xc000; i <= 0xffff; i++)
    {
129
        ok( !GlobalAddAtomA((LPCSTR)i), "succeeded adding %lx\n", i );
130
        if (unicode_OS)
131
            ok( !GlobalAddAtomW((LPCWSTR)i), "succeeded adding %lx\n", i );
132 133 134 135 136 137 138
    }
}

static void test_get_atom_name(void)
{
    char buf[10];
    WCHAR bufW[10];
139 140
    int i;
    UINT len;
141
    static const WCHAR resultW[] = {'f','o','o','b','a','r',0,'.','.','.'};
142 143
    char in[257], out[257];
    WCHAR inW[257], outW[257];
144 145 146 147 148 149

    ATOM atom = GlobalAddAtomA( "foobar" );

    /* Get the name of the atom we added above */
    memset( buf, '.', sizeof(buf) );
    len = GlobalGetAtomNameA( atom, buf, 10 );
150 151
    ok( len == strlen("foobar"), "bad length %d\n", len );
    ok( !memcmp( buf, "foobar\0...", 10 ), "bad buffer contents\n" );
152 153

    /* Repeat, unicode-style */
154
    if (unicode_OS)
155
    {
156 157 158
        for (i = 0; i < 10; i++) bufW[i] = '.';
        SetLastError( 0xdeadbeef );
        len = GlobalGetAtomNameW( atom, bufW, 10 );
159 160 161
        ok( len && GetLastError() == 0xdeadbeef, "GlobalGetAtomNameW failed\n" );
        ok( len == lstrlenW(foobarW), "bad length %d\n", len );
        ok( !memcmp( bufW, resultW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
162 163 164
    }

    /* Check error code returns */
165
    memset(buf, '.', 10);
166 167
    ok( !GlobalGetAtomNameA( atom, buf,  0 ), "succeeded\n" );
    ok( !memcmp( buf, "..........", 10 ), "should not touch buffer\n" );
168 169 170

    if (unicode_OS)
    {
171
        static const WCHAR sampleW[] = {'.','.','.','.','.','.','.','.','.','.'};
172 173

        for (i = 0; i < 10; i++) bufW[i] = '.';
174
        ok( !GlobalGetAtomNameW( atom, bufW, 0 ), "succeeded\n" );
175
        ok( !memcmp( bufW, sampleW, sizeof(sampleW) ), "should not touch buffer\n" );
176
    }
177 178 179 180 181

    /* Test integer atoms */
    for (i = 0; i <= 0xbfff; i++)
    {
        memset( buf, 'a', 10 );
182
        len = GlobalGetAtomNameA( (ATOM)i, buf, 10 );
183 184 185
        if (i)
        {
            char res[20];
186
            ok( (len > 1) && (len < 7), "bad length %d\n", len );
187 188
            sprintf( res, "#%d", i );
            memset( res + strlen(res) + 1, 'a', 10 );
189
            ok( !memcmp( res, buf, 10 ), "bad buffer contents %s\n", buf );
190
            if (len <= 1 || len >= 7) break;  /* don't bother testing all of them */
191
        }
192
        else
193
            ok( !len, "bad length %d\n", len );
194

195
        SetLastError(0xdeadbeef);
196
        len = GlobalGetAtomNameA( (ATOM)i, buf, 2);
197 198 199
        ok(!len, "bad length %d\n", len);
	ok(GetLastError() == ERROR_MORE_DATA || GetLastError() == ERROR_INVALID_PARAMETER,
            "wrong error conditions %u for %u\n", GetLastError(), i);
200 201
    }

202 203
    memset( buf, '.', sizeof(buf) );
    len = GlobalGetAtomNameA( atom, buf, 6 );
204
    ok( len == 0, "bad length %d\n", len );
205 206 207 208 209 210 211 212 213 214 215 216
    ok( !memcmp( buf, "fooba\0....", 10 ), "bad buffer contents\n");
    if (unicode_OS)
    {
        static const WCHAR resW[] = {'f','o','o','b','a','r','.','.','.','.'};
        for (len = 0; len < 10; len++) bufW[len] = '.';
	SetLastError(0xdeadbeef);
        len = GlobalGetAtomNameW( atom, bufW, 6 );
        ok( len && GetLastError() == 0xdeadbeef, "GlobalGetAtomNameW failed\n" );
        ok( len == lstrlenW(foobarW), "bad length %d\n", len );
        ok( !memcmp( bufW, resW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
    }

217 218 219 220 221 222 223 224 225 226 227 228 229 230
    /* test string limits & overflow */
    do_initA(in, "abcdefghij", 255);
    atom = GlobalAddAtomA(in);
    ok(atom, "couldn't add atom for %s\n", in);
    len = GlobalGetAtomNameA(atom, out, sizeof(out));
    ok(len == 255, "length mismatch (%u instead of 255)\n", len);
    for (i = 0; i < 255; i++)
    {
        ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
    }
    ok(out[255] == '\0', "wrong end of string\n");
    memset(out, '.', sizeof(out));
    SetLastError(0xdeadbeef);
    len = GlobalGetAtomNameA(atom, out, 10);
231 232
    ok(!len, "bad length %d\n", len);
    ok(GetLastError() == ERROR_MORE_DATA, "wrong error code (%u instead of %u)\n", GetLastError(), ERROR_MORE_DATA);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    for (i = 0; i < 9; i++)
    {
        ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
    }
    ok(out[9] == '\0', "wrong end of string\n");
    ok(out[10] == '.', "wrote after end of buf\n");
    do_initA(in, "abcdefghij", 256);
    atom = GlobalAddAtomA(in);
    ok(!atom, "succeeded\n");
    if (unicode_OS)
    {
        /* test integral atoms */
        for (i = 0; i <= 0xbfff; i++)
        {
            memset(outW, 'a', sizeof(outW));
            len = GlobalGetAtomNameW( (ATOM)i, outW, 10 );
            if (i)
            {
                WCHAR res[20];
                
253
                ok( (len > 1) && (len < 7), "bad length %d\n", len );
254
                wsprintfW( res, integfmt, i );
255 256
                memset( res + lstrlenW(res) + 1, 'a', 10 * sizeof(WCHAR));
                ok( !memcmp( res, outW, 10 * sizeof(WCHAR) ), "bad buffer contents for %d\n", i );
257
                if (len <= 1 || len >= 7) break;  /* don't bother testing all of them */
258 259 260 261 262
            }
            else
                ok( !len, "bad length %d\n", len );

            memset(outW, '.', sizeof(outW));
263
            SetLastError(0xdeadbeef);
264 265 266
            len = GlobalGetAtomNameW( (ATOM)i, outW, 1);
            if (i)
            {
267 268
                /* len == 0 with ERROR_MORE_DATA is on NT3.51 */
                ok(len == 1 || (len == 0 && GetLastError() == ERROR_MORE_DATA),
269
                         "0x%04x: got %u with %d (expected '1' or '0' with "
270
                         "ERROR_MORE_DATA)\n", i, len, GetLastError());
271 272 273 274 275 276 277 278
                ok(outW[1] == DOUBLE('.'), "buffer overwrite\n");
            }
            else ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "0 badly handled\n");
        }

        do_initW(inW, "abcdefghij", 255);
        atom = GlobalAddAtomW(inW);
        ok(atom, "couldn't add atom for %s\n", in);
279
        len = GlobalGetAtomNameW(atom, outW, sizeof(outW)/sizeof(outW[0]));
280 281 282 283 284 285 286 287
        ok(len == 255, "length mismatch (%u instead of 255)\n", len);
        for (i = 0; i < 255; i++)
        {
            ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
        }
        ok(outW[255] == '\0', "wrong end of string\n");
        memset(outW, '.', sizeof(outW));
        len = GlobalGetAtomNameW(atom, outW, 10);
288
        ok(len == 10, "succeeded\n");
289 290
        for (i = 0; i < 10; i++)
        {
291
            ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
292 293 294 295 296 297
        }
        ok(outW[10] == DOUBLE('.'), "wrote after end of buf\n");
        do_initW(inW, "abcdefghij", 256);
        atom = GlobalAddAtomW(inW);
        ok(!atom, "succeeded\n");
        ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error code\n");
298 299 300 301 302 303 304 305 306
    }
}

static void test_error_handling(void)
{
    char buffer[260];
    WCHAR bufferW[260];
    int i;

307
    memset( buffer, 'a', 256 );
308
    buffer[256] = 0;
309 310
    ok( !GlobalAddAtomA(buffer), "add succeeded\n" );
    ok( !GlobalFindAtomA(buffer), "find succeeded\n" );
311 312 313 314 315

    if (unicode_OS)
    {
        for (i = 0; i < 256; i++) bufferW[i] = 'b';
        bufferW[256] = 0;
316 317
        ok( !GlobalAddAtomW(bufferW), "add succeeded\n" );
        ok( !GlobalFindAtomW(bufferW), "find succeeded\n" );
318
    }
319 320
}

321 322 323
static void test_local_add_atom(void)
{
    ATOM atom, w_atom;
324
    INT_PTR i;
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

    SetLastError( 0xdeadbeef );
    atom = AddAtomA( "foobar" );
    ok( atom >= 0xc000, "bad atom id %x\n", atom );
    ok( GetLastError() == 0xdeadbeef, "AddAtomA set last error\n" );

    /* Verify that it can be found (or not) appropriately */
    ok( FindAtomA( "foobar" ) == atom, "could not find atom foobar\n" );
    ok( FindAtomA( "FOOBAR" ) == atom, "could not find atom FOOBAR\n" );
    ok( !FindAtomA( "_foobar" ), "found _foobar\n" );

    /* Add the same atom, specifying string as unicode; should
     * find the first one, not add a new one */
    SetLastError( 0xdeadbeef );
    w_atom = AddAtomW( foobarW );
    if (w_atom && GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
        unicode_OS = TRUE;
    else
        trace("WARNING: Unicode atom APIs are not supported on this platform\n");

    if (unicode_OS)
    {
        ok( w_atom == atom, "Unicode atom does not match ASCII\n" );
        ok( GetLastError() == 0xdeadbeef, "AddAtomW set last error\n" );
    }

    /* Verify that it can be found (or not) appropriately via unicode name */
    if (unicode_OS)
    {
        ok( FindAtomW( foobarW ) == atom, "could not find atom foobar\n" );
        ok( FindAtomW( FOOBARW ) == atom, "could not find atom FOOBAR\n" );
        ok( !FindAtomW( _foobarW ), "found _foobar\n" );
    }

    /* Test integer atoms
     * (0x0001 .. 0xbfff) should be valid;
     * (0xc000 .. 0xffff) should be invalid */

    SetLastError( 0xdeadbeef );
    ok( AddAtomA(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
    if (unicode_OS)
    {
        SetLastError( 0xdeadbeef );
        ok( AddAtomW(0) == 0 && GetLastError() == 0xdeadbeef, "succeeded to add atom 0\n" );
    }

    SetLastError( 0xdeadbeef );
    for (i = 1; i <= 0xbfff; i++)
    {
        SetLastError( 0xdeadbeef );
        ok( AddAtomA((LPCSTR)i) == i && GetLastError() == 0xdeadbeef,
376
            "failed to add atom %lx\n", i );
377 378 379 380
        if (unicode_OS)
        {
            SetLastError( 0xdeadbeef );
            ok( AddAtomW((LPCWSTR)i) == i && GetLastError() == 0xdeadbeef,
381
                "failed to add atom %lx\n", i );
382 383 384 385 386
        }
    }

    for (i = 0xc000; i <= 0xffff; i++)
    {
387
        ok( !AddAtomA((LPCSTR)i), "succeeded adding %lx\n", i );
388
        if (unicode_OS)
389
            ok( !AddAtomW((LPCWSTR)i), "succeeded adding %lx\n", i );
390 391 392 393 394
    }
}

static void test_local_get_atom_name(void)
{
395 396
    char buf[10], in[257], out[257];
    WCHAR bufW[10], inW[257], outW[257];
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    int i;
    UINT len;
    static const WCHAR resultW[] = {'f','o','o','b','a','r',0,'.','.','.'};

    ATOM atom = AddAtomA( "foobar" );

    /* Get the name of the atom we added above */
    memset( buf, '.', sizeof(buf) );
    len = GetAtomNameA( atom, buf, 10 );
    ok( len == strlen("foobar"), "bad length %d\n", len );
    ok( !memcmp( buf, "foobar\0...", 10 ), "bad buffer contents\n" );

    /* Repeat, unicode-style */
    if (unicode_OS)
    {
        for (i = 0; i < 10; i++) bufW[i] = '.';
        SetLastError( 0xdeadbeef );
        len = GetAtomNameW( atom, bufW, 10 );
        ok( len && GetLastError() == 0xdeadbeef, "GetAtomNameW failed\n" );
        ok( len == lstrlenW(foobarW), "bad length %d\n", len );
        ok( !memcmp( bufW, resultW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
    }

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
    /* Get the name of the atom we added above */
    memset( buf, '.', sizeof(buf) );
    len = GetAtomNameA( atom, buf, 6 );
    ok( len == 5, "bad length %d\n", len );
    ok( !memcmp( buf, "fooba\0....", 10 ), "bad buffer contents\n" );
 
    /* Repeat, unicode-style */
    if (unicode_OS)
    {
        WCHAR resW[] = {'f','o','o','b','a','\0','.','.','.','.'};
        for (i = 0; i < 10; i++) bufW[i] = '.';
        SetLastError( 0xdeadbeef );
        len = GetAtomNameW( atom, bufW, 6 );
        ok( len && GetLastError() == 0xdeadbeef, "GlobalGetAtomNameW failed\n" );
        ok( len == 5, "bad length %d\n", len );
        ok( !memcmp( bufW, resW, 10*sizeof(WCHAR) ), "bad buffer contents\n" );
    }
 
438 439 440 441 442 443 444
    /* Check error code returns */
    memset(buf, '.', 10);
    ok( !GetAtomNameA( atom, buf,  0 ), "succeeded\n" );
    ok( !memcmp( buf, "..........", 10 ), "should not touch buffer\n" );

    if (unicode_OS)
    {
445
        static const WCHAR sampleW[] = {'.','.','.','.','.','.','.','.','.','.'};
446 447 448

        for (i = 0; i < 10; i++) bufW[i] = '.';
        ok( !GetAtomNameW( atom, bufW, 0 ), "succeeded\n" );
449
        ok( !memcmp( bufW, sampleW, sizeof(sampleW) ), "should not touch buffer\n" );
450 451 452 453 454 455 456 457 458 459
    }

    /* Test integer atoms */
    for (i = 0; i <= 0xbfff; i++)
    {
        memset( buf, 'a', 10 );
        len = GetAtomNameA( (ATOM)i, buf, 10 );
        if (i)
        {
            char res[20];
460
            ok( (len > 1) && (len < 7), "bad length %d for %s\n", len, buf );
461 462 463 464 465 466
            sprintf( res, "#%d", i );
            memset( res + strlen(res) + 1, 'a', 10 );
            ok( !memcmp( res, buf, 10 ), "bad buffer contents %s\n", buf );
        }
        else
            ok( !len, "bad length %d\n", len );
467 468

        len = GetAtomNameA( (ATOM)i, buf, 1);
469 470 471
        ok(!len, "succeed with %u for %u\n", len, i);

        /* ERROR_MORE_DATA is on nt3.51 sp5 */
472
        if (i)
473
            ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER ||
474
               GetLastError() == ERROR_MORE_DATA,
475
               "wrong error conditions %u for %u\n", GetLastError(), i);
476
        else
477
            ok(GetLastError() == ERROR_INVALID_PARAMETER ||
478
               GetLastError() == ERROR_MORE_DATA,
479
               "wrong error conditions %u for %u\n", GetLastError(), i);
480 481 482 483 484 485 486 487 488 489 490 491 492 493
    }
    /* test string limits & overflow */
    do_initA(in, "abcdefghij", 255);
    atom = AddAtomA(in);
    ok(atom, "couldn't add atom for %s\n", in);
    len = GetAtomNameA(atom, out, sizeof(out));
    ok(len == 255, "length mismatch (%u instead of 255)\n", len);
    for (i = 0; i < 255; i++)
    {
        ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
    }
    ok(out[255] == '\0', "wrong end of string\n");
    memset(out, '.', sizeof(out));
    len = GetAtomNameA(atom, out, 10);
494
    ok(len == 9, "succeeded %d\n", len);
495 496 497 498 499 500 501 502 503
    for (i = 0; i < 9; i++)
    {
        ok(out[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, out[i], "abcdefghij"[i % 10]);
    }
    ok(out[9] == '\0', "wrong end of string\n");
    ok(out[10] == '.', "buffer overwrite\n");
    do_initA(in, "abcdefghij", 256);
    atom = AddAtomA(in);
    ok(!atom, "succeeded\n");
504 505 506

    /* ERROR_MORE_DATA is on nt3.51 sp5 */
    ok(GetLastError() == ERROR_INVALID_PARAMETER ||
507
       GetLastError() == ERROR_MORE_DATA,
508
       "wrong error code (%u)\n", GetLastError());
509 510 511 512 513 514 515 516 517 518 519 520

    if (unicode_OS)
    {
        /* test integral atoms */
        for (i = 0; i <= 0xbfff; i++)
        {
            memset(outW, 'a', sizeof(outW));
            len = GetAtomNameW( (ATOM)i, outW, 10 );
            if (i)
            {
                WCHAR res[20];
                
521
                ok( (len > 1) && (len < 7), "bad length %d\n", len );
522
                wsprintfW( res, integfmt, i );
523 524 525 526 527 528 529
                memset( res + lstrlenW(res) + 1, 'a', 10 * sizeof(WCHAR));
                ok( !memcmp( res, outW, 10 * sizeof(WCHAR) ), "bad buffer contents for %d\n", i );
            }
            else
                ok( !len, "bad length %d\n", len );

            len = GetAtomNameW( (ATOM)i, outW, 1);
530 531 532 533 534
            ok(!len, "succeed with %u for %u\n", len, i);

            /* ERROR_MORE_DATA is on nt3.51 sp5 */
            ok(GetLastError() == ERROR_MORE_DATA ||
               GetLastError() == (i ? ERROR_INSUFFICIENT_BUFFER : ERROR_INVALID_PARAMETER),
535
               "wrong error conditions %u for %u\n", GetLastError(), i);
536 537 538 539
        }
        do_initW(inW, "abcdefghij", 255);
        atom = AddAtomW(inW);
        ok(atom, "couldn't add atom for %s\n", in);
540
        len = GetAtomNameW(atom, outW, sizeof(outW)/sizeof(outW[0]));
541 542 543 544 545 546 547 548
        ok(len == 255, "length mismatch (%u instead of 255)\n", len);
        for (i = 0; i < 255; i++)
        {
            ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
        }
        ok(outW[255] == '\0', "wrong end of string\n");
        memset(outW, '.', sizeof(outW));
        len = GetAtomNameW(atom, outW, 10);
549
        ok(len == 9, "succeeded %d\n", len);
550 551 552 553 554 555 556 557 558
        for (i = 0; i < 9; i++)
        {
            ok(outW[i] == "abcdefghij"[i % 10], "wrong string at %i (%c instead of %c)\n", i, outW[i], "abcdefghij"[i % 10]);
        }
        ok(outW[9] == '\0', "wrong end of string\n");
        ok(outW[10] == DOUBLE('.'), "buffer overwrite\n");
        do_initW(inW, "abcdefghij", 256);
        atom = AddAtomW(inW);
        ok(!atom, "succeeded\n");
559 560 561 562

        /* ERROR_MORE_DATA is on nt3.51 sp5 */
        ok(GetLastError() == ERROR_INVALID_PARAMETER ||
           GetLastError() == ERROR_MORE_DATA,
563
           "wrong error code (%u)\n", GetLastError());
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
    }
}

static void test_local_error_handling(void)
{
    char buffer[260];
    WCHAR bufferW[260];
    int i;

    memset( buffer, 'a', 256 );
    buffer[256] = 0;
    ok( !AddAtomA(buffer), "add succeeded\n" );
    ok( !FindAtomA(buffer), "find succeeded\n" );

    if (unicode_OS)
    {
        for (i = 0; i < 256; i++) bufferW[i] = 'b';
        bufferW[256] = 0;
        ok( !AddAtomW(bufferW), "add succeeded\n" );
        ok( !FindAtomW(bufferW), "find succeeded\n" );
    }
}

587 588
START_TEST(atom)
{
589 590 591 592
    /* Global atom table seems to be available to GUI apps only in
       Win7, so let's turn this app into a GUI app */
    GetDesktopWindow();

593 594 595
    test_add_atom();
    test_get_atom_name();
    test_error_handling();
596 597 598
    test_local_add_atom();
    test_local_get_atom_name();
    test_local_error_handling();
599
}