aboutsummaryrefslogtreecommitdiff
path: root/tests/linked_list.c
blob: 71322b8b817c3aab3bb9ab1ec3c1561a817c75f6 (plain)
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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "linked_list.h"

struct list_elem {
	struct linked_list link;
	char *content;
};

static void test_linked_list_init(void) {
	struct linked_list list;
	linked_list_init(&list);

	// Both next and prev should point to self
	assert(list.next == &list && list.prev == &list);

	// The list should be empty
	assert(linked_list_empty(&list));
}

static void test_linked_list_single_insert(void) {
	struct linked_list list;
	linked_list_init(&list);

	struct list_elem elem1 = {{0}, NULL};
	linked_list_insert(&list, &elem1.link);

	// Both next and prev on list should point to the elem
	assert(list.next == &elem1.link && list.prev == &elem1.link);

	// Both next and prev on elem should point to the list
	assert(elem1.link.next == &list && elem1.link.prev == &list);

	// The list and element should not be empty
	assert(!linked_list_empty(&list));
	assert(!linked_list_empty(&elem1.link));
}

static void test_linked_list_single_remove(void) {
	struct linked_list list;
	linked_list_init(&list);

	struct list_elem elem1 = {{0}, NULL};
	linked_list_insert(&list, &elem1.link);
	linked_list_remove(&elem1.link);

	// Both next and prev on elem be NULL
	assert(elem1.link.next == NULL && elem1.link.prev == NULL);

	// Both next and prev should point to self
	assert(list.next == &list && list.prev == &list);

	// The list should be empty
	assert(linked_list_empty(&list));
}

static void test_linked_list_alternate_remove(void) {
	struct linked_list list;
	linked_list_init(&list);

	struct list_elem elem1 = {{0}, NULL};
	linked_list_insert(&list, &elem1.link);
	linked_list_remove(&list);

	// Both next and prev on list be NULL
	assert(list.next == NULL && list.prev == NULL);

	// Both next and prev should point to self
	assert(elem1.link.next == &elem1.link && elem1.link.prev == &elem1.link);

	// The elem should be empty
	assert(linked_list_empty(&elem1.link));
}

static void test_linked_list_sequential_remove(void) {
	struct linked_list list;
	linked_list_init(&list);

	struct list_elem elem1 = {{0}, NULL}, elem2 = {{0}, NULL}, elem3 = {{0}, NULL};
	linked_list_insert(&list, &elem1.link);
	linked_list_insert(&elem1.link, &elem2.link);
	linked_list_insert(&elem2.link, &elem3.link);

	// The order should now be list→elem1→elem2→elem3→list
	assert(list.next == &elem1.link && list.prev == &elem3.link);
	assert(elem1.link.next == &elem2.link && elem1.link.prev == &list);
	assert(elem2.link.next == &elem3.link && elem2.link.prev == &elem1.link);
	assert(elem3.link.next == &list && elem3.link.prev == &elem2.link);

	linked_list_remove(list.next);

	// The order should now be list→elem2→elem3→list
	assert(list.next == &elem2.link && list.prev == &elem3.link);
	assert(elem2.link.next == &elem3.link && elem2.link.prev == &list);
	assert(elem3.link.next == &list && elem3.link.prev == &elem2.link);
	assert(elem1.link.next == NULL && elem1.link.prev == NULL);

	linked_list_remove(list.next);

	// The order should now be list→elem3→list
	assert(list.next == &elem3.link && list.prev == &elem3.link);
	assert(elem3.link.next == &list && elem3.link.prev == &list);
	assert(elem1.link.next == NULL && elem1.link.prev == NULL);
	assert(elem2.link.next == NULL && elem2.link.prev == NULL);

	linked_list_remove(list.next);

	// The list should now be empty
	assert(elem1.link.next == NULL && elem1.link.prev == NULL);
	assert(elem2.link.next == NULL && elem2.link.prev == NULL);
	assert(elem3.link.next == NULL && elem3.link.prev == NULL);
	assert(list.next == &list && list.prev == &list);
	assert(linked_list_empty(&list));
}

static void test_linked_list_insert_after(void) {
	struct linked_list list;
	linked_list_init(&list);

	struct list_elem elem1 = {{0}, NULL}, elem2 = {{0}, NULL}, elem3 = {{0}, NULL};
	linked_list_insert(&list, &elem1.link);
	linked_list_insert(&elem1.link, &elem3.link);
	linked_list_insert(&elem1.link, &elem2.link);

	// The order should now be list→elem1→elem2→elem3→list
	assert(list.next == &elem1.link && list.prev == &elem3.link);
	assert(elem1.link.next == &elem2.link && elem1.link.prev == &list);
	assert(elem2.link.next == &elem3.link && elem2.link.prev == &elem1.link);
	assert(elem3.link.next == &list && elem3.link.prev == &elem2.link);
}

static void test_linked_list_remove_loop(void) {
	struct linked_list list;
	linked_list_init(&list);

	struct list_elem elem1 = {{0}, NULL}, elem2 = {{0}, NULL}, elem3 = {{0}, NULL};
	linked_list_insert(&list, &elem1.link);
	linked_list_insert(&elem1.link, &elem2.link);
	linked_list_insert(&elem2.link, &elem3.link);

	size_t cnt = 0;
	while (!linked_list_empty(&list)) {
		struct list_elem *elem = (struct list_elem *)list.next;
		linked_list_remove(&elem->link);
		cnt++;
	}
	assert(cnt == 3);

	// Link should now be empty, and next and prev on all elements hsould be NULL
	assert(linked_list_empty(&list));
	assert(elem1.link.next == NULL && elem1.link.prev == NULL);
	assert(elem2.link.next == NULL && elem2.link.prev == NULL);
	assert(elem3.link.next == NULL && elem3.link.prev == NULL);
}

static void test_linked_list_manual_iterate(void) {
	struct linked_list list;
	linked_list_init(&list);

	struct list_elem elem1 = {{0}, "elem1"};
	struct list_elem elem2 = {{0}, "elem2"};
	struct list_elem elem3 = {{0}, "elem3"};
	linked_list_insert(&list, &elem1.link);
	linked_list_insert(&elem1.link, &elem2.link);
	linked_list_insert(&elem2.link, &elem3.link);

	struct list_elem *ptr = NULL;

	ptr = (struct list_elem *)list.next;
	assert(strcmp("elem1", ptr->content) == 0);

	ptr = (struct list_elem *)ptr->link.next;
	assert(strcmp("elem2", ptr->content) == 0);

	ptr = (struct list_elem *)ptr->link.next;
	assert(strcmp("elem3", ptr->content) == 0);

	assert(ptr->link.next == &list);
}

static void test_linked_list_loop_iterate(void) {
	struct linked_list list;
	linked_list_init(&list);

	struct list_elem elem1 = {{0}, "elem"};
	struct list_elem elem2 = {{0}, "elem"};
	struct list_elem elem3 = {{0}, "elem"};
	linked_list_insert(&list, &elem1.link);
	linked_list_insert(&elem1.link, &elem2.link);
	linked_list_insert(&elem1.link, &elem3.link);

	size_t cnt = 0;
	for (struct linked_list *ptr = list.next; ptr != &list; ptr = ptr->next) {
		struct list_elem *elem = (struct list_elem *)ptr;
		assert(strcmp("elem", elem->content) == 0);
		cnt++;
	}
	assert(cnt == 3);
}

static void test_linked_list_take_empty(void) {
	struct linked_list list1, list2;
	linked_list_init(&list1);
	linked_list_init(&list2);

	linked_list_take(&list2, &list1);

	assert(linked_list_empty(&list1));
	assert(linked_list_empty(&list2));
}

static void test_linked_list_take_single(void) {
	struct linked_list list1, list2;
	linked_list_init(&list1);
	linked_list_init(&list2);

	struct list_elem elem1 = {{0}, NULL};
	linked_list_insert(&list1, &elem1.link);

	linked_list_take(&list2, &list1);

	assert(linked_list_empty(&list1));
	assert(list2.next == &elem1.link && list2.prev == &elem1.link);
	assert(elem1.link.next == &list2 && elem1.link.prev == &list2);
}

static void test_linked_list_take_many(void) {
	struct linked_list list1, list2;
	linked_list_init(&list1);
	linked_list_init(&list2);

	struct list_elem elem1 = {{0}, NULL};
	struct list_elem elem2 = {{0}, NULL};
	linked_list_insert(&list1, &elem2.link);
	linked_list_insert(&list1, &elem1.link);

	linked_list_take(&list2, &list1);

	assert(linked_list_empty(&list1));
	assert(list2.next == &elem1.link && list2.prev == &elem2.link);
	assert(elem1.link.next == &elem2.link && elem1.link.prev == &list2);
	assert(elem2.link.next == &list2 && elem2.link.prev == &elem1.link);
}

static void test_linked_list_take_concat(void) {
	struct linked_list list1, list2;
	linked_list_init(&list1);
	linked_list_init(&list2);

	struct list_elem elem1 = {{0}, NULL};
	struct list_elem elem2 = {{0}, NULL};
	struct list_elem elem3 = {{0}, NULL};
	struct list_elem elem4 = {{0}, NULL};
	linked_list_insert(&list1, &elem2.link);
	linked_list_insert(&list1, &elem1.link);
	linked_list_insert(&list2, &elem4.link);
	linked_list_insert(&list2, &elem3.link);

	linked_list_take(&list2, &list1);

	assert(linked_list_empty(&list1));
	assert(list2.next == &elem1.link && list2.prev == &elem4.link);
	assert(elem1.link.next == &elem2.link && elem1.link.prev == &list2);
	assert(elem2.link.next == &elem3.link && elem2.link.prev == &elem1.link);
	assert(elem3.link.next == &elem4.link && elem3.link.prev == &elem2.link);
	assert(elem4.link.next == &list2 && elem4.link.prev == &elem3.link);
}

int main(int argc, char *argv[]) {
	(void)argc;
	(void)argv;

	test_linked_list_init();
	test_linked_list_single_insert();
	test_linked_list_single_remove();
	test_linked_list_alternate_remove();
	test_linked_list_sequential_remove();
	test_linked_list_insert_after();
	test_linked_list_remove_loop();
	test_linked_list_manual_iterate();
	test_linked_list_loop_iterate();
	test_linked_list_take_empty();
	test_linked_list_take_single();
	test_linked_list_take_many();
	test_linked_list_take_concat();

	return 0;
}