Implement test IO fixtures

This commit is contained in:
Daniel Thorpe 2024-06-20 14:52:52 +01:00
parent d7fbb0b4f0
commit 8fda313a29
No known key found for this signature in database
4 changed files with 64 additions and 0 deletions

View File

@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.13)
project(gemhadar LANGUAGES C)
add_subdirectory(lib)
add_subdirectory(test)
add_executable(gemhadar
"main.c"

6
test/CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
add_library(test_lib
"test_io.c"
)
target_include_directories(test_lib PUBLIC "include")
set_property(TARGET test_lib PROPERTY C_STANDARD 11)
target_link_libraries(test_lib gemhadar_lib)

22
test/include/test_io.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef TEST_IO_H
#define TEST_IO_H
#include "io_abs.h"
struct test_reader_config {
const uint8_t *buf;
unsigned len;
unsigned pos;
};
void test_reader(struct test_reader_config *config, struct io_reader *reader_out);
struct test_writer_config {
uint8_t *buf;
unsigned len;
unsigned pos;
};
void test_writer(struct test_writer_config *config, struct io_writer *writer_out);
#endif

35
test/test_io.c Normal file
View File

@ -0,0 +1,35 @@
#include "test_io.h"
#include <string.h>
static ssize_t test_reader_read(void *ctx, uint8_t *buf, size_t len)
{
struct test_reader_config *config = (struct test_reader_config *)ctx;
const unsigned available = config->len - config->pos;
const unsigned to_read = len <= available ? len : available;
memcpy(buf, &config->buf[config->pos], to_read);
config->pos += to_read;
return to_read;
}
void test_reader(struct test_reader_config *config, struct io_reader *reader_out)
{
reader_out->ctx = config;
reader_out->read = test_reader_read;
}
static ssize_t test_writer_write(void *ctx, const uint8_t *buf, size_t len)
{
struct test_writer_config *config = (struct test_writer_config *)ctx;
const unsigned max = config->len - config->pos;
const unsigned to_write = len <= max ? len : max;
memcpy(&config->buf[config->pos], buf, to_write);
config->pos += to_write;
return to_write;
}
void test_writer(struct test_writer_config *config, struct io_writer *writer_out)
{
writer_out->ctx = config;
writer_out->write = test_writer_write;
}