1 /* $OpenBSD: fmt_test.c,v 1.9 2009/06/20 14:23:38 ian Exp $ */
4 * Combined tests for fmt_scaled and scan_scaled.
5 * Ian Darwin, January 2001. Public domain.
11 #include <sys/types.h>
16 static int fmt_test(void);
17 static int scan_test(void);
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_quad_t(int testnum, int checknum, quad_t expect, quad_t result);
23 static int assert_str(int testnum, int checknum, char * expect, char * result);
25 extern char *__progname;
26 static int verbose = 0;
28 __dead static void usage(int stat)
30 fprintf(stderr, "usage: %s [-v]\n", __progname);
35 main(int argc, char **argv)
41 while ((ch = getopt(argc, argv, "hv")) != -1) {
57 printf("Starting fmt_test\n");
60 printf("Starting scan_test\n");
63 printf("*** %d errors in libutil/fmt_scaled tests ***\n", i);
66 printf("Tests done; no unexpected errors\n");
71 /************** tests for fmt_scaled *******************/
73 static struct { /* the test cases */
86 { -1023, "-1023B", 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 */
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}
118 # define DDATA_LENGTH (sizeof ddata/sizeof *ddata)
123 unsigned int i, e, errs = 0;
125 char buf[FMT_SCALED_STRSIZE];
127 for (i = 0; i < DDATA_LENGTH; i++) {
128 strlcpy(buf, "UNSET", FMT_SCALED_STRSIZE);
129 ret = fmt_scaled(ddata[i].input, buf);
132 printf("%lld --> %s (%d)", ddata[i].input, buf, ret);
138 errs += assert_int(i, 1, ret, ddata[i].err == 0 ? 0 : -1);
140 errs += assert_errno(i, 2, ddata[i].err, errno);
142 errs += assert_str(i, 3, ddata[i].expect, buf);
148 /************** tests for scan_scaled *******************/
151 #define IMPROBABLE (-42)
155 struct { /* the test cases */
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 */
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, ERANGE }, /* invalid multiplier (XXX ERANGE??) */
183 { "1ab", 0, ERANGE }, /* ditto */
184 { "5.0e3", 0, EINVAL }, /* digits after */
185 { "5.0E3", 0, EINVAL }, /* ditto */
186 { "1..0", 0, EINVAL }, /* bad format */
187 { "", 0, 0 }, /* boundary */
188 { "--1", -1, EINVAL },
189 { "++42", -1, EINVAL },
190 /* { "9223372036854775808", -9223372036854775808LL, 0 }, */ /* XXX */
192 # define SDATA_LENGTH (sizeof sdata/sizeof *sdata)
198 case EINVAL: printf("EINVAL"); break;
199 case EDOM: printf("EDOM"); break;
200 case ERANGE: printf("ERANGE"); break;
201 default: printf("errno %d", errno);
205 /** Print one result */
207 print(char *input, quad_t result, int ret)
210 printf("\"%10s\" --> %lld (%d)", input, result, ret);
221 unsigned int i, errs = 0, e;
225 for (i = 0; i < SDATA_LENGTH; i++) {
227 /* printf("Calling scan_scaled(%s, ...)\n", sdata[i].input); */
228 ret = scan_scaled(sdata[i].input, &result);
229 e = errno; /* protect across printfs &c. */
231 print(sdata[i].input, result, ret);
234 errs += assert_int(i, 1, ret, sdata[i].err == 0 ? 0 : -1);
237 errs += assert_errno(i, 2, sdata[i].err, errno);
239 errs += assert_quad_t(i, 3, sdata[i].result, result);
244 /************** common testing stuff *******************/
247 assert_int(int testnum, int check, int expect, int result)
249 if (expect == result)
251 printf("** FAILURE: test %d check %d, expect %d, result %d **\n",
252 testnum, check, expect, result);
257 assert_errno(int testnum, int check, int expect, int result)
259 if (expect == result)
261 printf("** FAILURE: test %d check %d, expect ",
271 assert_quad_t(int testnum, int check, quad_t expect, quad_t result)
273 if (expect == result)
275 printf("** FAILURE: test %d check %d, expect %lld, result %lld **\n",
276 testnum, check, expect, result);
281 assert_str(int testnum, int check, char * expect, char * result)
283 if (strcmp(expect, result) == 0)
285 printf("** FAILURE: test %d check %d, expect %s, result %s **\n",
286 testnum, check, expect, result);