Deleted Added
full compact
humanize_number_test.c (256130) humanize_number_test.c (269904)
1/*-
2 * Copyright 2012 Clifton Royston
3 * Copyright 2013 John-Mark Gurney
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:

--- 10 unchanged lines hidden (view full) ---

19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
1/*-
2 * Copyright 2012 Clifton Royston
3 * Copyright 2013 John-Mark Gurney
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:

--- 10 unchanged lines hidden (view full) ---

19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/tools/regression/lib/libutil/test-humanize_number.c 256130 2013-10-07 22:22:57Z jmg $
27 * $FreeBSD: head/lib/libutil/tests/humanize_number_test.c 269904 2014-08-13 04:56:27Z ngie $
28 *
29 */
30
28 *
29 */
30
31#include <sys/types.h>
32#include <stdlib.h>
31#include <sys/param.h>
32#include <inttypes.h>
33#include <libutil.h>
33#include <libutil.h>
34#include <limits.h>
35#include <math.h>
34#include <stdio.h>
36#include <stdio.h>
37#include <stdlib.h>
35#include <string.h>
38#include <string.h>
36#include <inttypes.h>
37#include <math.h>
38#include <unistd.h>
39#include <unistd.h>
39#include <limits.h>
40
40
41extern char * optarg;
42
43#define MAX_STR_FLAGS_RESULT 80
44#define MAX_INT_STR_DIGITS 12
45
46static const int64_t halfExabyte = (int64_t)500*1000*1000*1000*1000*1000L;
47
48static struct {
49 int retval;
50 const char *res;

--- 434 unchanged lines hidden (view full) ---

485 snprintf(result, MAX_INT_STR_DIGITS, "%d", scale);
486 return result;
487}
488
489static void
490testskipped(size_t i)
491{
492
41#define MAX_STR_FLAGS_RESULT 80
42#define MAX_INT_STR_DIGITS 12
43
44static const int64_t halfExabyte = (int64_t)500*1000*1000*1000*1000*1000L;
45
46static struct {
47 int retval;
48 const char *res;

--- 434 unchanged lines hidden (view full) ---

483 snprintf(result, MAX_INT_STR_DIGITS, "%d", scale);
484 return result;
485}
486
487static void
488testskipped(size_t i)
489{
490
493 printf("ok %lu # skip - not turned on\n", i);
491 printf("ok %zu # skip - not turned on\n", i);
494}
495
496int
497main(int argc, char * const argv[])
498{
499 char *buf;
500 char *flag_str, *scale_str;
492}
493
494int
495main(int argc, char * const argv[])
496{
497 char *buf;
498 char *flag_str, *scale_str;
501 size_t i;
502 size_t errcnt, tested, skipped;
499 size_t buflen, errcnt, i, skipped, tested;
503 int r;
500 int r;
504 size_t buflen;
505 int includeNegScale;
506 int includeExabyteTests;
507 int verbose;
508
509 buflen = 4;
510 includeNegScale = 0;
511 includeExabyteTests = 0;
512 verbose = 0;

--- 4 unchanged lines hidden (view full) ---

517 buf = malloc(buflen);
518 errcnt = 0;
519 tested = 0;
520 skipped = 0;
521
522 if (buflen != 4)
523 printf("Warning: buffer size %zu != 4, expect some results to differ.\n", buflen);
524
501 int includeNegScale;
502 int includeExabyteTests;
503 int verbose;
504
505 buflen = 4;
506 includeNegScale = 0;
507 includeExabyteTests = 0;
508 verbose = 0;

--- 4 unchanged lines hidden (view full) ---

513 buf = malloc(buflen);
514 errcnt = 0;
515 tested = 0;
516 skipped = 0;
517
518 if (buflen != 4)
519 printf("Warning: buffer size %zu != 4, expect some results to differ.\n", buflen);
520
525 printf("1..%lu\n", sizeof test_args / sizeof *test_args);
526 for (i = 0; i < sizeof test_args / sizeof *test_args; i++) {
521 printf("1..%zu\n", nitems(test_args));
522 for (i = 0; i < nitems(test_args); i++) {
527 /* KLUDGE */
528 if (test_args[i].num == INT64_MAX && buflen == 4) {
529 /* Start final tests which require buffer of 6 */
530 free(buf);
531 buflen = 6;
532 buf = malloc(buflen);
533 if (verbose)
534 printf("Buffer length increased to %zu\n",
535 buflen);
536 }
537
538 if (test_args[i].scale < 0 && ! includeNegScale) {
539 skipped++;
523 /* KLUDGE */
524 if (test_args[i].num == INT64_MAX && buflen == 4) {
525 /* Start final tests which require buffer of 6 */
526 free(buf);
527 buflen = 6;
528 buf = malloc(buflen);
529 if (verbose)
530 printf("Buffer length increased to %zu\n",
531 buflen);
532 }
533
534 if (test_args[i].scale < 0 && ! includeNegScale) {
535 skipped++;
540 testskipped(i);
536 testskipped(i + 1);
541 continue;
542 }
543 if (test_args[i].num >= halfExabyte && ! includeExabyteTests) {
544 skipped++;
537 continue;
538 }
539 if (test_args[i].num >= halfExabyte && ! includeExabyteTests) {
540 skipped++;
545 testskipped(i);
541 testskipped(i + 1);
546 continue;
547 }
548
549 r = humanize_number(buf, buflen, test_args[i].num, "",
550 test_args[i].scale, test_args[i].flags);
551 flag_str = str_flags(test_args[i].flags, "[no flags]");
552 scale_str = str_scale(test_args[i].scale);
553
554 if (r != test_args[i].retval) {
555 if (verbose)
542 continue;
543 }
544
545 r = humanize_number(buf, buflen, test_args[i].num, "",
546 test_args[i].scale, test_args[i].flags);
547 flag_str = str_flags(test_args[i].flags, "[no flags]");
548 scale_str = str_scale(test_args[i].scale);
549
550 if (r != test_args[i].retval) {
551 if (verbose)
556 printf("wrong return value on index %lu, buflen: %zu, got: %d + \"%s\", expected %d + \"%s\"; num = %" PRId64 ", scale = %s, flags= %s.\n",
552 printf("wrong return value on index %zu, "
553 "buflen: %zu, got: %d + \"%s\", "
554 "expected %d + \"%s\"; num = %jd, "
555 "scale = %s, flags= %s.\n",
557 i, buflen, r, buf, test_args[i].retval,
556 i, buflen, r, buf, test_args[i].retval,
558 test_args[i].res, test_args[i].num,
557 test_args[i].res,
558 (intmax_t)test_args[i].num,
559 scale_str, flag_str);
560 else
559 scale_str, flag_str);
560 else
561 printf("not ok %lu # return %d != %d\n", i, r,
562 test_args[i].retval);
561 printf("not ok %zu # return %d != %d\n",
562 i + 1, r, test_args[i].retval);
563 errcnt++;
564 } else if (strcmp(buf, test_args[i].res) != 0) {
565 if (verbose)
563 errcnt++;
564 } else if (strcmp(buf, test_args[i].res) != 0) {
565 if (verbose)
566 printf("result mismatch on index %lu, got: \"%s\", expected \"%s\"; num = %" PRId64 ", scale = %s, flags= %s.\n",
567 i, buf, test_args[i].res, test_args[i].num,
566 printf("result mismatch on index %zu, got: "
567 "\"%s\", expected \"%s\"; num = %jd, "
568 "scale = %s, flags= %s.\n",
569 i, buf, test_args[i].res,
570 (intmax_t)test_args[i].num,
568 scale_str, flag_str);
569 else
571 scale_str, flag_str);
572 else
570 printf("not ok %lu # buf \"%s\" != \"%s\"\n", i,
571 buf, test_args[i].res);
573 printf("not ok %zu # buf \"%s\" != \"%s\"\n",
574 i + 1, buf, test_args[i].res);
572 errcnt++;
573 } else {
574 if (verbose)
575 errcnt++;
576 } else {
577 if (verbose)
575 printf("successful result on index %lu, returned %d, got: \"%s\"; num = %" PRId64 ", scale = %s, flags= %s.\n",
576 i, r, buf, test_args[i].num, scale_str,
577 flag_str);
578 printf("successful result on index %zu, "
579 "returned %d, got: \"%s\"; num = %jd, "
580 "scale = %s, flags= %s.\n",
581 i, r, buf,
582 (intmax_t)test_args[i].num,
583 scale_str, flag_str);
578 else
584 else
579 printf("ok %lu\n", i);
585 printf("ok %zu\n", i + 1);
580 }
581 tested++;
582 }
583
584 if (verbose)
586 }
587 tested++;
588 }
589
590 if (verbose)
585 printf("total errors: %lu/%lu tests, %lu skipped\n", errcnt,
591 printf("total errors: %zu/%zu tests, %zu skipped\n", errcnt,
586 tested, skipped);
587
588 if (errcnt)
589 return 1;
590
591 return 0;
592}
592 tested, skipped);
593
594 if (errcnt)
595 return 1;
596
597 return 0;
598}