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) {
|
struct stack *reverse(struct stack *stack) {
|
||||||
if (length(stack) > 1) {
|
if (length(stack) > 1) {
|
||||||
struct stack *tmp_stack;
|
struct stack *tmp_stack, *iterator;
|
||||||
|
|
||||||
allocate_stack(&tmp_stack);
|
allocate_stack(&tmp_stack);
|
||||||
initialize_stack(tmp_stack);
|
initialize_stack(tmp_stack);
|
||||||
while (!empty(stack)) {
|
for (iterator = stack; iterator; iterator = iterator->next) {
|
||||||
push(&tmp_stack, pop((&stack))->card);
|
push(&tmp_stack, iterator->card);
|
||||||
}
|
}
|
||||||
return(tmp_stack);
|
return(tmp_stack);
|
||||||
} else {
|
} else {
|
||||||
|
@ -276,7 +276,7 @@ void test_reverse_on_stack_with_more_than_one_element() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void test_reverse_should_not_change_stack() {
|
void test_reverse_should_not_change_stack() {
|
||||||
struct stack *stack, *old_stack;
|
struct stack *stack, *old_stack, *old_stack_address;
|
||||||
struct card *card[3];
|
struct card *card[3];
|
||||||
|
|
||||||
allocate_stack(&stack);
|
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);
|
set_card(card[i], TWO + i, DIAMONDS + i, EXPOSED, 0, 0);
|
||||||
push(&stack, card[i]);
|
push(&stack, card[i]);
|
||||||
}
|
}
|
||||||
old_stack = stack;
|
old_stack_address = stack;
|
||||||
|
old_stack = duplicate_stack(stack);
|
||||||
reverse(stack);
|
reverse(stack);
|
||||||
|
|
||||||
assert(stack == old_stack);
|
assert(stack == old_stack_address);
|
||||||
|
assert(stacks_equal(stack, old_stack));
|
||||||
|
|
||||||
free_stack(stack);
|
free_stack(stack);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user