Release 0.9.39.
[wine/gsoc-2012-control.git] / dlls / kernel32 / tests / heap.c
bloba02a8d4cb7c379792b52ac0ce06875d9181ab347
1 /*
2 * Unit test suite for heap functions
4 * Copyright 2003 Dimitrie O. Paun
5 * Copyright 2006 Detlef Riekenberg
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
23 #include <stdlib.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wine/test.h"
29 #define MAGIC_DEAD 0xdeadbeef
31 static SIZE_T resize_9x(SIZE_T size)
33 DWORD dwSizeAligned = (size + 3) & ~3;
34 return max(dwSizeAligned, 12); /* at least 12 bytes */
37 START_TEST(heap)
39 LPVOID mem;
40 LPVOID msecond;
41 DWORD res;
42 UINT flags;
43 HGLOBAL gbl;
44 HGLOBAL hsecond;
45 SIZE_T size;
47 /* Heap*() functions */
48 mem = HeapAlloc(GetProcessHeap(), 0, 0);
49 ok(mem != NULL, "memory not allocated for size 0\n");
51 mem = HeapReAlloc(GetProcessHeap(), 0, NULL, 10);
52 ok(mem == NULL, "memory allocated by HeapReAlloc\n");
54 for (size = 0; size <= 256; size++)
56 SIZE_T heap_size;
57 mem = HeapAlloc(GetProcessHeap(), 0, size);
58 heap_size = HeapSize(GetProcessHeap(), 0, mem);
59 ok(heap_size == size || heap_size == resize_9x(size),
60 "HeapSize returned %lu instead of %lu or %lu\n", heap_size, size, resize_9x(size));
61 HeapFree(GetProcessHeap(), 0, mem);
64 /* test some border cases of HeapAlloc and HeapReAlloc */
65 mem = HeapAlloc(GetProcessHeap(), 0, 0);
66 ok(mem != NULL, "memory not allocated for size 0\n");
67 msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~0UL - 7);
68 ok(msecond == NULL, "HeapReAlloc(0xfffffff8) should have failed\n");
69 msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~0UL);
70 ok(msecond == NULL, "HeapReAlloc(0xffffffff) should have failed\n");
71 HeapFree(GetProcessHeap(), 0, mem);
72 mem = HeapAlloc(GetProcessHeap(), 0, ~0UL);
73 ok(mem == NULL, "memory allocated for size ~0UL\n");
75 /* Global*() functions */
76 gbl = GlobalAlloc(GMEM_MOVEABLE, 0);
77 ok(gbl != NULL, "global memory not allocated for size 0\n");
79 gbl = GlobalReAlloc(gbl, 10, GMEM_MOVEABLE);
80 ok(gbl != NULL, "Can't realloc global memory\n");
81 size = GlobalSize(gbl);
82 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
84 gbl = GlobalReAlloc(gbl, 0, GMEM_MOVEABLE);
85 ok(gbl != NULL, "GlobalReAlloc should not fail on size 0\n");
87 size = GlobalSize(gbl);
88 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
89 ok(GlobalFree(gbl) == NULL, "Memory not freed\n");
90 size = GlobalSize(gbl);
91 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
93 gbl = GlobalReAlloc(0, 10, GMEM_MOVEABLE);
94 ok(gbl == NULL, "global realloc allocated memory\n");
96 /* GlobalLock / GlobalUnlock with a valid handle */
97 gbl = GlobalAlloc(GMEM_MOVEABLE, 256);
99 SetLastError(MAGIC_DEAD);
100 mem = GlobalLock(gbl); /* #1 */
101 ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
102 SetLastError(MAGIC_DEAD);
103 flags = GlobalFlags(gbl);
104 ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
105 flags, GetLastError());
107 SetLastError(MAGIC_DEAD);
108 msecond = GlobalLock(gbl); /* #2 */
109 ok( msecond == mem, "returned %p with %d (expected '%p')\n",
110 msecond, GetLastError(), mem);
111 SetLastError(MAGIC_DEAD);
112 flags = GlobalFlags(gbl);
113 ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
114 flags, GetLastError());
115 SetLastError(MAGIC_DEAD);
117 SetLastError(MAGIC_DEAD);
118 res = GlobalUnlock(gbl); /* #1 */
119 ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
120 SetLastError(MAGIC_DEAD);
121 flags = GlobalFlags(gbl);
122 ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
123 flags, GetLastError());
125 SetLastError(MAGIC_DEAD);
126 res = GlobalUnlock(gbl); /* #0 */
127 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
128 ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
129 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
130 "MAGIC_DEAD)\n", res, GetLastError());
131 SetLastError(MAGIC_DEAD);
132 flags = GlobalFlags(gbl);
133 ok( !flags , "returned 0x%04x with %d (expected '0')\n",
134 flags, GetLastError());
136 /* Unlock an already unlocked Handle */
137 SetLastError(MAGIC_DEAD);
138 res = GlobalUnlock(gbl);
139 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
140 ok( !res &&
141 ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
142 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
143 "MAGIC_DEAD)\n", res, GetLastError());
145 GlobalFree(gbl);
146 /* invalid handles are caught in windows: */
147 SetLastError(MAGIC_DEAD);
148 hsecond = GlobalFree(gbl); /* invalid handle: free memory twice */
149 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
150 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
151 hsecond, GetLastError(), gbl);
152 SetLastError(MAGIC_DEAD);
153 flags = GlobalFlags(gbl);
154 ok( (flags == GMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
155 "returned 0x%04x with 0x%08x (expected GMEM_INVALID_HANDLE with "
156 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
157 SetLastError(MAGIC_DEAD);
158 size = GlobalSize(gbl);
159 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
160 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
161 size, GetLastError());
163 SetLastError(MAGIC_DEAD);
164 mem = GlobalLock(gbl);
165 ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
166 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
167 mem, GetLastError());
169 /* documented on MSDN: GlobalUnlock() return FALSE on failure.
170 Win9x and wine return FALSE with ERROR_INVALID_HANDLE, but on
171 NT 3.51 and XPsp2, TRUE with ERROR_INVALID_HANDLE is returned.
172 The similar Test for LocalUnlock() works on all Systems */
173 SetLastError(MAGIC_DEAD);
174 res = GlobalUnlock(gbl);
175 ok(GetLastError() == ERROR_INVALID_HANDLE,
176 "returned %d with %d (expected ERROR_INVALID_HANDLE)\n",
177 res, GetLastError());
180 /* ####################################### */
181 /* Local*() functions */
182 gbl = LocalAlloc(LMEM_MOVEABLE, 0);
183 ok(gbl != NULL, "local memory not allocated for size 0\n");
185 gbl = LocalReAlloc(gbl, 10, LMEM_MOVEABLE);
186 ok(gbl != NULL, "Can't realloc local memory\n");
187 size = LocalSize(gbl);
188 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
190 gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE);
191 ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n");
193 size = LocalSize(gbl);
194 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
195 ok(LocalFree(gbl) == NULL, "Memory not freed\n");
196 size = LocalSize(gbl);
197 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
199 gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE);
200 ok(gbl == NULL, "local realloc allocated memory\n");
202 /* LocalLock / LocalUnlock with a valid handle */
203 gbl = LocalAlloc(LMEM_MOVEABLE, 256);
204 SetLastError(MAGIC_DEAD);
205 mem = LocalLock(gbl); /* #1 */
206 ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
207 SetLastError(MAGIC_DEAD);
208 flags = LocalFlags(gbl);
209 ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
210 flags, GetLastError());
212 SetLastError(MAGIC_DEAD);
213 msecond = LocalLock(gbl); /* #2 */
214 ok( msecond == mem, "returned %p with %d (expected '%p')\n",
215 msecond, GetLastError(), mem);
216 SetLastError(MAGIC_DEAD);
217 flags = LocalFlags(gbl);
218 ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
219 flags, GetLastError());
220 SetLastError(MAGIC_DEAD);
222 SetLastError(MAGIC_DEAD);
223 res = LocalUnlock(gbl); /* #1 */
224 ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
225 SetLastError(MAGIC_DEAD);
226 flags = LocalFlags(gbl);
227 ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
228 flags, GetLastError());
230 SetLastError(MAGIC_DEAD);
231 res = LocalUnlock(gbl); /* #0 */
232 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
233 ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
234 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
235 "MAGIC_DEAD)\n", res, GetLastError());
236 SetLastError(MAGIC_DEAD);
237 flags = LocalFlags(gbl);
238 ok( !flags , "returned 0x%04x with %d (expected '0')\n",
239 flags, GetLastError());
241 /* Unlock an already unlocked Handle */
242 SetLastError(MAGIC_DEAD);
243 res = LocalUnlock(gbl);
244 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
245 ok( !res &&
246 ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
247 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
248 "MAGIC_DEAD)\n", res, GetLastError());
250 LocalFree(gbl);
251 /* invalid handles are caught in windows: */
252 SetLastError(MAGIC_DEAD);
253 hsecond = LocalFree(gbl); /* invalid handle: free memory twice */
254 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
255 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
256 hsecond, GetLastError(), gbl);
257 SetLastError(MAGIC_DEAD);
258 flags = LocalFlags(gbl);
259 ok( (flags == LMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
260 "returned 0x%04x with 0x%08x (expected LMEM_INVALID_HANDLE with "
261 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
262 SetLastError(MAGIC_DEAD);
263 size = LocalSize(gbl);
264 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
265 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
266 size, GetLastError());
268 SetLastError(MAGIC_DEAD);
269 mem = LocalLock(gbl);
270 ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
271 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
272 mem, GetLastError());
274 /* This Test works the same on all Systems (GlobalUnlock() is different) */
275 SetLastError(MAGIC_DEAD);
276 res = LocalUnlock(gbl);
277 ok(!res && (GetLastError() == ERROR_INVALID_HANDLE),
278 "returned %d with %d (expected '0' with ERROR_INVALID_HANDLE)\n",
279 res, GetLastError());
281 /* trying to lock empty memory should give an error */
282 gbl = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,0);
283 ok(gbl != NULL, "returned NULL\n");
284 SetLastError(MAGIC_DEAD);
285 mem = GlobalLock(gbl);
286 /* NT: ERROR_DISCARDED, 9x: untouched */
287 ok( (mem == NULL) &&
288 ((GetLastError() == ERROR_DISCARDED) || (GetLastError() == MAGIC_DEAD)),
289 "returned %p with 0x%x/%d (expected 'NULL' with: ERROR_DISCARDED or "
290 "MAGIC_DEAD)\n", mem, GetLastError(), GetLastError());
292 GlobalFree(gbl);