]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/regression/lib/libutil/test-humanize_number.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / regression / lib / libutil / test-humanize_number.c
1 /*-
2  * Copyright 2012 Clifton Royston
3  * Copyright 2013 John-Mark Gurney
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  *
29  */
30
31 #include <sys/types.h>
32 #include <stdlib.h>
33 #include <libutil.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <inttypes.h>
37 #include <math.h>
38 #include <unistd.h>
39 #include <limits.h>
40
41 extern char * optarg;
42
43 #define MAX_STR_FLAGS_RESULT    80
44 #define MAX_INT_STR_DIGITS      12
45
46 static const int64_t halfExabyte = (int64_t)500*1000*1000*1000*1000*1000L;
47
48 static struct {
49         int retval;
50         const char *res;
51         int64_t num;
52         int flags;
53         int scale;
54 } test_args[] = {
55         /* tests 0-13 test 1000 suffixes */
56         { 2, "0 ", (int64_t)0L, HN_DIVISOR_1000, HN_AUTOSCALE },
57         { 3, "1 k", (int64_t)500L, HN_DIVISOR_1000, HN_AUTOSCALE },
58         { 3, "1 M", (int64_t)500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
59         { 3, "1 G", (int64_t)500*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
60         { 3, "1 T", (int64_t)500*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
61         { 3, "1 P", (int64_t)500*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
62         { 3, "1 E", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
63         { 2, "1 ", (int64_t)1L, HN_DIVISOR_1000, HN_AUTOSCALE },
64         { 3, "2 k", (int64_t)1500L, HN_DIVISOR_1000, HN_AUTOSCALE },
65         { 3, "2 M", (int64_t)1500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
66         { 3, "2 G", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
67         { 3, "2 T", (int64_t)1500*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
68         { 3, "2 P", (int64_t)1500*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
69         { 3, "2 E", (int64_t)1500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
70
71         /* tests 14-27 test 1024 suffixes */
72         { 2, "0 ", (int64_t)0L, 0, HN_AUTOSCALE },
73         { 3, "1 K", (int64_t)512L, 0, HN_AUTOSCALE },
74         { 3, "1 M", (int64_t)512*1024L, 0, HN_AUTOSCALE },
75         { 3, "1 G", (int64_t)512*1024*1024L, 0, HN_AUTOSCALE },
76         { 3, "1 T", (int64_t)512*1024*1024*1024L, 0, HN_AUTOSCALE },
77         { 3, "1 P", (int64_t)512*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
78         { 3, "1 E", (int64_t)512*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
79         { 2, "1 ", (int64_t)1L, 0, HN_AUTOSCALE },
80         { 3, "2 K", (int64_t)1536L, 0, HN_AUTOSCALE },
81         { 3, "2 M", (int64_t)1536*1024L, 0, HN_AUTOSCALE },
82         { 3, "2 G", (int64_t)1536*1024*1024L, 0, HN_AUTOSCALE },
83         { 3, "2 T", (int64_t)1536*1024*1024*1024L, 0, HN_AUTOSCALE },
84         { 3, "2 P", (int64_t)1536*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
85         { 3, "2 E", (int64_t)1536*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
86
87         /* tests 28-37 test rounding */
88         { 3, "0 M", (int64_t)500*1000L-1, HN_DIVISOR_1000, HN_AUTOSCALE },
89         { 3, "1 M", (int64_t)500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
90         { 3, "1 M", (int64_t)1000*1000L + 500*1000L-1, HN_DIVISOR_1000, HN_AUTOSCALE },
91         { 3, "2 M", (int64_t)1000*1000L + 500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
92         { 3, "0 K", (int64_t)512L-1, 0, HN_AUTOSCALE },
93         { 3, "1 K", (int64_t)512L, 0, HN_AUTOSCALE },
94         { 3, "0 M", (int64_t)512*1024L-1, 0, HN_AUTOSCALE },
95         { 3, "1 M", (int64_t)512*1024L, 0, HN_AUTOSCALE },
96         { 3, "1 M", (int64_t)1024*1024L + 512*1024L-1, 0, HN_AUTOSCALE },
97         { 3, "2 M", (int64_t)1024*1024L + 512*1024L, 0, HN_AUTOSCALE },
98
99         /* tests 38-61 test specific scale factors with 1000 divisor */
100         { 3, "0 k", (int64_t)0L, HN_DIVISOR_1000, 1 },
101         { 3, "1 k", (int64_t)500L, HN_DIVISOR_1000, 1 },
102         { 3, "0 M", (int64_t)500L, HN_DIVISOR_1000, 2 },
103         { 3, "1 M", (int64_t)500*1000L, HN_DIVISOR_1000, 2 },
104         { 3, "0 G", (int64_t)500*1000L, HN_DIVISOR_1000, 3 },
105         { 3, "1 G", (int64_t)500*1000*1000L, HN_DIVISOR_1000, 3 },
106         { 3, "0 T", (int64_t)500*1000*1000L, HN_DIVISOR_1000, 4 },
107         { 3, "1 T", (int64_t)500*1000*1000*1000L, HN_DIVISOR_1000, 4 },
108         { 3, "0 P", (int64_t)500*1000*1000*1000L, HN_DIVISOR_1000, 5 },
109         { 3, "1 P", (int64_t)500*1000*1000*1000*1000L, HN_DIVISOR_1000, 5 },
110         { 3, "0 E", (int64_t)500*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 },
111         { 3, "1 E", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 },
112         { 3, "0 k", (int64_t)1L, HN_DIVISOR_1000, 1 },
113         { 3, "2 k", (int64_t)1500L, HN_DIVISOR_1000, 1 },
114         { 3, "0 M", (int64_t)1500L, HN_DIVISOR_1000, 2 },
115         { 3, "2 M", (int64_t)1500*1000L, HN_DIVISOR_1000, 2 },
116         { 3, "0 G", (int64_t)1500*1000L, HN_DIVISOR_1000, 3 },
117         { 3, "2 G", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, 3 },
118         { 3, "0 T", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, 4 },
119         { 3, "2 T", (int64_t)1500*1000*1000*1000L, HN_DIVISOR_1000, 4 },
120         { 3, "0 P", (int64_t)1500*1000*1000*1000L, HN_DIVISOR_1000, 5 },
121         { 3, "2 P", (int64_t)1500*1000*1000*1000*1000L, HN_DIVISOR_1000, 5 },
122         { 3, "0 E", (int64_t)1500*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 },
123         { 3, "2 E", (int64_t)1500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 },
124
125         /* tests 62-85 test specific scale factors with 1024 divisor */
126         { 3, "0 K", (int64_t)0L, 0, 1 },
127         { 3, "1 K", (int64_t)512L, 0, 1 },
128         { 3, "0 M", (int64_t)512L, 0, 2 },
129         { 3, "1 M", (int64_t)512*1024L, 0, 2 },
130         { 3, "0 G", (int64_t)512*1024L, 0, 3 },
131         { 3, "1 G", (int64_t)512*1024*1024L, 0, 3 },
132         { 3, "0 T", (int64_t)512*1024*1024L, 0, 4 },
133         { 3, "1 T", (int64_t)512*1024*1024*1024L, 0, 4 },
134         { 3, "0 P", (int64_t)512*1024*1024*1024L, 0, 5 },
135         { 3, "1 P", (int64_t)512*1024*1024*1024*1024L, 0, 5 },
136         { 3, "0 E", (int64_t)512*1024*1024*1024*1024L, 0, 6 },
137         { 3, "1 E", (int64_t)512*1024*1024*1024*1024*1024L, 0, 6 },
138         { 3, "0 K", (int64_t)1L, 0, 1 },
139         { 3, "2 K", (int64_t)1536L, 0, 1 },
140         { 3, "0 M", (int64_t)1536L, 0, 2 },
141         { 3, "2 M", (int64_t)1536*1024L, 0, 2 },
142         { 3, "0 G", (int64_t)1536*1024L, 0, 3 },
143         { 3, "2 G", (int64_t)1536*1024*1024L, 0, 3 },
144         { 3, "0 T", (int64_t)1536*1024*1024L, 0, 4 },
145         { 3, "2 T", (int64_t)1536*1024*1024*1024L, 0, 4 },
146         { 3, "0 P", (int64_t)1536*1024*1024*1024L, 0, 5 },
147         { 3, "2 P", (int64_t)1536*1024*1024*1024*1024L, 0, 5 },
148         { 3, "0 E", (int64_t)1536*1024*1024*1024*1024L, 0, 6 },
149         { 3, "2 E", (int64_t)1536*1024*1024*1024*1024*1024L, 0, 6 },
150
151         /* tests 86-99 test invalid specific scale values of < 0 or >= 7 with
152         and without HN_DIVISOR_1000 set */
153         /*  all should return errors with new code; with old, the latter 3
154         are instead processed as if having AUTOSCALE and/or GETSCALE set */
155         { -1, "", (int64_t)1L, 0, 7 },
156         { -1, "", (int64_t)1L, HN_DIVISOR_1000, 7 },
157         { -1, "", (int64_t)1L, 0, 1000 },
158         { -1, "", (int64_t)1L, HN_DIVISOR_1000, 1000 },
159         { -1, "", (int64_t)0L, 0, 1000*1000 },
160         { -1, "", (int64_t)0L, HN_DIVISOR_1000, 1000*1000 },
161         { -1, "", (int64_t)0L, 0, INT_MAX },
162         { -1, "", (int64_t)0L, HN_DIVISOR_1000, INT_MAX },
163
164         /* Negative scale values are not handled well
165          by the existing library routine - should report as error */
166         /*  all should return errors with new code, fail assertion with old */
167
168         { -1, "", (int64_t)1L, 0, -1 },
169         { -1, "", (int64_t)1L, HN_DIVISOR_1000, -1 },
170         { -1, "", (int64_t)1L, 0, -1000 },
171         { -1, "", (int64_t)1L, HN_DIVISOR_1000, -1000 },
172
173         /* __INT_MIN doesn't print properly, skipped. */
174
175         { -1, "", (int64_t)1L, 0, -__INT_MAX },
176         { -1, "", (int64_t)1L, HN_DIVISOR_1000, -__INT_MAX },
177
178
179         /* tests for scale == 0, without autoscale */
180         /* tests 100-114 test scale 0 with 1000 divisor - print first N digits */
181         { 2, "0 ", (int64_t)0L, HN_DIVISOR_1000, 0 },
182         { 2, "1 ", (int64_t)1L, HN_DIVISOR_1000, 0 },
183         { 3, "10 ", (int64_t)10L, HN_DIVISOR_1000, 0 },
184         { 3, "0 M", (int64_t)150L, HN_DIVISOR_1000, HN_NOSPACE },
185         { 3, "0 M", (int64_t)500L, HN_DIVISOR_1000, HN_NOSPACE },
186         { 3, "0 M", (int64_t)999L, HN_DIVISOR_1000, HN_NOSPACE },
187         { 4, "150", (int64_t)150L, HN_DIVISOR_1000, 0 },
188         { 4, "500", (int64_t)500L, HN_DIVISOR_1000, 0 },
189         { 4, "999", (int64_t)999L, HN_DIVISOR_1000, 0 },
190         { 5, "100", (int64_t)1000L, HN_DIVISOR_1000, 0 },
191         { 5, "150", (int64_t)1500L, HN_DIVISOR_1000, 0 },
192         { 7, "500", (int64_t)500*1000L, HN_DIVISOR_1000, 0 },
193         { 8, "150", (int64_t)1500*1000L, HN_DIVISOR_1000, 0 },
194         { 10, "500", (int64_t)500*1000*1000L, HN_DIVISOR_1000, 0 },
195         { 11, "150", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, 0 },
196
197         /* tests 115-126 test scale 0 with 1024 divisor - print first N digits */
198         { 2, "0 ", (int64_t)0L, 0, 0 },
199         { 2, "1 ", (int64_t)1L, 0, 0 },
200         { 3, "10 ", (int64_t)10L, 0, 0 },
201         { 4, "150", (int64_t)150L, 0, 0 },
202         { 4, "500", (int64_t)500L, 0, 0 },
203         { 4, "999", (int64_t)999L, 0, 0 },
204         { 5, "100", (int64_t)1000L, 0, 0 },
205         { 5, "150", (int64_t)1500L, 0, 0 },
206         { 7, "500", (int64_t)500*1000L, 0, 0 },
207         { 8, "150", (int64_t)1500*1000L, 0, 0 },
208         { 10, "500", (int64_t)500*1000*1000L, 0, 0 },
209         { 11, "150", (int64_t)1500*1000*1000L, 0, 0 },
210
211         /* Test boundary cases for very large positive/negative number formatting */
212         /* Explicit scale, divisor 1024 */
213
214         /* XXX = requires length 5 (buflen 6) for some cases*/
215         /* KLUDGE - test loop below will bump length 5 up to 5 */
216         { 3, "8 E",   INT64_MAX, 0, 6 },
217         { 4, "-8 E", -INT64_MAX, 0, 6 },
218         { 3, "0 E", (int64_t)92*1024*1024*1024*1024*1024L, 0, 6 },
219         { 3, "0 E", -(int64_t)92*1024*1024*1024*1024*1024L, 0, 6 },
220         { 3, "0 E", (int64_t)82*1024*1024*1024*1024*1024L, 0, 6 },
221         { 3, "0 E", -(int64_t)82*1024*1024*1024*1024*1024L, 0, 6 },
222         { 3, "0 E", (int64_t)81*1024*1024*1024*1024*1024L, 0, 6 },
223         { 3, "0 E", -(int64_t)81*1024*1024*1024*1024*1024L, 0, 6 },
224         { 4, "92 P", (int64_t)92*1024*1024*1024*1024*1024L, 0, 5 },
225         { 5, "-92 P", -(int64_t)92*1024*1024*1024*1024*1024L, 0, 5 },
226         { 4, "82 P", (int64_t)82*1024*1024*1024*1024*1024L, 0, 5 },
227         { 5, "-82 P", -(int64_t)82*1024*1024*1024*1024*1024L, 0, 5 },
228         { 4, "81 P", (int64_t)81*1024*1024*1024*1024*1024L, 0, 5 },
229         { 5, "-81 P", -(int64_t)81*1024*1024*1024*1024*1024L, 0, 5 },
230
231         /* Explicit scale, divisor 1000 */
232         { 3, "9 E",   INT64_MAX, HN_DIVISOR_1000, 6 },
233         { 4, "-9 E", -INT64_MAX, HN_DIVISOR_1000,  6 },
234         { 3, "0 E", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  6 },
235         { 3, "0 E", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  6 },
236         { 3, "0 E", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  6 },
237         { 3, "0 E", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  6 },
238         { 4, "92 P", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  5 },
239         { 5, "-92 P", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  5 },
240         { 4, "91 P", (int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  5 },
241         { 5, "-91 P", -(int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  5 },
242
243         /* Autoscale, divisor 1024 */
244         { 3, "8 E",   INT64_MAX, 0, HN_AUTOSCALE },
245         { 4, "-8 E", -INT64_MAX, 0, HN_AUTOSCALE },
246         { 4, "92 P", (int64_t)92*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
247         { 5, "-92 P", -(int64_t)92*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
248         { 4, "82 P", (int64_t)82*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
249         { 5, "-82 P", -(int64_t)82*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
250         { 4, "81 P", (int64_t)81*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
251         { 5, "-81 P", -(int64_t)81*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
252         /* Autoscale, divisor 1000 */
253         { 3, "9 E",   INT64_MAX, HN_DIVISOR_1000, HN_AUTOSCALE },
254         { 4, "-9 E", -INT64_MAX, HN_DIVISOR_1000, HN_AUTOSCALE },
255         { 4, "92 P", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE },
256         { 5, "-92 P", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE },
257         { 4, "91 P", (int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE },
258         { 5, "-91 P", -(int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE },
259
260         /* 0 scale, divisor 1024 */
261         { 12, "skdj",  INT64_MAX, 0, 0 },
262         { 21, "-9223", -INT64_MAX, 0, 0 },
263         { 19, "10358", (int64_t)92*1024*1024*1024*1024*1024L, 0, 0 },
264         { 20, "-1035", -(int64_t)92*1024*1024*1024*1024*1024L, 0, 0 },
265         { 18, "92323", (int64_t)82*1024*1024*1024*1024*1024L, 0, 0 },
266         { 19, "-9232", -(int64_t)82*1024*1024*1024*1024*1024L, 0, 0 },
267         { 18, "91197", (int64_t)81*1024*1024*1024*1024*1024L, 0, 0 },
268         { 19, "-9119", -(int64_t)81*1024*1024*1024*1024*1024L, 0, 0 },
269
270         /* 0 scale, divisor 1000 */
271         /* XXX - why does this fail? */
272         { -1, "", INT64_MAX, HN_DIVISOR_1000, 0 },
273         { 21, "-9223", -INT64_MAX, HN_DIVISOR_1000,  0 },
274         { 19, "10358", (int64_t)92*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  0 },
275         { 20, "-1035", -(int64_t)92*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  0 },
276         { 18, "92323", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  0 },
277         { 19, "-9232", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  0 },
278         /* Expected to pass */
279         { 18, "91197", (int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  0 },
280         { 19, "-9119", -(int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  0 },
281
282
283
284         /* Need to implement tests for GETSCALE */
285 /*      { ?, "", (int64_t)0L, HN_DIVISOR_1000, HN_GETSCALE },
286         ...
287 */
288         /* Tests for HN_DECIMAL */
289         /* Positive, Autoscale */
290         { 5, "500 k", (int64_t)500*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
291         { 5, "994 k", (int64_t)994*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
292         { 5, "995 k", (int64_t)995*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
293         { 5, "999 k", (int64_t)999*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
294         { 5, "1.0 M", (int64_t)1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
295         { 5, "1.5 M", (int64_t)1500*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
296         { 5, "1.9 M", (int64_t)1949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
297         { 5, "2.0 M", (int64_t)1950*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
298         { 5, "9.9 M", (int64_t)9949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
299         { 4, "10 M", (int64_t)9950*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
300         { 5, "500 M", (int64_t)500*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
301         { 5, "994 M", (int64_t)994*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
302         { 5, "995 M", (int64_t)995*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
303         { 5, "999 M", (int64_t)999*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
304
305         { 5, "500 K", (int64_t)500*1024L, HN_DECIMAL, HN_AUTOSCALE },
306         { 5, "994 K", (int64_t)994*1024L, HN_DECIMAL, HN_AUTOSCALE },
307         { 5, "995 K", (int64_t)995*1024L, HN_DECIMAL, HN_AUTOSCALE },
308         { 5, "999 K", (int64_t)999*1024L, HN_DECIMAL, HN_AUTOSCALE },
309         { 5, "1.0 M", (int64_t)1000*1024L, HN_DECIMAL, HN_AUTOSCALE },
310         { 5, "1.0 M", (int64_t)1018*1024L, HN_DECIMAL, HN_AUTOSCALE },
311         { 5, "1.0 M", (int64_t)1019*1024L, HN_DECIMAL, HN_AUTOSCALE },
312         { 5, "1.5 M", (int64_t)1536*1024L, HN_DECIMAL, HN_AUTOSCALE },
313         { 5, "1.9 M", (int64_t)1996*1024L, HN_DECIMAL, HN_AUTOSCALE },
314         { 5, "2.0 M", (int64_t)1997*1024L, HN_DECIMAL, HN_AUTOSCALE },
315         { 5, "2.0 M", (int64_t)2047*1024L, HN_DECIMAL, HN_AUTOSCALE },
316         { 5, "2.0 M", (int64_t)2048*1024L, HN_DECIMAL, HN_AUTOSCALE },
317         { 5, "2.0 M", (int64_t)2099*1024L, HN_DECIMAL, HN_AUTOSCALE },
318         { 5, "2.1 M", (int64_t)2100*1024L, HN_DECIMAL, HN_AUTOSCALE },
319         { 5, "9.9 M", (int64_t)10188*1024L, HN_DECIMAL, HN_AUTOSCALE },
320         /* XXX - shouldn't the following two be "10. M"? */
321         { 4, "10 M", (int64_t)10189*1024L, HN_DECIMAL, HN_AUTOSCALE },
322         { 4, "10 M", (int64_t)10240*1024L, HN_DECIMAL, HN_AUTOSCALE },
323         { 5, "500 M", (int64_t)500*1024*1024L, HN_DECIMAL, HN_AUTOSCALE },
324         { 5, "994 M", (int64_t)994*1024*1024L, HN_DECIMAL, HN_AUTOSCALE },
325         { 5, "995 M", (int64_t)995*1024*1024L, HN_DECIMAL, HN_AUTOSCALE },
326         { 5, "999 M", (int64_t)999*1024*1024L, HN_DECIMAL, HN_AUTOSCALE },
327         { 5, "1.0 G", (int64_t)1000*1024*1024L, HN_DECIMAL, HN_AUTOSCALE },
328         { 5, "1.0 G", (int64_t)1023*1024*1024L, HN_DECIMAL, HN_AUTOSCALE },
329
330         /* Negative, Autoscale - should pass */
331         { 6, "-1.5 ", -(int64_t)1500*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
332         { 6, "-1.9 ", -(int64_t)1949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
333         { 6, "-9.9 ", -(int64_t)9949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
334
335         { 6, "-1.5 ", -(int64_t)1536*1024L, HN_DECIMAL, HN_AUTOSCALE },
336         { 6, "-1.9 ", -(int64_t)1949*1024L, HN_DECIMAL, HN_AUTOSCALE },
337         { 6, "-9.7 ", -(int64_t)9949*1024L, HN_DECIMAL, HN_AUTOSCALE },
338
339         /* Positive/negative, at maximum scale */
340         { 5, "500 P", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
341         { 5, "1.9 E", (int64_t)1949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
342         { 5, "8.9 E", (int64_t)8949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
343         { 5, "9.2 E", INT64_MAX, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
344         /* Negatives work with latest rev only: */
345         { 6, "-9.2 ", -INT64_MAX, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
346         { 6, "-8.9 ", -(int64_t)8949*1000*1000*1000*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
347
348         { 5, "8.0 E",   INT64_MAX, HN_DECIMAL, HN_AUTOSCALE },
349         { 5, "7.9 E",   INT64_MAX-(int64_t)100*1024*1024*1024*1024*1024LL, HN_DECIMAL, HN_AUTOSCALE },
350         { 6, "-8.0 ", -INT64_MAX, HN_DECIMAL, HN_AUTOSCALE },
351         { 6, "-7.9 ",   -INT64_MAX+(int64_t)100*1024*1024*1024*1024*1024LL, HN_DECIMAL, HN_AUTOSCALE },
352
353         /* Positive, Fixed scales */
354         { 5, "500 k", (int64_t)500*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1 },
355         { 5, "0.5 M", (int64_t)500*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
356         { 5, "949 k", (int64_t)949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1 },
357         { 5, "0.9 M", (int64_t)949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
358         { 5, "950 k", (int64_t)950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1 },
359         { 5, "1.0 M", (int64_t)950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
360         { 5, "999 k", (int64_t)999*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1 },
361         { 5, "1.0 M", (int64_t)999*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
362         { 5, "1.5 M", (int64_t)1500*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
363         { 5, "1.9 M", (int64_t)1949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
364         { 5, "2.0 M", (int64_t)1950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
365         { 5, "9.9 M", (int64_t)9949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
366         { 4, "10 M", (int64_t)9950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
367         { 5, "500 M", (int64_t)500*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
368         { 5, "0.5 G", (int64_t)500*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 3 },
369         { 5, "999 M", (int64_t)999*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
370         { 5, "1.0 G", (int64_t)999*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 3 },
371         /* Positive/negative, at maximum scale */
372         { 5, "500 P", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 5 },
373         { 5, "1.0 E", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 },
374         { 5, "1.9 E", (int64_t)1949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 },
375         { 5, "8.9 E", (int64_t)8949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 },
376         { 5, "9.2 E", INT64_MAX, HN_DECIMAL|HN_DIVISOR_1000, 6 },
377
378         /* HN_DECIMAL + binary + fixed scale cases not completed */
379         { 5, "512 K", (int64_t)512*1024L, HN_DECIMAL, 1 },
380         { 5, "0.5 M", (int64_t)512*1024L, HN_DECIMAL, 2 },
381
382         /* Negative, Fixed scales */
383         /* Not yet added, but should work with latest rev */
384
385 };
386
387
388 /* Command line options usage */
389 static void
390 usage(char * progname) {
391         printf("%s: tests libutil humanize_number function\n", progname);
392         printf("Usage: %s [-nE] [-l num] [-v]\n\n", progname);
393         printf("Options:\n");
394         printf("\t-l num\tSet max length for result; buflen = num + 1\n");
395         printf("\t\t  (NOTE: does not change expected result strings.)\n");
396         printf("\t-n\tInclude negative scale tests, which cause older libutil\n");
397         printf("\t\t  version of function to coredump with assertion failure\n");
398         printf("\t-E\tInclude numbers > 1/2 Exa[byte] which currently fail\n");
399         printf("\t-v\tVerbose - always print summary results\n");
400         printf("\t-h, -?\tShow options\n");
401 }
402
403 /* Parse command line options */
404 static void
405 read_options(int argc, char * const argv[], size_t *bufLength,
406     int *includeNegativeScale, int *includeExabytes, int *verbose) {
407         int ch;
408         size_t temp;
409
410         while ((ch = getopt(argc, argv, "nEh?vl:")) != -1) {
411                 switch (ch) {
412                         default:
413                                 usage(argv[0]);
414                                 exit(1);
415                                 break;  /* UNREACHABLE */
416                         case 'h' :
417                         case '?' :
418                                 usage(argv[0]);
419                                 exit(0);
420                                 break;  /* UNREACHABLE */
421                         case 'l' :
422                                 sscanf(optarg, "%zu", &temp);
423                                 *bufLength = temp + 1;
424                                 break;
425                         case 'n' :
426                                 *includeNegativeScale = 1;
427                                 break;
428                         case 'E' :
429                                 *includeExabytes = 1;
430                                 break;
431                         case 'v' :
432                                 *verbose = 1;
433                                 break;
434                 }
435         }
436 }
437
438 static struct {
439         int value;
440         const char *name;
441  } flags[] = {
442         { HN_AUTOSCALE, "HN_AUTOSCALE" },
443         { HN_GETSCALE, "HN_GETSCALE" },
444         { HN_DIVISOR_1000, "HN_DIVISOR_1000"},
445         { HN_B, "HN_B"},
446         { HN_DECIMAL, "HN_DECIMAL"},
447 };
448
449 static const char *separator = "|";
450
451 /* Format flags parameter for meaningful display */
452 static char *
453 str_flags(int hn_flags, char *noFlags) {
454         size_t i;
455         char * result;
456
457         result = malloc(MAX_STR_FLAGS_RESULT);
458         result[0] = '\0';
459
460         for (i = 0; i < sizeof flags / sizeof *flags; i++) {
461                 if (hn_flags & flags[i].value) {
462                         if (*result != 0)
463                                 strlcat(result, separator,
464                                     MAX_STR_FLAGS_RESULT);
465                         strlcat(result, flags[i].name, MAX_STR_FLAGS_RESULT);
466                 }
467         }
468
469         if (strlen(result) == 0)
470                 strlcat(result, noFlags, MAX_STR_FLAGS_RESULT);
471         return result;
472 }
473
474
475 /* Format scale parameter for meaningful display */
476 static char *
477 str_scale(int scale) {
478         char *result;
479
480         if (scale == HN_AUTOSCALE || scale == HN_GETSCALE)
481                 return str_flags(scale, "");
482
483         result = malloc(MAX_INT_STR_DIGITS);
484         result[0] = '\0';
485         snprintf(result, MAX_INT_STR_DIGITS, "%d", scale);
486         return result;
487 }
488
489 static void
490 testskipped(size_t i)
491 {
492
493         printf("ok %lu # skip - not turned on\n", i);
494 }
495
496 int
497 main(int argc, char * const argv[])
498 {
499         char *buf;
500         char *flag_str, *scale_str;
501         size_t i;
502         size_t errcnt, tested, skipped;
503         int r;
504         size_t buflen;
505         int includeNegScale;
506         int includeExabyteTests;
507         int verbose;
508
509         buflen = 4;
510         includeNegScale = 0;
511         includeExabyteTests = 0;
512         verbose = 0;
513
514         read_options(argc, argv, &buflen, &includeNegScale,
515             &includeExabyteTests, &verbose);
516
517         buf = malloc(buflen);
518         errcnt = 0;
519         tested = 0;
520         skipped = 0;
521
522         if (buflen != 4)
523                 printf("Warning: buffer size %zu != 4, expect some results to differ.\n", buflen);
524
525         printf("1..%lu\n", sizeof test_args / sizeof *test_args);
526         for (i = 0; i < sizeof test_args / sizeof *test_args; i++) {
527                 /* KLUDGE */
528                 if (test_args[i].num == INT64_MAX && buflen == 4) {
529                         /* Start final tests which require buffer of 6 */
530                         free(buf);
531                         buflen = 6;
532                         buf = malloc(buflen);
533                         if (verbose)
534                                 printf("Buffer length increased to %zu\n",
535                                     buflen);
536                 }
537
538                 if (test_args[i].scale < 0 && ! includeNegScale) {
539                         skipped++;
540                         testskipped(i);
541                         continue;
542                 }
543                 if (test_args[i].num >= halfExabyte && ! includeExabyteTests) {
544                         skipped++;
545                         testskipped(i);
546                         continue;
547                 }
548
549                 r = humanize_number(buf, buflen, test_args[i].num, "",
550                     test_args[i].scale, test_args[i].flags);
551                 flag_str = str_flags(test_args[i].flags, "[no flags]");
552                 scale_str = str_scale(test_args[i].scale);
553
554                 if (r != test_args[i].retval) {
555                         if (verbose)
556                                 printf("wrong return value on index %lu, buflen: %zu, got: %d + \"%s\", expected %d + \"%s\"; num = %" PRId64 ", scale = %s, flags= %s.\n",
557                                     i, buflen, r, buf, test_args[i].retval,
558                                     test_args[i].res, test_args[i].num,
559                                     scale_str, flag_str);
560                         else
561                                 printf("not ok %lu # return %d != %d\n", i, r,
562                                     test_args[i].retval);
563                         errcnt++;
564                 } else if (strcmp(buf, test_args[i].res) != 0) {
565                         if (verbose)
566                                 printf("result mismatch on index %lu, got: \"%s\", expected \"%s\"; num = %" PRId64 ", scale = %s, flags= %s.\n",
567                                     i, buf, test_args[i].res, test_args[i].num,
568                                     scale_str, flag_str);
569                         else
570                                 printf("not ok %lu # buf \"%s\" != \"%s\"\n", i,
571                                     buf, test_args[i].res);
572                         errcnt++;
573                 } else {
574                         if (verbose)
575                                 printf("successful result on index %lu, returned %d, got: \"%s\"; num = %" PRId64 ", scale = %s, flags= %s.\n",
576                                     i, r, buf, test_args[i].num, scale_str,
577                                     flag_str);
578                         else
579                                 printf("ok %lu\n", i);
580                 }
581                 tested++;
582         }
583
584         if (verbose)
585                 printf("total errors: %lu/%lu tests, %lu skipped\n", errcnt,
586                     tested, skipped);
587
588         if (errcnt)
589                 return 1;
590
591         return 0;
592 }