]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/portable/snprintf-t.c
Change my email address to eagle@eyrie.org
[kerberos/krb5-strength.git] / tests / portable / snprintf-t.c
1 /*
2  * snprintf test suite.
3  *
4  * The canonical version of this file is maintained in the rra-c-util package,
5  * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
6  *
7  * Written by Russ Allbery <eagle@eyrie.org>
8  * Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006
9  *     Russ Allbery <eagle@eyrie.org>
10  * Copyright 2009, 2010
11  *     The Board of Trustees of the Leland Stanford Junior University
12  * Copyright 1995 Patrick Powell
13  * Copyright 2001 Hrvoje Niksic
14  *
15  * This code is based on code written by Patrick Powell (papowell@astart.com)
16  * It may be used for any purpose as long as this notice remains intact
17  * on all source code distributions
18  */
19
20 #include <config.h>
21 #include <portable/system.h>
22
23 #include <tests/tap/basic.h>
24
25 /*
26  * Disable the requirement that format strings be literals.  We need variable
27  * formats for easy testing.
28  */
29 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
30
31 /*
32  * Intentionally don't add the printf attribute here since we pass a
33  * zero-length printf format during testing and don't want warnings.
34  */
35 int test_snprintf(char *str, size_t count, const char *fmt, ...);
36 int test_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
37
38 static const char string[] = "abcdefghijklmnopqrstuvwxyz0123456789";
39
40 static const char *const fp_formats[] = {
41     "%-1.5f",   "%1.5f",    "%31.9f",   "%10.5f",   "% 10.5f",  "%+22.9f",
42     "%+4.9f",   "%01.3f",   "%3.1f",    "%3.2f",    "%.0f",     "%.1f",
43     "%f",
44
45     /* %e and %g formats aren't really implemented yet. */
46 #if 0
47     "%-1.5e",   "%1.5e",    "%31.9e",   "%10.5e",   "% 10.5e",  "%+22.9e",
48     "%+4.9e",   "%01.3e",   "%3.1e",    "%3.2e",    "%.0e",     "%.1e",
49     "%e",
50     "%-1.5g",   "%1.5g",    "%31.9g",   "%10.5g",   "% 10.5g",  "%+22.9g",
51     "%+4.9g",   "%01.3g",   "%3.1g",    "%3.2g",    "%.0g",     "%.1g",
52     "%g",
53 #endif
54     NULL
55 };
56 static const char *const int_formats[] = {
57     "%-1.5d",   "%1.5d",    "%31.9d",   "%5.5d",    "%10.5d",   "% 10.5d",
58     "%+22.30d", "%01.3d",   "%4d",      "%d",       "%ld",      NULL
59 };
60 static const char *const uint_formats[] = {
61     "%-1.5lu",  "%1.5lu",   "%31.9lu",  "%5.5lu",   "%10.5lu",  "% 10.5lu",
62     "%+6.30lu", "%01.3lu",  "%4lu",     "%lu",      "%4lx",     "%4lX",
63     "%01.3lx",  "%1lo",     NULL
64 };
65 static const char *const llong_formats[] = {
66     "%lld",     "%-1.5lld",  "%1.5lld",    "%123.9lld",  "%5.5lld",
67     "%10.5lld", "% 10.5lld", "%+22.33lld", "%01.3lld",   "%4lld",
68     NULL
69 };
70 static const char *const ullong_formats[] = {
71     "%llu",     "%-1.5llu",  "%1.5llu",    "%123.9llu",  "%5.5llu",
72     "%10.5llu", "% 10.5llu", "%+22.33llu", "%01.3llu",   "%4llu",
73     "%llx",     "%llo",      NULL
74 };
75
76 static const double fp_nums[] = {
77     -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996, 0.9996, 1.996,
78     4.136, 0.1, 0.01, 0.001, 10.1, 0
79 };
80 static long int_nums[] = {
81     -1, 134, 91340, 341, 0203, 0
82 };
83 static unsigned long uint_nums[] = {
84     (unsigned long) -1, 134, 91340, 341, 0203, 0
85 };
86 static long long llong_nums[] = {
87     ~(long long) 0,                     /* All-1 bit pattern. */
88     (~(unsigned long long) 0) >> 1,     /* Largest signed long long. */
89     -150, 134, 91340, 341,
90     0
91 };
92 static unsigned long long ullong_nums[] = {
93     ~(unsigned long long) 0,            /* All-1 bit pattern. */
94     (~(unsigned long long) 0) >> 1,     /* Largest signed long long. */
95     134, 91340, 341,
96     0
97 };
98
99
100 static void
101 test_format(bool trunc, const char *expected, int count,
102             const char *format, ...)
103 {
104     char buf[128];
105     int result;
106     va_list args;
107
108     va_start(args, format);
109     result = test_vsnprintf(buf, trunc ? 32 : sizeof(buf), format, args);
110     va_end(args);
111     is_string(expected, buf, "format %s, wanted %s", format, expected);
112     is_int(count, result, "...and output length correct");
113 }
114
115
116 int
117 main(void)
118 {
119     int i, count;
120     unsigned int j;
121     long lcount;
122     char lgbuf[128];
123
124     plan(8 +
125          (18 + (ARRAY_SIZE(fp_formats) - 1) * ARRAY_SIZE(fp_nums)
126           + (ARRAY_SIZE(int_formats) - 1) * ARRAY_SIZE(int_nums)
127           + (ARRAY_SIZE(uint_formats) - 1) * ARRAY_SIZE(uint_nums)
128           + (ARRAY_SIZE(llong_formats) - 1) * ARRAY_SIZE(llong_nums)
129           + (ARRAY_SIZE(ullong_formats) - 1) * ARRAY_SIZE(ullong_nums)) * 2);
130
131     is_int(4, test_snprintf(NULL, 0, "%s", "abcd"), "simple string length");
132     is_int(2, test_snprintf(NULL, 0, "%d", 20), "number length");
133     is_int(7, test_snprintf(NULL, 0, "Test %.2s", "abcd"), "limited string");
134     is_int(1, test_snprintf(NULL, 0, "%c", 'a'), "character length");
135     is_int(0, test_snprintf(NULL, 0, ""), "empty format length");
136
137     test_format(true, "abcd", 4, "%s", "abcd");
138     test_format(true, "20", 2, "%d", 20);
139     test_format(true, "Test ab", 7, "Test %.2s", "abcd");
140     test_format(true, "a", 1, "%c", 'a');
141     test_format(true, "", 0, "");
142     test_format(true, "abcdefghijklmnopqrstuvwxyz01234", 36, "%s", string);
143     test_format(true, "abcdefghij", 10, "%.10s", string);
144     test_format(true, "  abcdefghij", 12, "%12.10s", string);
145     test_format(true, "    abcdefghijklmnopqrstuvwxyz0", 40, "%40s", string);
146     test_format(true, "abcdefghij    ", 14, "%-14.10s", string);
147     test_format(true, "              abcdefghijklmnopq", 50, "%50s", string);
148     test_format(true, "%abcd%", 6, "%%%0s%%", "abcd");
149     test_format(true, "", 0, "%.0s", string);
150     test_format(true, "abcdefghijklmnopqrstuvwxyz  444", 32, "%.26s  %d",
151                 string, 4444);
152     test_format(true, "abcdefghijklmnopqrstuvwxyz  -2.", 32, "%.26s  %.1f",
153                 string, -2.5);
154     test_format(true, "abcdefghij4444", 14, "%.10s%n%d", string, &count, 4444);
155     is_int(10, count, "correct output from %%n");
156     test_format(true, "abcdefghijklmnopqrstuvwxyz01234", 36, "%n%s%ln",
157                 &count, string, &lcount);
158     is_int(0, count, "correct output from two %%n");
159     is_int(31, lcount, "correct output from long %%ln");
160     test_format(true, "(null)", 6, "%s", NULL);
161
162     for (i = 0; fp_formats[i] != NULL; i++)
163         for (j = 0; j < ARRAY_SIZE(fp_nums); j++) {
164             count = sprintf(lgbuf, fp_formats[i], fp_nums[j]);
165             test_format(false, lgbuf, count, fp_formats[i], fp_nums[j]);
166         }
167     for (i = 0; int_formats[i] != NULL; i++)
168         for (j = 0; j < ARRAY_SIZE(int_nums); j++) {
169             count = sprintf(lgbuf, int_formats[i], int_nums[j]);
170             test_format(false, lgbuf, count, int_formats[i], int_nums[j]);
171         }
172     for (i = 0; uint_formats[i] != NULL; i++)
173         for (j = 0; j < ARRAY_SIZE(uint_nums); j++) {
174             count = sprintf(lgbuf, uint_formats[i], uint_nums[j]);
175             test_format(false, lgbuf, count, uint_formats[i], uint_nums[j]);
176         }
177     for (i = 0; llong_formats[i] != NULL; i++)
178         for (j = 0; j < ARRAY_SIZE(llong_nums); j++) {
179             count = sprintf(lgbuf, llong_formats[i], llong_nums[j]);
180             test_format(false, lgbuf, count, llong_formats[i], llong_nums[j]);
181         }
182     for (i = 0; ullong_formats[i] != NULL; i++)
183         for (j = 0; j < ARRAY_SIZE(ullong_nums); j++) {
184             count = sprintf(lgbuf, ullong_formats[i], ullong_nums[j]);
185             test_format(false, lgbuf, count, ullong_formats[i],
186                         ullong_nums[j]);
187         }
188
189     return 0;
190 }