forked from urbanserj/entente
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dlist_test.c
59 lines (50 loc) · 1.17 KB
/
dlist_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
/* Force DEBUG on so that tests can use assert(). */
#undef NDEBUG
#include <stddef.h>
#include <assert.h>
/* Define the dlist entry type. */
typedef struct entry entry;
struct entry {
entry *next, *prev;
int i;
};
/* Instantiate the dlist. */
#define ENTRY entry
#include "dlist.h"
int main(void)
{
entry *l = NULL, *e;
entry e1, e2, e3;
int i = 0, v[3];
e1.i = 1;
e2.i = 2;
e3.i = 3;
entry_add(&l, &e1);
assert(l == &e1);
assert(l->next == l);
assert(l->prev == l);
entry_add(&l, &e2);
assert(l == &e1);
assert(l->next == &e2);
assert(l->prev == &e2);
entry_add(&l, &e3);
assert(l == &e1);
assert(l->next == &e2);
assert(l->prev == &e3);
entry_rem(&l, l);
assert(l == &e2);
assert(l->next == &e3);
assert(l->prev == &e3);
assert(entry_next(&l, l) == &e3);
assert(entry_prev(&l, l) == NULL);
assert(entry_next(&l, &e3) == NULL);
assert(entry_prev(&l, &e3) == &e2);
for (e = l; e != NULL; e = entry_next(&l, e))
v[i++] = e->i;
assert(v[0] == 2);
assert(v[1] == 3);
entry_rem(&l, &e3);
assert(l == &e2);
entry_rem(&l, l);
assert(l == NULL);
}