Really, don't change the stack when calling stack#reverse.
This commit is contained in:
parent
0e0c44f5c6
commit
0a37b5982a
@ -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 {
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user