Blame src/stdio/fscanf.c

nsz 086c6a
#include <stdlib.h>
nsz a95e16
#include <stdio.h>
nsz a95e16
#include <string.h>
nsz a95e16
#include <errno.h>
nsz a95e16
#include <limits.h>
nsz a95e16
#include <unistd.h>
nsz a95e16
#include "test.h"
nsz a95e16
nsz a95e16
#define T(r, f, x, m) do { \
nsz a95e16
	r = (f); \
nsz a95e16
	if (r != (x)) \
nsz a95e16
		error("%s failed (got %d, expected %d, errno \"%s\") (%s)\n", \
nsz a95e16
			#f, r, x, errno ? strerror(errno) : "", m); \
nsz a95e16
	errno = 0; \
nsz a95e16
} while (0)
nsz a95e16
nsz a95e16
static void S(const char *s, const char *x, const char *m) {
nsz a95e16
	if (strcmp(s, x) != 0)
nsz a95e16
		error("got [%s], expected [%s] (%s)\n", s, x, m);
nsz a95e16
}
nsz a95e16
nsz 086c6a
void test_scanf_long(void) {
nsz 086c6a
	enum {n = 1<<21};
nsz 086c6a
	char *s = malloc(n+1);
nsz 086c6a
	int i;
nsz 086c6a
	int r;
nsz 086c6a
nsz 086c6a
	for (i = 0; i < n; i++) s[i] = '1';
nsz 086c6a
	s[n] = 0;
nsz 086c6a
	r = sscanf(s, "%d", &i);
nsz 086c6a
	free(s);
nsz 086c6a
}
nsz 086c6a
nsz a95e16
void test_fscanf(void) {
nsz a95e16
	int i, x, y;
nsz a95e16
	char a[100], b[100];
nsz a95e16
	int p[2];
nsz a95e16
	FILE *f;
nsz a95e16
nsz a95e16
	T(i, pipe(p), 0, "open pipe");
nsz a95e16
	T(i, !(f = fdopen(p[0], "rb")), 0, "fdopen pipe");
nsz a95e16
	if (!f) {
nsz a95e16
		close(p[0]);
nsz a95e16
		close(p[1]);
nsz a95e16
		return;
nsz a95e16
	}
nsz a95e16
nsz a95e16
	T(i, write(p[1], "hello, world\n", 13), 13, "write to pipe");
nsz a95e16
	T(i, fscanf(f, "%s %[own]", a, b), 2, "");
nsz a95e16
	S(a, "hello,", "wrong result for %s");
nsz a95e16
	S(b, "wo", "wrong result for %[own]");
nsz a95e16
	T(i, fgetc(f), 'r', "fgetc 'r'");
nsz a95e16
nsz a95e16
	T(i, write(p[1], " 0x12 0x34", 10), 10, "");
nsz a95e16
	T(i, fscanf(f, "ld %5i%2i", &x, &y), 1, "");
nsz a95e16
	T(i, x, 0x12, "");
nsz a95e16
	T(i, fgetc(f), '3', "fgetc '3'");
nsz a95e16
nsz a95e16
	fclose(f);
nsz a95e16
	close(p[1]);
nsz a95e16
}