commit 16cf844396181e743403b046c06a21f1dca39426
parent 317ced6e3bbfc9a4f2ebe1ca1b4705a0fddf6df4
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date: Mon, 9 Sep 2024 17:33:16 +0200
Added tableinfo
Diffstat:
5 files changed, 240 insertions(+), 0 deletions(-)
diff --git a/src/solvers/solvers.h b/src/solvers/solvers.h
@@ -1,2 +1,3 @@
+#include "tables.h"
#include "generic/generic.h"
#include "h48/h48.h"
diff --git a/src/solvers/tables.h b/src/solvers/tables.h
@@ -0,0 +1,93 @@
+#define OFFSET(B, K) (((uint8_t *)B) + K)
+#define INFOSIZE 512
+#define INFO_OFFSET_SOLVER 0
+#define INFO_SOLVER_STRLEN 20
+#define INFO_OFFSET_INFOSIZE INFO_SOLVER_STRLEN
+#define INFO_OFFSET_FULLSIZE (INFO_OFFSET_INFOSIZE + sizeof(uint64_t))
+#define INFO_OFFSET_HASH (INFO_OFFSET_FULLSIZE + sizeof(uint64_t))
+#define INFO_OFFSET_ENTRIES (INFO_OFFSET_HASH + sizeof(uint64_t))
+#define INFO_OFFSET_BITS (INFO_OFFSET_ENTRIES + sizeof(uint64_t))
+#define INFO_OFFSET_BASE (INFO_OFFSET_BITS + sizeof(uint8_t))
+#define INFO_OFFSET_MAXVALUE (INFO_OFFSET_BASE + sizeof(uint8_t))
+#define INFO_OFFSET_DISTRIBUTION (INFO_OFFSET_MAXVALUE + sizeof(uint8_t))
+#define INFO_DISTRIBUTION_LEN 21
+
+typedef struct {
+ char solver[INFO_SOLVER_STRLEN];
+ uint64_t infosize;
+ uint64_t fullsize;
+ uint64_t hash;
+ uint64_t entries;
+ uint8_t bits;
+ uint8_t base;
+ uint8_t maxvalue;
+ uint64_t distribution[INFO_DISTRIBUTION_LEN];
+} tableinfo_t;
+
+STATIC bool readtableinfo(const void *, tableinfo_t *);
+STATIC bool writetableinfo(const tableinfo_t *, void *);
+
+STATIC bool
+readtableinfo(const void *buf, tableinfo_t *info)
+{
+ if (buf == NULL) {
+ LOG("Error reading table: buffer in NULL\n");
+ return false;
+ }
+
+ if (info == NULL) {
+ LOG("Error reading table info: info struct is NULL\n");
+ return false;
+ }
+
+ memcpy(info->solver, OFFSET(buf, INFO_OFFSET_SOLVER),
+ INFO_SOLVER_STRLEN);
+ info->infosize = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_INFOSIZE);
+ info->fullsize = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_FULLSIZE);
+ info->hash = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_HASH);
+ info->entries = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_ENTRIES);
+ info->bits = *OFFSET(buf, INFO_OFFSET_BITS);
+ info->base = *OFFSET(buf, INFO_OFFSET_BASE);
+ info->maxvalue = *OFFSET(buf, INFO_OFFSET_MAXVALUE);
+ memcpy(info->distribution, OFFSET(buf, INFO_OFFSET_DISTRIBUTION),
+ INFO_DISTRIBUTION_LEN * sizeof(uint64_t));
+
+ return true;
+}
+
+STATIC bool
+writetableinfo(const tableinfo_t *info, void *buf)
+{
+ int i;
+
+ if (buf == NULL) {
+ LOG("Error reading table: buffer in NULL\n");
+ return false;
+ }
+
+ if (info == NULL) {
+ LOG("Error writing table info: provided info is NULL\n");
+ return false;
+ }
+
+ memcpy(OFFSET(buf, INFO_OFFSET_SOLVER), info->solver,
+ INFO_SOLVER_STRLEN);
+
+ /* Zeroing all chars after the end of the string, for consistency */
+ for (i = 1; i < INFO_SOLVER_STRLEN; i++)
+ if (*OFFSET(buf, i) == 0)
+ *OFFSET(buf, i) = 0;
+
+ *(uint64_t *)OFFSET(buf, INFO_OFFSET_INFOSIZE) = info->infosize;
+ *(uint64_t *)OFFSET(buf, INFO_OFFSET_FULLSIZE) = info->fullsize;
+ *(uint64_t *)OFFSET(buf, INFO_OFFSET_HASH) = info->hash;
+ *(uint64_t *)OFFSET(buf, INFO_OFFSET_ENTRIES) = info->entries;
+ *OFFSET(buf, INFO_OFFSET_BITS) = info->bits;
+ *OFFSET(buf, INFO_OFFSET_BASE) = info->base;
+ *OFFSET(buf, INFO_OFFSET_MAXVALUE) = info->maxvalue;
+
+ memcpy(OFFSET(buf, INFO_OFFSET_DISTRIBUTION), info->distribution,
+ INFO_DISTRIBUTION_LEN * sizeof(uint64_t));
+
+ return true;
+}
diff --git a/test/090_tables_readwrite/00_table.in b/test/090_tables_readwrite/00_table.in
@@ -0,0 +1,31 @@
+Test solver
+
+512
+100000000000
+12345678912345
+399999999998
+2
+0
+20
+
+100
+101
+102
+103
+104
+105
+106
+107
+108
+109
+110
+111
+112
+113
+114
+115
+116
+117
+118
+119
+120
diff --git a/test/090_tables_readwrite/00_table.out b/test/090_tables_readwrite/00_table.out
@@ -0,0 +1,31 @@
+Test solver
+
+512
+100000000000
+12345678912345
+399999999998
+2
+0
+20
+
+100
+101
+102
+103
+104
+105
+106
+107
+108
+109
+110
+111
+112
+113
+114
+115
+116
+117
+118
+119
+120
diff --git a/test/090_tables_readwrite/tables_readwrite_tests.c b/test/090_tables_readwrite/tables_readwrite_tests.c
@@ -0,0 +1,84 @@
+#include "../test.h"
+
+#define INFOSIZE 512
+#define INFO_SOLVER_STRLEN 20
+#define INFO_DISTRIBUTION_LEN 21
+
+typedef struct {
+ char solver[INFO_SOLVER_STRLEN];
+ uint64_t infosize;
+ uint64_t fullsize;
+ uint64_t hash;
+ uint64_t entries;
+ uint8_t bits;
+ uint8_t base;
+ uint8_t maxvalue;
+ uint64_t distribution[INFO_DISTRIBUTION_LEN];
+} tableinfo_t;
+
+bool readtableinfo(const void *, tableinfo_t *);
+bool writetableinfo(const tableinfo_t *, void *);
+
+uint64_t readn(void) {
+ char str[STRLENMAX];
+
+ fgets(str, STRLENMAX, stdin);
+ return atoll(str);
+}
+
+tableinfo_t test_readinfo(void) {
+ int i;
+ tableinfo_t ret;
+ char emptyline[2];
+
+ fgets(ret.solver, INFO_SOLVER_STRLEN, stdin);
+ for (i = 0; i < INFO_SOLVER_STRLEN; i++)
+ if (ret.solver[i] == '\n')
+ ret.solver[i] = 0;
+
+ fgets(emptyline, 2, stdin);
+
+ ret.infosize = readn();
+ ret.fullsize = readn();
+ ret.hash = readn();
+ ret.entries = readn();
+ ret.bits = (uint8_t)readn();
+ ret.base = (uint8_t)readn();
+ ret.maxvalue = (uint8_t)readn();
+
+ fgets(emptyline, 2, stdin);
+
+ for (i = 0; i < INFO_DISTRIBUTION_LEN; i++)
+ ret.distribution[i] = readn();
+
+ return ret;
+}
+
+void test_writeinfo(tableinfo_t info) {
+ int i;
+
+ printf("%s\n", info.solver);
+ printf("\n");
+
+ printf("%" PRIu64 "\n", info.infosize);
+ printf("%" PRIu64 "\n", info.fullsize);
+ printf("%" PRIu64 "\n", info.hash);
+ printf("%" PRIu64 "\n", info.entries);
+ printf("%" PRIu8 "\n", info.bits);
+ printf("%" PRIu8 "\n", info.base);
+ printf("%" PRIu8 "\n", info.maxvalue);
+ printf("\n");
+
+ for (i = 0; i < INFO_DISTRIBUTION_LEN; i++)
+ printf("%" PRIu64 "\n", info.distribution[i]);
+}
+
+void run(void) {
+ char buf[INFOSIZE];
+ tableinfo_t expected, actual;
+
+ expected = test_readinfo();
+ writetableinfo(&expected, buf);
+ readtableinfo(buf, &actual);
+ test_writeinfo(actual);
+}