Implement test IO fixtures
This commit is contained in:
parent
d7fbb0b4f0
commit
8fda313a29
@ -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
6
test/CMakeLists.txt
Normal 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
22
test/include/test_io.h
Normal 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
35
test/test_io.c
Normal 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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user