aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/list.h6
-rw-r--r--tests/test_list.c44
2 files changed, 47 insertions, 3 deletions
diff --git a/src/list.h b/src/list.h
index e20771f..a2453b6 100644
--- a/src/list.h
+++ b/src/list.h
@@ -45,7 +45,7 @@ LIST_DEF(Int);
self->vhead->next = self->vtail; \
self->vhead->prev = NULL; \
self->vtail->next = NULL; \
- self->vhead->prev = self->vhead; \
+ self->vtail->prev = self->vhead; \
self->len = 0; \
} \
void T##List_free(T##List *self) { \
@@ -118,10 +118,10 @@ LIST_DEF(Int);
return self->len; \
} \
T##ListIter T##List_push_back(T##List *self, T val) { \
- T##List_insert_before(self, self->vtail, val); \
+ return T##List_insert_before(self, self->vtail, val); \
} \
T##ListIter T##List_push_front(T##List *self, T val) { \
- T##List_insert_after(self, self->vhead, val); \
+ return T##List_insert_after(self, self->vhead, val); \
} \
void T##List_pop_back(T##List *self) { \
T##List_remove(self, self->vtail->prev); \
diff --git a/tests/test_list.c b/tests/test_list.c
new file mode 100644
index 0000000..e9ff5e8
--- /dev/null
+++ b/tests/test_list.c
@@ -0,0 +1,44 @@
+#include "list.h"
+
+#include <assert.h>
+#include <stdio.h>
+
+int main() {
+ printf("[TEST] list\n");
+
+ IntList lst;
+ IntList_init(&lst);
+
+ IntList_push_back(&lst, 3);
+ IntList_push_front(&lst, 1);
+ IntList_insert_after(&lst, IntList_begin(&lst), 2);
+
+ assert(lst.vhead->next->val == 1);
+ assert(lst.vhead->next->next->val == 2);
+ assert(lst.vhead->next->next->next->val == 3);
+
+ assert(lst.vtail->prev->val == 3);
+ assert(lst.vtail->prev->prev->val == 2);
+ assert(lst.vtail->prev->prev->prev->val == 1);
+
+ assert(IntList_len(&lst) == 3);
+ assert(IntList_last(&lst)->val == 3);
+ assert(IntList_begin(&lst)->val == 1);
+ assert(IntList_end(&lst) == lst.vtail);
+
+ IntList_pop_back(&lst);
+ assert(lst.vhead->next->val == 1);
+ assert(lst.vhead->next->next->val == 2);
+ assert(lst.vtail->prev->val == 2);
+ assert(lst.vtail->prev->prev->val == 1);
+
+ IntList_pop_front(&lst);
+ assert(lst.vhead->next->val == 2);
+ assert(lst.vtail->prev->val == 2);
+
+ assert(IntList_len(&lst) == 1);
+
+ IntList_free(&lst);
+
+ printf("[PASS] list\n");
+}