1*d3273b5bSchristos /* $NetBSD: snprintf-test.c,v 1.2 2017/01/28 21:31:50 christos Exp $ */
2ca1c9b0cSelric
3ca1c9b0cSelric /*
4ca1c9b0cSelric * Copyright (c) 2000 - 2001 Kungliga Tekniska Högskolan
5ca1c9b0cSelric * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric * All rights reserved.
7ca1c9b0cSelric *
8ca1c9b0cSelric * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric * modification, are permitted provided that the following conditions
10ca1c9b0cSelric * are met:
11ca1c9b0cSelric *
12ca1c9b0cSelric * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric * notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric *
15ca1c9b0cSelric * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric * notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric * documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric *
19ca1c9b0cSelric * 3. Neither the name of KTH nor the names of its contributors may be
20ca1c9b0cSelric * used to endorse or promote products derived from this software without
21ca1c9b0cSelric * specific prior written permission.
22ca1c9b0cSelric *
23ca1c9b0cSelric * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24ca1c9b0cSelric * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26ca1c9b0cSelric * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27ca1c9b0cSelric * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28ca1c9b0cSelric * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29ca1c9b0cSelric * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30ca1c9b0cSelric * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31ca1c9b0cSelric * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32ca1c9b0cSelric * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33ca1c9b0cSelric * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34ca1c9b0cSelric
35ca1c9b0cSelric #include <config.h>
36ca1c9b0cSelric #include <krb5/roken.h>
37ca1c9b0cSelric #include <limits.h>
38ca1c9b0cSelric
39b9d004c6Schristos extern int rk_snprintf(char *, size_t, const char *, ...);
40b9d004c6Schristos extern int rk_vsnprintf(char *, size_t, const char *, va_list);
41b9d004c6Schristos
42ca1c9b0cSelric static int
try(const char * format,...)43ca1c9b0cSelric try (const char *format, ...)
44ca1c9b0cSelric {
45ca1c9b0cSelric int ret;
46ca1c9b0cSelric va_list ap;
47ca1c9b0cSelric char buf1[256], buf2[256];
48ca1c9b0cSelric
49ca1c9b0cSelric va_start (ap, format);
50ca1c9b0cSelric ret = rk_vsnprintf (buf1, sizeof(buf1), format, ap);
51ca1c9b0cSelric if (ret >= sizeof(buf1))
52ca1c9b0cSelric errx (1, "increase buf and try again");
53ca1c9b0cSelric va_end (ap);
54ca1c9b0cSelric va_start (ap, format);
55ca1c9b0cSelric vsprintf (buf2, format, ap);
56ca1c9b0cSelric ret = strcmp (buf1, buf2);
57ca1c9b0cSelric if (ret)
58ca1c9b0cSelric printf ("failed: format = \"%s\", \"%s\" != \"%s\"\n",
59ca1c9b0cSelric format, buf1, buf2);
60ca1c9b0cSelric va_end (ap);
61ca1c9b0cSelric return ret;
62ca1c9b0cSelric }
63ca1c9b0cSelric
64ca1c9b0cSelric static int
cmp_with_sprintf_int(void)65ca1c9b0cSelric cmp_with_sprintf_int (void)
66ca1c9b0cSelric {
67ca1c9b0cSelric int tot = 0;
68ca1c9b0cSelric int int_values[] = {INT_MIN, -17, -1, 0, 1, 17, 4711, 65535, INT_MAX};
69ca1c9b0cSelric int i;
70ca1c9b0cSelric
71ca1c9b0cSelric for (i = 0; i < sizeof(int_values) / sizeof(int_values[0]); ++i) {
72ca1c9b0cSelric tot += try ("%d", int_values[i]);
73ca1c9b0cSelric tot += try ("%x", int_values[i]);
74ca1c9b0cSelric tot += try ("%X", int_values[i]);
75ca1c9b0cSelric tot += try ("%o", int_values[i]);
76ca1c9b0cSelric tot += try ("%#x", int_values[i]);
77ca1c9b0cSelric tot += try ("%#X", int_values[i]);
78ca1c9b0cSelric tot += try ("%#o", int_values[i]);
79ca1c9b0cSelric tot += try ("%10d", int_values[i]);
80ca1c9b0cSelric tot += try ("%10x", int_values[i]);
81ca1c9b0cSelric tot += try ("%10X", int_values[i]);
82ca1c9b0cSelric tot += try ("%10o", int_values[i]);
83ca1c9b0cSelric tot += try ("%#10x", int_values[i]);
84ca1c9b0cSelric tot += try ("%#10X", int_values[i]);
85ca1c9b0cSelric tot += try ("%#10o", int_values[i]);
86ca1c9b0cSelric tot += try ("%-10d", int_values[i]);
87ca1c9b0cSelric tot += try ("%-10x", int_values[i]);
88ca1c9b0cSelric tot += try ("%-10X", int_values[i]);
89ca1c9b0cSelric tot += try ("%-10o", int_values[i]);
90ca1c9b0cSelric tot += try ("%-#10x", int_values[i]);
91ca1c9b0cSelric tot += try ("%-#10X", int_values[i]);
92ca1c9b0cSelric tot += try ("%-#10o", int_values[i]);
93ca1c9b0cSelric }
94ca1c9b0cSelric return tot;
95ca1c9b0cSelric }
96ca1c9b0cSelric
97ca1c9b0cSelric static int
cmp_with_sprintf_long(void)98ca1c9b0cSelric cmp_with_sprintf_long (void)
99ca1c9b0cSelric {
100ca1c9b0cSelric int tot = 0;
101ca1c9b0cSelric long long_values[] = {LONG_MIN, -17, -1, 0, 1, 17, 4711, 65535, LONG_MAX};
102ca1c9b0cSelric int i;
103ca1c9b0cSelric
104ca1c9b0cSelric for (i = 0; i < sizeof(long_values) / sizeof(long_values[0]); ++i) {
105ca1c9b0cSelric tot += try ("%ld", long_values[i]);
106ca1c9b0cSelric tot += try ("%lx", long_values[i]);
107ca1c9b0cSelric tot += try ("%lX", long_values[i]);
108ca1c9b0cSelric tot += try ("%lo", long_values[i]);
109ca1c9b0cSelric tot += try ("%#lx", long_values[i]);
110ca1c9b0cSelric tot += try ("%#lX", long_values[i]);
111ca1c9b0cSelric tot += try ("%#lo", long_values[i]);
112ca1c9b0cSelric tot += try ("%10ld", long_values[i]);
113ca1c9b0cSelric tot += try ("%10lx", long_values[i]);
114ca1c9b0cSelric tot += try ("%10lX", long_values[i]);
115ca1c9b0cSelric tot += try ("%10lo", long_values[i]);
116ca1c9b0cSelric tot += try ("%#10lx", long_values[i]);
117ca1c9b0cSelric tot += try ("%#10lX", long_values[i]);
118ca1c9b0cSelric tot += try ("%#10lo", long_values[i]);
119ca1c9b0cSelric tot += try ("%-10ld", long_values[i]);
120ca1c9b0cSelric tot += try ("%-10lx", long_values[i]);
121ca1c9b0cSelric tot += try ("%-10lX", long_values[i]);
122ca1c9b0cSelric tot += try ("%-10lo", long_values[i]);
123ca1c9b0cSelric tot += try ("%-#10lx", long_values[i]);
124ca1c9b0cSelric tot += try ("%-#10lX", long_values[i]);
125ca1c9b0cSelric tot += try ("%-#10lo", long_values[i]);
126ca1c9b0cSelric }
127ca1c9b0cSelric return tot;
128ca1c9b0cSelric }
129ca1c9b0cSelric
130ca1c9b0cSelric #ifdef HAVE_LONG_LONG
131ca1c9b0cSelric
132ca1c9b0cSelric /* XXX doesn't work as expected on lp64 platforms with sizeof(long
133ca1c9b0cSelric * long) == sizeof(long) */
134ca1c9b0cSelric
135ca1c9b0cSelric static int
cmp_with_sprintf_long_long(void)136ca1c9b0cSelric cmp_with_sprintf_long_long (void)
137ca1c9b0cSelric {
138ca1c9b0cSelric int tot = 0;
139ca1c9b0cSelric long long long_long_values[] = {
140b9d004c6Schristos ((long long)LONG_MIN) - (sizeof(long long) > sizeof(long)),
141b9d004c6Schristos LONG_MIN, -17, -1, 0, 1, 17, 4711, 65535, LONG_MAX,
142b9d004c6Schristos ((long long)LONG_MAX) + (sizeof(long long) > sizeof(long))
143b9d004c6Schristos };
144ca1c9b0cSelric int i;
145ca1c9b0cSelric
146ca1c9b0cSelric for (i = 0; i < sizeof(long_long_values) / sizeof(long_long_values[0]); ++i) {
147ca1c9b0cSelric tot += try ("%lld", long_long_values[i]);
148ca1c9b0cSelric tot += try ("%llx", long_long_values[i]);
149ca1c9b0cSelric tot += try ("%llX", long_long_values[i]);
150ca1c9b0cSelric tot += try ("%llo", long_long_values[i]);
151ca1c9b0cSelric tot += try ("%#llx", long_long_values[i]);
152ca1c9b0cSelric tot += try ("%#llX", long_long_values[i]);
153ca1c9b0cSelric tot += try ("%#llo", long_long_values[i]);
154ca1c9b0cSelric tot += try ("%10lld", long_long_values[i]);
155ca1c9b0cSelric tot += try ("%10llx", long_long_values[i]);
156ca1c9b0cSelric tot += try ("%10llX", long_long_values[i]);
157ca1c9b0cSelric tot += try ("%10llo", long_long_values[i]);
158ca1c9b0cSelric tot += try ("%#10llx", long_long_values[i]);
159ca1c9b0cSelric tot += try ("%#10llX", long_long_values[i]);
160ca1c9b0cSelric tot += try ("%#10llo", long_long_values[i]);
161ca1c9b0cSelric tot += try ("%-10lld", long_long_values[i]);
162ca1c9b0cSelric tot += try ("%-10llx", long_long_values[i]);
163ca1c9b0cSelric tot += try ("%-10llX", long_long_values[i]);
164ca1c9b0cSelric tot += try ("%-10llo", long_long_values[i]);
165ca1c9b0cSelric tot += try ("%-#10llx", long_long_values[i]);
166ca1c9b0cSelric tot += try ("%-#10llX", long_long_values[i]);
167ca1c9b0cSelric tot += try ("%-#10llo", long_long_values[i]);
168ca1c9b0cSelric }
169ca1c9b0cSelric return tot;
170ca1c9b0cSelric }
171ca1c9b0cSelric
172ca1c9b0cSelric #endif
173ca1c9b0cSelric
174ca1c9b0cSelric #if 0
175ca1c9b0cSelric static int
176ca1c9b0cSelric cmp_with_sprintf_float (void)
177ca1c9b0cSelric {
178ca1c9b0cSelric int tot = 0;
179ca1c9b0cSelric double double_values[] = {-99999, -999, -17.4, -4.3, -3.0, -1.5, -1,
180ca1c9b0cSelric 0, 0.1, 0.2342374852, 0.2340007,
181ca1c9b0cSelric 3.1415926, 14.7845, 34.24758, 9999, 9999999};
182ca1c9b0cSelric int i;
183ca1c9b0cSelric
184ca1c9b0cSelric for (i = 0; i < sizeof(double_values) / sizeof(double_values[0]); ++i) {
185ca1c9b0cSelric tot += try ("%f", double_values[i]);
186ca1c9b0cSelric tot += try ("%10f", double_values[i]);
187ca1c9b0cSelric tot += try ("%.2f", double_values[i]);
188ca1c9b0cSelric tot += try ("%7.0f", double_values[i]);
189ca1c9b0cSelric tot += try ("%5.2f", double_values[i]);
190ca1c9b0cSelric tot += try ("%0f", double_values[i]);
191ca1c9b0cSelric tot += try ("%#f", double_values[i]);
192ca1c9b0cSelric tot += try ("%e", double_values[i]);
193ca1c9b0cSelric tot += try ("%10e", double_values[i]);
194ca1c9b0cSelric tot += try ("%.2e", double_values[i]);
195ca1c9b0cSelric tot += try ("%7.0e", double_values[i]);
196ca1c9b0cSelric tot += try ("%5.2e", double_values[i]);
197ca1c9b0cSelric tot += try ("%0e", double_values[i]);
198ca1c9b0cSelric tot += try ("%#e", double_values[i]);
199ca1c9b0cSelric tot += try ("%E", double_values[i]);
200ca1c9b0cSelric tot += try ("%10E", double_values[i]);
201ca1c9b0cSelric tot += try ("%.2E", double_values[i]);
202ca1c9b0cSelric tot += try ("%7.0E", double_values[i]);
203ca1c9b0cSelric tot += try ("%5.2E", double_values[i]);
204ca1c9b0cSelric tot += try ("%0E", double_values[i]);
205ca1c9b0cSelric tot += try ("%#E", double_values[i]);
206ca1c9b0cSelric tot += try ("%g", double_values[i]);
207ca1c9b0cSelric tot += try ("%10g", double_values[i]);
208ca1c9b0cSelric tot += try ("%.2g", double_values[i]);
209ca1c9b0cSelric tot += try ("%7.0g", double_values[i]);
210ca1c9b0cSelric tot += try ("%5.2g", double_values[i]);
211ca1c9b0cSelric tot += try ("%0g", double_values[i]);
212ca1c9b0cSelric tot += try ("%#g", double_values[i]);
213ca1c9b0cSelric tot += try ("%G", double_values[i]);
214ca1c9b0cSelric tot += try ("%10G", double_values[i]);
215ca1c9b0cSelric tot += try ("%.2G", double_values[i]);
216ca1c9b0cSelric tot += try ("%7.0G", double_values[i]);
217ca1c9b0cSelric tot += try ("%5.2G", double_values[i]);
218ca1c9b0cSelric tot += try ("%0G", double_values[i]);
219ca1c9b0cSelric tot += try ("%#G", double_values[i]);
220ca1c9b0cSelric }
221ca1c9b0cSelric return tot;
222ca1c9b0cSelric }
223ca1c9b0cSelric #endif
224ca1c9b0cSelric
225ca1c9b0cSelric static int
test_null(void)226ca1c9b0cSelric test_null (void)
227ca1c9b0cSelric {
228ca1c9b0cSelric return rk_snprintf (NULL, 0, "foo") != 3;
229ca1c9b0cSelric }
230ca1c9b0cSelric
231ca1c9b0cSelric static int
test_sizet(void)232ca1c9b0cSelric test_sizet (void)
233ca1c9b0cSelric {
234ca1c9b0cSelric int tot = 0;
235ca1c9b0cSelric size_t sizet_values[] = { 0, 1, 2, 200, 4294967295u }; /* SIZE_MAX */
236ca1c9b0cSelric char *result[] = { "0", "1", "2", "200", "4294967295" };
237ca1c9b0cSelric int i;
238ca1c9b0cSelric
239ca1c9b0cSelric for (i = 0; i < sizeof(sizet_values) / sizeof(sizet_values[0]); ++i) {
240ca1c9b0cSelric #if 0
241ca1c9b0cSelric tot += try("%zu", sizet_values[i]);
242ca1c9b0cSelric tot += try("%zx", sizet_values[i]);
243ca1c9b0cSelric tot += try("%zX", sizet_values[i]);
244ca1c9b0cSelric #else
245ca1c9b0cSelric char buf[256];
246ca1c9b0cSelric rk_snprintf(buf, sizeof(buf), "%zu", sizet_values[i]);
247ca1c9b0cSelric if (strcmp(buf, result[i]) != 0) {
248ca1c9b0cSelric printf("%s != %s", buf, result[i]);
249ca1c9b0cSelric tot++;
250ca1c9b0cSelric }
251ca1c9b0cSelric #endif
252ca1c9b0cSelric }
253ca1c9b0cSelric return tot;
254ca1c9b0cSelric }
255ca1c9b0cSelric
256ca1c9b0cSelric
257ca1c9b0cSelric int
main(int argc,char ** argv)258ca1c9b0cSelric main (int argc, char **argv)
259ca1c9b0cSelric {
260ca1c9b0cSelric int ret = 0;
261ca1c9b0cSelric
262ca1c9b0cSelric ret += cmp_with_sprintf_int ();
263ca1c9b0cSelric ret += cmp_with_sprintf_long ();
264ca1c9b0cSelric #ifdef HAVE_LONG_LONG
265ca1c9b0cSelric ret += cmp_with_sprintf_long_long ();
266ca1c9b0cSelric #endif
267ca1c9b0cSelric ret += test_null ();
268ca1c9b0cSelric ret += test_sizet ();
269ca1c9b0cSelric return ret;
270ca1c9b0cSelric }
271