1
/* Test of scanf(): 's' conversion directive.
2
$Id: sscanf-s2.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. */
36
} v[6] = { {1, {1}} };
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); \
75
CHECK (1, !strcmp_P(v[0].s, PSTR("A")), "A", "%1s", v[0].s);
76
CHECK (1, !strcmp_P(v[0].s, PSTR("A")), "ABCD", "%1s", v[0].s);
77
CHECK (1, !strcmp_P(v[0].s, PSTR("AB")), "ABCD", "%2s", v[0].s);
78
CHECK (2, !memcmp_P(v[0].s, PSTR("CD\000AB\000"), 6),
79
"ABCD", "%2s%2s", v[0].s + 3, v[0].s);
80
CHECK (1, !strcmp_P(v[0].s, PSTR("The_quick_b")),
81
"The_quick_brown_fox", "%11s", v[0].s);
83
/* Suppress and width. */
84
CHECK (0, (v[0].s[0] == FILL), "A", "%*1s", v[0].s);
85
CHECK (0, (v[0].s[0] == FILL), "AA", "%*2s", v[0].s);
87
/* Split long string. */
90
!strcmp_P (v[0].s, PSTR("ABCDE"))
91
&& !strcmp_P (v[1].s, PSTR("FGHIJ"))
92
&& !strcmp_P (v[2].s, PSTR("KLMNO"))
93
&& !strcmp_P (v[3].s, PSTR("PQRST"))
94
&& !strcmp_P (v[4].s, PSTR("UVWXY"))
95
&& !strcmp_P (v[5].s, PSTR("Z")),
96
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
97
"%5s%5s %5s %5s\t%5s\n%5s",
98
v[0].s, v[1].s, v[2].s, v[3].s, v[4].s, v[5].s);
102
CHECK (0, (v[0].s[0] == FILL), "A", "%0s", v[0].s);
104
CHECK (1, !strcmp_P(v[0].s, PSTR("A")), "A", "%0s", v[0].s); /* ??? */
107
/* Left width digit is 0. */
110
!strcmp_P(v[0].s, PSTR("A")) && !strcmp_P(v[1].s, PSTR("BC")),
112
"%01s %02s", v[0].s, v[1].s);
114
/* Invalid symbol after '%'. */
115
CHECK (0, (v[0].s[0] == FILL), "A", "% s", v[0].s);
116
CHECK (0, (v[0].s[0] == FILL), "A", "%-s", v[0].s);
117
CHECK (0, (v[0].s[0] == FILL), "A", "%+s", v[0].s);
118
CHECK (0, (v[0].s[0] == FILL), "A", "%.s", v[0].s);
119
CHECK (0, (v[0].s[0] == FILL), "A", "%#s", v[0].s);
121
/* The length modifier. */
122
CHECK (1, !strcmp_P(v[0].s, PSTR("A")), "A", "%hs", v[0].s);
123
CHECK (1, !strcmp_P(v[0].s, PSTR("A")), "A", "%ls", v[0].s);