xref: /minix3/tests/lib/libc/gen/t_humanize_number.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: t_humanize_number.c,v 1.8 2012/03/18 07:14:08 jruoho Exp $	*/
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc  * are met:
10*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc  *
16*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*11be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*11be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*11be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*11be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*11be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*11be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc  */
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include <atf-c.h>
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <err.h>
32*11be35a1SLionel Sambuc #include <inttypes.h>
33*11be35a1SLionel Sambuc #include <stdarg.h>
34*11be35a1SLionel Sambuc #include <stdio.h>
35*11be35a1SLionel Sambuc #include <stdlib.h>
36*11be35a1SLionel Sambuc #include <string.h>
37*11be35a1SLionel Sambuc #include <util.h>
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc const struct hnopts {
40*11be35a1SLionel Sambuc 	size_t ho_len;
41*11be35a1SLionel Sambuc 	int64_t ho_num;
42*11be35a1SLionel Sambuc 	const char *ho_suffix;
43*11be35a1SLionel Sambuc 	int ho_scale;
44*11be35a1SLionel Sambuc 	int ho_flags;
45*11be35a1SLionel Sambuc 	int ho_retval;			/* expected return value */
46*11be35a1SLionel Sambuc 	const char *ho_retstr;		/* expected string in buffer */
47*11be35a1SLionel Sambuc } hnopts[] = {
48*11be35a1SLionel Sambuc 	/*
49*11be35a1SLionel Sambuc 	 * Rev. 1.6 produces "10.0".
50*11be35a1SLionel Sambuc 	 */
51*11be35a1SLionel Sambuc 	{ 5, 10737418236ULL * 1024, "",
52*11be35a1SLionel Sambuc 	  HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL, 3, "10T" },
53*11be35a1SLionel Sambuc 
54*11be35a1SLionel Sambuc 	{ 5, 10450000, "",
55*11be35a1SLionel Sambuc 	  HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL, 3, "10M" },
56*11be35a1SLionel Sambuc 	{ 5, 10500000, "",		/* just for reference */
57*11be35a1SLionel Sambuc 	  HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL, 3, "10M" },
58*11be35a1SLionel Sambuc 
59*11be35a1SLionel Sambuc 	/*
60*11be35a1SLionel Sambuc 	 * Trailing space.  Rev. 1.7 produces "1 ".
61*11be35a1SLionel Sambuc 	 */
62*11be35a1SLionel Sambuc 	{ 5, 1, "", 0, HN_NOSPACE, 1, "1" },
63*11be35a1SLionel Sambuc 
64*11be35a1SLionel Sambuc 	{ 5, 1, "", 0, 0, 2, "1 " }, /* just for reference */
65*11be35a1SLionel Sambuc 	{ 5, 1, "", 0, HN_B, 3, "1 B" }, /* and more ... */
66*11be35a1SLionel Sambuc 	{ 5, 1, "", 0, HN_DECIMAL, 2, "1 " },
67*11be35a1SLionel Sambuc 	{ 5, 1, "", 0, HN_NOSPACE | HN_B, 2, "1B" },
68*11be35a1SLionel Sambuc 	{ 5, 1, "", 0, HN_B | HN_DECIMAL, 3, "1 B" },
69*11be35a1SLionel Sambuc 	{ 5, 1, "", 0, HN_NOSPACE | HN_B | HN_DECIMAL, 2, "1B" },
70*11be35a1SLionel Sambuc 
71*11be35a1SLionel Sambuc 	/*
72*11be35a1SLionel Sambuc 	 * Space and HN_B.  Rev. 1.7 produces "1B".
73*11be35a1SLionel Sambuc 	 */
74*11be35a1SLionel Sambuc 	{ 5, 1, "", HN_AUTOSCALE, HN_B, 3, "1 B" },
75*11be35a1SLionel Sambuc 	{ 5, 1000, "",			/* just for reference */
76*11be35a1SLionel Sambuc 	  HN_AUTOSCALE, HN_B, 3, "1 K" },
77*11be35a1SLionel Sambuc 
78*11be35a1SLionel Sambuc 	/*
79*11be35a1SLionel Sambuc 	 * Truncated output.  Rev. 1.7 produces "1.0 K".
80*11be35a1SLionel Sambuc 	 */
81*11be35a1SLionel Sambuc 	{ 6, 1000, "A", HN_AUTOSCALE, HN_DECIMAL, -1, "" },
82*11be35a1SLionel Sambuc 
83*11be35a1SLionel Sambuc 	/*
84*11be35a1SLionel Sambuc 	 * Failure case reported by Greg Troxel <gdt@NetBSD.org>.
85*11be35a1SLionel Sambuc 	 * Rev. 1.11 incorrectly returns 5 with filling the buffer
86*11be35a1SLionel Sambuc 	 * with "1000".
87*11be35a1SLionel Sambuc 	 */
88*11be35a1SLionel Sambuc 	{ 5, 1048258238, "",
89*11be35a1SLionel Sambuc 	  HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL, 4, "1.0G" },
90*11be35a1SLionel Sambuc 	/* Similar case it prints 1000 where it shouldn't */
91*11be35a1SLionel Sambuc 	{ 5, 1023488, "",
92*11be35a1SLionel Sambuc 	  HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL, 4, "1.0M" },
93*11be35a1SLionel Sambuc 	{ 5, 1023999, "",
94*11be35a1SLionel Sambuc 	  HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL, 4, "1.0M" },
95*11be35a1SLionel Sambuc };
96*11be35a1SLionel Sambuc 
97*11be35a1SLionel Sambuc struct hnflags {
98*11be35a1SLionel Sambuc 	int hf_flags;
99*11be35a1SLionel Sambuc 	const char *hf_name;
100*11be35a1SLionel Sambuc };
101*11be35a1SLionel Sambuc 
102*11be35a1SLionel Sambuc const struct hnflags scale_flags[] = {
103*11be35a1SLionel Sambuc 	{ HN_GETSCALE, "HN_GETSCALE" },
104*11be35a1SLionel Sambuc 	{ HN_AUTOSCALE, "HN_AUTOSCALE" },
105*11be35a1SLionel Sambuc };
106*11be35a1SLionel Sambuc const struct hnflags normal_flags[] = {
107*11be35a1SLionel Sambuc 	{ HN_DECIMAL, "HN_DECIMAL" },
108*11be35a1SLionel Sambuc 	{ HN_NOSPACE, "HN_NOSPACE" },
109*11be35a1SLionel Sambuc 	{ HN_B, "HN_B" },
110*11be35a1SLionel Sambuc 	{ HN_DIVISOR_1000, "HN_DIVISOR_1000" },
111*11be35a1SLionel Sambuc };
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc const char *formatflags(char *, size_t, const struct hnflags *, size_t, int);
114*11be35a1SLionel Sambuc void	    newline(void);
115*11be35a1SLionel Sambuc void	    w_printf(const char *, ...) __printflike(1, 2);
116*11be35a1SLionel Sambuc int	    main(int, char *[]);
117*11be35a1SLionel Sambuc 
118*11be35a1SLionel Sambuc const char *
formatflags(char * buf,size_t buflen,const struct hnflags * hfs,size_t hfslen,int flags)119*11be35a1SLionel Sambuc formatflags(char *buf, size_t buflen, const struct hnflags *hfs,
120*11be35a1SLionel Sambuc     size_t hfslen, int flags)
121*11be35a1SLionel Sambuc {
122*11be35a1SLionel Sambuc 	const struct hnflags *hf;
123*11be35a1SLionel Sambuc 	char *p = buf;
124*11be35a1SLionel Sambuc 	ssize_t len = buflen;
125*11be35a1SLionel Sambuc 	unsigned int i, found;
126*11be35a1SLionel Sambuc 	int n;
127*11be35a1SLionel Sambuc 
128*11be35a1SLionel Sambuc 	if (flags == 0) {
129*11be35a1SLionel Sambuc 		snprintf(buf, buflen, "0");
130*11be35a1SLionel Sambuc 		return (buf);
131*11be35a1SLionel Sambuc 	}
132*11be35a1SLionel Sambuc 	for (i = found = 0; i < hfslen && flags & ~found; i++) {
133*11be35a1SLionel Sambuc 		hf = &hfs[i];
134*11be35a1SLionel Sambuc 		if (flags & hf->hf_flags) {
135*11be35a1SLionel Sambuc 			found |= hf->hf_flags;
136*11be35a1SLionel Sambuc 			n = snprintf(p, len, "|%s", hf->hf_name);
137*11be35a1SLionel Sambuc 			if (n >= len) {
138*11be35a1SLionel Sambuc 				p = buf;
139*11be35a1SLionel Sambuc 				len = buflen;
140*11be35a1SLionel Sambuc 				/* Print `flags' as number */
141*11be35a1SLionel Sambuc 				goto bad;
142*11be35a1SLionel Sambuc 			}
143*11be35a1SLionel Sambuc 			p += n;
144*11be35a1SLionel Sambuc 			len -= n;
145*11be35a1SLionel Sambuc 		}
146*11be35a1SLionel Sambuc 	}
147*11be35a1SLionel Sambuc 	flags &= ~found;
148*11be35a1SLionel Sambuc 	if (flags)
149*11be35a1SLionel Sambuc bad:
150*11be35a1SLionel Sambuc 		snprintf(p, len, "|0x%x", flags);
151*11be35a1SLionel Sambuc 	return (*buf == '|' ? buf + 1 : buf);
152*11be35a1SLionel Sambuc }
153*11be35a1SLionel Sambuc 
154*11be35a1SLionel Sambuc static int col, bol = 1;
155*11be35a1SLionel Sambuc void
newline(void)156*11be35a1SLionel Sambuc newline(void)
157*11be35a1SLionel Sambuc {
158*11be35a1SLionel Sambuc 
159*11be35a1SLionel Sambuc 	fprintf(stderr, "\n");
160*11be35a1SLionel Sambuc 	col = 0;
161*11be35a1SLionel Sambuc 	bol = 1;
162*11be35a1SLionel Sambuc }
163*11be35a1SLionel Sambuc 
164*11be35a1SLionel Sambuc void
w_printf(const char * fmt,...)165*11be35a1SLionel Sambuc w_printf(const char *fmt, ...)
166*11be35a1SLionel Sambuc {
167*11be35a1SLionel Sambuc 	char buf[80];
168*11be35a1SLionel Sambuc 	va_list ap;
169*11be35a1SLionel Sambuc 	int n;
170*11be35a1SLionel Sambuc 
171*11be35a1SLionel Sambuc 	va_start(ap, fmt);
172*11be35a1SLionel Sambuc 	if (col >= 0) {
173*11be35a1SLionel Sambuc 		n = vsnprintf(buf, sizeof(buf), fmt, ap);
174*11be35a1SLionel Sambuc 		if (n >= (int)sizeof(buf)) {
175*11be35a1SLionel Sambuc 			col = -1;
176*11be35a1SLionel Sambuc 			goto overflow;
177*11be35a1SLionel Sambuc 		} else if (n == 0)
178*11be35a1SLionel Sambuc 			goto out;
179*11be35a1SLionel Sambuc 
180*11be35a1SLionel Sambuc 		if (!bol) {
181*11be35a1SLionel Sambuc 			if (col + n > 75)
182*11be35a1SLionel Sambuc 				fprintf(stderr, "\n    "), col = 4;
183*11be35a1SLionel Sambuc 			else
184*11be35a1SLionel Sambuc 				fprintf(stderr, " "), col++;
185*11be35a1SLionel Sambuc 		}
186*11be35a1SLionel Sambuc 		fprintf(stderr, "%s", buf);
187*11be35a1SLionel Sambuc 		col += n;
188*11be35a1SLionel Sambuc 		bol = 0;
189*11be35a1SLionel Sambuc 	} else {
190*11be35a1SLionel Sambuc overflow:
191*11be35a1SLionel Sambuc 		vfprintf(stderr, fmt, ap);
192*11be35a1SLionel Sambuc 	}
193*11be35a1SLionel Sambuc out:
194*11be35a1SLionel Sambuc 	va_end(ap);
195*11be35a1SLionel Sambuc }
196*11be35a1SLionel Sambuc 
197*11be35a1SLionel Sambuc ATF_TC(humanize_number_basic);
ATF_TC_HEAD(humanize_number_basic,tc)198*11be35a1SLionel Sambuc ATF_TC_HEAD(humanize_number_basic, tc)
199*11be35a1SLionel Sambuc {
200*11be35a1SLionel Sambuc 
201*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test humanize_number(3)");
202*11be35a1SLionel Sambuc }
203*11be35a1SLionel Sambuc 
ATF_TC_BODY(humanize_number_basic,tc)204*11be35a1SLionel Sambuc ATF_TC_BODY(humanize_number_basic, tc)
205*11be35a1SLionel Sambuc {
206*11be35a1SLionel Sambuc 	char fbuf[128];
207*11be35a1SLionel Sambuc 	const struct hnopts *ho;
208*11be35a1SLionel Sambuc 	char *buf = NULL;
209*11be35a1SLionel Sambuc 	size_t buflen = 0;
210*11be35a1SLionel Sambuc 	unsigned int i;
211*11be35a1SLionel Sambuc 	int rv = 0;
212*11be35a1SLionel Sambuc 
213*11be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(hnopts); i++) {
214*11be35a1SLionel Sambuc 		ho = &hnopts[i];
215*11be35a1SLionel Sambuc 		if (buflen < ho->ho_len) {
216*11be35a1SLionel Sambuc 			buflen = ho->ho_len;
217*11be35a1SLionel Sambuc 			buf = realloc(buf, buflen);
218*11be35a1SLionel Sambuc 			if (buf == NULL)
219*11be35a1SLionel Sambuc 				atf_tc_fail("realloc(..., %zu) failed", buflen);
220*11be35a1SLionel Sambuc 		}
221*11be35a1SLionel Sambuc 
222*11be35a1SLionel Sambuc 		rv = humanize_number(buf, ho->ho_len, ho->ho_num,
223*11be35a1SLionel Sambuc 		    ho->ho_suffix, ho->ho_scale, ho->ho_flags);
224*11be35a1SLionel Sambuc 
225*11be35a1SLionel Sambuc 		if (rv == ho->ho_retval &&
226*11be35a1SLionel Sambuc 		    (rv == -1 || strcmp(buf, ho->ho_retstr) == 0))
227*11be35a1SLionel Sambuc 			continue;
228*11be35a1SLionel Sambuc 
229*11be35a1SLionel Sambuc 		w_printf("humanize_number(\"%s\", %zu, %" PRId64 ",",
230*11be35a1SLionel Sambuc 		    ho->ho_retstr, ho->ho_len, ho->ho_num);
231*11be35a1SLionel Sambuc 		w_printf("\"%s\",", ho->ho_suffix);
232*11be35a1SLionel Sambuc 		w_printf("%s,", formatflags(fbuf, sizeof(fbuf), scale_flags,
233*11be35a1SLionel Sambuc 		    sizeof(scale_flags) / sizeof(scale_flags[0]),
234*11be35a1SLionel Sambuc 		    ho->ho_scale));
235*11be35a1SLionel Sambuc 		w_printf("%s)", formatflags(fbuf, sizeof(fbuf), normal_flags,
236*11be35a1SLionel Sambuc 		    sizeof(normal_flags) / sizeof(normal_flags[0]),
237*11be35a1SLionel Sambuc 		    ho->ho_flags));
238*11be35a1SLionel Sambuc 		w_printf("= %d,", ho->ho_retval);
239*11be35a1SLionel Sambuc 		w_printf("but got");
240*11be35a1SLionel Sambuc 		w_printf("%d/[%s]", rv, rv == -1 ? "" : buf);
241*11be35a1SLionel Sambuc 		newline();
242*11be35a1SLionel Sambuc 		atf_tc_fail_nonfatal("Failed for table entry %d", i);
243*11be35a1SLionel Sambuc 	}
244*11be35a1SLionel Sambuc }
245*11be35a1SLionel Sambuc 
246*11be35a1SLionel Sambuc ATF_TC(humanize_number_big);
ATF_TC_HEAD(humanize_number_big,tc)247*11be35a1SLionel Sambuc ATF_TC_HEAD(humanize_number_big, tc)
248*11be35a1SLionel Sambuc {
249*11be35a1SLionel Sambuc 
250*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test humanize "
251*11be35a1SLionel Sambuc 	    "big numbers (PR lib/44097)");
252*11be35a1SLionel Sambuc }
253*11be35a1SLionel Sambuc 
ATF_TC_BODY(humanize_number_big,tc)254*11be35a1SLionel Sambuc ATF_TC_BODY(humanize_number_big, tc)
255*11be35a1SLionel Sambuc {
256*11be35a1SLionel Sambuc 	char buf[1024];
257*11be35a1SLionel Sambuc 	int rv;
258*11be35a1SLionel Sambuc 
259*11be35a1SLionel Sambuc 	/*
260*11be35a1SLionel Sambuc 	 * Seems to work.
261*11be35a1SLionel Sambuc 	 */
262*11be35a1SLionel Sambuc 	(void)memset(buf, 0, sizeof(buf));
263*11be35a1SLionel Sambuc 
264*11be35a1SLionel Sambuc 	rv = humanize_number(buf, 10, 10000, "", HN_AUTOSCALE, HN_NOSPACE);
265*11be35a1SLionel Sambuc 
266*11be35a1SLionel Sambuc 	ATF_REQUIRE(rv != -1);
267*11be35a1SLionel Sambuc 	ATF_CHECK_STREQ(buf, "10000");
268*11be35a1SLionel Sambuc 
269*11be35a1SLionel Sambuc 	/*
270*11be35a1SLionel Sambuc 	 * A bogus value with large number.
271*11be35a1SLionel Sambuc 	 */
272*11be35a1SLionel Sambuc 	(void)memset(buf, 0, sizeof(buf));
273*11be35a1SLionel Sambuc 
274*11be35a1SLionel Sambuc 	rv = humanize_number(buf, 10, INT64_MAX, "", HN_AUTOSCALE, HN_NOSPACE);
275*11be35a1SLionel Sambuc 
276*11be35a1SLionel Sambuc 	ATF_REQUIRE(rv != -1);
277*11be35a1SLionel Sambuc 	ATF_REQUIRE(strcmp(buf, "0") != 0);
278*11be35a1SLionel Sambuc 
279*11be35a1SLionel Sambuc 	/*
280*11be35a1SLionel Sambuc 	 * Large buffer with HN_AUTOSCALE. Entirely bogus.
281*11be35a1SLionel Sambuc 	 */
282*11be35a1SLionel Sambuc 	(void)memset(buf, 0, sizeof(buf));
283*11be35a1SLionel Sambuc 
284*11be35a1SLionel Sambuc 	rv = humanize_number(buf, sizeof(buf), 10000, "",
285*11be35a1SLionel Sambuc 	    HN_AUTOSCALE, HN_NOSPACE);
286*11be35a1SLionel Sambuc 
287*11be35a1SLionel Sambuc 	ATF_REQUIRE(rv != -1);
288*11be35a1SLionel Sambuc 	ATF_REQUIRE(strcmp(buf, "0%d%s%d%s%s%s") != 0);
289*11be35a1SLionel Sambuc 
290*11be35a1SLionel Sambuc 	/*
291*11be35a1SLionel Sambuc 	 * Tight buffer.
292*11be35a1SLionel Sambuc 	 *
293*11be35a1SLionel Sambuc 	 * The man page says that len must be at least 4.
294*11be35a1SLionel Sambuc 	 * 3 works, but anything less that will not. This
295*11be35a1SLionel Sambuc 	 * is because baselen starts with 2 for positive
296*11be35a1SLionel Sambuc 	 * numbers.
297*11be35a1SLionel Sambuc 	 */
298*11be35a1SLionel Sambuc 	(void)memset(buf, 0, sizeof(buf));
299*11be35a1SLionel Sambuc 
300*11be35a1SLionel Sambuc 	rv = humanize_number(buf, 3, 1, "", HN_AUTOSCALE, HN_NOSPACE);
301*11be35a1SLionel Sambuc 
302*11be35a1SLionel Sambuc 	ATF_REQUIRE(rv != -1);
303*11be35a1SLionel Sambuc }
304*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)305*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
306*11be35a1SLionel Sambuc {
307*11be35a1SLionel Sambuc 
308*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, humanize_number_basic);
309*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, humanize_number_big);
310*11be35a1SLionel Sambuc 
311*11be35a1SLionel Sambuc 	return atf_no_error();
312*11be35a1SLionel Sambuc }
313