xref: /openbsd-src/regress/lib/libutil/fmt_scaled/fmt_test.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /* $OpenBSD: fmt_test.c,v 1.16 2020/02/14 19:17:34 schwarze Exp $ */
2 
3 /*
4  * Combined tests for fmt_scaled and scan_scaled.
5  * Ian Darwin, January 2001. Public domain.
6  */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <errno.h>
13 #include <limits.h>
14 #include <unistd.h>
15 
16 #include <util.h>
17 
18 static int fmt_test(void);
19 static int scan_test(void);
20 
21 static void print_errno(int e);
22 static int assert_int(int testnum, int checknum, int expect, int result);
23 static int assert_errno(int testnum, int checknum, int expect, int result);
24 static int assert_llong(int testnum, int checknum, long long expect, long long result);
25 static int assert_str(int testnum, int checknum, char * expect, char * result);
26 
27 extern char *__progname;
28 static int verbose = 0;
29 
30 __dead static void usage(int stat)
31 {
32 	fprintf(stderr, "usage: %s [-v]\n", __progname);
33 	exit(stat);
34 }
35 
36 int
37 main(int argc, char **argv)
38 {
39 	int i, ch;
40 
41 	while ((ch = getopt(argc, argv, "hv")) != -1) {
42 			switch (ch) {
43 			case 'v':
44 					verbose = 1;
45 					break;
46 			case 'h':
47 					usage(0);
48 			case '?':
49 			default:
50 					usage(1);
51 			}
52 	}
53 	argc -= optind;
54 	argv += optind;
55 
56 	if (verbose)
57 		printf("Starting fmt_test\n");
58 	i = fmt_test();
59 	if (verbose)
60 		printf("Starting scan_test\n");
61 	i += scan_test();
62 	if (i) {
63 		printf("*** %d errors in libutil/fmt_scaled tests ***\n", i);
64 	} else {
65 		if (verbose)
66 			printf("Tests done; no unexpected errors\n");
67 	}
68 	return i;
69 }
70 
71 /************** tests for fmt_scaled *******************/
72 
73 static struct {			/* the test cases */
74 	long long input;
75 	char *expect;
76 	int err;
77 } ddata[] = {
78 	{ 0, "0B", 0 },
79 	{ 1, "1B", 0 },
80 	{ -1, "-1B", 0 },
81 	{ 100, "100B", 0},
82 	{ -100, "-100B", 0},
83 	{ 999, "999B", 0 },
84 	{ 1000, "1000B", 0 },
85 	{ 1023, "1023B", 0 },
86 	{ -1023, "-1023B", 0 },
87 	{ 1024, "1.0K", 0 },
88 	{ 1025, "1.0K", 0 },
89 	{ 1234, "1.2K", 0 },
90 	{ -1234, "-1.2K", 0 },
91 	{ 1484, "1.4K", 0 },		/* rounding boundary, down */
92 	{ 1485, "1.5K", 0 },		/* rounding boundary, up   */
93 	{ -1484, "-1.4K", 0 },		/* rounding boundary, down */
94 	{ -1485, "-1.5K", 0 },		/* rounding boundary, up   */
95 	{ 1536, "1.5K", 0 },
96 	{ 1786, "1.7K", 0 },
97 	{ 1800, "1.8K", 0 },
98 	{ 2000, "2.0K", 0 },
99 	{ 123456, "121K", 0 },
100 	{ 578318, "565K", 0 },
101 	{ 902948, "882K", 0 },
102 	{ 1048576, "1.0M", 0},
103 	{ 1048628, "1.0M", 0},
104 	{ 1049447, "1.0M", 0},
105 	{ -102400, "-100K", 0},
106 	{ -103423, "-101K", 0 },
107 	{ 7299072, "7.0M", 0 },
108 	{ 409478144L, "391M", 0 },
109 	{ -409478144L, "-391M", 0 },
110 	{ 999999999L, "954M", 0 },
111 	{ 1499999999L, "1.4G", 0 },
112 	{ 12475423744LL, "11.6G", 0},
113 	{ 1LL<<61, "2.0E", 0 },
114 	{ 1LL<<62, "4.0E", 0 },
115 	{ 1LL<<63, "", ERANGE },
116 	{ 1099512676352LL, "1.0T", 0}
117 };
118 #	define DDATA_LENGTH (sizeof ddata/sizeof *ddata)
119 
120 static int
121 fmt_test(void)
122 {
123 	unsigned int i, e, errs = 0;
124 	int ret;
125 	char buf[FMT_SCALED_STRSIZE];
126 
127 	for (i = 0; i < DDATA_LENGTH; i++) {
128 		strlcpy(buf, "UNSET", FMT_SCALED_STRSIZE);
129 		errno = 0;
130 		ret = fmt_scaled(ddata[i].input, buf);
131 		e = errno;
132 		if (verbose) {
133 			printf("%lld --> %s (%d)", ddata[i].input, buf, ret);
134 			if (ret == -1)
135 				print_errno(e);
136 			printf("\n");
137 		}
138 		if (ret == -1)
139 			errs += assert_int(i, 1, ret, ddata[i].err == 0 ? 0 : -1);
140 		if (ddata[i].err)
141 			errs += assert_errno(i, 2, ddata[i].err, e);
142 		else
143 			errs += assert_str(i, 3, ddata[i].expect, buf);
144 	}
145 
146 	return errs;
147 }
148 
149 /************** tests for scan_scaled *******************/
150 
151 
152 #define	IMPROBABLE	(-42)
153 
154 struct {					/* the test cases */
155 	char *input;
156 	long long result;
157 	int err;
158 } sdata[] = {
159 	{ "0",		0, 0 },
160 	{ "123",	123, 0 },
161 	{ "1k",		1024, 0 },		/* lower case */
162 	{ "100.944", 100, 0 },	/* should --> 100 (truncates fraction) */
163 	{ "10099",	10099LL, 0 },
164 	{ "1M",		1048576LL, 0 },
165 	{ "1.1M",	1153433LL, 0 },		/* fractions */
166 	{ "1.111111111111111111M",	1165084LL, 0 },		/* fractions */
167 	{ "1.55M",	1625292LL, 0 },	/* fractions */
168 	{ "1.9M",	1992294LL, 0 },		/* fractions */
169 	{ "-2K",	-2048LL, 0 },		/* negatives */
170 	{ "-2.2K",	-2252LL, 0 },	/* neg with fract */
171 	{ "4.5k", 4608, 0 },
172 	{ "4.5555555555555555K", 4664, 0 },
173 	{ "4.5555555555555555555K", 4664, 0 },	/* handle enough digits? */
174 	{ "4.555555555555555555555555555555K", 4664, 0 }, /* ignores extra digits? */
175 	{ "1G",		1073741824LL, 0 },
176 	{ "G", 		0, 0 },			/* should == 0G? */
177 	{ "1234567890", 1234567890LL, 0 },	/* should work */
178 	{ "1.5E",	1729382256910270464LL, 0 },		/* big */
179 	{ "32948093840918378473209480483092", 0, ERANGE },  /* too big */
180 	{ "1.5Q",	0, EINVAL },		/* invalid multiplier */
181 	{ "1ab",	0, EINVAL },		/* ditto */
182 	{ "3&",		0, EINVAL },		/* ditto */
183 	{ "5.0e3",	0, EINVAL },	/* digits after */
184 	{ "5.0E3",	0, EINVAL },	/* ditto */
185 	{ "1..0",	0, EINVAL },		/* bad format */
186 	{ "",		0, 0 },			/* boundary */
187 	{ "--1", -1, EINVAL },
188 	{ "++42", -1, EINVAL },
189 	{ "SCALE_OVERFLOW", 0, ERANGE },
190 	{ "SCALE_UNDERFLOW", 0, ERANGE },
191 	{ "LLONG_MAX_K", (LLONG_MAX / 1024) * 1024, 0 },
192 	{ "LLONG_MIN_K", (LLONG_MIN / 1024) * 1024, 0 },
193 	{ "LLONG_MAX", LLONG_MAX, 0 },	/* upper limit */
194 
195 	/*
196 	 * Lower limit is a bit special: because scan_scaled accumulates into a
197 	 * signed long long it can only handle up to the negative value of
198 	 * LLONG_MAX not LLONG_MIN.
199 	 */
200 	{ "NEGATIVE_LLONG_MAX", LLONG_MAX*-1, 0 },	/* lower limit */
201 	{ "LLONG_MIN", 0, ERANGE },	/* can't handle */
202 #if LLONG_MAX == 0x7fffffffffffffffLL
203 	{ "-9223372036854775807", -9223372036854775807, 0 },
204 	{ "9223372036854775807", 9223372036854775807, 0 },
205 	{ "9223372036854775808", 0, ERANGE },
206 	{ "9223372036854775809", 0, ERANGE },
207 #endif
208 #if LLONG_MIN == (-0x7fffffffffffffffLL-1)
209 	{ "-9223372036854775808", 0, ERANGE },
210 	{ "-9223372036854775809", 0, ERANGE },
211 	{ "-9223372036854775810", 0, ERANGE },
212 #endif
213 };
214 #	define SDATA_LENGTH (sizeof sdata/sizeof *sdata)
215 
216 static void
217 print_errno(int e)
218 {
219 	switch(e) {
220 		case EINVAL: printf("EINVAL"); break;
221 		case EDOM:   printf("EDOM"); break;
222 		case ERANGE: printf("ERANGE"); break;
223 		default: printf("errno %d", e);
224 	}
225 }
226 
227 /** Print one result */
228 static void
229 print(char *input, long long result, int ret, int e)
230 {
231 	printf("\"%40s\" --> %lld (%d)", input, result, ret);
232 	if (ret == -1) {
233 		printf(" -- ");
234 		print_errno(e);
235 	}
236 	printf("\n");
237 }
238 
239 static int
240 scan_test(void)
241 {
242 	unsigned int i, errs = 0, e;
243 	int ret;
244 	long long result;
245 	char buf[1024], *input;
246 
247 	for (i = 0; i < SDATA_LENGTH; i++) {
248 		result = IMPROBABLE;
249 
250 		input = sdata[i].input;
251 		/* some magic values for architecture dependent limits */
252 		if (strcmp(input, "LLONG_MAX") == 0) {
253 			snprintf(buf, sizeof buf," %lld", LLONG_MAX);
254 			input = buf;
255 		} else if (strcmp(input, "LLONG_MIN") == 0) {
256 			snprintf(buf, sizeof buf," %lld", LLONG_MIN);
257 			input = buf;
258 		} else if (strcmp(input, "LLONG_MAX_K") == 0) {
259 			snprintf(buf, sizeof buf," %lldK", LLONG_MAX/1024);
260 			input = buf;
261 		} else if (strcmp(input, "LLONG_MIN_K") == 0) {
262 			snprintf(buf, sizeof buf," %lldK", LLONG_MIN/1024);
263 			input = buf;
264 		} else if (strcmp(input, "SCALE_OVERFLOW") == 0) {
265 			snprintf(buf, sizeof buf," %lldK", (LLONG_MAX/1024)+1);
266 			input = buf;
267 		} else if (strcmp(input, "SCALE_UNDERFLOW") == 0) {
268 			snprintf(buf, sizeof buf," %lldK", (LLONG_MIN/1024)-1);
269 			input = buf;
270 		} else if (strcmp(input, "NEGATIVE_LLONG_MAX") == 0) {
271 			snprintf(buf, sizeof buf," %lld", LLONG_MAX*-1);
272 			input = buf;
273 		}
274 		if (verbose && input != sdata[i].input)
275 			printf("expand '%s' -> '%s'\n", sdata[i].input,
276 			    input);
277 
278 		/* printf("Calling scan_scaled(%s, ...)\n", sdata[i].input); */
279 		errno = 0;
280 		ret = scan_scaled(input, &result);
281 		e = errno;	/* protect across printfs &c. */
282 		if (verbose)
283 			print(input, result, ret, e);
284 		if (ret == -1)
285 			errs += assert_int(i, 1, ret, sdata[i].err == 0 ? 0 : -1);
286 		if (sdata[i].err)
287 			errs += assert_errno(i, 2, sdata[i].err, e);
288 		else
289 			errs += assert_llong(i, 3, sdata[i].result, result);
290 	}
291 	return errs;
292 }
293 
294 /************** common testing stuff *******************/
295 
296 static int
297 assert_int(int testnum, int check, int expect, int result)
298 {
299 	if (expect == result)
300 		return 0;
301 	printf("** FAILURE: test %d check %d, expect %d, result %d **\n",
302 		testnum, check, expect, result);
303 	return 1;
304 }
305 
306 static int
307 assert_errno(int testnum, int check, int expect, int result)
308 {
309 	if (expect == result)
310 		return 0;
311 	printf("** FAILURE: test %d check %d, expect ",
312 		testnum, check);
313 	print_errno(expect);
314 	printf(", got ");
315 	print_errno(result);
316 	printf(" **\n");
317 	return 1;
318 }
319 
320 static int
321 assert_llong(int testnum, int check, long long expect, long long result)
322 {
323 	if (expect == result)
324 		return 0;
325 	printf("** FAILURE: test %d check %d, expect %lld, result %lld **\n",
326 		testnum, check, expect, result);
327 	return 1;
328 }
329 
330 static int
331 assert_str(int testnum, int check, char * expect, char * result)
332 {
333 	if (strcmp(expect, result) == 0)
334 		return 0;
335 	printf("** FAILURE: test %d check %d, expect %s, result %s **\n",
336 		testnum, check, expect, result);
337 	return 1;
338 }
339