unit_number__scnprintf.c 741 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <inttypes.h>
  3. #include <linux/compiler.h>
  4. #include <linux/types.h>
  5. #include "tests.h"
  6. #include "units.h"
  7. #include "debug.h"
  8. int test__unit_number__scnprint(struct test *t __maybe_unused, int subtest __maybe_unused)
  9. {
  10. struct {
  11. u64 n;
  12. const char *str;
  13. } test[] = {
  14. { 1, "1B" },
  15. { 10*1024, "10K" },
  16. { 20*1024*1024, "20M" },
  17. { 30*1024*1024*1024ULL, "30G" },
  18. { 0, "0B" },
  19. { 0, NULL },
  20. };
  21. unsigned i = 0;
  22. while (test[i].str) {
  23. char buf[100];
  24. unit_number__scnprintf(buf, sizeof(buf), test[i].n);
  25. pr_debug("n %" PRIu64 ", str '%s', buf '%s'\n",
  26. test[i].n, test[i].str, buf);
  27. if (strcmp(test[i].str, buf))
  28. return TEST_FAIL;
  29. i++;
  30. }
  31. return TEST_OK;
  32. }