|
|
#include "../../unity/unity.h" |
|
|
#include <stdlib.h> |
|
|
#include <string.h> |
|
|
#include <stdio.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static struct buffer_record* helper_make_buffer(size_t n_nodes, int alloc_buf) |
|
|
{ |
|
|
struct buffer_record *buf = (struct buffer_record*)malloc(sizeof *buf); |
|
|
TEST_ASSERT_NOT_NULL_MESSAGE(buf, "Failed to allocate buffer_record"); |
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof *buf); |
|
|
|
|
|
buf->bytes_alloc = 0; |
|
|
buf->bytes_used = 0; |
|
|
buf->start_line = 0; |
|
|
buf->first_available = 0; |
|
|
buf->num_lines = 0; |
|
|
buf->line_start = NULL; |
|
|
buf->curr_line = NULL; |
|
|
buf->next = NULL; |
|
|
|
|
|
if (alloc_buf) { |
|
|
buf->buffer = (char*)malloc(128); |
|
|
TEST_ASSERT_NOT_NULL_MESSAGE(buf->buffer, "Failed to allocate buf->buffer"); |
|
|
memset(buf->buffer, 'A', 127); |
|
|
buf->buffer[127] = '\0'; |
|
|
buf->bytes_alloc = 128; |
|
|
buf->bytes_used = 64; |
|
|
} else { |
|
|
buf->buffer = NULL; |
|
|
} |
|
|
|
|
|
struct line *head = NULL; |
|
|
struct line *prev = NULL; |
|
|
for (size_t i = 0; i < n_nodes; i++) { |
|
|
struct line *node = (struct line*)malloc(sizeof *node); |
|
|
TEST_ASSERT_NOT_NULL_MESSAGE(node, "Failed to allocate line node"); |
|
|
memset(node, 0, sizeof *node); |
|
|
node->used = 0; |
|
|
node->insert_index = 0; |
|
|
node->retrieve_index = 0; |
|
|
node->next = NULL; |
|
|
if (!head) head = node; |
|
|
if (prev) prev->next = node; |
|
|
prev = node; |
|
|
} |
|
|
buf->line_start = head; |
|
|
buf->curr_line = (head && head->next) ? head->next : head; |
|
|
|
|
|
return buf; |
|
|
} |
|
|
|
|
|
void setUp(void) { |
|
|
|
|
|
} |
|
|
|
|
|
void tearDown(void) { |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
void test_free_buffer_null_buffer_no_lines(void) |
|
|
{ |
|
|
struct buffer_record *buf = helper_make_buffer(0, 0); |
|
|
|
|
|
free_buffer(buf); |
|
|
|
|
|
|
|
|
TEST_ASSERT_TRUE(1); |
|
|
} |
|
|
|
|
|
|
|
|
void test_free_buffer_single_line_node_with_buffer(void) |
|
|
{ |
|
|
struct buffer_record *buf = helper_make_buffer(1, 1); |
|
|
free_buffer(buf); |
|
|
TEST_ASSERT_TRUE(1); |
|
|
} |
|
|
|
|
|
|
|
|
void test_free_buffer_long_chain_many_nodes(void) |
|
|
{ |
|
|
struct buffer_record *buf = helper_make_buffer(1000, 1); |
|
|
free_buffer(buf); |
|
|
TEST_ASSERT_TRUE(1); |
|
|
} |
|
|
|
|
|
|
|
|
void test_free_buffer_curr_line_not_head_fields_populated(void) |
|
|
{ |
|
|
struct buffer_record *buf = helper_make_buffer(5, 1); |
|
|
|
|
|
buf->bytes_alloc = 1024; |
|
|
buf->bytes_used = 512; |
|
|
buf->start_line = 10; |
|
|
buf->first_available = 12; |
|
|
buf->num_lines = 5; |
|
|
|
|
|
|
|
|
if (buf->line_start && buf->line_start->next) { |
|
|
buf->curr_line = buf->line_start->next->next ? buf->line_start->next->next : buf->line_start->next; |
|
|
} |
|
|
|
|
|
free_buffer(buf); |
|
|
TEST_ASSERT_TRUE(1); |
|
|
} |
|
|
|
|
|
int main(void) |
|
|
{ |
|
|
UNITY_BEGIN(); |
|
|
RUN_TEST(test_free_buffer_null_buffer_no_lines); |
|
|
RUN_TEST(test_free_buffer_single_line_node_with_buffer); |
|
|
RUN_TEST(test_free_buffer_long_chain_many_nodes); |
|
|
RUN_TEST(test_free_buffer_curr_line_not_head_fields_populated); |
|
|
return UNITY_END(); |
|
|
} |