Permalink
Browse files

Fixed List_pop() head/tail NULL on len of 0

  • Loading branch information...
tj committed Aug 23, 2010
1 parent 4af26b3 commit f5885009e7942408c83a65a9daead97dfcbdd709
Showing with 9 additions and 2 deletions.
  1. +5 −2 src/list.c
  2. +4 −0 test.c
View
@@ -70,9 +70,12 @@ List_push(List *self, ListNode *node) {
ListNode *
List_pop(List *self) {
if (!self->len) return NULL;
- --self->len;
ListNode *node = self->tail;
- self->tail = node->prev;
+ if (--self->len) {
+ self->tail = node->prev;
+ } else {
+ self->tail = self->head = NULL;
+ }
node->next = node->prev = NULL;
return node;
}
View
4 test.c
@@ -221,9 +221,13 @@ test_List_pop() {
assert(b == List_pop(list));
assert(1 == list->len);
+ assert(a == list->head);
+ assert(a == list->tail);
assert(a == List_pop(list));
assert(0 == list->len);
+ assert(NULL == list->head);
+ assert(NULL == list->tail);
assert(NULL == List_pop(list));
assert(0 == list->len);

0 comments on commit f588500

Please sign in to comment.