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 /* 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 */
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());
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());
179 gbl
= GlobalAlloc(GMEM_DDESHARE
, 100);
182 mem
= GlobalFree(gbl
);
183 ok(mem
== NULL
, "Expected NULL, got %p\n", mem
);
186 SetLastError(MAGIC_DEAD
);
187 mem
= GlobalFree(gbl
);
188 ok(mem
== gbl
, "Expected gbl, got %p\n", mem
);
189 ok(GetLastError() == ERROR_INVALID_HANDLE
||
190 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
191 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
193 gbl
= GlobalAlloc(GMEM_DDESHARE
, 100);
195 res
= GlobalUnlock(gbl
);
197 res
== 0, /* win9x */
198 "Expected 1 or 0, got %d\n", res
);
200 res
= GlobalUnlock(gbl
);
202 res
== 0, /* win9x */
203 "Expected 1 or 0, got %d\n", res
);
205 /* GlobalSize on an invalid handle */
206 SetLastError(MAGIC_DEAD
);
207 size
= GlobalSize((HGLOBAL
)0xc042);
208 ok(size
== 0, "Expected 0, got %ld\n", size
);
209 ok(GetLastError() == ERROR_INVALID_HANDLE
||
210 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
211 "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
213 /* ####################################### */
214 /* Local*() functions */
215 gbl
= LocalAlloc(LMEM_MOVEABLE
, 0);
216 ok(gbl
!= NULL
, "local memory not allocated for size 0\n");
218 gbl
= LocalReAlloc(gbl
, 10, LMEM_MOVEABLE
);
219 ok(gbl
!= NULL
, "Can't realloc local memory\n");
220 size
= LocalSize(gbl
);
221 ok(size
>= 10 && size
<= 16, "Memory not resized to size 10, instead size=%ld\n", size
);
223 gbl
= LocalReAlloc(gbl
, 0, LMEM_MOVEABLE
);
224 ok(gbl
!= NULL
, "LocalReAlloc should not fail on size 0\n");
226 size
= LocalSize(gbl
);
227 ok(size
== 0, "Memory not resized to size 0, instead size=%ld\n", size
);
228 ok(LocalFree(gbl
) == NULL
, "Memory not freed\n");
229 size
= LocalSize(gbl
);
230 ok(size
== 0, "Memory should have been freed, size=%ld\n", size
);
232 gbl
= LocalReAlloc(0, 10, LMEM_MOVEABLE
);
233 ok(gbl
== NULL
, "local realloc allocated memory\n");
235 /* LocalLock / LocalUnlock with a valid handle */
236 gbl
= LocalAlloc(LMEM_MOVEABLE
, 256);
237 SetLastError(MAGIC_DEAD
);
238 mem
= LocalLock(gbl
); /* #1 */
239 ok(mem
!= NULL
, "returned %p with %d (expected '!= NULL')\n", mem
, GetLastError());
240 SetLastError(MAGIC_DEAD
);
241 flags
= LocalFlags(gbl
);
242 ok( flags
== 1, "returned 0x%04x with %d (expected '0x0001')\n",
243 flags
, GetLastError());
245 SetLastError(MAGIC_DEAD
);
246 msecond
= LocalLock(gbl
); /* #2 */
247 ok( msecond
== mem
, "returned %p with %d (expected '%p')\n",
248 msecond
, GetLastError(), mem
);
249 SetLastError(MAGIC_DEAD
);
250 flags
= LocalFlags(gbl
);
251 ok( flags
== 2, "returned 0x%04x with %d (expected '0x0002')\n",
252 flags
, GetLastError());
253 SetLastError(MAGIC_DEAD
);
255 SetLastError(MAGIC_DEAD
);
256 res
= LocalUnlock(gbl
); /* #1 */
257 ok(res
, "returned %d with %d (expected '!= 0')\n", res
, GetLastError());
258 SetLastError(MAGIC_DEAD
);
259 flags
= LocalFlags(gbl
);
260 ok( flags
, "returned 0x%04x with %d (expected '!= 0')\n",
261 flags
, GetLastError());
263 SetLastError(MAGIC_DEAD
);
264 res
= LocalUnlock(gbl
); /* #0 */
265 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
266 ok(!res
&& ((GetLastError() == ERROR_SUCCESS
) || (GetLastError() == MAGIC_DEAD
)),
267 "returned %d with %d (expected '0' with: ERROR_SUCCESS or "
268 "MAGIC_DEAD)\n", res
, GetLastError());
269 SetLastError(MAGIC_DEAD
);
270 flags
= LocalFlags(gbl
);
271 ok( !flags
, "returned 0x%04x with %d (expected '0')\n",
272 flags
, GetLastError());
274 /* Unlock an already unlocked Handle */
275 SetLastError(MAGIC_DEAD
);
276 res
= LocalUnlock(gbl
);
277 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
279 ((GetLastError() == ERROR_NOT_LOCKED
) || (GetLastError() == MAGIC_DEAD
)),
280 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or "
281 "MAGIC_DEAD)\n", res
, GetLastError());
284 /* invalid handles are caught in windows: */
285 SetLastError(MAGIC_DEAD
);
286 hsecond
= LocalFree(gbl
); /* invalid handle: free memory twice */
287 ok( (hsecond
== gbl
) && (GetLastError() == ERROR_INVALID_HANDLE
),
288 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
289 hsecond
, GetLastError(), gbl
);
290 SetLastError(MAGIC_DEAD
);
291 flags
= LocalFlags(gbl
);
292 ok( (flags
== LMEM_INVALID_HANDLE
) && (GetLastError() == ERROR_INVALID_HANDLE
),
293 "returned 0x%04x with 0x%08x (expected LMEM_INVALID_HANDLE with "
294 "ERROR_INVALID_HANDLE)\n", flags
, GetLastError());
295 SetLastError(MAGIC_DEAD
);
296 size
= LocalSize(gbl
);
297 ok( (size
== 0) && (GetLastError() == ERROR_INVALID_HANDLE
),
298 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
299 size
, GetLastError());
301 SetLastError(MAGIC_DEAD
);
302 mem
= LocalLock(gbl
);
303 ok( (mem
== NULL
) && (GetLastError() == ERROR_INVALID_HANDLE
),
304 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
305 mem
, GetLastError());
307 /* This Test works the same on all Systems (GlobalUnlock() is different) */
308 SetLastError(MAGIC_DEAD
);
309 res
= LocalUnlock(gbl
);
310 ok(!res
&& (GetLastError() == ERROR_INVALID_HANDLE
),
311 "returned %d with %d (expected '0' with ERROR_INVALID_HANDLE)\n",
312 res
, GetLastError());
314 /* trying to lock empty memory should give an error */
315 gbl
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_ZEROINIT
,0);
316 ok(gbl
!= NULL
, "returned NULL\n");
317 SetLastError(MAGIC_DEAD
);
318 mem
= GlobalLock(gbl
);
319 /* NT: ERROR_DISCARDED, 9x: untouched */
321 ((GetLastError() == ERROR_DISCARDED
) || (GetLastError() == MAGIC_DEAD
)),
322 "returned %p with 0x%x/%d (expected 'NULL' with: ERROR_DISCARDED or "
323 "MAGIC_DEAD)\n", mem
, GetLastError(), GetLastError());