From: nsz Date: Wed, 3 Aug 2011 06:02:24 +0000 (+0200) Subject: fscanf test from libc-testsuit X-Git-Url: http://nsz.repo.hu/git/?a=commitdiff_plain;h=a95e16c734aa513a11f2a626f8d9230c9a4274dc;p=libc-test fscanf test from libc-testsuit --- diff --git a/src/stdio/fscanf.c b/src/stdio/fscanf.c new file mode 100644 index 0000000..d0c47c7 --- /dev/null +++ b/src/stdio/fscanf.c @@ -0,0 +1,49 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include "test.h" + +#define T(r, f, x, m) do { \ + r = (f); \ + if (r != (x)) \ + error("%s failed (got %d, expected %d, errno \"%s\") (%s)\n", \ + #f, r, x, errno ? strerror(errno) : "", m); \ + errno = 0; \ +} while (0) + +static void S(const char *s, const char *x, const char *m) { + if (strcmp(s, x) != 0) + error("got [%s], expected [%s] (%s)\n", s, x, m); +} + +void test_fscanf(void) { + int i, x, y; + char a[100], b[100]; + int p[2]; + FILE *f; + + T(i, pipe(p), 0, "open pipe"); + T(i, !(f = fdopen(p[0], "rb")), 0, "fdopen pipe"); + if (!f) { + close(p[0]); + close(p[1]); + return; + } + + T(i, write(p[1], "hello, world\n", 13), 13, "write to pipe"); + T(i, fscanf(f, "%s %[own]", a, b), 2, ""); + S(a, "hello,", "wrong result for %s"); + S(b, "wo", "wrong result for %[own]"); + T(i, fgetc(f), 'r', "fgetc 'r'"); + + T(i, write(p[1], " 0x12 0x34", 10), 10, ""); + T(i, fscanf(f, "ld %5i%2i", &x, &y), 1, ""); + T(i, x, 0x12, ""); + T(i, fgetc(f), '3', "fgetc '3'"); + + fclose(f); + close(p[1]); +}