Created new subtree for groups of related test programs.
[reactos.git] / reactos / apps / testsets / msvcrt / fileio / _tfileio.c
1 /*
2 * ReactOS test program -
3 *
4 * _tfileio.c
5 *
6 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #define WIN32_LEAN_AND_MEAN
24 #include <windows.h>
25 #include <tchar.h>
26 #include <wchar.h>
27 #include <stdio.h>
28
29
30 #ifdef UNICODE
31
32 #define _tfopen _wfopen
33 #define _tunlink _wunlink
34 #define _TEOF WEOF
35 #define _gettchar getwchar
36 #define _puttchar putwchar
37 #define _THEX_FORMAT _T("0x%04x ")
38 #define TEST_B1_FILE_SIZE 85
39 #define TEST_B2_FILE_SIZE 332
40 #define TEST_B3_FILE_SIZE 83
41 #define TEST_B4_FILE_SIZE 324
42
43 #else /*UNICODE*/
44
45 #define _tfopen fopen
46 #define _tunlink _unlink
47 #define _TEOF EOF
48 #define _gettchar getchar
49 #define _puttchar putchar
50 #define _THEX_FORMAT "0x%02x "
51 #define TEST_B1_FILE_SIZE 170
52 #define TEST_B2_FILE_SIZE 162
53 #define TEST_B3_FILE_SIZE 166
54 #define TEST_B4_FILE_SIZE 162
55
56 #endif /*UNICODE*/
57
58
59 #define TEST_BUFFER_SIZE 200
60
61 extern BOOL verbose_flagged;
62 extern TCHAR test_buffer[TEST_BUFFER_SIZE];
63
64
65 static TCHAR dos_data[] = _T("line1: this is a bunch of readable text.\r\n")\
66 _T("line2: some more printable text and punctuation !@#$%^&*()\r\n")\
67 _T("line3: followed up with some numerals 1234567890\r\n")\
68 _T("line4: done.\r\n");
69
70 static TCHAR nix_data[] = _T("line1: this is a bunch of readable text.\n")\
71 _T("line2: some more printable text and punctuation !@#$%^&*()\n")\
72 _T("line3: followed up with some numerals 1234567890\n")\
73 _T("line4: done.\n");
74
75
76 static BOOL create_output_file(TCHAR* file_name, TCHAR* file_mode, TCHAR* file_data)
77 {
78 BOOL result = FALSE;
79 FILE *file = _tfopen(file_name, file_mode);
80 if (file == NULL) {
81 _tprintf(_T("ERROR: Can't open file \"%s\" for writing\n"), file_name);
82 return FALSE;
83 }
84 if (_fputts(file_data, file) != _TEOF) {
85 result = TRUE;
86 } else {
87 _tprintf(_T("ERROR: failed to write to file \"%s\"\n"), file_name);
88 }
89 fclose(file);
90 return result;
91 }
92
93 static BOOL verify_output_file(TCHAR* file_name, TCHAR* file_mode, TCHAR* file_data)
94 {
95 int offset = 0;
96 int line_num = 0;
97 BOOL result = FALSE;
98 FILE* file = _tfopen(file_name, file_mode);
99 if (file == NULL) {
100 _tprintf(_T("ERROR: Can't open file \"%s\" for reading\n"), file_name);
101 return FALSE;
102 }
103 while (_fgetts(test_buffer, TEST_BUFFER_SIZE, file)) {
104 int length = _tcslen(test_buffer);
105 _tprintf(_T("STATUS: Verifying %d bytes read from line %d\n"), length, ++line_num);
106 if (_tcsncmp(test_buffer, file_data+offset, length) == 0) {
107 result = TRUE;
108 } else {
109 _tprintf(_T("WARNING: failed to verify data from file \"%s\"\n"), file_name);
110 }
111 offset += length;
112 }
113 if (!line_num) {
114 _tprintf(_T("ERROR: failed to read from file \"%s\"\n"), file_name);
115 }
116 fclose(file);
117 return result;
118 }
119
120 static int test_A(TCHAR* file_name, TCHAR* write_mode, TCHAR* read_mode, TCHAR* file_data)
121 {
122 _tprintf(_T("STATUS: Attempting to create output file %s\n"), file_name);
123 if (create_output_file(file_name, write_mode, file_data)) {
124 _tprintf(_T("STATUS: Attempting to verify output file %s\n"), file_name);
125 if (verify_output_file(file_name, read_mode, file_data)) {
126 _tprintf(_T("SUCCESS: %s verified ok\n"), file_name);
127 } else {
128 _tprintf(_T("ERROR: Can't verify output file %s\n"), file_name);
129 return 2;
130 }
131 } else {
132 _tprintf(_T("ERROR: Can't create output file %s\n"), file_name);
133 return 1;
134 }
135 return 0;
136 }
137
138 static int test_B(TCHAR* file_name, TCHAR* file_mode, int expected)
139 {
140 int count = 0;
141 FILE* file;
142 TCHAR ch;
143
144 _tprintf(_T("STATUS: checking %s in %s mode\n"), file_name, _tcschr(file_mode, _T('b')) ? _T("binary") : _T("text"));
145
146 file = _tfopen(file_name, file_mode);
147 if (file == NULL) {
148 _tprintf(_T("ERROR: Can't open file \"%s\" for reading\n"), file_name);
149 return 1;
150 }
151 while ((ch = _fgettc(file)) != _TEOF) {
152 if (verbose_flagged) {
153 _tprintf(_THEX_FORMAT, ch);
154 }
155 ++count;
156 }
157 if (verbose_flagged) {
158 _puttchar(_T('\n'));
159 }
160 fclose(file);
161 if (count == expected) {
162 _tprintf(_T("PASSED: read %d bytes from %s as expected\n"), count, file_name);
163 } else {
164 _tprintf(_T("ERROR: read %d bytes from %s, expected %d\n"), count, file_name, expected);
165 }
166 return count;
167 }
168
169 static int test_C(void)
170 {
171 TCHAR buffer[81];
172 TCHAR ch;
173 int i;
174
175 _tprintf(_T("Enter a line: "));
176 for (i = 0; (i < 80) && ((ch = _gettchar()) != _TEOF) && (ch != _T('\n')); i++) {
177 buffer[i] = (TCHAR)ch;
178 }
179 buffer[i] = _T('\0');
180 _tprintf(_T("%s\n"), buffer);
181 return 0;
182 }
183
184 static int test_D(void)
185 {
186 int result = 0;
187 TCHAR ch;
188
189 while ((ch = _gettchar()) != _TEOF) {
190 _tprintf(_THEX_FORMAT, ch);
191 }
192 return result;
193 }
194
195 static int clean_files(void)
196 {
197 int result = 0;
198
199 result |= _tunlink(_T("binary.dos"));
200 result |= _tunlink(_T("binary.nix"));
201 result |= _tunlink(_T("text.dos"));
202 result |= _tunlink(_T("text.nix"));
203 return result;
204 }
205
206 static int test_files(int test_num, char* type)
207 {
208 int result = 0;
209
210 printf("performing test: %d (%s)\n", test_num, type);
211
212 switch (test_num) {
213 case 1:
214 result = test_A(_T("text.dos"), _T("w"), _T("r"), dos_data);
215 break;
216 case 2:
217 result = test_A(_T("binary.dos"), _T("wb"), _T("rb"), dos_data);
218 break;
219 case 3:
220 result = test_A(_T("text.nix"), _T("w"), _T("r"), nix_data);
221 break;
222 case 4:
223 result = test_A(_T("binary.nix"), _T("wb"), _T("rb"), nix_data);
224 break;
225
226 case 5:
227 result = test_B(_T("text.dos"), _T("r"), 166);
228 result = test_B(_T("text.dos"), _T("rb"), TEST_B1_FILE_SIZE);
229 break;
230 case 6:
231 result = test_B(_T("binary.dos"), _T("r"), TEST_B2_FILE_SIZE);
232 result = test_B(_T("binary.dos"), _T("rb"), 166);
233 break;
234 case 7:
235 result = test_B(_T("text.nix"), _T("r"), 162);
236 result = test_B(_T("text.nix"), _T("rb"), TEST_B3_FILE_SIZE);
237 break;
238 case 8:
239 result = test_B(_T("binary.nix"), _T("r"), TEST_B4_FILE_SIZE);
240 result = test_B(_T("binary.nix"), _T("rb"), 162);
241 break;
242
243 case 9:
244 result = test_C();
245 break;
246 case 0:
247 result = test_D();
248 break;
249 case -1:
250 result = clean_files();
251 break;
252 default:
253 _tprintf(_T("no test number selected\n"));
254 break;
255 }
256 return result;
257 }