From 0a37b5982afdf531406ac4a5111a85692d4e3e50 Mon Sep 17 00:00:00 2001 From: Murilo Pereira Date: Sun, 1 May 2011 02:04:16 -0300 Subject: [PATCH] Really, don't change the stack when calling stack#reverse. --- lib/stack.c | 6 +++--- test/stack_test.c | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/stack.c b/lib/stack.c index b74a3ce..0688b5f 100644 --- a/lib/stack.c +++ b/lib/stack.c @@ -97,12 +97,12 @@ struct stack *pop(struct stack **stack) { struct stack *reverse(struct stack *stack) { if (length(stack) > 1) { - struct stack *tmp_stack; + struct stack *tmp_stack, *iterator; allocate_stack(&tmp_stack); initialize_stack(tmp_stack); - while (!empty(stack)) { - push(&tmp_stack, pop((&stack))->card); + for (iterator = stack; iterator; iterator = iterator->next) { + push(&tmp_stack, iterator->card); } return(tmp_stack); } else { diff --git a/test/stack_test.c b/test/stack_test.c index 3d31ee4..d61d94f 100644 --- a/test/stack_test.c +++ b/test/stack_test.c @@ -276,7 +276,7 @@ void test_reverse_on_stack_with_more_than_one_element() { } void test_reverse_should_not_change_stack() { - struct stack *stack, *old_stack; + struct stack *stack, *old_stack, *old_stack_address; struct card *card[3]; allocate_stack(&stack); @@ -287,10 +287,12 @@ void test_reverse_should_not_change_stack() { set_card(card[i], TWO + i, DIAMONDS + i, EXPOSED, 0, 0); push(&stack, card[i]); } - old_stack = stack; + old_stack_address = stack; + old_stack = duplicate_stack(stack); reverse(stack); - assert(stack == old_stack); + assert(stack == old_stack_address); + assert(stacks_equal(stack, old_stack)); free_stack(stack); }