Added helper functions for the unit tests.

* Also added tests for the helper functions
This commit is contained in:
Murilo Pereira 2011-02-13 17:12:45 -02:00
parent 259f0dc536
commit 422a866e10
6 changed files with 337 additions and 21 deletions

View File

@ -27,12 +27,14 @@ TEST_OBJECTS = ${TEST_DIR}/frame_test.o \
${TEST_DIR}/keyboard_test.o \ ${TEST_DIR}/keyboard_test.o \
${TEST_DIR}/display_test.o \ ${TEST_DIR}/display_test.o \
${TEST_DIR}/game_test.o \ ${TEST_DIR}/game_test.o \
${TEST_DIR}/test_helper.o \
${TEST_DIR}/test_helper_test.o \
ttysolitaire: ${LIB_OBJECTS} ttysolitaire: ${LIB_OBJECTS}
${CC} ${CFLAGS} ${LDFLAGS} ${SRC} -o ${EXECUTABLE} ${LIB_OBJECTS} ${CC} ${CFLAGS} ${LDFLAGS} ${SRC} -o ${EXECUTABLE} ${LIB_OBJECTS}
test: ${LIB_OBJECTS} ${TEST_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} @${TEST_EXECUTABLE}
clean: clean:

72
test/test_helper.c Normal file
View File

@ -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);
}

16
test/test_helper.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef TEST_HELPER_H
#define TEST_HELPER_H
#include <stdbool.h>
#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

226
test/test_helper_test.c Normal file
View File

@ -0,0 +1,226 @@
#include <assert.h>
#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;
}

View File

@ -1,5 +1,3 @@
#include <assert.h>
#include <stdbool.h>
#include "ttysolitaire_test.h" #include "ttysolitaire_test.h"
const char *program_name; const char *program_name;
@ -16,6 +14,7 @@ int main(int argc, const char *argv[]) {
test_keyboard(); test_keyboard();
test_stack(); test_stack();
test_util(); test_util();
test_test_helper();
return(0); return(0);
} }

View File

@ -10,5 +10,6 @@ void test_game();
void test_keyboard(); void test_keyboard();
void test_stack(); void test_stack();
void test_util(); void test_util();
void test_test_helper();
#endif #endif