star-buffer

Load 1D arrays in binary format
git clone git://git.meso-star.fr/star-buffer.git
Log | Files | Refs | README | LICENSE

commit 520534192f697a9ee4baa749f892f2487321b215
parent 55d6dcdd77b4f3119a5b1303feb4d86f56b9c35f
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Fri, 25 Mar 2022 17:14:31 +0100

Test the sbuf loading

Diffstat:
Mcmake/CMakeLists.txt | 2+-
Mdoc/sbuf.5.scd | 6+++---
Msrc/sbuf.c | 4++--
Asrc/test_sbuf_load.c | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 123 insertions(+), 6 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -82,7 +82,7 @@ if(NOT NO_TEST) endfunction() new_test(test_sbuf) - #new_test(test_sbuf_load) + new_test(test_sbuf_load) endif() ################################################################################ diff --git a/doc/sbuf.5.scd b/doc/sbuf.5.scd @@ -25,15 +25,15 @@ Data are encoded with respect to the little endian bytes ordering, i.e. least significant bytes are stored first. ``` -<sprp> ::= <pagesize> <count> <size> <alignment> +<sbuf> ::= <pagesize> <count> <szelmt> <alelmt> <padding> <list> <padding> <pagesize> ::= UINT64 <count> ::= UINT64 -<size> ::= UINT64 -<alignment> ::= UINT64 # less than <pagesize> +<szelmt> ::= UINT64 +<alelmt> ::= UINT64 # less than <pagesize> --- diff --git a/src/sbuf.c b/src/sbuf.c @@ -128,7 +128,7 @@ load_stream(struct sbuf* sbuf, FILE* stream, const char* stream_name) res = RES_BAD_ARG; goto error; } - if(!IS_POW2(!sbuf->alelmt)) { + if(!IS_POW2(sbuf->alelmt)) { log_err(sbuf, "%s: invalid element alignment `%lu'. It must be a power of 2.\n", stream_name, (unsigned long)sbuf->alelmt); @@ -148,7 +148,7 @@ load_stream(struct sbuf* sbuf, FILE* stream, const char* stream_name) sbuf->pitch = ALIGN_SIZE(sbuf->szelmt, sbuf->alelmt); /* Compute the length in bytes of the data to map */ - sbuf->map_len = sbuf->count * sbuf->pitch * sbuf->count; + sbuf->map_len = sbuf->count * sbuf->pitch; sbuf->map_len = ALIGN_SIZE(sbuf->map_len, (size_t)sbuf->pagesize); /* Find the offsets of the data into the stream */ diff --git a/src/test_sbuf_load.c b/src/test_sbuf_load.c @@ -0,0 +1,117 @@ +/* Copyright (C) 2022 |Méso|Star> (contact@meso-star.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "sbuf.h" +#include <rsys/mem_allocator.h> + +struct my_type { + int16_t i16; + float f32; +}; + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static void +check_sbuf_desc(const struct sbuf_desc* desc, const uint64_t count) +{ + size_t i; + CHK(desc); + + CHK(desc->buffer != NULL); + CHK(desc->count == count); + CHK(desc->szelmt == sizeof(struct my_type)); + CHK(desc->alelmt == ALIGNOF(struct my_type)); + + FOR_EACH(i, 0, count) { + const struct my_type* elmt = sbuf_desc_at(desc, i); + CHK(elmt->i16 == (int16_t)i); + CHK(elmt->f32 == (float)i/100.f); + } +} + +static void +test_load(struct sbuf* buf) +{ + struct sbuf_desc desc = SBUF_DESC_NULL; + FILE* fp = NULL; + const char* filename = "test_file.sbuf"; + const uint64_t pagesize = 16384; + const uint64_t count = 287; + uint64_t szelmt; + uint64_t alelmt; + size_t i; + char byte = 0; + + fp = fopen(filename, "w+"); + CHK(fp); + + /* Write file header */ + szelmt = sizeof(struct my_type); + alelmt = ALIGNOF(struct my_type); + CHK(fwrite(&pagesize, sizeof(pagesize), 1, fp) == 1); + CHK(fwrite(&count, sizeof(count), 1, fp) == 1); + CHK(fwrite(&szelmt, sizeof(szelmt), 1, fp) == 1); + CHK(fwrite(&alelmt, sizeof(alelmt), 1, fp) == 1); + + /* Padding */ + CHK(fseek(fp, (long)ALIGN_SIZE((size_t)ftell(fp), pagesize), SEEK_SET) == 0); + + /* Write the buffer data */ + FOR_EACH(i, 0, count) { + struct my_type elmt = {0}; + elmt.i16 = (int16_t)i; + elmt.f32 = (float)i/100.f; + CHK(fwrite(&elmt, sizeof(elmt), 1, fp) == 1); + } + + /* Padding. Write one char to position the EOF indicator */ + CHK(fseek(fp, (long)ALIGN_SIZE((size_t)ftell(fp), pagesize)-1, SEEK_SET) == 0); + CHK(fwrite(&byte, sizeof(byte), 1, fp) == 1); + CHK(fflush(fp) == 0); + + rewind(fp); + CHK(sbuf_load_stream(NULL, fp, filename) == RES_BAD_ARG); + CHK(sbuf_load_stream(buf, NULL, filename) == RES_BAD_ARG); + CHK(sbuf_load_stream(buf, fp, NULL) == RES_OK); + CHK(sbuf_get_desc(NULL, &desc) == RES_BAD_ARG); + CHK(sbuf_get_desc(buf, NULL) == RES_BAD_ARG); + CHK(sbuf_get_desc(buf, &desc) == RES_OK); + check_sbuf_desc(&desc, count); + + fclose(fp); +} + +/******************************************************************************* + * Main function + ******************************************************************************/ +int +main(int argc, char** argv) +{ + struct sbuf_create_args args = SBUF_CREATE_ARGS_DEFAULT; + struct sbuf* sbuf = NULL; + (void)argc, (void)argv; + + args.verbose = 1; + CHK(sbuf_create(&args, &sbuf) == RES_OK); + + test_load(sbuf); /* Tetrahedra */ + /*test_load_fail(smsh); + test_load_files(smsh, argc, argv);*/ + + CHK(sbuf_ref_put(sbuf) == RES_OK); + CHK(mem_allocated_size() == 0); + return 0; +}