diff --git a/lib/card.c b/lib/card.c index 73c02ec..e44b39d 100644 --- a/lib/card.c +++ b/lib/card.c @@ -25,6 +25,20 @@ void initialize_card(struct card *card) { return; } +struct card *duplicate_card(struct card *card) { + struct card *new_card; + + allocate_card(&new_card); + set_card(new_card, + card->value, + card->suit, + card->face, + card->frame->start_y, + card->frame->start_x); + + return(new_card); +} + void free_card(struct card *card) { if (card) { free_frame(card->frame); diff --git a/lib/card.h b/lib/card.h index 766e3ad..996a5cf 100644 --- a/lib/card.h +++ b/lib/card.h @@ -45,6 +45,7 @@ extern const char *program_name; void allocate_card(struct card **); void initialize_card(struct card *); +struct card *duplicate_card(struct card *); void free_card(struct card *); void set_card(struct card *, enum value, enum suit, enum face, int, int); void expose_card(struct card *); diff --git a/lib/frame.c b/lib/frame.c index 06dbde2..a1ea4fb 100644 --- a/lib/frame.c +++ b/lib/frame.c @@ -22,6 +22,15 @@ void initialize_frame(struct frame *frame) { return; } +struct frame *duplicate_frame(struct frame *frame) { + struct frame *new_frame; + + allocate_frame(&new_frame); + set_frame(new_frame, frame->start_y, frame->start_x); + + return(new_frame); +} + void free_frame(struct frame *frame) { if (frame) { delwin(frame->shape); diff --git a/lib/frame.h b/lib/frame.h index 04baebb..1b4ad04 100644 --- a/lib/frame.h +++ b/lib/frame.h @@ -16,6 +16,7 @@ extern const char *program_name; void allocate_frame(struct frame **); void initialize_frame(struct frame *); +struct frame *duplicate_frame(struct frame *); void free_frame(struct frame *); void set_frame(struct frame *, int, int); diff --git a/lib/stack.c b/lib/stack.c index 0688b5f..41b1ab9 100644 --- a/lib/stack.c +++ b/lib/stack.c @@ -24,6 +24,25 @@ void initialize_stack(struct stack *stack) { return; } +struct stack *duplicate_stack(struct stack *stack) { + struct stack *iterator = stack; + struct stack *tmp_stack, *new_stack; + + allocate_stack(&new_stack); + allocate_stack(&tmp_stack); + initialize_stack(new_stack); + initialize_stack(tmp_stack); + for (iterator = stack; iterator; iterator = iterator->next) { + push(&tmp_stack, duplicate_card(iterator->card)); + } + while (!empty(tmp_stack)) { + push(&new_stack, (pop(&tmp_stack))->card); + } + free_stack(tmp_stack); + + return(new_stack); +} + void free_stack(struct stack *stack) { struct stack *tmp_stack; diff --git a/lib/stack.h b/lib/stack.h index 89372f4..fe5b655 100644 --- a/lib/stack.h +++ b/lib/stack.h @@ -12,6 +12,7 @@ extern const char *program_name; void allocate_stack(struct stack **); void initialize_stack(struct stack *); +struct stack *duplicate_stack(struct stack *); void free_stack(struct stack *); bool empty(struct stack *); int length(struct stack *); diff --git a/test/card_test.c b/test/card_test.c index 41d29e7..38235d7 100644 --- a/test/card_test.c +++ b/test/card_test.c @@ -16,6 +16,20 @@ void test_initialize_card() { return; } +void test_duplicate_card() { + struct card *card_0, *card_1; + const int start_y = 5, start_x = 10; + + allocate_card(&card_0); + set_card(card_0, ACE, SPADES, EXPOSED, start_y, start_x); + card_1 = duplicate_card(card_0); + + assert(card_0 != card_1); + assert(cards_equal(card_0, card_1)); + + return; +} + void test_set_card() { struct card *card; int start_y = 5; @@ -66,6 +80,7 @@ void test_cover_card() { void test_card() { test_initialize_card(); + test_duplicate_card(); test_set_card(); test_expose_card(); test_cover_card(); diff --git a/test/frame_test.c b/test/frame_test.c index ec2d0ed..53b9b6b 100644 --- a/test/frame_test.c +++ b/test/frame_test.c @@ -16,6 +16,20 @@ void test_initialize_frame() { return; } +void test_duplicate_frame() { + struct frame *frame_0, *frame_1; + const int start_y = 5, start_x = 10; + + allocate_frame(&frame_0); + set_frame(frame_0, start_y, start_x); + frame_1 = duplicate_frame(frame_0); + + assert(frame_0 != frame_1); + assert(frames_equal(frame_0, frame_1)); + + return; +} + void test_set_frame() { struct frame *frame; int start_y = 5; @@ -35,6 +49,7 @@ void test_set_frame() { void test_frame() { test_initialize_frame(); + test_duplicate_frame(); test_set_frame(); return; diff --git a/test/stack_test.c b/test/stack_test.c index d61d94f..a8eb6d6 100644 --- a/test/stack_test.c +++ b/test/stack_test.c @@ -16,6 +16,26 @@ void test_initialize_stack() { return; } +void test_duplicate_stack() { + struct stack *stack_0, *stack_1; + struct card *card[5]; + const int start_y = 5, start_x = 10; + + allocate_stack(&stack_0); + initialize_stack(stack_0); + for (int i = 0; i < 5; i++) { + allocate_card(&card[i]); + set_card(card[i], i, SPADES, EXPOSED, start_y, start_x); + push(&stack_0, card[i]); + } + stack_1 = duplicate_stack(stack_0); + + assert(stack_0 != stack_1); + assert(stacks_equal(stack_0, stack_1)); + + return; +} + void test_empty_on_empty_stack() { struct stack *stack; @@ -300,6 +320,8 @@ void test_reverse_should_not_change_stack() { void test_stack() { test_initialize_stack(); + test_duplicate_stack(); + test_empty_on_empty_stack(); test_empty_on_non_empty_stack(); diff --git a/test/test_helper.c b/test/test_helper.c index 71960bc..62a22dc 100644 --- a/test/test_helper.c +++ b/test/test_helper.c @@ -34,45 +34,3 @@ bool stacks_equal(struct stack *stack_0, struct stack *stack_1) { return(true); } } - -struct frame *duplicate_frame(struct frame *frame) { - struct frame *new_frame; - - allocate_frame(&new_frame); - set_frame(new_frame, frame->start_y, frame->start_x); - - return(new_frame); -} - -struct card *duplicate_card(struct card *card) { - struct card *new_card; - - allocate_card(&new_card); - set_card(new_card, - card->value, - card->suit, - card->face, - card->frame->start_y, - card->frame->start_x); - - return(new_card); -} - -struct stack *duplicate_stack(struct stack *stack) { - struct stack *iterator = stack; - struct stack *tmp_stack, *new_stack; - - allocate_stack(&new_stack); - allocate_stack(&tmp_stack); - initialize_stack(new_stack); - initialize_stack(tmp_stack); - for (iterator = stack; iterator; iterator = iterator->next) { - push(&tmp_stack, duplicate_card(iterator->card)); - } - while (!empty(tmp_stack)) { - push(&new_stack, (pop(&tmp_stack))->card); - } - free_stack(tmp_stack); - - return(new_stack); -} diff --git a/test/test_helper.h b/test/test_helper.h index ca83500..354a56f 100644 --- a/test/test_helper.h +++ b/test/test_helper.h @@ -9,8 +9,5 @@ bool frames_equal(struct frame *, struct frame *); bool cards_equal(struct card *, struct card *); bool stacks_equal(struct stack *, struct stack *); -struct frame *duplicate_frame(struct frame *); -struct card *duplicate_card(struct card *); -struct stack *duplicate_stack(struct stack *); #endif diff --git a/test/test_helper_test.c b/test/test_helper_test.c index 085f235..a3a0493 100644 --- a/test/test_helper_test.c +++ b/test/test_helper_test.c @@ -145,55 +145,6 @@ void test_stacks_equal_with_two_stack_pointers_to_the_same_address() { return; } -void test_duplicate_frame() { - struct frame *frame_0, *frame_1; - const int start_y = 5, start_x = 10; - - allocate_frame(&frame_0); - set_frame(frame_0, start_y, start_x); - frame_1 = duplicate_frame(frame_0); - - assert(frame_0 != frame_1); - assert(frames_equal(frame_0, frame_1)); - - return; -} - -void test_duplicate_card() { - struct card *card_0, *card_1; - const int start_y = 5, start_x = 10; - - allocate_card(&card_0); - set_card(card_0, ACE, SPADES, EXPOSED, start_y, start_x); - card_1 = duplicate_card(card_0); - - assert(card_0 != card_1); - assert(cards_equal(card_0, card_1)); - - return; -} - -void test_duplicate_stack() { - struct stack *stack_0, *stack_1; - struct card *card[5]; - const int start_y = 5, start_x = 10; - - allocate_stack(&stack_0); - initialize_stack(stack_0); - for (int i = 0; i < 5; i++) { - allocate_card(&card[i]); - set_card(card[i], i, SPADES, EXPOSED, start_y, start_x); - push(&stack_0, card[i]); - } - stack_1 = duplicate_stack(stack_0); - - assert(stack_0 != stack_1); - assert(stacks_equal(stack_0, stack_1)); - - return; -} - - void test_test_helper() { test_frames_equal_with_two_nulls(); test_frames_equal_with_one_null(); @@ -210,9 +161,5 @@ void test_test_helper() { test_stacks_equal_with_two_different_stacks(); test_stacks_equal_with_two_stack_pointers_to_the_same_address(); - test_duplicate_frame(); - test_duplicate_card(); - test_duplicate_stack(); - return; }