-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathTSTDIO2.C
More file actions
54 lines (52 loc) · 1.83 KB
/
TSTDIO2.C
File metadata and controls
54 lines (52 loc) · 1.83 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
/* test stdio functions, part 2 */
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
int main()
{ /* test basic workings of stdio functions */
char buf[32], tname[L_tmpnam], *tn;
FILE *pf;
static int macs[] = {
_IOFBF, _IOLBF, _IONBF, BUFSIZ, EOF, FILENAME_MAX,
FOPEN_MAX, TMP_MAX, SEEK_CUR, SEEK_END, SEEK_SET};
assert(256 <= BUFSIZ && EOF < 0);
assert(8 <= FOPEN_MAX && 25 <= TMP_MAX);
assert(tmpnam(tname) == tname && strlen(tname) < L_tmpnam);
assert((tn = tmpnam(NULL)) != NULL
&& strcmp(tn, tname) != 0);
pf = fopen(tname, "w");
assert(pf != NULL
&& pf != stdin && pf != stdout && pf != stderr);
assert(feof(pf) == 0 && ferror(pf) == 0);
assert(fgetc(pf) == EOF
&& feof(pf) == 0 && ferror(pf) != 0);
clearerr(pf);
assert(ferror(pf) == 0);
assert(fputc('a', pf) == 'a' && putc('b', pf) == 'b');
assert(0 <= fputs("cde\n", pf));
assert(0 <= fputs("fghij\n", pf));
assert(fflush(pf) == 0);
assert(fwrite("klmnopq\n", 2, 4, pf) == 4);
assert(fclose(pf) == 0);
assert(freopen(tname, "r", stdin) == stdin);
assert(fgetc(stdin) == 'a' && getc(stdin) == 'b');
assert(getchar() == 'c');
assert(fgets(buf, sizeof (buf), stdin) == buf
&& strcmp(buf, "de\n") == 0);
assert(ungetc('x', stdin) == 'x');
assert(gets(buf) == buf && strcmp(buf, "xfghij") == 0);
assert(fread(buf, 2, 4, stdin) == 4
&& strncmp(buf, "klmnopq\n", 8) == 0);
assert(getchar() == EOF && feof(stdin) != 0);
remove(tn);
assert(rename(tname, tn) == 0
&& fopen(tname, "r") == NULL);
assert((pf = fopen(tn, "r")) != NULL && fclose(pf) == 0);
assert(remove(tn) == 0 && fopen(tn, "r") == NULL);
assert((pf = tmpfile()) != NULL && fputc('x', pf) == 'x');
errno = EDOM;
perror("Domain error reported as");
putchar('S'), puts("UCCESS testing <stdio.h>, part 2");
return (0);
}