[NTDLL_APITEST]
[reactos.git] / rostests / apitests / ntdll / RtlReAllocateHeap.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test for RtlReAllocateHeap
5 * PROGRAMMERS: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8 #include <apitest.h>
9 #include <ndk/rtlfuncs.h>
10
11 static
12 BOOLEAN
13 CheckBuffer(
14 PVOID Buffer,
15 SIZE_T Size,
16 UCHAR Value)
17 {
18 PUCHAR Array = Buffer;
19 SIZE_T i;
20
21 for (i = 0; i < Size; i++)
22 if (Array[i] != Value)
23 {
24 trace("Expected %x, found %x at offset %lu\n", Value, Array[i], (ULONG)i);
25 return FALSE;
26 }
27 return TRUE;
28 }
29
30 static
31 BOOLEAN
32 ReAllocBuffer(
33 PUCHAR *Buffer,
34 SIZE_T Size,
35 SIZE_T *OldSizePtr,
36 PCSTR Action)
37 {
38 PUCHAR NewBuffer;
39 SIZE_T OldSize = *OldSizePtr;
40
41 RtlFillMemory(*Buffer, OldSize, 0x7a);
42 NewBuffer = RtlReAllocateHeap(RtlGetProcessHeap(),
43 HEAP_ZERO_MEMORY,
44 *Buffer,
45 Size);
46 if (!NewBuffer)
47 {
48 skip("RtlReAllocateHeap failed for size %lu (%s)\n", Size, Action);
49 return FALSE;
50 }
51 *Buffer = NewBuffer;
52 ok_hex(RtlSizeHeap(RtlGetProcessHeap(), 0, NewBuffer), Size);
53 if (OldSize < Size)
54 {
55 ok(CheckBuffer(NewBuffer, OldSize, 0x7a), "CheckBuffer failed at size 0x%lx -> 0x%lx\n", OldSize, Size);
56 ok(CheckBuffer(NewBuffer + OldSize, Size - OldSize, 0), "HEAP_ZERO_MEMORY not respected for 0x%lx -> 0x%lx\n", OldSize, Size);
57 }
58 else
59 {
60 ok(CheckBuffer(NewBuffer, Size, 0x7a), "CheckBuffer failed at size 0x%lx -> 0x%lx\n", OldSize, Size);
61 }
62 *OldSizePtr = Size;
63 return TRUE;
64 }
65
66 START_TEST(RtlReAllocateHeap)
67 {
68 PUCHAR Buffer = NULL;
69 SIZE_T OldSize = 0;
70 SIZE_T Size;
71 BOOLEAN Continue = TRUE;
72
73 OldSize = 0x100;
74 Buffer = RtlReAllocateHeap(RtlGetProcessHeap(),
75 HEAP_ZERO_MEMORY,
76 NULL,
77 OldSize);
78 ok(Buffer == NULL, "RtlReAllocateHeap succeeded for NULL\n");
79 if (Buffer)
80 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
81
82 Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
83 HEAP_ZERO_MEMORY,
84 OldSize);
85 if (!Buffer)
86 {
87 skip("RtlAllocateHeap failed for size %lu\n", OldSize);
88 return;
89 }
90 ok(CheckBuffer(Buffer, OldSize, 0), "HEAP_ZERO_MEMORY not respected for 0x%lx\n", OldSize);
91
92 for (Size = 0x78000; Size < 0x90000 && Continue; Size += 0x100)
93 {
94 Continue = ReAllocBuffer(&Buffer, Size, &OldSize, "growing");
95 }
96
97 /* and back again */
98 for (Size -= 0x100; Size >= 0x78000 && Continue; Size -= 0x100)
99 {
100 Continue = ReAllocBuffer(&Buffer, Size, &OldSize, "shrinking");
101 }
102 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
103 }