1 /* $Id: snprintf-t.c 7510 2006-04-02 18:31:51Z eagle $ */
2 /* snprintf test suite. */
9 int test_snprintf(char *str, size_t count, const char *fmt, ...);
10 int test_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
12 static const char string[] = "abcdefghijklmnopqrstuvwxyz0123456789";
14 static const char *const fp_formats[] = {
15 "%-1.5f", "%1.5f", "%31.9f", "%10.5f", "% 10.5f", "%+22.9f",
16 "%+4.9f", "%01.3f", "%3.1f", "%3.2f", "%.0f", "%.1f",
19 static const char *const int_formats[] = {
20 "%-1.5d", "%1.5d", "%31.9d", "%5.5d", "%10.5d", "% 10.5d",
21 "%+22.30d", "%01.3d", "%4d", "%d", "%ld", NULL
23 static const char *const uint_formats[] = {
24 "%-1.5lu", "%1.5lu", "%31.9lu", "%5.5lu", "%10.5lu", "% 10.5lu",
25 "%+6.30lu", "%01.3lu", "%4lu", "%lu", "%4lx", "%4lX",
26 "%01.3lx", "%1lo", NULL
28 static const char *const llong_formats[] = {
29 "%lld", "%-1.5lld", "%1.5lld", "%123.9lld", "%5.5lld",
30 "%10.5lld", "% 10.5lld", "%+22.33lld", "%01.3lld", "%4lld",
33 static const char *const ullong_formats[] = {
34 "%llu", "%-1.5llu", "%1.5llu", "%123.9llu", "%5.5llu",
35 "%10.5llu", "% 10.5llu", "%+22.33llu", "%01.3llu", "%4llu",
39 static const double fp_nums[] = {
40 -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996, 0.9996, 1.996,
43 static long int_nums[] = {
44 -1, 134, 91340, 341, 0203, 0
46 static unsigned long uint_nums[] = {
47 (unsigned long) -1, 134, 91340, 341, 0203, 0
49 static long long llong_nums[] = {
50 ~(long long) 0, /* All-1 bit pattern. */
51 (~(unsigned long long) 0) >> 1, /* Largest signed long long. */
52 -150, 134, 91340, 341,
55 static unsigned long long ullong_nums[] = {
56 ~(unsigned long long) 0, /* All-1 bit pattern. */
57 (~(unsigned long long) 0) >> 1, /* Largest signed long long. */
63 test_format(int n, bool truncate, const char *expected, int count,
64 const char *format, ...)
70 va_start(args, format);
71 result = test_vsnprintf(buf, truncate ? 32 : sizeof(buf), format, args);
73 if (!strcmp(buf, expected) && result == count) {
76 printf("not ok %d\n", n);
77 printf(" format: %s\n", format);
78 if (strcmp(buf, expected))
79 printf(" saw: %s\n want: %s\n", buf, expected);
80 if (result != count) printf(" %d != %d\n", result, count);
93 (25 + (ARRAY_SIZE(fp_formats) - 1) * ARRAY_SIZE(fp_nums)
94 + (ARRAY_SIZE(int_formats) - 1) * ARRAY_SIZE(int_nums)
95 + (ARRAY_SIZE(uint_formats) - 1) * ARRAY_SIZE(uint_nums)
96 + (ARRAY_SIZE(llong_formats) - 1) * ARRAY_SIZE(llong_nums)
97 + (ARRAY_SIZE(ullong_formats) - 1) * ARRAY_SIZE(ullong_nums)));
99 ok(1, test_snprintf(NULL, 0, "%s", "abcd") == 4);
100 ok(2, test_snprintf(NULL, 0, "%d", 20) == 2);
101 ok(3, test_snprintf(NULL, 0, "Test %.2s", "abcd") == 7);
102 ok(4, test_snprintf(NULL, 0, "%c", 'a') == 1);
103 ok(5, test_snprintf(NULL, 0, "") == 0);
105 test_format(6, true, "abcd", 4, "%s", "abcd");
106 test_format(7, true, "20", 2, "%d", 20);
107 test_format(8, true, "Test ab", 7, "Test %.2s", "abcd");
108 test_format(9, true, "a", 1, "%c", 'a');
109 test_format(10, true, "", 0, "");
110 test_format(11, true, "abcdefghijklmnopqrstuvwxyz01234", 36, "%s",
112 test_format(12, true, "abcdefghij", 10, "%.10s", string);
113 test_format(13, true, " abcdefghij", 12, "%12.10s", string);
114 test_format(14, true, " abcdefghijklmnopqrstuvwxyz0", 40, "%40s",
116 test_format(15, true, "abcdefghij ", 14, "%-14.10s", string);
117 test_format(16, true, " abcdefghijklmnopq", 50, "%50s",
119 test_format(17, true, "%abcd%", 6, "%%%0s%%", "abcd");
120 test_format(18, true, "", 0, "%.0s", string);
121 test_format(19, true, "abcdefghijklmnopqrstuvwxyz 444", 32, "%.26s %d",
123 test_format(20, true, "abcdefghijklmnopqrstuvwxyz -2.", 32,
124 "%.26s %.1f", string, -2.5);
125 test_format(21, true, "abcdefghij4444", 14, "%.10s%n%d", string, &count,
128 test_format(23, true, "abcdefghijklmnopqrstuvwxyz01234", 36, "%n%s%ln",
129 &count, string, &lcount);
131 ok(25, lcount == 31);
134 for (i = 0; fp_formats[i] != NULL; i++)
135 for (j = 0; j < ARRAY_SIZE(fp_nums); j++) {
136 count = sprintf(lgbuf, fp_formats[i], fp_nums[j]);
137 test_format(++n, false, lgbuf, count, fp_formats[i], fp_nums[j]);
139 for (i = 0; int_formats[i] != NULL; i++)
140 for (j = 0; j < ARRAY_SIZE(int_nums); j++) {
141 count = sprintf(lgbuf, int_formats[i], int_nums[j]);
142 test_format(++n, false, lgbuf, count, int_formats[i],
145 for (i = 0; uint_formats[i] != NULL; i++)
146 for (j = 0; j < ARRAY_SIZE(uint_nums); j++) {
147 count = sprintf(lgbuf, uint_formats[i], uint_nums[j]);
148 test_format(++n, false, lgbuf, count, uint_formats[i],
151 for (i = 0; llong_formats[i] != NULL; i++)
152 for (j = 0; j < ARRAY_SIZE(llong_nums); j++) {
153 count = sprintf(lgbuf, llong_formats[i], llong_nums[j]);
154 test_format(++n, false, lgbuf, count, llong_formats[i],
157 for (i = 0; ullong_formats[i] != NULL; i++)
158 for (j = 0; j < ARRAY_SIZE(ullong_nums); j++) {
159 count = sprintf(lgbuf, ullong_formats[i], ullong_nums[j]);
160 test_format(++n, false, lgbuf, count, ullong_formats[i],