123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- #include <sys/time.h>
- #include <time.h>
- #include <errno.h>
- #include <stdint.h>
- #include <stdio.h>
- #include "faux/time.h"
- #include "faux/sched.h"
- int testc_faux_sched_once(void)
- {
- faux_sched_t *sched = NULL;
- long long int nsec = 500000000l;
- struct timespec pol_s = {}; // One half of second
- struct timespec now = {};
- struct timespec t = {};
- int id = 78;
- char *str = "test";
- int e_id = 0;
- void *e_str = NULL;
- struct timespec twait = {};
- faux_ev_t *ev = NULL;
- faux_nsec_to_timespec(&pol_s, nsec);
- faux_timespec_now(&now);
- faux_timespec_sum(&t, &now, &pol_s);
- sched = faux_sched_new();
- if (!sched)
- return -1;
- // Schedule event
- faux_sched_once(sched, &t, id, str);
- // Don't wait so pop must return -1
- if (faux_sched_pop(sched))
- return -1;
- // Get next event interval. It must be greater than 0 and greater
- // than full interval (half of second)
- if (!faux_sched_next_interval(sched, &twait))
- return -1;
- if (faux_timespec_cmp(&twait, &(struct timespec){0, 0}) <= 0)
- return -1;
- if (faux_timespec_cmp(&twait, &pol_s) >= 0)
- return -1;
- // Wait and get event
- nanosleep(&pol_s, NULL); // wait
- if (!(ev = faux_sched_pop(sched)))
- return -1;
- e_id = faux_ev_id(ev);
- e_str = faux_ev_data(ev);
- if (e_id != id)
- return -1;
- if (e_str != str)
- return -1;
- // Schedule event delayed
- faux_sched_once(sched, &pol_s, id, str);
- // Wait and get event
- nanosleep(&pol_s, NULL); // wait
- e_str = NULL;
- if (!(ev = faux_sched_pop(sched)))
- return -1;
- e_id = faux_ev_id(ev);
- e_str = faux_ev_data(ev);
- if (e_id != id)
- return -1;
- if (e_str != str)
- return -1;
- faux_sched_free(sched);
- return 0;
- }
- int testc_faux_sched_periodic(void)
- {
- faux_sched_t *sched = NULL;
- long long int nsec = 500000000l;
- struct timespec pol_s = {}; // One half of second
- struct timespec now = {};
- struct timespec t = {};
- int id = 78;
- char *str = "test";
- int e_id = 0;
- void *e_str = NULL;
- faux_ev_t *ev = NULL;
- faux_nsec_to_timespec(&pol_s, nsec);
- faux_timespec_now(&now);
- faux_timespec_sum(&t, &now, &pol_s);
- sched = faux_sched_new();
- if (!sched)
- return -1;
- // Schedule event
- faux_sched_periodic_delayed(sched, id, str, &pol_s, 2);
- // Don't wait so pop must return -1
- if (faux_sched_pop(sched)) {
- printf("faux_shed_pop: Immediately event\n");
- return -1;
- }
- // Wait and get one event
- nanosleep(&pol_s, NULL); // wait
- if (!(ev = faux_sched_pop(sched))) {
- printf("faux_shed_pop: Can't get 1/2 event\n");
- return -1;
- }
- e_id = faux_ev_id(ev);
- e_str = faux_ev_data(ev);
- if (e_id != id)
- return -1;
- if (e_str != str)
- return -1;
- if (faux_sched_pop(sched)) { // another event?
- printf("faux_shed_pop: Two events at once\n");
- return -1;
- }
- nanosleep(&pol_s, NULL); // wait next time
- if (!faux_sched_pop(sched)) {
- printf("faux_shed_pop: Can't get 2/2 event\n");
- return -1;
- }
- nanosleep(&pol_s, NULL); // wait third time
- if (faux_sched_pop(sched)) { // no events any more
- printf("faux_shed_pop: The 3/2 event\n");
- return -1;
- }
- faux_sched_free(sched);
- return 0;
- }
- int testc_faux_sched_infinite(void)
- {
- faux_sched_t *sched = NULL;
- long long int nsec = 500000000l;
- struct timespec pol_s = {}; // One half of second
- struct timespec now = {};
- struct timespec t = {};
- int id = 78;
- char *str = "test";
- int e_id = 0;
- void *e_str = NULL;
- faux_ev_t *ev = NULL;
- faux_nsec_to_timespec(&pol_s, nsec);
- faux_timespec_now(&now);
- faux_timespec_sum(&t, &now, &pol_s);
- sched = faux_sched_new();
- if (!sched)
- return -1;
- // Schedule event
- faux_sched_periodic_delayed(sched, id, str, &pol_s,
- FAUX_SCHED_INFINITE);
- // Don't wait so pop must return -1
- if (faux_sched_pop(sched)) {
- printf("faux_shed_pop: Immediately event\n");
- return -1;
- }
- // Wait and get one event
- nanosleep(&pol_s, NULL); // wait
- if (!(ev = faux_sched_pop(sched))) {
- printf("faux_shed_pop: Can't get 1 event\n");
- return -1;
- }
- e_id = faux_ev_id(ev);
- e_str = faux_ev_data(ev);
- if (e_id != id)
- return -1;
- if (e_str != str)
- return -1;
- if (faux_sched_pop(sched)) { // another event?
- printf("faux_shed_pop: Two events at once\n");
- return -1;
- }
- nanosleep(&pol_s, NULL); // wait next time
- if (!faux_sched_pop(sched)) {
- printf("faux_shed_pop: Can't get 2 event\n");
- return -1;
- }
- faux_sched_del_all(sched); // Empty the list
- nanosleep(&pol_s, NULL); // wait third time
- if (faux_sched_pop(sched)) {
- printf("faux_shed_pop: Event after empty operation\n");
- return -1;
- }
- faux_sched_free(sched);
- return 0;
- }
|