logo

cmd-timer

run command at a specific interval git clone https://anongit.hacktivis.me/git/cmd-timer.git

test_strtodur.c (1088B)


  1. // SPDX-FileCopyrightText: 2025 Haelwenn (lanodan) Monnier <contact+cmd-timer@hacktivis.me>
  2. // SPDX-License-Identifier: MIT
  3. #define _POSIX_C_SOURCE 200809L
  4. #include "timer.h"
  5. #include <assert.h>
  6. #include <stdio.h>
  7. int counter = 0;
  8. int err = 0;
  9. static void
  10. t(char *str, unsigned long exp)
  11. {
  12. int id = ++counter;
  13. unsigned long res = strtodur(str);
  14. if(res == exp)
  15. {
  16. printf("ok %d - %s -> %ld\n", id, str, res);
  17. return;
  18. }
  19. err = 1;
  20. printf("not ok %d - %s -> %ld\n", id, str, res);
  21. printf("# Expected: %ld\n", exp);
  22. }
  23. int
  24. main()
  25. {
  26. int plan = 13;
  27. printf("1..%d\n", plan);
  28. #define T_MIN 60
  29. #define T_HOUR 60 * T_MIN
  30. #define T_DAY 24 * T_HOUR
  31. t(NULL, 0);
  32. t("", 0);
  33. t("5", 5);
  34. t("5s", 5);
  35. t("5m", 5 * T_MIN);
  36. t("5m5s", (5 * T_MIN) + 5);
  37. t("5h", 5 * T_HOUR);
  38. t("5h5m", (5 * T_HOUR) + (5 * T_MIN));
  39. t("5h5m5s", (5 * T_HOUR) + (5 * T_MIN) + 5);
  40. t("5d", 5 * T_DAY);
  41. t("5d5h", (5 * T_DAY) + (5 * T_HOUR));
  42. t("5d5h5m", (5 * T_DAY) + (5 * T_HOUR) + (5 * T_MIN));
  43. t("5d5h5m5s", (5 * T_DAY) + (5 * T_HOUR) + (5 * T_MIN) + 5);
  44. assert(counter == plan);
  45. return err;
  46. }