summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/realloc/test1/test1.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pal/tests/palsuite/c_runtime/realloc/test1/test1.c')
-rw-r--r--src/pal/tests/palsuite/c_runtime/realloc/test1/test1.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/pal/tests/palsuite/c_runtime/realloc/test1/test1.c b/src/pal/tests/palsuite/c_runtime/realloc/test1/test1.c
new file mode 100644
index 0000000000..d0dd128cc1
--- /dev/null
+++ b/src/pal/tests/palsuite/c_runtime/realloc/test1/test1.c
@@ -0,0 +1,66 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+/*============================================================================
+**
+** Source: test1.c
+**
+** Purpose: Uses realloc to allocate and realloate memory, checking
+** that memory contents are copied when the memory is reallocated.
+**
+**
+**==========================================================================*/
+
+#include <palsuite.h>
+
+int __cdecl main(int argc, char **argv)
+{
+ char *testA;
+ const int len1 = 10;
+ const char str1[] = "aaaaaaaaaa";
+
+ const int len2 = 20;
+ const char str2[] = "bbbbbbbbbbbbbbbbbbbb";
+
+ if (PAL_Initialize(argc, argv))
+ {
+ return FAIL;
+ }
+
+ /* this should work like malloc */
+ testA = (char *)realloc(NULL, len1*sizeof(char));
+ memcpy(testA, str1, len1);
+ if (testA == NULL)
+ {
+ Fail("We ran out of memory (unlikely), or realloc is broken.\n");
+ }
+
+ if (memcmp(testA, str1, len1) != 0)
+ {
+ Fail("realloc doesn't properly allocate new memory.\n");
+ }
+
+ testA = (char *)realloc(testA, len2*sizeof(char));
+ if (memcmp(testA, str1, len1) != 0)
+ {
+ Fail("realloc doesn't move the contents of the original memory "
+ "block to the newly allocated block.\n");
+ }
+
+ memcpy(testA, str2, len2);
+ if (memcmp(testA, str2, len2) != 0)
+ {
+ Fail("Couldn't write to memory allocated by realloc.\n");
+ }
+
+ /* free the buffer */
+ testA = realloc(testA, 0);
+ if (testA != NULL)
+ {
+ Fail("Realloc didn't return NULL when called with a length "
+ "of zero.\n");
+ }
+ PAL_Terminate();
+ return PASS;
+}