logo

utils-std

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

strtodur.c (1505B)


  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. int counter = 0;
  9. int err = 0;
  10. static void
  11. t_strtodur(char *str, time_t ex_sec, long ex_nsec)
  12. {
  13. int id = ++counter;
  14. struct timespec dur = {.tv_sec = 0, .tv_nsec = 0};
  15. int ret = strtodur(str, &dur);
  16. if(dur.tv_sec == ex_sec && dur.tv_nsec == ex_nsec && ret == 0)
  17. {
  18. printf("ok %d - strtodur(\"%s\", _) -> {.tv_sec = %ld, .tv_nsec = %ld}\n",
  19. id,
  20. str,
  21. dur.tv_sec,
  22. dur.tv_nsec);
  23. return;
  24. }
  25. err = 1;
  26. printf("not ok %d - strtodur(\"%s\", _) -> {.tv_sec = %ld, .tv_nsec = %ld}\n",
  27. id,
  28. str,
  29. dur.tv_sec,
  30. dur.tv_nsec);
  31. if(dur.tv_sec != ex_sec || dur.tv_nsec != ex_nsec)
  32. printf("# Expected: {.tv_sec = %ld, .tv_nsec = %ld}\n", ex_sec, ex_nsec);
  33. if(ret != 0) printf("# Exit status: %d\n", ret);
  34. }
  35. int
  36. main(void)
  37. {
  38. int plan = 8;
  39. printf("1..%d\n", plan);
  40. // TODO: Capture errors, say with open_memstream(3)
  41. t_strtodur(NULL, 0, 0);
  42. t_strtodur((char *)"", 0, 0);
  43. t_strtodur((char *)",", 0, 0);
  44. t_strtodur((char *)"1", 1, 0);
  45. t_strtodur((char *)"1.", 1, 0);
  46. t_strtodur((char *)"1,", 1, 0);
  47. t_strtodur((char *)".1", 0, 1000000000 * 0.1);
  48. t_strtodur((char *)"0.1", 0, 1000000000 * 0.1);
  49. assert(counter == plan);
  50. return err;
  51. }