summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/getc/test1
diff options
context:
space:
mode:
Diffstat (limited to 'src/pal/tests/palsuite/c_runtime/getc/test1')
-rw-r--r--src/pal/tests/palsuite/c_runtime/getc/test1/CMakeLists.txt19
-rw-r--r--src/pal/tests/palsuite/c_runtime/getc/test1/getc.c152
-rw-r--r--src/pal/tests/palsuite/c_runtime/getc/test1/testinfo.dat14
3 files changed, 185 insertions, 0 deletions
diff --git a/src/pal/tests/palsuite/c_runtime/getc/test1/CMakeLists.txt b/src/pal/tests/palsuite/c_runtime/getc/test1/CMakeLists.txt
new file mode 100644
index 0000000000..2a29e8af0a
--- /dev/null
+++ b/src/pal/tests/palsuite/c_runtime/getc/test1/CMakeLists.txt
@@ -0,0 +1,19 @@
+cmake_minimum_required(VERSION 2.8.12.2)
+
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
+
+set(SOURCES
+ getc.c
+)
+
+add_executable(paltest_getc_test1
+ ${SOURCES}
+)
+
+add_dependencies(paltest_getc_test1 coreclrpal)
+
+target_link_libraries(paltest_getc_test1
+ pthread
+ m
+ coreclrpal
+)
diff --git a/src/pal/tests/palsuite/c_runtime/getc/test1/getc.c b/src/pal/tests/palsuite/c_runtime/getc/test1/getc.c
new file mode 100644
index 0000000000..dfe10d6160
--- /dev/null
+++ b/src/pal/tests/palsuite/c_runtime/getc/test1/getc.c
@@ -0,0 +1,152 @@
+// 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: getc.c (test 1)
+**
+** Purpose: Tests the PAL implementation of the getc function.
+**
+**
+**===================================================================*/
+
+#include <palsuite.h>
+
+
+int __cdecl main(int argc, char *argv[])
+{
+ const char szFileName[] = {"testfile.tmp"};
+ const char szTestString[] =
+ {"The quick brown fox jumped over the lazy dog's back."};
+ FILE* pFile = NULL;
+ int nCount = 0;
+ int nChar = 0;
+ char szBuiltString[256];
+
+
+ if (0 != PAL_Initialize(argc,argv))
+ {
+ return FAIL;
+ }
+
+ memset(szBuiltString, 0, 256);
+
+
+ /* create/open a file for read and write */
+ pFile = fopen(szFileName, "w+");
+ if (pFile == NULL)
+ {
+ Fail("getc: ERROR -> fopen failed to create the file %s with the "
+ "error code %ld\n",
+ szFileName,
+ GetLastError());
+ }
+
+ /* try reading from an empty file */
+ if ((nChar = getc(pFile)) != EOF)
+ {
+ Trace("getc: ERROR -> getc returned \"%c\" when run on "
+ "an empty file.\n", nChar);
+ if (fclose(pFile) != 0)
+ {
+ Trace("getc: ERROR -> fclose failed to close the file. "
+ "GetLastError returned %ld\n",
+ GetLastError());
+ }
+ Fail("");
+ }
+
+ // Move the file pointer back to the beginning of the file. Some
+ // platforms require an fseek() between a getc() that returns EOF
+ // and any subsequent output to the file.
+ if (fseek(pFile, 0, SEEK_SET) != 0)
+ {
+ Trace("getc: ERROR -> fseek failed to move the file pointer to the "
+ "beginning of the file. GetLastError returned %ld\n",
+ GetLastError());
+ if (fclose(pFile) != 0)
+ {
+ Trace("getc: ERROR -> fclose failed to close the file. "
+ "GetLastError returned %ld\n",
+ GetLastError());
+ }
+ Fail("");
+ }
+
+ /* populate the file with a known string */
+ nCount = fprintf(pFile, szTestString);
+ if (nCount != strlen(szTestString))
+ {
+ Fail("getc: ERROR -> fprintf failed to write %s. The string is %d "
+ "characters long but fprintf apparently only wrote %d characters."
+ " GetLastError returned %ld\n",
+ szTestString,
+ strlen(szTestString),
+ nCount,
+ GetLastError());
+ }
+
+ /* move the file pointer back to the beginning of the file */
+ if (fseek(pFile, 0, SEEK_SET) != 0)
+ {
+ Trace("getc: ERROR -> fseek failed to move the file pointer to the "
+ "beginning of the file. GetLastError returned %ld\n",
+ GetLastError());
+ if (fclose(pFile) != 0)
+ {
+ Trace("getc: ERROR -> fclose failed to close the file. "
+ "GetLastError returned %ld\n",
+ GetLastError());
+ }
+ Fail("");
+ }
+
+ /* now get the characters one at a time */
+ nCount = 0;
+ while ((nChar = getc(pFile)) != EOF)
+ {
+ szBuiltString[nCount++] = nChar;
+ }
+
+ /* now, let's see if it worked */
+ if (strcmp(szBuiltString, szTestString) != 0)
+ {
+ Trace("getc: ERROR -> Reading one char at a time, getc built \"%s\" "
+ "however it should have built \"%s\".\n",
+ szBuiltString,
+ szTestString);
+ if (fclose(pFile) != 0)
+ {
+ Trace("getc: ERROR -> fclose failed to close the file. "
+ "GetLastError returned %ld\n",
+ GetLastError());
+ }
+ Fail("");
+ }
+
+ /* with the file pointer at EOF, try reading past EOF*/
+ if ((nChar = getc(pFile)) != EOF)
+ {
+ Trace("getc: ERROR -> getc returned \"%c\" when reading past "
+ "the end of the file.\n", nChar);
+ if (fclose(pFile) != 0)
+ {
+ Trace("getc: ERROR -> fclose failed to close the file. "
+ "GetLastError returned %ld\n",
+ GetLastError());
+ }
+ Fail("");
+ }
+
+ if (fclose(pFile) != 0)
+ {
+ Fail("getc: ERROR -> fclose failed to close the file. "
+ "GetLastError returned %ld\n",
+ GetLastError());
+ }
+
+
+ PAL_Terminate();
+ return PASS;
+}
diff --git a/src/pal/tests/palsuite/c_runtime/getc/test1/testinfo.dat b/src/pal/tests/palsuite/c_runtime/getc/test1/testinfo.dat
new file mode 100644
index 0000000000..19a0c1ee1a
--- /dev/null
+++ b/src/pal/tests/palsuite/c_runtime/getc/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 = file_io
+Function = getc
+Name = test for getc (test 1)
+Type = DEFAULT
+EXE1 = getc
+Description
+= Write a string to a file, read it in one character
+= at a time with getc and compare with the original string.
+= It also verifies that getc can't read past EOF.