Statistics
| Branch: | Tag: | Revision:

sssimulator / EventScheduler / Test / event_scheduler_test.c @ 0bf1a874

History | View | Annotate | Download (3.81 KB)

1
/*
2
 * this is sssim: the simple & stupid simulator
3
 *
4
 *  copyright (c) 2015 luca baldesi
5
 *
6
 *  this is free software; see gpl-3.0.txt
7
 */
8

    
9
#include<malloc.h>
10
#include<assert.h>
11

    
12
#include"event_scheduler.h"
13

    
14
void event_scheduler_destroy_test()
15
{
16
        event_scheduler_destroy(NULL);
17
        fprintf(stderr,"%s successfully passed!\n",__func__);
18
}
19

    
20
void event_scheduler_new_test()
21
{
22
        struct event_scheduler * es;
23

    
24
        es = event_scheduler_new(0);
25
        assert(es != NULL);
26
        event_scheduler_destroy(&es);
27
        assert(es == NULL);
28

    
29
        es = event_scheduler_new(1);
30
        assert(es != NULL);
31
        event_scheduler_destroy(&es);
32
        assert(es == NULL);
33

    
34
        fprintf(stderr,"%s successfully passed!\n",__func__);
35
}
36

    
37
void event_scheduler_insert_test()
38
{
39
        struct event_scheduler * es;
40
        struct action_event * ev;
41
        int res;
42
        uint8_t priority = 0;
43

    
44
        es = event_scheduler_new(0);
45
        ev = action_event_new(NULL);
46

    
47
        res = event_scheduler_insert(NULL,NULL,-1, priority);
48
        assert(res < 0);
49

    
50
        res = event_scheduler_insert(NULL,ev,-1, priority);
51
        assert(res < 0);
52

    
53
        res = event_scheduler_insert(NULL,NULL,1, priority);
54
        assert(res < 0);
55

    
56
        res = event_scheduler_insert(NULL,ev,1, priority);
57
        assert(res < 0);
58

    
59
        res = event_scheduler_insert(es,NULL,-1, priority);
60
        assert(res < 0);
61

    
62
        res = event_scheduler_insert(es,ev,-1, priority);
63
        assert(res < 0);
64

    
65
        res = event_scheduler_insert(es,NULL,1, priority);
66
        assert(res < 0);
67

    
68
        res = event_scheduler_insert(es,ev,1, priority);
69
        assert(res == 0);
70

    
71
        event_scheduler_destroy(&es);
72
        action_event_destroy(&ev);
73

    
74
        fprintf(stderr,"%s successfully passed!\n",__func__);
75
}
76

    
77
void event_scheduler_queue_length_test()
78
{
79
        struct event_scheduler * es;
80
        struct action_event * ev;
81
        uint32_t res;
82
        uint8_t priority = 0;
83

    
84
        es = event_scheduler_new(0);
85
        ev = action_event_new(NULL);
86

    
87
        res = event_scheduler_queue_length(NULL);
88
        assert(res == 0);
89

    
90
        res = event_scheduler_queue_length(es);
91
        assert(res == 0);
92

    
93
        event_scheduler_insert(es,ev,0, priority);
94
        res = event_scheduler_queue_length(es);
95
        assert(res == 1);
96

    
97
        event_scheduler_insert(es,ev,0, priority);
98
        res = event_scheduler_queue_length(es);
99
        assert(res == 2);
100

    
101
        event_scheduler_destroy(&es);
102
        action_event_destroy(&ev);
103

    
104
        fprintf(stderr,"%s successfully passed!\n",__func__);
105
}
106

    
107
void event_scheduler_elapsed_time_test()
108
{
109
        struct event_scheduler * es;
110

    
111
        es = event_scheduler_new(0);
112

    
113
        assert(event_scheduler_elapsed_time(NULL) < 0);
114
        assert(event_scheduler_elapsed_time(es) == 0);
115

    
116
        event_scheduler_destroy(&es);
117
        fprintf(stderr,"%s successfully passed!\n",__func__);
118
}
119

    
120
void event_scheduler_pop_test()
121
{
122
        struct event_scheduler * es;
123
        struct action_event * ev, *ev2, *ev3, *ev4;
124
        uint8_t priority = 0;
125

    
126
        es = event_scheduler_new(0);
127
        ev = action_event_new(NULL);
128

    
129
        assert(event_scheduler_pop(NULL) == NULL);
130
        assert(event_scheduler_pop(es) == NULL);
131
        event_scheduler_insert(es,ev,1, priority);
132
        assert(event_scheduler_pop(es) == ev);
133
        assert(event_scheduler_queue_length(es) == 0);
134
        assert(event_scheduler_elapsed_time(es) == 1);
135

    
136
        ev2 = action_event_new(NULL);
137
        ev3 = action_event_new(NULL);
138
        ev4= action_event_new(NULL);
139
        event_scheduler_insert(es,ev4,3, priority);
140
        event_scheduler_insert(es,ev2,1, priority);
141
        event_scheduler_insert(es,ev3,2, priority);
142

    
143
        assert(event_scheduler_pop(es) == ev2);
144
        assert(event_scheduler_elapsed_time(es) == 2);
145
        assert(event_scheduler_pop(es) == ev3);
146
        assert(event_scheduler_elapsed_time(es) == 3);
147
        assert(event_scheduler_pop(es) == ev4);
148
        assert(event_scheduler_elapsed_time(es) == 4);
149

    
150
        event_scheduler_destroy(&es);
151
        action_event_destroy(&ev);
152
        action_event_destroy(&ev2);
153
        action_event_destroy(&ev3);
154
        action_event_destroy(&ev4);
155

    
156
        fprintf(stderr,"%s successfully passed!\n",__func__);
157
}
158

    
159
int main(char ** argc,int argv)
160
{
161
        event_scheduler_destroy_test();
162
        event_scheduler_new_test();
163
        event_scheduler_insert_test();
164
        event_scheduler_queue_length_test();
165
        event_scheduler_elapsed_time_test();
166
        event_scheduler_pop_test();
167
        return 0;
168
}