1 /* $OpenBSD: fmt_test.c,v 1.12 2017/02/25 07:28:32 jsg 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 <unistd.h> 14 15 #include <util.h> 16 17 static int fmt_test(void); 18 static int scan_test(void); 19 20 static void print_errno(int e); 21 static int assert_int(int testnum, int checknum, int expect, int result); 22 static int assert_errno(int testnum, int checknum, int expect, int result); 23 static int assert_llong(int testnum, int checknum, long long expect, long long result); 24 static int assert_str(int testnum, int checknum, char * expect, char * result); 25 26 extern char *__progname; 27 static int verbose = 0; 28 29 __dead static void usage(int stat) 30 { 31 fprintf(stderr, "usage: %s [-v]\n", __progname); 32 exit(stat); 33 } 34 35 int 36 main(int argc, char **argv) 37 { 38 extern char *optarg; 39 extern int optind; 40 int i, ch; 41 42 while ((ch = getopt(argc, argv, "hv")) != -1) { 43 switch (ch) { 44 case 'v': 45 verbose = 1; 46 break; 47 case 'h': 48 usage(0); 49 case '?': 50 default: 51 usage(1); 52 } 53 } 54 argc -= optind; 55 argv += optind; 56 57 if (verbose) 58 printf("Starting fmt_test\n"); 59 i = fmt_test(); 60 if (verbose) 61 printf("Starting scan_test\n"); 62 i += scan_test(); 63 if (i) { 64 printf("*** %d errors in libutil/fmt_scaled tests ***\n", i); 65 } else { 66 if (verbose) 67 printf("Tests done; no unexpected errors\n"); 68 } 69 return i; 70 } 71 72 /************** tests for fmt_scaled *******************/ 73 74 static struct { /* the test cases */ 75 long long input; 76 char *expect; 77 int err; 78 } ddata[] = { 79 { 0, "0B", 0 }, 80 { 1, "1B", 0 }, 81 { -1, "-1B", 0 }, 82 { 100, "100B", 0}, 83 { -100, "-100B", 0}, 84 { 999, "999B", 0 }, 85 { 1000, "1000B", 0 }, 86 { 1023, "1023B", 0 }, 87 { -1023, "-1023B", 0 }, 88 { 1024, "1.0K", 0 }, 89 { 1025, "1.0K", 0 }, 90 { 1234, "1.2K", 0 }, 91 { -1234, "-1.2K", 0 }, 92 { 1484, "1.4K", 0 }, /* rounding boundary, down */ 93 { 1485, "1.5K", 0 }, /* rounding boundary, up */ 94 { -1484, "-1.4K", 0 }, /* rounding boundary, down */ 95 { -1485, "-1.5K", 0 }, /* rounding boundary, up */ 96 { 1536, "1.5K", 0 }, 97 { 1786, "1.7K", 0 }, 98 { 1800, "1.8K", 0 }, 99 { 2000, "2.0K", 0 }, 100 { 123456, "121K", 0 }, 101 { 578318, "565K", 0 }, 102 { 902948, "882K", 0 }, 103 { 1048576, "1.0M", 0}, 104 { 1048628, "1.0M", 0}, 105 { 1049447, "1.0M", 0}, 106 { -102400, "-100K", 0}, 107 { -103423, "-101K", 0 }, 108 { 7299072, "7.0M", 0 }, 109 { 409478144L, "391M", 0 }, 110 { -409478144L, "-391M", 0 }, 111 { 999999999L, "954M", 0 }, 112 { 1499999999L, "1.4G", 0 }, 113 { 12475423744LL, "11.6G", 0}, 114 { 1LL<<61, "2.0E", 0 }, 115 { 1LL<<62, "4.0E", 0 }, 116 { 1LL<<63, "", ERANGE }, 117 { 1099512676352LL, "1.0T", 0} 118 }; 119 # define DDATA_LENGTH (sizeof ddata/sizeof *ddata) 120 121 static int 122 fmt_test(void) 123 { 124 unsigned int i, e, errs = 0; 125 int ret; 126 char buf[FMT_SCALED_STRSIZE]; 127 128 for (i = 0; i < DDATA_LENGTH; i++) { 129 strlcpy(buf, "UNSET", FMT_SCALED_STRSIZE); 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, errno); 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 extern int errno; 155 156 struct { /* the test cases */ 157 char *input; 158 long long result; 159 int err; 160 } sdata[] = { 161 { "0", 0, 0 }, 162 { "123", 123, 0 }, 163 { "1k", 1024, 0 }, /* lower case */ 164 { "100.944", 100, 0 }, /* should --> 100 (truncates fraction) */ 165 { "10099", 10099LL, 0 }, 166 { "1M", 1048576LL, 0 }, 167 { "1.1M", 1153433LL, 0 }, /* fractions */ 168 { "1.111111111111111111M", 1165084LL, 0 }, /* fractions */ 169 { "1.55M", 1625292LL, 0 }, /* fractions */ 170 { "1.9M", 1992294LL, 0 }, /* fractions */ 171 { "-2K", -2048LL, 0 }, /* negatives */ 172 { "-2.2K", -2252LL, 0 }, /* neg with fract */ 173 { "4.5k", 4608, 0 }, 174 { "4.5555555555555555K", 4664, 0 }, 175 { "4.5555555555555555555K", 4664, 0 }, /* handle enough digits? */ 176 { "4.555555555555555555555555555555K", 4664, 0 }, /* ignores extra digits? */ 177 { "1G", 1073741824LL, 0 }, 178 { "G", 0, 0 }, /* should == 0G? */ 179 { "1234567890", 1234567890LL, 0 }, /* should work */ 180 { "1.5E", 1729382256910270464LL, 0 }, /* big */ 181 { "32948093840918378473209480483092", 0, ERANGE }, /* too big */ 182 { "329480938409.8378473209480483092", 0, ERANGE }, /* fraction too big */ 183 { "1.5Q", 0, EINVAL }, /* invalid multiplier */ 184 { "1ab", 0, EINVAL }, /* ditto */ 185 { "3&", 0, EINVAL }, /* ditto */ 186 { "5.0e3", 0, EINVAL }, /* digits after */ 187 { "5.0E3", 0, EINVAL }, /* ditto */ 188 { "1..0", 0, EINVAL }, /* bad format */ 189 { "", 0, 0 }, /* boundary */ 190 { "--1", -1, EINVAL }, 191 { "++42", -1, EINVAL }, 192 /* { "9223372036854775808", -9223372036854775808LL, 0 }, */ /* XXX */ 193 }; 194 # define SDATA_LENGTH (sizeof sdata/sizeof *sdata) 195 196 static void 197 print_errno(int e) 198 { 199 switch(e) { 200 case EINVAL: printf("EINVAL"); break; 201 case EDOM: printf("EDOM"); break; 202 case ERANGE: printf("ERANGE"); break; 203 default: printf("errno %d", errno); 204 } 205 } 206 207 /** Print one result */ 208 static void 209 print(char *input, long long result, int ret) 210 { 211 int e = errno; 212 printf("\"%10s\" --> %lld (%d)", input, result, ret); 213 if (ret == -1) { 214 printf(" -- "); 215 print_errno(e); 216 } 217 printf("\n"); 218 } 219 220 static int 221 scan_test(void) 222 { 223 unsigned int i, errs = 0, e; 224 int ret; 225 long long result; 226 227 for (i = 0; i < SDATA_LENGTH; i++) { 228 result = IMPROBABLE; 229 /* printf("Calling scan_scaled(%s, ...)\n", sdata[i].input); */ 230 ret = scan_scaled(sdata[i].input, &result); 231 e = errno; /* protect across printfs &c. */ 232 if (verbose) 233 print(sdata[i].input, result, ret); 234 errno = e; 235 if (ret == -1) 236 errs += assert_int(i, 1, ret, sdata[i].err == 0 ? 0 : -1); 237 errno = e; 238 if (sdata[i].err) 239 errs += assert_errno(i, 2, sdata[i].err, errno); 240 else 241 errs += assert_llong(i, 3, sdata[i].result, result); 242 } 243 return errs; 244 } 245 246 /************** common testing stuff *******************/ 247 248 static int 249 assert_int(int testnum, int check, int expect, int result) 250 { 251 if (expect == result) 252 return 0; 253 printf("** FAILURE: test %d check %d, expect %d, result %d **\n", 254 testnum, check, expect, result); 255 return 1; 256 } 257 258 static int 259 assert_errno(int testnum, int check, int expect, int result) 260 { 261 if (expect == result) 262 return 0; 263 printf("** FAILURE: test %d check %d, expect ", 264 testnum, check); 265 print_errno(expect); 266 printf(", got "); 267 print_errno(result); 268 printf(" **\n"); 269 return 1; 270 } 271 272 static int 273 assert_llong(int testnum, int check, long long expect, long long result) 274 { 275 if (expect == result) 276 return 0; 277 printf("** FAILURE: test %d check %d, expect %lld, result %lld **\n", 278 testnum, check, expect, result); 279 return 1; 280 } 281 282 static int 283 assert_str(int testnum, int check, char * expect, char * result) 284 { 285 if (strcmp(expect, result) == 0) 286 return 0; 287 printf("** FAILURE: test %d check %d, expect %s, result %s **\n", 288 testnum, check, expect, result); 289 return 1; 290 } 291