1
/* Test of scanf(): 'c' conversion directive.
2
$Id: sscanf-c1.c,v 1.1.2.3 2008/03/20 21:42:33 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, "", "%c", v.s);
75
CHECK (-1, 1, "", " %c", v.s);
76
CHECK (-1, 1, " ", " %c", v.s);
77
CHECK (-1, 1, " ", " %c", v.s);
78
CHECK (-1, 1, "\t\n\v\f\r", " %c", v.s);
80
/* Normal conversion. */
81
CHECK (1, (v.s[0] == 'a'), "a", "%c", v.s);
82
CHECK (3, !memcmp_P (v.s, PSTR(" \001\377"), 3),
83
" \001\377", "%c%c%c", v.s, v.s + 1, v.s + 2);
84
CHECK (4, !memcmp_P (v.s, PSTR("DCBA"), 4),
85
"ABCD", "%c%c%c%c", v.s + 3, v.s + 2, v.s + 1, v.s);
88
CHECK (1, (v.s[0] == 'q'), "%The %q", "%%The %%%c", v.s);
90
/* Suppress a writing. */
91
CHECK (0, (v.s[0] == FILL), "a", "%*c", v.s);
92
CHECK (3, !memcmp_P(v.s, PSTR("ACD"), 3),
93
"ABCD", "%c%*c%c%c", v.s, v.s + 1, v.s + 2);
96
CHECK (1, (v.s[0] == 'A'), "A", "%1c", v.s);
97
CHECK (1, !memcmp_P(v.s, PSTR("AB"), 2), "AB", "%2c", v.s);
98
CHECK (1, !memcmp_P(v.s, PSTR("The_quick_br"), 12),
99
"The_quick_brown_fox", "%12c", v.s);
100
CHECK (1, !memcmp_P(v.s, PSTR("A\t D"), 4), "A\t D", "%4c", v.s);
101
CHECK (2, !memcmp_P(v.s, PSTR("3412"), 4),
102
"1234", "%2c%2c", v.s + 2, v.s);
104
/* Suppress and width. */
105
CHECK (0, (v.s[0] == FILL), "A", "%*1c", v.s);
106
CHECK (0, (v.s[0] == FILL), "AA", "%*2c", v.s);