From 4b4aad7217d3292650e77eec2cf4c198ea9c3b4b Mon Sep 17 00:00:00 2001 From: Jiyoung Yun Date: Wed, 23 Nov 2016 19:09:09 +0900 Subject: Imported Upstream version 1.1.0 --- .../tests/palsuite/c_runtime/fseek/CMakeLists.txt | 4 + .../palsuite/c_runtime/fseek/test1/CMakeLists.txt | 19 ++ .../tests/palsuite/c_runtime/fseek/test1/test1.c | 193 +++++++++++++++++++++ .../palsuite/c_runtime/fseek/test1/testinfo.dat | 15 ++ 4 files changed, 231 insertions(+) create mode 100644 src/pal/tests/palsuite/c_runtime/fseek/CMakeLists.txt create mode 100644 src/pal/tests/palsuite/c_runtime/fseek/test1/CMakeLists.txt create mode 100644 src/pal/tests/palsuite/c_runtime/fseek/test1/test1.c create mode 100644 src/pal/tests/palsuite/c_runtime/fseek/test1/testinfo.dat (limited to 'src/pal/tests/palsuite/c_runtime/fseek') diff --git a/src/pal/tests/palsuite/c_runtime/fseek/CMakeLists.txt b/src/pal/tests/palsuite/c_runtime/fseek/CMakeLists.txt new file mode 100644 index 0000000000..f6aa0cb2d9 --- /dev/null +++ b/src/pal/tests/palsuite/c_runtime/fseek/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/fseek/test1/CMakeLists.txt b/src/pal/tests/palsuite/c_runtime/fseek/test1/CMakeLists.txt new file mode 100644 index 0000000000..fb55bd1051 --- /dev/null +++ b/src/pal/tests/palsuite/c_runtime/fseek/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_fseek_test1 + ${SOURCES} +) + +add_dependencies(paltest_fseek_test1 coreclrpal) + +target_link_libraries(paltest_fseek_test1 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/c_runtime/fseek/test1/test1.c b/src/pal/tests/palsuite/c_runtime/fseek/test1/test1.c new file mode 100644 index 0000000000..8496289a01 --- /dev/null +++ b/src/pal/tests/palsuite/c_runtime/fseek/test1/test1.c @@ -0,0 +1,193 @@ +// 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: Call fseek to move a file pointer to the start of a file, +** a position offset from the start, a position offset from the +** current position, and a position offset from the end of the +** file. Check that the file pointer is at the correct position +** after each seek. +** +** +**==========================================================================*/ + +#include + +const char filename[] = "testfile.txt"; + +static BOOL Cleanup(HANDLE hFile) +{ + BOOL result= TRUE; + + if (fclose(hFile)) + { + Trace("fseek: ERROR -> Unable to close file \"%s\".\n", + filename); + result= FALSE; + } + if (!DeleteFileA(filename)) + { + result= FALSE; + Trace("fseek: ERROR -> Unable to delete file \"%s\". ", + "GetLastError returned %u.\n", + filename, + GetLastError()); + } + return result; +} + +int __cdecl main(int argc, char **argv) +{ + char outBuf[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + char inBuf[20]; + FILE * fp; + int size = ( sizeof(outBuf)/sizeof(char) ) - 1; + + if (PAL_Initialize(argc, argv)) + { + return FAIL; + } + + + /*create the file*/ + fp = fopen(filename, "w"); + if (fp == NULL) + { + Fail("Unable to open a file for write.\n"); + } + if(fprintf(fp, outBuf) != size) + { + Trace("Unable to write to %s.\n", filename); + Cleanup(fp); + Fail(""); + } + + if (fclose(fp) != 0) + { + Trace("Unable to close newly written file.\n"); + if (!DeleteFileA(filename)) + { + Trace("fseek: ERROR -> Unable to delete file \"%s\". ", + "GetLastError returned %u.\n", + filename, + GetLastError()); + } + Fail(""); + } + + fp = fopen(filename, "r"); + if (fp == NULL) + { + if (!DeleteFileA(filename)) + { + Trace("_putw: ERROR -> Unable to delete file \"%s\". ", + "GetLastError returned %u.\n", + filename, + GetLastError()); + } + Fail("Unable to open a file for read.\n"); + } + + /*seek to the start*/ + if (fseek(fp, 0, SEEK_SET) != 0) + { + Cleanup(fp); + Fail("fseek failed when seeking the start of a file.\n"); + } + if (fgets(inBuf, 11, fp) != inBuf) + { + Cleanup(fp); + Fail("Unable to read from file after using fseek to move to the start.\n"); + } + if (strncmp(inBuf, outBuf, 10) != 0) + { + Cleanup(fp); + Fail("fseek was asked to seek the start of a file," + "but didn't get there.\n"); + } + + /*Seek with an offset from the start*/ + + if (fseek(fp, 10, SEEK_SET) != 0) + { + Cleanup(fp); + Fail("fseek failed when called with SEEK_SET and a positive offset.\n"); + } + + if (fgets(inBuf, 6, fp) != inBuf) + { + Cleanup(fp); + Fail("fgets failed after feek was called with SEEK_SET" + "and a positive offset.\n"); + } + + + if (strncmp(inBuf, "ABCDE", 5) != 0) + { + Cleanup(fp); + Fail("fseek did not move to the correct position when passed SEEK_SET" + " and a positive offset.\n"); + } + + /*now move backwards and read the same string*/ + if (fseek(fp, -5, SEEK_CUR) != 0) + { + Cleanup(fp); + Fail("fseek failed when passed SEEK_CUR and a negative offset.\n"); + } + + if (fgets(inBuf, 6, fp) != inBuf) + { + Cleanup(fp); + Fail("fgets failed after fseek was called with SEEK_CUR and a " + "negative offset.\n"); + } + + if (strncmp(inBuf, "ABCDE", 5) != 0) + { + Cleanup(fp); + Fail("fseek did not move to the correct position when called with" + " SEEK_CUR and a negative offset.\n"); + } + + /*Try seeking relative to the end of the file.*/ + if (fseek(fp, -10, SEEK_END) != 0) + { + Cleanup(fp); + Fail("fseek failed when called with SEEK_END and a negative" + " offset.\n"); + } + if (fgets(inBuf, 2, fp) != inBuf) + { + Cleanup(fp); + Fail("fgets failed after fseek was called with SEEK_END and a " + "negative offset\n"); + } + + if (strncmp(inBuf, "Q", 1) != 0) + { + Cleanup(fp); + Fail("fseek did not move to the correct position when called with " + "SEEK_END and a negative offset.\n"); + } + + + /*close the file*/ + if(!Cleanup(fp)) + { + Fail(""); + } + + PAL_Terminate(); + return PASS; +} + + + + + + diff --git a/src/pal/tests/palsuite/c_runtime/fseek/test1/testinfo.dat b/src/pal/tests/palsuite/c_runtime/fseek/test1/testinfo.dat new file mode 100644 index 0000000000..788f8d4bea --- /dev/null +++ b/src/pal/tests/palsuite/c_runtime/fseek/test1/testinfo.dat @@ -0,0 +1,15 @@ +# 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 = fseek +Name = Positive Test for fseek +TYPE = DEFAULT +EXE1 = test1 +Description += Call seek to move a file pointer to the start of a file, a position += offset from the start, a position offset from the current position, and += a position offset from the end of the file. Check that the file += pointer is at the correct position after each seek. -- cgit v1.2.3