1
/* Test of scanf(): '[' conversion directive.
2
$Id: sscanf_brk-1.c,v 1.2.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. */
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, "", "%[a]", v.c);
75
CHECK (-1, 1, " ", " %[a]", v.c);
76
CHECK (-1, 1, "\t\n\v\f\r", " %[a]", v.c);
78
/* Normal conversion. */
79
CHECK (1, !strcmp_P(v.c, PSTR("A")), "A", "%[A]", v.c);
80
CHECK (1, !strcmp_P(v.c, PSTR("[")), "[", "%[[]", v.c);
81
CHECK (1, !strcmp_P(v.c, PSTR("\001")), "\001", "%[\001]", v.c);
82
CHECK (1, !strcmp_P(v.c, PSTR("\377")), "\377", "%[\377]", v.c);
83
CHECK (1, !strcmp_P(v.c, PSTR("ABCD")), "ABCD", "%[ABCD]", v.c);
85
/* The special symbol as normal. */
86
CHECK (1, !strcmp_P(v.c, PSTR("]")), "]", "%[]]", v.c);
87
CHECK (1, !strcmp_P(v.c, PSTR("]]")), "]]}", "%[]]", v.c);
88
CHECK (1, !strcmp_P(v.c, PSTR("^")), "^", "%[ ^]", v.c);
89
CHECK (1, !strcmp_P(v.c, PSTR("-")), "-", "%[-]", v.c);
90
CHECK (1, !strcmp_P(v.c, PSTR("-a")), "-a", "%[a-]", v.c);
91
CHECK (1, !strcmp_P(v.c, PSTR("-^]")), "-^]", "%[]^-]", v.c);
94
CHECK (1, !strcmp_P(v.c, PSTR("A")), "A A", "%[A]", v.c);
95
CHECK (1, !strcmp_P(v.c, PSTR("A")), "AB", "%[A]", v.c);
96
CHECK (1, !strcmp_P(v.c, PSTR("A")), "Aa", "%[A]", v.c);
99
CHECK (0, v.c[0] == FILL, " A", "%[A]", v.c);
101
/* Spaces in string. */
102
CHECK (1, !strcmp_P(v.c, PSTR(" A")), " A", "%[A ]", v.c);
103
CHECK (1, !strcmp_P(v.c, PSTR("Z A")), "Z A", "%[A Z]", v.c);