1
/* Test of scanf(): 's' conversion directive.
2
$Id: sscanf-s1.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. */
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); \
74
CHECK (-1, 1, "", "%s", v.c);
75
CHECK (-1, 1, " ", " %s", v.c);
76
CHECK (-1, 1, "\t\n\v\f\r", " %s", v.c);
78
/* Normal conversion. */
79
CHECK (1, !strcmp_P(v.c, PSTR("A")), "A", "%s", v.c);
80
CHECK (1, !strcmp_P(v.c, PSTR("\001")), "\001", "%s", v.c);
81
CHECK (1, !strcmp_P(v.c, PSTR("\377")), "\377", "%s", v.c);
82
CHECK (1, !strcmp_P(v.c, PSTR("ABCD")), "ABCD", "%s", v.c);
85
// CHECK (-1, (v.c[0] == FILL), " ", "%s", v.c);
86
// CHECK (-1, (v.c[0] == FILL), "\t\n\v\f\r", "%s", v.c);
87
CHECK (1, !strcmp_P(v.c, PSTR("A")), " A", "%s", v.c);
88
CHECK (1, !strcmp_P(v.c, PSTR("AB")), "\t\n\v\f\rAB", "%s", v.c);
91
CHECK (1, !strcmp_P(v.c, PSTR("A")), "A B", "%s", v.c);
92
CHECK (1, !strcmp_P(v.c, PSTR("A")), "A\tB", "%s", v.c);
93
CHECK (1, !strcmp_P(v.c, PSTR("A")), "A\nB", "%s", v.c);
94
CHECK (1, !strcmp_P(v.c, PSTR("A")), "A\vB", "%s", v.c);
95
CHECK (1, !strcmp_P(v.c, PSTR("A")), "A\fB", "%s", v.c);
96
CHECK (1, !strcmp_P(v.c, PSTR("A")), "A\rB", "%s", v.c);
98
/* A few conversions. */
99
CHECK (2, !memcmp_P(v.c, PSTR("AB\000CD\000"), 6),
100
"AB CD", "%s%s", v.c, v.c + 3);
105
!memcmp_P (v.c, PSTR("A\000 B\000\nC\000"), 8),
106
"A B\nC", "%s%c%s%c %s",
107
v.c, v.c + 2, v.c + 3, v.c + 5, v.c + 6);
109
/* Suppress a writing. */
110
CHECK (0, (v.c[0] == FILL), "A", "%*s", v.c);
111
CHECK (2, !memcmp_P(v.c, PSTR("A\000C\000"), 4),
112
"A B C", "%s%*s%s", v.c, v.c + 2);