Regenerated from source files
[glibc.git] / stdio-common / tst-ungetc.c
blob2cadf1cdaab8c3a69b8ad4bef99c54123411a5d2
1 /* Test for ungetc bugs. */
3 #include <stdio.h>
4 #include <unistd.h>
6 #define assert(x) \
7 if (!(x)) \
8 { \
9 fputs ("test failed: " #x "\n", stderr); \
10 retval = 1; \
11 goto the_end; \
14 int
15 main (int argc, char *argv[])
17 char *name;
18 FILE *fp = NULL;
19 int retval = 0;
20 int c;
21 char buffer[64];
23 name = tmpnam (NULL);
24 fp = fopen (name, "w");
25 assert (fp != NULL)
26 fputs ("bla", fp);
27 fclose (fp);
28 fp = NULL;
30 fp = fopen (name, "r");
31 assert (fp != NULL);
32 assert (ungetc ('z', fp) == 'z');
33 assert (getc (fp) == 'z');
34 assert (getc (fp) == 'b');
35 assert (getc (fp) == 'l');
36 assert (ungetc ('m', fp) == 'm');
37 assert (getc (fp) == 'm');
38 assert ((c = getc (fp)) == 'a');
39 assert (getc (fp) == EOF);
40 assert (ungetc (c, fp) == c);
41 assert (feof (fp) == 0);
42 assert (getc (fp) == c);
43 assert (getc (fp) == EOF);
44 fclose (fp);
45 fp = NULL;
47 fp = fopen (name, "r");
48 assert (fp != NULL);
49 assert (getc (fp) == 'b');
50 assert (getc (fp) == 'l');
51 assert (ungetc ('b', fp) == 'b');
52 assert (fread (buffer, 1, 64, fp) == 2);
53 assert (buffer[0] == 'b');
54 assert (buffer[1] == 'a');
56 the_end:
57 if (fp != NULL)
58 fclose (fp);
59 unlink (name);
61 return retval;