From 5a62ecb95566daa42bb12eaa4efabcc0e347e5ff Mon Sep 17 00:00:00 2001 From: Murilo Pereira Date: Sat, 30 Apr 2011 22:13:41 -0300 Subject: [PATCH] Added stack#reverse. --- lib/stack.c | 15 +++++++++++ lib/stack.h | 1 + test/stack_test.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/lib/stack.c b/lib/stack.c index b033f8d..9b97904 100644 --- a/lib/stack.c +++ b/lib/stack.c @@ -94,3 +94,18 @@ struct stack *pop(struct stack **stack) { return(popped_entry); } + +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); + } + return(tmp_stack); + } else { + return(*stack); + } +} diff --git a/lib/stack.h b/lib/stack.h index 6ca832f..f1b8897 100644 --- a/lib/stack.h +++ b/lib/stack.h @@ -17,5 +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 **); #endif diff --git a/test/stack_test.c b/test/stack_test.c index c4d5f9e..1d3e178 100644 --- a/test/stack_test.c +++ b/test/stack_test.c @@ -1,4 +1,5 @@ #include +#include "test_helper.h" #include "../lib/stack.h" void test_initialize_stack() { @@ -215,6 +216,65 @@ void test_pop_on_stack_with_more_than_one_element() { return; } +void test_reverse_on_empty_stack() { + struct stack *stack, *old_stack, *reversed_stack; + + allocate_stack(&stack); + initialize_stack(stack); + old_stack = stack; + reversed_stack = reverse(&stack); + + assert(reversed_stack == old_stack); + + free_stack(stack); +} + +void test_reverse_on_stack_with_one_element() { + struct stack *stack, *old_stack, *reversed_stack; + struct card *card; + + allocate_card(&card); + initialize_card(card); + set_card(card, ACE, SPADES, EXPOSED, 0, 0); + + allocate_stack(&stack); + initialize_stack(stack); + push(&stack, card); + old_stack = stack; + reversed_stack = reverse(&stack); + + assert(reversed_stack == old_stack); + + free_stack(stack); +} + +void test_reverse_on_stack_with_more_than_one_element() { + struct stack *stack, *old_stack, *reversed_stack, *unreversed_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 = duplicate_stack(stack); + reversed_stack = reverse(&stack); + + allocate_stack(&unreversed_stack); + initialize_stack(unreversed_stack); + for (int i = 0; i < 3; i++) { + push(&unreversed_stack, pop(&reversed_stack)->card); + } + + assert(stacks_equal(unreversed_stack, old_stack)); + + free_stack(reversed_stack); + free_stack(stack); +} + void test_stack() { test_initialize_stack(); @@ -231,5 +291,9 @@ void test_stack() { test_pop_on_stack_with_one_element(); test_pop_on_stack_with_more_than_one_element(); + test_reverse_on_empty_stack(); + test_reverse_on_stack_with_one_element(); + test_reverse_on_stack_with_more_than_one_element(); + return; }