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: ftell.c (test 1)
8**
9** Purpose: Tests the PAL implementation of the ftell function.
10**
11**
12**===================================================================*/
13
14#include <palsuite.h>
15
16FILE* pFile;
17struct TESTS
18{
19 long lDist;
20 int nFrom;
21 long lPosition;
22};
23
24
25/*************************************************
26**
27** Validate
28**
29** Purpose:
30** Tests whether the move was successful. If
31** it passes, it returns TRUE. If it fails
32** it outputs some error messages and returns
33** FALSE.
34**
35*************************************************/
36BOOL Validate(long lExpected)
37{
38 long lPos = -2;
39
40 if (((lPos = ftell(pFile)) == -1) || (lPos != lExpected))
41 {
42 Trace("ftell: ERROR -> ftell returned %ld when expecting %ld.\n",
43 lPos,
44 lExpected);
45 if (fclose(pFile) != 0)
46 {
47 Trace("ftell: ERROR -> fclose failed to close the file.\n");
48 }
49 return FALSE;
50 }
51 return TRUE;
52}
53
54
55/*************************************************
56**
57** MovePointer
58**
59** Purpose:
60** Accepts the distance to move and the
61** distance and calls fseek to move the file
62** pointer. If the fseek fails, error messages
63** are displayed and FALSE is returned. TRUE
64** is returned on a successful fseek.
65**
66*************************************************/
67BOOL MovePointer(long lDist, int nFrom)
68{
69 /* move the file pointer*/
70 if (fseek(pFile, lDist, nFrom) != 0)
71 {
72 Trace("ftell: ERROR -> fseek failed to move the file pointer "
73 "%l characters.\n",
74 lDist);
75 if (fclose(pFile) != 0)
76 {
77 Trace("ftell: ERROR -> fclose failed to close the file.\n");
78 }
79 return FALSE;
80 }
81 return TRUE;
82}
83
84
85
86int __cdecl main(int argc, char *argv[])
87{
88 const char szFileName[] = {"testfile.txt"};
89 long lPos = -1;
90 int i;
91 char szTempBuffer[256];
92 struct TESTS testCase[] =
93 {
94 {0, SEEK_SET, 0},
95 {10, SEEK_CUR, 10},
96 {-5, SEEK_CUR, 5},
97 {-2, SEEK_END, 50}
98 };
99
100
101
102 if (0 != PAL_Initialize(argc,argv))
103 {
104 return FAIL;
105 }
106
107 memset(szTempBuffer, 0, 256);
108
109
110 /* open the test file */
111 pFile = fopen(szFileName, "r");
112 if (pFile == NULL)
113 {
114 Fail("ftell: ERROR -> fopen failed to open the file \"%s\".\n");
115 }
116
117 /* loop through the test cases */
118 for (i = 0; i < (sizeof(testCase)/sizeof(struct TESTS)); i++)
119 {
120 if (MovePointer(testCase[i].lDist, testCase[i].nFrom) != TRUE)
121 {
122 Fail("");
123 }
124 else if (Validate(testCase[i].lPosition) != TRUE)
125 {
126 Fail("");
127 }
128 }
129
130 if (fclose(pFile) != 0)
131 {
132 Fail("ftell: ERROR -> fclose failed to close the file.\n");
133 }
134
135 /* lets just see if we can find out where we are in a closed stream... */
136 if ((lPos = ftell(pFile)) != -1)
137 {
138 Fail("ftell: ERROR -> ftell returned a valid position (%ld) on a "
139 "closed file handle\n",
140 lPos);
141 }
142
143 PAL_Terminate();
144 return PASS;
145}
146