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
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 */
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
++)
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 /* large blocks must be 16-byte aligned */
76 mem
= HeapAlloc(GetProcessHeap(), 0, 512 * 1024);
77 ok( mem
!= NULL
, "failed for size 512K\n" );
78 ok( (ULONG_PTR
)mem
% 16 == 0 || broken((ULONG_PTR
)mem
% 16) /* win9x */,
79 "512K block not 16-byte aligned\n" );
80 HeapFree(GetProcessHeap(), 0, mem
);
82 /* Global*() functions */
83 gbl
= GlobalAlloc(GMEM_MOVEABLE
, 0);
84 ok(gbl
!= NULL
, "global memory not allocated for size 0\n");
86 gbl
= GlobalReAlloc(gbl
, 10, GMEM_MOVEABLE
);
87 ok(gbl
!= NULL
, "Can't realloc global memory\n");
88 size
= GlobalSize(gbl
);
89 ok(size
>= 10 && size
<= 16, "Memory not resized to size 10, instead size=%ld\n", size
);
91 gbl
= GlobalReAlloc(gbl
, 0, GMEM_MOVEABLE
);
92 ok(gbl
!= NULL
, "GlobalReAlloc should not fail on size 0\n");
94 size
= GlobalSize(gbl
);
95 ok(size
== 0, "Memory not resized to size 0, instead size=%ld\n", size
);
96 ok(GlobalFree(gbl
) == NULL
, "Memory not freed\n");
97 size
= GlobalSize(gbl
);
98 ok(size
== 0, "Memory should have been freed, size=%ld\n", size
);
100 gbl
= GlobalReAlloc(0, 10, GMEM_MOVEABLE
);
101 ok(gbl
== NULL
, "global realloc allocated memory\n");
103 /* GlobalLock / GlobalUnlock with a valid handle */
104 gbl
= GlobalAlloc(GMEM_MOVEABLE
, 256);
106 SetLastError(MAGIC_DEAD
);
107 mem
= GlobalLock(gbl
); /* #1 */
108 ok(mem
!= NULL
, "returned %p with %d (expected '!= NULL')\n", mem
, GetLastError());
109 SetLastError(MAGIC_DEAD
);
110 flags
= GlobalFlags(gbl
);
111 ok( flags
== 1, "returned 0x%04x with %d (expected '0x0001')\n",
112 flags
, GetLastError());
114 SetLastError(MAGIC_DEAD
);
115 msecond
= GlobalLock(gbl
); /* #2 */
116 ok( msecond
== mem
, "returned %p with %d (expected '%p')\n",
117 msecond
, GetLastError(), mem
);
118 SetLastError(MAGIC_DEAD
);
119 flags
= GlobalFlags(gbl
);
120 ok( flags
== 2, "returned 0x%04x with %d (expected '0x0002')\n",
121 flags
, GetLastError());
122 SetLastError(MAGIC_DEAD
);
124 SetLastError(MAGIC_DEAD
);
125 res
= GlobalUnlock(gbl
); /* #1 */
126 ok(res
, "returned %d with %d (expected '!= 0')\n", res
, GetLastError());
127 SetLastError(MAGIC_DEAD
);
128 flags
= GlobalFlags(gbl
);
129 ok( flags
, "returned 0x%04x with %d (expected '!= 0')\n",
130 flags
, GetLastError());
132 SetLastError(MAGIC_DEAD
);
133 res
= GlobalUnlock(gbl
); /* #0 */
134 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
135 ok(!res
&& ((GetLastError() == ERROR_SUCCESS
) || (GetLastError() == MAGIC_DEAD
)),
136 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
137 "MAGIC_DEAD)\n", res
, GetLastError());
138 SetLastError(MAGIC_DEAD
);
139 flags
= GlobalFlags(gbl
);
140 ok( !flags
, "returned 0x%04x with %d (expected '0')\n",
141 flags
, GetLastError());
143 /* Unlock an already unlocked Handle */
144 SetLastError(MAGIC_DEAD
);
145 res
= GlobalUnlock(gbl
);
146 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
148 ((GetLastError() == ERROR_NOT_LOCKED
) || (GetLastError() == MAGIC_DEAD
)),
149 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
150 "MAGIC_DEAD)\n", res
, GetLastError());
153 /* invalid handles are caught in windows: */
154 SetLastError(MAGIC_DEAD
);
155 hsecond
= GlobalFree(gbl
); /* invalid handle: free memory twice */
156 ok( (hsecond
== gbl
) && (GetLastError() == ERROR_INVALID_HANDLE
),
157 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
158 hsecond
, GetLastError(), gbl
);
159 SetLastError(MAGIC_DEAD
);
160 flags
= GlobalFlags(gbl
);
161 ok( (flags
== GMEM_INVALID_HANDLE
) && (GetLastError() == ERROR_INVALID_HANDLE
),
162 "returned 0x%04x with 0x%08x (expected GMEM_INVALID_HANDLE with "
163 "ERROR_INVALID_HANDLE)\n", flags
, GetLastError());
164 SetLastError(MAGIC_DEAD
);
165 size
= GlobalSize(gbl
);
166 ok( (size
== 0) && (GetLastError() == ERROR_INVALID_HANDLE
),
167 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
168 size
, GetLastError());
170 SetLastError(MAGIC_DEAD
);
171 mem
= GlobalLock(gbl
);
172 ok( (mem
== NULL
) && (GetLastError() == ERROR_INVALID_HANDLE
),
173 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
174 mem
, GetLastError());
176 /* documented on MSDN: GlobalUnlock() return FALSE on failure.
177 Win9x and wine return FALSE with ERROR_INVALID_HANDLE, but on
178 NT 3.51 and XPsp2, TRUE with ERROR_INVALID_HANDLE is returned.
179 The similar Test for LocalUnlock() works on all Systems */
180 SetLastError(MAGIC_DEAD
);
181 res
= GlobalUnlock(gbl
);
182 ok(GetLastError() == ERROR_INVALID_HANDLE
,
183 "returned %d with %d (expected ERROR_INVALID_HANDLE)\n",
184 res
, GetLastError());
186 gbl
= GlobalAlloc(GMEM_DDESHARE
, 100);
189 mem
= GlobalFree(gbl
);
190 ok(mem
== NULL
, "Expected NULL, got %p\n", mem
);
193 SetLastError(MAGIC_DEAD
);
194 mem
= GlobalFree(gbl
);
195 ok(mem
== gbl
|| broken(mem
== NULL
) /* nt4 */, "Expected gbl, got %p\n", mem
);
197 ok(GetLastError() == ERROR_INVALID_HANDLE
||
198 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
199 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
201 gbl
= GlobalAlloc(GMEM_DDESHARE
, 100);
203 res
= GlobalUnlock(gbl
);
205 res
== 0, /* win9x */
206 "Expected 1 or 0, got %d\n", res
);
208 res
= GlobalUnlock(gbl
);
210 res
== 0, /* win9x */
211 "Expected 1 or 0, got %d\n", res
);
213 /* GlobalSize on an invalid handle */
214 SetLastError(MAGIC_DEAD
);
215 size
= GlobalSize((HGLOBAL
)0xc042);
216 ok(size
== 0, "Expected 0, got %ld\n", size
);
217 ok(GetLastError() == ERROR_INVALID_HANDLE
||
218 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
219 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
221 /* ####################################### */
222 /* Local*() functions */
223 gbl
= LocalAlloc(LMEM_MOVEABLE
, 0);
224 ok(gbl
!= NULL
, "local memory not allocated for size 0\n");
226 gbl
= LocalReAlloc(gbl
, 10, LMEM_MOVEABLE
);
227 ok(gbl
!= NULL
, "Can't realloc local memory\n");
228 size
= LocalSize(gbl
);
229 ok(size
>= 10 && size
<= 16, "Memory not resized to size 10, instead size=%ld\n", size
);
231 gbl
= LocalReAlloc(gbl
, 0, LMEM_MOVEABLE
);
232 ok(gbl
!= NULL
, "LocalReAlloc should not fail on size 0\n");
234 size
= LocalSize(gbl
);
235 ok(size
== 0, "Memory not resized to size 0, instead size=%ld\n", size
);
236 ok(LocalFree(gbl
) == NULL
, "Memory not freed\n");
237 size
= LocalSize(gbl
);
238 ok(size
== 0, "Memory should have been freed, size=%ld\n", size
);
240 gbl
= LocalReAlloc(0, 10, LMEM_MOVEABLE
);
241 ok(gbl
== NULL
, "local realloc allocated memory\n");
243 /* LocalLock / LocalUnlock with a valid handle */
244 gbl
= LocalAlloc(LMEM_MOVEABLE
, 256);
245 SetLastError(MAGIC_DEAD
);
246 mem
= LocalLock(gbl
); /* #1 */
247 ok(mem
!= NULL
, "returned %p with %d (expected '!= NULL')\n", mem
, GetLastError());
248 SetLastError(MAGIC_DEAD
);
249 flags
= LocalFlags(gbl
);
250 ok( flags
== 1, "returned 0x%04x with %d (expected '0x0001')\n",
251 flags
, GetLastError());
253 SetLastError(MAGIC_DEAD
);
254 msecond
= LocalLock(gbl
); /* #2 */
255 ok( msecond
== mem
, "returned %p with %d (expected '%p')\n",
256 msecond
, GetLastError(), mem
);
257 SetLastError(MAGIC_DEAD
);
258 flags
= LocalFlags(gbl
);
259 ok( flags
== 2, "returned 0x%04x with %d (expected '0x0002')\n",
260 flags
, GetLastError());
261 SetLastError(MAGIC_DEAD
);
263 SetLastError(MAGIC_DEAD
);
264 res
= LocalUnlock(gbl
); /* #1 */
265 ok(res
, "returned %d with %d (expected '!= 0')\n", res
, GetLastError());
266 SetLastError(MAGIC_DEAD
);
267 flags
= LocalFlags(gbl
);
268 ok( flags
, "returned 0x%04x with %d (expected '!= 0')\n",
269 flags
, GetLastError());
271 SetLastError(MAGIC_DEAD
);
272 res
= LocalUnlock(gbl
); /* #0 */
273 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
274 ok(!res
&& ((GetLastError() == ERROR_SUCCESS
) || (GetLastError() == MAGIC_DEAD
)),
275 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
276 "MAGIC_DEAD)\n", res
, GetLastError());
277 SetLastError(MAGIC_DEAD
);
278 flags
= LocalFlags(gbl
);
279 ok( !flags
, "returned 0x%04x with %d (expected '0')\n",
280 flags
, GetLastError());
282 /* Unlock an already unlocked Handle */
283 SetLastError(MAGIC_DEAD
);
284 res
= LocalUnlock(gbl
);
285 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
287 ((GetLastError() == ERROR_NOT_LOCKED
) || (GetLastError() == MAGIC_DEAD
)),
288 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
289 "MAGIC_DEAD)\n", res
, GetLastError());
292 /* invalid handles are caught in windows: */
293 SetLastError(MAGIC_DEAD
);
294 hsecond
= LocalFree(gbl
); /* invalid handle: free memory twice */
295 ok( (hsecond
== gbl
) && (GetLastError() == ERROR_INVALID_HANDLE
),
296 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
297 hsecond
, GetLastError(), gbl
);
298 SetLastError(MAGIC_DEAD
);
299 flags
= LocalFlags(gbl
);
300 ok( (flags
== LMEM_INVALID_HANDLE
) && (GetLastError() == ERROR_INVALID_HANDLE
),
301 "returned 0x%04x with 0x%08x (expected LMEM_INVALID_HANDLE with "
302 "ERROR_INVALID_HANDLE)\n", flags
, GetLastError());
303 SetLastError(MAGIC_DEAD
);
304 size
= LocalSize(gbl
);
305 ok( (size
== 0) && (GetLastError() == ERROR_INVALID_HANDLE
),
306 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
307 size
, GetLastError());
309 SetLastError(MAGIC_DEAD
);
310 mem
= LocalLock(gbl
);
311 ok( (mem
== NULL
) && (GetLastError() == ERROR_INVALID_HANDLE
),
312 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
313 mem
, GetLastError());
315 /* This Test works the same on all Systems (GlobalUnlock() is different) */
316 SetLastError(MAGIC_DEAD
);
317 res
= LocalUnlock(gbl
);
318 ok(!res
&& (GetLastError() == ERROR_INVALID_HANDLE
),
319 "returned %d with %d (expected '0' with ERROR_INVALID_HANDLE)\n",
320 res
, GetLastError());
322 /* trying to lock empty memory should give an error */
323 gbl
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_ZEROINIT
,0);
324 ok(gbl
!= NULL
, "returned NULL\n");
325 SetLastError(MAGIC_DEAD
);
326 mem
= GlobalLock(gbl
);
327 /* NT: ERROR_DISCARDED, 9x: untouched */
329 ((GetLastError() == ERROR_DISCARDED
) || (GetLastError() == MAGIC_DEAD
)),
330 "returned %p with 0x%x/%d (expected 'NULL' with: ERROR_DISCARDED or "
331 "MAGIC_DEAD)\n", mem
, GetLastError(), GetLastError());