forked from PDXostc/reliable_multicast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_interval_test.c
331 lines (271 loc) · 8.94 KB
/
sub_interval_test.c
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright (C) 2018, Jaguar Land Rover
// This program is licensed under the terms and conditions of the
// Mozilla Public License, version 2.0. The full text of the
// Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
//
// Author: Magnus Feuer ([email protected])
#include "rmc_sub.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static uint8_t _test_print_interval(sub_pid_interval_node_t* node, void* dt)
{
sub_pid_interval_t intv = node->data;
int indent = (int) (uint64_t) dt;
printf("%*cInterval: %lu - %lu\n", indent*2, ' ', intv.first_pid, intv.last_pid);
return 1;
}
static uint8_t _test_print_interval_list(sub_publisher_t* pub)
{
puts("Interval:");
sub_pid_interval_list_for_each(&pub->received_interval, _test_print_interval, (void*) 1);
return 1;
}
static void add_packet(sub_publisher_t* pub, packet_id_t pid)
{
sub_packet_received(pub, pid, "", 0, 0, user_data_nil());
}
static void reset_list(sub_packet_list_t* lst)
{
sub_packet_t *pack = 0;
while(sub_packet_list_pop_head(lst, &pack))
free(pack);
}
void test_packet_interval()
{
sub_publisher_t pub;
sub_pid_interval_list_t ilst;
sub_packet_list_t plst;
sub_pid_interval_node_t* pnode;
sub_pid_interval_t intv = { .first_pid = 0, .last_pid = 0 };
packet_id_t pid = 0;
sub_init_publisher(&pub);
sub_pid_interval_list_init(&ilst, 0, 0, 0);
sub_packet_list_init(&plst, 0, 0, 0);
//
// Try Single packet
//
add_packet(&pub, 100);
sub_process_received_packets(&pub, &plst);
if (sub_pid_interval_list_size(&pub.received_interval) != 1) {
printf("Failed interval test 1.1. Wanted size 1. Got %d\n",
sub_pid_interval_list_size(&pub.received_interval));
_test_print_interval_list(&pub);
exit(255);
}
sub_pid_interval_list_pop_head(&pub.received_interval, &intv);
if (intv.first_pid != 100 || intv.last_pid != 100) {
printf("Failed interval test 1.2. Wanted 100:100. Got %lu:%lu\n",
intv.first_pid, intv.last_pid);
_test_print_interval_list(&pub);
exit(255);
}
sub_reset_publisher(&pub, 0);
sub_packet_list_empty(&plst);
sub_pid_interval_list_empty(&ilst);
//
// Try multiple intervals
//
add_packet(&pub, 4);
add_packet(&pub, 5);
add_packet(&pub, 8);
add_packet(&pub, 9);
add_packet(&pub, 10);
add_packet(&pub, 223);
add_packet(&pub, 224);
add_packet(&pub, 226);
//
// Check integrity of the interval list
//
// 4-5
// 8-10
// 223-224
// 226-226
sub_process_received_packets(&pub, &plst);
if (sub_pid_interval_list_size(&pub.received_interval) != 4) {
printf("Failed interval test 1.3. Wanted size 4. Got %d\n",
sub_pid_interval_list_size(&pub.received_interval));
_test_print_interval_list(&pub);
exit(255);
}
// Check that intervals are correct.
pnode = sub_pid_interval_list_head(&pub.received_interval);
if (pnode->data.first_pid != 4 || pnode->data.last_pid != 5) {
printf("Failed interval test 1.4. Wanted 3:5. Got %lu:%lu\n",
pnode->data.first_pid, pnode->data.last_pid);
_test_print_interval_list(&pub);
exit(255);
}
pnode = sub_pid_interval_list_next(pnode);
if (pnode->data.first_pid != 8 || pnode->data.last_pid != 10) {
printf("Failed interval test 1.5. Wanted 8:10. Got %lu:%lu\n",
pnode->data.first_pid, pnode->data.last_pid);
_test_print_interval_list(&pub);
exit(255);
}
pnode = sub_pid_interval_list_next(pnode);
if (pnode->data.first_pid != 223 || pnode->data.last_pid != 224) {
printf("Failed interval test 1.6. Wanted 223:224. Got %lu:%lu\n",
pnode->data.first_pid, pnode->data.last_pid);
_test_print_interval_list(&pub);
exit(255);
}
pnode = sub_pid_interval_list_next(pnode);
if (pnode->data.first_pid != 226 || pnode->data.last_pid != 226) {
printf("Failed interval test 1.7. Wanted 226:226. Got %lu:%lu\n",
pnode->data.first_pid, pnode->data.last_pid);
_test_print_interval_list(&pub);
exit(255);
}
// Check that we can add a new pid by extending the first interval
// element from 4-5 to 3-5
//
// Before
// 4-5
// 8-10
// 223-224
// 226-226
//
// After
// 3-5
// 8-10
// 223-224
// 226-226
add_packet(&pub, 3);
sub_process_received_packets(&pub, &plst);
if (sub_pid_interval_list_size(&pub.received_interval) != 4) {
printf("Failed interval test 1.8. Wanted size 4. Got %d\n",
sub_pid_interval_list_size(&pub.received_interval));
_test_print_interval_list(&pub);
exit(255);
}
pnode = sub_pid_interval_list_head(&pub.received_interval);
if (pnode->data.first_pid != 3 || pnode->data.last_pid != 5) {
printf("Failed interval test 1.9. Wanted 3:5 Got %lu:%lu\n",
pnode->data.first_pid, pnode->data.last_pid);
_test_print_interval_list(&pub);
exit(255);
}
//
// Check that we can add a new interval element at the very
// beginning of the interval list.
//
// Before
// 3-5
// 8-10
// 223-224
// 226-226
//
// After
// 1-1
// 3-5
// 8-10
// 223-224
// 226-226
add_packet(&pub, 1);
sub_process_received_packets(&pub, &plst);
pnode = sub_pid_interval_list_head(&pub.received_interval);
if (pnode->data.first_pid != 1 || pnode->data.last_pid != 1) {
printf("Failed interval test 1.10. Wanted 1:1 Got %lu:%lu\n",
pnode->data.first_pid, pnode->data.last_pid);
_test_print_interval_list(&pub);
exit(255);
}
//
// Check that we can merge the two first elements
// in the interval list
// Before:
// 1-1
// 3-5
// 8-10
// 223-224
// 226-226
//
// After:
// 1-5
// 8-10
// 223-224
// 226-226
add_packet(&pub, 2);
sub_process_received_packets(&pub, &plst);
if (sub_pid_interval_list_size(&pub.received_interval) != 4) {
printf("Failed interval test 1.11. Wanted size 4. Got %d\n",
sub_pid_interval_list_size(&pub.received_interval));
_test_print_interval_list(&pub);
exit(255);
}
pnode = sub_pid_interval_list_head(&pub.received_interval);
if (pnode->data.first_pid != 1 || pnode->data.last_pid != 5) {
printf("Failed interval test 1.12. Wanted 1:5 Got %lu:%lu\n",
pnode->data.first_pid, pnode->data.last_pid);
_test_print_interval_list(&pub);
exit(255);
}
//
// Check that we can extend an existing interval at the end.
//
// Before
// 1-5
// 8-10
// 223-224
// 226-226
//
// After
// 1-5
// 8-11
// 223-224
// 226-226
add_packet(&pub, 11);
sub_process_received_packets(&pub, &plst);
if (sub_pid_interval_list_size(&pub.received_interval) != 4) {
printf("Failed interval test 1.13. Wanted size 4. Got %d\n",
sub_pid_interval_list_size(&pub.received_interval));
_test_print_interval_list(&pub);
exit(255);
}
pnode = sub_pid_interval_list_head(&pub.received_interval);
pnode = sub_pid_interval_list_next(pnode);
if (pnode->data.first_pid != 8 || pnode->data.last_pid != 11) {
printf("Failed interval test 1.14. Wanted 6:11 Got %lu:%lu\n",
pnode->data.first_pid, pnode->data.last_pid);
_test_print_interval_list(&pub);
exit(255);
}
//
// Check that we can add three consecutive packets to merge an interval.
//
// Before
// 1-5
// 8-11
// 223-224
// 226-226
//
// After
// 1-11
// 223-226
add_packet(&pub, 6);
add_packet(&pub, 7);
add_packet(&pub, 225);
sub_process_received_packets(&pub, &plst);
if (sub_pid_interval_list_size(&pub.received_interval) != 2) {
printf("Failed interval test 1.15. Wanted size 2. Got %d\n",
sub_pid_interval_list_size(&pub.received_interval));
_test_print_interval_list(&pub);
exit(255);
}
// Check that intervals are correct.
pnode = sub_pid_interval_list_head(&pub.received_interval);
if (pnode->data.first_pid != 1 || pnode->data.last_pid != 11) {
printf("Failed interval test 1.16. Wanted 3:5. Got %lu:%lu\n",
pnode->data.first_pid, pnode->data.last_pid);
_test_print_interval_list(&pub);
exit(255);
}
pnode = sub_pid_interval_list_next(pnode);
if (pnode->data.first_pid != 223 || pnode->data.last_pid != 226) {
printf("Failed interval test 1.17. Wanted 8:10. Got %lu:%lu\n",
pnode->data.first_pid, pnode->data.last_pid);
_test_print_interval_list(&pub);
exit(255);
}
}