add test for strftime
[libc-test.git] / src / functional / ungetc.c
blob97bc3c60a70292a6aa4aa796b19cbf4532eb6af9
1 #include <stdio.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <limits.h>
5 #include <unistd.h>
6 #include "test.h"
8 #define TEST(r, f, x, m) ( \
9 errno = 0, ((r) = (f)) == (x) || \
10 (t_error("%s failed (" m ")\n", #f, r, x, strerror(errno)), 0) )
12 #define TEST_S(s, x, m) ( \
13 !strcmp((s),(x)) || \
14 (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
16 int main(void)
18 int i;
19 char a[100];
20 FILE *f;
22 TEST(i, !(f = tmpfile()), 0, "failed to create temp file %d!=%d (%s)");
24 if (!f) return t_status;
26 TEST(i, fprintf(f, "hello, world\n"), 13, "%d != %d (%m)");
27 TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d (%m)");
29 TEST(i, feof(f), 0, "%d != %d");
30 TEST(i, fgetc(f), 'h', "'%c' != '%c'");
31 TEST(i, ftell(f), 1, "%d != %d");
32 TEST(i, ungetc('x', f), 'x', "%d != %d");
33 TEST(i, ftell(f), 0, "%d != %d");
34 TEST(i, fscanf(f, "%[h]", a), 0, "got %d fields, expected %d");
35 TEST(i, ftell(f), 0, "%d != %d");
36 TEST(i, fgetc(f), 'x', "'%c' != '%c'");
37 TEST(i, ftell(f), 1, "%d != %d");
39 TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d");
40 TEST(i, ungetc('x', f), 'x', "%d != %d");
41 TEST(i, fread(a, 1, sizeof a, f), 14, "read %d, expected %d");
42 a[14] = 0;
43 TEST_S(a, "xhello, world\n", "mismatch reading ungot character");
45 TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d");
46 TEST(i, fscanf(f, "%[x]", a), 0, "got %d fields, expected %d");
47 TEST(i, ungetc('x', f), 'x', "unget failed after fscanf: %d != %d");
48 TEST(i, fgetc(f), 'x', "'%c' != '%c'");
49 TEST(i, fgetc(f), 'h', "'%c' != '%c'");
51 fclose(f);
52 return t_status;