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: test1.c |
8 | ** |
9 | ** Purpose: Tests the PAL implementation of the fread function. |
10 | ** Open a file in READ mode, and then try to read all |
11 | ** the characters, more than all the characters, |
12 | ** 0 characters and 0 sized characters and check that |
13 | ** the return values are correct. |
14 | ** |
15 | ** Depends: |
16 | ** fopen |
17 | ** fseek |
18 | ** fclose |
19 | ** |
20 | ** |
21 | **===================================================================*/ |
22 | |
23 | /* Note: testfile should exist in the directory with 15 characters |
24 | in it ... something got lost if it isn't here. |
25 | */ |
26 | |
27 | /* Note: Under win32, fread() crashes when passed NULL. The test to ensure that |
28 | it returns 0 has been removed to reflect this. |
29 | */ |
30 | |
31 | #include <palsuite.h> |
32 | |
33 | int __cdecl main(int argc, char **argv) |
34 | { |
35 | const char filename[] = "testfile" ; |
36 | char buffer[128]; |
37 | FILE * fp = NULL; |
38 | int result; |
39 | |
40 | if (0 != PAL_Initialize(argc, argv)) |
41 | { |
42 | return FAIL; |
43 | } |
44 | |
45 | /* Open a file in READ mode */ |
46 | |
47 | if((fp = fopen(filename, "r" )) == NULL) |
48 | { |
49 | Fail("Unable to open a file for reading. Is the file " |
50 | "in the directory? It should be." ); |
51 | } |
52 | |
53 | /* Read 15 characters from the file. The file has exactly this many |
54 | in it. |
55 | */ |
56 | if((result = fread(buffer,1,15,fp)) == 0) |
57 | { |
58 | Fail("ERROR: Zero characters read from the file. It should have " |
59 | "15 characters in it." ); |
60 | } |
61 | |
62 | if(result != 15) |
63 | { |
64 | Fail("ERROR: The fread function should have returned that it read " |
65 | "in 15 characters from the file. But it indicates having " |
66 | "read %i characters." ,result); |
67 | } |
68 | |
69 | /* Go back to the start of the file */ |
70 | |
71 | if(fseek(fp, 0, SEEK_SET)) |
72 | { |
73 | Fail("ERROR: fseek failed, and this test depends on it." ); |
74 | } |
75 | |
76 | /* Attempt to read 17 characters, the return should still be 15 */ |
77 | |
78 | if((result = fread(buffer,1,17,fp)) == 0) |
79 | { |
80 | Fail("ERROR: Zero characters read from the file. It should have " |
81 | "15 characters in it. Though, it attempted to read 17." ); |
82 | } |
83 | |
84 | if(result != 15) |
85 | { |
86 | Fail("ERROR: The fread function should have returned that it read " |
87 | "in 15 characters from the file. " |
88 | "But it indicates having read %i characters." ,result); |
89 | } |
90 | |
91 | /* Back to the start of the file */ |
92 | |
93 | if(fseek(fp, 0, SEEK_SET)) |
94 | { |
95 | Fail("ERROR: fseek failed, and this test depends on it." ); |
96 | } |
97 | |
98 | /* Read 0 characters and ensure the function returns 0 */ |
99 | |
100 | if((result = fread(buffer,1,0,fp)) != 0) |
101 | { |
102 | Fail("ERROR: The return value should be 0, as we attempted to " |
103 | "read 0 characters." ); |
104 | } |
105 | |
106 | /* Read characters of 0 size and ensure the return value is 0 */ |
107 | |
108 | if((result = fread(buffer,0,5,fp)) != 0) |
109 | { |
110 | Fail("ERROR: The return value should be 0, as we attempted to " |
111 | "read 0 sized data." ); |
112 | } |
113 | |
114 | /* Close the file */ |
115 | |
116 | if(fclose(fp)) |
117 | { |
118 | Fail("ERROR: fclose failed. Test depends on it." ); |
119 | } |
120 | |
121 | /* Read 5 characters of 1 size from a closed file pointer |
122 | and ensure the return value is 0 |
123 | */ |
124 | |
125 | if((result = fread(buffer,1,5,fp)) != 0) |
126 | { |
127 | Fail("ERROR: The return value should be 0, as we attempted to " |
128 | "read data from a closed file pointer." ); |
129 | } |
130 | |
131 | PAL_Terminate(); |
132 | return PASS; |
133 | } |
134 | |
135 | |
136 | |