unit_number__scnprintf.c 761 B

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