2
0

macros.c 995 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <assert.h>
  2. #include <sys/time.h>
  3. #include "test_helpers.h"
  4. int main(void) {
  5. struct timeval x = { .tv_usec = 15 };
  6. struct timeval y = { 0 };
  7. struct timeval z = { 0 };
  8. struct timeval one_usec = { .tv_usec = 1 };
  9. struct timeval max_usec = { .tv_usec = 999999 };
  10. struct timeval one_sec = { .tv_sec = 1 };
  11. assert(!timerisset(&y));
  12. assert(timerisset(&x));
  13. timerclear(&x);
  14. assert(!timerisset(&x));
  15. assert(timercmp(&x, &y, ==));
  16. timeradd(&y, &one_usec, &z);
  17. assert(!timercmp(&x, &z, ==));
  18. assert(timercmp(&x, &z, <));
  19. timeradd(&z, &max_usec, &y);
  20. assert(y.tv_sec == 1);
  21. assert(y.tv_usec == 0);
  22. timersub(&y, &one_usec, &z);
  23. assert(z.tv_sec == 0);
  24. assert(z.tv_usec == 999999);
  25. timeradd(&z, &one_sec, &y);
  26. assert(y.tv_sec == 1);
  27. assert(y.tv_usec == 999999);
  28. for (int i = 0; i < 3; i += 1) {
  29. timersub(&y, &one_sec, &y);
  30. }
  31. assert(y.tv_sec == -2);
  32. assert(y.tv_usec == 999999);
  33. }