comctl32: A couple fixes for tab icon offsets.
[wine] / dlls / kernel / tests / heap.c
1 /*
2  * Unit test suite for heap functions
3  *
4  * Copyright 2003 Dimitrie O. Paun
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 <stdlib.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wine/test.h"
27
28 #define MAGIC_DEAD 0xdeadbeef
29
30 static SIZE_T resize_9x(SIZE_T size)
31 {
32     DWORD dwSizeAligned = (size + 3) & ~3;
33     return max(dwSizeAligned, 12); /* at least 12 bytes */
34 }
35
36 START_TEST(heap)
37 {
38     void *mem;
39     HGLOBAL gbl;
40     HGLOBAL hsecond;
41     SIZE_T  size;
42
43     /* Heap*() functions */
44     mem = HeapAlloc(GetProcessHeap(), 0, 0);
45     ok(mem != NULL, "memory not allocated for size 0\n");
46
47     mem = HeapReAlloc(GetProcessHeap(), 0, NULL, 10);
48     ok(mem == NULL, "memory allocated by HeapReAlloc\n");
49
50     for (size = 0; size <= 256; size++)
51     {
52         SIZE_T heap_size;
53         mem = HeapAlloc(GetProcessHeap(), 0, size);
54         heap_size = HeapSize(GetProcessHeap(), 0, mem);
55         ok(heap_size == size || heap_size == resize_9x(size), 
56             "HeapSize returned %lu instead of %lu or %lu\n", heap_size, size, resize_9x(size));
57         HeapFree(GetProcessHeap(), 0, mem);
58     }
59
60     /* Global*() functions */
61     gbl = GlobalAlloc(GMEM_MOVEABLE, 0);
62     ok(gbl != NULL, "global memory not allocated for size 0\n");
63
64     gbl = GlobalReAlloc(gbl, 10, GMEM_MOVEABLE);
65     ok(gbl != NULL, "Can't realloc global memory\n");
66     size = GlobalSize(gbl);
67     ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
68
69     gbl = GlobalReAlloc(gbl, 0, GMEM_MOVEABLE);
70     ok(gbl != NULL, "GlobalReAlloc should not fail on size 0\n");
71
72     size = GlobalSize(gbl);
73     ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
74     ok(GlobalFree(gbl) == NULL, "Memory not freed\n");
75     size = GlobalSize(gbl);
76     ok(size == 0, "Memory should have been freed, size=%ld\n", size);
77
78     gbl = GlobalReAlloc(0, 10, GMEM_MOVEABLE);
79     ok(gbl == NULL, "global realloc allocated memory\n");
80
81     /* invalid handles are catched in windows */
82     gbl = GlobalAlloc(GMEM_MOVEABLE, 256);
83     GlobalFree(gbl);
84     SetLastError(MAGIC_DEAD);
85     hsecond = GlobalFree(gbl);      /* invalid handle: free memory twice */
86     ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
87         "returned %p with 0x%08lx (expected %p with ERROR_INVALID_HANDLE)\n",
88         hsecond, GetLastError(), gbl);
89
90     /* Local*() functions */
91     gbl = LocalAlloc(LMEM_MOVEABLE, 0);
92     ok(gbl != NULL, "local memory not allocated for size 0\n");
93
94     gbl = LocalReAlloc(gbl, 10, LMEM_MOVEABLE);
95     ok(gbl != NULL, "Can't realloc local memory\n");
96     size = LocalSize(gbl);
97     ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
98
99     gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE);
100     ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n");
101
102     size = LocalSize(gbl);
103     ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
104     ok(LocalFree(gbl) == NULL, "Memory not freed\n");
105     size = LocalSize(gbl);
106     ok(size == 0, "Memory should have been freed, size=%ld\n", size);
107
108     gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE);
109     ok(gbl == NULL, "local realloc allocated memory\n");
110
111     /* invalid handles are catched in windows */
112     gbl = LocalAlloc(GMEM_MOVEABLE, 256);
113     LocalFree(gbl);
114     SetLastError(MAGIC_DEAD);
115     hsecond = LocalFree(gbl);       /* invalid handle: free memory twice */
116     ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
117         "returned %p with 0x%08lx (expected %p with ERROR_INVALID_HANDLE)\n",
118         hsecond, GetLastError(), gbl);
119
120     /* trying to lock empty memory should give an error */
121     gbl = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,0);
122     ok(gbl != NULL, "returned NULL\n");
123     SetLastError(0xdeadbeef);
124     mem = GlobalLock(gbl);
125     ok( GetLastError() == ERROR_DISCARDED, "should return an error\n");
126     ok( mem == NULL, "should return NULL\n");
127     GlobalFree(gbl);
128 }