summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/ftell/test1/ftell.c
blob: 66e0854847a5ad6f5ad8db2377b89ff6568b8df9 (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
// 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;
}