Fix all memory leaks reported by valgrind
This commit is contained in:
parent
69bf25f87e
commit
1aa7e560f1
@ -162,6 +162,7 @@ static void shuffle_deck(struct deck *deck) {
|
|||||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||||
stack_push(&(deck->stock), card[i]);
|
stack_push(&(deck->stock), card[i]);
|
||||||
}
|
}
|
||||||
|
free(card);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void deal_cards(struct deck *deck) {
|
static void deal_cards(struct deck *deck) {
|
||||||
|
@ -69,6 +69,7 @@ int stack_length(struct stack *stack) {
|
|||||||
void stack_push(struct stack **stack, struct card *card) {
|
void stack_push(struct stack **stack, struct card *card) {
|
||||||
if (card) {
|
if (card) {
|
||||||
if (stack_empty(*stack)) {
|
if (stack_empty(*stack)) {
|
||||||
|
card_free((*stack)->card);
|
||||||
(*stack)->card = card;
|
(*stack)->card = card;
|
||||||
} else {
|
} else {
|
||||||
/* Allocating by hand because stack_malloc() would
|
/* Allocating by hand because stack_malloc() would
|
||||||
|
@ -25,6 +25,9 @@ void test_card_dup() {
|
|||||||
|
|
||||||
assert(card_0 != card_1);
|
assert(card_0 != card_1);
|
||||||
assert(cards_equal(card_0, card_1));
|
assert(cards_equal(card_0, card_1));
|
||||||
|
|
||||||
|
card_free(card_0);
|
||||||
|
card_free(card_1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_card_set() {
|
void test_card_set() {
|
||||||
|
@ -25,6 +25,9 @@ void test_frame_dup() {
|
|||||||
|
|
||||||
assert(frame_0 != frame_1);
|
assert(frame_0 != frame_1);
|
||||||
assert(frames_equal(frame_0, frame_1));
|
assert(frames_equal(frame_0, frame_1));
|
||||||
|
|
||||||
|
frame_free(frame_0);
|
||||||
|
frame_free(frame_1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_frame_set() {
|
void test_frame_set() {
|
||||||
|
@ -406,6 +406,8 @@ void test_move_card_from_stack_empty_stack_to_stack_empty_stack() {
|
|||||||
assert(destination == new_destination);
|
assert(destination == new_destination);
|
||||||
assert(stacks_equal(destination, destination_duplicate));
|
assert(stacks_equal(destination, destination_duplicate));
|
||||||
|
|
||||||
|
stack_free(origin_duplicate);
|
||||||
|
stack_free(destination_duplicate);
|
||||||
stack_free(origin);
|
stack_free(origin);
|
||||||
stack_free(destination);
|
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(destination == new_destination);
|
||||||
assert(stacks_equal(destination, destination_duplicate));
|
assert(stacks_equal(destination, destination_duplicate));
|
||||||
|
|
||||||
|
stack_free(origin_duplicate);
|
||||||
|
stack_free(destination_duplicate);
|
||||||
stack_free(origin);
|
stack_free(origin);
|
||||||
stack_free(destination);
|
stack_free(destination);
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,9 @@ void test_stack_dup() {
|
|||||||
|
|
||||||
assert(stack_0 != stack_1);
|
assert(stack_0 != stack_1);
|
||||||
assert(stacks_equal(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() {
|
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);
|
assert(stack_popped_card == card);
|
||||||
|
|
||||||
stack_free(stack);
|
stack_free(stack);
|
||||||
|
card_free(stack_popped_card);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_stack_pop_on_stack_with_more_than_one_element() {
|
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]);
|
assert(stack_popped_card == card[2]);
|
||||||
|
|
||||||
stack_free(stack);
|
stack_free(stack);
|
||||||
|
card_free(stack_popped_card);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_stack_reverse_on_stack_empty_stack() {
|
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));
|
assert(stacks_equal(stack_reversed_stack, old_stack));
|
||||||
|
|
||||||
stack_free(stack);
|
stack_free(stack);
|
||||||
|
stack_free(stack_reversed_stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_stack_reverse_on_stack_with_one_element() {
|
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));
|
assert(stacks_equal(stack_reversed_stack, old_stack));
|
||||||
|
|
||||||
|
stack_free(stack_reversed_stack);
|
||||||
stack_free(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));
|
assert(stacks_equal(unstack_reversed_stack, old_stack));
|
||||||
|
|
||||||
|
stack_free(unstack_reversed_stack);
|
||||||
stack_free(stack_reversed_stack);
|
stack_free(stack_reversed_stack);
|
||||||
|
stack_free(old_stack);
|
||||||
stack_free(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_stack_reverse_should_not_change_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];
|
struct card *card[3];
|
||||||
|
|
||||||
stack_malloc(&stack);
|
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);
|
card_set(card[i], TWO + i, DIAMONDS + i, EXPOSED, 0, 0);
|
||||||
stack_push(&stack, card[i]);
|
stack_push(&stack, card[i]);
|
||||||
}
|
}
|
||||||
old_stack_address = stack;
|
stack_reversed_stack_0 = stack_reverse(stack);
|
||||||
old_stack = stack_dup(stack);
|
stack_reversed_stack_1 = stack_reverse(stack_reversed_stack_0);
|
||||||
stack_reverse(stack);
|
|
||||||
|
|
||||||
assert(stack == old_stack_address);
|
assert(!stacks_equal(stack, stack_reversed_stack_0));
|
||||||
assert(stacks_equal(stack, old_stack));
|
assert(stacks_equal(stack, stack_reversed_stack_1));
|
||||||
|
|
||||||
|
stack_free(stack_reversed_stack_0);
|
||||||
|
stack_free(stack_reversed_stack_1);
|
||||||
stack_free(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@ void test_frames_equal_with_one_null() {
|
|||||||
frame_malloc(&frame);
|
frame_malloc(&frame);
|
||||||
assert(!frames_equal(frame, NULL));
|
assert(!frames_equal(frame, NULL));
|
||||||
assert(!frames_equal(NULL, frame));
|
assert(!frames_equal(NULL, frame));
|
||||||
|
|
||||||
|
frame_free(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_frames_equal_with_two_equivalent_frames() {
|
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);
|
frame_set(frame_1, begin_y, begin_x);
|
||||||
|
|
||||||
assert(frames_equal(frame_0, frame_1));
|
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() {
|
void test_frames_equal_with_two_frame_pointers_to_the_same_address() {
|
||||||
struct frame *frame;
|
struct frame *frame;
|
||||||
|
|
||||||
frame_malloc(&frame);
|
frame_malloc(&frame);
|
||||||
|
frame_init(frame);
|
||||||
|
|
||||||
assert(frames_equal(frame, frame));
|
assert(frames_equal(frame, frame));
|
||||||
|
|
||||||
|
frame_free(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_cards_equal_with_two_nulls() {
|
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);
|
card_set(card_1, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||||
|
|
||||||
assert(cards_equal(card_0, card_1));
|
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() {
|
void test_cards_equal_with_two_card_pointers_to_the_same_address() {
|
||||||
struct card *card;
|
struct card *card;
|
||||||
|
|
||||||
card_malloc(&card);
|
card_malloc(&card);
|
||||||
|
card_init(card);
|
||||||
|
|
||||||
assert(cards_equal(card, card));
|
assert(cards_equal(card, card));
|
||||||
|
|
||||||
|
card_free(card);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_stacks_equal_with_two_nulls() {
|
void test_stacks_equal_with_two_nulls() {
|
||||||
@ -73,8 +87,12 @@ void test_stacks_equal_with_one_null() {
|
|||||||
struct stack *stack;
|
struct stack *stack;
|
||||||
|
|
||||||
stack_malloc(&stack);
|
stack_malloc(&stack);
|
||||||
|
stack_init(stack);
|
||||||
|
|
||||||
assert(!stacks_equal(stack, NULL));
|
assert(!stacks_equal(stack, NULL));
|
||||||
assert(!stacks_equal(NULL, stack));
|
assert(!stacks_equal(NULL, stack));
|
||||||
|
|
||||||
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_stacks_equal_with_two_equivalent_stacks() {
|
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);
|
card_set(card_1, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||||
stack_malloc(&stack_0);
|
stack_malloc(&stack_0);
|
||||||
stack_malloc(&stack_1);
|
stack_malloc(&stack_1);
|
||||||
|
stack_init(stack_0);
|
||||||
|
stack_init(stack_1);
|
||||||
stack_push(&stack_0, card_0);
|
stack_push(&stack_0, card_0);
|
||||||
stack_push(&stack_1, card_1);
|
stack_push(&stack_1, card_1);
|
||||||
|
|
||||||
assert(stacks_equal(stack_0, stack_1));
|
assert(stacks_equal(stack_0, stack_1));
|
||||||
|
|
||||||
|
stack_free(stack_0);
|
||||||
|
stack_free(stack_1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_stacks_equal_with_two_different_stacks() {
|
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);
|
card_set(card_1, KING, HEARTS, EXPOSED, begin_y, begin_x);
|
||||||
stack_malloc(&stack_0);
|
stack_malloc(&stack_0);
|
||||||
stack_malloc(&stack_1);
|
stack_malloc(&stack_1);
|
||||||
|
stack_init(stack_0);
|
||||||
|
stack_init(stack_1);
|
||||||
stack_push(&stack_0, card_0);
|
stack_push(&stack_0, card_0);
|
||||||
stack_push(&stack_1, card_1);
|
stack_push(&stack_1, card_1);
|
||||||
|
|
||||||
assert(!stacks_equal(stack_0, stack_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() {
|
void test_stacks_equal_with_two_stack_pointers_to_the_same_address() {
|
||||||
struct stack *stack;
|
struct stack *stack;
|
||||||
|
|
||||||
stack_malloc(&stack);
|
stack_malloc(&stack);
|
||||||
|
stack_init(stack);
|
||||||
|
|
||||||
assert(stacks_equal(stack, stack));
|
assert(stacks_equal(stack, stack));
|
||||||
|
|
||||||
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_test_helper() {
|
void test_test_helper() {
|
||||||
|
Loading…
Reference in New Issue
Block a user