Skip to content

Commit

Permalink
Fixed List_pop() head/tail NULL on len of 0
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Aug 23, 2010
1 parent 4af26b3 commit f588500
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f588500

Please sign in to comment.