1
/* Test of sprintf_min(), specifications: d,i,u.
2
$Id: sprintf_min-2.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
/* '%d' specification */
60
CHECK ("12345", "%d", 12345);
61
CHECK ("-32768 -32767 -1 0 1 32767",
63
(int)-32768, -32767, -1, 0, 1, 32767);
65
CHECK ("0 1 -1", "%0d %0d %0d", 0, 1, -1);
66
CHECK ("0 1 -1", "%-d %-d %-d", 0, 1, -1);
67
CHECK ("0 1 -1", "%#d %#d %#d", 0, 1, -1);
68
#ifdef __AVR__ /* PRINTF_MIN */
69
CHECK ("0 1 -1", "%+d %+d %+d", 0, 1, -1);
70
CHECK ("0 1 -1", "% d % d % d", 0, 1, -1);
72
CHECK ("0.1.-1.1234.-123.12345.-1234",
73
"%04d.%04d.%04d.%04d.%04d.%04d.%04d",
74
0, 1, -1, 1234, -123, 12345, -1234);
76
CHECK ("1", "%08.4d", 1);
77
CHECK ("1", "%08.0d", 1);
78
CHECK ("1", "%08.d", 1);
80
CHECK ("0", "%.0d", 0);
82
CHECK ("1 -1", "%.0d %.0d", 1, -1);
83
CHECK ("0 1 -1", "%.1d %.1d %.1d", 0, 1, -1);
84
#ifdef __AVR__ /* PRINTF_MIN */
85
CHECK ("0 1 -1", "%.2d %.2d %.2d", 0, 1, -1);
86
CHECK ("0 1 12 123 1234 12345",
87
"%.4d %.4d %.4d %.4d %.4d %.4d",
88
0, 1, 12, 123, 1234, 12345);
90
CHECK ("0.1.-1", "%1d.%1d.%1d", 0, 1, -1);
91
CHECK ("1.12.123", "%2d.%2d.%2d", 1, 12, 123);
92
CHECK ("-1.-12.-123.-1234", "%4d.%4d.%4d.%4d", -1, -12, -123, -1234);
93
CHECK ("12.-13", "%#4d.%#4d", 12, -13);
94
CHECK ("14.-15", "% 4d.% 4d", 14, -15);
95
CHECK ("16.-17", "%+4d.%+4d", 16, -17);
96
CHECK ("18.-19", "%-4d.%-4d", 18, -19);
98
CHECK ("1.-1.1234.-1234",
99
"%8.4d.%8.4d.%8.4d.%8.4d", 1, -1, 1234, -1234);
100
CHECK ("12.13.14.15",
101
"%08.4d.%#8.4d.% 8.4d.%+8.4d", 12, 13, 14, 15);
102
CHECK ("1.123.1234.12345",
103
"%-8.4d.%-8.4d.%-8.4d.%-8.4d", 1, 123, 1234, 12345);
104
CHECK ("-1.-123.-1234.-12345",
105
"%-8.4d.%-8.4d.%-8.4d.%-8.4d", -1, -123, -1234, -12345);
107
CHECK ("1", "%255d", 1);
108
CHECK ("1", "%-255d", 1);
109
CHECK ("2", "%.255d", 2);
113
CHECK ("0 1 -1 2147483647 -2147483647 -2147483648",
114
"%ld %ld %ld %ld %ld %ld",
115
0L, 1L, -1L, 2147483647L, -2147483647L, 0x80000000L);
118
CHECK ("0 2 -2", "%hd %hd %hd", 0, 2, -2);
120
/* '%i' specification */
121
CHECK ("-9", "%i", -9);
123
/* '%u' specification */
124
CHECK ("0 1 32767 32768 32769 65535",
126
0, 1, 0x7fff, 0x8000, 0x8001, 0xffff);