diff --git a/CMakeLists.txt b/CMakeLists.txt index c806aaa..44fd113 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..d720f59 --- /dev/null +++ b/test/CMakeLists.txt @@ -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) diff --git a/test/include/test_io.h b/test/include/test_io.h new file mode 100644 index 0000000..dd11cbd --- /dev/null +++ b/test/include/test_io.h @@ -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 diff --git a/test/test_io.c b/test/test_io.c new file mode 100644 index 0000000..f125254 --- /dev/null +++ b/test/test_io.c @@ -0,0 +1,35 @@ +#include "test_io.h" + +#include + +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; +}