test_private_parts.c 1021 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <zf_log.c>
  2. #include <zf_test.h>
  3. static char *strcopy_r(const char *s, char *e)
  4. {
  5. e -= strlen(s) + 1;
  6. for (char *p = e; 0 != (*p++ = *s++);) {}
  7. return e;
  8. }
  9. typedef struct put_padding_r_testcase
  10. {
  11. const char *const s;
  12. const unsigned w;
  13. const char *const p;
  14. }
  15. put_padding_r_testcase;
  16. static const put_padding_r_testcase g_put_padding_r_testcases[] =
  17. {
  18. {"", 0, ""},
  19. {"1", 0, "1"},
  20. {"123", 0, "123"},
  21. {"", 3, "---"},
  22. {"1", 3, "--1"},
  23. {"123", 3, "123"},
  24. {"1234", 3, "1234"},
  25. };
  26. static void test_put_padding_r()
  27. {
  28. char buf[16];
  29. char *const e = buf + _countof(buf) - 1;
  30. char *p;
  31. for (unsigned i = 0; _countof(g_put_padding_r_testcases) > i; ++i)
  32. {
  33. const put_padding_r_testcase *const tc = g_put_padding_r_testcases + i;
  34. p = strcopy_r(tc->s, e + 1);
  35. p = put_padding_r(tc->w, '-', p, e);
  36. TEST_VERIFY_TRUE_MSG(0 == strcmp(p, tc->p), "i=%u", i);
  37. }
  38. }
  39. int main(int argc, char *argv[])
  40. {
  41. TEST_RUNNER_CREATE(argc, argv);
  42. TEST_EXECUTE(test_put_padding_r());
  43. return TEST_RUNNER_EXIT_CODE();
  44. }