aboutsummaryrefslogtreecommitdiff
path: root/src/lib9/testfltfmt.c
blob: 540504225970a97a176f2d6fee511ebffe0f5fe7 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <u.h>
#include <libc.h>
#include <stdio.h>

/*
 * try all combination of flags and float conversions
 * with some different widths & precisions
 */

#define Njust 2
#define Nplus 3
#define Nalt 2
#define Nzero 2
#define Nspec 5
#define Nwidth 5
#define Nprec 5

static double fmtvals[] = {
	3.1415925535897932e15,
	3.1415925535897932e14,
	3.1415925535897932e13,
	3.1415925535897932e12,
	3.1415925535897932e11,
	3.1415925535897932e10,
	3.1415925535897932e9,
	3.1415925535897932e8,
	3.1415925535897932e7,
	3.1415925535897932e6,
	3.1415925535897932e5,
	3.1415925535897932e4,
	3.1415925535897932e3,
	3.1415925535897932e2,
	3.1415925535897932e1,
	3.1415925535897932e0,
	3.1415925535897932e-1,
	3.1415925535897932e-2,
	3.1415925535897932e-3,
	3.1415925535897932e-4,
	3.1415925535897932e-5,
	3.1415925535897932e-6,
	3.1415925535897932e-7,
	3.1415925535897932e-8,
	3.1415925535897932e-9,
	3.1415925535897932e-10,
	3.1415925535897932e-11,
	3.1415925535897932e-12,
	3.1415925535897932e-13,
	3.1415925535897932e-14,
	3.1415925535897932e-15,
};

/*
 * are the numbers close?
 * used to compare long numbers where the last few digits are garbage
 * due to precision problems
 */
static int
numclose(char *num1, char *num2)
{
	int ndig;
	enum { MAXDIG = 15 };

	ndig = 0;
	while (*num1) {
		if (*num1 >= '0' && *num1 <= '9') {
			ndig++;
			if (ndig > MAXDIG) {
				if (!(*num2 >= '0' && *num2 <= '9')) {
					return 0;
				}
			} else if (*num1 != *num2) {
				return 0;
			}
		} else if (*num1 != *num2) {
			return 0;
		} else if (*num1 == 'e' || *num1 == 'E') {
			ndig = 0;
		}
		num1++;
		num2++;
	}
	if (*num1 || !num2)
		return 0;
	return 1;
}

static void
doit(int just, int plus, int alt, int zero, int width, int prec, int spec)
{
	char format[256];
	char *p;
	const char *s;
	int i;

	p = format;
	*p++ = '%';
	if (just > 0)
		*p++ = "-"[just - 1];
	if (plus > 0)
		*p++ = "+ "[plus - 1];
	if (alt > 0)
		*p++ = "#"[alt - 1];
	if (zero > 0)
		*p++ = "0"[zero - 1];

	s = "";
	switch (width) {
	case 1: s = "1"; break;
	case 2: s = "5"; break;
	case 3: s = "10"; break;
	case 4: s = "15"; break;
	}
	strcpy(p, s);

	s = "";
	switch (prec) {
	case 1: s = ".0"; break;
	case 2: s = ".2"; break;
	case 3: s = ".5"; break;
	case 4: s = ".15"; break;
	}
	strcat(p, s);

	p = strchr(p, '\0');
	*p++ = "efgEG"[spec];
	*p = '\0';

	for (i = 0; i < sizeof(fmtvals) / sizeof(fmtvals[0]); i++) {
		char ref[256], buf[256];
		Rune rbuf[256];

		sprintf(ref, format, fmtvals[i]);
		snprint(buf, sizeof(buf), format, fmtvals[i]);
		if (strcmp(ref, buf) != 0
		&& !numclose(ref, buf)) {
			fprintf(stderr, "%s: ref='%s' fmt='%s'\n", format, ref, buf);
			exit(1);
		}

		/* Check again with output to rune string */
		runesnprint(rbuf, 256, format, fmtvals[i]);
		snprint(buf, sizeof(buf), "%S", rbuf);
		if (strcmp(ref, buf) != 0
		&& !numclose(ref, buf)) {
			fprintf(stderr, "%s: rune ref='%s' fmt='%s'\n", format, ref, buf);
			exits("oops");
		}
	}
}

void
main(int argc, char **argv)
{
	int just, plus, alt, zero, width, prec, spec;

	for (just = 0; just < Njust; just++)
	for (plus = 0; plus < Nplus; plus++)
	for (alt = 0; alt < Nalt; alt++)
	for (zero = 0; zero < Nzero; zero++)
	for (width = 0; width < Nwidth; width++)
	for (prec = 0; prec < Nprec; prec++)
	for (spec = 0; spec < Nspec; spec++)
		doit(just, plus, alt, zero, width, prec, spec);

	exits(0);
}