Name-spaced functions for card and stack.
This commit is contained in:
parent
c3f6e16dc8
commit
95a1c77a25
22
src/card.c
22
src/card.c
@ -6,7 +6,7 @@
|
||||
#include "card.h"
|
||||
#include "common.h"
|
||||
|
||||
void allocate_card(struct card **card) {
|
||||
void card_malloc(struct card **card) {
|
||||
if (!(*card = malloc(sizeof(**card)))) {
|
||||
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
||||
exit(errno);
|
||||
@ -14,19 +14,19 @@ void allocate_card(struct card **card) {
|
||||
allocate_frame(&((*card)->frame));
|
||||
}
|
||||
|
||||
void initialize_card(struct card *card) {
|
||||
void card_init(struct card *card) {
|
||||
initialize_frame(card->frame);
|
||||
card->value = NO_VALUE;
|
||||
card->suit = NO_SUIT;
|
||||
card->face = NO_FACE;
|
||||
}
|
||||
|
||||
void free_card(struct card *card) {
|
||||
void card_free(struct card *card) {
|
||||
free_frame(card->frame);
|
||||
free(card);
|
||||
}
|
||||
|
||||
void set_card(struct card *card,
|
||||
void card_set(struct card *card,
|
||||
enum value value,
|
||||
enum suit suit,
|
||||
enum face face,
|
||||
@ -38,27 +38,27 @@ void set_card(struct card *card,
|
||||
card->face = face;
|
||||
}
|
||||
|
||||
void expose_card(struct card *card) {
|
||||
void card_expose(struct card *card) {
|
||||
card->face = EXPOSED;
|
||||
}
|
||||
|
||||
void cover_card(struct card *card) {
|
||||
void card_cover(struct card *card) {
|
||||
card->face = COVERED;
|
||||
}
|
||||
|
||||
void mark_card(struct card *card) {
|
||||
void card_mark(struct card *card) {
|
||||
set_frame(card->frame, card->frame->begin_y + 1, card->frame->begin_x);
|
||||
}
|
||||
|
||||
void unmark_card(struct card *card) {
|
||||
void card_unmark(struct card *card) {
|
||||
set_frame(card->frame, card->frame->begin_y - 1, card->frame->begin_x);
|
||||
}
|
||||
|
||||
struct card *duplicate_card(struct card *card) {
|
||||
struct card *card_dup(struct card *card) {
|
||||
struct card *new_card;
|
||||
|
||||
allocate_card(&new_card);
|
||||
set_card(new_card,
|
||||
card_malloc(&new_card);
|
||||
card_set(new_card,
|
||||
card->value,
|
||||
card->suit,
|
||||
card->face,
|
||||
|
18
src/card.h
18
src/card.h
@ -29,14 +29,14 @@ struct card {
|
||||
enum face face;
|
||||
};
|
||||
|
||||
void allocate_card(struct card **);
|
||||
void initialize_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 *);
|
||||
void cover_card(struct card *);
|
||||
struct card *duplicate_card(struct card *);
|
||||
void mark_card(struct card *);
|
||||
void unmark_card(struct card *);
|
||||
void card_malloc(struct card **);
|
||||
void card_init(struct card *);
|
||||
void card_free(struct card *);
|
||||
void card_set(struct card *, enum value, enum suit, enum face, int, int);
|
||||
void card_expose(struct card *);
|
||||
void card_cover(struct card *);
|
||||
void card_mark(struct card *);
|
||||
void card_unmark(struct card *);
|
||||
struct card *card_dup(struct card *);
|
||||
|
||||
#endif
|
||||
|
14
src/cursor.c
14
src/cursor.c
@ -51,25 +51,25 @@ void move_cursor(struct cursor *cursor, enum movement movement) {
|
||||
if (cursor->y == CURSOR_BEGIN_Y) {
|
||||
switch (cursor->x - 3) {
|
||||
case MANEUVRE_0_BEGIN_X:
|
||||
cursor->y = cursor->y + 7 + length(deck->maneuvre[0]);
|
||||
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[0]);
|
||||
break;
|
||||
case MANEUVRE_1_BEGIN_X:
|
||||
cursor->y = cursor->y + 7 + length(deck->maneuvre[1]);
|
||||
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[1]);
|
||||
break;
|
||||
case MANEUVRE_2_BEGIN_X:
|
||||
cursor->y = cursor->y + 7 + length(deck->maneuvre[2]);
|
||||
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[2]);
|
||||
break;
|
||||
case MANEUVRE_3_BEGIN_X:
|
||||
cursor->y = cursor->y + 7 + length(deck->maneuvre[3]);
|
||||
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[3]);
|
||||
break;
|
||||
case MANEUVRE_4_BEGIN_X:
|
||||
cursor->y = cursor->y + 7 + length(deck->maneuvre[4]);
|
||||
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[4]);
|
||||
break;
|
||||
case MANEUVRE_5_BEGIN_X:
|
||||
cursor->y = cursor->y + 7 + length(deck->maneuvre[5]);
|
||||
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[5]);
|
||||
break;
|
||||
case MANEUVRE_6_BEGIN_X:
|
||||
cursor->y = cursor->y + 7 + length(deck->maneuvre[6]);
|
||||
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[6]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
24
src/deck.c
24
src/deck.c
@ -11,35 +11,35 @@ void allocate_deck(struct deck **deck) {
|
||||
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
||||
exit(errno);
|
||||
}
|
||||
allocate_stack(&((*deck)->stock));
|
||||
allocate_stack(&((*deck)->waste_pile));
|
||||
stack_malloc(&((*deck)->stock));
|
||||
stack_malloc(&((*deck)->waste_pile));
|
||||
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
|
||||
allocate_stack(&((*deck)->foundation[i]));
|
||||
stack_malloc(&((*deck)->foundation[i]));
|
||||
}
|
||||
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
|
||||
allocate_stack(&((*deck)->maneuvre[i]));
|
||||
stack_malloc(&((*deck)->maneuvre[i]));
|
||||
}
|
||||
}
|
||||
|
||||
void initialize_deck(struct deck *deck) {
|
||||
initialize_stack(deck->stock);
|
||||
initialize_stack(deck->waste_pile);
|
||||
stack_init(deck->stock);
|
||||
stack_init(deck->waste_pile);
|
||||
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
|
||||
initialize_stack(deck->foundation[i]);
|
||||
stack_init(deck->foundation[i]);
|
||||
}
|
||||
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
|
||||
initialize_stack(deck->maneuvre[i]);
|
||||
stack_init(deck->maneuvre[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void free_deck(struct deck *deck) {
|
||||
free_stack(deck->stock);
|
||||
free_stack(deck->waste_pile);
|
||||
stack_free(deck->stock);
|
||||
stack_free(deck->waste_pile);
|
||||
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
|
||||
free_stack(deck->foundation[i]);
|
||||
stack_free(deck->foundation[i]);
|
||||
}
|
||||
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
|
||||
free_stack(deck->maneuvre[i]);
|
||||
stack_free(deck->maneuvre[i]);
|
||||
}
|
||||
free(deck);
|
||||
}
|
||||
|
@ -62,16 +62,16 @@ void draw_card(struct card *card) {
|
||||
}
|
||||
|
||||
void draw_stack(struct stack *stack) {
|
||||
if (empty(stack)) {
|
||||
if (stack_empty(stack)) {
|
||||
box(stack->card->frame->window, 0, 0);
|
||||
wrefresh(stack->card->frame->window);
|
||||
} else {
|
||||
if (maneuvre_stack(stack)) {
|
||||
struct stack *reversed_stack = reverse(stack);
|
||||
for (struct stack *i = reversed_stack; i; i = i->next) {
|
||||
struct stack *stack_reversed_stack = stack_reverse(stack);
|
||||
for (struct stack *i = stack_reversed_stack; i; i = i->next) {
|
||||
draw_card(i->card);
|
||||
}
|
||||
free_stack(reversed_stack);
|
||||
stack_free(stack_reversed_stack);
|
||||
} else {
|
||||
draw_card(stack->card);
|
||||
}
|
||||
|
40
src/game.c
40
src/game.c
@ -77,7 +77,7 @@ bool valid_move(struct stack *origin, struct stack *destination) {
|
||||
if (stock_stack(origin) && waste_pile_stack(destination)) {
|
||||
return(true);
|
||||
} else if (foundation_stack(destination)) {
|
||||
if (empty(destination)) {
|
||||
if (stack_empty(destination)) {
|
||||
if (origin->card->value == ACE) {
|
||||
return(true);
|
||||
}
|
||||
@ -86,7 +86,7 @@ bool valid_move(struct stack *origin, struct stack *destination) {
|
||||
return(true);
|
||||
}
|
||||
} else if (maneuvre_stack(destination)) {
|
||||
if (empty(destination)) {
|
||||
if (stack_empty(destination)) {
|
||||
if (origin->card->value == KING) {
|
||||
return(true);
|
||||
}
|
||||
@ -103,31 +103,31 @@ bool valid_move(struct stack *origin, struct stack *destination) {
|
||||
|
||||
void move_card(struct stack **origin, struct stack **destination) {
|
||||
struct card *tmp;
|
||||
if ((tmp = pop(origin))) {
|
||||
if ((tmp = stack_pop(origin))) {
|
||||
int destination_y = (*destination)->card->frame->begin_y;
|
||||
int destination_x = (*destination)->card->frame->begin_x;
|
||||
if (!empty(*destination) && maneuvre_stack(*destination)) {
|
||||
if (!stack_empty(*destination) && maneuvre_stack(*destination)) {
|
||||
destination_y++;
|
||||
}
|
||||
push(destination, tmp);
|
||||
stack_push(destination, tmp);
|
||||
set_frame((*destination)->card->frame, destination_y, destination_x);
|
||||
}
|
||||
}
|
||||
|
||||
void move_block(struct stack **origin, struct stack **destination, int block_size) {
|
||||
struct stack *tmp;
|
||||
allocate_stack(&tmp);
|
||||
initialize_stack(tmp);
|
||||
stack_malloc(&tmp);
|
||||
stack_init(tmp);
|
||||
for (int i = 0; i < block_size; i++) {
|
||||
push(&tmp, pop(origin));
|
||||
stack_push(&tmp, stack_pop(origin));
|
||||
}
|
||||
for (int i = 0; i < block_size; i++) {
|
||||
move_card(&tmp, destination);
|
||||
}
|
||||
if (length(*destination) > 1) {
|
||||
if (stack_length(*destination) > 1) {
|
||||
cursor->y += block_size;
|
||||
}
|
||||
free_stack(tmp);
|
||||
stack_free(tmp);
|
||||
}
|
||||
|
||||
|
||||
@ -137,9 +137,9 @@ static void fill_deck(struct deck *deck) {
|
||||
for (int i = ACE; i <= KING; i++) {
|
||||
for (int j = DIAMONDS; j <= CLUBS; j++) {
|
||||
int index = 4 * (i - ACE) + j;
|
||||
allocate_card(&(card[index]));
|
||||
set_card(card[index], i, j, COVERED, 1, 1);
|
||||
push(&(deck->stock), card[index]);
|
||||
card_malloc(&(card[index]));
|
||||
card_set(card[index], i, j, COVERED, 1, 1);
|
||||
stack_push(&(deck->stock), card[index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -153,7 +153,7 @@ static void shuffle_deck(struct deck *deck) {
|
||||
exit(errno);
|
||||
}
|
||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||
card[i] = pop(&(deck->stock));
|
||||
card[i] = stack_pop(&(deck->stock));
|
||||
}
|
||||
srand(time(NULL));
|
||||
for (int i = 0; i < NUMBER_OF_CARDS - 1; i++) {
|
||||
@ -163,14 +163,14 @@ static void shuffle_deck(struct deck *deck) {
|
||||
*card[random] = tmp;
|
||||
}
|
||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||
push(&(deck->stock), card[i]);
|
||||
stack_push(&(deck->stock), card[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void deal_cards(struct deck *deck) {
|
||||
for (int i = 0; i < 7; i++) {
|
||||
move_card(&(deck->stock), &(deck->maneuvre[i]));
|
||||
expose_card(deck->maneuvre[i]->card);
|
||||
card_expose(deck->maneuvre[i]->card);
|
||||
for (int j = i + 1; j < 7; j++) {
|
||||
move_card(&(deck->stock), &(deck->maneuvre[j]));
|
||||
}
|
||||
@ -207,8 +207,8 @@ void game_end() {
|
||||
}
|
||||
|
||||
bool game_won() {
|
||||
return(length(deck->foundation[0]) == 13 &&
|
||||
length(deck->foundation[1]) == 13 &&
|
||||
length(deck->foundation[2]) == 13 &&
|
||||
length(deck->foundation[3]) == 13);
|
||||
return(stack_length(deck->foundation[0]) == 13 &&
|
||||
stack_length(deck->foundation[1]) == 13 &&
|
||||
stack_length(deck->foundation[2]) == 13 &&
|
||||
stack_length(deck->foundation[3]) == 13);
|
||||
}
|
||||
|
@ -49,9 +49,9 @@ static bool cursor_on_invalid_spot(struct cursor *cursor) {
|
||||
}
|
||||
|
||||
static void handle_stock_event() {
|
||||
if (!empty(deck->stock)) {
|
||||
if (!stack_empty(deck->stock)) {
|
||||
move_card(&(deck->stock), &(deck->waste_pile));
|
||||
expose_card(deck->waste_pile->card);
|
||||
card_expose(deck->waste_pile->card);
|
||||
erase_stack(deck->waste_pile);
|
||||
draw_stack(deck->stock);
|
||||
draw_stack(deck->waste_pile);
|
||||
@ -61,11 +61,11 @@ static void handle_stock_event() {
|
||||
/* FIXME: this function does not work on stacks with no marked cards.
|
||||
* In that case it returns the stack's length. */
|
||||
static int marked_cards_count(struct stack *stack) {
|
||||
if (length(stack) == 1) {
|
||||
if (stack_length(stack) == 1) {
|
||||
if (stack->card->frame->begin_y > MANEUVRE_BEGIN_Y) {
|
||||
return(1);
|
||||
}
|
||||
} else if (length(stack) > 1) {
|
||||
} else if (stack_length(stack) > 1) {
|
||||
for (int marked_cards_count = 0; stack; stack = stack->next) {
|
||||
marked_cards_count++;
|
||||
if (!stack->next || (stack->card->frame->begin_y - stack->next->card->frame->begin_y) > 1) {
|
||||
@ -79,7 +79,7 @@ static int marked_cards_count(struct stack *stack) {
|
||||
static void unmark_cards(struct stack *stack) {
|
||||
int _marked_cards_count = marked_cards_count(stack);
|
||||
for (int i = 0; i < _marked_cards_count; stack = stack->next, i++) {
|
||||
unmark_card(stack->card);
|
||||
card_unmark(stack->card);
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,13 +88,13 @@ static void handle_card_movement(struct cursor *cursor) {
|
||||
struct stack **destination;
|
||||
int option;
|
||||
|
||||
if (cursor_on_invalid_spot(cursor) || empty(*origin)) {
|
||||
if (cursor_on_invalid_spot(cursor) || stack_empty(*origin)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (maneuvre_stack(*origin)) {
|
||||
erase_stack(*origin);
|
||||
mark_card((*origin)->card);
|
||||
card_mark((*origin)->card);
|
||||
draw_stack(*origin);
|
||||
cursor->y++;
|
||||
}
|
||||
@ -134,7 +134,7 @@ static void handle_card_movement(struct cursor *cursor) {
|
||||
if (i->next->card->face == EXPOSED &&
|
||||
(i->card->frame->begin_y - i->next->card->frame->begin_y) > 1) {
|
||||
erase_stack(*origin);
|
||||
mark_card(i->next->card);
|
||||
card_mark(i->next->card);
|
||||
draw_stack(*origin);
|
||||
break;
|
||||
}
|
||||
@ -147,14 +147,14 @@ static void handle_card_movement(struct cursor *cursor) {
|
||||
if (i->next) {
|
||||
if ((i->card->frame->begin_y - i->next->card->frame->begin_y) > 1) {
|
||||
erase_stack(*origin);
|
||||
unmark_card(i->card);
|
||||
card_unmark(i->card);
|
||||
draw_stack(*origin);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (i->card->frame->begin_y == (MANEUVRE_BEGIN_Y + 1)) {
|
||||
erase_stack(*origin);
|
||||
unmark_card(i->card);
|
||||
card_unmark(i->card);
|
||||
draw_stack(*origin);
|
||||
break;
|
||||
}
|
||||
@ -184,7 +184,7 @@ static void handle_card_movement(struct cursor *cursor) {
|
||||
}
|
||||
} else {
|
||||
if (valid_move(*origin, *destination)) {
|
||||
if (maneuvre_stack(*destination) && length(*destination) > 1) {
|
||||
if (maneuvre_stack(*destination) && stack_length(*destination) > 1) {
|
||||
cursor->y++;
|
||||
}
|
||||
move_card(origin, destination);
|
||||
@ -257,7 +257,7 @@ void handle_keyboard_event(int key) {
|
||||
struct card *card;
|
||||
if (cursor_stack(cursor) &&
|
||||
(card = (*cursor_stack(cursor))->card)->face == COVERED) {
|
||||
expose_card(card);
|
||||
card_expose(card);
|
||||
draw_card(card);
|
||||
} else {
|
||||
handle_card_movement(cursor);
|
||||
|
72
src/stack.c
72
src/stack.c
@ -8,72 +8,72 @@
|
||||
#include "card.h"
|
||||
#include "common.h"
|
||||
|
||||
void allocate_stack(struct stack **stack) {
|
||||
void stack_malloc(struct stack **stack) {
|
||||
if (!(*stack = malloc(sizeof(**stack)))) {
|
||||
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
||||
exit(errno);
|
||||
}
|
||||
allocate_card(&((*stack)->card));
|
||||
card_malloc(&((*stack)->card));
|
||||
}
|
||||
|
||||
void initialize_stack(struct stack *stack) {
|
||||
initialize_card(stack->card);
|
||||
void stack_init(struct stack *stack) {
|
||||
card_init(stack->card);
|
||||
stack->next = NULL;
|
||||
}
|
||||
|
||||
void free_stack(struct stack *stack) {
|
||||
void stack_free(struct stack *stack) {
|
||||
struct stack *tmp;
|
||||
|
||||
for (; stack; stack = tmp) {
|
||||
tmp = stack->next;
|
||||
free_card(stack->card);
|
||||
card_free(stack->card);
|
||||
free(stack);
|
||||
}
|
||||
}
|
||||
|
||||
struct stack *duplicate_stack(struct stack *stack) {
|
||||
struct stack *stack_dup(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);
|
||||
stack_malloc(&new_stack);
|
||||
stack_malloc(&tmp_stack);
|
||||
stack_init(new_stack);
|
||||
stack_init(tmp_stack);
|
||||
for (iterator = stack; iterator; iterator = iterator->next) {
|
||||
push(&tmp_stack, duplicate_card(iterator->card));
|
||||
stack_push(&tmp_stack, card_dup(iterator->card));
|
||||
}
|
||||
while (!empty(tmp_stack)) {
|
||||
push(&new_stack, (pop(&tmp_stack)));
|
||||
while (!stack_empty(tmp_stack)) {
|
||||
stack_push(&new_stack, (stack_pop(&tmp_stack)));
|
||||
}
|
||||
free_stack(tmp_stack);
|
||||
stack_free(tmp_stack);
|
||||
|
||||
return(new_stack);
|
||||
}
|
||||
|
||||
bool empty(struct stack *stack) {
|
||||
bool stack_empty(struct stack *stack) {
|
||||
return(stack->card->value == NO_VALUE &&
|
||||
stack->card->suit == NO_SUIT &&
|
||||
stack->card->face == NO_FACE &&
|
||||
!stack->next);
|
||||
}
|
||||
|
||||
int length(struct stack *stack) {
|
||||
int length = 0;
|
||||
int stack_length(struct stack *stack) {
|
||||
int stack_length = 0;
|
||||
|
||||
if (!empty(stack)) {
|
||||
for (length = 1; stack->next; stack = stack->next, length++)
|
||||
if (!stack_empty(stack)) {
|
||||
for (stack_length = 1; stack->next; stack = stack->next, stack_length++)
|
||||
;
|
||||
}
|
||||
|
||||
return(length);
|
||||
return(stack_length);
|
||||
}
|
||||
|
||||
void push(struct stack **stack, struct card *card) {
|
||||
void stack_push(struct stack **stack, struct card *card) {
|
||||
if (card) {
|
||||
if (empty(*stack)) {
|
||||
if (stack_empty(*stack)) {
|
||||
(*stack)->card = card;
|
||||
} else {
|
||||
/* Allocating by hand because stack#allocate_stack would
|
||||
/* Allocating by hand because stack_malloc() would
|
||||
* have allocated an unwanted card object. */
|
||||
struct stack *new_stack = malloc(sizeof(*new_stack));
|
||||
new_stack->card = card;
|
||||
@ -83,19 +83,19 @@ void push(struct stack **stack, struct card *card) {
|
||||
}
|
||||
}
|
||||
|
||||
struct card *pop(struct stack **stack) {
|
||||
if(empty(*stack)) {
|
||||
struct card *stack_pop(struct stack **stack) {
|
||||
if(stack_empty(*stack)) {
|
||||
return(NULL);
|
||||
} else {
|
||||
struct card *popped_card = (*stack)->card;
|
||||
if (length(*stack) == 1) {
|
||||
if (stack_length(*stack) == 1) {
|
||||
/* Remembering the stack position before clearing it. */
|
||||
int begin_y = (*stack)->card->frame->begin_y;
|
||||
int begin_x = (*stack)->card->frame->begin_x;
|
||||
allocate_card(&((*stack)->card));
|
||||
/* An empty stack is a stack with a blank top card
|
||||
card_malloc(&((*stack)->card));
|
||||
/* An stack_empty stack is a stack with a blank top card
|
||||
* and with stack->next == NULL. */
|
||||
set_card((*stack)->card, NO_VALUE, NO_SUIT, NO_FACE, begin_y, begin_x);
|
||||
card_set((*stack)->card, NO_VALUE, NO_SUIT, NO_FACE, begin_y, begin_x);
|
||||
(*stack)->next = NULL;
|
||||
} else {
|
||||
struct stack *tmp = *stack;
|
||||
@ -106,17 +106,17 @@ struct card *pop(struct stack **stack) {
|
||||
}
|
||||
}
|
||||
|
||||
struct stack *reverse(struct stack *stack) {
|
||||
struct stack *stack_reverse(struct stack *stack) {
|
||||
struct stack *tmp_stack, *iterator;
|
||||
|
||||
allocate_stack(&tmp_stack);
|
||||
initialize_stack(tmp_stack);
|
||||
if (length(stack) > 1) {
|
||||
stack_malloc(&tmp_stack);
|
||||
stack_init(tmp_stack);
|
||||
if (stack_length(stack) > 1) {
|
||||
for (iterator = stack; iterator; iterator = iterator->next) {
|
||||
push(&tmp_stack, duplicate_card(iterator->card));
|
||||
stack_push(&tmp_stack, card_dup(iterator->card));
|
||||
}
|
||||
} else {
|
||||
set_card(tmp_stack->card,
|
||||
card_set(tmp_stack->card,
|
||||
stack->card->value,
|
||||
stack->card->suit,
|
||||
stack->card->face,
|
||||
|
18
src/stack.h
18
src/stack.h
@ -8,14 +8,14 @@ struct stack {
|
||||
struct stack *next;
|
||||
};
|
||||
|
||||
void allocate_stack(struct stack **);
|
||||
void initialize_stack(struct stack *);
|
||||
void free_stack(struct stack *);
|
||||
struct stack *duplicate_stack(struct stack *);
|
||||
bool empty(struct stack *);
|
||||
int length(struct stack *);
|
||||
void push(struct stack **, struct card *);
|
||||
struct card *pop(struct stack **);
|
||||
struct stack *reverse(struct stack *);
|
||||
void stack_malloc(struct stack **);
|
||||
void stack_init(struct stack *);
|
||||
void stack_free(struct stack *);
|
||||
bool stack_empty(struct stack *);
|
||||
int stack_length(struct stack *);
|
||||
void stack_push(struct stack **, struct card *);
|
||||
struct card *stack_pop(struct stack **);
|
||||
struct stack *stack_reverse(struct stack *);
|
||||
struct stack *stack_dup(struct stack *);
|
||||
|
||||
#endif
|
||||
|
@ -2,39 +2,39 @@
|
||||
#include "test_helper.h"
|
||||
#include "../src/card.h"
|
||||
|
||||
void test_initialize_card() {
|
||||
void test_card_init() {
|
||||
struct card *card;
|
||||
|
||||
allocate_card(&card);
|
||||
initialize_card(card);
|
||||
card_malloc(&card);
|
||||
card_init(card);
|
||||
|
||||
assert(card->value == NO_VALUE);
|
||||
assert(card->suit == NO_SUIT);
|
||||
assert(card->face == NO_FACE);
|
||||
|
||||
free_card(card);
|
||||
card_free(card);
|
||||
}
|
||||
|
||||
void test_duplicate_card() {
|
||||
void test_card_dup() {
|
||||
struct card *card_0, *card_1;
|
||||
const int begin_y = 5, begin_x = 10;
|
||||
|
||||
allocate_card(&card_0);
|
||||
set_card(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||
card_1 = duplicate_card(card_0);
|
||||
card_malloc(&card_0);
|
||||
card_set(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||
card_1 = card_dup(card_0);
|
||||
|
||||
assert(card_0 != card_1);
|
||||
assert(cards_equal(card_0, card_1));
|
||||
}
|
||||
|
||||
void test_set_card() {
|
||||
void test_card_set() {
|
||||
struct card *card;
|
||||
int begin_y = 5;
|
||||
int begin_x = 10;
|
||||
|
||||
allocate_card(&card);
|
||||
initialize_card(card);
|
||||
set_card(card, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||
card_malloc(&card);
|
||||
card_init(card);
|
||||
card_set(card, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||
|
||||
assert(card->value == ACE);
|
||||
assert(card->suit == SPADES);
|
||||
@ -42,37 +42,37 @@ void test_set_card() {
|
||||
assert(card->frame->begin_y == begin_y);
|
||||
assert(card->frame->begin_x == begin_x);
|
||||
|
||||
free_card(card);
|
||||
card_free(card);
|
||||
}
|
||||
|
||||
void test_expose_card() {
|
||||
void test_card_expose() {
|
||||
struct card *card;
|
||||
|
||||
allocate_card(&card);
|
||||
initialize_card(card);
|
||||
expose_card(card);
|
||||
card_malloc(&card);
|
||||
card_init(card);
|
||||
card_expose(card);
|
||||
|
||||
assert(card->face == EXPOSED);
|
||||
|
||||
free_card(card);
|
||||
card_free(card);
|
||||
}
|
||||
|
||||
void test_cover_card() {
|
||||
void test_card_cover() {
|
||||
struct card *card;
|
||||
|
||||
allocate_card(&card);
|
||||
initialize_card(card);
|
||||
cover_card(card);
|
||||
card_malloc(&card);
|
||||
card_init(card);
|
||||
card_cover(card);
|
||||
|
||||
assert(card->face == COVERED);
|
||||
|
||||
free_card(card);
|
||||
card_free(card);
|
||||
}
|
||||
|
||||
void test_card() {
|
||||
test_initialize_card();
|
||||
test_duplicate_card();
|
||||
test_set_card();
|
||||
test_expose_card();
|
||||
test_cover_card();
|
||||
test_card_init();
|
||||
test_card_dup();
|
||||
test_card_set();
|
||||
test_card_expose();
|
||||
test_card_cover();
|
||||
}
|
||||
|
@ -6,206 +6,206 @@
|
||||
void test_valid_move_from_stock_to_stock() {
|
||||
struct stack *stock_0, *stock_1;
|
||||
|
||||
allocate_stack(&stock_0);
|
||||
allocate_stack(&stock_1);
|
||||
initialize_stack(stock_0);
|
||||
initialize_stack(stock_1);
|
||||
set_card(stock_0->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
set_card(stock_1->card, KING, HEARTS, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
stack_malloc(&stock_0);
|
||||
stack_malloc(&stock_1);
|
||||
stack_init(stock_0);
|
||||
stack_init(stock_1);
|
||||
card_set(stock_0->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
card_set(stock_1->card, KING, HEARTS, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
assert(!valid_move(stock_0, stock_0));
|
||||
assert(!valid_move(stock_0, stock_1));
|
||||
assert(!valid_move(stock_1, stock_0));
|
||||
assert(!valid_move(stock_1, stock_1));
|
||||
free_stack(stock_0);
|
||||
free_stack(stock_1);
|
||||
stack_free(stock_0);
|
||||
stack_free(stock_1);
|
||||
}
|
||||
|
||||
void test_valid_move_from_stock_to_waste_pile() {
|
||||
struct stack *stock, *waste_pile;
|
||||
|
||||
allocate_stack(&stock);
|
||||
allocate_stack(&waste_pile);
|
||||
initialize_stack(stock);
|
||||
initialize_stack(waste_pile);
|
||||
set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
set_card(waste_pile->card, KING, HEARTS, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
stack_malloc(&stock);
|
||||
stack_malloc(&waste_pile);
|
||||
stack_init(stock);
|
||||
stack_init(waste_pile);
|
||||
card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
card_set(waste_pile->card, KING, HEARTS, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
assert(valid_move(stock, waste_pile));
|
||||
free_stack(stock);
|
||||
free_stack(waste_pile);
|
||||
stack_free(stock);
|
||||
stack_free(waste_pile);
|
||||
}
|
||||
|
||||
void test_valid_move_from_stock_to_foundation_stacks() {
|
||||
struct stack *stock, *foundation_stacks[4];
|
||||
|
||||
allocate_stack(&stock);
|
||||
initialize_stack(stock);
|
||||
set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
stack_malloc(&stock);
|
||||
stack_init(stock);
|
||||
card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
allocate_stack(&foundation_stacks[i]);
|
||||
initialize_stack(foundation_stacks[i]);
|
||||
stack_malloc(&foundation_stacks[i]);
|
||||
stack_init(foundation_stacks[i]);
|
||||
}
|
||||
set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||
set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||
set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||
set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||
card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||
card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||
card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||
card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
assert(!valid_move(stock, foundation_stacks[i]));
|
||||
}
|
||||
free_stack(stock);
|
||||
stack_free(stock);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
free_stack(foundation_stacks[i]);
|
||||
stack_free(foundation_stacks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void test_valid_move_from_stock_to_maneuvre_stacks() {
|
||||
struct stack *stock, *maneuvre_stacks[7];
|
||||
|
||||
allocate_stack(&stock);
|
||||
initialize_stack(stock);
|
||||
set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
stack_malloc(&stock);
|
||||
stack_init(stock);
|
||||
card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
allocate_stack(&maneuvre_stacks[i]);
|
||||
initialize_stack(maneuvre_stacks[i]);
|
||||
stack_malloc(&maneuvre_stacks[i]);
|
||||
stack_init(maneuvre_stacks[i]);
|
||||
}
|
||||
set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||
set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||
set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||
set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||
set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||
card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||
card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||
card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||
card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||
card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
assert(!valid_move(stock, maneuvre_stacks[i]));
|
||||
}
|
||||
free_stack(stock);
|
||||
stack_free(stock);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
free_stack(maneuvre_stacks[i]);
|
||||
stack_free(maneuvre_stacks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void test_valid_move_from_waste_pile_to_stock() {
|
||||
struct stack *stock, *waste_pile;
|
||||
|
||||
allocate_stack(&stock);
|
||||
allocate_stack(&waste_pile);
|
||||
initialize_stack(stock);
|
||||
initialize_stack(waste_pile);
|
||||
set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
set_card(waste_pile->card, KING, HEARTS, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
stack_malloc(&stock);
|
||||
stack_malloc(&waste_pile);
|
||||
stack_init(stock);
|
||||
stack_init(waste_pile);
|
||||
card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
card_set(waste_pile->card, KING, HEARTS, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
assert(!valid_move(waste_pile, stock));
|
||||
free_stack(stock);
|
||||
free_stack(waste_pile);
|
||||
stack_free(stock);
|
||||
stack_free(waste_pile);
|
||||
}
|
||||
|
||||
void test_valid_move_from_waste_pile_to_waste_pile() {
|
||||
struct stack *waste_pile_0, *waste_pile_1;
|
||||
|
||||
allocate_stack(&waste_pile_0);
|
||||
allocate_stack(&waste_pile_1);
|
||||
initialize_stack(waste_pile_0);
|
||||
initialize_stack(waste_pile_1);
|
||||
set_card(waste_pile_0->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
set_card(waste_pile_1->card, KING, HEARTS, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
stack_malloc(&waste_pile_0);
|
||||
stack_malloc(&waste_pile_1);
|
||||
stack_init(waste_pile_0);
|
||||
stack_init(waste_pile_1);
|
||||
card_set(waste_pile_0->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
card_set(waste_pile_1->card, KING, HEARTS, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
assert(!valid_move(waste_pile_0, waste_pile_0));
|
||||
assert(!valid_move(waste_pile_0, waste_pile_1));
|
||||
assert(!valid_move(waste_pile_1, waste_pile_0));
|
||||
assert(!valid_move(waste_pile_1, waste_pile_1));
|
||||
free_stack(waste_pile_0);
|
||||
free_stack(waste_pile_1);
|
||||
stack_free(waste_pile_0);
|
||||
stack_free(waste_pile_1);
|
||||
}
|
||||
|
||||
void test_valid_move_from_waste_pile_to_foundation_stacks() {
|
||||
struct stack *waste_pile, *foundation_stacks[4];
|
||||
|
||||
allocate_stack(&waste_pile);
|
||||
initialize_stack(waste_pile);
|
||||
set_card(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
stack_malloc(&waste_pile);
|
||||
stack_init(waste_pile);
|
||||
card_set(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
allocate_stack(&foundation_stacks[i]);
|
||||
initialize_stack(foundation_stacks[i]);
|
||||
stack_malloc(&foundation_stacks[i]);
|
||||
stack_init(foundation_stacks[i]);
|
||||
}
|
||||
set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||
set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||
set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||
set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||
card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||
card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||
card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||
card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
assert(valid_move(waste_pile, foundation_stacks[i]));
|
||||
}
|
||||
free_stack(waste_pile);
|
||||
stack_free(waste_pile);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
free_stack(foundation_stacks[i]);
|
||||
stack_free(foundation_stacks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void test_valid_move_from_waste_pile_to_maneuvre_stacks() {
|
||||
struct stack *waste_pile, *maneuvre_stacks[7];
|
||||
|
||||
allocate_stack(&waste_pile);
|
||||
initialize_stack(waste_pile);
|
||||
set_card(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
stack_malloc(&waste_pile);
|
||||
stack_init(waste_pile);
|
||||
card_set(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
allocate_stack(&maneuvre_stacks[i]);
|
||||
initialize_stack(maneuvre_stacks[i]);
|
||||
stack_malloc(&maneuvre_stacks[i]);
|
||||
stack_init(maneuvre_stacks[i]);
|
||||
}
|
||||
set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||
set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||
set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||
set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||
set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||
card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||
card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||
card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||
card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||
card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
assert(valid_move(waste_pile, maneuvre_stacks[i]));
|
||||
}
|
||||
free_stack(waste_pile);
|
||||
stack_free(waste_pile);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
free_stack(maneuvre_stacks[i]);
|
||||
stack_free(maneuvre_stacks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void test_valid_move_from_foundation_stack_to_stock() {
|
||||
struct stack *stock, *foundation_stacks[4];
|
||||
|
||||
allocate_stack(&stock);
|
||||
initialize_stack(stock);
|
||||
set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
stack_malloc(&stock);
|
||||
stack_init(stock);
|
||||
card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
allocate_stack(&foundation_stacks[i]);
|
||||
initialize_stack(foundation_stacks[i]);
|
||||
stack_malloc(&foundation_stacks[i]);
|
||||
stack_init(foundation_stacks[i]);
|
||||
}
|
||||
set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||
set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||
set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||
set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||
card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||
card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||
card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||
card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
assert(!valid_move(foundation_stacks[i], stock));
|
||||
}
|
||||
free_stack(stock);
|
||||
stack_free(stock);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
free_stack(foundation_stacks[i]);
|
||||
stack_free(foundation_stacks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void test_valid_move_from_foundation_stack_to_waste_pile() {
|
||||
struct stack *waste_pile, *foundation_stacks[4];
|
||||
|
||||
allocate_stack(&waste_pile);
|
||||
initialize_stack(waste_pile);
|
||||
set_card(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
stack_malloc(&waste_pile);
|
||||
stack_init(waste_pile);
|
||||
card_set(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
allocate_stack(&foundation_stacks[i]);
|
||||
initialize_stack(foundation_stacks[i]);
|
||||
stack_malloc(&foundation_stacks[i]);
|
||||
stack_init(foundation_stacks[i]);
|
||||
}
|
||||
set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||
set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||
set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||
set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||
card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||
card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||
card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||
card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
assert(!valid_move(foundation_stacks[i], waste_pile));
|
||||
}
|
||||
free_stack(waste_pile);
|
||||
stack_free(waste_pile);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
free_stack(foundation_stacks[i]);
|
||||
stack_free(foundation_stacks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -213,13 +213,13 @@ void test_valid_move_from_foundation_stack_to_foundation_stacks() {
|
||||
struct stack *foundation_stacks[4];
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
allocate_stack(&foundation_stacks[i]);
|
||||
initialize_stack(foundation_stacks[i]);
|
||||
stack_malloc(&foundation_stacks[i]);
|
||||
stack_init(foundation_stacks[i]);
|
||||
}
|
||||
set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||
set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||
set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||
set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||
card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||
card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||
card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||
card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
if (i == j) {
|
||||
@ -230,7 +230,7 @@ void test_valid_move_from_foundation_stack_to_foundation_stacks() {
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 4; i++) {
|
||||
free_stack(foundation_stacks[i]);
|
||||
stack_free(foundation_stacks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -239,86 +239,86 @@ void test_valid_move_from_foundation_stack_to_maneuvre_stacks() {
|
||||
struct stack *maneuvre_stacks[7];
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
allocate_stack(&foundation_stacks[i]);
|
||||
initialize_stack(foundation_stacks[i]);
|
||||
stack_malloc(&foundation_stacks[i]);
|
||||
stack_init(foundation_stacks[i]);
|
||||
}
|
||||
set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||
set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||
set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||
set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||
card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||
card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||
card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||
card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
allocate_stack(&maneuvre_stacks[i]);
|
||||
initialize_stack(maneuvre_stacks[i]);
|
||||
stack_malloc(&maneuvre_stacks[i]);
|
||||
stack_init(maneuvre_stacks[i]);
|
||||
}
|
||||
set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||
set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||
set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||
set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||
set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||
card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||
card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||
card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||
card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||
card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 7; j++) {
|
||||
assert(valid_move(foundation_stacks[i], maneuvre_stacks[j]));
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 4; i++) {
|
||||
free_stack(foundation_stacks[i]);
|
||||
stack_free(foundation_stacks[i]);
|
||||
}
|
||||
for (int i = 0; i < 7; i++) {
|
||||
free_stack(maneuvre_stacks[i]);
|
||||
stack_free(maneuvre_stacks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void test_valid_move_from_maneuvre_stack_to_stock() {
|
||||
struct stack *stock, *maneuvre_stacks[7];
|
||||
|
||||
allocate_stack(&stock);
|
||||
initialize_stack(stock);
|
||||
set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
stack_malloc(&stock);
|
||||
stack_init(stock);
|
||||
card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
allocate_stack(&maneuvre_stacks[i]);
|
||||
initialize_stack(maneuvre_stacks[i]);
|
||||
stack_malloc(&maneuvre_stacks[i]);
|
||||
stack_init(maneuvre_stacks[i]);
|
||||
}
|
||||
set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||
set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||
set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||
set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||
set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||
card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||
card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||
card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||
card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||
card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
assert(!valid_move(maneuvre_stacks[i], stock));
|
||||
}
|
||||
free_stack(stock);
|
||||
stack_free(stock);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
free_stack(maneuvre_stacks[i]);
|
||||
stack_free(maneuvre_stacks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void test_valid_move_from_maneuvre_stack_to_waste_pile() {
|
||||
struct stack *waste_pile, *maneuvre_stacks[7];
|
||||
|
||||
allocate_stack(&waste_pile);
|
||||
initialize_stack(waste_pile);
|
||||
set_card(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
stack_malloc(&waste_pile);
|
||||
stack_init(waste_pile);
|
||||
card_set(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
allocate_stack(&maneuvre_stacks[i]);
|
||||
initialize_stack(maneuvre_stacks[i]);
|
||||
stack_malloc(&maneuvre_stacks[i]);
|
||||
stack_init(maneuvre_stacks[i]);
|
||||
}
|
||||
set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||
set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||
set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||
set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||
set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||
card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||
card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||
card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||
card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||
card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
assert(!valid_move(maneuvre_stacks[i], waste_pile));
|
||||
}
|
||||
free_stack(waste_pile);
|
||||
stack_free(waste_pile);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
free_stack(maneuvre_stacks[i]);
|
||||
stack_free(maneuvre_stacks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -327,34 +327,34 @@ void test_valid_move_from_maneuvre_stack_to_foundation_stacks() {
|
||||
struct stack *maneuvre_stacks[7];
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
allocate_stack(&foundation_stacks[i]);
|
||||
initialize_stack(foundation_stacks[i]);
|
||||
stack_malloc(&foundation_stacks[i]);
|
||||
stack_init(foundation_stacks[i]);
|
||||
}
|
||||
set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||
set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||
set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||
set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||
card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||
card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||
card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||
card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
allocate_stack(&maneuvre_stacks[i]);
|
||||
initialize_stack(maneuvre_stacks[i]);
|
||||
stack_malloc(&maneuvre_stacks[i]);
|
||||
stack_init(maneuvre_stacks[i]);
|
||||
}
|
||||
set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||
set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||
set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||
set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||
set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||
card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||
card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||
card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||
card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||
card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
assert(valid_move(maneuvre_stacks[i], foundation_stacks[j]));
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 4; i++) {
|
||||
free_stack(foundation_stacks[i]);
|
||||
stack_free(foundation_stacks[i]);
|
||||
}
|
||||
for (int i = 0; i < 7; i++) {
|
||||
free_stack(maneuvre_stacks[i]);
|
||||
stack_free(maneuvre_stacks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -362,16 +362,16 @@ void test_valid_move_from_maneuvre_stack_to_maneuvre_stacks() {
|
||||
struct stack *maneuvre_stacks[7];
|
||||
|
||||
for (int i = 0; i < 7; i++) {
|
||||
allocate_stack(&maneuvre_stacks[i]);
|
||||
initialize_stack(maneuvre_stacks[i]);
|
||||
stack_malloc(&maneuvre_stacks[i]);
|
||||
stack_init(maneuvre_stacks[i]);
|
||||
}
|
||||
set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||
set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||
set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||
set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||
set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||
card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||
card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||
card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||
card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||
card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
for (int j = 0; j < 7; j++) {
|
||||
if (i == j) {
|
||||
@ -382,23 +382,23 @@ void test_valid_move_from_maneuvre_stack_to_maneuvre_stacks() {
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 7; i++) {
|
||||
free_stack(maneuvre_stacks[i]);
|
||||
stack_free(maneuvre_stacks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void test_move_card_from_empty_stack_to_empty_stack() {
|
||||
void test_move_card_from_stack_empty_stack_to_stack_empty_stack() {
|
||||
struct stack *origin, *destination,
|
||||
*new_origin, *new_destination,
|
||||
*origin_duplicate, *destination_duplicate;
|
||||
|
||||
allocate_stack(&origin);
|
||||
allocate_stack(&destination);
|
||||
initialize_stack(origin);
|
||||
initialize_stack(destination);
|
||||
stack_malloc(&origin);
|
||||
stack_malloc(&destination);
|
||||
stack_init(origin);
|
||||
stack_init(destination);
|
||||
new_origin = origin;
|
||||
new_destination = destination;
|
||||
origin_duplicate = duplicate_stack(origin);
|
||||
destination_duplicate = duplicate_stack(destination);
|
||||
origin_duplicate = stack_dup(origin);
|
||||
destination_duplicate = stack_dup(destination);
|
||||
move_card(&new_origin, &new_destination);
|
||||
|
||||
assert(origin == new_origin);
|
||||
@ -406,29 +406,29 @@ void test_move_card_from_empty_stack_to_empty_stack() {
|
||||
assert(destination == new_destination);
|
||||
assert(stacks_equal(destination, destination_duplicate));
|
||||
|
||||
free_stack(origin);
|
||||
free_stack(destination);
|
||||
stack_free(origin);
|
||||
stack_free(destination);
|
||||
}
|
||||
|
||||
void test_move_card_from_empty_stack_to_non_empty_stack() {
|
||||
void test_move_card_from_stack_empty_stack_to_non_stack_empty_stack() {
|
||||
struct stack *origin, *destination,
|
||||
*new_origin, *new_destination,
|
||||
*origin_duplicate, *destination_duplicate;
|
||||
struct card *card;
|
||||
|
||||
allocate_card(&card);
|
||||
initialize_card(card);
|
||||
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||
card_malloc(&card);
|
||||
card_init(card);
|
||||
card_set(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||
|
||||
allocate_stack(&origin);
|
||||
allocate_stack(&destination);
|
||||
initialize_stack(origin);
|
||||
initialize_stack(destination);
|
||||
stack_malloc(&origin);
|
||||
stack_malloc(&destination);
|
||||
stack_init(origin);
|
||||
stack_init(destination);
|
||||
new_origin = origin;
|
||||
new_destination = destination;
|
||||
push(&new_destination, card);
|
||||
origin_duplicate = duplicate_stack(origin);
|
||||
destination_duplicate = duplicate_stack(destination);
|
||||
stack_push(&new_destination, card);
|
||||
origin_duplicate = stack_dup(origin);
|
||||
destination_duplicate = stack_dup(destination);
|
||||
move_card(&new_origin, &new_destination);
|
||||
|
||||
assert(origin == new_origin);
|
||||
@ -436,104 +436,104 @@ void test_move_card_from_empty_stack_to_non_empty_stack() {
|
||||
assert(destination == new_destination);
|
||||
assert(stacks_equal(destination, destination_duplicate));
|
||||
|
||||
free_stack(origin);
|
||||
free_stack(destination);
|
||||
stack_free(origin);
|
||||
stack_free(destination);
|
||||
}
|
||||
|
||||
void test_move_card_from_non_empty_stack_to_empty_stack() {
|
||||
void test_move_card_from_non_stack_empty_stack_to_stack_empty_stack() {
|
||||
struct stack *origin, *destination;
|
||||
struct card *card[6];
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
allocate_card(&card[i]);
|
||||
initialize_card(card[i]);
|
||||
set_card(card[i], TWO + i, i % 5, i % 2, 99, 99);
|
||||
card_malloc(&card[i]);
|
||||
card_init(card[i]);
|
||||
card_set(card[i], TWO + i, i % 5, i % 2, 99, 99);
|
||||
}
|
||||
|
||||
allocate_stack(&origin);
|
||||
allocate_stack(&destination);
|
||||
initialize_stack(origin);
|
||||
initialize_stack(destination);
|
||||
stack_malloc(&origin);
|
||||
stack_malloc(&destination);
|
||||
stack_init(origin);
|
||||
stack_init(destination);
|
||||
for (int i = 0; i < 6; i++) {
|
||||
push(&origin, card[i]);
|
||||
stack_push(&origin, card[i]);
|
||||
}
|
||||
move_card(&origin, &destination);
|
||||
|
||||
assert(length(origin) == 5);
|
||||
assert(length(destination) == 1);
|
||||
assert(stack_length(origin) == 5);
|
||||
assert(stack_length(destination) == 1);
|
||||
assert(cards_equal(destination->card, card[5]));
|
||||
|
||||
move_card(&origin, &destination);
|
||||
|
||||
assert(length(origin) == 4);
|
||||
assert(length(destination) == 2);
|
||||
assert(stack_length(origin) == 4);
|
||||
assert(stack_length(destination) == 2);
|
||||
assert(cards_equal(destination->card, card[4]));
|
||||
|
||||
move_card(&origin, &destination);
|
||||
|
||||
assert(length(origin) == 3);
|
||||
assert(length(destination) == 3);
|
||||
assert(stack_length(origin) == 3);
|
||||
assert(stack_length(destination) == 3);
|
||||
assert(cards_equal(destination->card, card[3]));
|
||||
|
||||
free_stack(origin);
|
||||
free_stack(destination);
|
||||
stack_free(origin);
|
||||
stack_free(destination);
|
||||
}
|
||||
|
||||
void test_move_card_from_non_empty_stack_to_non_empty_stack() {
|
||||
void test_move_card_from_non_stack_empty_stack_to_non_stack_empty_stack() {
|
||||
struct stack *origin, *destination;
|
||||
struct card *card[6];
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
allocate_card(&card[i]);
|
||||
initialize_card(card[i]);
|
||||
set_card(card[i], TWO + i, i % 5, i % 2, 99, 99);
|
||||
card_malloc(&card[i]);
|
||||
card_init(card[i]);
|
||||
card_set(card[i], TWO + i, i % 5, i % 2, 99, 99);
|
||||
}
|
||||
|
||||
allocate_stack(&origin);
|
||||
allocate_stack(&destination);
|
||||
initialize_stack(origin);
|
||||
initialize_stack(destination);
|
||||
stack_malloc(&origin);
|
||||
stack_malloc(&destination);
|
||||
stack_init(origin);
|
||||
stack_init(destination);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
push(&origin, card[i]);
|
||||
stack_push(&origin, card[i]);
|
||||
}
|
||||
for (int i = 3; i < 6; i++) {
|
||||
push(&destination, card[i]);
|
||||
stack_push(&destination, card[i]);
|
||||
}
|
||||
move_card(&origin, &destination);
|
||||
|
||||
assert(length(origin) == 2);
|
||||
assert(length(destination) == 4);
|
||||
assert(stack_length(origin) == 2);
|
||||
assert(stack_length(destination) == 4);
|
||||
assert(cards_equal(destination->card, card[2]));
|
||||
assert(cards_equal(destination->next->card, card[5]));
|
||||
|
||||
free_stack(origin);
|
||||
free_stack(destination);
|
||||
stack_free(origin);
|
||||
stack_free(destination);
|
||||
}
|
||||
|
||||
void test_move_card_should_not_change_empty_stack_coordinates() {
|
||||
void test_move_card_should_not_change_stack_empty_stack_coordinates() {
|
||||
struct stack *origin, *destination;
|
||||
struct card *card[2];
|
||||
|
||||
allocate_card(&card[0]);
|
||||
allocate_card(&card[1]);
|
||||
initialize_card(card[0]);
|
||||
initialize_card(card[1]);
|
||||
set_card(card[0], ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
set_card(card[1], KING, HEARTS, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
card_malloc(&card[0]);
|
||||
card_malloc(&card[1]);
|
||||
card_init(card[0]);
|
||||
card_init(card[1]);
|
||||
card_set(card[0], ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
card_set(card[1], KING, HEARTS, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
|
||||
allocate_stack(&origin);
|
||||
allocate_stack(&destination);
|
||||
initialize_stack(origin);
|
||||
initialize_stack(destination);
|
||||
push(&origin, card[0]);
|
||||
push(&destination, card[1]);
|
||||
stack_malloc(&origin);
|
||||
stack_malloc(&destination);
|
||||
stack_init(origin);
|
||||
stack_init(destination);
|
||||
stack_push(&origin, card[0]);
|
||||
stack_push(&destination, card[1]);
|
||||
move_card(&origin, &destination);
|
||||
|
||||
assert(origin->card->frame->begin_y == MANEUVRE_BEGIN_Y);
|
||||
assert(origin->card->frame->begin_x == MANEUVRE_0_BEGIN_X);
|
||||
|
||||
free_stack(origin);
|
||||
free_stack(destination);
|
||||
stack_free(origin);
|
||||
stack_free(destination);
|
||||
}
|
||||
|
||||
void test_game() {
|
||||
@ -554,9 +554,9 @@ void test_game() {
|
||||
test_valid_move_from_maneuvre_stack_to_foundation_stacks();
|
||||
test_valid_move_from_maneuvre_stack_to_maneuvre_stacks();
|
||||
|
||||
test_move_card_from_empty_stack_to_empty_stack();
|
||||
test_move_card_from_empty_stack_to_non_empty_stack();
|
||||
test_move_card_from_non_empty_stack_to_empty_stack();
|
||||
test_move_card_from_non_empty_stack_to_non_empty_stack();
|
||||
test_move_card_should_not_change_empty_stack_coordinates();
|
||||
test_move_card_from_stack_empty_stack_to_stack_empty_stack();
|
||||
test_move_card_from_stack_empty_stack_to_non_stack_empty_stack();
|
||||
test_move_card_from_non_stack_empty_stack_to_stack_empty_stack();
|
||||
test_move_card_from_non_stack_empty_stack_to_non_stack_empty_stack();
|
||||
test_move_card_should_not_change_stack_empty_stack_coordinates();
|
||||
}
|
||||
|
@ -2,314 +2,314 @@
|
||||
#include "test_helper.h"
|
||||
#include "../src/stack.h"
|
||||
|
||||
void test_initialize_stack() {
|
||||
void test_stack_init() {
|
||||
struct stack *stack;
|
||||
|
||||
allocate_stack(&stack);
|
||||
initialize_stack(stack);
|
||||
stack_malloc(&stack);
|
||||
stack_init(stack);
|
||||
|
||||
assert(stack->card->value == NO_VALUE);
|
||||
assert(!stack->next);
|
||||
|
||||
free_stack(stack);
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void test_duplicate_stack() {
|
||||
void test_stack_dup() {
|
||||
struct stack *stack_0, *stack_1;
|
||||
struct card *card[5];
|
||||
const int begin_y = 5, begin_x = 10;
|
||||
|
||||
allocate_stack(&stack_0);
|
||||
initialize_stack(stack_0);
|
||||
stack_malloc(&stack_0);
|
||||
stack_init(stack_0);
|
||||
for (int i = 0; i < 5; i++) {
|
||||
allocate_card(&card[i]);
|
||||
set_card(card[i], i, SPADES, EXPOSED, begin_y, begin_x);
|
||||
push(&stack_0, card[i]);
|
||||
card_malloc(&card[i]);
|
||||
card_set(card[i], i, SPADES, EXPOSED, begin_y, begin_x);
|
||||
stack_push(&stack_0, card[i]);
|
||||
}
|
||||
stack_1 = duplicate_stack(stack_0);
|
||||
stack_1 = stack_dup(stack_0);
|
||||
|
||||
assert(stack_0 != stack_1);
|
||||
assert(stacks_equal(stack_0, stack_1));
|
||||
}
|
||||
|
||||
void test_empty_on_empty_stack() {
|
||||
void test_stack_empty_on_stack_empty_stack() {
|
||||
struct stack *stack;
|
||||
|
||||
allocate_stack(&stack);
|
||||
initialize_stack(stack);
|
||||
stack_malloc(&stack);
|
||||
stack_init(stack);
|
||||
|
||||
assert(empty(stack));
|
||||
assert(stack_empty(stack));
|
||||
|
||||
free_stack(stack);
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void test_empty_on_non_empty_stack() {
|
||||
void test_stack_empty_on_non_stack_empty_stack() {
|
||||
struct stack *stack;
|
||||
struct card *card;
|
||||
|
||||
allocate_card(&card);
|
||||
initialize_card(card);
|
||||
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||
card_malloc(&card);
|
||||
card_init(card);
|
||||
card_set(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||
|
||||
allocate_stack(&stack);
|
||||
initialize_stack(stack);
|
||||
push(&stack, card);
|
||||
stack_malloc(&stack);
|
||||
stack_init(stack);
|
||||
stack_push(&stack, card);
|
||||
|
||||
assert(!empty(stack));
|
||||
assert(!stack_empty(stack));
|
||||
|
||||
free_stack(stack);
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void test_length() {
|
||||
void test_stack_length() {
|
||||
struct stack *stack;
|
||||
struct card *card[4];
|
||||
|
||||
allocate_stack(&stack);
|
||||
initialize_stack(stack);
|
||||
stack_malloc(&stack);
|
||||
stack_init(stack);
|
||||
|
||||
assert(length(stack) == 0);
|
||||
assert(stack_length(stack) == 0);
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
allocate_card(&card[i]);
|
||||
initialize_card(card[i]);
|
||||
set_card(card[i], i, SPADES, EXPOSED, 0, 0);
|
||||
push(&stack, card[i]);
|
||||
assert(length(stack) == i + 1);
|
||||
card_malloc(&card[i]);
|
||||
card_init(card[i]);
|
||||
card_set(card[i], i, SPADES, EXPOSED, 0, 0);
|
||||
stack_push(&stack, card[i]);
|
||||
assert(stack_length(stack) == i + 1);
|
||||
}
|
||||
|
||||
free_stack(stack);
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void test_push_on_empty_stack() {
|
||||
void test_stack_push_on_stack_empty_stack() {
|
||||
struct stack *stack;
|
||||
struct card *card;
|
||||
|
||||
allocate_card(&card);
|
||||
initialize_card(card);
|
||||
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||
card_malloc(&card);
|
||||
card_init(card);
|
||||
card_set(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||
|
||||
allocate_stack(&stack);
|
||||
initialize_stack(stack);
|
||||
push(&stack, card);
|
||||
stack_malloc(&stack);
|
||||
stack_init(stack);
|
||||
stack_push(&stack, card);
|
||||
|
||||
assert(cards_equal(stack->card, card));
|
||||
assert(!stack->next);
|
||||
|
||||
free_stack(stack);
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void test_push_on_non_empty_stack() {
|
||||
void test_stack_push_on_non_stack_empty_stack() {
|
||||
struct stack *stack;
|
||||
struct card *card_0, *card_1;
|
||||
|
||||
allocate_card(&card_0);
|
||||
allocate_card(&card_1);
|
||||
initialize_card(card_0);
|
||||
initialize_card(card_1);
|
||||
set_card(card_0, ACE, SPADES, EXPOSED, 0, 0);
|
||||
set_card(card_1, ACE, HEARTS, EXPOSED, 0, 0);
|
||||
card_malloc(&card_0);
|
||||
card_malloc(&card_1);
|
||||
card_init(card_0);
|
||||
card_init(card_1);
|
||||
card_set(card_0, ACE, SPADES, EXPOSED, 0, 0);
|
||||
card_set(card_1, ACE, HEARTS, EXPOSED, 0, 0);
|
||||
|
||||
allocate_stack(&stack);
|
||||
initialize_stack(stack);
|
||||
push(&stack, card_0);
|
||||
push(&stack, card_1);
|
||||
stack_malloc(&stack);
|
||||
stack_init(stack);
|
||||
stack_push(&stack, card_0);
|
||||
stack_push(&stack, card_1);
|
||||
|
||||
assert(cards_equal(stack->card, card_1));
|
||||
assert(cards_equal(stack->next->card, card_0));
|
||||
assert(!stack->next->next);
|
||||
|
||||
free_stack(stack);
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void test_push_null_on_empty_stack() {
|
||||
void test_stack_push_null_on_stack_empty_stack() {
|
||||
struct stack *stack, *old_stack;
|
||||
|
||||
allocate_stack(&stack);
|
||||
initialize_stack(stack);
|
||||
stack_malloc(&stack);
|
||||
stack_init(stack);
|
||||
old_stack = stack;
|
||||
push(&stack, NULL);
|
||||
stack_push(&stack, NULL);
|
||||
|
||||
assert(cards_equal(stack->card, old_stack->card));
|
||||
assert(!stack->next);
|
||||
|
||||
free_stack(stack);
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void test_push_null_on_non_empty_stack() {
|
||||
void test_stack_push_null_on_non_stack_empty_stack() {
|
||||
struct stack *stack, *old_stack;
|
||||
struct card *card;
|
||||
|
||||
allocate_card(&card);
|
||||
initialize_card(card);
|
||||
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||
card_malloc(&card);
|
||||
card_init(card);
|
||||
card_set(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||
|
||||
allocate_stack(&stack);
|
||||
initialize_stack(stack);
|
||||
old_stack = duplicate_stack(stack);
|
||||
push(&stack, NULL);
|
||||
stack_malloc(&stack);
|
||||
stack_init(stack);
|
||||
old_stack = stack_dup(stack);
|
||||
stack_push(&stack, NULL);
|
||||
|
||||
assert(cards_equal(stack->card, old_stack->card));
|
||||
assert(stacks_equal(stack->next, old_stack->next));
|
||||
|
||||
free_stack(stack);
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void test_pop_on_empty_stack() {
|
||||
void test_stack_pop_on_stack_empty_stack() {
|
||||
struct stack *stack;
|
||||
struct card *popped_card;
|
||||
struct card *stack_popped_card;
|
||||
|
||||
allocate_stack(&stack);
|
||||
initialize_stack(stack);
|
||||
popped_card = pop(&stack);
|
||||
stack_malloc(&stack);
|
||||
stack_init(stack);
|
||||
stack_popped_card = stack_pop(&stack);
|
||||
|
||||
assert(empty(stack));
|
||||
assert(!popped_card);
|
||||
assert(stack_empty(stack));
|
||||
assert(!stack_popped_card);
|
||||
|
||||
free_stack(stack);
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void test_pop_on_stack_with_one_element() {
|
||||
void test_stack_pop_on_stack_with_one_element() {
|
||||
struct stack *stack;
|
||||
struct card *card, *popped_card;
|
||||
struct card *card, *stack_popped_card;
|
||||
|
||||
allocate_card(&card);
|
||||
initialize_card(card);
|
||||
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||
card_malloc(&card);
|
||||
card_init(card);
|
||||
card_set(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||
|
||||
allocate_stack(&stack);
|
||||
initialize_stack(stack);
|
||||
push(&stack, card);
|
||||
popped_card = pop(&stack);
|
||||
stack_malloc(&stack);
|
||||
stack_init(stack);
|
||||
stack_push(&stack, card);
|
||||
stack_popped_card = stack_pop(&stack);
|
||||
|
||||
assert(empty(stack));
|
||||
assert(popped_card == card);
|
||||
assert(stack_empty(stack));
|
||||
assert(stack_popped_card == card);
|
||||
|
||||
free_stack(stack);
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void test_pop_on_stack_with_more_than_one_element() {
|
||||
void test_stack_pop_on_stack_with_more_than_one_element() {
|
||||
struct stack *stack, *old_stack_next;
|
||||
struct card *card[3], *popped_card;
|
||||
struct card *card[3], *stack_popped_card;
|
||||
|
||||
allocate_stack(&stack);
|
||||
initialize_stack(stack);
|
||||
stack_malloc(&stack);
|
||||
stack_init(stack);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
allocate_card(&card[i]);
|
||||
initialize_card(card[i]);
|
||||
set_card(card[i], ACE, SPADES, EXPOSED, 0, 0);
|
||||
push(&stack, card[i]);
|
||||
card_malloc(&card[i]);
|
||||
card_init(card[i]);
|
||||
card_set(card[i], ACE, SPADES, EXPOSED, 0, 0);
|
||||
stack_push(&stack, card[i]);
|
||||
}
|
||||
old_stack_next = stack->next;
|
||||
popped_card = pop(&stack);
|
||||
stack_popped_card = stack_pop(&stack);
|
||||
|
||||
assert(length(stack) == 2);
|
||||
assert(stack_length(stack) == 2);
|
||||
assert(stack == old_stack_next);
|
||||
assert(popped_card == card[2]);
|
||||
assert(stack_popped_card == card[2]);
|
||||
|
||||
free_stack(stack);
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void test_reverse_on_empty_stack() {
|
||||
struct stack *stack, *old_stack, *reversed_stack;
|
||||
void test_stack_reverse_on_stack_empty_stack() {
|
||||
struct stack *stack, *old_stack, *stack_reversed_stack;
|
||||
|
||||
allocate_stack(&stack);
|
||||
initialize_stack(stack);
|
||||
stack_malloc(&stack);
|
||||
stack_init(stack);
|
||||
old_stack = stack;
|
||||
reversed_stack = reverse(stack);
|
||||
stack_reversed_stack = stack_reverse(stack);
|
||||
|
||||
assert(reversed_stack == old_stack);
|
||||
assert(stack_reversed_stack == old_stack);
|
||||
|
||||
free_stack(stack);
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void test_reverse_on_stack_with_one_element() {
|
||||
struct stack *stack, *old_stack, *reversed_stack;
|
||||
void test_stack_reverse_on_stack_with_one_element() {
|
||||
struct stack *stack, *old_stack, *stack_reversed_stack;
|
||||
struct card *card;
|
||||
|
||||
allocate_card(&card);
|
||||
initialize_card(card);
|
||||
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||
card_malloc(&card);
|
||||
card_init(card);
|
||||
card_set(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||
|
||||
allocate_stack(&stack);
|
||||
initialize_stack(stack);
|
||||
push(&stack, card);
|
||||
stack_malloc(&stack);
|
||||
stack_init(stack);
|
||||
stack_push(&stack, card);
|
||||
old_stack = stack;
|
||||
reversed_stack = reverse(stack);
|
||||
stack_reversed_stack = stack_reverse(stack);
|
||||
|
||||
assert(reversed_stack == old_stack);
|
||||
assert(stack_reversed_stack == old_stack);
|
||||
|
||||
free_stack(stack);
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void test_reverse_on_stack_with_more_than_one_element() {
|
||||
struct stack *stack, *old_stack, *reversed_stack, *unreversed_stack;
|
||||
void test_stack_reverse_on_stack_with_more_than_one_element() {
|
||||
struct stack *stack, *old_stack, *stack_reversed_stack, *unstack_reversed_stack;
|
||||
struct card *card[3];
|
||||
|
||||
allocate_stack(&stack);
|
||||
initialize_stack(stack);
|
||||
stack_malloc(&stack);
|
||||
stack_init(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]);
|
||||
card_malloc(&card[i]);
|
||||
card_init(card[i]);
|
||||
card_set(card[i], TWO + i, DIAMONDS + i, EXPOSED, 0, 0);
|
||||
stack_push(&stack, card[i]);
|
||||
}
|
||||
old_stack = duplicate_stack(stack);
|
||||
reversed_stack = reverse(stack);
|
||||
old_stack = stack_dup(stack);
|
||||
stack_reversed_stack = stack_reverse(stack);
|
||||
|
||||
allocate_stack(&unreversed_stack);
|
||||
initialize_stack(unreversed_stack);
|
||||
stack_malloc(&unstack_reversed_stack);
|
||||
stack_init(unstack_reversed_stack);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
push(&unreversed_stack, pop(&reversed_stack));
|
||||
stack_push(&unstack_reversed_stack, stack_pop(&stack_reversed_stack));
|
||||
}
|
||||
|
||||
assert(stacks_equal(unreversed_stack, old_stack));
|
||||
assert(stacks_equal(unstack_reversed_stack, old_stack));
|
||||
|
||||
free_stack(reversed_stack);
|
||||
free_stack(stack);
|
||||
stack_free(stack_reversed_stack);
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void test_reverse_should_not_change_stack() {
|
||||
void test_stack_reverse_should_not_change_stack() {
|
||||
struct stack *stack, *old_stack, *old_stack_address;
|
||||
struct card *card[3];
|
||||
|
||||
allocate_stack(&stack);
|
||||
initialize_stack(stack);
|
||||
stack_malloc(&stack);
|
||||
stack_init(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]);
|
||||
card_malloc(&card[i]);
|
||||
card_init(card[i]);
|
||||
card_set(card[i], TWO + i, DIAMONDS + i, EXPOSED, 0, 0);
|
||||
stack_push(&stack, card[i]);
|
||||
}
|
||||
old_stack_address = stack;
|
||||
old_stack = duplicate_stack(stack);
|
||||
reverse(stack);
|
||||
old_stack = stack_dup(stack);
|
||||
stack_reverse(stack);
|
||||
|
||||
assert(stack == old_stack_address);
|
||||
assert(stacks_equal(stack, old_stack));
|
||||
|
||||
free_stack(stack);
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void test_stack() {
|
||||
test_initialize_stack();
|
||||
test_stack_init();
|
||||
|
||||
test_duplicate_stack();
|
||||
test_stack_dup();
|
||||
|
||||
test_empty_on_empty_stack();
|
||||
test_empty_on_non_empty_stack();
|
||||
test_stack_empty_on_stack_empty_stack();
|
||||
test_stack_empty_on_non_stack_empty_stack();
|
||||
|
||||
test_length();
|
||||
test_stack_length();
|
||||
|
||||
test_push_on_empty_stack();
|
||||
test_push_on_non_empty_stack();
|
||||
test_push_null_on_empty_stack();
|
||||
test_stack_push_on_stack_empty_stack();
|
||||
test_stack_push_on_non_stack_empty_stack();
|
||||
test_stack_push_null_on_stack_empty_stack();
|
||||
|
||||
test_pop_on_empty_stack();
|
||||
test_pop_on_stack_with_one_element();
|
||||
test_pop_on_stack_with_more_than_one_element();
|
||||
test_stack_pop_on_stack_empty_stack();
|
||||
test_stack_pop_on_stack_with_one_element();
|
||||
test_stack_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();
|
||||
test_reverse_should_not_change_stack();
|
||||
test_stack_reverse_on_stack_empty_stack();
|
||||
test_stack_reverse_on_stack_with_one_element();
|
||||
test_stack_reverse_on_stack_with_more_than_one_element();
|
||||
test_stack_reverse_should_not_change_stack();
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ void test_cards_equal_with_two_nulls() {
|
||||
void test_cards_equal_with_one_null() {
|
||||
struct card *card;
|
||||
|
||||
allocate_card(&card);
|
||||
card_malloc(&card);
|
||||
assert(!cards_equal(card, NULL));
|
||||
assert(!cards_equal(NULL, card));
|
||||
}
|
||||
@ -49,10 +49,10 @@ void test_cards_equal_with_two_equivalent_cards() {
|
||||
struct card *card_0, *card_1;
|
||||
const int begin_y = 5, begin_x = 10;
|
||||
|
||||
allocate_card(&card_0);
|
||||
allocate_card(&card_1);
|
||||
set_card(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||
set_card(card_1, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||
card_malloc(&card_0);
|
||||
card_malloc(&card_1);
|
||||
card_set(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||
card_set(card_1, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||
|
||||
assert(cards_equal(card_0, card_1));
|
||||
}
|
||||
@ -60,7 +60,7 @@ void test_cards_equal_with_two_equivalent_cards() {
|
||||
void test_cards_equal_with_two_card_pointers_to_the_same_address() {
|
||||
struct card *card;
|
||||
|
||||
allocate_card(&card);
|
||||
card_malloc(&card);
|
||||
|
||||
assert(cards_equal(card, card));
|
||||
}
|
||||
@ -72,7 +72,7 @@ void test_stacks_equal_with_two_nulls() {
|
||||
void test_stacks_equal_with_one_null() {
|
||||
struct stack *stack;
|
||||
|
||||
allocate_stack(&stack);
|
||||
stack_malloc(&stack);
|
||||
assert(!stacks_equal(stack, NULL));
|
||||
assert(!stacks_equal(NULL, stack));
|
||||
}
|
||||
@ -82,14 +82,14 @@ void test_stacks_equal_with_two_equivalent_stacks() {
|
||||
struct card *card_0, *card_1;
|
||||
const int begin_y = 5, begin_x = 10;
|
||||
|
||||
allocate_card(&card_0);
|
||||
allocate_card(&card_1);
|
||||
set_card(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||
set_card(card_1, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||
allocate_stack(&stack_0);
|
||||
allocate_stack(&stack_1);
|
||||
push(&stack_0, card_0);
|
||||
push(&stack_1, card_1);
|
||||
card_malloc(&card_0);
|
||||
card_malloc(&card_1);
|
||||
card_set(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||
card_set(card_1, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||
stack_malloc(&stack_0);
|
||||
stack_malloc(&stack_1);
|
||||
stack_push(&stack_0, card_0);
|
||||
stack_push(&stack_1, card_1);
|
||||
|
||||
assert(stacks_equal(stack_0, stack_1));
|
||||
}
|
||||
@ -99,14 +99,14 @@ void test_stacks_equal_with_two_different_stacks() {
|
||||
struct card *card_0, *card_1;
|
||||
const int begin_y = 5, begin_x = 10;
|
||||
|
||||
allocate_card(&card_0);
|
||||
allocate_card(&card_1);
|
||||
set_card(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||
set_card(card_1, KING, HEARTS, EXPOSED, begin_y, begin_x);
|
||||
allocate_stack(&stack_0);
|
||||
allocate_stack(&stack_1);
|
||||
push(&stack_0, card_0);
|
||||
push(&stack_1, card_1);
|
||||
card_malloc(&card_0);
|
||||
card_malloc(&card_1);
|
||||
card_set(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||
card_set(card_1, KING, HEARTS, EXPOSED, begin_y, begin_x);
|
||||
stack_malloc(&stack_0);
|
||||
stack_malloc(&stack_1);
|
||||
stack_push(&stack_0, card_0);
|
||||
stack_push(&stack_1, card_1);
|
||||
|
||||
assert(!stacks_equal(stack_0, stack_1));
|
||||
}
|
||||
@ -114,7 +114,7 @@ void test_stacks_equal_with_two_different_stacks() {
|
||||
void test_stacks_equal_with_two_stack_pointers_to_the_same_address() {
|
||||
struct stack *stack;
|
||||
|
||||
allocate_stack(&stack);
|
||||
stack_malloc(&stack);
|
||||
|
||||
assert(stacks_equal(stack, stack));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user