summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/fread/test2/test2.cpp
blob: d7262a9321676694ec1df66f3630cd71fb3e9178 (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
// 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:  test2.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 strings read in are correct.
**
** Depends:
**     fopen
**     fseek
**     fclose
**     strcmp
**     memset
**
**
**===================================================================*/

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

/* Note: The behaviour in win32 is to crash if a NULL pointer is passed to
   fread, so the test to check that it returns 0 has been removed.
*/

#include <palsuite.h>

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

    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. Then check to see that the data read in is correct.
       Note: The 'testfile' should have "This is a test." written in it.
    */
    memset(buffer,'\0',128);
    fread(buffer,1,15,fp);

    if(strcmp(buffer,"This is a test.") != 0)
    {
	Fail("ERROR: The data read in should have been "
	     "'This is a test.' but, the buffer contains '%s'.",
	     buffer);
    }
    
    /* 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 same 15 characters should
       be in the buffer.
    */
    
    memset(buffer,'\0',128);
    fread(buffer,1,17,fp);

    if(strcmp(buffer,"This is a test.") != 0)
    {
	Fail("ERROR: The data read in should have been "
	     "'This is a test.' but, the buffer contains '%s'.",
	     buffer);
    }
      
    /* 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 buffer is empty */
    
    memset(buffer,'\0',128);
    fread(buffer,1,0,fp);
     
    if(strcmp(buffer,"\0") != 0)
    {
	Fail("ERROR: The data read in should have been "
	     "NULL but, the buffer contains '%s'.",
	     buffer);
    }
   
    /* Read characters of 0 size and ensure the buffer is empty */
    
    memset(buffer,'\0',128);
    fread(buffer,0,5,fp);
   
    if(strcmp(buffer,"\0") != 0)
    {
	Fail("ERROR: The data read in should have been "
	     "NULL but, the buffer contains '%s'.",
	     buffer);
    }
   
    /* 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 buffer is empty 
    */
    memset(buffer,'\0',128);
    fread(buffer,1,5,fp);
    if(strcmp(buffer,"\0") != 0)
    {
	Fail("ERROR: The data read in should have been "
	     "NULL but, the buffer contains '%s'.",
	     buffer);
    }

    PAL_Terminate();
    return PASS;
}