tests: distinct structs for read-only or read/write stream buffers
This commit is contained in:
parent
de5bc9c6f4
commit
0e526a6ec7
@ -19,13 +19,13 @@
|
||||
|
||||
static gmio_bool_t gmio_stream_buffer_at_end(void* cookie)
|
||||
{
|
||||
const gmio_stream_buffer_t* buff = (const gmio_stream_buffer_t*)cookie;
|
||||
const gmio_ro_buffer_t* buff = (const gmio_ro_buffer_t*)cookie;
|
||||
return buff->pos >= buff->len;
|
||||
}
|
||||
|
||||
static int gmio_stream_buffer_error(void* cookie)
|
||||
{
|
||||
const gmio_stream_buffer_t* buff = (const gmio_stream_buffer_t*)cookie;
|
||||
const gmio_ro_buffer_t* buff = (const gmio_ro_buffer_t*)cookie;
|
||||
return buff == NULL || buff->pos > buff->len;
|
||||
}
|
||||
|
||||
@ -33,15 +33,14 @@ static size_t gmio_stream_buffer_read(
|
||||
void* cookie, void* ptr, size_t item_size, size_t item_count)
|
||||
{
|
||||
if (item_size > 0 && item_count > 0) {
|
||||
gmio_stream_buffer_t* buff = (gmio_stream_buffer_t*)cookie;
|
||||
const void* buff_ptr =
|
||||
buff->readonly_ptr != NULL ?
|
||||
buff->readonly_ptr : buff->readwrite_ptr;
|
||||
gmio_ro_buffer_t* buff = (gmio_ro_buffer_t*)cookie;
|
||||
const void* buff_ptr = buff->ptr;
|
||||
const size_t buff_remaining_size = buff->len - buff->pos;
|
||||
const size_t wanted_read_size = item_size * item_count;
|
||||
const size_t next_read_size =
|
||||
wanted_read_size <= buff_remaining_size ?
|
||||
wanted_read_size : buff_remaining_size;
|
||||
wanted_read_size :
|
||||
buff_remaining_size;
|
||||
const size_t next_read_item_count = next_read_size / item_size;
|
||||
|
||||
memcpy(ptr,
|
||||
@ -59,15 +58,16 @@ static size_t gmio_stream_buffer_write(
|
||||
void* cookie, const void* ptr, size_t item_size, size_t item_count)
|
||||
{
|
||||
if (item_size > 0 && item_count > 0) {
|
||||
gmio_stream_buffer_t* buff = (gmio_stream_buffer_t*)cookie;
|
||||
gmio_rw_buffer_t* buff = (gmio_rw_buffer_t*)cookie;
|
||||
const size_t buff_remaining_size = buff->len - buff->pos;
|
||||
const size_t wanted_write_size = item_size * item_count;
|
||||
const size_t next_write_size =
|
||||
wanted_write_size <= buff_remaining_size ?
|
||||
wanted_write_size : buff_remaining_size;
|
||||
wanted_write_size :
|
||||
buff_remaining_size;
|
||||
const size_t next_write_item_count = next_write_size / item_size;
|
||||
|
||||
memcpy((char*)buff->readwrite_ptr + buff->pos,
|
||||
memcpy((char*)buff->ptr + buff->pos,
|
||||
ptr,
|
||||
next_write_item_count * item_size);
|
||||
buff->pos += next_write_item_count * item_size;
|
||||
@ -80,32 +80,40 @@ static size_t gmio_stream_buffer_write(
|
||||
|
||||
static gmio_streamsize_t gmio_stream_buffer_size(void* cookie)
|
||||
{
|
||||
const gmio_stream_buffer_t* buff = (const gmio_stream_buffer_t*)cookie;
|
||||
const gmio_ro_buffer_t* buff = (const gmio_ro_buffer_t*)cookie;
|
||||
return buff->len;
|
||||
}
|
||||
|
||||
static int gmio_stream_buffer_get_pos(void* cookie, gmio_stream_pos_t* pos)
|
||||
{
|
||||
gmio_stream_buffer_t* buff = (gmio_stream_buffer_t*)cookie;
|
||||
gmio_ro_buffer_t* buff = (gmio_ro_buffer_t*)cookie;
|
||||
memcpy(&pos->cookie[0], &buff->pos, sizeof(size_t));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int gmio_stream_buffer_set_pos(void* cookie, const gmio_stream_pos_t* pos)
|
||||
{
|
||||
gmio_stream_buffer_t* buff = (gmio_stream_buffer_t*)cookie;
|
||||
gmio_ro_buffer_t* buff = (gmio_ro_buffer_t*)cookie;
|
||||
memcpy(&buff->pos, &pos->cookie[0], sizeof(size_t));
|
||||
return 0;
|
||||
}
|
||||
|
||||
void gmio_stream_set_buffer(gmio_stream_t *stream, gmio_stream_buffer_t* buff)
|
||||
gmio_stream_t gmio_istream_buffer(gmio_ro_buffer_t* buff)
|
||||
{
|
||||
stream->cookie = buff;
|
||||
stream->func_at_end = gmio_stream_buffer_at_end;
|
||||
stream->func_error = gmio_stream_buffer_error;
|
||||
stream->func_read = gmio_stream_buffer_read;
|
||||
stream->func_write = gmio_stream_buffer_write;
|
||||
stream->func_size = gmio_stream_buffer_size;
|
||||
stream->func_get_pos = gmio_stream_buffer_get_pos;
|
||||
stream->func_set_pos = gmio_stream_buffer_set_pos;
|
||||
gmio_stream_t stream = {0};
|
||||
stream.cookie = buff;
|
||||
stream.func_at_end = gmio_stream_buffer_at_end;
|
||||
stream.func_error = gmio_stream_buffer_error;
|
||||
stream.func_read = gmio_stream_buffer_read;
|
||||
stream.func_size = gmio_stream_buffer_size;
|
||||
stream.func_get_pos = gmio_stream_buffer_get_pos;
|
||||
stream.func_set_pos = gmio_stream_buffer_set_pos;
|
||||
return stream;
|
||||
}
|
||||
|
||||
gmio_stream_t gmio_stream_buffer(gmio_rw_buffer_t* buff)
|
||||
{
|
||||
gmio_stream_t stream = gmio_istream_buffer((gmio_ro_buffer_t*)buff);
|
||||
stream.func_write = gmio_stream_buffer_write;
|
||||
return stream;
|
||||
}
|
||||
|
@ -18,15 +18,25 @@
|
||||
|
||||
#include "../src/gmio_core/stream.h"
|
||||
|
||||
struct gmio_stream_buffer
|
||||
/* Read-only buffer */
|
||||
struct gmio_ro_buffer
|
||||
{
|
||||
const void* readonly_ptr;
|
||||
void* readwrite_ptr;
|
||||
const void* ptr;
|
||||
size_t len;
|
||||
size_t pos;
|
||||
};
|
||||
typedef struct gmio_stream_buffer gmio_stream_buffer_t;
|
||||
typedef struct gmio_ro_buffer gmio_ro_buffer_t;
|
||||
|
||||
void gmio_stream_set_buffer(gmio_stream_t* stream, gmio_stream_buffer_t* buff);
|
||||
/* Read/write buffer */
|
||||
struct gmio_rw_buffer
|
||||
{
|
||||
void* ptr;
|
||||
size_t len;
|
||||
size_t pos;
|
||||
};
|
||||
typedef struct gmio_rw_buffer gmio_rw_buffer_t;
|
||||
|
||||
gmio_stream_t gmio_istream_buffer(gmio_ro_buffer_t* buff);
|
||||
gmio_stream_t gmio_iostream_buffer(gmio_rw_buffer_t* buff);
|
||||
|
||||
#endif /* GMIO_STREAM_BUFFER_H */
|
||||
|
@ -124,22 +124,21 @@ const char* test_internal__fast_atof()
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char test_internal__gmio_fast_atof__fstr[] = "1234.567E05";
|
||||
const char* test_internal__gmio_fast_atof()
|
||||
{
|
||||
const char fstr[] = "1234.567E05";
|
||||
const float f1 = fast_atof(fstr);
|
||||
const float f1 = fast_atof(test_internal__gmio_fast_atof__fstr);
|
||||
|
||||
{
|
||||
char strbuff[2048] = {0};
|
||||
gmio_stringstream_t it = {0};
|
||||
gmio_stream_buffer_t streambuff = {0};
|
||||
gmio_stream_t stream = {0};
|
||||
gmio_ro_buffer_t streambuff = {
|
||||
&test_internal__gmio_fast_atof__fstr[0],
|
||||
sizeof(test_internal__gmio_fast_atof__fstr) - 1,
|
||||
0 };
|
||||
gmio_stream_t stream = gmio_istream_buffer(&streambuff);
|
||||
float f2;
|
||||
|
||||
streambuff.readonly_ptr = &fstr[0];
|
||||
streambuff.len = sizeof(fstr) - 1;
|
||||
gmio_stream_set_buffer(&stream, &streambuff);
|
||||
|
||||
it.stream = &stream;
|
||||
it.strbuff.ptr = &strbuff[0];
|
||||
it.strbuff.max_len = sizeof(strbuff) - 1;
|
||||
@ -176,16 +175,19 @@ const char* test_internal__safe_cast()
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char test_internal__stringstream__text[] =
|
||||
"Une citation,\to je crois qu'elle est de moi :"
|
||||
"Parfois le chemin est rude.\n"
|
||||
"pi : 3.1415926535897932384626433832795";
|
||||
|
||||
const char* test_internal__stringstream()
|
||||
{
|
||||
const char text[] =
|
||||
"Une citation,\to je crois qu'elle est de moi :"
|
||||
"Parfois le chemin est rude.\n"
|
||||
"pi : 3.1415926535897932384626433832795";
|
||||
|
||||
{
|
||||
gmio_stream_buffer_t buff = {0};
|
||||
gmio_stream_t stream = {0};
|
||||
gmio_ro_buffer_t buff = {
|
||||
&test_internal__stringstream__text[0],
|
||||
sizeof(test_internal__stringstream__text) - 1,
|
||||
0 };
|
||||
gmio_stream_t stream = gmio_istream_buffer(&buff);
|
||||
|
||||
char small_fwd_it_str[4];
|
||||
char fwd_it_str[32];
|
||||
@ -194,11 +196,6 @@ const char* test_internal__stringstream()
|
||||
char copy_str[128];
|
||||
gmio_string_t copy_strbuff;
|
||||
|
||||
buff.readonly_ptr = text;
|
||||
buff.len = strlen(text);
|
||||
buff.pos = 0;
|
||||
gmio_stream_set_buffer(&stream, &buff);
|
||||
|
||||
fwd_it.stream = &stream;
|
||||
fwd_it.strbuff.ptr = fwd_it_str;
|
||||
fwd_it.strbuff.max_len = sizeof(fwd_it_str);
|
||||
@ -246,8 +243,11 @@ const char* test_internal__stringstream()
|
||||
}
|
||||
|
||||
{
|
||||
gmio_stream_buffer_t buff = {0};
|
||||
gmio_stream_t stream = {0};
|
||||
gmio_ro_buffer_t buff = {
|
||||
&test_internal__stringstream__text[0],
|
||||
sizeof(test_internal__stringstream__text) - 1,
|
||||
0 };
|
||||
gmio_stream_t stream = gmio_istream_buffer(&buff);
|
||||
|
||||
char fwd_it_str[32];
|
||||
gmio_stringstream_t fwd_it = {0};
|
||||
@ -255,11 +255,6 @@ const char* test_internal__stringstream()
|
||||
char copy_str[128];
|
||||
gmio_string_t copy_strbuff;
|
||||
|
||||
buff.readonly_ptr = text;
|
||||
buff.len = strlen(text);
|
||||
buff.pos = 0;
|
||||
gmio_stream_set_buffer(&stream, &buff);
|
||||
|
||||
fwd_it.stream = &stream;
|
||||
fwd_it.strbuff.ptr = fwd_it_str;
|
||||
fwd_it.strbuff.max_len = sizeof(fwd_it_str);
|
||||
|
Loading…
Reference in New Issue
Block a user