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