From 62d87e063318cc4c842b1b2d8c184f48aeaf6659 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 19 May 2025 17:45:14 +0200 Subject: Refactored checkdata Relevant changes include: - Changed the signature of nissy_checkdata(). - Removed expected_distribution.h from tools; this data is now included in each solver's src/ code. - Removed distribution check for cocsep; may add back later. --- tools/000_gendata/gendata.c | 21 +-- tools/001_derive_h48/derive_h48.c | 40 ------ tools/100_checkdata/checkdata.c | 10 +- tools/expected_distributions.h | 267 -------------------------------------- tools/nissy_extra.h | 14 -- tools/tool.h | 86 ------------ 6 files changed, 9 insertions(+), 429 deletions(-) delete mode 100644 tools/001_derive_h48/derive_h48.c delete mode 100644 tools/expected_distributions.h delete mode 100644 tools/nissy_extra.h (limited to 'tools') diff --git a/tools/000_gendata/gendata.c b/tools/000_gendata/gendata.c index 66c4864..f2da890 100644 --- a/tools/000_gendata/gendata.c +++ b/tools/000_gendata/gendata.c @@ -1,13 +1,10 @@ #include "../tool.h" -#include "../expected_distributions.h" char *solver; -uint64_t *expected; static void run(void) { int64_t size; - bool consistent, expected; char filename[1024], dataid[NISSY_SIZE_DATAID]; unsigned char *buf; @@ -18,20 +15,14 @@ run(void) { case -2: goto gendata_run_finish; default: - nissy_datainfo(size, buf); - consistent = nissy_checkdata(size, buf) == 0; - expected = check_distribution(solver, size, buf); - if (consistent && expected) { + if (nissy_checkdata(solver, size, buf) == NISSY_OK) { printf("\n"); printf("Generated %" PRId64 " bytes.\n", size); sprintf(filename, "tables/%s", dataid); writetable(buf, size, filename); + } else { + printf("Error: table generated incorrectly!\n"); } - if (!consistent) - printf("Error: table is not consistent with info" - " (nissy_checkdata() failed)\n"); - if (!expected) - printf("Error: distribution is not as expected\n"); break; } @@ -40,8 +31,6 @@ gendata_run_finish: } int main(int argc, char **argv) { - uint8_t h, k; - if (argc < 2) { printf("Error: not enough arguments. " "A solver must be given.\n"); @@ -49,11 +38,7 @@ int main(int argc, char **argv) { } solver = argv[1]; - parse_h48_hk(solver, &h, &k); - expected = expected_h48[h][k]; - nissy_setlogger(log_stderr, NULL); - timerun(run); return 0; diff --git a/tools/001_derive_h48/derive_h48.c b/tools/001_derive_h48/derive_h48.c deleted file mode 100644 index e8db207..0000000 --- a/tools/001_derive_h48/derive_h48.c +++ /dev/null @@ -1,40 +0,0 @@ -/* -This tool is specific to the H48 solver. It can be used to derive small -intermediate tables from larger tables, including the full h11 table. - -When using k=2, the base values for the tables may differ. If you want -to change this value, for example for generating a table with base = 9 -from the h11 table with base = 10, you must re-generate the large table -with the correct base value. The easiest way to do so is to manually -edit the base value in the source code and recompile. -*/ - -#include "../tool.h" - -char *solver_large, *solver_small, *filename_large, *filename_small; - -void run(void) { - derivedata_run(solver_large, solver_small, filename_large, filename_small); -} - -int main(int argc, char **argv) { - if (argc < 5) { - printf("Error: not enough arguments. Required:\n" - "1. Solver name for large table\n" - "2. Solver name for derived table\n" - "3. Filename containing large table\n" - "4. Filename for saving derived table\n"); - return 1; - } - - solver_large = argv[1]; - solver_small = argv[2]; - filename_large = argv[3]; - filename_small = argv[4]; - - nissy_setlogger(log_stderr, NULL); - - timerun(run); - - return 0; -} diff --git a/tools/100_checkdata/checkdata.c b/tools/100_checkdata/checkdata.c index c284aa4..3a3c098 100644 --- a/tools/100_checkdata/checkdata.c +++ b/tools/100_checkdata/checkdata.c @@ -1,11 +1,11 @@ #include "../tool.h" -#include "../expected_distributions.h" char *solver, *filename; static void run(void) { - long long int size, sizeread, result; + bool result; + long long int size, sizeread; char dataid[NISSY_SIZE_DATAID]; unsigned char *buf; FILE *f; @@ -25,10 +25,12 @@ run(void) { buf = malloc(size); sizeread = fread(buf, size, 1, f); fclose(f); - result = sizeread == 1 && nissy_checkdata(size, buf); + if (sizeread != 1) + printf("File has unexpected size\n"); + result = sizeread == 1 && nissy_checkdata(solver, size, buf) == NISSY_OK; free(buf); - printf("checkdata %s\n", result == 0 ? "succeeded" : "failed"); + printf("checkdata %s\n", result ? "succeeded" : "failed"); /* TODO: cross-check with expected distributions? */ } diff --git a/tools/expected_distributions.h b/tools/expected_distributions.h deleted file mode 100644 index 1193fb3..0000000 --- a/tools/expected_distributions.h +++ /dev/null @@ -1,267 +0,0 @@ -uint64_t expected_cocsep[21] = { - [0] = 1, - [1] = 6, - [2] = 63, - [3] = 468, - [4] = 3068, - [5] = 15438, - [6] = 53814, - [7] = 71352, - [8] = 8784, - [9] = 96 -}; - -uint64_t expected_h48[12][9][21] = { - [0] = { - [2] = { - [0] = 5473562, - [1] = 34776317, - [2] = 68566704, - [3] = 8750867, - }, - [4] = { - [0] = 1, - [1] = 1, - [2] = 4, - [3] = 34, - [4] = 331, - [5] = 3612, - [6] = 41605, - [7] = 474128, - [8] = 4953846, - [9] = 34776317, - [10] = 68566704, - [11] = 8749194, - [12] = 1673, - }, - }, - [1] = { - [2] = { - [0] = 6012079, - [1] = 45822302, - [2] = 142018732, - [3] = 41281787, - }, - }, - [2] = { - [2] = { - [0] = 6391286, - [1] = 55494785, - [2] = 252389935, - [3] = 155993794, - }, - }, - [3] = { - [2] = { - [0] = 6686828, - [1] = 63867852, - [2] = 392789689, - [3] = 477195231, - }, - }, - [4] = { - [2] = { - [0] = 77147213, - [1] = 543379415, - [2] = 1139570251, - [3] = 120982321, - }, - }, - [5] = { - [2] = { - [0] = 82471284, - [1] = 687850732, - [2] = 2345840746, - [3] = 645995638, - }, - }, - [6] = { - [2] = { - [0] = 85941099, - [1] = 804752968, - [2] = 4077248182, - [3] = 2556374551, - }, - }, - [7] = { - [2] = { - [0] = 88529761, - [1] = 897323475, - [2] = 6126260791, - [3] = 7936519573, - }, - }, - [8] = { - [2] = { - [0] = 1051579940, - [1] = 8136021316, - [2] = 19024479822, - [3] = 18851861220, - }, - }, - [9] = { - [2] = { - [0] = 1102038189, - [1] = 9888265242, - [2] = 38299375805, - [3] = 10904855164, - }, - }, - [10] = { - [2] = { - [0] = 1133240039, - [1] = 11196285614, - [2] = 64164702961, - [3] = 43894840186, - }, - }, - [11] = { - [2] = { - [0] = 1150763161, - [1] = 12045845660, - [2] = 91163433330, - [3] = 136418095449, - }, - }, -}; - -uint64_t expected_eo[21] = { - [0] = 1, - [1] = 2, - [2] = 25, - [3] = 202, - [4] = 620, - [5] = 900, - [6] = 285, - [7] = 13, -}; - -uint64_t expected_dr[21] = { - [0] = 1, - [1] = 1, - [2] = 5, - [3] = 44, - [4] = 487, - [5] = 5841, - [6] = 68364, - [7] = 776568, - [8] = 7950748, - [9] = 52098876, - [10] = 76236234, - [11] = 3771112, - [12] = 129, -}; - -uint64_t expected_dreo[21] = { - [0] = 1, - [1] = 1, - [2] = 4, - [3] = 22, - [4] = 160, - [5] = 1286, - [6] = 8550, - [7] = 42152, - [8] = 90748, - [9] = 33466, - [10] = 757, -}; - -static bool -distribution_equal(const uint64_t *expected, const uint64_t *actual, int n) -{ - bool equal; - int i; - - for (i = 0, equal = true; i <= n; i++) { - if (expected[i] != actual[i]) { - equal = false; - printf("Wrong value for %d: expected %" PRIu64 - ", actual %" PRIu64 "\n", - i, expected[i], actual[i]); - } - } - - return equal; -} - -STATIC bool -check_table(uint64_t *exp, tableinfo_t *info) -{ - if (!distribution_equal(exp, info->distribution, info->maxvalue)) { - printf("ERROR! Distribution is incorrect\n"); - return false; - } - - printf("Distribution is correct\n"); - return true; -} - -static bool -check_cocsep(size_t data_size, const unsigned char *data) -{ - tableinfo_t info; - - readtableinfo(data_size, data, &info); - return distribution_equal( - expected_cocsep, info.distribution, info.maxvalue); -} - -static bool -unknown_h48(uint8_t h, uint8_t k) -{ - if (k != 2 && k != 4) - return true; - - if (k == 4 && h != 0) - return true; - - return k == 2 && h > 7; -} - -STATIC bool -check_distribution( - const char *solver, - size_t data_size, - const unsigned char *data -) -{ - const char *str; - tableinfo_t info = {0}; - - if (!strncmp(solver, "h48", 3)) { - readtableinfo(data_size, data, &info); - if (!distribution_equal( - expected_cocsep, info.distribution, info.maxvalue)) { - printf("ERROR! cocsep distribution is incorrect\n"); - return false; - } - printf("cocsep distribution is correct\n"); - - readtableinfo_n(data_size, data, 2, &info); - if (unknown_h48(info.h48h, info.bits)) - goto check_distribution_unknown; - - return check_table(expected_h48[info.h48h][info.bits], &info); - } - - if (!strncmp(solver, "coord_", 6)) { - readtableinfo(data_size, data, &info); - if (!strncmp(info.solver, "coord helper table for ", 23)) - readtableinfo_n(data_size, data, 2, &info); - - str = info.solver + 22; /* "coordinate solver for COORD" */ - if (!strcmp(str, "EO")) { - return check_table(expected_eo, &info); - } else if (!strcmp(str, "DR")) { - return check_table(expected_dr, &info); - } else if (!strcmp(str, "DREO")) { - return check_table(expected_dreo, &info); - } else { - goto check_distribution_unknown; - } - } - -check_distribution_unknown: - printf("Distribution unknown, not checked\n"); - return true; -} diff --git a/tools/nissy_extra.h b/tools/nissy_extra.h deleted file mode 100644 index 9a7df50..0000000 --- a/tools/nissy_extra.h +++ /dev/null @@ -1,14 +0,0 @@ -/* -This header file exposes certain functions that are meant to be used -for testing purposes only. -*/ - -#define STATIC static -#define LOG printf - -#include "../src/solvers/tables_types_macros.h" -#include "../src/solvers/tables.h" - -size_t gendata_h48_derive(uint8_t, const unsigned char *, unsigned char *); -long long parse_h48_hk(const char *, uint8_t [static 1], uint8_t [static 1]); -long long int nissy_datainfo(uint64_t, const unsigned char *); diff --git a/tools/tool.h b/tools/tool.h index 225f789..9c55d2b 100644 --- a/tools/tool.h +++ b/tools/tool.h @@ -7,19 +7,14 @@ #include #include "../src/nissy.h" -#include "nissy_extra.h" static void log_stderr(const char *, void *); static double timerun(void (*)(void)); static void writetable(const unsigned char *, int64_t, const char *); static long long int generatetable(const char *, unsigned char **, char [static NISSY_SIZE_DATAID]); -static long long int derivetable( - const char *, const char *, const char *, unsigned char **); static int getdata(const char *, unsigned char **, const char *); static void gendata_run(const char *, uint64_t[static 21]); -static void derivedata_run( - const char *, const char *, const char *, const char *); static void log_stderr(const char *str, void *unused) @@ -100,53 +95,6 @@ generatetable( return gensize; } -static long long int -derivetable( - const char *solver_large, - const char *solver_small, - const char *filename_large, - unsigned char **buf -) -{ - uint8_t h, k; - long long int size, gensize; - char dataid[NISSY_SIZE_DATAID]; - unsigned char *fulltable; - - if (getdata(solver_large, &fulltable, filename_large) != 0) { - printf("Error reading full table.\n"); - gensize = -1; - goto derivetable_error_nofree; - } - - size = nissy_solverinfo(solver_small, dataid); - if (size == -1) { - printf("Error getting table size.\n"); - gensize = -2; - goto derivetable_error; - } - - if (parse_h48_hk(solver_small, &h, &k) != NISSY_OK) { - gensize = -3; - goto derivetable_error; - } - - *buf = malloc(size); - gensize = gendata_h48_derive(h, fulltable, *buf); - - if (gensize != size) { - printf("Error deriving table\n"); - gensize = -4; - goto derivetable_error; - } - -derivetable_error: - free(fulltable); - -derivetable_error_nofree: - return gensize; -} - static int getdata( const char *solver, @@ -206,8 +154,6 @@ gendata_run( case -2: goto gendata_run_finish; default: - nissy_datainfo(size, buf); - printf("\n"); printf("Succesfully generated %lld bytes. " "See above for details on the tables.\n", size); @@ -219,35 +165,3 @@ gendata_run( gendata_run_finish: free(buf); } - -static void -derivedata_run( - const char *solver_large, - const char *solver_small, - const char *filename_large, - const char *filename_small -) -{ - long long int size; - unsigned char *buf; - - buf = NULL; - size = derivetable(solver_large, solver_small, filename_large, &buf); - switch (size) { - case -1: - return; - case -2: - goto derivedata_run_finish; - default: - nissy_datainfo(size, buf); - printf("\n"); - printf("Succesfully generated %lld bytes. " - "See above for details on the tables.\n", size); - - writetable(buf, size, filename_small); - break; - } - -derivedata_run_finish: - free(buf); -} -- cgit v1.3