1
/* Test of scanf(): 'o' conversion directive.
2
$Id: sscanf-o1.c,v 1.2.2.3 2008/03/20 21:42:34 joerg_wunsch Exp $ */
10
# define ASSERT(expr) \
12
if (!(expr)) exit(__LINE__); \
14
# define EXIT(v) exit (v)
15
# if defined(__AVR_ATmega128__)
16
/* ATmega128 has enough RAM for sprintf(), print to 0x2000 in XRAM. */
17
# define PRINTF(f...) sprintf((char *)0x2000, f)
24
# define ASSERT(expr) assert (expr)
25
# define EXIT(v) exit ((v) < 256 ? (v) : 255)
26
# define PRINTF(f...) printf (f)
27
# define sscanf_P sscanf
28
# define memcmp_P memcmp
31
/* Next variables are useful to debug the AVR. */
38
void Check (int line, int expval, int rslt)
42
PRINTF ("\nLine %d: expect= %d, rslt= %d\n", line, expval, rslt);
47
/* The sscanf() is called 4 times: SRAM and FLASH format, 2 values
48
to fill before run. */
49
#define CHECK(expval, ass_expr, str, fmt, ...) \
51
PROGMEM static char fmt_p[] = fmt; \
56
int (* volatile vp)(const char *, const char *, ...); \
58
ASSERT (sizeof(str_s) >= sizeof(str)); \
59
ASSERT (sizeof(fmt_s) >= sizeof(fmt_p)); \
60
strcpy_P (str_s, PSTR(str)); \
61
strcpy_P (fmt_s, fmt_p); \
63
for (FILL = 0; FILL < 4; FILL++) { \
64
memset (&v, FILL, sizeof(v)); \
65
vp = (FILL & 1) ? sscanf_P : sscanf; \
66
i = vp (str_s, (FILL & 1) ? fmt_p : fmt_s, ##__VA_ARGS__); \
67
Check (__LINE__, expval, i); \
72
#define PVEC(args...) ({ static int __x[] PROGMEM = {args}; __x; })
77
CHECK (-1, (*(char *)v.i == FILL), "", "%o", v.i);
78
CHECK (-1, (*(char *)v.i == FILL), "", " %o", v.i);
79
CHECK (-1, (*(char *)v.i == FILL), " ", " %o", v.i);
80
CHECK (-1, (*(char *)v.i == FILL), " ", " %o", v.i);
81
CHECK (-1, (*(char *)v.i == FILL), "\t\n\v\f\r", " %o", v.i);
83
/* Normal conversion. */
84
CHECK (1, (v.i[0] == 0), "0", "%o", v.i);
89
PVEC (0, 0, 1, 0123456, 077777, 0100000, 0177777, -1,
92
"+0 -0 1 123456 77777 100000 177777 -1 +12345 -7654",
93
"%o %o %o %o %o %o %o %o %o %o",
100
/* Leading spaces. */
101
CHECK (1, (v.i[0] == 012), " 12", "%o", v.i);
102
CHECK (1, (v.i[0] == 0123), "\t\n\v\f\r123", "%o", v.i);
105
CHECK (1, (v.i[0] == 04567), "04567", "%o", v.i);
106
CHECK (1, (v.i[0] == 07654), "00000000007654", "%o", v.i);
109
CHECK (2, v.i[0] == 0 && v.s[0] == 'x', "0x1", "%o%c", v.i, v.s);
112
!memcmp_P (v.i, PVEC (1,2,3,4,5,6,7), 7 * sizeof(int))
113
&& !memcmp_P (v.s, PSTR(" \n.89\001\377"), 7),
114
"1 2\n3.48596\0017\3776",
115
"%o%c%o%c%o%c%o%c%o%c%o%c%o%c",
125
!memcmp_P (v.i, PVEC (1, -2, 3), 3 * sizeof(int)),
126
"1-2+3", "%o%o%o", v.i, v.i + 1, v.i + 2);