testc_sched.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <sys/time.h>
  2. #include <time.h>
  3. #include <errno.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include "faux/time.h"
  7. #include "faux/sched.h"
  8. int testc_faux_sched(void)
  9. {
  10. faux_sched_t *sched = NULL;
  11. long long int nsec = 500000000l;
  12. struct timespec pol_s = {}; // One half of second
  13. struct timespec now = {};
  14. struct timespec t = {};
  15. int id = 78;
  16. char *str = "test";
  17. int e_id = 0;
  18. void *e_str = NULL;
  19. struct timespec twait = {};
  20. faux_nsec_to_timespec(&pol_s, nsec);
  21. faux_timespec_now(&now);
  22. faux_timespec_sum(&t, &now, &pol_s);
  23. sched = faux_sched_new();
  24. if (!sched)
  25. return -1;
  26. // Wait and get event
  27. faux_sched_once(sched, &t, id, str);
  28. nanosleep(&pol_s, NULL); // wait
  29. if (faux_sched_pop(sched, &e_id, &e_str) < 0)
  30. return -1;
  31. if (e_id != id)
  32. return -1;
  33. if (e_str != str)
  34. return -1;
  35. // Don't wait so pop must return -1
  36. faux_timespec_sum(&t, &t, &pol_s);
  37. faux_sched_once(sched, &t, id, str);
  38. // Don't wait. Pop must return -1
  39. if (faux_sched_pop(sched, &e_id, &e_str) == 0)
  40. return -1;
  41. // Get next event interval. It must be greater than 0 and greater
  42. // than full interval (half of second)
  43. if (faux_sched_next_interval(sched, &twait) < 0)
  44. return -1;
  45. if (faux_timespec_cmp(&twait, &(struct timespec){0, 0}) <= 0)
  46. return -1;
  47. if (faux_timespec_cmp(&twait, &pol_s) >= 0)
  48. return -1;
  49. faux_sched_free(sched);
  50. return 0;
  51. }