|
nsz |
1be84a |
#define _POSIX_C_SOURCE 200809L
|
|
nsz |
1be84a |
#include <stdio.h>
|
|
nsz |
1be84a |
#include <string.h>
|
|
nsz |
1be84a |
#include <errno.h>
|
|
nsz |
1be84a |
#include <limits.h>
|
|
nsz |
1be84a |
#include <unistd.h>
|
|
nsz |
1be84a |
#include "test.h"
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
#define TEST(r, f, x, m) ( \
|
|
nsz |
1be84a |
errno = 0, ((r) = (f)) == (x) || \
|
|
nsz |
1be84a |
(error("%s failed (" m ")\n", #f, r, x, strerror(errno)), 0) )
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
#define TEST_S(s, x, m) ( \
|
|
nsz |
1be84a |
!strcmp((s),(x)) || \
|
|
nsz |
1be84a |
(error("[%s] != [%s] (%s)\n", s, x, m), 0) )
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
void test_ungetc(void) {
|
|
nsz |
1be84a |
int i;
|
|
nsz |
1be84a |
char a[100];
|
|
nsz |
1be84a |
FILE *f;
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
TEST(i, !(f = tmpfile()), 0, "failed to create temp file %d!=%d (%s)");
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
if (!f) return;
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
TEST(i, fprintf(f, "hello, world\n"), 13, "%d != %d (%m)");
|
|
nsz |
1be84a |
TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d (%m)");
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
TEST(i, feof(f), 0, "%d != %d");
|
|
nsz |
1be84a |
TEST(i, fgetc(f), 'h', "'%c' != '%c'");
|
|
nsz |
1be84a |
TEST(i, ftell(f), 1, "%d != %d");
|
|
nsz |
1be84a |
TEST(i, ungetc('x', f), 'x', "%d != %d");
|
|
nsz |
1be84a |
TEST(i, ftell(f), 0, "%d != %d");
|
|
nsz |
1be84a |
TEST(i, fscanf(f, "%[h]", a), 0, "got %d fields, expected %d");
|
|
nsz |
1be84a |
TEST(i, ftell(f), 0, "%d != %d");
|
|
nsz |
1be84a |
TEST(i, fgetc(f), 'x', "'%c' != '%c'");
|
|
nsz |
1be84a |
TEST(i, ftell(f), 1, "%d != %d");
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d");
|
|
nsz |
1be84a |
TEST(i, ungetc('x', f), 'x', "%d != %d");
|
|
nsz |
1be84a |
TEST(i, fread(a, 1, sizeof a, f), 14, "read %d, expected %d");
|
|
nsz |
1be84a |
a[14] = 0;
|
|
nsz |
1be84a |
TEST_S(a, "xhello, world\n", "mismatch reading ungot character");
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d");
|
|
nsz |
1be84a |
TEST(i, fscanf(f, "%[x]", a), 0, "got %d fields, expected %d");
|
|
nsz |
1be84a |
TEST(i, ungetc('x', f), 'x', "unget failed after fscanf: %d != %d");
|
|
nsz |
1be84a |
TEST(i, fgetc(f), 'x', "'%c' != '%c'");
|
|
nsz |
1be84a |
TEST(i, fgetc(f), 'h', "'%c' != '%c'");
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
fclose(f);
|
|
nsz |
1be84a |
}
|