1
/* Test of sprintf(), float version, '%g(G)' specification.
2
$Id: sprintf_flt-g01.c,v 1.1 2007/02/18 13:45:55 dmix Exp $ */
9
PROGMEM static const struct sprf_s {
15
/* Nonregular cases. */
18
{ "%.8g", 12345678, "12345678" },
19
{ "%.8g", 123456780, "1.2345678e+08" },
20
{ "%.8g", 0.00012345678, "0.00012345678" },
21
{ "%.8g", 0.000012345678, "1.2345678e-05" },
23
/* Flags separatly. */
24
{ "%g", 1.23456e-5, "1.23456e-05" },
25
{ "%G", 1.23456e-5, "1.23456E-05" },
26
{ "%g", -1.23456e-5, "-1.23456e-05" },
27
{ "% g", 1.23456e-5, " 1.23456e-05" },
28
{ "%+g", 1.23456e-5, "+1.23456e-05" },
29
{ "%.4g", 1.23456e-5, "1.235e-05" },
30
{ "%15g", 1.23456e-5, " 1.23456e-05" },
31
{ "%-15g", 1.23456e-5, "1.23456e-05 " },
32
{ "%015g", 1.23456e-5, "00001.23456e-05" },
35
{ "% G", 1.23456e-5, " 1.23456E-05" },
36
{ "% +g", 1.23456e-5, "+1.23456e-05" },
37
{ "% g", -1.23456e-5, "-1.23456e-05" },
38
{ "%+015g", 1.23456e-5, "+0001.23456e-05" },
39
{ "%-015g", 1.23456e-5, "1.23456e-05 " },
42
{ "%.8g", 1.2345678, "1.2345678" },
43
{ "%.7g", 1.2345678, "1.234568" },
44
{ "%.6g", 1.2345678, "1.23457" },
45
{ "%.5g", 1.2345678, "1.2346" },
46
{ "%.4g", 1.2345678, "1.235" },
47
{ "%.3g", 1.2345678, "1.23" },
48
{ "%.2g", 1.2345678, "1.2" },
49
{ "%.1g", 1.2345678, "1" },
50
{ "%.0g", 1.2345678, "1" },
51
{ "%.g", 1.2345678, "1" },
52
{ "%g", 1.2345678, "1.23457" },
55
{ "%.8g", 1.2345678e+12, "1.2345678e+12" },
56
{ "%.7g", 1.2345678e+12, "1.234568e+12" },
57
{ "%.6g", 1.2345678e+12, "1.23457e+12" },
58
{ "%.5g", 1.2345678e+12, "1.2346e+12" },
59
{ "%.4g", 1.2345678e+12, "1.235e+12" },
60
{ "%.3g", 1.2345678e+12, "1.23e+12" },
61
{ "%.2g", 1.2345678e+12, "1.2e+12" },
62
{ "%.1g", 1.2345678e+12, "1e+12" },
63
{ "%.0g", 1.2345678e+12, "1e+12" },
64
{ "%.g", 1.2345678e+12, "1e+12" },
65
{ "%g", 1.2345678e+12, "1.23457e+12" },
67
/* Width, 'f' without dec. point */
71
{ "%12g", 10, " 10" },
72
{ "%12g", -10, " -10" },
74
/* Width, 'f', only fractional part */
75
{ "%.3g", 0.123, "0.123" },
76
{ "%0.3g", 0.123, "0.123" },
77
{ "%5.3g", 0.123, "0.123" },
78
{ "%6.3g", 0.123, " 0.123" },
79
{ "%12.3g", 0.123, " 0.123" },
80
{ "%12.3g", -0.123, " -0.123" },
82
/* Width, 'f', integer and fractional parts */
83
{ "%.5g", 12.345, "12.345" },
84
{ "%0.5g", 12.345, "12.345" },
85
{ "%6.5g", 12.345, "12.345" },
86
{ "%7.5g", 12.345, " 12.345" },
87
{ "%12.5g", 12.345, " 12.345" },
88
{ "%12.5g", -12.345, " -12.345" },
90
/* Width, 'e' without dec. point */
91
{ "%0g", 1e+23, "1e+23" },
92
{ "%5g", 1e+23, "1e+23" },
93
{ "%6g", 1e+23, " 1e+23" },
94
{ "%12g", 1e+23, " 1e+23" },
95
{ "%12g", -1e+23, " -1e+23" },
97
/* Width, 'e' with dec. point */
98
{ "%0g", 1.2e+34, "1.2e+34" },
99
{ "%7g", 1.2e+34, "1.2e+34" },
100
{ "%8g", 1.2e+34, " 1.2e+34" },
101
{ "%12g", 1.2e+34, " 1.2e+34" },
102
{ "%12g", -1.2e+34, " -1.2e+34" },
104
/* In contrast to 'f' spec. */
105
{ "%.3g", 0.46, "0.46" },
106
{ "%.2g", 0.46, "0.46" },
107
{ "%.1g", 0.46, "0.5" },
108
{ "%.0g", 0.46, "0.5" },
109
{ "%3.0g", 0.46, "0.5" },
110
{ "%4.0g", 0.46, " 0.5" },
114
# define strlen_P strlen
117
void run_sprf (const struct sprf_s *pt, int testno)
124
n = sprintf_P (s, pt->fmt, pgm_read_dword (& pt->val));
126
n = sprintf (s, pt->fmt, pt->val);
128
if (n != (int)strlen_P (pt->pattern))
129
code = testno + 1000;
130
else if (strcmp_P (s, pt->pattern))
134
#if !defined(__AVR__)
135
printf ("\ntestno %3d: expect: %3d, \"%s\","
136
"\n output: %3d, \"%s\"\n",
137
testno, strlen(pt->pattern), pt->pattern, n, s);
138
exit (code < 256 ? testno : 255);
148
for (i = 0; (unsigned)i != sizeof(t)/sizeof(t[0]); i++)