~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/kernel32/tests/heap.c

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  1 /*
  2  * Unit test suite for heap functions
  3  *
  4  * Copyright 2003 Dimitrie O. Paun
  5  * Copyright 2006 Detlef Riekenberg
  6  *
  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.
 11  *
 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.
 16  *
 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
 20  */
 21 
 22 #include <stdarg.h>
 23 #include <stdlib.h>
 24 
 25 #include "windef.h"
 26 #include "winbase.h"
 27 #include "wine/test.h"
 28 
 29 #define MAGIC_DEAD 0xdeadbeef
 30 
 31 static SIZE_T resize_9x(SIZE_T size)
 32 {
 33     DWORD dwSizeAligned = (size + 3) & ~3;
 34     return max(dwSizeAligned, 12); /* at least 12 bytes */
 35 }
 36 
 37 START_TEST(heap)
 38 {
 39     LPVOID  mem;
 40     LPVOID  msecond;
 41     DWORD   res;
 42     UINT    flags;
 43     HGLOBAL gbl;
 44     HGLOBAL hsecond;
 45     SIZE_T  size;
 46 
 47     /* Heap*() functions */
 48     mem = HeapAlloc(GetProcessHeap(), 0, 0);
 49     ok(mem != NULL, "memory not allocated for size 0\n");
 50 
 51     mem = HeapReAlloc(GetProcessHeap(), 0, NULL, 10);
 52     ok(mem == NULL, "memory allocated by HeapReAlloc\n");
 53 
 54     for (size = 0; size <= 256; size++)
 55     {
 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);
 62     }
 63 
 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");
 74 
 75     /* Global*() functions */
 76     gbl = GlobalAlloc(GMEM_MOVEABLE, 0);
 77     ok(gbl != NULL, "global memory not allocated for size 0\n");
 78 
 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);
 83 
 84     gbl = GlobalReAlloc(gbl, 0, GMEM_MOVEABLE);
 85     ok(gbl != NULL, "GlobalReAlloc should not fail on size 0\n");
 86 
 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);
 92 
 93     gbl = GlobalReAlloc(0, 10, GMEM_MOVEABLE);
 94     ok(gbl == NULL, "global realloc allocated memory\n");
 95 
 96     /* GlobalLock / GlobalUnlock with a valid handle */
 97     gbl = GlobalAlloc(GMEM_MOVEABLE, 256);
 98 
 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());
106 
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);
116 
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());
124 
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 '' 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 '')\n",
134         flags, GetLastError());
135 
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 '' with: ERROR_NOT_LOCKED or "
143         "MAGIC_DEAD)\n", res, GetLastError());
144  
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 '' with ERROR_INVALID_HANDLE)\n",
161         size, GetLastError());
162 
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());
168 
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());
178 
179     gbl = GlobalAlloc(GMEM_DDESHARE, 100);
180 
181     /* first free */
182     mem = GlobalFree(gbl);
183     ok(mem == NULL, "Expected NULL, got %p\n", mem);
184 
185     /* invalid free */
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());
192 
193     gbl = GlobalAlloc(GMEM_DDESHARE, 100);
194 
195     res = GlobalUnlock(gbl);
196     ok(res == 1 ||
197        res == 0, /* win9x */
198        "Expected 1 or 0, got %d\n", res);
199 
200     res = GlobalUnlock(gbl);
201     ok(res == 1 ||
202        res == 0, /* win9x */
203        "Expected 1 or 0, got %d\n", res);
204 
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());
212 
213     /* ####################################### */
214     /* Local*() functions */
215     gbl = LocalAlloc(LMEM_MOVEABLE, 0);
216     ok(gbl != NULL, "local memory not allocated for size 0\n");
217 
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);
222 
223     gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE);
224     ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n");
225 
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);
231 
232     gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE);
233     ok(gbl == NULL, "local realloc allocated memory\n");
234 
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());
244 
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);
254 
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());
262 
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 '' 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 '')\n",
272         flags, GetLastError());
273 
274     /* Unlock an already unlocked Handle */
275     SetLastError(MAGIC_DEAD);
276     res = LocalUnlock(gbl);
277     /* NT: ERROR_NOT_LOCKED,  9x: untouched */
278     ok( !res &&
279         ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
280         "returned %d with %d (expected '' with: ERROR_NOT_LOCKED or "
281         "MAGIC_DEAD)\n", res, GetLastError());
282 
283     LocalFree(gbl);
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 '' with ERROR_INVALID_HANDLE)\n",
299         size, GetLastError());
300 
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());
306 
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 '' with ERROR_INVALID_HANDLE)\n",
312         res, GetLastError());
313 
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 */
320     ok( (mem == NULL) &&
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());
324 
325     GlobalFree(gbl);
326 }
327 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.