1
/* Test of scanf(): float conversion.
2
$Id: sscanf_flt-fnn.c,v 1.2.2.3 2008/03/20 21:42:36 joerg_wunsch Exp $ */
11
# define ASSERT(expr) \
13
if (!(expr)) exit(__LINE__); \
15
# define EXIT(v) exit (v)
16
# if defined(__AVR_ATmega128__)
17
/* ATmega128 has enough RAM for sprintf(), print to 0x2000 in XRAM. */
18
# define PRINTF(f...) sprintf((char *)0x2000, f)
25
# define ASSERT(expr) assert (expr)
26
# define EXIT(v) exit ((v) < 256 ? (v) : 255)
27
# define PRINTF(f...) printf (f)
28
# define sscanf_P sscanf
29
# define memcmp_P memcmp
32
/* Next variables are useful to debug the AVR. */
39
void Check (int line, int expval, int rslt)
43
PRINTF ("\nLine %d: expect= %d, rslt= %d\n", line, expval, rslt);
48
/* The sscanf() is called 4 times: SRAM and FLASH format, 2 values
49
to fill before run. */
50
#define CHECK(expval, ass_expr, str, fmt, ...) \
52
PROGMEM static char fmt_p[] = fmt; \
57
int (* volatile vp)(const char *, const char *, ...); \
59
strcpy_P (str_s, PSTR(str)); \
60
strcpy_P (fmt_s, fmt_p); \
62
for (FILL = 0; FILL < 4; FILL++) { \
63
memset (&v, FILL, sizeof(v)); \
64
vp = (FILL & 1) ? sscanf_P : sscanf; \
65
i = vp (str_s, (FILL & 1) ? fmt_p : fmt_s, ##__VA_ARGS__); \
66
Check (__LINE__, expval, i); \
71
int all_nans (const float v[], int n)
74
for (i = 0; i < n; i++) {
83
/* No valid digits. */
84
CHECK (0, 1, ". ", "%e", & v.x[0]);
85
CHECK (0, 1, "+ ", "%e", & v.x[0]);
86
CHECK (0, 1, "- ", "%e", & v.x[0]);
87
CHECK (0, 1, "E ", "%e", & v.x[0]);
88
CHECK (0, 1, "+-123", "%e", & v.x[0]);
89
CHECK (0, 1, "-+123", "%e", & v.x[0]);
90
CHECK (0, 1, ".-123", "%e", & v.x[0]);
91
CHECK (0, 1, ".+123", "%e", & v.x[0]);
92
CHECK (0, 1, "e12", "%e", & v.x[0]);
93
CHECK (0, 1, "- 1", "%e", & v.x[0]);
94
CHECK (0, 1, "IN ", "%e", & v.x[0]);
95
CHECK (0, 1, "NA ", "%e", & v.x[0]);
96
CHECK (0, 1, "infinit ", "%e", & v.x[0]);
102
"nan NAN NaN -nan +NAN -nAn",
104
& v.x[0], & v.x[1], & v.x[2], & v.x[3], & v.x[4], & v.x[5]);
106
/* Character after NaN. */
109
all_nans (v.x, 6) && !memcmp_P (v.c, PSTR (".+eQ"), 4),
110
"nan. nan+ nane NANQ nannan",
111
"%e%c %e%c %e%c %e%c %e %e",
112
& v.x[0], & v.c[0], & v.x[1], & v.c[1], & v.x[2], & v.c[2],
113
& v.x[3], & v.c[3], & v.x[4], & v.x[5]);
120
({ static float __x[] PROGMEM = {
121
INFINITY, INFINITY, INFINITY,
122
INFINITY, -INFINITY, -INFINITY
127
"INF inf +Inf INFINITY -infinity -InFiNiTy",
129
& v.x[0], & v.x[1], & v.x[2], & v.x[3], & v.x[4], & v.x[5]);
131
/* Character after Inf. */
136
({ static float __x[] PROGMEM = {
137
INFINITY, INFINITY, INFINITY,
138
INFINITY, INFINITY, -INFINITY
143
&& !memcmp_P (v.c, PSTR (" s\b"), 3),
144
"inf infinityinfinitys INF\b inf-inf",
145
"%e%c %e%e%c %e%c %e%e",