File: scanf.5c

package info (click to toggle)
nickle 2.107
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,756 kB
  • sloc: ansic: 27,954; yacc: 1,874; lex: 954; sh: 204; makefile: 13; lisp: 1
file content (50 lines) | stat: -rw-r--r-- 1,287 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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);