C-ifying project.

This commit is contained in:
Murilo Pereira
2011-05-31 03:03:13 -03:00
parent 57a38c7152
commit 1c63b767f6
38 changed files with 47 additions and 49 deletions

78
tests/card_test.c Normal file
View File

@@ -0,0 +1,78 @@
#include <assert.h>
#include "test_helper.h"
#include "../src/card.h"
void test_initialize_card() {
struct card *card;
allocate_card(&card);
initialize_card(card);
assert(card->value == NO_VALUE);
assert(card->suit == NO_SUIT);
assert(card->face == NO_FACE);
free_card(card);
}
void test_duplicate_card() {
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);
assert(card_0 != card_1);
assert(cards_equal(card_0, card_1));
}
void test_set_card() {
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);
assert(card->value == ACE);
assert(card->suit == SPADES);
assert(card->face == EXPOSED);
assert(card->frame->begin_y == begin_y);
assert(card->frame->begin_x == begin_x);
free_card(card);
}
void test_expose_card() {
struct card *card;
allocate_card(&card);
initialize_card(card);
expose_card(card);
assert(card->face == EXPOSED);
free_card(card);
}
void test_cover_card() {
struct card *card;
allocate_card(&card);
initialize_card(card);
cover_card(card);
assert(card->face == COVERED);
free_card(card);
}
void test_card() {
test_initialize_card();
test_duplicate_card();
test_set_card();
test_expose_card();
test_cover_card();
}

7
tests/curses_test.c Normal file
View File

@@ -0,0 +1,7 @@
#include <assert.h>
#include <stdbool.h>
#include "../src/curses.h"
void test_curses() {
assert(true);
}

7
tests/cursor_test.c Normal file
View File

@@ -0,0 +1,7 @@
#include <assert.h>
#include <stdbool.h>
#include "../src/cursor.h"
void test_cursor() {
assert(true);
}

7
tests/deck_test.c Normal file
View File

@@ -0,0 +1,7 @@
#include <assert.h>
#include <stdbool.h>
#include "../src/deck.h"
void test_deck() {
assert(true);
}

7
tests/display_test.c Normal file
View File

@@ -0,0 +1,7 @@
#include <assert.h>
#include <stdbool.h>
#include "../src/display.h"
void test_display() {
assert(true);
}

49
tests/frame_test.c Normal file
View File

@@ -0,0 +1,49 @@
#include <assert.h>
#include "test_helper.h"
#include "../src/frame.h"
void test_initialize_frame() {
struct frame *frame;
allocate_frame(&frame);
initialize_frame(frame);
assert(frame->window == NULL);
assert(frame->begin_y == 0);
assert(frame->begin_x == 0);
free_frame(frame);
}
void test_duplicate_frame() {
struct frame *frame_0, *frame_1;
const int begin_y = 5, begin_x = 10;
allocate_frame(&frame_0);
set_frame(frame_0, begin_y, begin_x);
frame_1 = duplicate_frame(frame_0);
assert(frame_0 != frame_1);
assert(frames_equal(frame_0, frame_1));
}
void test_set_frame() {
struct frame *frame;
int begin_y = 5;
int begin_x = 10;
allocate_frame(&frame);
initialize_frame(frame);
set_frame(frame, begin_y, begin_x);
assert(frame->begin_y == begin_y);
assert(frame->begin_x == begin_x);
free_frame(frame);
}
void test_frame() {
test_initialize_frame();
test_duplicate_frame();
test_set_frame();
}

562
tests/game_test.c Normal file
View File

@@ -0,0 +1,562 @@
#include <assert.h>
#include "../src/stack.h"
#include "../src/game.h"
#include "test_helper.h"
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);
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);
}
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);
assert(valid_move(stock, waste_pile));
free_stack(stock);
free_stack(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);
for (int i = 0; i < 4; i++) {
allocate_stack(&foundation_stacks[i]);
initialize_stack(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);
for (int i = 0; i < 4; i++) {
assert(!valid_move(stock, foundation_stacks[i]));
}
free_stack(stock);
for (int i = 0; i < 4; i++) {
free_stack(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);
for (int i = 0; i < 7; i++) {
allocate_stack(&maneuvre_stacks[i]);
initialize_stack(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);
for (int i = 0; i < 7; i++) {
assert(!valid_move(stock, maneuvre_stacks[i]));
}
free_stack(stock);
for (int i = 0; i < 7; i++) {
free_stack(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);
assert(!valid_move(waste_pile, stock));
free_stack(stock);
free_stack(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);
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);
}
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);
for (int i = 0; i < 4; i++) {
allocate_stack(&foundation_stacks[i]);
initialize_stack(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);
for (int i = 0; i < 4; i++) {
assert(valid_move(waste_pile, foundation_stacks[i]));
}
free_stack(waste_pile);
for (int i = 0; i < 4; i++) {
free_stack(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);
for (int i = 0; i < 7; i++) {
allocate_stack(&maneuvre_stacks[i]);
initialize_stack(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);
for (int i = 0; i < 7; i++) {
assert(valid_move(waste_pile, maneuvre_stacks[i]));
}
free_stack(waste_pile);
for (int i = 0; i < 7; i++) {
free_stack(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);
for (int i = 0; i < 4; i++) {
allocate_stack(&foundation_stacks[i]);
initialize_stack(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);
for (int i = 0; i < 4; i++) {
assert(!valid_move(foundation_stacks[i], stock));
}
free_stack(stock);
for (int i = 0; i < 4; i++) {
free_stack(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);
for (int i = 0; i < 4; i++) {
allocate_stack(&foundation_stacks[i]);
initialize_stack(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);
for (int i = 0; i < 4; i++) {
assert(!valid_move(foundation_stacks[i], waste_pile));
}
free_stack(waste_pile);
for (int i = 0; i < 4; i++) {
free_stack(foundation_stacks[i]);
}
}
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]);
}
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);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i == j) {
assert(!valid_move(foundation_stacks[i], foundation_stacks[j]));
} else {
assert(valid_move(foundation_stacks[i], foundation_stacks[j]));
}
}
}
for (int i = 0; i < 4; i++) {
free_stack(foundation_stacks[i]);
}
}
void test_valid_move_from_foundation_stack_to_maneuvre_stacks() {
struct stack *foundation_stacks[4];
struct stack *maneuvre_stacks[7];
for (int i = 0; i < 4; i++) {
allocate_stack(&foundation_stacks[i]);
initialize_stack(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);
for (int i = 0; i < 7; i++) {
allocate_stack(&maneuvre_stacks[i]);
initialize_stack(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);
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]);
}
for (int i = 0; i < 7; i++) {
free_stack(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);
for (int i = 0; i < 7; i++) {
allocate_stack(&maneuvre_stacks[i]);
initialize_stack(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);
for (int i = 0; i < 7; i++) {
assert(!valid_move(maneuvre_stacks[i], stock));
}
free_stack(stock);
for (int i = 0; i < 7; i++) {
free_stack(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);
for (int i = 0; i < 7; i++) {
allocate_stack(&maneuvre_stacks[i]);
initialize_stack(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);
for (int i = 0; i < 7; i++) {
assert(!valid_move(maneuvre_stacks[i], waste_pile));
}
free_stack(waste_pile);
for (int i = 0; i < 7; i++) {
free_stack(maneuvre_stacks[i]);
}
}
void test_valid_move_from_maneuvre_stack_to_foundation_stacks() {
struct stack *foundation_stacks[4];
struct stack *maneuvre_stacks[7];
for (int i = 0; i < 4; i++) {
allocate_stack(&foundation_stacks[i]);
initialize_stack(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);
for (int i = 0; i < 7; i++) {
allocate_stack(&maneuvre_stacks[i]);
initialize_stack(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);
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]);
}
for (int i = 0; i < 7; i++) {
free_stack(maneuvre_stacks[i]);
}
}
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]);
}
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);
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (i == j) {
assert(!valid_move(maneuvre_stacks[i], maneuvre_stacks[j]));
} else {
assert(valid_move(maneuvre_stacks[i], maneuvre_stacks[j]));
}
}
}
for (int i = 0; i < 7; i++) {
free_stack(maneuvre_stacks[i]);
}
}
void test_move_card_from_empty_stack_to_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);
new_origin = origin;
new_destination = destination;
origin_duplicate = duplicate_stack(origin);
destination_duplicate = duplicate_stack(destination);
move_card(&new_origin, &new_destination);
assert(origin == new_origin);
assert(stacks_equal(origin, origin_duplicate));
assert(destination == new_destination);
assert(stacks_equal(destination, destination_duplicate));
free_stack(origin);
free_stack(destination);
}
void test_move_card_from_empty_stack_to_non_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);
allocate_stack(&origin);
allocate_stack(&destination);
initialize_stack(origin);
initialize_stack(destination);
new_origin = origin;
new_destination = destination;
push(&new_destination, card);
origin_duplicate = duplicate_stack(origin);
destination_duplicate = duplicate_stack(destination);
move_card(&new_origin, &new_destination);
assert(origin == new_origin);
assert(stacks_equal(origin, origin_duplicate));
assert(destination == new_destination);
assert(stacks_equal(destination, destination_duplicate));
free_stack(origin);
free_stack(destination);
}
void test_move_card_from_non_empty_stack_to_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);
}
allocate_stack(&origin);
allocate_stack(&destination);
initialize_stack(origin);
initialize_stack(destination);
for (int i = 0; i < 6; i++) {
push(&origin, card[i]);
}
move_card(&origin, &destination);
assert(length(origin) == 5);
assert(length(destination) == 1);
assert(cards_equal(destination->card, card[5]));
move_card(&origin, &destination);
assert(length(origin) == 4);
assert(length(destination) == 2);
assert(cards_equal(destination->card, card[4]));
move_card(&origin, &destination);
assert(length(origin) == 3);
assert(length(destination) == 3);
assert(cards_equal(destination->card, card[3]));
free_stack(origin);
free_stack(destination);
}
void test_move_card_from_non_empty_stack_to_non_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);
}
allocate_stack(&origin);
allocate_stack(&destination);
initialize_stack(origin);
initialize_stack(destination);
for (int i = 0; i < 3; i++) {
push(&origin, card[i]);
}
for (int i = 3; i < 6; i++) {
push(&destination, card[i]);
}
move_card(&origin, &destination);
assert(length(origin) == 2);
assert(length(destination) == 4);
assert(cards_equal(destination->card, card[2]));
assert(cards_equal(destination->next->card, card[5]));
free_stack(origin);
free_stack(destination);
}
void test_move_card_should_not_change_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);
allocate_stack(&origin);
allocate_stack(&destination);
initialize_stack(origin);
initialize_stack(destination);
push(&origin, card[0]);
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);
}
void test_game() {
test_valid_move_from_stock_to_stock();
test_valid_move_from_stock_to_waste_pile();
test_valid_move_from_stock_to_foundation_stacks();
test_valid_move_from_stock_to_maneuvre_stacks();
test_valid_move_from_waste_pile_to_stock();
test_valid_move_from_waste_pile_to_waste_pile();
test_valid_move_from_waste_pile_to_foundation_stacks();
test_valid_move_from_waste_pile_to_maneuvre_stacks();
test_valid_move_from_foundation_stack_to_stock();
test_valid_move_from_foundation_stack_to_waste_pile();
test_valid_move_from_foundation_stack_to_foundation_stacks();
test_valid_move_from_foundation_stack_to_maneuvre_stacks();
test_valid_move_from_maneuvre_stack_to_stock();
test_valid_move_from_maneuvre_stack_to_waste_pile();
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();
}

7
tests/keyboard_test.c Normal file
View File

@@ -0,0 +1,7 @@
#include <assert.h>
#include <stdbool.h>
#include "../src/keyboard.h"
void test_keyboard() {
assert(true);
}

315
tests/stack_test.c Normal file
View File

@@ -0,0 +1,315 @@
#include <assert.h>
#include "test_helper.h"
#include "../src/stack.h"
void test_initialize_stack() {
struct stack *stack;
allocate_stack(&stack);
initialize_stack(stack);
assert(stack->card->value == NO_VALUE);
assert(!stack->next);
free_stack(stack);
}
void test_duplicate_stack() {
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);
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]);
}
stack_1 = duplicate_stack(stack_0);
assert(stack_0 != stack_1);
assert(stacks_equal(stack_0, stack_1));
}
void test_empty_on_empty_stack() {
struct stack *stack;
allocate_stack(&stack);
initialize_stack(stack);
assert(empty(stack));
free_stack(stack);
}
void test_empty_on_non_empty_stack() {
struct stack *stack;
struct card *card;
allocate_card(&card);
initialize_card(card);
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
allocate_stack(&stack);
initialize_stack(stack);
push(&stack, card);
assert(!empty(stack));
free_stack(stack);
}
void test_length() {
struct stack *stack;
struct card *card[4];
allocate_stack(&stack);
initialize_stack(stack);
assert(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);
}
free_stack(stack);
}
void test_push_on_empty_stack() {
struct stack *stack;
struct card *card;
allocate_card(&card);
initialize_card(card);
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
allocate_stack(&stack);
initialize_stack(stack);
push(&stack, card);
assert(cards_equal(stack->card, card));
assert(!stack->next);
free_stack(stack);
}
void test_push_on_non_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);
allocate_stack(&stack);
initialize_stack(stack);
push(&stack, card_0);
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);
}
void test_push_null_on_empty_stack() {
struct stack *stack, *old_stack;
allocate_stack(&stack);
initialize_stack(stack);
old_stack = stack;
push(&stack, NULL);
assert(cards_equal(stack->card, old_stack->card));
assert(!stack->next);
free_stack(stack);
}
void test_push_null_on_non_empty_stack() {
struct stack *stack, *old_stack;
struct card *card;
allocate_card(&card);
initialize_card(card);
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
allocate_stack(&stack);
initialize_stack(stack);
old_stack = duplicate_stack(stack);
push(&stack, NULL);
assert(cards_equal(stack->card, old_stack->card));
assert(stacks_equal(stack->next, old_stack->next));
free_stack(stack);
}
void test_pop_on_empty_stack() {
struct stack *stack;
struct card *popped_card;
allocate_stack(&stack);
initialize_stack(stack);
popped_card = pop(&stack);
assert(empty(stack));
assert(!popped_card);
free_stack(stack);
}
void test_pop_on_stack_with_one_element() {
struct stack *stack;
struct card *card, *popped_card;
allocate_card(&card);
initialize_card(card);
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
allocate_stack(&stack);
initialize_stack(stack);
push(&stack, card);
popped_card = pop(&stack);
assert(empty(stack));
assert(popped_card == card);
free_stack(stack);
}
void test_pop_on_stack_with_more_than_one_element() {
struct stack *stack, *old_stack_next;
struct card *card[3], *popped_card;
allocate_stack(&stack);
initialize_stack(stack);
for (int i = 0; i < 3; i++) {
allocate_card(&card[i]);
initialize_card(card[i]);
set_card(card[i], ACE, SPADES, EXPOSED, 0, 0);
push(&stack, card[i]);
}
old_stack_next = stack->next;
popped_card = pop(&stack);
assert(length(stack) == 2);
assert(stack == old_stack_next);
assert(popped_card == card[2]);
free_stack(stack);
}
void test_reverse_on_empty_stack() {
struct stack *stack, *old_stack, *reversed_stack;
allocate_stack(&stack);
initialize_stack(stack);
old_stack = stack;
reversed_stack = reverse(stack);
assert(reversed_stack == old_stack);
free_stack(stack);
}
void test_reverse_on_stack_with_one_element() {
struct stack *stack, *old_stack, *reversed_stack;
struct card *card;
allocate_card(&card);
initialize_card(card);
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
allocate_stack(&stack);
initialize_stack(stack);
push(&stack, card);
old_stack = stack;
reversed_stack = reverse(stack);
assert(reversed_stack == old_stack);
free_stack(stack);
}
void test_reverse_on_stack_with_more_than_one_element() {
struct stack *stack, *old_stack, *reversed_stack, *unreversed_stack;
struct card *card[3];
allocate_stack(&stack);
initialize_stack(stack);
for (int i = 0; i < 3; i++) {
allocate_card(&card[i]);
initialize_card(card[i]);
set_card(card[i], TWO + i, DIAMONDS + i, EXPOSED, 0, 0);
push(&stack, card[i]);
}
old_stack = duplicate_stack(stack);
reversed_stack = reverse(stack);
allocate_stack(&unreversed_stack);
initialize_stack(unreversed_stack);
for (int i = 0; i < 3; i++) {
push(&unreversed_stack, pop(&reversed_stack));
}
assert(stacks_equal(unreversed_stack, old_stack));
free_stack(reversed_stack);
free_stack(stack);
}
void test_reverse_should_not_change_stack() {
struct stack *stack, *old_stack, *old_stack_address;
struct card *card[3];
allocate_stack(&stack);
initialize_stack(stack);
for (int i = 0; i < 3; i++) {
allocate_card(&card[i]);
initialize_card(card[i]);
set_card(card[i], TWO + i, DIAMONDS + i, EXPOSED, 0, 0);
push(&stack, card[i]);
}
old_stack_address = stack;
old_stack = duplicate_stack(stack);
reverse(stack);
assert(stack == old_stack_address);
assert(stacks_equal(stack, old_stack));
free_stack(stack);
}
void test_stack() {
test_initialize_stack();
test_duplicate_stack();
test_empty_on_empty_stack();
test_empty_on_non_empty_stack();
test_length();
test_push_on_empty_stack();
test_push_on_non_empty_stack();
test_push_null_on_empty_stack();
test_pop_on_empty_stack();
test_pop_on_stack_with_one_element();
test_pop_on_stack_with_more_than_one_element();
test_reverse_on_empty_stack();
test_reverse_on_stack_with_one_element();
test_reverse_on_stack_with_more_than_one_element();
test_reverse_should_not_change_stack();
}

36
tests/test_helper.c Normal file
View File

@@ -0,0 +1,36 @@
#include "test_helper.h"
bool frames_equal(struct frame *frame_0, struct frame *frame_1) {
if (frame_0 && frame_1) {
return(frame_0->begin_y == frame_1->begin_y &&
frame_0->begin_x == frame_1->begin_x);
} else if ((frame_0 && !frame_1) || (!frame_0 && frame_1)) {
return(false);
} else {
return(true);
}
}
bool cards_equal(struct card *card_0, struct card *card_1) {
if (card_0 && card_1) {
return(frames_equal(card_0->frame, card_1->frame) &&
card_0->value == card_1->value &&
card_0->suit == card_1->suit &&
card_0->face == card_1->face);
} else if ((card_0 && !card_1) || (!card_0 && card_1)) {
return(false);
} else {
return(true);
}
}
bool stacks_equal(struct stack *stack_0, struct stack *stack_1) {
if (stack_0 && stack_1) {
return(cards_equal(stack_0->card, stack_1->card) &&
stacks_equal(stack_0->next, stack_1->next));
} else if ((stack_0 && !stack_1) || (!stack_0 && stack_1)) {
return(false);
} else {
return(true);
}
}

13
tests/test_helper.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef TEST_HELPER_H
#define TEST_HELPER_H
#include <stdbool.h>
#include "../src/frame.h"
#include "../src/card.h"
#include "../src/stack.h"
bool frames_equal(struct frame *, struct frame *);
bool cards_equal(struct card *, struct card *);
bool stacks_equal(struct stack *, struct stack *);
#endif

137
tests/test_helper_test.c Normal file
View File

@@ -0,0 +1,137 @@
#include <assert.h>
#include "test_helper.h"
void test_frames_equal_with_two_nulls() {
assert(frames_equal(NULL, NULL));
}
void test_frames_equal_with_one_null() {
struct frame *frame;
allocate_frame(&frame);
assert(!frames_equal(frame, NULL));
assert(!frames_equal(NULL, frame));
}
void test_frames_equal_with_two_equivalent_frames() {
struct frame *frame_0, *frame_1;
const int begin_y = 5, begin_x = 10;
allocate_frame(&frame_0);
allocate_frame(&frame_1);
set_frame(frame_0, begin_y, begin_x);
set_frame(frame_1, begin_y, begin_x);
assert(frames_equal(frame_0, frame_1));
}
void test_frames_equal_with_two_frame_pointers_to_the_same_address() {
struct frame *frame;
allocate_frame(&frame);
assert(frames_equal(frame, frame));
}
void test_cards_equal_with_two_nulls() {
assert(cards_equal(NULL, NULL));
}
void test_cards_equal_with_one_null() {
struct card *card;
allocate_card(&card);
assert(!cards_equal(card, NULL));
assert(!cards_equal(NULL, card));
}
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);
assert(cards_equal(card_0, card_1));
}
void test_cards_equal_with_two_card_pointers_to_the_same_address() {
struct card *card;
allocate_card(&card);
assert(cards_equal(card, card));
}
void test_stacks_equal_with_two_nulls() {
assert(stacks_equal(NULL, NULL));
}
void test_stacks_equal_with_one_null() {
struct stack *stack;
allocate_stack(&stack);
assert(!stacks_equal(stack, NULL));
assert(!stacks_equal(NULL, stack));
}
void test_stacks_equal_with_two_equivalent_stacks() {
struct stack *stack_0, *stack_1;
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);
assert(stacks_equal(stack_0, stack_1));
}
void test_stacks_equal_with_two_different_stacks() {
struct stack *stack_0, *stack_1;
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);
assert(!stacks_equal(stack_0, stack_1));
}
void test_stacks_equal_with_two_stack_pointers_to_the_same_address() {
struct stack *stack;
allocate_stack(&stack);
assert(stacks_equal(stack, stack));
}
void test_test_helper() {
test_frames_equal_with_two_nulls();
test_frames_equal_with_one_null();
test_frames_equal_with_two_equivalent_frames();
test_frames_equal_with_two_frame_pointers_to_the_same_address();
test_cards_equal_with_two_nulls();
test_cards_equal_with_two_equivalent_cards();
test_cards_equal_with_two_card_pointers_to_the_same_address();
test_stacks_equal_with_two_nulls();
test_stacks_equal_with_one_null();
test_stacks_equal_with_two_equivalent_stacks();
test_stacks_equal_with_two_different_stacks();
test_stacks_equal_with_two_stack_pointers_to_the_same_address();
}

20
tests/ttysolitaire_test.c Normal file
View File

@@ -0,0 +1,20 @@
#include "ttysolitaire_test.h"
const char *program_name;
int main(int argc, const char *argv[]) {
program_name = argv[0];
test_card();
test_cursor();
test_deck();
test_display();
test_frame();
test_game();
test_keyboard();
test_stack();
test_curses();
test_test_helper();
return(0);
}

15
tests/ttysolitaire_test.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef TTYSOLITAIRE_TEST_H
#define TTYSOLITAIRE_TEST_H
void test_card();
void test_cursor();
void test_deck();
void test_display();
void test_frame();
void test_game();
void test_keyboard();
void test_stack();
void test_curses();
void test_test_helper();
#endif