logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

t_strtodur.c (1883B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _POSIX_C_SOURCE 200809L
  5. #include "../lib/strtodur.h"
  6. #include <assert.h>
  7. #include <stdio.h> // printf
  8. const char *argv0 = "t_strtodur";
  9. int counter = 0;
  10. int err = 0;
  11. static void
  12. t_strtodur(char *str, time_t ex_sec, long ex_nsec)
  13. {
  14. int id = ++counter;
  15. struct timespec dur = {.tv_sec = 0, .tv_nsec = 0};
  16. int ret = strtodur(str, &dur);
  17. if(dur.tv_sec == ex_sec && dur.tv_nsec == ex_nsec && ret == 0)
  18. {
  19. printf("ok %d - strtodur(\"%s\", _) -> {.tv_sec = %ld, .tv_nsec = %ld}\n",
  20. id,
  21. str,
  22. dur.tv_sec,
  23. dur.tv_nsec);
  24. return;
  25. }
  26. err = 1;
  27. printf("not ok %d - strtodur(\"%s\", _) -> {.tv_sec = %ld, .tv_nsec = %ld}\n",
  28. id,
  29. str,
  30. dur.tv_sec,
  31. dur.tv_nsec);
  32. if(dur.tv_sec != ex_sec || dur.tv_nsec != ex_nsec)
  33. printf("# Expected: {.tv_sec = %ld, .tv_nsec = %ld}\n", ex_sec, ex_nsec);
  34. if(ret != 0) printf("# Exit status: %d\n", ret);
  35. }
  36. int
  37. main(void)
  38. {
  39. int plan = 16;
  40. printf("1..%d\n", plan);
  41. // TODO: Capture errors, say with open_memstream(3)
  42. t_strtodur(NULL, 0, 0);
  43. t_strtodur((char *)"", 0, 0);
  44. t_strtodur((char *)",", 0, 0);
  45. t_strtodur((char *)"1", 1, 0);
  46. t_strtodur((char *)"1.", 1, 0);
  47. t_strtodur((char *)"1,", 1, 0);
  48. t_strtodur((char *)".1", 0, 1000000000 * 0.1);
  49. t_strtodur((char *)"0.1", 0, 1000000000 * 0.1);
  50. t_strtodur((char *)"1s", 1, 0);
  51. t_strtodur((char *)"1m", 60, 0);
  52. t_strtodur((char *)"1h", 60 * 60, 0);
  53. t_strtodur((char *)"1d", 60 * 60 * 24, 0);
  54. t_strtodur((char *)"1.5s", 1, 1000000000 * 0.5);
  55. t_strtodur((char *)"1.5m", 1.5 * 60, 0);
  56. t_strtodur((char *)"1.5h", 1.5 * 60 * 60, 0);
  57. t_strtodur((char *)"1.5d", 1.5 * 60 * 60 * 24, 0);
  58. assert(counter == plan);
  59. return err;
  60. }