summaryrefslogtreecommitdiff
path: root/fscanf.c
diff options
context:
space:
mode:
Diffstat (limited to 'fscanf.c')
-rw-r--r--fscanf.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/fscanf.c b/fscanf.c
new file mode 100644
index 0000000..b03589a
--- /dev/null
+++ b/fscanf.c
@@ -0,0 +1,47 @@
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <limits.h>
+#include <unistd.h>
+
+#define TEST(r, f, x, m) ( \
+((r) = (f)) == (x) || \
+(printf(__FILE__ ":%d: %s failed (" m ")\n", __LINE__, #f, r, x, strerror(errno)), err++, 0) )
+
+#define TEST_S(s, x, m) ( \
+!strcmp((s),(x)) || \
+(printf(__FILE__ ":%d: [%s] != [%s] (%s)\n", __LINE__, s, x, m), err++, 0) )
+
+int test_fscanf(void)
+{
+ int i, x, y;
+ int err=0;
+ char a[100], b[100], *s;
+ FILE *f;
+ int p[2];
+
+ TEST(i, pipe(p), 0, "failed to open pipe %d!=%d (%s)");
+ TEST(i, !(f = fdopen(p[0], "rb")), 0, "failed to fdopen pipe %d!=%d (%s)");
+
+ if (!f) {
+ close(p[0]);
+ close(p[1]);
+ return err;
+ }
+
+ TEST(i, write(p[1], "hello, world\n", 13), 13, "write error %d!=%d (%s)");
+ TEST(i, fscanf(f, "%s %[own]", a, b), 2, "got %d fields, expected %d");
+ TEST_S(a, "hello,", "wrong result for %s");
+ TEST_S(b, "wo", "wrong result for %[own]");
+ TEST(i, fgetc(f), 'r', "'%c' != '%c') (%s)");
+
+ TEST(i, write(p[1], " 0x12 0x34", 10), 10, "write error %d!=%d (%s)");
+ TEST(i, fscanf(f, "ld %5i%2i", &x, &y), 1, "got %d fields, expected %d");
+ TEST(i, x, 0x12, "%d != %d");
+ TEST(i, fgetc(f), '3', "'%c' != '%c'");
+
+ fclose(f);
+ close(p[1]);
+
+ return err;
+}