kernel32/tests: Use BOOL type where appropriate.
[wine.git] / dlls / kernel32 / tests / alloc.c
blob86a31e7023ab30085aecf838872f195e27beb7b3
1 /*
2 * Unit test suite for memory allocation functions.
4 * Copyright 2002 Geoffrey Hausheer
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #include "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
29 /* The following functions don't have tests, because either I don't know how
30 to test them, or they are WinNT only, or require multiple threads.
31 Since the last two issues shouldn't really stop the tests from being
32 written, assume for now that it is all due to the first case
33 HeapCompact
34 HeapLock
35 HeapQueryInformation
36 HeapSetInformation
37 HeapUnlock
38 HeapValidate
39 HeapWalk
41 /* In addition, these features aren't being tested
42 HEAP_NO_SERIALIZE
43 HEAP_GENERATE_EXCEPTIONS
44 STATUS_ACCESS_VIOLATION (error code from HeapAlloc)
47 static void test_Heap(void)
49 SYSTEM_INFO sysInfo;
50 ULONG memchunk;
51 HANDLE heap;
52 LPVOID mem1,mem1a,mem3;
53 UCHAR *mem2,*mem2a;
54 UINT i;
55 BOOL error;
56 DWORD dwSize;
58 /* Retrieve the page size for this system */
59 sysInfo.dwPageSize=0;
60 GetSystemInfo(&sysInfo);
61 ok(sysInfo.dwPageSize>0,"GetSystemInfo should return a valid page size\n");
63 /* Create a Heap with a minimum and maximum size */
64 /* Note that Windows and Wine seem to behave a bit differently with respect
65 to memory allocation. In Windows, you can't access all the memory
66 specified in the heap (due to overhead), so choosing a reasonable maximum
67 size for the heap was done mostly by trial-and-error on Win2k. It may need
68 more tweaking for otherWindows variants.
70 memchunk=10*sysInfo.dwPageSize;
71 heap=HeapCreate(0,2*memchunk,5*memchunk);
73 /* Check that HeapCreate allocated the right amount of ram */
74 mem1=HeapAlloc(heap,0,5*memchunk+1);
75 ok(mem1==NULL,"HeapCreate allocated more Ram than it should have\n");
76 HeapFree(heap,0,mem1);
78 /* Check that a normal alloc works */
79 mem1=HeapAlloc(heap,0,memchunk);
80 ok(mem1!=NULL,"HeapAlloc failed\n");
81 if(mem1) {
82 ok(HeapSize(heap,0,mem1)>=memchunk, "HeapAlloc should return a big enough memory block\n");
85 /* Check that a 'zeroing' alloc works */
86 mem2=HeapAlloc(heap,HEAP_ZERO_MEMORY,memchunk);
87 ok(mem2!=NULL,"HeapAlloc failed\n");
88 if(mem2) {
89 ok(HeapSize(heap,0,mem2)>=memchunk,"HeapAlloc should return a big enough memory block\n");
90 error=FALSE;
91 for(i=0;i<memchunk;i++) {
92 if(mem2[i]!=0) {
93 error=TRUE;
96 ok(!error,"HeapAlloc should have zeroed out it's allocated memory\n");
99 /* Check that HeapAlloc returns NULL when requested way too much memory */
100 mem3=HeapAlloc(heap,0,5*memchunk);
101 ok(mem3==NULL,"HeapAlloc should return NULL\n");
102 if(mem3) {
103 ok(HeapFree(heap,0,mem3),"HeapFree didn't pass successfully\n");
106 /* Check that HeapRealloc works */
107 mem2a=HeapReAlloc(heap,HEAP_ZERO_MEMORY,mem2,memchunk+5*sysInfo.dwPageSize);
108 ok(mem2a!=NULL,"HeapReAlloc failed\n");
109 if(mem2a) {
110 ok(HeapSize(heap,0,mem2a)>=memchunk+5*sysInfo.dwPageSize,"HeapReAlloc failed\n");
111 error=FALSE;
112 for(i=0;i<5*sysInfo.dwPageSize;i++) {
113 if(mem2a[memchunk+i]!=0) {
114 error=TRUE;
117 ok(!error,"HeapReAlloc should have zeroed out it's allocated memory\n");
120 /* Check that HeapRealloc honours HEAP_REALLOC_IN_PLACE_ONLY */
121 error=FALSE;
122 mem1a=HeapReAlloc(heap,HEAP_REALLOC_IN_PLACE_ONLY,mem1,memchunk+sysInfo.dwPageSize);
123 if(mem1a!=NULL) {
124 if(mem1a!=mem1) {
125 error=TRUE;
128 ok(mem1a==NULL || !error,"HeapReAlloc didn't honour HEAP_REALLOC_IN_PLACE_ONLY\n");
130 /* Check that HeapFree works correctly */
131 if(mem1a) {
132 ok(HeapFree(heap,0,mem1a),"HeapFree failed\n");
133 } else {
134 ok(HeapFree(heap,0,mem1),"HeapFree failed\n");
136 if(mem2a) {
137 ok(HeapFree(heap,0,mem2a),"HeapFree failed\n");
138 } else {
139 ok(HeapFree(heap,0,mem2),"HeapFree failed\n");
142 /* 0-length buffer */
143 mem1 = HeapAlloc(heap, 0, 0);
144 ok(mem1 != NULL, "Reserved memory\n");
146 dwSize = HeapSize(heap, 0, mem1);
147 /* should work with 0-length buffer */
148 ok(dwSize < 0xFFFFFFFF, "The size of the 0-length buffer\n");
149 ok(HeapFree(heap, 0, mem1), "Freed the 0-length buffer\n");
151 /* Check that HeapDestry works */
152 ok(HeapDestroy(heap),"HeapDestroy failed\n");
155 /* The following functions don't have tests, because either I don't know how
156 to test them, or they are WinNT only, or require multiple threads.
157 Since the last two issues shouldn't really stop the tests from being
158 written, assume for now that it is all due to the first case
159 GlobalFlags
160 GlobalMemoryStatus
161 GlobalMemoryStatusEx
163 /* In addition, these features aren't being tested
164 GMEM_DISCARDABLE
165 GMEM_NOCOMPACT
167 static void test_Global(void)
169 ULONG memchunk;
170 HGLOBAL mem1,mem2,mem2a,mem2b;
171 UCHAR *mem2ptr;
172 UINT i;
173 BOOL error;
174 memchunk=100000;
176 SetLastError(NO_ERROR);
177 /* Check that a normal alloc works */
178 mem1=GlobalAlloc(0,memchunk);
179 ok(mem1!=NULL,"GlobalAlloc failed\n");
180 if(mem1) {
181 ok(GlobalSize(mem1)>=memchunk, "GlobalAlloc should return a big enough memory block\n");
184 /* Check that a 'zeroing' alloc works */
185 mem2=GlobalAlloc(GMEM_ZEROINIT,memchunk);
186 ok(mem2!=NULL,"GlobalAlloc failed: error=%d\n",GetLastError());
187 if(mem2) {
188 ok(GlobalSize(mem2)>=memchunk,"GlobalAlloc should return a big enough memory block\n");
189 mem2ptr=GlobalLock(mem2);
190 ok(mem2ptr==mem2,"GlobalLock should have returned the same memory as was allocated\n");
191 if(mem2ptr) {
192 error=FALSE;
193 for(i=0;i<memchunk;i++) {
194 if(mem2ptr[i]!=0) {
195 error=TRUE;
198 ok(!error,"GlobalAlloc should have zeroed out it's allocated memory\n");
201 /* Check that GlobalReAlloc works */
202 /* Check that we can change GMEM_FIXED to GMEM_MOVEABLE */
203 mem2a=GlobalReAlloc(mem2,0,GMEM_MODIFY | GMEM_MOVEABLE);
204 if(mem2a!=NULL) {
205 mem2=mem2a;
206 mem2ptr=GlobalLock(mem2a);
207 ok(mem2ptr!=NULL && !GlobalUnlock(mem2a)&&GetLastError()==NO_ERROR,
208 "Converting from FIXED to MOVEABLE didn't REALLY work\n");
211 /* Check that ReAllocing memory works as expected */
212 mem2a=GlobalReAlloc(mem2,2*memchunk,GMEM_MOVEABLE | GMEM_ZEROINIT);
213 ok(mem2a!=NULL,"GlobalReAlloc failed\n");
214 if(mem2a) {
215 ok(GlobalSize(mem2a)>=2*memchunk,"GlobalReAlloc failed\n");
216 mem2ptr=GlobalLock(mem2a);
217 ok(mem2ptr!=NULL,"GlobalLock Failed\n");
218 if(mem2ptr) {
219 error=FALSE;
220 for(i=0;i<memchunk;i++) {
221 if(mem2ptr[memchunk+i]!=0) {
222 error=TRUE;
225 ok(!error,"GlobalReAlloc should have zeroed out it's allocated memory\n");
227 /* Check that GlobalHandle works */
228 mem2b=GlobalHandle(mem2ptr);
229 ok(mem2b==mem2a,"GlobalHandle didn't return the correct memory handle\n");
231 /* Check that we can't discard locked memory */
232 mem2b=GlobalDiscard(mem2a);
233 if(mem2b==NULL) {
234 ok(!GlobalUnlock(mem2a) && GetLastError()==NO_ERROR,"GlobalUnlock Failed\n");
238 if(mem1) {
239 ok(GlobalFree(mem1)==NULL,"GlobalFree failed\n");
241 if(mem2a) {
242 ok(GlobalFree(mem2a)==NULL,"GlobalFree failed\n");
243 } else {
244 ok(GlobalFree(mem2)==NULL,"GlobalFree failed\n");
249 /* The following functions don't have tests, because either I don't know how
250 to test them, or they are WinNT only, or require multiple threads.
251 Since the last two issues shouldn't really stop the tests from being
252 written, assume for now that it is all due to the first case
253 LocalDiscard
254 LocalFlags
256 /* In addition, these features aren't being tested
257 LMEM_DISCARDABLE
258 LMEM_NOCOMPACT
260 static void test_Local(void)
262 ULONG memchunk;
263 HLOCAL mem1,mem2,mem2a,mem2b;
264 UCHAR *mem2ptr;
265 UINT i;
266 BOOL error;
267 memchunk=100000;
269 /* Check that a normal alloc works */
270 mem1=LocalAlloc(0,memchunk);
271 ok(mem1!=NULL,"LocalAlloc failed: error=%d\n",GetLastError());
272 if(mem1) {
273 ok(LocalSize(mem1)>=memchunk, "LocalAlloc should return a big enough memory block\n");
276 /* Check that a 'zeroing' and lock alloc works */
277 mem2=LocalAlloc(LMEM_ZEROINIT|LMEM_MOVEABLE,memchunk);
278 ok(mem2!=NULL,"LocalAlloc failed: error=%d\n",GetLastError());
279 if(mem2) {
280 ok(LocalSize(mem2)>=memchunk,"LocalAlloc should return a big enough memory block\n");
281 mem2ptr=LocalLock(mem2);
282 ok(mem2ptr!=NULL,"LocalLock: error=%d\n",GetLastError());
283 if(mem2ptr) {
284 error=FALSE;
285 for(i=0;i<memchunk;i++) {
286 if(mem2ptr[i]!=0) {
287 error=TRUE;
290 ok(!error,"LocalAlloc should have zeroed out it's allocated memory\n");
291 SetLastError(0);
292 error=LocalUnlock(mem2);
293 ok(!error && GetLastError()==NO_ERROR,
294 "LocalUnlock Failed: rc=%d err=%d\n",error,GetLastError());
297 mem2a=LocalFree(mem2);
298 ok(mem2a==NULL, "LocalFree failed: %p\n",mem2a);
300 /* Reallocate mem2 as moveable memory */
301 mem2=LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT,memchunk);
302 ok(mem2!=NULL, "LocalAlloc failed to create moveable memory, error=%d\n",GetLastError());
304 /* Check that ReAllocing memory works as expected */
305 mem2a=LocalReAlloc(mem2,2*memchunk,LMEM_MOVEABLE | LMEM_ZEROINIT);
306 ok(mem2a!=NULL,"LocalReAlloc failed, error=%d\n",GetLastError());
307 if(mem2a) {
308 ok(LocalSize(mem2a)>=2*memchunk,"LocalReAlloc failed\n");
309 mem2ptr=LocalLock(mem2a);
310 ok(mem2ptr!=NULL,"LocalLock Failed\n");
311 if(mem2ptr) {
312 error=FALSE;
313 for(i=0;i<memchunk;i++) {
314 if(mem2ptr[memchunk+i]!=0) {
315 error=TRUE;
318 ok(!error,"LocalReAlloc should have zeroed out it's allocated memory\n");
319 /* Check that LocalHandle works */
320 mem2b=LocalHandle(mem2ptr);
321 ok(mem2b==mem2a,"LocalHandle didn't return the correct memory handle\n");
322 /* Check that we can't discard locked memory */
323 mem2b=LocalDiscard(mem2a);
324 ok(mem2b==NULL,"Discarded memory we shouldn't have\n");
325 SetLastError(NO_ERROR);
326 ok(!LocalUnlock(mem2a) && GetLastError()==NO_ERROR, "LocalUnlock Failed\n");
329 if(mem1) {
330 ok(LocalFree(mem1)==NULL,"LocalFree failed\n");
332 if(mem2a) {
333 ok(LocalFree(mem2a)==NULL,"LocalFree failed\n");
334 } else {
335 ok(LocalFree(mem2)==NULL,"LocalFree failed\n");
339 /* The Virtual* routines are not tested as thoroughly,
340 since I don't really understand how to use them correctly :)
341 The following routines are not tested at all
342 VirtualAllocEx
343 VirtualFreeEx
344 VirtualLock
345 VirtualProtect
346 VirtualProtectEx
347 VirtualQuery
348 VirtualQueryEx
349 VirtualUnlock
350 And the only features (flags) being tested are
351 MEM_COMMIT
352 MEM_RELEASE
353 PAGE_READWRITE
354 Testing the rest requires using exceptions, which I really don't
355 understand well
357 static void test_Virtual(void)
359 SYSTEM_INFO sysInfo;
360 ULONG memchunk;
361 UCHAR *mem1;
362 UINT i;
363 BOOL error;
365 /* Retrieve the page size for this system */
366 sysInfo.dwPageSize=0;
367 GetSystemInfo(&sysInfo);
368 ok(sysInfo.dwPageSize>0,"GetSystemInfo should return a valid page size\n");
370 /* Choose a reasonable allocation size */
371 memchunk=10*sysInfo.dwPageSize;
373 /* Check that a normal alloc works */
374 mem1=VirtualAlloc(NULL,memchunk,MEM_COMMIT,PAGE_READWRITE);
375 ok(mem1!=NULL,"VirtualAlloc failed\n");
376 if(mem1) {
377 /* check that memory is initialized to 0 */
378 error=FALSE;
379 for(i=0;i<memchunk;i++) {
380 if(mem1[i]!=0) {
381 error=TRUE;
384 ok(!error,"VirtualAlloc did not initialize memory to '0's\n");
385 /* Check that we can read/write to memory */
386 error=FALSE;
387 for(i=0;i<memchunk;i+=100) {
388 mem1[i]='a';
389 if(mem1[i]!='a') {
390 error=TRUE;
393 ok(!error,"Virtual memory was not writable\n");
395 ok(VirtualFree(mem1,0,MEM_RELEASE),"VirtualFree failed\n");
397 START_TEST(alloc)
399 test_Heap();
400 test_Global();
401 test_Local();
402 test_Virtual();