testc_sched.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_once(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_ev_t *ev = NULL;
  21. faux_nsec_to_timespec(&pol_s, nsec);
  22. faux_timespec_now(&now);
  23. faux_timespec_sum(&t, &now, &pol_s);
  24. sched = faux_sched_new();
  25. if (!sched)
  26. return -1;
  27. // Schedule event
  28. faux_sched_once(sched, &t, id, str);
  29. // Don't wait so pop must return -1
  30. if (faux_sched_pop(sched))
  31. return -1;
  32. // Get next event interval. It must be greater than 0 and greater
  33. // than full interval (half of second)
  34. if (!faux_sched_next_interval(sched, &twait))
  35. return -1;
  36. if (faux_timespec_cmp(&twait, &(struct timespec){0, 0}) <= 0)
  37. return -1;
  38. if (faux_timespec_cmp(&twait, &pol_s) >= 0)
  39. return -1;
  40. // Wait and get event
  41. nanosleep(&pol_s, NULL); // wait
  42. if (!(ev = faux_sched_pop(sched)))
  43. return -1;
  44. e_id = faux_ev_id(ev);
  45. e_str = faux_ev_data(ev);
  46. if (e_id != id)
  47. return -1;
  48. if (e_str != str)
  49. return -1;
  50. // Schedule event delayed
  51. faux_sched_once(sched, &pol_s, id, str);
  52. // Wait and get event
  53. nanosleep(&pol_s, NULL); // wait
  54. e_id = 0;
  55. e_str = NULL;
  56. if (!(ev = faux_sched_pop(sched)))
  57. return -1;
  58. e_id = faux_ev_id(ev);
  59. e_str = faux_ev_data(ev);
  60. if (e_id != id)
  61. return -1;
  62. if (e_str != str)
  63. return -1;
  64. faux_sched_free(sched);
  65. return 0;
  66. }
  67. int testc_faux_sched_periodic(void)
  68. {
  69. faux_sched_t *sched = NULL;
  70. long long int nsec = 500000000l;
  71. struct timespec pol_s = {}; // One half of second
  72. struct timespec now = {};
  73. struct timespec t = {};
  74. int id = 78;
  75. char *str = "test";
  76. int e_id = 0;
  77. void *e_str = NULL;
  78. faux_ev_t *ev = NULL;
  79. faux_nsec_to_timespec(&pol_s, nsec);
  80. faux_timespec_now(&now);
  81. faux_timespec_sum(&t, &now, &pol_s);
  82. sched = faux_sched_new();
  83. if (!sched)
  84. return -1;
  85. // Schedule event
  86. faux_sched_periodic_delayed(sched, id, str, &pol_s, 2);
  87. // Don't wait so pop must return -1
  88. if (faux_sched_pop(sched)) {
  89. printf("faux_shed_pop: Immediately event\n");
  90. return -1;
  91. }
  92. // Wait and get one event
  93. nanosleep(&pol_s, NULL); // wait
  94. if (!(ev = faux_sched_pop(sched))) {
  95. printf("faux_shed_pop: Can't get 1/2 event\n");
  96. return -1;
  97. }
  98. e_id = faux_ev_id(ev);
  99. e_str = faux_ev_data(ev);
  100. if (e_id != id)
  101. return -1;
  102. if (e_str != str)
  103. return -1;
  104. if (faux_sched_pop(sched)) { // another event?
  105. printf("faux_shed_pop: Two events at once\n");
  106. return -1;
  107. }
  108. nanosleep(&pol_s, NULL); // wait next time
  109. if (!faux_sched_pop(sched)) {
  110. printf("faux_shed_pop: Can't get 2/2 event\n");
  111. return -1;
  112. }
  113. nanosleep(&pol_s, NULL); // wait third time
  114. if (faux_sched_pop(sched)) { // no events any more
  115. printf("faux_shed_pop: The 3/2 event\n");
  116. return -1;
  117. }
  118. faux_sched_free(sched);
  119. return 0;
  120. }
  121. int testc_faux_sched_infinite(void)
  122. {
  123. faux_sched_t *sched = NULL;
  124. long long int nsec = 500000000l;
  125. struct timespec pol_s = {}; // One half of second
  126. struct timespec now = {};
  127. struct timespec t = {};
  128. int id = 78;
  129. char *str = "test";
  130. int e_id = 0;
  131. void *e_str = NULL;
  132. faux_ev_t *ev = NULL;
  133. faux_nsec_to_timespec(&pol_s, nsec);
  134. faux_timespec_now(&now);
  135. faux_timespec_sum(&t, &now, &pol_s);
  136. sched = faux_sched_new();
  137. if (!sched)
  138. return -1;
  139. // Schedule event
  140. faux_sched_periodic_delayed(sched, id, str, &pol_s,
  141. FAUX_SCHED_INFINITE);
  142. // Don't wait so pop must return -1
  143. if (faux_sched_pop(sched)) {
  144. printf("faux_shed_pop: Immediately event\n");
  145. return -1;
  146. }
  147. // Wait and get one event
  148. nanosleep(&pol_s, NULL); // wait
  149. if (!(ev = faux_sched_pop(sched))) {
  150. printf("faux_shed_pop: Can't get 1 event\n");
  151. return -1;
  152. }
  153. e_id = faux_ev_id(ev);
  154. e_str = faux_ev_data(ev);
  155. if (e_id != id)
  156. return -1;
  157. if (e_str != str)
  158. return -1;
  159. if (faux_sched_pop(sched)) { // another event?
  160. printf("faux_shed_pop: Two events at once\n");
  161. return -1;
  162. }
  163. nanosleep(&pol_s, NULL); // wait next time
  164. if (!faux_sched_pop(sched)) {
  165. printf("faux_shed_pop: Can't get 2 event\n");
  166. return -1;
  167. }
  168. faux_sched_del_all(sched); // Empty the list
  169. nanosleep(&pol_s, NULL); // wait third time
  170. if (faux_sched_pop(sched)) {
  171. printf("faux_shed_pop: Event after empty operation\n");
  172. return -1;
  173. }
  174. faux_sched_free(sched);
  175. return 0;
  176. }