int errors = 0;

void check(string input, string format, int count, poly value) {
    poly got_value;
    int	got_count = File::fscanf(File::string_read(input), format, &got_value);
    if (got_count != count) {
	printf ("got %d wanted %d. input %s format %s\n",
		got_count, count, input, format);
	errors++;
	return;
    }
    if (count != 0) {
	if (got_value != value) {
	    printf ("read %v wanted %v. input %s format %s\n",
		    got_value, value, input, format);
	    errors++;
	}
    }
}

string[] int_formats={ "%b", "%o", "%d", "%x", "%e", "%f", "%g" };
for (int i = 0; i < dim(int_formats); i++) {
    check("0", int_formats[i], 1, 0);
    check(" 0", int_formats[i], 1, 0);
    check("1", "%x", 1, 1);
    check(" 1", "%x", 1, 1);
    check("", "%x", 0, 1);
    check(" ", "%x", 0, 1);
}

string[] real_formats={ "%e", "%f", "%g" };

typedef struct {
    string	input;
    real	value;
} real_t;

real_t[] real_tests = {
    { .input = ".{3}", .value = 1/3 },
    { .input = "1.0e-30", value = 1e-30 },
    { .input = "1e30", .value = 1e30 },
    { .input = "0x1.1p2", .value = 4.25 },
};

for (int i = 0; i < dim(real_formats); i++) {
    for (int t = 0; t < dim(real_tests); t++) {
	check(real_tests[t].input, real_formats[i], 1, real_tests[t].value);
    }
}
exit(errors);
