1
/* Test of scanf(): 'x' conversion directive.
2
$Id: sscanf-x1.c,v 1.2.2.3 2008/03/20 21:42:35 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), "", "%x", v.i);
78
CHECK (-1, (*(char *)v.i == FILL), "", " %x", v.i);
79
CHECK (-1, (*(char *)v.i == FILL), " ", " %x", v.i);
80
CHECK (-1, (*(char *)v.i == FILL), " ", " %x", v.i);
81
CHECK (-1, (*(char *)v.i == FILL), "\t\n\v\f\r", " %x", v.i);
83
/* Normal conversion. */
84
CHECK (1, (v.i[0] == 0), "0", "%x", v.i);
86
CHECK (1, (v.i[0] == 0), "+0", "%x", v.i);
87
CHECK (1, (v.i[0] == 0), "-0", "%x", v.i);
88
CHECK (1, (v.i[0] == 1), "1", "%x", v.i);
89
CHECK (1, (v.i[0] == 0x1234), "1234", "%x", v.i);
90
CHECK (1, (v.i[0] == 0x5678), "5678", "%x", v.i);
91
CHECK (1, (v.i[0] == 0x9ABC), "9ABC", "%x", v.i);
92
CHECK (1, (v.i[0] == 0xDEF0), "DEF0", "%x", v.i);
93
CHECK (1, (v.i[0] == 0xFFFF), "FFFF", "%x", v.i);
94
CHECK (1, (v.i[0] == (unsigned)(-1)), "-1", "%x", v.i);
95
CHECK (1, (v.i[0] == 1), "-FFFFFFFF", "%x", v.i);
101
PVEC (0, 0, 1, 0x1234, 0x5678, 0x9ABC, 0xDEF0, 0xFFFF, -1, 1),
103
"+0 -0 1 1234 5678 9ABC DEF0 FFFF -1 -FFFFFFFF",
104
"%x %x %x %x %x %x %x %x %x %x",
112
/* Low/upper characters. */
117
PVEC(0xABCD, 0xEF, 0xABCD, 0xEF, 0xABCD, 0xEF, 0xABCD, 0xEF),
119
"abcd ef abcd ef ABCD EF AbCd eF",
120
"%x %x %X %X %X %X %x %X",
126
/* '%p' conversion. */
129
!memcmp_P (v.i, PVEC(0xABCD, -0x12EF, 0x10), 3 * sizeof(int)),
131
"%p%p%p", v.i, v.i + 1, v.i + 2);
133
/* Leading spaces. */
134
CHECK (1, (v.i[0] == 0x12), " 12", "%x", v.i);
135
CHECK (1, (v.i[0] == 0x123), "\t\n\v\f\r123", "%x", v.i);
138
CHECK (1, (v.i[0] == 04567), "04567", "%o", v.i);
139
CHECK (1, (v.i[0] == 07654), "00000000007654", "%o", v.i);
143
6, !memcmp_P (v.i, PVEC(1,2,3,4,0xA987,-0x100), 6 * sizeof(int)),
144
"0x1 0X2 0x3 0X4 0xA987 -0x100",