unit_number__scnprintf.c 674 B

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