-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_utimer.c
More file actions
246 lines (207 loc) · 5.45 KB
/
Copy pathcheck_utimer.c
File metadata and controls
246 lines (207 loc) · 5.45 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "utimer.h"
#include <stdlib.h>
#include <stdint.h>
#include <check.h>
#define MAX_EVENTS 128
#define MAX_ACTIONS 128
#define MAX_TIMERS 16
typedef struct {
enum {
ACTION_LAST = 0,
ACTION_NOOP,
ACTION_START,
ACTION_STOP,
} type;
utimer_t *timer;
ticks_t ticks;
bool next;
} action_t;
typedef struct {
utimer_t *timer;
ticks_t ticks;
} event_t;
static ticks_t m_ticks;
static struct {
event_t events[MAX_EVENTS];
unsigned index;
} m_event_list;
static struct {
action_t actions[MAX_ACTIONS];
unsigned index;
} m_action_list;
static utimer_t *m_timers[MAX_TIMERS];
static void callback(void *ud)
{
utimer_t *timer = *(utimer_t**)ud;
m_event_list.events[m_event_list.index++] = (event_t) { .ticks = m_ticks, .timer = timer};
bool next = true;
while (next) {
action_t *action = &m_action_list.actions[m_action_list.index];
switch (action->type) {
case ACTION_LAST:
return;
case ACTION_NOOP:
break;
case ACTION_START:
utimer_start(action->timer, action->ticks);
break;
case ACTION_STOP:
utimer_stop(action->timer);
break;
}
next = action->next;
m_action_list.index++;
}
}
static void forward(ticks_t ticks)
{
m_ticks += ticks;
utimer_schedule(m_ticks);
}
void setup(void)
{
m_ticks = UINT32_MAX - 20;
utimer_schedule(m_ticks);
for (int i = 0; i < MAX_TIMERS; i++) {
m_timers[i] = utimer_new(callback, &m_timers[i]);
}
}
void teardown(void)
{
for (int i = 0; i < MAX_TIMERS; i++) {
utimer_delete(m_timers[i]);
}
}
START_TEST(test_utimer_single)
{
utimer_start(m_timers[0], 10);
forward(9);
ck_assert_int_eq(m_event_list.index, 0);
forward(1);
ck_assert_int_eq(m_event_list.index, 1);
forward(100);
ck_assert_int_eq(m_event_list.index, 1);
utimer_start(m_timers[0], 10);
forward(100);
ck_assert_int_eq(m_event_list.index, 2);
}
END_TEST
START_TEST(test_utimer_stop_1)
{
m_action_list.actions[0].type = ACTION_STOP;
m_action_list.actions[0].timer = m_timers[1];
utimer_start(m_timers[0], 10);
utimer_start(m_timers[1], 20);
forward(9);
ck_assert_int_eq(m_event_list.index, 0);
forward(100);
ck_assert_int_eq(m_event_list.index, 1);
forward(100);
ck_assert_int_eq(m_event_list.index, 1);
}
END_TEST
START_TEST(test_utimer_stop_2)
{
m_action_list.actions[0].type = ACTION_STOP;
m_action_list.actions[0].next = true;
m_action_list.actions[0].timer = m_timers[1];
m_action_list.actions[1].type = ACTION_STOP;
m_action_list.actions[1].timer = m_timers[2];
utimer_start(m_timers[0], 10);
utimer_start(m_timers[1], 20);
utimer_start(m_timers[2], 21);
utimer_start(m_timers[3], 21);
forward(9);
ck_assert_int_eq(m_event_list.index, 0);
forward(100);
ck_assert_int_eq(m_event_list.index, 2);
forward(100);
ck_assert_int_eq(m_event_list.index, 2);
}
END_TEST
START_TEST(test_utimer_restart_same)
{
m_action_list.actions[0].type = ACTION_START;
m_action_list.actions[0].ticks = 1;
m_action_list.actions[0].timer = m_timers[0];
utimer_start(m_timers[0], 10);
forward(10);
ck_assert_int_eq(m_event_list.index, 1);
forward(1);
ck_assert_int_eq(m_event_list.index, 2);
}
END_TEST
START_TEST(test_utimer_restart_other)
{
m_action_list.actions[0].type = ACTION_START;
m_action_list.actions[0].ticks = 2;
m_action_list.actions[0].timer = m_timers[1];
utimer_start(m_timers[0], 10);
utimer_start(m_timers[1], 11);
forward(20);
ck_assert_int_eq(m_event_list.index, 1);
forward(1);
ck_assert_int_eq(m_event_list.index, 1);
forward(2);
ck_assert_int_eq(m_event_list.index, 2);
}
END_TEST
START_TEST(test_utimer_immediate)
{
m_action_list.actions[0].type = ACTION_START;
m_action_list.actions[0].ticks = 0;
m_action_list.actions[0].timer = m_timers[1];
utimer_start(m_timers[0], 10);
forward(10);
ck_assert_int_eq(m_event_list.index, 2);
ck_assert_ptr_eq(m_event_list.events[0].timer, m_timers[0]);
ck_assert_ptr_eq(m_event_list.events[1].timer, m_timers[1]);
}
END_TEST
START_TEST(test_utimer_immediate2)
{
m_action_list.actions[0].type = ACTION_START;
m_action_list.actions[0].ticks = 0;
m_action_list.actions[0].timer = m_timers[2];
utimer_start(m_timers[0], 10);
utimer_start(m_timers[1], 10);
forward(10);
ck_assert_int_eq(m_event_list.index, 3);
ck_assert_ptr_eq(m_event_list.events[0].timer, m_timers[0]);
ck_assert_ptr_eq(m_event_list.events[1].timer, m_timers[1]);
ck_assert_ptr_eq(m_event_list.events[2].timer, m_timers[2]);
}
END_TEST
Suite * utimer_suite(void)
{
Suite *s;
TCase *tc_single;
TCase *tc_periodic;
TCase *tc_oneshot;
TCase *tc_mixed;
s = suite_create("utimer");
/* Core test case */
tc_single = tcase_create("Single");
tcase_add_checked_fixture(tc_single, setup, teardown);
tcase_add_test(tc_single, test_utimer_single);
tcase_add_test(tc_single, test_utimer_stop_1);
tcase_add_test(tc_single, test_utimer_stop_2);
tcase_add_test(tc_single, test_utimer_restart_same);
tcase_add_test(tc_single, test_utimer_restart_other);
tcase_add_test(tc_single, test_utimer_immediate);
tcase_add_test(tc_single, test_utimer_immediate2);
suite_add_tcase(s, tc_single);
return s;
}
int main(void)
{
int number_failed;
Suite *s;
SRunner *sr;
s = utimer_suite();
sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}