diff --git a/lib/stack.c b/lib/stack.c index 9b97904..b74a3ce 100644 --- a/lib/stack.c +++ b/lib/stack.c @@ -95,17 +95,17 @@ struct stack *pop(struct stack **stack) { return(popped_entry); } -struct stack *reverse(struct stack **stack) { - if (length(*stack) > 1) { +struct stack *reverse(struct stack *stack) { + if (length(stack) > 1) { struct stack *tmp_stack; allocate_stack(&tmp_stack); initialize_stack(tmp_stack); - while (!empty(*stack)) { - push(&tmp_stack, pop((stack))->card); + while (!empty(stack)) { + push(&tmp_stack, pop((&stack))->card); } return(tmp_stack); } else { - return(*stack); + return(stack); } } diff --git a/lib/stack.h b/lib/stack.h index f1b8897..89372f4 100644 --- a/lib/stack.h +++ b/lib/stack.h @@ -17,6 +17,6 @@ bool empty(struct stack *); int length(struct stack *); void push(struct stack **, struct card *); struct stack *pop(struct stack **); -struct stack *reverse(struct stack **); +struct stack *reverse(struct stack *); #endif diff --git a/test/stack_test.c b/test/stack_test.c index 1d3e178..3d31ee4 100644 --- a/test/stack_test.c +++ b/test/stack_test.c @@ -222,7 +222,7 @@ void test_reverse_on_empty_stack() { allocate_stack(&stack); initialize_stack(stack); old_stack = stack; - reversed_stack = reverse(&stack); + reversed_stack = reverse(stack); assert(reversed_stack == old_stack); @@ -241,7 +241,7 @@ void test_reverse_on_stack_with_one_element() { initialize_stack(stack); push(&stack, card); old_stack = stack; - reversed_stack = reverse(&stack); + reversed_stack = reverse(stack); assert(reversed_stack == old_stack); @@ -261,7 +261,7 @@ void test_reverse_on_stack_with_more_than_one_element() { push(&stack, card[i]); } old_stack = duplicate_stack(stack); - reversed_stack = reverse(&stack); + reversed_stack = reverse(stack); allocate_stack(&unreversed_stack); initialize_stack(unreversed_stack); @@ -275,6 +275,26 @@ void test_reverse_on_stack_with_more_than_one_element() { free_stack(stack); } +void test_reverse_should_not_change_stack() { + struct stack *stack, *old_stack; + struct card *card[3]; + + allocate_stack(&stack); + initialize_stack(stack); + for (int i = 0; i < 3; i++) { + allocate_card(&card[i]); + initialize_card(card[i]); + set_card(card[i], TWO + i, DIAMONDS + i, EXPOSED, 0, 0); + push(&stack, card[i]); + } + old_stack = stack; + reverse(stack); + + assert(stack == old_stack); + + free_stack(stack); +} + void test_stack() { test_initialize_stack(); @@ -294,6 +314,7 @@ void test_stack() { test_reverse_on_empty_stack(); test_reverse_on_stack_with_one_element(); test_reverse_on_stack_with_more_than_one_element(); + test_reverse_should_not_change_stack(); return; }