commit 3bb31d7cb9712a1bf4a22bbb0ba5b36c6ebc91c9
parent 520534192f697a9ee4baa749f892f2487321b215
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 25 Mar 2022 19:21:24 +0100
Push load tests further
Diffstat:
| M | src/test_sbuf_load.c | | | 122 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- |
1 file changed, 101 insertions(+), 21 deletions(-)
diff --git a/src/test_sbuf_load.c b/src/test_sbuf_load.c
@@ -15,36 +15,96 @@
#include "sbuf.h"
#include <rsys/mem_allocator.h>
+#include <string.h>
-struct my_type {
+struct type_desc {
+ void (*set)(void* data, const size_t i);
+ int (*eq)(const void* a, const void* b);
+
+ size_t size;
+ size_t alignment;
+};
+
+/*******************************************************************************
+ * i16_f32 type
+ ******************************************************************************/
+struct i16_f32 {
int16_t i16;
float f32;
};
+static void
+i16_f32_set(void* data, const size_t i)
+{
+ struct i16_f32* t = data;
+ CHK(t);
+ t->i16 = (int16_t)i;
+ t->f32 = (float)i/100.f;
+}
+
+static int
+i16_f32_eq(const void* a, const void* b)
+{
+ const struct i16_f32* t0 = a;
+ const struct i16_f32* t1 = b;
+ CHK(t0 && t1);
+ return t0->i16 == t1->i16 && t0->f32 == t1->f32;
+}
+
+/*******************************************************************************
+ * char[7]
+ ******************************************************************************/
+typedef char (char7_T) [7];
+
+static void
+char7_set(void* data, const size_t i)
+{
+ char* str = data;
+ CHK(i < 1000);
+ sprintf(str, "%3dabc", (int)i);
+}
+
+static int
+char7_eq(const void* a, const void* b)
+{
+ const char* s0 = a;
+ const char* s1 = b;
+ CHK(s0 && s1);
+ return strcmp(s0, s1) == 0;
+}
+
/*******************************************************************************
* Helper functions
******************************************************************************/
static void
-check_sbuf_desc(const struct sbuf_desc* desc, const uint64_t count)
+check_sbuf_desc
+ (const struct sbuf_desc* desc,
+ const uint64_t count,
+ const struct type_desc* type)
{
+ char ALIGN(512) mem[512] = {0};
size_t i;
- CHK(desc);
+ CHK(desc && type);
+ CHK(type->size <= sizeof(mem));
+ CHK(type->alignment <= ALIGNOF(mem));
CHK(desc->buffer != NULL);
CHK(desc->count == count);
- CHK(desc->szelmt == sizeof(struct my_type));
- CHK(desc->alelmt == ALIGNOF(struct my_type));
+ CHK(desc->szelmt == type->size);
+ CHK(desc->alelmt == type->alignment);
+ CHK(desc->pitch == ALIGN_SIZE(type->size, type->alignment));
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);
+ const void* elmt = sbuf_desc_at(desc, i);
+ type->set(mem, i);
+ CHK(type->eq(mem, elmt));
}
}
static void
-test_load(struct sbuf* buf)
+test_load(struct sbuf* buf, const struct type_desc* type)
{
+ char ALIGN(512) mem[512] = {0};
struct sbuf_desc desc = SBUF_DESC_NULL;
FILE* fp = NULL;
const char* filename = "test_file.sbuf";
@@ -54,13 +114,18 @@ test_load(struct sbuf* buf)
uint64_t alelmt;
size_t i;
char byte = 0;
-
+
+ CHK(buf && type);
+
+ CHK(type->size <= sizeof(mem));
+ CHK(type->alignment <= ALIGNOF(mem));
+
fp = fopen(filename, "w+");
CHK(fp);
/* Write file header */
- szelmt = sizeof(struct my_type);
- alelmt = ALIGNOF(struct my_type);
+ szelmt = (uint64_t)type->size;
+ alelmt = (uint64_t)type->alignment;
CHK(fwrite(&pagesize, sizeof(pagesize), 1, fp) == 1);
CHK(fwrite(&count, sizeof(count), 1, fp) == 1);
CHK(fwrite(&szelmt, sizeof(szelmt), 1, fp) == 1);
@@ -71,10 +136,10 @@ test_load(struct sbuf* buf)
/* 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);
+ type->set(mem, i);
+ CHK(fwrite(mem, type->size, 1, fp) == 1);
+ CHK(fseek
+ (fp, (long)ALIGN_SIZE((size_t)ftell(fp), type->alignment), SEEK_SET) == 0);
}
/* Padding. Write one char to position the EOF indicator */
@@ -89,7 +154,7 @@ test_load(struct sbuf* buf)
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);
+ check_sbuf_desc(&desc, count, type);
fclose(fp);
}
@@ -101,17 +166,32 @@ int
main(int argc, char** argv)
{
struct sbuf_create_args args = SBUF_CREATE_ARGS_DEFAULT;
- struct sbuf* sbuf = NULL;
+ struct type_desc type;
+ struct sbuf* buf = NULL;
(void)argc, (void)argv;
args.verbose = 1;
- CHK(sbuf_create(&args, &sbuf) == RES_OK);
+ CHK(sbuf_create(&args, &buf) == RES_OK);
+
+ type.set = i16_f32_set;
+ type.eq = i16_f32_eq;
+ type.size = sizeof(struct i16_f32);
+ type.alignment = ALIGNOF(struct i16_f32);
+ test_load(buf, &type);
+
+ type.alignment = 32;
+ test_load(buf, &type);
+
+ type.set = char7_set;
+ type.eq = char7_eq;
+ type.size = sizeof(char7_T);
+ type.alignment = ALIGNOF(char7_T);
+ test_load(buf, &type);
- test_load(sbuf); /* Tetrahedra */
/*test_load_fail(smsh);
test_load_files(smsh, argc, argv);*/
- CHK(sbuf_ref_put(sbuf) == RES_OK);
+ CHK(sbuf_ref_put(buf) == RES_OK);
CHK(mem_allocated_size() == 0);
return 0;
}