1 /*
2  * SpanDSP - a series of DSP components for telephony
3  *
4  * schedule_tests.c
5  *
6  * Written by Steve Underwood <steveu@coppice.org>
7  *
8  * Copyright (C) 2004 Steve Underwood
9  *
10  * All rights reserved.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2, as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25 
26 /*! \page schedule_tests_page Event scheduler tests
27 \section schedule_tests_page_sec_1 What does it do?
28 ???.
29 
30 \section schedule_tests_page_sec_2 How does it work?
31 ???.
32 */
33 
34 #if defined(HAVE_CONFIG_H)
35 #include "config.h"
36 #endif
37 
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 
42 #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
43 #include "spandsp.h"
44 
45 uint64_t when1;
46 uint64_t when2;
47 
callback1(span_sched_state_t * s,void * user_data)48 static void callback1(span_sched_state_t *s, void *user_data)
49 {
50     int id;
51     uint64_t when;
52 
53     when = span_schedule_time(s);
54     printf("1: Callback at %f %" PRId64 "\n", (float) when/1000000.0, when - when1);
55     if ((when - when1))
56     {
57         printf("Callback occured at the wrong time.\n");
58         exit(2);
59     }
60     id = span_schedule_event(s, 500000, callback1, NULL);
61     when1 = when + 500000;
62     when = span_schedule_next(s);
63     printf("1: Event %d, earliest is %" PRId64 "\n", id, when);
64 }
65 
callback2(span_sched_state_t * s,void * user_data)66 static void callback2(span_sched_state_t *s, void *user_data)
67 {
68     int id;
69     uint64_t when;
70 
71     when = span_schedule_time(s);
72     printf("2: Callback at %f %" PRId64 "\n", (float) when/1000000.0, when - when2);
73     id = span_schedule_event(s, 550000, callback2, NULL);
74     if ((when - when2) != 10000)
75     {
76         printf("Callback occured at the wrong time.\n");
77         exit(2);
78     }
79     when2 = when + 550000;
80     when = span_schedule_next(s);
81     printf("2: Event %d, earliest is %" PRId64 "\n", id, when);
82 }
83 
main(int argc,char * argv[])84 int main(int argc, char *argv[])
85 {
86     int i;
87     span_sched_state_t sched;
88     uint64_t when;
89 
90     span_schedule_init(&sched);
91 
92     span_schedule_event(&sched, 500000, callback1, NULL);
93     span_schedule_event(&sched, 550000, callback2, NULL);
94     when1 = span_schedule_time(&sched) + 500000;
95     when2 = span_schedule_time(&sched) + 550000;
96     //span_schedule_del(&sched, id);
97 
98     for (i = 0;  i < 100000000;  i += 20000)
99         span_schedule_update(&sched, 20000);
100     when = span_schedule_time(&sched);
101     if ((when1 - when) < 0  ||  (when1 - when) > 500000  ||  (when2 - when) < 0  ||  (when2 - when) > 550000)
102     {
103         printf("Callback failed to occur.\n");
104         exit(2);
105     }
106     span_schedule_release(&sched);
107 
108     printf("Tests passed.\n");
109     return 0;
110 }
111 /*- End of function --------------------------------------------------------*/
112 /*- End of file ------------------------------------------------------------*/
113