-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_stdio.c
More file actions
356 lines (274 loc) · 5.86 KB
/
Copy pathtest_stdio.c
File metadata and controls
356 lines (274 loc) · 5.86 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright 2022 Mark Seminatore. All rights reserved.
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "errno.h"
#if defined(_WIN32)
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
#else
#include "unistd.h"
#endif
#include "testy/test.h"
//
static void test_puts()
{
SUITE("puts");
TEST(1 == puts("Hello"));
}
//
static void test_open()
{
char str[] = "Hello there!\n";
int len = strlen(str);
SUITE("open");
#if defined(_WIN32)
int fd = open("./a.txt", O_CREAT | O_WRONLY, S_IWRITE | S_IREAD);
#else
int fd = open("./a.txt", O_CREAT | O_WRONLY, S_IRWXU);
#endif
TEST(fd > 0);
TEST(len == write(fd, str, len));
close(fd);
remove("./a.txt");
}
//
static void test_fputs()
{
SUITE("fputs");
TEST(fputs("Hello!", stdout));
}
//
static void test_sprintf()
{
char str[256];
SUITE("sprintf");
sprintf(str, "Hi %s, is it %d o'clock yet%c%c", "there", 1, '?', '!');
TEST(0 == strcmp("Hi there, is it 1 o'clock yet?!", str));
}
//
static void test_fprintf()
{
SUITE("fprintf");
int result = fprintf(stdout, "%s", "Hello");
TEST(5 == result);
}
//
static void test_fwrite()
{
SUITE("fwrite");
FILE *f = fopen("./write_test.txt", "wt");
TEST(f != NULL);
char str[] = "Hello there!";
TEST(1 == fwrite(str, strlen(str), 1, f));
fclose(f);
}
//
static void test_fread()
{
SUITE("fread");
FILE *f = fopen("./write_test.txt", "rt");
TEST(f != NULL);
char str[] = "Hello there!";
char buf[32] = {0};
TEST(1 == fread(buf, strlen(str), 1, f));
TEST(0 == strcmp(str, buf));
fclose(f);
}
//
static void test_fgets()
{
SUITE("fgets");
FILE *f = fopen("./write_test.txt", "rt");
TEST(f != NULL);
char str[] = "Hello there!";
char buf[32] = {0};
TEST(buf == fgets(buf, sizeof(buf), f));
TEST(0 == strcmp(str, buf));
fclose(f);
}
//
static void test_gets()
{
SUITE("gets");
char str[] = "Hello there!";
char buf[32] = {0};
SKIP_TEST(buf == gets(buf));
SKIP_TEST(0 == strcmp(str, buf));
}
//
static void test_feof()
{
SUITE("feof");
// write a small file to read from
FILE *f = fopen("./feof_test.txt", "wt");
fwrite("Hi", 1, 2, f);
fclose(f);
f = fopen("./feof_test.txt", "rt");
TEST(f != NULL);
// fresh stream: not at EOF
TEST(0 == feof(f));
// read the two bytes, then one past end to trigger EOF
fgetc(f);
fgetc(f);
fgetc(f);
TEST(0 != feof(f));
// clearerr resets the EOF indicator
clearerr(f);
TEST(0 == feof(f));
fclose(f);
remove("./feof_test.txt");
}
//
static void test_ferror()
{
SUITE("ferror");
FILE *f = fopen("./ferror_test.txt", "wt");
fwrite("Hi", 1, 2, f);
fclose(f);
f = fopen("./ferror_test.txt", "rt");
TEST(f != NULL);
// fresh stream: no error
TEST(0 == ferror(f));
// read past end; status=EOF=-1, which != 0, so ferror returns non-zero
fgetc(f);
fgetc(f);
fgetc(f);
TEST(0 != ferror(f));
// clearerr resets the error indicator
clearerr(f);
TEST(0 == ferror(f));
fclose(f);
remove("./ferror_test.txt");
}
//
static void test_clearerr()
{
SUITE("clearerr");
FILE *f = fopen("./clearerr_test.txt", "wt");
fwrite("Hi", 1, 2, f);
fclose(f);
f = fopen("./clearerr_test.txt", "rt");
TEST(f != NULL);
// clearerr on a clean stream leaves it clean
clearerr(f);
TEST(0 == feof(f));
TEST(0 == ferror(f));
// read past end to set EOF, then clearerr clears both indicators
fgetc(f);
fgetc(f);
fgetc(f);
TEST(0 != feof(f));
clearerr(f);
TEST(0 == feof(f));
TEST(0 == ferror(f));
fclose(f);
remove("./clearerr_test.txt");
}
//
static void test_remove()
{
SUITE("remove");
char filename[] = "write_test.txt";
TEST(0 == remove(filename));
}
//
static void test_rename()
{
SUITE("rename");
// success: create a file, rename it, verify return is 0 and new name is accessible
FILE *f = fopen("./rename_old.txt", "wt");
TEST(f != NULL);
fwrite("data", 1, 4, f);
fclose(f);
TEST(0 == rename("./rename_old.txt", "./rename_new.txt"));
FILE *g = fopen("./rename_new.txt", "rt");
TEST(g != NULL);
fclose(g);
remove("./rename_new.txt");
// non-existent source returns non-zero
TEST(0 != rename("./rename_does_not_exist.txt", "./rename_out.txt"));
// NULL args trigger assert before graceful return; in NDEBUG builds assert is a no-op
// so the graceful if-check runs and returns -1
#ifdef NDEBUG
TEST(-1 == rename(NULL, "./rename_new.txt"));
TEST(-1 == rename("./rename_old.txt", NULL));
#else
SKIP_TEST(-1 == rename(NULL, "./rename_new.txt"));
SKIP_TEST(-1 == rename("./rename_old.txt", NULL));
#endif
}
//
static void test_perror()
{
SUITE("perror");
// perror writes "prefix: message\n" to stderr; verify it doesn't crash
errno = EINVAL;
perror("test");
TEST(1); // reached here without crashing
errno = 0;
perror("no error");
TEST(1);
// NULL arg triggers assert; skip in debug, run in release
#ifdef NDEBUG
perror(NULL);
TEST(1);
#else
SKIP_TEST(perror(NULL));
#endif
}
//
static void test_fseek()
{
SUITE("fseek/ftell/rewind");
// write a known file to test seeking within
FILE *f = fopen("./fseek_test.txt", "wt");
TEST(f != NULL);
char content[] = "ABCDEFGHIJ";
fwrite(content, 1, 10, f);
fclose(f);
f = fopen("./fseek_test.txt", "rt");
TEST(f != NULL);
// ftell at start should be 0
TEST(0 == ftell(f));
// seek to offset 3 from start
TEST(0 == fseek(f, 3, SEEK_SET));
TEST(3 == ftell(f));
// read one byte — should be 'D' (4th char, index 3)
char buf[4] = {0};
fread(buf, 1, 1, f);
TEST('D' == buf[0]);
// position should now be 4
TEST(4 == ftell(f));
// seek 2 forward from current
TEST(0 == fseek(f, 2, SEEK_CUR));
TEST(6 == ftell(f));
// rewind should reset to 0
rewind(f);
TEST(0 == ftell(f));
fclose(f);
remove("./fseek_test.txt");
}
//
// Run all stdio tests.
//
void test_stdio()
{
test_puts();
test_open();
test_fputs();
test_sprintf();
test_fprintf();
test_fwrite();
test_fread();
test_fgets();
test_gets();
test_feof();
test_ferror();
test_clearerr();
test_remove();
test_rename();
test_perror();
test_fseek();
}