1
/* Test of scanf(): 'l' type modifier.
2
$Id: sscanf-l.c,v 1.1.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. */
37
void Check (int line, int expval, int rslt)
41
PRINTF ("\nLine %d: expect= %d, rslt= %d\n", line, expval, rslt);
46
/* The sscanf() is called 4 times: SRAM and FLASH format, 2 values
47
to fill before run. */
48
#define CHECK(expval, ass_expr, str, fmt, ...) \
50
PROGMEM static char fmt_p[] = fmt; \
55
int (* volatile vp)(const char *, const char *, ...); \
57
ASSERT (sizeof(str_s) >= sizeof(str)); \
58
ASSERT (sizeof(fmt_s) >= sizeof(fmt_p)); \
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); \
76
CHECK (-1, (*(char *)v.i == FILL), "", "%ld", v.i);
77
CHECK (-1, (*(char *)v.i == FILL), "", " %ld", v.i);
78
CHECK (-1, (*(char *)v.i == FILL), " ", " %ld", v.i);
79
CHECK (-1, (*(char *)v.i == FILL), " ", " %ld", v.i);
80
CHECK (-1, (*(char *)v.i == FILL), "\t\n\v\f\r", " %ld", v.i);
82
/* Normal conversion. */
83
CHECK (1, (v.i[0] == 1234567890) && (*(char *)(v.i + 1) == FILL),
84
"1234567890", "%ld", v.i);
85
CHECK (1, ((unsigned long)(v.i[0]) == 4111222333)
86
&& (*(char *)(v.i + 1) == FILL),
87
"4111222333", "%lu", v.i);
88
CHECK (1, (v.i[0] == 012345670123) && (*(char *)(v.i + 1) == FILL),
89
"012345670123", "%li", v.i);
90
CHECK (1, ((unsigned long)(v.i[0]) == 021076543210)
91
&& (*(char *)(v.i + 1) == FILL),
92
"21076543210", "%lo", v.i);
93
CHECK (1, (v.i[0] == 0x12345678) && (*(char *)(v.i + 1) == FILL),
94
"12345678", "%lx", v.i);
95
CHECK (1, (v.i[0] == 0x09ABCDEF) && (*(char *)(v.i + 1) == FILL),
96
"0x09ABCDEF", "%lX", v.i);
97
CHECK (1, (v.i[0] == 0x01234567) && (*(char *)(v.i + 1) == FILL),
98
"1234567", "%lp", v.i);
100
CHECK (0, (n == 1) && (*(char *)(v.i) == FILL),
103
/* All possible conversions. */
106
v.i[0] == 10 && v.i[1] == 20 && v.i[2] == 030 && v.i[3] == 040
107
&& v.i[4] == 0x50 && v.i[5] == 0x60 && v.i[6] == 0x70 && v.i[7] == 22
109
"10 20 030 40 50 60 70 80",
110
"%ld %lu %li %lo %lx %lX %lp %ln %ld",
111
v.i + 0, v.i + 1, v.i + 2, v.i + 3,
112
v.i + 4, v.i + 5, v.i + 6, v.i + 7,
118
v.i[0] == 12 && *(unsigned char *)(v.i + 1) == FILL,
122
v.i[0] == 98 && *(unsigned char *)(v.i + 1) == FILL,
123
"00000000009876", "%12ld", v.i);
125
/* Suppress a writing. */
126
CHECK (0, (*(unsigned char *)v.i == FILL), "123", "%*ld", v.i);
127
CHECK (2, (v.i[0] == 1) && (v.i[1] == 3) && (*(char *)(v.i + 2) == FILL),
128
"1 2 3", "%ld%*ld%ld", v.i, v.i + 1);