xref: /openbsd-src/regress/lib/libutil/fmt_scaled/fmt_test.c (revision 4c8432e8c87d8aa6ecaa7c8f757cf50b817eaa92)
1 /* $OpenBSD: fmt_test.c,v 1.11 2016/08/14 23:08:09 guenther 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 
14 #include <util.h>
15 
16 static int fmt_test(void);
17 static int scan_test(void);
18 
19 static void print_errno(int e);
20 static int assert_int(int testnum, int checknum, int expect, int result);
21 static int assert_errno(int testnum, int checknum, int expect, int result);
22 static int assert_llong(int testnum, int checknum, long long expect, long long result);
23 static int assert_str(int testnum, int checknum, char * expect, char * result);
24 
25 extern char *__progname;
26 static int verbose = 0;
27 
28 __dead static void usage(int stat)
29 {
30 	fprintf(stderr, "usage: %s [-v]\n", __progname);
31 	exit(stat);
32 }
33 
34 int
35 main(int argc, char **argv)
36 {
37 	extern char *optarg;
38 	extern int optind;
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 		ret = fmt_scaled(ddata[i].input, buf);
130 		e = errno;
131 		if (verbose) {
132 			printf("%lld --> %s (%d)", ddata[i].input, buf, ret);
133 			if (ret == -1)
134 				print_errno(e);
135 			printf("\n");
136 		}
137 		if (ret == -1)
138 			errs += assert_int(i, 1, ret, ddata[i].err == 0 ? 0 : -1);
139 		if (ddata[i].err)
140 			errs += assert_errno(i, 2, ddata[i].err, errno);
141 		else
142 			errs += assert_str(i, 3, ddata[i].expect, buf);
143 	}
144 
145 	return errs;
146 }
147 
148 /************** tests for scan_scaled *******************/
149 
150 
151 #define	IMPROBABLE	(-42)
152 
153 extern int errno;
154 
155 struct {					/* the test cases */
156 	char *input;
157 	long long result;
158 	int err;
159 } sdata[] = {
160 	{ "0",		0, 0 },
161 	{ "123",	123, 0 },
162 	{ "1k",		1024, 0 },		/* lower case */
163 	{ "100.944", 100, 0 },	/* should --> 100 (truncates fraction) */
164 	{ "10099",	10099LL, 0 },
165 	{ "1M",		1048576LL, 0 },
166 	{ "1.1M",	1153433LL, 0 },		/* fractions */
167 	{ "1.111111111111111111M",	1165084LL, 0 },		/* fractions */
168 	{ "1.55M",	1625292LL, 0 },	/* fractions */
169 	{ "1.9M",	1992294LL, 0 },		/* fractions */
170 	{ "-2K",	-2048LL, 0 },		/* negatives */
171 	{ "-2.2K",	-2252LL, 0 },	/* neg with fract */
172 	{ "4.5k", 4608, 0 },
173 	{ "4.5555555555555555K", 4664, 0 },
174 	{ "4.5555555555555555555K", 4664, 0 },	/* handle enough digits? */
175 	{ "4.555555555555555555555555555555K", 4664, 0 }, /* ignores extra digits? */
176 	{ "1G",		1073741824LL, 0 },
177 	{ "G", 		0, 0 },			/* should == 0G? */
178 	{ "1234567890", 1234567890LL, 0 },	/* should work */
179 	{ "1.5E",	1729382256910270464LL, 0 },		/* big */
180 	{ "32948093840918378473209480483092", 0, ERANGE },  /* too big */
181 	{ "329480938409.8378473209480483092", 0, ERANGE },  /* fraction too big */
182 	{ "1.5Q",	0, EINVAL },		/* invalid multiplier */
183 	{ "1ab",	0, EINVAL },		/* ditto */
184 	{ "3&",		0, EINVAL },		/* ditto */
185 	{ "5.0e3",	0, EINVAL },	/* digits after */
186 	{ "5.0E3",	0, EINVAL },	/* ditto */
187 	{ "1..0",	0, EINVAL },		/* bad format */
188 	{ "",		0, 0 },			/* boundary */
189 	{ "--1", -1, EINVAL },
190 	{ "++42", -1, EINVAL },
191 	/* { "9223372036854775808", -9223372036854775808LL, 0 }, */	/* XXX  */
192 };
193 #	define SDATA_LENGTH (sizeof sdata/sizeof *sdata)
194 
195 static void
196 print_errno(int e)
197 {
198 	switch(e) {
199 		case EINVAL: printf("EINVAL"); break;
200 		case EDOM:   printf("EDOM"); break;
201 		case ERANGE: printf("ERANGE"); break;
202 		default: printf("errno %d", errno);
203 	}
204 }
205 
206 /** Print one result */
207 static void
208 print(char *input, long long result, int ret)
209 {
210 	int e = errno;
211 	printf("\"%10s\" --> %lld (%d)", input, result, ret);
212 	if (ret == -1) {
213 		printf(" -- ");
214 		print_errno(e);
215 	}
216 	printf("\n");
217 }
218 
219 static int
220 scan_test(void)
221 {
222 	unsigned int i, errs = 0, e;
223 	int ret;
224 	long long result;
225 
226 	for (i = 0; i < SDATA_LENGTH; i++) {
227 		result = IMPROBABLE;
228 		/* printf("Calling scan_scaled(%s, ...)\n", sdata[i].input); */
229 		ret = scan_scaled(sdata[i].input, &result);
230 		e = errno;	/* protect across printfs &c. */
231 		if (verbose)
232 			print(sdata[i].input, result, ret);
233 		errno = e;
234 		if (ret == -1)
235 			errs += assert_int(i, 1, ret, sdata[i].err == 0 ? 0 : -1);
236 		errno = e;
237 		if (sdata[i].err)
238 			errs += assert_errno(i, 2, sdata[i].err, errno);
239 		else
240 			errs += assert_llong(i, 3, sdata[i].result, result);
241 	}
242 	return errs;
243 }
244 
245 /************** common testing stuff *******************/
246 
247 static int
248 assert_int(int testnum, int check, int expect, int result)
249 {
250 	if (expect == result)
251 		return 0;
252 	printf("** FAILURE: test %d check %d, expect %d, result %d **\n",
253 		testnum, check, expect, result);
254 	return 1;
255 }
256 
257 static int
258 assert_errno(int testnum, int check, int expect, int result)
259 {
260 	if (expect == result)
261 		return 0;
262 	printf("** FAILURE: test %d check %d, expect ",
263 		testnum, check);
264 	print_errno(expect);
265 	printf(", got ");
266 	print_errno(result);
267 	printf(" **\n");
268 	return 1;
269 }
270 
271 static int
272 assert_llong(int testnum, int check, long long expect, long long result)
273 {
274 	if (expect == result)
275 		return 0;
276 	printf("** FAILURE: test %d check %d, expect %lld, result %lld **\n",
277 		testnum, check, expect, result);
278 	return 1;
279 }
280 
281 static int
282 assert_str(int testnum, int check, char * expect, char * result)
283 {
284 	if (strcmp(expect, result) == 0)
285 		return 0;
286 	printf("** FAILURE: test %d check %d, expect %s, result %s **\n",
287 		testnum, check, expect, result);
288 	return 1;
289 }
290