summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/fread/test1/test1.cpp
blob: b706b2e91ca453bc37c72fb5dc30082a012689e2 (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
// 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:  test1.c
**
** Purpose: Tests the PAL implementation of the fread function.
**          Open a file in READ mode, and then try to read all
**          the characters, more than all the characters,
**          0 characters and 0 sized characters and check that
**          the return values are correct.
**
** Depends:
**     fopen
**     fseek
**     fclose
**
**
**===================================================================*/

/* Note: testfile should exist in the directory with 15 characters 
   in it ... something got lost if it isn't here.
*/

/* Note: Under win32, fread() crashes when passed NULL.  The test to ensure that
   it returns 0 has been removed to reflect this.
*/

#include <palsuite.h>

int __cdecl main(int argc, char **argv)
{
    const char filename[] = "testfile";
    char buffer[128];
    FILE * fp = NULL;
    int result;

    if (0 != PAL_Initialize(argc, argv))
    {
        return FAIL;
    }
  
    /* Open a file in READ mode */

    if((fp = fopen(filename, "r")) == NULL)
    {
        Fail("Unable to open a file for reading.  Is the file "
               "in the directory?  It should be.");
    }

    /* Read 15 characters from the file.  The file has exactly this many 
       in it.
    */
    if((result = fread(buffer,1,15,fp)) == 0)
    {
        Fail("ERROR: Zero characters read from the file.  It should have "
               "15 characters in it.");
    }

    if(result != 15)
    {
        Fail("ERROR: The fread function should have returned that it read "
               "in 15 characters from the file.  But it indicates having "
               "read %i characters.",result);
    }

    /* Go back to the start of the file */

    if(fseek(fp, 0, SEEK_SET)) 
    {
        Fail("ERROR: fseek failed, and this test depends on it.");
    }

    /* Attempt to read 17 characters, the return should still be 15 */

    if((result = fread(buffer,1,17,fp)) == 0)
    {
        Fail("ERROR: Zero characters read from the file.  It should have "
               "15 characters in it.  Though, it attempted to read 17.");
    }  
  
    if(result != 15)
    {
        Fail("ERROR: The fread function should have returned that it read "
               "in 15 characters from the file.  "
               "But it indicates having read  %i characters.",result);
    }
  
    /* Back to the start of the file */
  
    if(fseek(fp, 0, SEEK_SET)) 
    {
        Fail("ERROR: fseek failed, and this test depends on it.");
    }

    /* Read 0 characters and ensure the function returns 0 */

    if((result = fread(buffer,1,0,fp)) != 0)
    {
        Fail("ERROR: The return value should be 0, as we attempted to "
               "read 0 characters.");
    } 

    /* Read characters of 0 size and ensure the return value is 0 */

    if((result = fread(buffer,0,5,fp)) != 0)
    {
        Fail("ERROR: The return value should be 0, as we attempted to "
               "read 0 sized data.");
    } 

    /* Close the file */

    if(fclose(fp))
    {
        Fail("ERROR: fclose failed.  Test depends on it.");
    }

    /* Read 5 characters of 1 size from a closed file pointer 
       and ensure the return value is 0 
    */

    if((result = fread(buffer,1,5,fp)) != 0)
    {
        Fail("ERROR: The return value should be 0, as we attempted to "
               "read data from a closed file pointer.");
    } 

    PAL_Terminate();
    return PASS;
}