From c6ac149dde4a9d614d5dd0a0558ed07094c68b1e Mon Sep 17 00:00:00 2001 From: Murilo Pereira Date: Sun, 13 Feb 2011 23:43:58 -0200 Subject: [PATCH] 'duplicate_stack(stack)' was returning reversed stacks. --- test/test_helper.c | 10 ++++++++-- test/test_helper_test.c | 10 ++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/test/test_helper.c b/test/test_helper.c index 072b9c2..8681372 100644 --- a/test/test_helper.c +++ b/test/test_helper.c @@ -60,13 +60,19 @@ struct card *duplicate_card(struct card *card) { struct stack *duplicate_stack(struct stack *stack) { struct stack *iterator = stack; - struct stack *new_stack; + struct stack *tmp_stack, *new_stack; allocate_stack(&new_stack); + allocate_stack(&tmp_stack); initialize_stack(new_stack); + initialize_stack(tmp_stack); 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); } diff --git a/test/test_helper_test.c b/test/test_helper_test.c index 3cfc02a..085f235 100644 --- a/test/test_helper_test.c +++ b/test/test_helper_test.c @@ -175,14 +175,16 @@ void test_duplicate_card() { void test_duplicate_stack() { struct stack *stack_0, *stack_1; - struct card *card; + struct card *card[5]; 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); 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); assert(stack_0 != stack_1);