1
/* Test of scanf(): 'x' conversion directive.
2
$Id: sscanf-x3.c,v 1.1.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, (v.i[0] == 9), "9", "%1x", v.i);
78
CHECK (1, (v.i[0] == 9), "98", "%1x", v.i);
79
CHECK (1, (v.i[0] == 0xFE), "FEDC", "%2x", v.i);
80
CHECK (2, (v.i[0] == 0x34) && (v.i[1] == 0x12),
81
"1234", "%2x%2x", v.i + 1, v.i);
82
CHECK (1, (v.i[0] == 0x1234), "000000000012345", "%14x", v.i);
83
CHECK (1, (v.i[0] == 0x1234), "0x0000000012345", "%14x", v.i);
84
CHECK (1, (v.i[0] == 0), "01", "%1x", v.i);
86
/* Width field and spaces. */
87
CHECK (1, (v.i[0] == 0x12), " 12345", "%2x", v.i);
88
CHECK (1, (v.i[0] == 0x12), " \t\n\v\f\r12345", "%2x", v.i);
90
/* Width field and a sign. */
91
CHECK (1, (v.i[0] == 0x12), "+12345", "%3x", v.i);
92
CHECK (1, ((int)v.i[0] == -0x12), "-12345", "%3x", v.i);
94
/* Width, space and sign. */
95
CHECK (1, ((int)v.i[0] == -0x12), " -12345", "%3x", v.i);
97
/* Suppress and width. */
98
CHECK (0, (*(char *)v.i == FILL), "9", "%*1x", v.i);
99
CHECK (0, (*(char *)v.i == FILL), "9876", "%*2x", v.i);