summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/ftell
diff options
context:
space:
mode:
Diffstat (limited to 'src/pal/tests/palsuite/c_runtime/ftell')
-rw-r--r--src/pal/tests/palsuite/c_runtime/ftell/CMakeLists.txt4
-rw-r--r--src/pal/tests/palsuite/c_runtime/ftell/test1/CMakeLists.txt19
-rw-r--r--src/pal/tests/palsuite/c_runtime/ftell/test1/ftell.c145
-rw-r--r--src/pal/tests/palsuite/c_runtime/ftell/test1/testfile.txt1
-rw-r--r--src/pal/tests/palsuite/c_runtime/ftell/test1/testinfo.dat16
5 files changed, 185 insertions, 0 deletions
diff --git a/src/pal/tests/palsuite/c_runtime/ftell/CMakeLists.txt b/src/pal/tests/palsuite/c_runtime/ftell/CMakeLists.txt
new file mode 100644
index 0000000000..f6aa0cb2d9
--- /dev/null
+++ b/src/pal/tests/palsuite/c_runtime/ftell/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/ftell/test1/CMakeLists.txt b/src/pal/tests/palsuite/c_runtime/ftell/test1/CMakeLists.txt
new file mode 100644
index 0000000000..7ea9a51111
--- /dev/null
+++ b/src/pal/tests/palsuite/c_runtime/ftell/test1/CMakeLists.txt
@@ -0,0 +1,19 @@
+cmake_minimum_required(VERSION 2.8.12.2)
+
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
+
+set(SOURCES
+ ftell.c
+)
+
+add_executable(paltest_ftell_test1
+ ${SOURCES}
+)
+
+add_dependencies(paltest_ftell_test1 coreclrpal)
+
+target_link_libraries(paltest_ftell_test1
+ pthread
+ m
+ coreclrpal
+)
diff --git a/src/pal/tests/palsuite/c_runtime/ftell/test1/ftell.c b/src/pal/tests/palsuite/c_runtime/ftell/test1/ftell.c
new file mode 100644
index 0000000000..66e0854847
--- /dev/null
+++ b/src/pal/tests/palsuite/c_runtime/ftell/test1/ftell.c
@@ -0,0 +1,145 @@
+// 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: ftell.c (test 1)
+**
+** Purpose: Tests the PAL implementation of the ftell function.
+**
+**
+**===================================================================*/
+
+#include <palsuite.h>
+
+FILE* pFile;
+struct TESTS
+{
+ long lDist;
+ int nFrom;
+ long lPosition;
+};
+
+
+/*************************************************
+**
+** Validate
+**
+** Purpose:
+** Tests whether the move was successful. If
+** it passes, it returns TRUE. If it fails
+** it outputs some error messages and returns
+** FALSE.
+**
+*************************************************/
+BOOL Validate(long lExpected)
+{
+ long lPos = -2;
+
+ if (((lPos = ftell(pFile)) == -1) || (lPos != lExpected))
+ {
+ Trace("ftell: ERROR -> ftell returned %ld when expecting %ld.\n",
+ lPos,
+ lExpected);
+ if (fclose(pFile) != 0)
+ {
+ Trace("ftell: ERROR -> fclose failed to close the file.\n");
+ }
+ return FALSE;
+ }
+ return TRUE;
+}
+
+
+/*************************************************
+**
+** MovePointer
+**
+** Purpose:
+** Accepts the distance to move and the
+** distance and calls fseek to move the file
+** pointer. If the fseek fails, error messages
+** are displayed and FALSE is returned. TRUE
+** is returned on a successful fseek.
+**
+*************************************************/
+BOOL MovePointer(long lDist, int nFrom)
+{
+ /* move the file pointer*/
+ if (fseek(pFile, lDist, nFrom) != 0)
+ {
+ Trace("ftell: ERROR -> fseek failed to move the file pointer "
+ "%l characters.\n",
+ lDist);
+ if (fclose(pFile) != 0)
+ {
+ Trace("ftell: ERROR -> fclose failed to close the file.\n");
+ }
+ return FALSE;
+ }
+ return TRUE;
+}
+
+
+
+int __cdecl main(int argc, char *argv[])
+{
+ const char szFileName[] = {"testfile.txt"};
+ long lPos = -1;
+ int i;
+ char szTempBuffer[256];
+ struct TESTS testCase[] =
+ {
+ {0, SEEK_SET, 0},
+ {10, SEEK_CUR, 10},
+ {-5, SEEK_CUR, 5},
+ {-2, SEEK_END, 50}
+ };
+
+
+
+ if (0 != PAL_Initialize(argc,argv))
+ {
+ return FAIL;
+ }
+
+ memset(szTempBuffer, 0, 256);
+
+
+ /* open the test file */
+ pFile = fopen(szFileName, "r");
+ if (pFile == NULL)
+ {
+ Fail("ftell: ERROR -> fopen failed to open the file \"%s\".\n");
+ }
+
+ /* loop through the test cases */
+ for (i = 0; i < (sizeof(testCase)/sizeof(struct TESTS)); i++)
+ {
+ if (MovePointer(testCase[i].lDist, testCase[i].nFrom) != TRUE)
+ {
+ Fail("");
+ }
+ else if (Validate(testCase[i].lPosition) != TRUE)
+ {
+ Fail("");
+ }
+ }
+
+ if (fclose(pFile) != 0)
+ {
+ Fail("ftell: ERROR -> fclose failed to close the file.\n");
+ }
+
+ /* lets just see if we can find out where we are in a closed stream... */
+ if ((lPos = ftell(pFile)) != -1)
+ {
+ Fail("ftell: ERROR -> ftell returned a valid position (%ld) on a "
+ "closed file handle\n",
+ lPos);
+ }
+
+ PAL_Terminate();
+ return PASS;
+}
diff --git a/src/pal/tests/palsuite/c_runtime/ftell/test1/testfile.txt b/src/pal/tests/palsuite/c_runtime/ftell/test1/testfile.txt
new file mode 100644
index 0000000000..dd0fe15fe1
--- /dev/null
+++ b/src/pal/tests/palsuite/c_runtime/ftell/test1/testfile.txt
@@ -0,0 +1 @@
+The quick brown fox jumped over the lazy dog's back. \ No newline at end of file
diff --git a/src/pal/tests/palsuite/c_runtime/ftell/test1/testinfo.dat b/src/pal/tests/palsuite/c_runtime/ftell/test1/testinfo.dat
new file mode 100644
index 0000000000..c17ec9ad99
--- /dev/null
+++ b/src/pal/tests/palsuite/c_runtime/ftell/test1/testinfo.dat
@@ -0,0 +1,16 @@
+# 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 = ftell
+Name = test for ftell (test 1)
+Type = DEFAULT
+EXE1 = ftell
+Description
+= Use fseek and a static list of distances to move, direction
+= to move and expected results to test the ftell function. A typical
+= test will move the file pointer with fseek then call ftell. The
+= results from ftell will then be compared to the expected result to
+= determine whether the test passed or failed.