stack#push and stack#pop less destructive.

This commit is contained in:
Murilo Pereira 2011-05-01 05:36:46 -03:00
parent 5d96d20893
commit 854184c2fd
2 changed files with 25 additions and 22 deletions

View File

@ -81,10 +81,10 @@ void push(struct stack **stack, struct card *card) {
if (card) {
if (empty(*stack)) {
(*stack)->card = card;
(*stack)->card = duplicate_card(card);
} else {
allocate_stack(&new_stack);
new_stack->card = card;
new_stack->card = duplicate_card(card);
new_stack->next = (*stack);
*stack = new_stack;
}
@ -95,20 +95,23 @@ struct stack *pop(struct stack **stack) {
struct stack *popped_entry = NULL;
if(!empty(*stack)) {
popped_entry = *stack;
/* As what's considered an empty stack is an allocated and initialized
* stack structure, we make sure pop doesn't make '*stack' point to NULL
* when popping a stack with only 1 element. */
allocate_stack(&popped_entry);
initialize_stack(popped_entry);
popped_entry->card = duplicate_card((*stack)->card);
popped_entry->next = NULL;
if (length(*stack) == 1) {
allocate_stack(stack);
initialize_stack(*stack);
set_frame((*stack)->card->frame,
(*stack)->card->frame->start_y,
(*stack)->card->frame->start_x);
/* An empty stack is a stack with a blank top card
* and with stack->next == NULL. */
set_card((*stack)->card,
NO_VALUE,
NO_SUIT,
NO_FACE,
(*stack)->card->frame->start_y,
(*stack)->card->frame->start_x);
(*stack)->next = NULL;
} else {
*stack = (*stack)->next;
}
popped_entry->next = NULL;
}
return(popped_entry);

View File

@ -102,7 +102,7 @@ void test_push_on_empty_stack() {
initialize_stack(stack);
push(&stack, card);
assert(stack->card == card);
assert(cards_equal(stack->card, card));
assert(!stack->next);
free_stack(stack);
@ -126,8 +126,8 @@ void test_push_on_non_empty_stack() {
push(&stack, card_0);
push(&stack, card_1);
assert(stack->card == card_1);
assert(stack->next->card == card_0);
assert(cards_equal(stack->card, card_1));
assert(cards_equal(stack->next->card, card_0));
assert(!stack->next->next);
free_stack(stack);
@ -143,8 +143,8 @@ void test_push_null_on_empty_stack() {
old_stack = stack;
push(&stack, NULL);
assert(stack->card == old_stack->card);
assert(stack->next == NULL);
assert(cards_equal(stack->card, old_stack->card));
assert(!stack->next);
free_stack(stack);
@ -161,11 +161,11 @@ void test_push_null_on_non_empty_stack() {
allocate_stack(&stack);
initialize_stack(stack);
old_stack = stack;
old_stack = duplicate_stack(stack);
push(&stack, NULL);
assert(stack->card == old_stack->card);
assert(stack->next == old_stack->next);
assert(cards_equal(stack->card, old_stack->card));
assert(stacks_equal(stack->next, old_stack->next));
free_stack(stack);
@ -203,7 +203,7 @@ void test_pop_on_stack_with_one_element() {
popped_entry = pop(&stack);
assert(empty(stack));
assert(popped_entry->card == card);
assert(cards_equal(popped_entry->card, card));
assert(!popped_entry->next);
free_stack(stack);
@ -228,7 +228,7 @@ void test_pop_on_stack_with_more_than_one_element() {
assert(length(stack) == 2);
assert(stack == old_stack_next);
assert(popped_entry->card == card[2]);
assert(cards_equal(popped_entry->card, card[2]));
assert(!popped_entry->next);
free_stack(stack);