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 /* Global*() functions */
65 gbl = GlobalAlloc(GMEM_MOVEABLE, 0);
66 ok(gbl != NULL, "global memory not allocated for size 0\n");
68 gbl = GlobalReAlloc(gbl, 10, GMEM_MOVEABLE);
69 ok(gbl != NULL, "Can't realloc global memory\n");
70 size = GlobalSize(gbl);
71 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
73 gbl = GlobalReAlloc(gbl, 0, GMEM_MOVEABLE);
74 ok(gbl != NULL, "GlobalReAlloc should not fail on size 0\n");
76 size = GlobalSize(gbl);
77 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
78 ok(GlobalFree(gbl) == NULL, "Memory not freed\n");
79 size = GlobalSize(gbl);
80 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
82 gbl = GlobalReAlloc(0, 10, GMEM_MOVEABLE);
83 ok(gbl == NULL, "global realloc allocated memory\n");
85 /* GlobalLock / GlobalUnlock with a valid handle */
86 gbl = GlobalAlloc(GMEM_MOVEABLE, 256);
88 SetLastError(MAGIC_DEAD);
89 mem = GlobalLock(gbl); /* #1 */
90 ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
91 SetLastError(MAGIC_DEAD);
92 flags = GlobalFlags(gbl);
93 ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
94 flags, GetLastError());
96 SetLastError(MAGIC_DEAD);
97 msecond = GlobalLock(gbl); /* #2 */
98 ok( msecond == mem, "returned %p with %d (expected '%p')\n",
99 msecond, GetLastError(), mem);
100 SetLastError(MAGIC_DEAD);
101 flags = GlobalFlags(gbl);
102 ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
103 flags, GetLastError());
104 SetLastError(MAGIC_DEAD);
106 SetLastError(MAGIC_DEAD);
107 res = GlobalUnlock(gbl); /* #1 */
108 ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
109 SetLastError(MAGIC_DEAD);
110 flags = GlobalFlags(gbl);
111 ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
112 flags, GetLastError());
114 SetLastError(MAGIC_DEAD);
115 res = GlobalUnlock(gbl); /* #0 */
116 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
117 ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
118 "returned %d with %d (expected '0' with: ERROR_SUCCESS or " \
119 "MAGIC_DEAD)\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 /* Unlock an already unlocked Handle */
126 SetLastError(MAGIC_DEAD);
127 res = GlobalUnlock(gbl);
128 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
130 ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
131 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or " \
132 "MAGIC_DEAD)\n", res, GetLastError());
135 /* invalid handles are catched in windows: */
136 SetLastError(MAGIC_DEAD);
137 hsecond = GlobalFree(gbl); /* invalid handle: free memory twice */
138 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
139 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
140 hsecond, GetLastError(), gbl);
141 SetLastError(MAGIC_DEAD);
142 flags = GlobalFlags(gbl);
143 ok( (flags == GMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
144 "returned 0x%04x with 0x%08x (expected GMEM_INVALID_HANDLE with " \
145 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
146 SetLastError(MAGIC_DEAD);
147 size = GlobalSize(gbl);
148 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
149 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
150 size, GetLastError());
152 SetLastError(MAGIC_DEAD);
153 mem = GlobalLock(gbl);
154 ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
155 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
156 mem, GetLastError());
158 /* documented on MSDN: GlobalUnlock() return FALSE on failure.
159 Win9x and wine return FALSE with ERROR_INVALID_HANDLE, but on
160 NT 3.51 and XPsp2, TRUE with ERROR_INVALID_HANDLE is returned.
161 The similar Test for LocalUnlock() works on all Systems */
162 SetLastError(MAGIC_DEAD);
163 res = GlobalUnlock(gbl);
164 ok(GetLastError() == ERROR_INVALID_HANDLE,
165 "returned %d with %d (expected ERROR_INVALID_HANDLE)\n",
166 res, GetLastError());
169 /* ####################################### */
170 /* Local*() functions */
171 gbl = LocalAlloc(LMEM_MOVEABLE, 0);
172 ok(gbl != NULL, "local memory not allocated for size 0\n");
174 gbl = LocalReAlloc(gbl, 10, LMEM_MOVEABLE);
175 ok(gbl != NULL, "Can't realloc local memory\n");
176 size = LocalSize(gbl);
177 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
179 gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE);
180 ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n");
182 size = LocalSize(gbl);
183 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
184 ok(LocalFree(gbl) == NULL, "Memory not freed\n");
185 size = LocalSize(gbl);
186 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
188 gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE);
189 ok(gbl == NULL, "local realloc allocated memory\n");
191 /* LocalLock / LocalUnlock with a valid handle */
192 gbl = LocalAlloc(LMEM_MOVEABLE, 256);
193 SetLastError(MAGIC_DEAD);
194 mem = LocalLock(gbl); /* #1 */
195 ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
196 SetLastError(MAGIC_DEAD);
197 flags = LocalFlags(gbl);
198 ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
199 flags, GetLastError());
201 SetLastError(MAGIC_DEAD);
202 msecond = LocalLock(gbl); /* #2 */
203 ok( msecond == mem, "returned %p with %d (expected '%p')\n",
204 msecond, GetLastError(), mem);
205 SetLastError(MAGIC_DEAD);
206 flags = LocalFlags(gbl);
207 ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
208 flags, GetLastError());
209 SetLastError(MAGIC_DEAD);
211 SetLastError(MAGIC_DEAD);
212 res = LocalUnlock(gbl); /* #1 */
213 ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
214 SetLastError(MAGIC_DEAD);
215 flags = LocalFlags(gbl);
216 ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
217 flags, GetLastError());
219 SetLastError(MAGIC_DEAD);
220 res = LocalUnlock(gbl); /* #0 */
221 /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
222 ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
223 "returned %d with %d (expected '0' with: ERROR_SUCCESS or " \
224 "MAGIC_DEAD)\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 /* Unlock an already unlocked Handle */
231 SetLastError(MAGIC_DEAD);
232 res = LocalUnlock(gbl);
233 /* NT: ERROR_NOT_LOCKED, 9x: untouched */
235 ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
236 "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or " \
237 "MAGIC_DEAD)\n", res, GetLastError());
240 /* invalid handles are catched in windows: */
241 SetLastError(MAGIC_DEAD);
242 hsecond = LocalFree(gbl); /* invalid handle: free memory twice */
243 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
244 "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
245 hsecond, GetLastError(), gbl);
246 SetLastError(MAGIC_DEAD);
247 flags = LocalFlags(gbl);
248 ok( (flags == LMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
249 "returned 0x%04x with 0x%08x (expected LMEM_INVALID_HANDLE with " \
250 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
251 SetLastError(MAGIC_DEAD);
252 size = LocalSize(gbl);
253 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
254 "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n",
255 size, GetLastError());
257 SetLastError(MAGIC_DEAD);
258 mem = LocalLock(gbl);
259 ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
260 "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
261 mem, GetLastError());
263 /* This Test works the same on all Systems (GlobalUnlock() is different) */
264 SetLastError(MAGIC_DEAD);
265 res = LocalUnlock(gbl);
266 ok(!res && (GetLastError() == ERROR_INVALID_HANDLE),
267 "returned %d with %d (expected '0' with ERROR_INVALID_HANDLE)\n",
268 res, GetLastError());
270 /* trying to lock empty memory should give an error */
271 gbl = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,0);
272 ok(gbl != NULL, "returned NULL\n");
273 SetLastError(MAGIC_DEAD);
274 mem = GlobalLock(gbl);
275 /* NT: ERROR_DISCARDED, 9x: untouched */
277 ((GetLastError() == ERROR_DISCARDED) || (GetLastError() == MAGIC_DEAD)),
278 "returned %p with 0x%x/%d (expected 'NULL' with: ERROR_DISCARDED or " \
279 "MAGIC_DEAD)\n", mem, GetLastError(), GetLastError());