1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=============================================================
6**
7** Source: test2.c
8**
9** Purpose: Allocate some memory. Then reallocate that memory into less
10** space than the original amount. Ensure the
11** return values are correct, and also that data placed in the allocated
12** memory carries over to the reallocated block.
13**
14**
15**============================================================*/
16
17#include <palsuite.h>
18
19int __cdecl main(int argc, char *argv[])
20{
21
22 HANDLE TheHeap;
23 char* TheMemory;
24 char* ReAllocMemory;
25 int i;
26
27 if(PAL_Initialize(argc, argv) != 0)
28 {
29 return FAIL;
30 }
31
32 TheHeap = GetProcessHeap();
33
34 if(TheHeap == NULL)
35 {
36 Fail("ERROR: GetProcessHeap() returned NULL when it was called. "
37 "GetLastError() returned %d.",GetLastError());
38 }
39
40 /* Allocate 200 bytes on the heap */
41 if((TheMemory = (char*)HeapAlloc(TheHeap, 0, 200)) == NULL)
42 {
43 Fail("ERROR: HeapAlloc returned NULL when it was called. "
44 "GetLastError() returned %d.",GetLastError());
45 }
46
47 /* Set the first 100 bytes to 'X' */
48 memset(TheMemory, 'X', 100);
49
50 /* Set the second 100 bytes to 'Z' */
51 memset(TheMemory+100, 'Z', 100);
52
53 /* Reallocate the memory to 100 bytes */
54 ReAllocMemory = (char*)HeapReAlloc(TheHeap, 0, TheMemory, 100);
55
56 if(ReAllocMemory == NULL)
57 {
58 Fail("ERROR: HeapReAlloc failed to reallocate the 100 bytes of "
59 "heap memory. GetLastError returns %d.",GetLastError());
60 }
61
62 /* Check that each of the first 100 bytes hasn't lost any data.
63 Anything beyond the first 100 might still be valid, but we can't
64 gaurentee it.
65 */
66
67 for(i=0; i<100; ++i)
68 {
69 /* Note: Cast to char* so the function knows the size is 1 */
70 if(ReAllocMemory[i] != 'X')
71 {
72 Fail("ERROR: Byte number %d of the reallocated memory block "
73 "is not set to 'X' as it should be.",i);
74 }
75 }
76
77 PAL_Terminate();
78 return PASS;
79}
80