kernel: Fix handling of invalid parameter in GlobalSize().
[wine/wine64.git] / dlls / kernel / tests / heap.c
blob089c5029e758117a82a944279e4ef6a63e4c7486
1 /*
2 * Unit test suite for heap functions
4 * Copyright 2003 Dimitrie O. Paun
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.
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.
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
21 #include <stdarg.h>
22 #include <stdlib.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wine/test.h"
28 #define MAGIC_DEAD 0xdeadbeef
30 static SIZE_T resize_9x(SIZE_T size)
32 DWORD dwSizeAligned = (size + 3) & ~3;
33 return max(dwSizeAligned, 12); /* at least 12 bytes */
36 START_TEST(heap)
38 void *mem;
39 UINT flags;
40 HGLOBAL gbl;
41 HGLOBAL hsecond;
42 SIZE_T size;
44 /* Heap*() functions */
45 mem = HeapAlloc(GetProcessHeap(), 0, 0);
46 ok(mem != NULL, "memory not allocated for size 0\n");
48 mem = HeapReAlloc(GetProcessHeap(), 0, NULL, 10);
49 ok(mem == NULL, "memory allocated by HeapReAlloc\n");
51 for (size = 0; size <= 256; size++)
53 SIZE_T heap_size;
54 mem = HeapAlloc(GetProcessHeap(), 0, size);
55 heap_size = HeapSize(GetProcessHeap(), 0, mem);
56 ok(heap_size == size || heap_size == resize_9x(size),
57 "HeapSize returned %lu instead of %lu or %lu\n", heap_size, size, resize_9x(size));
58 HeapFree(GetProcessHeap(), 0, mem);
61 /* Global*() functions */
62 gbl = GlobalAlloc(GMEM_MOVEABLE, 0);
63 ok(gbl != NULL, "global memory not allocated for size 0\n");
65 gbl = GlobalReAlloc(gbl, 10, GMEM_MOVEABLE);
66 ok(gbl != NULL, "Can't realloc global memory\n");
67 size = GlobalSize(gbl);
68 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
70 gbl = GlobalReAlloc(gbl, 0, GMEM_MOVEABLE);
71 ok(gbl != NULL, "GlobalReAlloc should not fail on size 0\n");
73 size = GlobalSize(gbl);
74 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
75 ok(GlobalFree(gbl) == NULL, "Memory not freed\n");
76 size = GlobalSize(gbl);
77 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
79 gbl = GlobalReAlloc(0, 10, GMEM_MOVEABLE);
80 ok(gbl == NULL, "global realloc allocated memory\n");
82 /* invalid handles are catched in windows */
83 gbl = GlobalAlloc(GMEM_MOVEABLE, 256);
84 GlobalFree(gbl);
85 SetLastError(MAGIC_DEAD);
86 hsecond = GlobalFree(gbl); /* invalid handle: free memory twice */
87 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
88 "returned %p with 0x%08lx (expected %p with ERROR_INVALID_HANDLE)\n",
89 hsecond, GetLastError(), gbl);
90 SetLastError(MAGIC_DEAD);
91 flags = GlobalFlags(gbl);
92 ok( (flags == GMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
93 "returned 0x%04x with 0x%08lx (expected GMEM_INVALID_HANDLE with " \
94 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
95 SetLastError(MAGIC_DEAD);
96 size = GlobalSize(gbl);
97 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
98 "returned %ld with 0x%08lx (expected '0' with ERROR_INVALID_HANDLE)\n",
99 size, GetLastError());
102 /* Local*() functions */
103 gbl = LocalAlloc(LMEM_MOVEABLE, 0);
104 ok(gbl != NULL, "local memory not allocated for size 0\n");
106 gbl = LocalReAlloc(gbl, 10, LMEM_MOVEABLE);
107 ok(gbl != NULL, "Can't realloc local memory\n");
108 size = LocalSize(gbl);
109 ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
111 gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE);
112 ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n");
114 size = LocalSize(gbl);
115 ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
116 ok(LocalFree(gbl) == NULL, "Memory not freed\n");
117 size = LocalSize(gbl);
118 ok(size == 0, "Memory should have been freed, size=%ld\n", size);
120 gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE);
121 ok(gbl == NULL, "local realloc allocated memory\n");
123 /* invalid handles are catched in windows */
124 gbl = LocalAlloc(LMEM_MOVEABLE, 256);
125 LocalFree(gbl);
126 SetLastError(MAGIC_DEAD);
127 hsecond = LocalFree(gbl); /* invalid handle: free memory twice */
128 ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
129 "returned %p with 0x%08lx (expected %p with ERROR_INVALID_HANDLE)\n",
130 hsecond, GetLastError(), gbl);
131 SetLastError(MAGIC_DEAD);
132 flags = LocalFlags(gbl);
133 ok( (flags == LMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
134 "returned 0x%04x with 0x%08lx (expected LMEM_INVALID_HANDLE with " \
135 "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
136 SetLastError(MAGIC_DEAD);
137 size = LocalSize(gbl);
138 ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
139 "returned %ld with 0x%08lx (expected '0' with ERROR_INVALID_HANDLE)\n",
140 size, GetLastError());
143 /* trying to lock empty memory should give an error */
144 gbl = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,0);
145 ok(gbl != NULL, "returned NULL\n");
146 SetLastError(0xdeadbeef);
147 mem = GlobalLock(gbl);
148 ok( GetLastError() == ERROR_DISCARDED, "should return an error\n");
149 ok( mem == NULL, "should return NULL\n");
150 GlobalFree(gbl);