aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/solvers/solvers.h1
-rw-r--r--src/solvers/tables.h93
-rw-r--r--test/090_tables_readwrite/00_table.in31
-rw-r--r--test/090_tables_readwrite/00_table.out31
-rw-r--r--test/090_tables_readwrite/tables_readwrite_tests.c84
5 files changed, 240 insertions, 0 deletions
diff --git a/src/solvers/solvers.h b/src/solvers/solvers.h
index 66c9b27..00a28c1 100644
--- a/src/solvers/solvers.h
+++ b/src/solvers/solvers.h
@@ -1,2 +1,3 @@
1#include "tables.h"
1#include "generic/generic.h" 2#include "generic/generic.h"
2#include "h48/h48.h" 3#include "h48/h48.h"
diff --git a/src/solvers/tables.h b/src/solvers/tables.h
new file mode 100644
index 0000000..be0209f
--- /dev/null
+++ b/src/solvers/tables.h
@@ -0,0 +1,93 @@
1#define OFFSET(B, K) (((uint8_t *)B) + K)
2#define INFOSIZE 512
3#define INFO_OFFSET_SOLVER 0
4#define INFO_SOLVER_STRLEN 20
5#define INFO_OFFSET_INFOSIZE INFO_SOLVER_STRLEN
6#define INFO_OFFSET_FULLSIZE (INFO_OFFSET_INFOSIZE + sizeof(uint64_t))
7#define INFO_OFFSET_HASH (INFO_OFFSET_FULLSIZE + sizeof(uint64_t))
8#define INFO_OFFSET_ENTRIES (INFO_OFFSET_HASH + sizeof(uint64_t))
9#define INFO_OFFSET_BITS (INFO_OFFSET_ENTRIES + sizeof(uint64_t))
10#define INFO_OFFSET_BASE (INFO_OFFSET_BITS + sizeof(uint8_t))
11#define INFO_OFFSET_MAXVALUE (INFO_OFFSET_BASE + sizeof(uint8_t))
12#define INFO_OFFSET_DISTRIBUTION (INFO_OFFSET_MAXVALUE + sizeof(uint8_t))
13#define INFO_DISTRIBUTION_LEN 21
14
15typedef struct {
16 char solver[INFO_SOLVER_STRLEN];
17 uint64_t infosize;
18 uint64_t fullsize;
19 uint64_t hash;
20 uint64_t entries;
21 uint8_t bits;
22 uint8_t base;
23 uint8_t maxvalue;
24 uint64_t distribution[INFO_DISTRIBUTION_LEN];
25} tableinfo_t;
26
27STATIC bool readtableinfo(const void *, tableinfo_t *);
28STATIC bool writetableinfo(const tableinfo_t *, void *);
29
30STATIC bool
31readtableinfo(const void *buf, tableinfo_t *info)
32{
33 if (buf == NULL) {
34 LOG("Error reading table: buffer in NULL\n");
35 return false;
36 }
37
38 if (info == NULL) {
39 LOG("Error reading table info: info struct is NULL\n");
40 return false;
41 }
42
43 memcpy(info->solver, OFFSET(buf, INFO_OFFSET_SOLVER),
44 INFO_SOLVER_STRLEN);
45 info->infosize = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_INFOSIZE);
46 info->fullsize = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_FULLSIZE);
47 info->hash = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_HASH);
48 info->entries = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_ENTRIES);
49 info->bits = *OFFSET(buf, INFO_OFFSET_BITS);
50 info->base = *OFFSET(buf, INFO_OFFSET_BASE);
51 info->maxvalue = *OFFSET(buf, INFO_OFFSET_MAXVALUE);
52 memcpy(info->distribution, OFFSET(buf, INFO_OFFSET_DISTRIBUTION),
53 INFO_DISTRIBUTION_LEN * sizeof(uint64_t));
54
55 return true;
56}
57
58STATIC bool
59writetableinfo(const tableinfo_t *info, void *buf)
60{
61 int i;
62
63 if (buf == NULL) {
64 LOG("Error reading table: buffer in NULL\n");
65 return false;
66 }
67
68 if (info == NULL) {
69 LOG("Error writing table info: provided info is NULL\n");
70 return false;
71 }
72
73 memcpy(OFFSET(buf, INFO_OFFSET_SOLVER), info->solver,
74 INFO_SOLVER_STRLEN);
75
76 /* Zeroing all chars after the end of the string, for consistency */
77 for (i = 1; i < INFO_SOLVER_STRLEN; i++)
78 if (*OFFSET(buf, i) == 0)
79 *OFFSET(buf, i) = 0;
80
81 *(uint64_t *)OFFSET(buf, INFO_OFFSET_INFOSIZE) = info->infosize;
82 *(uint64_t *)OFFSET(buf, INFO_OFFSET_FULLSIZE) = info->fullsize;
83 *(uint64_t *)OFFSET(buf, INFO_OFFSET_HASH) = info->hash;
84 *(uint64_t *)OFFSET(buf, INFO_OFFSET_ENTRIES) = info->entries;
85 *OFFSET(buf, INFO_OFFSET_BITS) = info->bits;
86 *OFFSET(buf, INFO_OFFSET_BASE) = info->base;
87 *OFFSET(buf, INFO_OFFSET_MAXVALUE) = info->maxvalue;
88
89 memcpy(OFFSET(buf, INFO_OFFSET_DISTRIBUTION), info->distribution,
90 INFO_DISTRIBUTION_LEN * sizeof(uint64_t));
91
92 return true;
93}
diff --git a/test/090_tables_readwrite/00_table.in b/test/090_tables_readwrite/00_table.in
new file mode 100644
index 0000000..1816242
--- /dev/null
+++ b/test/090_tables_readwrite/00_table.in
@@ -0,0 +1,31 @@
1Test solver
2
3512
4100000000000
512345678912345
6399999999998
72
80
920
10
11100
12101
13102
14103
15104
16105
17106
18107
19108
20109
21110
22111
23112
24113
25114
26115
27116
28117
29118
30119
31120
diff --git a/test/090_tables_readwrite/00_table.out b/test/090_tables_readwrite/00_table.out
new file mode 100644
index 0000000..1816242
--- /dev/null
+++ b/test/090_tables_readwrite/00_table.out
@@ -0,0 +1,31 @@
1Test solver
2
3512
4100000000000
512345678912345
6399999999998
72
80
920
10
11100
12101
13102
14103
15104
16105
17106
18107
19108
20109
21110
22111
23112
24113
25114
26115
27116
28117
29118
30119
31120
diff --git a/test/090_tables_readwrite/tables_readwrite_tests.c b/test/090_tables_readwrite/tables_readwrite_tests.c
new file mode 100644
index 0000000..97b8ab7
--- /dev/null
+++ b/test/090_tables_readwrite/tables_readwrite_tests.c
@@ -0,0 +1,84 @@
1#include "../test.h"
2
3#define INFOSIZE 512
4#define INFO_SOLVER_STRLEN 20
5#define INFO_DISTRIBUTION_LEN 21
6
7typedef struct {
8 char solver[INFO_SOLVER_STRLEN];
9 uint64_t infosize;
10 uint64_t fullsize;
11 uint64_t hash;
12 uint64_t entries;
13 uint8_t bits;
14 uint8_t base;
15 uint8_t maxvalue;
16 uint64_t distribution[INFO_DISTRIBUTION_LEN];
17} tableinfo_t;
18
19bool readtableinfo(const void *, tableinfo_t *);
20bool writetableinfo(const tableinfo_t *, void *);
21
22uint64_t readn(void) {
23 char str[STRLENMAX];
24
25 fgets(str, STRLENMAX, stdin);
26 return atoll(str);
27}
28
29tableinfo_t test_readinfo(void) {
30 int i;
31 tableinfo_t ret;
32 char emptyline[2];
33
34 fgets(ret.solver, INFO_SOLVER_STRLEN, stdin);
35 for (i = 0; i < INFO_SOLVER_STRLEN; i++)
36 if (ret.solver[i] == '\n')
37 ret.solver[i] = 0;
38
39 fgets(emptyline, 2, stdin);
40
41 ret.infosize = readn();
42 ret.fullsize = readn();
43 ret.hash = readn();
44 ret.entries = readn();
45 ret.bits = (uint8_t)readn();
46 ret.base = (uint8_t)readn();
47 ret.maxvalue = (uint8_t)readn();
48
49 fgets(emptyline, 2, stdin);
50
51 for (i = 0; i < INFO_DISTRIBUTION_LEN; i++)
52 ret.distribution[i] = readn();
53
54 return ret;
55}
56
57void test_writeinfo(tableinfo_t info) {
58 int i;
59
60 printf("%s\n", info.solver);
61 printf("\n");
62
63 printf("%" PRIu64 "\n", info.infosize);
64 printf("%" PRIu64 "\n", info.fullsize);
65 printf("%" PRIu64 "\n", info.hash);
66 printf("%" PRIu64 "\n", info.entries);
67 printf("%" PRIu8 "\n", info.bits);
68 printf("%" PRIu8 "\n", info.base);
69 printf("%" PRIu8 "\n", info.maxvalue);
70 printf("\n");
71
72 for (i = 0; i < INFO_DISTRIBUTION_LEN; i++)
73 printf("%" PRIu64 "\n", info.distribution[i]);
74}
75
76void run(void) {
77 char buf[INFOSIZE];
78 tableinfo_t expected, actual;
79
80 expected = test_readinfo();
81 writetableinfo(&expected, buf);
82 readtableinfo(buf, &actual);
83 test_writeinfo(actual);
84}

Generated with cgit - Back to sebastiano.tronto.net