forked from PDXostc/reliable_multicast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_test.c
304 lines (244 loc) · 6.73 KB
/
list_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
// 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])
// Trivial double linked list.
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
typedef uint32_t test_id_t;
#include "rmc_common.h"
#include "rmc_list.h"
#include "rmc_list_template.h"
RMC_LIST(test_list, test_node_t, test_id_t)
RMC_LIST_IMPL(test_list, test_node_t, test_id_t)
void test_dump_list(test_list* list)
{
test_node_t* node = test_list_head(list);
printf("LIST: Element count: %d\n", test_list_size(list));
while(node) {
printf(" node[%p] data[%d]\n",
node, node->data);
node = test_list_next(node);
}
}
static uint8_t _test_sequence(test_list* list, test_id_t start, test_id_t stop)
{
test_node_t* node = 0;
test_id_t pid = start;
// Traverse forward
node = test_list_head(list);
while(node) {
if (node->data != pid) {
printf("Fwd Sequence test [%d-%d]. Wanted %d. Got %d\n",
start, stop, pid, node->data);
return 1;
}
node = test_list_next(node);
if (start < stop)
pid++;
else
pid--;
}
if (start < stop)
pid--;
else
pid++;
if (pid != stop) {
printf("Fwd Sequence test [%d-%d]. Wanted final %d. Got %d\n",
start, stop, pid, node->data);
return 2;
}
// Traverse backward
node = test_list_tail(list);
pid = stop;
while(node) {
if (node->data != pid) {
printf("Bwd Sequence test [%d-%d]. Wanted %d. Got %d\n",
start, stop, pid, node->data);
return 3;
}
node = test_list_prev(node);
if (start < stop)
pid--;
else
pid++;
}
if (start < stop)
pid++;
else
pid--;
if (pid != start) {
printf("Bwd Sequence test [%d-%d]. Wanted final %d. Got %d\n",
start, stop, pid, node->data);
return 4;
}
return 0;
}
static int _compare_pid(test_id_t existing_pid, test_id_t new_pid)
{
if (existing_pid > new_pid)
return 1;
if (existing_pid < new_pid)
return -1;
return 0;
}
static int _find_pid(test_id_t pid1, test_id_t pid2)
{
return pid1 == pid2;
}
void run_list_tests()
{
test_list p1;
test_node_t* node;
test_id_t pid = 1;
test_list_init(&p1, 0, 0, 0);
// 1
test_list_push_tail(&p1, 1);
if (_test_sequence(&p1, 1, 1)) {
puts("Failed list test 1.1.\n");
exit(255);
}
// 2
test_list_push_tail(&p1, 2);
if (_test_sequence(&p1, 1, 2)) {
puts("Failed list test 1.2.\n");
exit(255);
}
// 3
test_list_push_tail(&p1, 3);
if (_test_sequence(&p1, 1, 3)) {
puts("Failed list test 1.3.\n");
exit(255);
}
// 4
test_list_push_tail(&p1, 4);
if (_test_sequence(&p1, 1, 4)) {
puts("Failed list test 1.4.\n");
exit(255);
}
// 5
test_list_push_tail(&p1, 5);
if (_test_sequence(&p1, 1, 5)) {
puts("Failed list test 1.5.\n");
exit(255);
}
// 6
test_list_push_tail(&p1, 6);
if (_test_sequence(&p1, 1, 6)) {
puts("Failed list test 1.6.\n");
exit(255);
}
// Insert in middle of list.
node = test_list_head(&p1); // pid == 1
node = test_list_next(node); // pid == 2
node = test_list_next(node); // pid == 3
test_list_insert_after(node, 31);
// Validate list
if (node->data != 3) {
printf("Failed list test 2.1. Wanted 3 Got %d\n",
node->data);
exit(255);
}
if (test_list_next(node)->data != 31) {
printf("Failed list test 2.2. Wanted 31 Got %d\n",
node->data);
exit(255);
}
if (test_list_next(test_list_next(node))->data != 4) {
printf("Failed list test 2.3. Wanted 4 Got %d\n",
node->data);
exit(255);
}
// Delete the element we just put in
node = test_list_next(node);
test_list_delete(node);
if (_test_sequence(&p1, 1, 6)) {
puts("Failed list test 3.1.\n");
exit(255);
}
// Delete tail element
test_list_delete(test_list_tail(&p1));
if (_test_sequence(&p1, 1, 5)) {
puts("Failed list test 3.2.\n");
exit(255);
}
// Delete head element
test_list_delete(test_list_head(&p1));
if (_test_sequence(&p1, 2, 5)) {
puts("Failed list test 3.3.\n");
exit(255);
}
//
// Test sorted by pid
//
while(test_list_pop_head(&p1, &pid));
// Check that list is empty
if (test_list_pop_head(&p1, &pid) != 0) {
puts("Failed list test 4.0.\n");
exit(255);
}
test_list_insert_sorted(&p1, 2, _compare_pid);
if (_test_sequence(&p1, 2, 2)) {
puts("Failed list test 4.1.\n");
exit(255);
}
test_list_insert_sorted(&p1, 1, _compare_pid);
if (_test_sequence(&p1, 2, 1)) {
puts("Failed list test 4.2.\n");
exit(255);
}
test_list_insert_sorted(&p1, 3, _compare_pid);
if (_test_sequence(&p1, 3, 1)) {
puts("Failed list test 4.3.\n");
exit(255);
}
test_list_insert_sorted(&p1, 7, _compare_pid);
test_list_insert_sorted(&p1, 6, _compare_pid);
test_list_insert_sorted(&p1, 5, _compare_pid);
test_list_insert_sorted(&p1, 4, _compare_pid);
if (_test_sequence(&p1, 7, 1)) {
puts("Failed list test 4.4.\n");
exit(255);
}
test_list_insert_sorted(&p1, 8, _compare_pid);
if (_test_sequence(&p1, 8, 1)) {
puts("Failed list test 4.5.\n");
exit(255);
}
node = test_list_find_node(&p1, 1, _find_pid);
if (node->data != 1) {
puts("Failed list test 5.1.\n");
exit(255);
}
test_list_delete(node);
node = test_list_find_node(&p1, 1, _find_pid);
if (node) {
puts("Failed list test 5.2.\n");
exit(255);
}
node = test_list_find_node(&p1, 2, _find_pid);
if (node->data != 2) {
puts("Failed list test 5.3.\n");
exit(255);
}
test_list_delete(node);
node = test_list_find_node(&p1, 2, _find_pid);
if (node) {
puts("Failed list test 5.4.\n");
exit(255);
}
node = test_list_find_node(&p1, 7, _find_pid);
if (node->data != 7) {
puts("Failed list test 5.5.\n");
exit(255);
}
test_list_delete(node);
node = test_list_find_node(&p1, 7, _find_pid);
if (node) {
puts("Failed list test 5.6.\n");
exit(255);
}
}