|
|
#include "../../unity/unity.h" |
|
|
#include <stdlib.h> |
|
|
#include <string.h> |
|
|
#include <stdio.h> |
|
|
#include <stdbool.h> |
|
|
|
|
|
|
|
|
void setUp(void) { |
|
|
|
|
|
} |
|
|
void tearDown(void) { |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static struct line* make_line_with(size_t nfields, size_t bufsize_bytes) |
|
|
{ |
|
|
struct line *ln = (struct line*) malloc(sizeof *ln); |
|
|
if (!ln) return NULL; |
|
|
memset(ln, 0, sizeof *ln); |
|
|
|
|
|
if (nfields > 0) { |
|
|
ln->fields = (struct field*) malloc(nfields * sizeof *ln->fields); |
|
|
if (!ln->fields) { |
|
|
free(ln); |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
for (size_t i = 0; i < nfields; i++) { |
|
|
ln->fields[i].beg = NULL; |
|
|
ln->fields[i].len = 0; |
|
|
} |
|
|
ln->nfields = (idx_t)nfields; |
|
|
ln->nfields_allocated = (idx_t)nfields; |
|
|
} else { |
|
|
ln->fields = NULL; |
|
|
ln->nfields = 0; |
|
|
ln->nfields_allocated = 0; |
|
|
} |
|
|
|
|
|
if (bufsize_bytes > 0) { |
|
|
ln->buf.buffer = (char*) malloc(bufsize_bytes); |
|
|
if (ln->buf.buffer) { |
|
|
memset(ln->buf.buffer, 'X', bufsize_bytes); |
|
|
ln->buf.length = (idx_t)bufsize_bytes; |
|
|
} else { |
|
|
ln->buf.buffer = NULL; |
|
|
ln->buf.length = 0; |
|
|
} |
|
|
} else { |
|
|
ln->buf.buffer = NULL; |
|
|
ln->buf.length = 0; |
|
|
} |
|
|
|
|
|
return ln; |
|
|
} |
|
|
|
|
|
|
|
|
static void test_delseq_empty_seq(void) |
|
|
{ |
|
|
struct seq s; |
|
|
s.count = 0; |
|
|
s.alloc = 0; |
|
|
s.lines = NULL; |
|
|
|
|
|
delseq(&s); |
|
|
|
|
|
TEST_ASSERT_EQUAL_UINT(0, (unsigned)s.alloc); |
|
|
TEST_ASSERT_NULL(s.lines); |
|
|
} |
|
|
|
|
|
|
|
|
static void test_delseq_null_entries_only(void) |
|
|
{ |
|
|
struct seq s; |
|
|
s.count = 0; |
|
|
s.alloc = 5; |
|
|
s.lines = (struct line**) calloc(s.alloc, sizeof *s.lines); |
|
|
TEST_ASSERT_NOT_NULL(s.lines); |
|
|
|
|
|
void *oldptr = s.lines; |
|
|
delseq(&s); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_UINT(5, (unsigned)s.alloc); |
|
|
TEST_ASSERT_EQUAL_PTR(oldptr, s.lines); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void test_delseq_mixed_entries(void) |
|
|
{ |
|
|
struct seq s; |
|
|
s.count = 0; |
|
|
s.alloc = 4; |
|
|
s.lines = (struct line**) malloc(s.alloc * sizeof *s.lines); |
|
|
TEST_ASSERT_NOT_NULL(s.lines); |
|
|
|
|
|
s.lines[0] = make_line_with(0, 0); |
|
|
s.lines[1] = NULL; |
|
|
s.lines[2] = make_line_with(3, 64); |
|
|
s.lines[3] = make_line_with(0, 128); |
|
|
|
|
|
TEST_ASSERT_NOT_NULL(s.lines[0]); |
|
|
TEST_ASSERT_NULL(s.lines[1]); |
|
|
TEST_ASSERT_NOT_NULL(s.lines[2]); |
|
|
TEST_ASSERT_NOT_NULL(s.lines[3]); |
|
|
|
|
|
void *oldptr = s.lines; |
|
|
delseq(&s); |
|
|
|
|
|
TEST_ASSERT_EQUAL_UINT(4, (unsigned)s.alloc); |
|
|
TEST_ASSERT_EQUAL_PTR(oldptr, s.lines); |
|
|
} |
|
|
|
|
|
|
|
|
static void test_delseq_large_array(void) |
|
|
{ |
|
|
const size_t N = 1000; |
|
|
struct seq s; |
|
|
s.count = 0; |
|
|
s.alloc = (idx_t)N; |
|
|
s.lines = (struct line**) malloc(N * sizeof *s.lines); |
|
|
TEST_ASSERT_NOT_NULL(s.lines); |
|
|
|
|
|
|
|
|
for (size_t i = 0; i < N; i++) { |
|
|
if (i % 10 == 0) { |
|
|
s.lines[i] = make_line_with(2, 32); |
|
|
TEST_ASSERT_NOT_NULL_MESSAGE(s.lines[i], "Allocation failed in setup"); |
|
|
} else { |
|
|
s.lines[i] = NULL; |
|
|
} |
|
|
} |
|
|
|
|
|
void *oldptr = s.lines; |
|
|
delseq(&s); |
|
|
|
|
|
TEST_ASSERT_EQUAL_UINT(N, (unsigned)s.alloc); |
|
|
TEST_ASSERT_EQUAL_PTR(oldptr, s.lines); |
|
|
} |
|
|
|
|
|
int main(void) |
|
|
{ |
|
|
UNITY_BEGIN(); |
|
|
|
|
|
RUN_TEST(test_delseq_empty_seq); |
|
|
RUN_TEST(test_delseq_null_entries_only); |
|
|
RUN_TEST(test_delseq_mixed_entries); |
|
|
RUN_TEST(test_delseq_large_array); |
|
|
|
|
|
return UNITY_END(); |
|
|
} |