diff options
Diffstat (limited to 'src/pal/tests/palsuite/c_runtime/realloc')
4 files changed, 103 insertions, 0 deletions
diff --git a/src/pal/tests/palsuite/c_runtime/realloc/CMakeLists.txt b/src/pal/tests/palsuite/c_runtime/realloc/CMakeLists.txt new file mode 100644 index 0000000000..f6aa0cb2d9 --- /dev/null +++ b/src/pal/tests/palsuite/c_runtime/realloc/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +add_subdirectory(test1) + diff --git a/src/pal/tests/palsuite/c_runtime/realloc/test1/CMakeLists.txt b/src/pal/tests/palsuite/c_runtime/realloc/test1/CMakeLists.txt new file mode 100644 index 0000000000..0a9f34fc3b --- /dev/null +++ b/src/pal/tests/palsuite/c_runtime/realloc/test1/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + test1.c +) + +add_executable(paltest_realloc_test1 + ${SOURCES} +) + +add_dependencies(paltest_realloc_test1 coreclrpal) + +target_link_libraries(paltest_realloc_test1 + pthread + m + coreclrpal +) 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; +} diff --git a/src/pal/tests/palsuite/c_runtime/realloc/test1/testinfo.dat b/src/pal/tests/palsuite/c_runtime/realloc/test1/testinfo.dat new file mode 100644 index 0000000000..5d2a32224e --- /dev/null +++ b/src/pal/tests/palsuite/c_runtime/realloc/test1/testinfo.dat @@ -0,0 +1,14 @@ +# 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. + +Version = 1.0 +Section = C Runtime +Function = realloc +Name = Positive Test for realloc +TYPE = DEFAULT +EXE1 = test1 +Description += Uses realloc to allocate and realloate memory, checking that memory += contents are copied when the memory is reallocated. + |