diff --git a/Makefile b/Makefile index b809a0e..a26b08b 100644 --- a/Makefile +++ b/Makefile @@ -5,34 +5,36 @@ LDFLAGS = -lncursesw EXECUTABLE = bin/ttysolitaire LIB_DIR = lib SRC = ${LIB_DIR}/ttysolitaire.c -LIB_OBJECTS = ${LIB_DIR}/frame.o \ - ${LIB_DIR}/card.o \ - ${LIB_DIR}/stack.o \ - ${LIB_DIR}/deck.o \ - ${LIB_DIR}/util.o \ - ${LIB_DIR}/cursor.o \ - ${LIB_DIR}/keyboard.o \ - ${LIB_DIR}/display.o \ - ${LIB_DIR}/game.o \ +LIB_OBJECTS = ${LIB_DIR}/frame.o \ + ${LIB_DIR}/card.o \ + ${LIB_DIR}/stack.o \ + ${LIB_DIR}/deck.o \ + ${LIB_DIR}/util.o \ + ${LIB_DIR}/cursor.o \ + ${LIB_DIR}/keyboard.o \ + ${LIB_DIR}/display.o \ + ${LIB_DIR}/game.o \ TEST_EXECUTABLE = bin/ttysolitaire_test TEST_DIR = test TEST_SRC = ${TEST_DIR}/ttysolitaire_test.c -TEST_OBJECTS = ${TEST_DIR}/frame_test.o \ - ${TEST_DIR}/card_test.o \ - ${TEST_DIR}/stack_test.o \ - ${TEST_DIR}/deck_test.o \ - ${TEST_DIR}/util_test.o \ - ${TEST_DIR}/cursor_test.o \ - ${TEST_DIR}/keyboard_test.o \ - ${TEST_DIR}/display_test.o \ - ${TEST_DIR}/game_test.o \ +TEST_OBJECTS = ${TEST_DIR}/frame_test.o \ + ${TEST_DIR}/card_test.o \ + ${TEST_DIR}/stack_test.o \ + ${TEST_DIR}/deck_test.o \ + ${TEST_DIR}/util_test.o \ + ${TEST_DIR}/cursor_test.o \ + ${TEST_DIR}/keyboard_test.o \ + ${TEST_DIR}/display_test.o \ + ${TEST_DIR}/game_test.o \ + ${TEST_DIR}/test_helper.o \ + ${TEST_DIR}/test_helper_test.o \ ttysolitaire: ${LIB_OBJECTS} ${CC} ${CFLAGS} ${LDFLAGS} ${SRC} -o ${EXECUTABLE} ${LIB_OBJECTS} test: ${LIB_OBJECTS} ${TEST_OBJECTS} - @${CC} ${CFLAGS} ${LDFLAGS} ${TEST_SRC} -o ${TEST_EXECUTABLE} ${TEST_OBJECTS} ${LIB_OBJECTS} 2> /dev/null + @${CC} ${CFLAGS} ${LDFLAGS} ${TEST_SRC} -o ${TEST_EXECUTABLE} ${TEST_OBJECTS} ${LIB_OBJECTS} @${TEST_EXECUTABLE} clean: diff --git a/test/test_helper.c b/test/test_helper.c new file mode 100644 index 0000000..072b9c2 --- /dev/null +++ b/test/test_helper.c @@ -0,0 +1,72 @@ +#include "test_helper.h" + +bool frames_equal(struct frame *frame_0, struct frame *frame_1) { + if (frame_0 && frame_1) { + return(frame_0->start_y == frame_1->start_y && + frame_0->start_x == frame_1->start_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); + } +} + +struct frame *duplicate_frame(struct frame *frame) { + struct frame *new_frame; + + allocate_frame(&new_frame); + set_frame(new_frame, frame->start_y, frame->start_x); + + return(new_frame); +} + +struct card *duplicate_card(struct card *card) { + struct card *new_card; + + allocate_card(&new_card); + set_card(new_card, + card->value, + card->suit, + card->face, + card->frame->start_y, + card->frame->start_x); + + return(new_card); +} + +struct stack *duplicate_stack(struct stack *stack) { + struct stack *iterator = stack; + struct stack *new_stack; + + allocate_stack(&new_stack); + initialize_stack(new_stack); + for (iterator = stack; iterator; iterator = iterator->next) { + push(&new_stack, duplicate_card(iterator->card)); + } + + return(new_stack); +} diff --git a/test/test_helper.h b/test/test_helper.h new file mode 100644 index 0000000..ca83500 --- /dev/null +++ b/test/test_helper.h @@ -0,0 +1,16 @@ +#ifndef TEST_HELPER_H +#define TEST_HELPER_H + +#include +#include "../lib/frame.h" +#include "../lib/card.h" +#include "../lib/stack.h" + +bool frames_equal(struct frame *, struct frame *); +bool cards_equal(struct card *, struct card *); +bool stacks_equal(struct stack *, struct stack *); +struct frame *duplicate_frame(struct frame *); +struct card *duplicate_card(struct card *); +struct stack *duplicate_stack(struct stack *); + +#endif diff --git a/test/test_helper_test.c b/test/test_helper_test.c new file mode 100644 index 0000000..6dc6b1b --- /dev/null +++ b/test/test_helper_test.c @@ -0,0 +1,226 @@ +#include +#include "test_helper.h" + +void test_frames_equal_with_two_nulls() { + assert(frames_equal(NULL, NULL)); + + return; +} + +void test_frames_equal_with_one_null() { + struct frame *frame; + + allocate_frame(&frame); + assert(!frames_equal(frame, NULL)); + assert(!frames_equal(NULL, frame)); + + return; +} + +void test_frames_equal_with_two_equivalent_frames() { + struct frame *frame_0, *frame_1; + const int start_y = 5, start_x = 10; + + allocate_frame(&frame_0); + allocate_frame(&frame_1); + set_frame(frame_0, start_y, start_x); + set_frame(frame_1, start_y, start_x); + + assert(frames_equal(frame_0, frame_1)); + + return; +} + +void test_frames_equal_with_two_frame_pointers_to_the_same_address() { + struct frame *frame; + + allocate_frame(&frame); + + assert(frames_equal(frame, frame)); + + return; +} + +void test_cards_equal_with_two_nulls() { + assert(cards_equal(NULL, NULL)); + + return; +} + +void test_cards_equal_with_one_null() { + struct card *card; + + allocate_card(&card); + assert(!cards_equal(card, NULL)); + assert(!cards_equal(NULL, card)); + + return; +} + +void test_cards_equal_with_two_equivalent_cards() { + struct card *card_0, *card_1; + const int start_y = 5, start_x = 10; + + allocate_card(&card_0); + allocate_card(&card_1); + set_card(card_0, + ACE, + SPADES, + EXPOSED, + start_y, + start_x); + set_card(card_1, + ACE, + SPADES, + EXPOSED, + start_y, + start_x); + + assert(cards_equal(card_0, card_1)); + + return; +} + +void test_cards_equal_with_two_card_pointers_to_the_same_address() { + struct card *card; + + allocate_card(&card); + + assert(cards_equal(card, card)); + + return; +} + +void test_stacks_equal_with_two_nulls() { + assert(stacks_equal(NULL, NULL)); + + return; +} + +void test_stacks_equal_with_one_null() { + struct stack *stack; + + allocate_stack(&stack); + assert(!stacks_equal(stack, NULL)); + assert(!stacks_equal(NULL, stack)); + + return; +} + +void test_stacks_equal_with_two_equivalent_stacks() { + struct stack *stack_0, *stack_1; + struct card *card_0, *card_1; + const int start_y = 5, start_x = 10; + + allocate_card(&card_0); + allocate_card(&card_1); + set_card(card_0, ACE, SPADES, EXPOSED, start_y, start_x); + set_card(card_1, ACE, SPADES, EXPOSED, start_y, start_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)); + + return; +} + +void test_stacks_equal_with_two_different_stacks() { + struct stack *stack_0, *stack_1; + struct card *card_0, *card_1; + const int start_y = 5, start_x = 10; + + allocate_card(&card_0); + allocate_card(&card_1); + set_card(card_0, ACE, SPADES, EXPOSED, start_y, start_x); + set_card(card_1, KING, HEARTS, EXPOSED, start_y, start_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)); + + return; +} + +void test_stacks_equal_with_two_stack_pointers_to_the_same_address() { + struct stack *stack; + + allocate_stack(&stack); + + assert(stacks_equal(stack, stack)); + + return; +} + +void test_duplicate_frame() { + struct frame *frame_0, *frame_1; + const int start_y = 5, start_x = 10; + + allocate_frame(&frame_0); + set_frame(frame_0, start_y, start_x); + frame_1 = duplicate_frame(frame_0); + + assert(frame_0 != frame_1); + assert(frames_equal(frame_0, frame_1)); + + return; +} + +void test_duplicate_card() { + struct card *card_0, *card_1; + const int start_y = 5, start_x = 10; + + allocate_card(&card_0); + set_card(card_0, ACE, SPADES, EXPOSED, start_y, start_x); + card_1 = duplicate_card(card_0); + + assert(card_0 != card_1); + assert(cards_equal(card_0, card_1)); + + return; +} + +void test_duplicate_stack() { + struct stack *stack_0, *stack_1; + struct card *card; + const int start_y = 5, start_x = 10; + + allocate_card(&card); + set_card(card, ACE, SPADES, EXPOSED, start_y, start_x); + allocate_stack(&stack_0); + initialize_stack(stack_0); + push(&stack_0, card); + stack_1 = duplicate_stack(stack_0); + + assert(stack_0 != stack_1); + assert(stacks_equal(stack_0, stack_1)); + + return; +} + + +void test_test_helper() { + test_frames_equal_with_two_nulls(); + test_frames_equal_with_one_null(); + 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(); + + test_duplicate_frame(); + test_duplicate_card(); + test_duplicate_stack(); + + return; +} diff --git a/test/ttysolitaire_test.c b/test/ttysolitaire_test.c index 74c769f..d262920 100644 --- a/test/ttysolitaire_test.c +++ b/test/ttysolitaire_test.c @@ -1,5 +1,3 @@ -#include -#include #include "ttysolitaire_test.h" const char *program_name; @@ -16,6 +14,7 @@ int main(int argc, const char *argv[]) { test_keyboard(); test_stack(); test_util(); + test_test_helper(); return(0); } diff --git a/test/ttysolitaire_test.h b/test/ttysolitaire_test.h index 3da2d43..66e0cdb 100644 --- a/test/ttysolitaire_test.h +++ b/test/ttysolitaire_test.h @@ -10,5 +10,6 @@ void test_game(); void test_keyboard(); void test_stack(); void test_util(); +void test_test_helper(); #endif