Move dup functions to lib.

This commit is contained in:
Murilo Pereira 2011-05-01 03:06:43 -03:00
parent 20d68a6f3d
commit 5d96d20893
12 changed files with 97 additions and 98 deletions

View File

@ -25,6 +25,20 @@ void initialize_card(struct card *card) {
return; 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) { void free_card(struct card *card) {
if (card) { if (card) {
free_frame(card->frame); free_frame(card->frame);

View File

@ -45,6 +45,7 @@ extern const char *program_name;
void allocate_card(struct card **); void allocate_card(struct card **);
void initialize_card(struct card *); void initialize_card(struct card *);
struct card *duplicate_card(struct card *);
void free_card(struct card *); void free_card(struct card *);
void set_card(struct card *, enum value, enum suit, enum face, int, int); void set_card(struct card *, enum value, enum suit, enum face, int, int);
void expose_card(struct card *); void expose_card(struct card *);

View File

@ -22,6 +22,15 @@ void initialize_frame(struct frame *frame) {
return; 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) { void free_frame(struct frame *frame) {
if (frame) { if (frame) {
delwin(frame->shape); delwin(frame->shape);

View File

@ -16,6 +16,7 @@ extern const char *program_name;
void allocate_frame(struct frame **); void allocate_frame(struct frame **);
void initialize_frame(struct frame *); void initialize_frame(struct frame *);
struct frame *duplicate_frame(struct frame *);
void free_frame(struct frame *); void free_frame(struct frame *);
void set_frame(struct frame *, int, int); void set_frame(struct frame *, int, int);

View File

@ -24,6 +24,25 @@ void initialize_stack(struct stack *stack) {
return; 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) { void free_stack(struct stack *stack) {
struct stack *tmp_stack; struct stack *tmp_stack;

View File

@ -12,6 +12,7 @@ extern const char *program_name;
void allocate_stack(struct stack **); void allocate_stack(struct stack **);
void initialize_stack(struct stack *); void initialize_stack(struct stack *);
struct stack *duplicate_stack(struct stack *);
void free_stack(struct stack *); void free_stack(struct stack *);
bool empty(struct stack *); bool empty(struct stack *);
int length(struct stack *); int length(struct stack *);

View File

@ -16,6 +16,20 @@ void test_initialize_card() {
return; 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() { void test_set_card() {
struct card *card; struct card *card;
int start_y = 5; int start_y = 5;
@ -66,6 +80,7 @@ void test_cover_card() {
void test_card() { void test_card() {
test_initialize_card(); test_initialize_card();
test_duplicate_card();
test_set_card(); test_set_card();
test_expose_card(); test_expose_card();
test_cover_card(); test_cover_card();

View File

@ -16,6 +16,20 @@ void test_initialize_frame() {
return; 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() { void test_set_frame() {
struct frame *frame; struct frame *frame;
int start_y = 5; int start_y = 5;
@ -35,6 +49,7 @@ void test_set_frame() {
void test_frame() { void test_frame() {
test_initialize_frame(); test_initialize_frame();
test_duplicate_frame();
test_set_frame(); test_set_frame();
return; return;

View File

@ -16,6 +16,26 @@ void test_initialize_stack() {
return; 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() { void test_empty_on_empty_stack() {
struct stack *stack; struct stack *stack;
@ -300,6 +320,8 @@ void test_reverse_should_not_change_stack() {
void test_stack() { void test_stack() {
test_initialize_stack(); test_initialize_stack();
test_duplicate_stack();
test_empty_on_empty_stack(); test_empty_on_empty_stack();
test_empty_on_non_empty_stack(); test_empty_on_non_empty_stack();

View File

@ -34,45 +34,3 @@ bool stacks_equal(struct stack *stack_0, struct stack *stack_1) {
return(true); 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);
}

View File

@ -9,8 +9,5 @@
bool frames_equal(struct frame *, struct frame *); bool frames_equal(struct frame *, struct frame *);
bool cards_equal(struct card *, struct card *); bool cards_equal(struct card *, struct card *);
bool stacks_equal(struct stack *, struct stack *); 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 #endif

View File

@ -145,55 +145,6 @@ void test_stacks_equal_with_two_stack_pointers_to_the_same_address() {
return; 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() { void test_test_helper() {
test_frames_equal_with_two_nulls(); test_frames_equal_with_two_nulls();
test_frames_equal_with_one_null(); 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_different_stacks();
test_stacks_equal_with_two_stack_pointers_to_the_same_address(); test_stacks_equal_with_two_stack_pointers_to_the_same_address();
test_duplicate_frame();
test_duplicate_card();
test_duplicate_stack();
return; return;
} }