1
/* Test of sprintf(), float version, '%g(G)' specification.
2
$Id: sprintf_flt-g02.c,v 1.1 2007/02/18 13:46:17 dmix Exp $ */
9
PROGMEM static const struct sprf_s {
15
/* Tailing zeroes removing. */
17
{ "%.7g", 0.000010000002, "1e-05" },
18
{ "%.8g", 0.000010000002, "1.0000002e-05" },
19
{ "%.8g", 0.00001000002, "1.000002e-05" },
20
{ "%.8g", 0.0000100002, "1.00002e-05" },
21
{ "%.8g", 0.000010002, "1.0002e-05" },
22
{ "%.8g", 0.00001002, "1.002e-05" },
23
{ "%.8g", 0.0000102, "1.02e-05" },
24
{ "%.8g", 0.000012, "1.2e-05" },
26
{ "%.7g", 0.00010000002, "0.0001" },
27
{ "%.8g", 0.00010000002, "0.00010000002" },
28
{ "%.8g", 0.0001000002, "0.0001000002" },
29
{ "%.8g", 0.000100002, "0.000100002" },
30
{ "%.8g", 0.00010002, "0.00010002" },
31
{ "%.8g", 0.0001002, "0.0001002" },
32
{ "%.8g", 0.000102, "0.000102" },
33
{ "%.8g", 0.00012, "0.00012" },
35
{ "%.7g", 0.10000002, "0.1" },
36
{ "%.8g", 0.10000002, "0.10000002" },
37
{ "%.8g", 0.1000002, "0.1000002" },
38
{ "%.8g", 0.100002, "0.100002" },
39
{ "%.8g", 0.10002, "0.10002" },
40
{ "%.8g", 0.1002, "0.1002" },
41
{ "%.8g", 0.102, "0.102" },
42
{ "%.8g", 0.12, "0.12" },
44
{ "%.7g", 1.0000002, "1" },
45
{ "%.8g", 1.0000002, "1.0000002" },
46
{ "%.8g", 1.000002, "1.000002" },
47
{ "%.8g", 1.00002, "1.00002" },
48
{ "%.8g", 1.0002, "1.0002" },
49
{ "%.8g", 1.002, "1.002" },
50
{ "%.8g", 1.02, "1.02" },
51
{ "%.8g", 1.2, "1.2" },
53
{ "%.7g", 10.000002, "10" },
54
{ "%.8g", 10.000002, "10.000002" },
55
{ "%.8g", 10.00002, "10.00002" },
56
{ "%.8g", 10.0002, "10.0002" },
57
{ "%.8g", 10.002, "10.002" },
58
{ "%.8g", 10.02, "10.02" },
59
{ "%.8g", 10.2, "10.2" },
60
{ "%.8g", 12., "12" },
62
{ "%.7g", 1000000.2, "1000000" },
63
{ "%.8g", 1000000.2, "1000000.2" },
64
{ "%.8g", 1000002.0, "1000002" },
65
{ "%.8g", 1000020.0, "1000020" },
66
{ "%.8g", 1200000.0, "1200000" },
68
{ "%.7g", 100000020.0, "1e+08" },
69
{ "%.8g", 100000020.0, "1.0000002e+08" },
70
{ "%.8g", 100000200.0, "1.000002e+08" },
71
{ "%.8g", 100002000.0, "1.00002e+08" },
72
{ "%.8g", 100020000.0, "1.0002e+08" },
73
{ "%.8g", 100200000.0, "1.002e+08" },
74
{ "%.8g", 102000000.0, "1.02e+08" },
75
{ "%.8g", 120000000.0, "1.2e+08" },
77
/* Tailing zeroes removing with sign presence. */
79
{ "%.7g", -0.000010000002,"-1e-05" },
80
{ "%.8g", -0.000010000002,"-1.0000002e-05"},
81
{ "%.8g", -0.00001000002, "-1.000002e-05" },
82
{ "%.8g", -0.0000100002, "-1.00002e-05" },
83
{ "%.8g", -0.000010002, "-1.0002e-05" },
84
{ "%.8g", -0.00001002, "-1.002e-05" },
85
{ "%.8g", -0.0000102, "-1.02e-05" },
86
{ "%.8g", -0.000012, "-1.2e-05" },
88
{ "%.7g", -0.10000002, "-0.1" },
89
{ "%.8g", -0.10000002, "-0.10000002" },
90
{ "%.8g", -0.1000002, "-0.1000002" },
91
{ "%.8g", -0.100002, "-0.100002" },
92
{ "%.8g", -0.10002, "-0.10002" },
93
{ "%.8g", -0.1002, "-0.1002" },
94
{ "%.8g", -0.102, "-0.102" },
95
{ "%.8g", -0.12, "-0.12" },
97
{ "%.7g", -1.0000002, "-1" },
98
{ "%.8g", -1.0000002, "-1.0000002" },
99
{ "%.8g", -1.000002, "-1.000002" },
100
{ "%.8g", -1.00002, "-1.00002" },
101
{ "%.8g", -1.0002, "-1.0002" },
102
{ "%.8g", -1.002, "-1.002" },
103
{ "%.8g", -1.02, "-1.02" },
104
{ "%.8g", -1.2, "-1.2" },
106
{ "%.7g", -10.000002, "-10" },
107
{ "%.8g", -10.000002, "-10.000002" },
108
{ "%.8g", -10.00002, "-10.00002" },
109
{ "%.8g", -10.0002, "-10.0002" },
110
{ "%.8g", -10.002, "-10.002" },
111
{ "%.8g", -10.02, "-10.02" },
112
{ "%.8g", -10.2, "-10.2" },
113
{ "%.8g", -12., "-12" },
115
{ "%.7g", -1000000.2, "-1000000" },
116
{ "%.8g", -1000000.2, "-1000000.2" },
117
{ "%.8g", -1000002.0, "-1000002" },
118
{ "%.8g", -1000020.0, "-1000020" },
119
{ "%.8g", -1200000.0, "-1200000" },
121
{ "%.7g", -100000020.0, "-1e+08" },
122
{ "%.8g", -100000020.0, "-1.0000002e+08"},
123
{ "%.8g", -100000200.0, "-1.000002e+08" },
124
{ "%.8g", -100002000.0, "-1.00002e+08" },
125
{ "%.8g", -100020000.0, "-1.0002e+08" },
126
{ "%.8g", -100200000.0, "-1.002e+08" },
127
{ "%.8g", -102000000.0, "-1.02e+08" },
128
{ "%.8g", -120000000.0, "-1.2e+08" },
130
/* Decimal point removing. */
131
{ "%.8g", 2.0000001e-5, "2.0000001e-05" },
132
{ "%.7g", 2.0000001e-5, "2e-05" },
133
{ "%.1g", 2.0000001e-5, "2e-05" },
134
{ "%.7g", 2.0, "2" },
135
{ "%.1g", 2.0, "2" },
136
{ "%.7g", 23.0, "23" },
137
{ "%.2g", 23.0, "23" },
138
{ "%.7g", 234.0, "234" },
139
{ "%.3g", 234.0, "234" },
140
{ "%.7g", 2345.0, "2345" },
141
{ "%.4g", 2345.0, "2345" },
142
{ "%.7g", 23456.0, "23456" },
143
{ "%.5g", 23456.0, "23456" },
144
{ "%.7g", 234567.0, "234567" },
145
{ "%.6g", 234567.0, "234567" },
146
{ "%.7g", 2345678.0, "2345678" },
147
{ "%.1g", 20.0, "2e+01" },
148
{ "%.2g", 200.0, "2e+02" },
149
{ "%.3g", 2000.0, "2e+03" },
150
{ "%.4g", 20000.0, "2e+04" },
151
{ "%.5g", 200000.0, "2e+05" },
152
{ "%.6g", 2000000.0, "2e+06" },
153
{ "%.7g", 20000000.0, "2e+07" },
154
{ "%.8g", 200000000.0, "2e+08" },
156
/* Decimale point removing with sign presence. */
157
{ "%.8g", -2.0000001e-5, "-2.0000001e-05"},
158
{ "%.7g", -2.0000001e-5, "-2e-05" },
159
{ "%.1g", -2.0000001e-5, "-2e-05" },
160
{ "%.7g", -2.0, "-2" },
161
{ "%.1g", -2.0, "-2" },
162
{ "%.7g", -234567.0, "-234567" },
163
{ "%.6g", -234567.0, "-234567" },
164
{ "%.1g", -20.0, "-2e+01" },
165
{ "%.8g", -200000000.0, "-2e+08" },
169
# define strlen_P strlen
172
void run_sprf (const struct sprf_s *pt, int testno)
179
n = sprintf_P (s, pt->fmt, pgm_read_dword (& pt->val));
181
n = sprintf (s, pt->fmt, pt->val);
183
if (n != (int)strlen_P (pt->pattern))
184
code = testno + 1000;
185
else if (strcmp_P (s, pt->pattern))
189
#if !defined(__AVR__)
190
printf ("\ntestno %3d: expect: %3d, \"%s\","
191
"\n output: %3d, \"%s\"\n",
192
testno, strlen(pt->pattern), pt->pattern, n, s);
193
exit (code < 256 ? testno : 255);
203
for (i = 0; (unsigned)i != sizeof(t)/sizeof(t[0]); i++)