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: test3.c
8**
9** Purpose: Tests the PAL implementation of the fread function.
10** Open a file in READ mode, then try to read from the file with
11** different 'size' params. Check to ensure the return values and
12** the text in the buffer is correct.
13**
14** Depends:
15** fopen
16** fseek
17** strcmp
18** memset
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#include <palsuite.h>
28
29int __cdecl main(int argc, char **argv)
30{
31 const char filename[] = "testfile";
32 char buffer[128];
33 FILE * fp = NULL;
34 int result;
35
36 if (0 != PAL_Initialize(argc, argv))
37 {
38 return FAIL;
39 }
40
41 /* Open a file in READ mode */
42
43 if((fp = fopen(filename, "r")) == NULL)
44 {
45 Fail("Unable to open a file for reading. Is the file "
46 "in the directory? It should be.");
47 }
48
49 memset(buffer,'x',128);
50
51 /* Put the null one character past the end of the text that was read
52 in, to ensure that it wasn't reading in 0
53 */
54
55 buffer[16] = '\0';
56
57 /* Attempt to read in 5 bytes at a time. This should return 3 and
58 contain the full string in the buffer.
59 */
60
61 if((result = fread(buffer,5,3,fp)) != 3)
62 {
63 Fail("ERROR: Attempted to read in data of size 5. The file has "
64 "15 bytes in it so 3 items should have been read. But the value "
65 "returned was %d.",result);
66 }
67
68 if(strcmp(buffer, "This is a test.x") != 0)
69 {
70 Fail("ERROR: The buffer should have contained the text "
71 "'This is a test.x' but instead contained '%s'.",buffer);
72 }
73
74 memset(buffer,'x',128);
75
76 if(fseek(fp, 0, SEEK_SET))
77 {
78 Fail("ERROR: fseek failed, and this test depends on it.");
79 }
80
81 buffer[16] = '\0';
82
83 /* Attempt to read in 6 bytes at a time. The return should be 2. The
84 full string should still be in the buffer.
85 */
86
87 if((result = fread(buffer,6,3,fp)) != 2)
88 {
89 Fail("ERROR: Attempted to read in data of size 6. The file has "
90 "15 bytes in it, so 2 items should have been read. But the "
91 "value returned was %d.",result);
92 }
93
94 if(strcmp(buffer, "This is a test.x") != 0)
95 {
96 Fail("ERROR: The buffer should have contained the text "
97 "'This is a test.x' but instead contained '%s'.",buffer);
98 }
99
100 memset(buffer,'x',128);
101
102 buffer[7] = '\0';
103
104 if(fseek(fp, 0, SEEK_SET))
105 {
106 Fail("ERROR: fseek failed, and this test depends on it.");
107 }
108
109 /* Attempt to read in 6 bytes at a time but only one item max.
110 The return should be 1. The first 6 characters should be in the
111 buffer.
112 */
113
114 if((result = fread(buffer,6,1,fp)) != 1)
115 {
116 Fail("ERROR: Attempted to read in data of size 6 with a max count "
117 "of 1. Thus, one item should have been read, but the "
118 "value returned was %d.",result);
119 }
120
121 if(strcmp(buffer, "This ix") != 0)
122 {
123 Fail("ERROR: The buffer should have contained the text "
124 "'This ix.' but instead contained '%s'.",buffer);
125 }
126
127 PAL_Terminate();
128 return PASS;
129}
130
131
132