File: schedule_tests.c

package info (click to toggle)
spandsp 0.0.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 15,796 kB
  • sloc: ansic: 123,211; xml: 13,229; sh: 12,189; cpp: 1,521; makefile: 1,097
file content (112 lines) | stat: -rw-r--r-- 3,201 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * SpanDSP - a series of DSP components for telephony
 *
 * schedule_tests.c
 *
 * Written by Steve Underwood <steveu@coppice.org>
 *
 * Copyright (C) 2004 Steve Underwood
 *
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*! \page schedule_tests_page Event scheduler tests
\section schedule_tests_page_sec_1 What does it do?
???.

\section schedule_tests_page_sec_2 How does it work?
???.
*/

#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
#include "spandsp.h"

uint64_t when1;
uint64_t when2;

static void callback1(span_sched_state_t *s, void *user_data)
{
    int id;
    uint64_t when;

    when = span_schedule_time(s);
    printf("1: Callback at %f %" PRId64 "\n", (float) when/1000000.0, when - when1);
    if ((when - when1))
    {
        printf("Callback occured at the wrong time.\n");
        exit(2);
    }
    id = span_schedule_event(s, 500000, callback1, NULL);
    when1 = when + 500000;
    when = span_schedule_next(s);
    printf("1: Event %d, earliest is %" PRId64 "\n", id, when);
}

static void callback2(span_sched_state_t *s, void *user_data)
{
    int id;
    uint64_t when;

    when = span_schedule_time(s);
    printf("2: Callback at %f %" PRId64 "\n", (float) when/1000000.0, when - when2);
    id = span_schedule_event(s, 550000, callback2, NULL);
    if ((when - when2) != 10000)
    {
        printf("Callback occured at the wrong time.\n");
        exit(2);
    }
    when2 = when + 550000;
    when = span_schedule_next(s);
    printf("2: Event %d, earliest is %" PRId64 "\n", id, when);
}

int main(int argc, char *argv[])
{
    int i;
    span_sched_state_t sched;
    uint64_t when;

    span_schedule_init(&sched);

    span_schedule_event(&sched, 500000, callback1, NULL);
    span_schedule_event(&sched, 550000, callback2, NULL);
    when1 = span_schedule_time(&sched) + 500000;
    when2 = span_schedule_time(&sched) + 550000;
    //span_schedule_del(&sched, id);
    
    for (i = 0;  i < 100000000;  i += 20000)
        span_schedule_update(&sched, 20000);
    when = span_schedule_time(&sched);
    if ((when1 - when) < 0  ||  (when1 - when) > 500000  ||  (when2 - when) < 0  ||  (when2 - when) > 550000)
    {
        printf("Callback failed to occur.\n");
        exit(2);
    }
    span_schedule_release(&sched);

    printf("Tests passed.\n");
    return  0;
}
/*- End of function --------------------------------------------------------*/
/*- End of file ------------------------------------------------------------*/