1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=====================================================================
6**
7** Source: getc.c (test 1)
8**
9** Purpose: Tests the PAL implementation of the getc function.
10**
11**
12**===================================================================*/
13
14#include <palsuite.h>
15
16
17int __cdecl main(int argc, char *argv[])
18{
19 const char szFileName[] = {"testfile.tmp"};
20 const char szTestString[] =
21 {"The quick brown fox jumped over the lazy dog's back."};
22 FILE* pFile = NULL;
23 int nCount = 0;
24 int nChar = 0;
25 char szBuiltString[256];
26
27
28 if (0 != PAL_Initialize(argc,argv))
29 {
30 return FAIL;
31 }
32
33 memset(szBuiltString, 0, 256);
34
35
36 /* create/open a file for read and write */
37 pFile = fopen(szFileName, "w+");
38 if (pFile == NULL)
39 {
40 Fail("getc: ERROR -> fopen failed to create the file %s with the "
41 "error code %ld\n",
42 szFileName,
43 GetLastError());
44 }
45
46 /* try reading from an empty file */
47 if ((nChar = getc(pFile)) != EOF)
48 {
49 Trace("getc: ERROR -> getc returned \"%c\" when run on "
50 "an empty file.\n", nChar);
51 if (fclose(pFile) != 0)
52 {
53 Trace("getc: ERROR -> fclose failed to close the file. "
54 "GetLastError returned %ld\n",
55 GetLastError());
56 }
57 Fail("");
58 }
59
60 // Move the file pointer back to the beginning of the file. Some
61 // platforms require an fseek() between a getc() that returns EOF
62 // and any subsequent output to the file.
63 if (fseek(pFile, 0, SEEK_SET) != 0)
64 {
65 Trace("getc: ERROR -> fseek failed to move the file pointer to the "
66 "beginning of the file. GetLastError returned %ld\n",
67 GetLastError());
68 if (fclose(pFile) != 0)
69 {
70 Trace("getc: ERROR -> fclose failed to close the file. "
71 "GetLastError returned %ld\n",
72 GetLastError());
73 }
74 Fail("");
75 }
76
77 /* populate the file with a known string */
78 nCount = fprintf(pFile, szTestString);
79 if (nCount != strlen(szTestString))
80 {
81 Fail("getc: ERROR -> fprintf failed to write %s. The string is %d "
82 "characters long but fprintf apparently only wrote %d characters."
83 " GetLastError returned %ld\n",
84 szTestString,
85 strlen(szTestString),
86 nCount,
87 GetLastError());
88 }
89
90 /* move the file pointer back to the beginning of the file */
91 if (fseek(pFile, 0, SEEK_SET) != 0)
92 {
93 Trace("getc: ERROR -> fseek failed to move the file pointer to the "
94 "beginning of the file. GetLastError returned %ld\n",
95 GetLastError());
96 if (fclose(pFile) != 0)
97 {
98 Trace("getc: ERROR -> fclose failed to close the file. "
99 "GetLastError returned %ld\n",
100 GetLastError());
101 }
102 Fail("");
103 }
104
105 /* now get the characters one at a time */
106 nCount = 0;
107 while ((nChar = getc(pFile)) != EOF)
108 {
109 szBuiltString[nCount++] = nChar;
110 }
111
112 /* now, let's see if it worked */
113 if (strcmp(szBuiltString, szTestString) != 0)
114 {
115 Trace("getc: ERROR -> Reading one char at a time, getc built \"%s\" "
116 "however it should have built \"%s\".\n",
117 szBuiltString,
118 szTestString);
119 if (fclose(pFile) != 0)
120 {
121 Trace("getc: ERROR -> fclose failed to close the file. "
122 "GetLastError returned %ld\n",
123 GetLastError());
124 }
125 Fail("");
126 }
127
128 /* with the file pointer at EOF, try reading past EOF*/
129 if ((nChar = getc(pFile)) != EOF)
130 {
131 Trace("getc: ERROR -> getc returned \"%c\" when reading past "
132 "the end of the file.\n", nChar);
133 if (fclose(pFile) != 0)
134 {
135 Trace("getc: ERROR -> fclose failed to close the file. "
136 "GetLastError returned %ld\n",
137 GetLastError());
138 }
139 Fail("");
140 }
141
142 if (fclose(pFile) != 0)
143 {
144 Fail("getc: ERROR -> fclose failed to close the file. "
145 "GetLastError returned %ld\n",
146 GetLastError());
147 }
148
149
150 PAL_Terminate();
151 return PASS;
152}
153