#include #include #include #include struct { const char *res; int64_t num; int scale; int flags; } test_args[] = { /* test 1000 suffixes */ { "0 ", 0, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, { "1 K", 500, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, { "1 M", 500*1000, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, { "1 G", 500*1000*1000, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, { "1 T", 500*1000*1000*1000, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, { "1 E", 500*1000*1000*1000*1000, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, { "1 ", 1, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, { "2 K", 1500, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, { "2 M", 1500*1000, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, { "2 G", 1500*1000*1000, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, { "2 T", 1500*1000*1000*1000, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, { "2 E", 1500*1000*1000*1000*1000, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, /* test 1024 suffixes */ { "0 ", 0, HN_AUTOSCALE, 0 }, { "1 K", 512, HN_AUTOSCALE, 0 }, { "1 M", 512*1024, HN_AUTOSCALE, 0 }, { "1 G", 512*1024*1024, HN_AUTOSCALE, 0 }, { "1 T", 512*1024*1024*1024, HN_AUTOSCALE, 0 }, { "1 E", 512*1024*1024*1024*1024, HN_AUTOSCALE, 0 }, { "1 ", 1, HN_AUTOSCALE, 0 }, { "2 K", 1536, HN_AUTOSCALE, 0 }, { "2 M", 1536*1024, HN_AUTOSCALE, 0 }, { "2 G", 1536*1024*1024, HN_AUTOSCALE, 0 }, { "2 T", 1536*1024*1024*1024, HN_AUTOSCALE, 0 }, { "2 E", 1536*1024*1024*1024*1024, HN_AUTOSCALE, 0 }, /* test rounding */ { "0 M", 500*1000-1, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, { "1 M", 500*1000, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, { "1 M", 1000*1000 + 500*1000-1, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, { "2 M", 1000*1000 + 500*1000, HN_AUTOSCALE|HN_DIVISOR_1000, 0 }, { "0 K", 512-1, HN_AUTOSCALE, 0 }, { "0 M", 512*1024-1, HN_AUTOSCALE, 0 }, { "1 M", 512*1024, HN_AUTOSCALE, 0 }, { "1 M", 1024*1024 + 512*1024-1, HN_AUTOSCALE, 0 }, { "2 M", 1024*1024 + 512*1024, HN_AUTOSCALE, 0 }, }; int main(void) { char buf[4]; size_t i; size_t errcnt; int r; errcnt = 0; for (i = 0; i < sizeof test_args / sizeof *test_args; i++) { r = humanize_number(buf, sizeof buf, test_args[i].num, "", test_args[i].scale, test_args[i].flags); if (r == -1) { printf("error processing index %lu\n", i); errcnt++; } else if (strcmp(buf, test_args[i].res) != 0) { printf("mismatch on index %lu, got: \"%s\", expected \"%s\".\n", i, buf, test_args[i].res); errcnt++; } } if (errcnt) { printf("total errors: %lu\n", errcnt); return 1; } return 0; }