stack#push and stack#pop less destructive.
This commit is contained in:
parent
5d96d20893
commit
854184c2fd
27
lib/stack.c
27
lib/stack.c
@ -81,10 +81,10 @@ void push(struct stack **stack, struct card *card) {
|
|||||||
|
|
||||||
if (card) {
|
if (card) {
|
||||||
if (empty(*stack)) {
|
if (empty(*stack)) {
|
||||||
(*stack)->card = card;
|
(*stack)->card = duplicate_card(card);
|
||||||
} else {
|
} else {
|
||||||
allocate_stack(&new_stack);
|
allocate_stack(&new_stack);
|
||||||
new_stack->card = card;
|
new_stack->card = duplicate_card(card);
|
||||||
new_stack->next = (*stack);
|
new_stack->next = (*stack);
|
||||||
*stack = new_stack;
|
*stack = new_stack;
|
||||||
}
|
}
|
||||||
@ -95,20 +95,23 @@ struct stack *pop(struct stack **stack) {
|
|||||||
struct stack *popped_entry = NULL;
|
struct stack *popped_entry = NULL;
|
||||||
|
|
||||||
if(!empty(*stack)) {
|
if(!empty(*stack)) {
|
||||||
popped_entry = *stack;
|
allocate_stack(&popped_entry);
|
||||||
/* As what's considered an empty stack is an allocated and initialized
|
initialize_stack(popped_entry);
|
||||||
* stack structure, we make sure pop doesn't make '*stack' point to NULL
|
popped_entry->card = duplicate_card((*stack)->card);
|
||||||
* when popping a stack with only 1 element. */
|
popped_entry->next = NULL;
|
||||||
if (length(*stack) == 1) {
|
if (length(*stack) == 1) {
|
||||||
allocate_stack(stack);
|
/* An empty stack is a stack with a blank top card
|
||||||
initialize_stack(*stack);
|
* and with stack->next == NULL. */
|
||||||
set_frame((*stack)->card->frame,
|
set_card((*stack)->card,
|
||||||
(*stack)->card->frame->start_y,
|
NO_VALUE,
|
||||||
(*stack)->card->frame->start_x);
|
NO_SUIT,
|
||||||
|
NO_FACE,
|
||||||
|
(*stack)->card->frame->start_y,
|
||||||
|
(*stack)->card->frame->start_x);
|
||||||
|
(*stack)->next = NULL;
|
||||||
} else {
|
} else {
|
||||||
*stack = (*stack)->next;
|
*stack = (*stack)->next;
|
||||||
}
|
}
|
||||||
popped_entry->next = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return(popped_entry);
|
return(popped_entry);
|
||||||
|
@ -102,7 +102,7 @@ void test_push_on_empty_stack() {
|
|||||||
initialize_stack(stack);
|
initialize_stack(stack);
|
||||||
push(&stack, card);
|
push(&stack, card);
|
||||||
|
|
||||||
assert(stack->card == card);
|
assert(cards_equal(stack->card, card));
|
||||||
assert(!stack->next);
|
assert(!stack->next);
|
||||||
|
|
||||||
free_stack(stack);
|
free_stack(stack);
|
||||||
@ -126,8 +126,8 @@ void test_push_on_non_empty_stack() {
|
|||||||
push(&stack, card_0);
|
push(&stack, card_0);
|
||||||
push(&stack, card_1);
|
push(&stack, card_1);
|
||||||
|
|
||||||
assert(stack->card == card_1);
|
assert(cards_equal(stack->card, card_1));
|
||||||
assert(stack->next->card == card_0);
|
assert(cards_equal(stack->next->card, card_0));
|
||||||
assert(!stack->next->next);
|
assert(!stack->next->next);
|
||||||
|
|
||||||
free_stack(stack);
|
free_stack(stack);
|
||||||
@ -143,8 +143,8 @@ void test_push_null_on_empty_stack() {
|
|||||||
old_stack = stack;
|
old_stack = stack;
|
||||||
push(&stack, NULL);
|
push(&stack, NULL);
|
||||||
|
|
||||||
assert(stack->card == old_stack->card);
|
assert(cards_equal(stack->card, old_stack->card));
|
||||||
assert(stack->next == NULL);
|
assert(!stack->next);
|
||||||
|
|
||||||
free_stack(stack);
|
free_stack(stack);
|
||||||
|
|
||||||
@ -161,11 +161,11 @@ void test_push_null_on_non_empty_stack() {
|
|||||||
|
|
||||||
allocate_stack(&stack);
|
allocate_stack(&stack);
|
||||||
initialize_stack(stack);
|
initialize_stack(stack);
|
||||||
old_stack = stack;
|
old_stack = duplicate_stack(stack);
|
||||||
push(&stack, NULL);
|
push(&stack, NULL);
|
||||||
|
|
||||||
assert(stack->card == old_stack->card);
|
assert(cards_equal(stack->card, old_stack->card));
|
||||||
assert(stack->next == old_stack->next);
|
assert(stacks_equal(stack->next, old_stack->next));
|
||||||
|
|
||||||
free_stack(stack);
|
free_stack(stack);
|
||||||
|
|
||||||
@ -203,7 +203,7 @@ void test_pop_on_stack_with_one_element() {
|
|||||||
popped_entry = pop(&stack);
|
popped_entry = pop(&stack);
|
||||||
|
|
||||||
assert(empty(stack));
|
assert(empty(stack));
|
||||||
assert(popped_entry->card == card);
|
assert(cards_equal(popped_entry->card, card));
|
||||||
assert(!popped_entry->next);
|
assert(!popped_entry->next);
|
||||||
|
|
||||||
free_stack(stack);
|
free_stack(stack);
|
||||||
@ -228,7 +228,7 @@ void test_pop_on_stack_with_more_than_one_element() {
|
|||||||
|
|
||||||
assert(length(stack) == 2);
|
assert(length(stack) == 2);
|
||||||
assert(stack == old_stack_next);
|
assert(stack == old_stack_next);
|
||||||
assert(popped_entry->card == card[2]);
|
assert(cards_equal(popped_entry->card, card[2]));
|
||||||
assert(!popped_entry->next);
|
assert(!popped_entry->next);
|
||||||
|
|
||||||
free_stack(stack);
|
free_stack(stack);
|
||||||
|
Loading…
Reference in New Issue
Block a user