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
23 #include "wine/test.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
41 /* In addition, these features aren't being tested
43 HEAP_GENERATE_EXCEPTIONS
44 STATUS_ACCESS_VIOLATION (error code from HeapAlloc)
47 static void test_Heap(void)
52 LPVOID mem1
,mem1a
,mem3
;
57 /* Retrieve the page size for this system */
59 GetSystemInfo(&sysInfo
);
60 ok(sysInfo
.dwPageSize
>0,"GetSystemInfo should return a valid page size\n");
62 /* Create a Heap with a minimum and maximum size */
63 /* Note that Windows and Wine seem to behave a bit differently with respect
64 to memory allocation. In Windows, you can't access all the memory
65 specified in the heap (due to overhead), so choosing a reasonable maximum
66 size for the heap was done mostly by trial-and-error on Win2k. It may need
67 more tweaking for otherWindows variants.
69 memchunk
=10*sysInfo
.dwPageSize
;
70 heap
=HeapCreate(0,2*memchunk
,5*memchunk
);
72 /* Check that HeapCreate allocated the right amount of ram */
73 mem1
=HeapAlloc(heap
,0,5*memchunk
+1);
74 ok(mem1
==NULL
,"HeapCreate allocated more Ram than it should have\n");
75 HeapFree(heap
,0,mem1
);
77 /* Check that a normal alloc works */
78 mem1
=HeapAlloc(heap
,0,memchunk
);
79 ok(mem1
!=NULL
,"HeapAlloc failed\n");
81 ok(HeapSize(heap
,0,mem1
)>=memchunk
, "HeapAlloc should return a big enough memory block\n");
84 /* Check that a 'zeroing' alloc works */
85 mem2
=HeapAlloc(heap
,HEAP_ZERO_MEMORY
,memchunk
);
86 ok(mem2
!=NULL
,"HeapAlloc failed\n");
88 ok(HeapSize(heap
,0,mem2
)>=memchunk
,"HeapAlloc should return a big enough memory block\n");
90 for(i
=0;i
<memchunk
;i
++) {
95 ok(!error
,"HeapAlloc should have zeroed out it's allocated memory\n");
98 /* Check that HeapAlloc returns NULL when requested way too much memory */
99 mem3
=HeapAlloc(heap
,0,5*memchunk
);
100 ok(mem3
==NULL
,"HeapAlloc should return NULL\n");
102 ok(HeapFree(heap
,0,mem3
),"HeapFree didn't pass successfully\n");
105 /* Check that HeapRealloc works */
106 mem2a
=HeapReAlloc(heap
,HEAP_ZERO_MEMORY
,mem2
,memchunk
+5*sysInfo
.dwPageSize
);
107 ok(mem2a
!=NULL
,"HeapReAlloc failed\n");
109 ok(HeapSize(heap
,0,mem2a
)>=memchunk
+5*sysInfo
.dwPageSize
,"HeapReAlloc failed\n");
111 for(i
=0;i
<5*sysInfo
.dwPageSize
;i
++) {
112 if(mem2a
[memchunk
+i
]!=0) {
116 ok(!error
,"HeapReAlloc should have zeroed out it's allocated memory\n");
119 /* Check that HeapRealloc honours HEAP_REALLOC_IN_PLACE_ONLY */
121 mem1a
=HeapReAlloc(heap
,HEAP_REALLOC_IN_PLACE_ONLY
,mem1
,memchunk
+sysInfo
.dwPageSize
);
127 ok(mem1a
==NULL
|| error
==0,"HeapReAlloc didn't honour HEAP_REALLOC_IN_PLACE_ONLY\n");
129 /* Check that HeapFree works correctly */
131 ok(HeapFree(heap
,0,mem1a
),"HeapFree failed\n");
133 ok(HeapFree(heap
,0,mem1
),"HeapFree failed\n");
136 ok(HeapFree(heap
,0,mem2a
),"HeapFree failed\n");
138 ok(HeapFree(heap
,0,mem2
),"HeapFree failed\n");
141 /* 0-length buffer */
142 mem1
= HeapAlloc(heap
, 0, 0);
143 ok(mem1
!= NULL
, "Reserved memory\n");
145 dwSize
= HeapSize(heap
, 0, mem1
);
146 /* should work with 0-length buffer */
147 ok(dwSize
< 0xFFFFFFFF, "The size of the 0-length buffer\n");
148 ok(HeapFree(heap
, 0, mem1
), "Freed the 0-length buffer\n");
150 /* Check that HeapDestry works */
151 ok(HeapDestroy(heap
),"HeapDestroy failed\n");
154 /* The following functions don't have tests, because either I don't know how
155 to test them, or they are WinNT only, or require multiple threads.
156 Since the last two issues shouldn't really stop the tests from being
157 written, assume for now that it is all due to the first case
162 /* In addition, these features aren't being tested
166 static void test_Global(void)
169 HGLOBAL mem1
,mem2
,mem2a
,mem2b
;
174 SetLastError(NO_ERROR
);
175 /* Check that a normal alloc works */
176 mem1
=GlobalAlloc(0,memchunk
);
177 ok(mem1
!=NULL
,"GlobalAlloc failed\n");
179 ok(GlobalSize(mem1
)>=memchunk
, "GlobalAlloc should return a big enough memory block\n");
182 /* Check that a 'zeroing' alloc works */
183 mem2
=GlobalAlloc(GMEM_ZEROINIT
,memchunk
);
184 ok(mem2
!=NULL
,"GlobalAlloc failed: error=%d\n",GetLastError());
186 ok(GlobalSize(mem2
)>=memchunk
,"GlobalAlloc should return a big enough memory block\n");
187 mem2ptr
=GlobalLock(mem2
);
188 ok(mem2ptr
==mem2
,"GlobalLock should have returned the same memory as was allocated\n");
191 for(i
=0;i
<memchunk
;i
++) {
196 ok(!error
,"GlobalAlloc should have zeroed out it's allocated memory\n");
199 /* Check that GlobalReAlloc works */
200 /* Check that we can change GMEM_FIXED to GMEM_MOVEABLE */
201 mem2a
=GlobalReAlloc(mem2
,0,GMEM_MODIFY
| GMEM_MOVEABLE
);
204 mem2ptr
=GlobalLock(mem2a
);
205 ok(mem2ptr
!=NULL
&& !GlobalUnlock(mem2a
)&&GetLastError()==NO_ERROR
,
206 "Converting from FIXED to MOVEABLE didn't REALLY work\n");
209 /* Check that ReAllocing memory works as expected */
210 mem2a
=GlobalReAlloc(mem2
,2*memchunk
,GMEM_MOVEABLE
| GMEM_ZEROINIT
);
211 ok(mem2a
!=NULL
,"GlobalReAlloc failed\n");
213 ok(GlobalSize(mem2a
)>=2*memchunk
,"GlobalReAlloc failed\n");
214 mem2ptr
=GlobalLock(mem2a
);
215 ok(mem2ptr
!=NULL
,"GlobalLock Failed\n");
218 for(i
=0;i
<memchunk
;i
++) {
219 if(mem2ptr
[memchunk
+i
]!=0) {
223 ok(!error
,"GlobalReAlloc should have zeroed out it's allocated memory\n");
225 /* Check that GlobalHandle works */
226 mem2b
=GlobalHandle(mem2ptr
);
227 ok(mem2b
==mem2a
,"GlobalHandle didn't return the correct memory handle\n");
229 /* Check that we can't discard locked memory */
230 mem2b
=GlobalDiscard(mem2a
);
232 ok(!GlobalUnlock(mem2a
) && GetLastError()==NO_ERROR
,"GlobalUnlock Failed\n");
237 ok(GlobalFree(mem1
)==NULL
,"GlobalFree failed\n");
240 ok(GlobalFree(mem2a
)==NULL
,"GlobalFree failed\n");
242 ok(GlobalFree(mem2
)==NULL
,"GlobalFree failed\n");
247 /* The following functions don't have tests, because either I don't know how
248 to test them, or they are WinNT only, or require multiple threads.
249 Since the last two issues shouldn't really stop the tests from being
250 written, assume for now that it is all due to the first case
254 /* In addition, these features aren't being tested
258 static void test_Local(void)
261 HLOCAL mem1
,mem2
,mem2a
,mem2b
;
266 /* Check that a normal alloc works */
267 mem1
=LocalAlloc(0,memchunk
);
268 ok(mem1
!=NULL
,"LocalAlloc failed: error=%d\n",GetLastError());
270 ok(LocalSize(mem1
)>=memchunk
, "LocalAlloc should return a big enough memory block\n");
273 /* Check that a 'zeroing' and lock alloc works */
274 mem2
=LocalAlloc(LMEM_ZEROINIT
|LMEM_MOVEABLE
,memchunk
);
275 ok(mem2
!=NULL
,"LocalAlloc failed: error=%d\n",GetLastError());
277 ok(LocalSize(mem2
)>=memchunk
,"LocalAlloc should return a big enough memory block\n");
278 mem2ptr
=LocalLock(mem2
);
279 ok(mem2ptr
!=NULL
,"LocalLock: error=%d\n",GetLastError());
282 for(i
=0;i
<memchunk
;i
++) {
287 ok(!error
,"LocalAlloc should have zeroed out it's allocated memory\n");
289 error
=LocalUnlock(mem2
);
290 ok(error
==0 && GetLastError()==NO_ERROR
,
291 "LocalUnlock Failed: rc=%d err=%d\n",error
,GetLastError());
294 mem2a
=LocalFree(mem2
);
295 ok(mem2a
==NULL
, "LocalFree failed: %p\n",mem2a
);
297 /* Reallocate mem2 as moveable memory */
298 mem2
=LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
,memchunk
);
299 ok(mem2
!=NULL
, "LocalAlloc failed to create moveable memory, error=%d\n",GetLastError());
301 /* Check that ReAllocing memory works as expected */
302 mem2a
=LocalReAlloc(mem2
,2*memchunk
,LMEM_MOVEABLE
| LMEM_ZEROINIT
);
303 ok(mem2a
!=NULL
,"LocalReAlloc failed, error=%d\n",GetLastError());
305 ok(LocalSize(mem2a
)>=2*memchunk
,"LocalReAlloc failed\n");
306 mem2ptr
=LocalLock(mem2a
);
307 ok(mem2ptr
!=NULL
,"LocalLock Failed\n");
310 for(i
=0;i
<memchunk
;i
++) {
311 if(mem2ptr
[memchunk
+i
]!=0) {
315 ok(!error
,"LocalReAlloc should have zeroed out it's allocated memory\n");
316 /* Check that LocalHandle works */
317 mem2b
=LocalHandle(mem2ptr
);
318 ok(mem2b
==mem2a
,"LocalHandle didn't return the correct memory handle\n");
319 /* Check that we can't discard locked memory */
320 mem2b
=LocalDiscard(mem2a
);
321 ok(mem2b
==NULL
,"Discarded memory we shouldn't have\n");
322 SetLastError(NO_ERROR
);
323 ok(!LocalUnlock(mem2a
) && GetLastError()==NO_ERROR
, "LocalUnlock Failed\n");
327 ok(LocalFree(mem1
)==NULL
,"LocalFree failed\n");
330 ok(LocalFree(mem2a
)==NULL
,"LocalFree failed\n");
332 ok(LocalFree(mem2
)==NULL
,"LocalFree failed\n");
336 /* The Virtual* routines are not tested as thoroughly,
337 since I don't really understand how to use them correctly :)
338 The following routines are not tested at all
347 And the only features (flags) being tested are
351 Testing the rest requires using exceptions, which I really don't
354 static void test_Virtual(void)
361 /* Retrieve the page size for this system */
362 sysInfo
.dwPageSize
=0;
363 GetSystemInfo(&sysInfo
);
364 ok(sysInfo
.dwPageSize
>0,"GetSystemInfo should return a valid page size\n");
366 /* Choose a reasonable allocation size */
367 memchunk
=10*sysInfo
.dwPageSize
;
369 /* Check that a normal alloc works */
370 mem1
=VirtualAlloc(NULL
,memchunk
,MEM_COMMIT
,PAGE_READWRITE
);
371 ok(mem1
!=NULL
,"VirtualAlloc failed\n");
373 /* check that memory is initialized to 0 */
375 for(i
=0;i
<memchunk
;i
++) {
380 ok(!error
,"VirtualAlloc did not initialize memory to '0's\n");
381 /* Check that we can read/write to memory */
383 for(i
=0;i
<memchunk
;i
+=100) {
389 ok(!error
,"Virtual memory was not writable\n");
391 ok(VirtualFree(mem1
,0,MEM_RELEASE
),"VirtualFree failed\n");