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: test1.c
8**
9** Purpose: Uses realloc to allocate and realloate memory, checking
10** that memory contents are copied when the memory is reallocated.
11**
12**
13**==========================================================================*/
14
15#include <palsuite.h>
16
17int __cdecl main(int argc, char **argv)
18{
19 char *testA;
20 const int len1 = 10;
21 const char str1[] = "aaaaaaaaaa";
22
23 const int len2 = 20;
24 const char str2[] = "bbbbbbbbbbbbbbbbbbbb";
25
26 if (PAL_Initialize(argc, argv))
27 {
28 return FAIL;
29 }
30
31 /* this should work like malloc */
32 testA = (char *)realloc(NULL, len1*sizeof(char));
33 memcpy(testA, str1, len1);
34 if (testA == NULL)
35 {
36 Fail("We ran out of memory (unlikely), or realloc is broken.\n");
37 }
38
39 if (memcmp(testA, str1, len1) != 0)
40 {
41 Fail("realloc doesn't properly allocate new memory.\n");
42 }
43
44 testA = (char *)realloc(testA, len2*sizeof(char));
45 if (memcmp(testA, str1, len1) != 0)
46 {
47 Fail("realloc doesn't move the contents of the original memory "
48 "block to the newly allocated block.\n");
49 }
50
51 memcpy(testA, str2, len2);
52 if (memcmp(testA, str2, len2) != 0)
53 {
54 Fail("Couldn't write to memory allocated by realloc.\n");
55 }
56
57 /* free the buffer */
58 testA = (char*)realloc(testA, 0);
59 if (testA != NULL)
60 {
61 Fail("Realloc didn't return NULL when called with a length "
62 "of zero.\n");
63 }
64 PAL_Terminate();
65 return PASS;
66}
67