- Sync up Mm interface with WinLdr branch (introduce the concept of a memory type...
[reactos.git] / rostests / winetests / kernel32 / heap.c
1 /*
2 * Unit test suite for heap functions
3 *
4 * Copyright 2003 Dimitrie O. Paun
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <stdarg.h>
22 #include <stdlib.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wine/test.h"
27
28 static SIZE_T resize_9x(SIZE_T size)
29 {
30 DWORD dwSizeAligned = (size + 3) & ~3;
31 return max(dwSizeAligned, 12); /* at least 12 bytes */
32 }
33
34 START_TEST(heap)
35 {
36 void *mem;
37 HGLOBAL gbl;
38 SIZE_T size;
39
40 /* Heap*() functions */
41 mem = HeapAlloc(GetProcessHeap(), 0, 0);
42 ok(mem != NULL, "memory not allocated for size 0\n");
43
44 mem = HeapReAlloc(GetProcessHeap(), 0, NULL, 10);
45 ok(mem == NULL, "memory allocated by HeapReAlloc\n");
46
47 for (size = 0; size <= 256; size++)
48 {
49 SIZE_T heap_size;
50 mem = HeapAlloc(GetProcessHeap(), 0, size);
51 heap_size = HeapSize(GetProcessHeap(), 0, mem);
52 ok(heap_size == size || heap_size == resize_9x(size),
53 "HeapSize returned %lu instead of %lu or %lu\n", heap_size, size, resize_9x(size));
54 HeapFree(GetProcessHeap(), 0, mem);
55 }
56
57 /* Global*() functions */
58 gbl = GlobalAlloc(GMEM_MOVEABLE, 0);
59 ok(gbl != NULL, "global memory not allocated for size 0\n");
60
61 gbl = GlobalReAlloc(gbl, 10, GMEM_MOVEABLE);
62 ok(gbl != NULL, "Can't realloc global memory\n");
63 size = GlobalSize(gbl);
64 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
65
66 gbl = GlobalReAlloc(gbl, 0, GMEM_MOVEABLE);
67 ok(gbl != NULL, "GlobalReAlloc should not fail on size 0\n");
68
69 size = GlobalSize(gbl);
70 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
71 ok(GlobalFree(gbl) == NULL, "Memory not freed\n");
72 size = GlobalSize(gbl);
73 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
74
75 gbl = GlobalReAlloc(0, 10, GMEM_MOVEABLE);
76 ok(gbl == NULL, "global realloc allocated memory\n");
77
78 /* Local*() functions */
79 gbl = LocalAlloc(LMEM_MOVEABLE, 0);
80 ok(gbl != NULL, "local memory not allocated for size 0\n");
81
82 gbl = LocalReAlloc(gbl, 10, LMEM_MOVEABLE);
83 ok(gbl != NULL, "Can't realloc local memory\n");
84 size = LocalSize(gbl);
85 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
86
87 gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE);
88 ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n");
89
90 size = LocalSize(gbl);
91 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
92 ok(LocalFree(gbl) == NULL, "Memory not freed\n");
93 size = LocalSize(gbl);
94 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
95
96 gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE);
97 ok(gbl == NULL, "local realloc allocated memory\n");
98
99 /* trying to lock empty memory should give an error */
100 gbl = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,0);
101 ok(gbl != NULL, "returned NULL\n");
102 SetLastError(0xdeadbeef);
103 mem = GlobalLock(gbl);
104 ok( GetLastError() == ERROR_DISCARDED, "should return an error\n");
105 ok( mem == NULL, "should return NULL\n");
106 GlobalFree(gbl);
107 }