summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/getc/test1/getc.cpp
blob: dfe10d6160c578fcc711b78a58deb562a5dc53ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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;
}