diff options
Diffstat (limited to 'src/pal/tests/palsuite/c_runtime/ungetc/test1/ungetc.c')
-rw-r--r-- | src/pal/tests/palsuite/c_runtime/ungetc/test1/ungetc.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/src/pal/tests/palsuite/c_runtime/ungetc/test1/ungetc.c b/src/pal/tests/palsuite/c_runtime/ungetc/test1/ungetc.c new file mode 100644 index 0000000000..d894f8b0b2 --- /dev/null +++ b/src/pal/tests/palsuite/c_runtime/ungetc/test1/ungetc.c @@ -0,0 +1,131 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +/*===================================================================== +** +** Source: ungetc.c (test 1) +** +** Purpose: Tests the PAL implementation of the ungetc function by calling +** the function on a write-only file. +** +** Dependencies: +** fopen +** fclose +** fseek +** +** +**===================================================================*/ + +#include <palsuite.h> + + +int __cdecl main(int argc, char *argv[]) +{ + char szFileName[] = {"test1.tmp"}; + const char text[] = + {"The quick brown fox jumped over the lazy dog's back."}; + FILE* pFile = NULL; + int nChar = 65; /* 'A' */ + int nRc = 0; + int itemsExpected; + int itemsWritten; + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + /* create the file */ + pFile = fopen(szFileName, "w"); + if (pFile == NULL) + { + Fail("ungetc: ERROR -> fopen failed to create the file \"%s\"" + " as write-only.\n", + szFileName); + } + + /* write to the file */ + itemsExpected = sizeof(text); + itemsWritten = fwrite(text, sizeof(text[0]), sizeof(text), pFile); + if (itemsWritten == 0) + { + Trace("ungetc: ERROR -> fwrite failed to write to the file \"%s\"\n", + szFileName); + + if (fclose(pFile) != 0) + { + Fail("ungetc: ERROR -> fclose failed to close the file.\n"); + } + Fail(""); + + } + else if (itemsWritten != itemsExpected) + { + Trace("ungetc: ERROR -> fwrite failed to write the correct number " + "of characters to the file \"%s\"\n", + szFileName); + + if (fclose(pFile) != 0) + { + Fail("ungetc: ERROR -> fclose failed to close the file.\n"); + } + Fail(""); + } + + /* Close the file */ + if (fclose(pFile) != 0) + { + Fail("ungetc: ERROR -> fclose failed to close the file.\n"); + } + + /* + ** open the file in write only mode and + ** attempt to push an unread character back on the stream + */ + + + /* open the file write-only */ + pFile = fopen(szFileName, "a"); + if (pFile == NULL) + { + Fail("ungetc: ERROR -> fopen failed to open the file \"%s\"" + " as write-only.\n", + szFileName); + } + + /* move the file pointer back to the beginning of the file */ + if (fseek(pFile, 1, SEEK_SET) != 0) + { + + Trace("ungetc: ERROR -> fseek failed to move the file pointer to the " + "beginning of the file.\n"); + if (fclose(pFile) != 0) + { + Trace("ungetc: ERROR -> fclose failed to close the file.\n"); + } + Fail(""); + } + + /* call ungetc on a write-only file which should fail */ + if ((nRc = ungetc(nChar, pFile)) != EOF) + { + Trace("ungetc: ERROR -> ungetc returned \"%c\" when run on " + "an write-only file.\n", nChar); + if (fclose(pFile) != 0) + { + Trace("ungetc: ERROR -> fclose failed to close the file.\n"); + } + Fail(""); + } + + if (fclose(pFile) != 0) + { + Fail("ungetc: ERROR -> fclose failed to close the file.\n"); + } + + + PAL_Terminate(); + return PASS; +} |