From f24ec48b27d4cb8dd0555840206dedbb45f1423c Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 24 Oct 2026 01:17:50 +0200 Subject: Add a unit test harness and a first set of tests make check builds tests/runner from the modules that link without the io subsystem and runs it. --- tests/t_iovec.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 tests/t_iovec.c (limited to 'tests/t_iovec.c') diff --git a/tests/t_iovec.c b/tests/t_iovec.c new file mode 100644 index 0000000..89f3fda --- /dev/null +++ b/tests/t_iovec.c @@ -0,0 +1,124 @@ +/* This software was written by Dirk Engling + It is considered beerware. Prost. Skol. Cheers or whatever. + + $id$ */ + +/* System */ +#include +#include +#include + +/* Opentracker */ +#include "ot_iovec.h" +#include "test.h" + +static void case_increase_and_length(void) { + size_t entries = 0; + struct iovec *vector = NULL; + void *first, *second; + + test_case("increase appends an entry and length is their sum"); + first = iovec_increase(&entries, &vector, 100); + CHECK(first != NULL); + CHECK_INT(entries, 1); + CHECK_INT(vector[0].iov_len, 100); + CHECK_PTR(vector[0].iov_base, first); + + second = iovec_increase(&entries, &vector, 50); + CHECK(second != NULL); + CHECK_INT(entries, 2); + CHECK_INT(vector[1].iov_len, 50); + CHECK_INT(iovec_length(&entries, (const struct iovec **)&vector), 150); + + iovec_free(&entries, &vector); +} + +static void case_free_resets(void) { + size_t entries = 0; + struct iovec *vector = NULL; + + test_case("free releases the entries and resets the vector"); + CHECK(iovec_increase(&entries, &vector, 10) != NULL); + CHECK(iovec_increase(&entries, &vector, 10) != NULL); + iovec_free(&entries, &vector); + CHECK_INT(entries, 0); + CHECK_PTR(vector, NULL); + CHECK_INT(iovec_length(&entries, (const struct iovec **)&vector), 0); +} + +static void case_fixlast_shrinks(void) { + size_t entries = 0; + struct iovec *vector = NULL; + char *block; + + test_case("fixlast trims the last entry to the bytes actually used"); + CHECK(iovec_increase(&entries, &vector, 64) != NULL); + block = iovec_increase(&entries, &vector, 1000); + CHECK(block != NULL); + memset(block, 'x', 20); + + iovec_fixlast(&entries, &vector, block + 20); + CHECK_INT(entries, 2); + CHECK_INT(vector[1].iov_len, 20); + CHECK_INT(iovec_length(&entries, (const struct iovec **)&vector), 84); + CHECK_MEM(vector[1].iov_base, "xxxxxxxxxxxxxxxxxxxx", 20); + + iovec_free(&entries, &vector); +} + +static void case_fixlast_on_empty(void) { + size_t entries = 0; + struct iovec *vector = NULL; + + test_case("fixlast on an empty vector does nothing"); + iovec_fixlast(&entries, &vector, NULL); + CHECK_INT(entries, 0); + CHECK_PTR(vector, NULL); +} + +static void case_append_takes_ownership(void) { + size_t entries = 0; + struct iovec *vector = NULL; + struct iovec source; + + test_case("append moves the buffer and clears the source"); + source.iov_base = malloc(10); + CHECK(source.iov_base != NULL); + memcpy(source.iov_base, "0123456789", 10); + source.iov_len = 10; + + CHECK(iovec_append(&entries, &vector, &source) != NULL); + CHECK_INT(entries, 1); + CHECK_INT(vector[0].iov_len, 10); + CHECK_MEM(vector[0].iov_base, "0123456789", 10); + CHECK_PTR(source.iov_base, NULL); + CHECK_INT(source.iov_len, 0); + + iovec_free(&entries, &vector); +} + +static void case_fix_increase(void) { + size_t entries = 0; + struct iovec *vector = NULL; + char *block; + + test_case("fix_increase trims the previous entry and adds a new one"); + block = iovec_increase(&entries, &vector, 500); + CHECK(block != NULL); + block = iovec_fix_increase_or_free(&entries, &vector, block + 30, 200); + CHECK(block != NULL); + CHECK_INT(entries, 2); + CHECK_INT(vector[0].iov_len, 30); + CHECK_INT(vector[1].iov_len, 200); + + iovec_free(&entries, &vector); +} + +void test_iovec(void) { + case_increase_and_length(); + case_free_resets(); + case_fixlast_shrinks(); + case_fixlast_on_empty(); + case_append_takes_ownership(); + case_fix_increase(); +} -- cgit v1.3