Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix make test (Issue #14) #35

Merged
merged 3 commits into from
May 31, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix all memory leaks reported by valgrind
flamin committed Jan 21, 2019
commit 1aa7e560f1fe89908c701f0c63e026a80ab0faf5
1 change: 1 addition & 0 deletions src/game.c
Original file line number Diff line number Diff line change
@@ -162,6 +162,7 @@ static void shuffle_deck(struct deck *deck) {
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
stack_push(&(deck->stock), card[i]);
}
free(card);
}

static void deal_cards(struct deck *deck) {
1 change: 1 addition & 0 deletions src/stack.c
Original file line number Diff line number Diff line change
@@ -69,6 +69,7 @@ int stack_length(struct stack *stack) {
void stack_push(struct stack **stack, struct card *card) {
if (card) {
if (stack_empty(*stack)) {
card_free((*stack)->card);
(*stack)->card = card;
} else {
/* Allocating by hand because stack_malloc() would
3 changes: 3 additions & 0 deletions tests/card_test.c
Original file line number Diff line number Diff line change
@@ -25,6 +25,9 @@ void test_card_dup() {

assert(card_0 != card_1);
assert(cards_equal(card_0, card_1));

card_free(card_0);
card_free(card_1);
}

void test_card_set() {
3 changes: 3 additions & 0 deletions tests/frame_test.c
Original file line number Diff line number Diff line change
@@ -25,6 +25,9 @@ void test_frame_dup() {

assert(frame_0 != frame_1);
assert(frames_equal(frame_0, frame_1));

frame_free(frame_0);
frame_free(frame_1);
}

void test_frame_set() {
4 changes: 4 additions & 0 deletions tests/game_test.c
Original file line number Diff line number Diff line change
@@ -406,6 +406,8 @@ void test_move_card_from_stack_empty_stack_to_stack_empty_stack() {
assert(destination == new_destination);
assert(stacks_equal(destination, destination_duplicate));

stack_free(origin_duplicate);
stack_free(destination_duplicate);
stack_free(origin);
stack_free(destination);
}
@@ -436,6 +438,8 @@ void test_move_card_from_stack_empty_stack_to_non_stack_empty_stack() {
assert(destination == new_destination);
assert(stacks_equal(destination, destination_duplicate));

stack_free(origin_duplicate);
stack_free(destination_duplicate);
stack_free(origin);
stack_free(destination);
}
22 changes: 16 additions & 6 deletions tests/stack_test.c
Original file line number Diff line number Diff line change
@@ -30,6 +30,9 @@ void test_stack_dup() {

assert(stack_0 != stack_1);
assert(stacks_equal(stack_0, stack_1));

stack_free(stack_0);
stack_free(stack_1);
}

void test_stack_empty_on_stack_empty_stack() {
@@ -185,6 +188,7 @@ void test_stack_pop_on_stack_with_one_element() {
assert(stack_popped_card == card);

stack_free(stack);
card_free(stack_popped_card);
}

void test_stack_pop_on_stack_with_more_than_one_element() {
@@ -207,6 +211,7 @@ void test_stack_pop_on_stack_with_more_than_one_element() {
assert(stack_popped_card == card[2]);

stack_free(stack);
card_free(stack_popped_card);
}

void test_stack_reverse_on_stack_empty_stack() {
@@ -220,6 +225,7 @@ void test_stack_reverse_on_stack_empty_stack() {
assert(stacks_equal(stack_reversed_stack, old_stack));

stack_free(stack);
stack_free(stack_reversed_stack);
}

void test_stack_reverse_on_stack_with_one_element() {
@@ -238,6 +244,7 @@ void test_stack_reverse_on_stack_with_one_element() {

assert(stacks_equal(stack_reversed_stack, old_stack));

stack_free(stack_reversed_stack);
stack_free(stack);
}

@@ -264,12 +271,14 @@ void test_stack_reverse_on_stack_with_more_than_one_element() {

assert(stacks_equal(unstack_reversed_stack, old_stack));

stack_free(unstack_reversed_stack);
stack_free(stack_reversed_stack);
stack_free(old_stack);
stack_free(stack);
}

void test_stack_reverse_should_not_change_stack() {
struct stack *stack, *old_stack, *old_stack_address;
struct stack *stack, *stack_reversed_stack_0, *stack_reversed_stack_1;
struct card *card[3];

stack_malloc(&stack);
@@ -280,13 +289,14 @@ void test_stack_reverse_should_not_change_stack() {
card_set(card[i], TWO + i, DIAMONDS + i, EXPOSED, 0, 0);
stack_push(&stack, card[i]);
}
old_stack_address = stack;
old_stack = stack_dup(stack);
stack_reverse(stack);
stack_reversed_stack_0 = stack_reverse(stack);
stack_reversed_stack_1 = stack_reverse(stack_reversed_stack_0);

assert(stack == old_stack_address);
assert(stacks_equal(stack, old_stack));
assert(!stacks_equal(stack, stack_reversed_stack_0));
assert(stacks_equal(stack, stack_reversed_stack_1));

stack_free(stack_reversed_stack_0);
stack_free(stack_reversed_stack_1);
stack_free(stack);
}

31 changes: 31 additions & 0 deletions tests/test_helper_test.c
Original file line number Diff line number Diff line change
@@ -11,6 +11,8 @@ void test_frames_equal_with_one_null() {
frame_malloc(&frame);
assert(!frames_equal(frame, NULL));
assert(!frames_equal(NULL, frame));

frame_free(frame);
}

void test_frames_equal_with_two_equivalent_frames() {
@@ -23,14 +25,20 @@ void test_frames_equal_with_two_equivalent_frames() {
frame_set(frame_1, begin_y, begin_x);

assert(frames_equal(frame_0, frame_1));

frame_free(frame_0);
frame_free(frame_1);
}

void test_frames_equal_with_two_frame_pointers_to_the_same_address() {
struct frame *frame;

frame_malloc(&frame);
frame_init(frame);

assert(frames_equal(frame, frame));

frame_free(frame);
}

void test_cards_equal_with_two_nulls() {
@@ -55,14 +63,20 @@ void test_cards_equal_with_two_equivalent_cards() {
card_set(card_1, ACE, SPADES, EXPOSED, begin_y, begin_x);

assert(cards_equal(card_0, card_1));

card_free(card_0);
card_free(card_1);
}

void test_cards_equal_with_two_card_pointers_to_the_same_address() {
struct card *card;

card_malloc(&card);
card_init(card);

assert(cards_equal(card, card));

card_free(card);
}

void test_stacks_equal_with_two_nulls() {
@@ -73,8 +87,12 @@ void test_stacks_equal_with_one_null() {
struct stack *stack;

stack_malloc(&stack);
stack_init(stack);

assert(!stacks_equal(stack, NULL));
assert(!stacks_equal(NULL, stack));

stack_free(stack);
}

void test_stacks_equal_with_two_equivalent_stacks() {
@@ -88,10 +106,15 @@ void test_stacks_equal_with_two_equivalent_stacks() {
card_set(card_1, ACE, SPADES, EXPOSED, begin_y, begin_x);
stack_malloc(&stack_0);
stack_malloc(&stack_1);
stack_init(stack_0);
stack_init(stack_1);
stack_push(&stack_0, card_0);
stack_push(&stack_1, card_1);

assert(stacks_equal(stack_0, stack_1));

stack_free(stack_0);
stack_free(stack_1);
}

void test_stacks_equal_with_two_different_stacks() {
@@ -105,18 +128,26 @@ void test_stacks_equal_with_two_different_stacks() {
card_set(card_1, KING, HEARTS, EXPOSED, begin_y, begin_x);
stack_malloc(&stack_0);
stack_malloc(&stack_1);
stack_init(stack_0);
stack_init(stack_1);
stack_push(&stack_0, card_0);
stack_push(&stack_1, card_1);

assert(!stacks_equal(stack_0, stack_1));

stack_free(stack_0);
stack_free(stack_1);
}

void test_stacks_equal_with_two_stack_pointers_to_the_same_address() {
struct stack *stack;

stack_malloc(&stack);
stack_init(stack);

assert(stacks_equal(stack, stack));

stack_free(stack);
}

void test_test_helper() {