'duplicate_stack(stack)' was returning reversed stacks.

This commit is contained in:
Murilo Pereira 2011-02-13 23:43:58 -02:00
parent 777434177f
commit c6ac149dde
2 changed files with 14 additions and 6 deletions

View File

@ -60,13 +60,19 @@ struct card *duplicate_card(struct card *card) {
struct stack *duplicate_stack(struct stack *stack) { struct stack *duplicate_stack(struct stack *stack) {
struct stack *iterator = stack; struct stack *iterator = stack;
struct stack *new_stack; struct stack *tmp_stack, *new_stack;
allocate_stack(&new_stack); allocate_stack(&new_stack);
allocate_stack(&tmp_stack);
initialize_stack(new_stack); initialize_stack(new_stack);
initialize_stack(tmp_stack);
for (iterator = stack; iterator; iterator = iterator->next) { for (iterator = stack; iterator; iterator = iterator->next) {
push(&new_stack, duplicate_card(iterator->card)); push(&tmp_stack, duplicate_card(iterator->card));
} }
while (!empty(tmp_stack)) {
push(&new_stack, (pop(&tmp_stack))->card);
}
delete_stack(tmp_stack);
return(new_stack); return(new_stack);
} }

View File

@ -175,14 +175,16 @@ void test_duplicate_card() {
void test_duplicate_stack() { void test_duplicate_stack() {
struct stack *stack_0, *stack_1; struct stack *stack_0, *stack_1;
struct card *card; struct card *card[5];
const int start_y = 5, start_x = 10; const int start_y = 5, start_x = 10;
allocate_card(&card);
set_card(card, ACE, SPADES, EXPOSED, start_y, start_x);
allocate_stack(&stack_0); allocate_stack(&stack_0);
initialize_stack(stack_0); initialize_stack(stack_0);
push(&stack_0, card); for (int i = 0; i < 5; i++) {
allocate_card(&card[i]);
set_card(card[i], i, SPADES, EXPOSED, start_y, start_x);
push(&stack_0, card[i]);
}
stack_1 = duplicate_stack(stack_0); stack_1 = duplicate_stack(stack_0);
assert(stack_0 != stack_1); assert(stack_0 != stack_1);