1
/* Test of sprintf_min(), specification: x.
2
$Id: sprintf_min-4.c,v 1.1.2.1 2008/03/20 21:42:31 joerg_wunsch Exp $ */
5
# define PRINTFLN(line, fmt, ...) \
6
printf("\nLine %2d: " fmt "\n", line, ##__VA_ARGS__)
7
# define EXIT(code) exit ((code) < 255 ? (code) : 255)
8
# define sprintf_P sprintf
10
# if defined(__AVR_ATmega128__)
11
/* ATmega128 has enough RAM for sprintf(), print to 0x2000 in XRAM. */
12
# define PRINTFLN(line, fmt, ...) \
13
sprintf ((char *)0x2000, "\nLine %d: " fmt "\n", line, ##__VA_ARGS__)
16
# define PRINTFLN(args...)
28
int retval, const char *retstr)
32
if (retval != (int)strlen (retstr))
34
else if (strcmp_P (retstr, expstr))
38
PRINTFLN (line, "expect: %3d, \"%s\",\n%8s output: %3d, \"%s\"\n",
39
strlen(expstr), expstr, " ", retval, retstr);
46
/* 'vp' is used to avoid gcc warnings about format string. */
47
#define CHECK(expstr, fmt, ...) do { \
50
int (* volatile vp)(char *, const char *, ...) = sprintf_P; \
51
memset (s, 0, sizeof(s)); \
52
i = vp (s, PSTR(fmt), ##__VA_ARGS__); \
53
Check (__LINE__, PSTR(expstr), i, s); \
58
/* '%x' specification */
60
CHECK ("1234 5678 9abc def0",
61
"%x %x %x %x", 0x1234, 0x5678, 0x9abc, 0xdef0);
62
CHECK ("1 7fff 8000 8001 ffff",
63
"%x %x %x %x %x", 1, 0x7fff, 0x8000, 0x8001, 0xffff);
65
CHECK ("0 1 ffff", "%0x %0x %0x", 0, 1, 0xffff);
66
CHECK ("0 1 ffff", "% x % x % x", 0, 1, 0xffff);
67
CHECK ("0 1 ffff", "%+x %+x %+x", 0, 1, 0xffff);
68
CHECK ("0 1 ffff", "%-x %-x %-x", 0, 1, 0xffff);
69
CHECK ("0 0x1 0xffff", "%#x %#x %#x", 0, 1, 0xffff);
72
CHECK ("0", "%.0x", 0);
74
CHECK ("1 ffff", "%.0x %.0x", 1, 0xffff);
75
CHECK ("0 1 ffff", "%.1x %.1x %.1x", 0, 1, 0xffff);
76
#ifdef __AVR__ /* PRINTF_MIN */
77
CHECK ("0 1 ff ffff", "%.2x %.2x %.2x %.2x", 0, 1, 255, 0xffff);
78
CHECK ("0 1 12 123 1234",
79
"%.3x %.3x %.3x %.3x %.3x", 0, 1, 0x12, 0x123, 0x1234);
81
CHECK ("0.1.ffff", "%1x.%1x.%1x", 0, 1, 0xffff);
82
CHECK ("1.12.123", "%2x.%2x.%2x", 1, 0x12, 0x123);
83
CHECK ("1.12.123.1234",
84
"%3x.%3x.%3x.%3x", 1, 0x12, 0x123, 0x1234);
85
CHECK ("0.0x1.0x12.0x123.0x1234",
86
"%#4x.%#4x.%#4x.%#4x.%#4x", 0, 1, 0x12, 0x123, 0x1234);
87
CHECK ("14.ffff", "% 4x.% 4x", 0x14, 0xffff);
88
CHECK ("16.ffff", "%+4x.%+4x", 0x16, 0xffff);
89
CHECK ("17.ffff", "%-4x.%-4x", 0x17, 0xffff);
91
CHECK ("0.1.12.123.1234",
92
"%04x.%04x.%04x.%04x.%04x",
93
0, 1, 0x12, 0x123, 0x1234);
95
CHECK ("1", "%08.4x", 1); /* 0 - ignored */
96
CHECK ("1", "%08.0x", 1);
97
CHECK ("1", "%08.x", 1);
99
CHECK ("0.1.12.123.1234",
100
"%8.3x.%8.3x.%8.3x.%8.3x.%8.3x", 0, 1, 0x12, 0x123, 0x1234);
102
"%08.4x.% 8.4x.%+8.4x", 0x12, 0x13, 0x14);
103
CHECK ("0.1.12.123.1234",
104
"%-8.3x.%-8.3x.%-8.3x.%-8.3x.%-8.3x", 0, 1, 0x12, 0x123, 0x1234);
106
CHECK ("0.0x1.0x12.0x123.0x1234",
107
"%#8.4x.%#8.4x.%#8.4x.%#8.4x.%#8.4x",
108
0, 1, 0x12, 0x123, 0x1234);
110
CHECK ("1", "%255x", 1);
111
CHECK ("1", "%-255x", 1);
112
CHECK ("2", "%.255x", 2);
116
CHECK ("0 1 12345678 9abcdef0 7fffffff 80000000 80000001 ffffffff",
117
"%lx %lx %lx %lx %lx %lx %lx %lx",
118
0L, 1L, 0x12345678L, 0x9abcdef0L,
119
0x7fffffffL, 0x80000000L, 0x80000001L, 0xffffffffL);
122
CHECK ("0 2 fffe", "%hx %hx %hx", 0, 2, 0xfffe);