h48

A prototype for an optimal Rubik's cube solver, work in progress.
git clone https://git.tronto.net/h48
Download | Log | Files | Refs | README | LICENSE

commit e986713657bc5f9880e91ed49dd9b0d0227f048d
parent 02ce9adf6a9168ec66c322ce9e5a5cb9fe1e60a5
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Mon, 17 Jun 2024 17:39:06 +0200

Use callback function to log to stderr

Diffstat:
Msrc/cube.c | 22+++++++++++++++++-----
Msrc/cube.h | 4++++
Msrc/cube_generic.h | 18+++++++++---------
Msrc/cube_public.h | 6++++++
Msrc/cube_transform_with_switch.h | 6+++---
Msrc/io_cube.h | 12++++++------
Msrc/io_move_trans.h | 2+-
Msrc/solve_generic.h | 26+++++++++++++-------------
Msrc/solve_h48.h | 26+++++++++++++-------------
Mtest/000_basic/basic_tests.c | 4+---
Mtest/001_pieces/pieces_tests.c | 4+---
Mtest/010_math_permsign/permsgn_tests.c | 4+---
Mtest/020_io_H48_read_write/io_H48_tests.c | 4+---
Mtest/021_io_B32_write/io_B32_read_tests.c | 4+---
Mtest/022_io_B32_read/io_B32_read_tests.c | 4+---
Mtest/023_io_LST_write/io_LST_write_tests.c | 4+---
Mtest/024_io_LST_read/io_LST_read_tests.c | 4+---
Mtest/030_move/move_tests.c | 4+---
Mtest/040_inverse_cube/inverse_tests.c | 4+---
Mtest/050_compose/compose_tests.c | 4+---
Mtest/060_transform/transform_tests.c | 4+---
Mtest/061_inverse_trans/inverse_trans_tests.c | 4+---
Mtest/071_coord_eo/coord_eo_tests.c | 4+---
Mtest/072_coord_co/coord_co_tests.c | 4+---
Mtest/073_coord_csep/coord_csep_tests.c | 4+---
Mtest/074_coord_esep/coord_esep_tests.c | 4+---
Mtest/075_set_eo/set_eo_tests.c | 4+---
Mtest/076_copy_corners/copy_corners_tests.c | 4+---
Mtest/077_copy_edges/copy_edges_tests.c | 4+---
Mtest/078_invcoord_esep/invcoord_esep_tests.c | 4+---
Mtest/080_allowednext/allowednext_tests.c | 4+---
Mtest/090_solve_simple/solve_simple_tests.c | 4+---
Mtest/100_gendata_cocsep/gendata_cocsep_tests.c | 4+---
Mtest/101_cocsep_selfsim/cocsep_selfsim_tests.c | 4+---
Mtest/102_coord_invcoord_h48/coord_invcoord_h48_tests.c | 4+---
Mtest/103_gendata_h48/gendata_h48_tests.c | 3+--
Dtest/test | 41-----------------------------------------
Mtest/test.h | 13+++++++++++++
38 files changed, 112 insertions(+), 171 deletions(-)

diff --git a/src/cube.c b/src/cube.c @@ -1,20 +1,32 @@ #include <inttypes.h> +#include <stdarg.h> #include <stdbool.h> #include <string.h> +void (*nissy_log)(const char *, va_list); + +void +_log(const char *str, ...) /* TODO: rename */ +{ + va_list args; + + if (nissy_log != NULL) { + va_start(args, str); + nissy_log(str, args); + va_end(args); + } +} + #ifdef DEBUG -#include <stdio.h> #define _static #define _static_inline -#define DBG_LOG(...) fprintf(stderr, __VA_ARGS__) -#define DBG_WARN(condition, ...) if (!(condition)) DBG_LOG(__VA_ARGS__); +#define DBG_WARN(condition, ...) if (!(condition)) _log(__VA_ARGS__); #define DBG_ASSERT(condition, retval, ...) \ - if (!(condition)) { DBG_LOG(__VA_ARGS__); return retval; } + if (!(condition)) { _log(__VA_ARGS__); return retval; } #else #define _static static #define _static_inline static inline -#define DBG_LOG(...) #define DBG_WARN(condition, ...) #define DBG_ASSERT(condition, retval, ...) #endif diff --git a/src/cube.h b/src/cube.h @@ -1,3 +1,5 @@ +/* include: inttypes, stdarg, stdbool, string */ + /* All the functions below return 0 in case of success and a positive number in case of error, unless otherwise specified. See (TODO: @@ -81,3 +83,5 @@ int64_t nissy_solve( const void *data, char *solutions ); + +void nissy_setlogger(void (*logger_function)(const char *, va_list)); diff --git a/src/cube_generic.h b/src/cube_generic.h @@ -65,16 +65,16 @@ isconsistent(cube_t cube) return true; inconsistent_ep: - DBG_LOG("Inconsistent EP\n"); + _log("Inconsistent EP\n"); return false; inconsistent_cp: - DBG_LOG("Inconsistent CP\n"); + _log("Inconsistent CP\n"); return false; inconsistent_eo: - DBG_LOG("Inconsistent EO\n"); + _log("Inconsistent EO\n"); return false; inconsistent_co: - DBG_LOG("Inconsistent CO\n"); + _log("Inconsistent CO\n"); return false; } @@ -114,13 +114,13 @@ issolvable(cube_t cube) return true; issolvable_parity: - DBG_LOG("EP and CP parities are different\n"); + _log("EP and CP parities are different\n"); return false; issolvable_eo: - DBG_LOG("Odd number of flipped edges\n"); + _log("Odd number of flipped edges\n"); return false; issolvable_co: - DBG_LOG("Sum of corner orientation is not multiple of 3\n"); + _log("Sum of corner orientation is not multiple of 3\n"); return false; } @@ -161,7 +161,7 @@ applymoves_finish: return cube; applymoves_error: - DBG_LOG("applymoves error\n"); + _log("applymoves error\n"); return zero; } @@ -238,7 +238,7 @@ move(cube_t c, uint8_t m) case _move_B3: return _move(B3, c); default: - DBG_LOG("move error, unknown move\n"); + _log("move error, unknown move\n"); return zero; } } diff --git a/src/cube_public.h b/src/cube_public.h @@ -166,3 +166,9 @@ nissy_solve( /* TODO: move solve_generic here? */ return -1; } + +void +nissy_setlogger(void (*log)(const char *, va_list)) +{ + nissy_log = log; +} diff --git a/src/cube_transform_with_switch.h b/src/cube_transform_with_switch.h @@ -118,7 +118,7 @@ transform_edges(cube_t c, uint8_t t) case _trans_BLm: return _trans_edges_mirrored(BLm, c); default: - DBG_LOG("transform error, unknown transformation\n"); + _log("transform error, unknown transformation\n"); return zero; } } @@ -224,7 +224,7 @@ transform_corners(cube_t c, uint8_t t) case _trans_BLm: return _trans_corners_mirrored(BLm, c); default: - DBG_LOG("transform error, unknown transformation\n"); + _log("transform error, unknown transformation\n"); return zero; } } @@ -330,7 +330,7 @@ transform(cube_t c, uint8_t t) case _trans_BLm: return _trans_mirrored(BLm, c); default: - DBG_LOG("transform error, unknown transformation\n"); + _log("transform error, unknown transformation\n"); return zero; } } diff --git a/src/io_cube.h b/src/io_cube.h @@ -49,7 +49,7 @@ readcube(const char *format, const char *buf) if (!strcmp(format, ioformat[i].name)) return ioformat[i].read(buf); - DBG_LOG("Cannot read cube in the given format\n"); + _log("Cannot read cube in the given format\n"); return zero; } @@ -76,7 +76,7 @@ writecube(const char *format, cube_t cube, char *buf) errormsg = "ERROR: format"; writecube_error: - DBG_LOG("writecube error, see stdout for details\n"); + _log("writecube error, see stdout for details\n"); len = strlen(errormsg); memcpy(buf, errormsg, len); buf[len] = '\n'; @@ -93,7 +93,7 @@ readco(const char *str) if (*str == '2') return _ctwist_ccw; - DBG_LOG("Error reading CO\n"); + _log("Error reading CO\n"); return _error; } @@ -107,7 +107,7 @@ readcp(const char *str) !strncmp(str, cornerstralt[c], 3)) return c; - DBG_LOG("Error reading CP\n"); + _log("Error reading CP\n"); return _error; } @@ -119,7 +119,7 @@ readeo(const char *str) if (*str == '1') return _eflip; - DBG_LOG("Error reading EO\n"); + _log("Error reading EO\n"); return _error; } @@ -132,7 +132,7 @@ readep(const char *str) if (!strncmp(str, edgestr[e], 2)) return e; - DBG_LOG("Error reading EP\n"); + _log("Error reading EP\n"); return _error; } diff --git a/src/io_move_trans.h b/src/io_move_trans.h @@ -49,7 +49,7 @@ readtrans(const char *buf) if (!strncmp(buf, transstr[t], 11)) return t; - DBG_LOG("readtrans error\n"); + _log("readtrans error\n"); return _error; } diff --git a/src/solve_generic.h b/src/solve_generic.h @@ -49,11 +49,11 @@ solve( solutions ); } else { - DBG_LOG("solve: unknown solver '%s'\n", solver); + _log("solve: unknown solver '%s'\n", solver); return -1; } - DBG_LOG("solve: error\n"); + _log("solve: error\n"); return -1; } @@ -63,7 +63,7 @@ solve_generic_appendsolution(dfsarg_generic_t *arg) int strl; strl = writemoves(arg->moves, arg->depth, *arg->nextsol); - DBG_LOG("Solution found: %s\n", *arg->nextsol); + _log("Solution found: %s\n", *arg->nextsol); *arg->nextsol += strl; **arg->nextsol = '\n'; (*arg->nextsol)++; @@ -126,12 +126,12 @@ solve_generic( int64_t ret, tmp, first; if (!issolvable(cube)) { - DBG_LOG("solve: cube is not solvable\n"); + _log("solve: cube is not solvable\n"); return -1; } if (issolved(cube)) { - DBG_LOG("solve: cube is already solved\n"); + _log("solve: cube is already solved\n"); sols[0] = '\n'; sols[1] = 0; return 1; @@ -141,32 +141,32 @@ solve_generic( "solve: NISS not implemented yet, 'nisstype' ignored\n"); if (minmoves < 0) { - DBG_LOG("solve: 'minmoves' is negative, setting to 0\n"); + _log("solve: 'minmoves' is negative, setting to 0\n"); minmoves = 0; } if (maxmoves < 0) { - DBG_LOG("solve: invalid 'maxmoves', setting to 20\n"); + _log("solve: invalid 'maxmoves', setting to 20\n"); maxmoves = 20; } if (maxsols < 0) { - DBG_LOG("solve: 'maxsols' is negative\n"); + _log("solve: 'maxsols' is negative\n"); return -1; } if (maxsols == 0) { - DBG_LOG("solve: 'maxsols' is 0\n"); + _log("solve: 'maxsols' is 0\n"); return 0; } if (sols == NULL) { - DBG_LOG("solve: return parameter 'sols' is NULL\n"); + _log("solve: return parameter 'sols' is NULL\n"); return -1; } if (estimate == NULL) { - DBG_LOG("solve: 'estimate' is NULL\n"); + _log("solve: 'estimate' is NULL\n"); return -1; } @@ -187,7 +187,7 @@ solve_generic( if (tmp != 0) first = arg.depth; - DBG_LOG("Found %" PRId64 " solution%s at depth %" PRIu8 "\n", + _log("Found %" PRId64 " solution%s at depth %" PRIu8 "\n", tmp, tmp == 1 ? "" : "s", arg.depth); if (ret >= maxsols) @@ -248,7 +248,7 @@ gendata(const char *solver, const char *options, void *data) maxdepth = atoi(&options[i+1]); ret = gendata_h48(data, h, maxdepth); } else { - DBG_LOG("gendata: implemented only for H48 solver\n"); + _log("gendata: implemented only for H48 solver\n"); ret = -1; } diff --git a/src/solve_h48.h b/src/solve_h48.h @@ -146,13 +146,13 @@ gendata_cocsep(void *buf, uint64_t *selfsim, cube_t *rep) .rep = rep }; for (i = 0, n = 0, cc = 0; i < 10; i++) { - DBG_LOG("cocsep: generating depth %" PRIu8 "\n", i); + _log("cocsep: generating depth %" PRIu8 "\n", i); memset(visited, 0, COCSEP_VISITEDSIZE); arg.depth = 0; arg.maxdepth = i; cc = gendata_cocsep_dfs(&arg); info[i+2] = cc; - DBG_LOG("found %" PRIu32 "\n", cc); + _log("found %" PRIu32 "\n", cc); } info[0] = (uint32_t)n; @@ -161,12 +161,12 @@ gendata_cocsep(void *buf, uint64_t *selfsim, cube_t *rep) "cocsep: computed %" PRIu16 " symmetry classes, " "expected %zu\n", n, COCSEP_CLASSES); - DBG_LOG("cocsep data computed\n"); - DBG_LOG("Symmetry classes: %" PRIu32 "\n", info[0]); - DBG_LOG("Maximum pruning value: %" PRIu32 "\n", info[1]); - DBG_LOG("Pruning value distribution:\n"); + _log("cocsep data computed\n"); + _log("Symmetry classes: %" PRIu32 "\n", info[0]); + _log("Maximum pruning value: %" PRIu32 "\n", info[1]); + _log("Pruning value distribution:\n"); for (j = 0; j < 10; j++) - DBG_LOG("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+2]); + _log("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+2]); gendata_cocsep_return_size: return COCSEP_FULLSIZE; @@ -264,21 +264,21 @@ gendata_h48(void *buf, uint8_t h, uint8_t maxdepth) tot < esep_max && arg.depth <= maxdepth; arg.depth++ ) { - DBG_LOG("esep: generating depth %" PRIu8 "\n", arg.depth); + _log("esep: generating depth %" PRIu8 "\n", arg.depth); cc = gendata_esep_bfs(&arg); tot += cc; info[arg.depth+1] = cc; - DBG_LOG("found %" PRIu64 "\n", cc); + _log("found %" PRIu64 "\n", cc); } info[0] = arg.depth-1; infosize = 4 * (size_t)(info[0] + 2); - DBG_LOG("h48 pruning table computed\n"); - DBG_LOG("Maximum pruning value: %" PRIu32 "\n", info[0]); - DBG_LOG("Pruning value distribution:\n"); + _log("h48 pruning table computed\n"); + _log("Maximum pruning value: %" PRIu32 "\n", info[0]); + _log("Pruning value distribution:\n"); for (j = 0; j <= info[0]; j++) - DBG_LOG("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+1]); + _log("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+1]); gendata_h48_return_size: return cocsepsize + ESEP_TABLESIZE(h, k) + infosize; diff --git a/test/000_basic/basic_tests.c b/test/000_basic/basic_tests.c @@ -17,7 +17,7 @@ check2(cube_t cube1, char *name1, cube_t cube2, char *name2) equal(cube1, cube2) ? "" : " NOT"); } -int main(void) { +void run(void) { cube_t zero, solved; memset(&zero, 0, sizeof(cube_t)); @@ -28,6 +28,4 @@ int main(void) { check2(solved, "Solved", solved, "Solved"); check2(solved, "Solved", zero, "Zero"); check2(zero, "Zero", solved, "Solved"); - - return 0; } diff --git a/test/001_pieces/pieces_tests.c b/test/001_pieces/pieces_tests.c @@ -2,7 +2,7 @@ void pieces(cube_t *, uint8_t [static 8], uint8_t [static 12]); -int main(void) { +void run(void) { int i; uint8_t corner[8], edge[12]; char str[STRLENMAX], *aux; @@ -22,6 +22,4 @@ int main(void) { for (i = 0; i < 12; i++) printf("%" PRIu8 " ", edge[i]); printf("\n"); - - return 0; } diff --git a/test/010_math_permsign/permsgn_tests.c b/test/010_math_permsign/permsgn_tests.c @@ -2,7 +2,7 @@ int permsign(uint8_t *, int); -int main(void) { +void run(void) { char str[STRLENMAX]; uint8_t a[100]; int n, i, p; @@ -17,6 +17,4 @@ int main(void) { p = permsign(a, n); printf("%d\n", p); - - return 0; } diff --git a/test/020_io_H48_read_write/io_H48_tests.c b/test/020_io_H48_read_write/io_H48_tests.c @@ -1,6 +1,6 @@ #include "../test.h" -int main(void) { +void run(void) { char str[STRLENMAX], *aux; cube_t cube; @@ -19,6 +19,4 @@ int main(void) { writecube("H48", cube, str); printf("%s\n", str); } - - return 0; } diff --git a/test/021_io_B32_write/io_B32_read_tests.c b/test/021_io_B32_write/io_B32_read_tests.c @@ -1,6 +1,6 @@ #include "../test.h" -int main(void) { +void run(void) { char str[STRLENMAX], *aux; cube_t cube; @@ -19,6 +19,4 @@ int main(void) { writecube("B32", cube, str); printf("%s\n", str); } - - return 0; } diff --git a/test/022_io_B32_read/io_B32_read_tests.c b/test/022_io_B32_read/io_B32_read_tests.c @@ -1,6 +1,6 @@ #include "../test.h" -int main(void) { +void run(void) { char str[STRLENMAX], *aux; cube_t cube; @@ -19,6 +19,4 @@ int main(void) { writecube("H48", cube, str); printf("%s\n", str); } - - return 0; } diff --git a/test/023_io_LST_write/io_LST_write_tests.c b/test/023_io_LST_write/io_LST_write_tests.c @@ -1,6 +1,6 @@ #include "../test.h" -int main(void) { +void run(void) { char str[STRLENMAX], *aux; cube_t cube; @@ -19,6 +19,4 @@ int main(void) { writecube("LST", cube, str); printf("%s\n", str); } - - return 0; } diff --git a/test/024_io_LST_read/io_LST_read_tests.c b/test/024_io_LST_read/io_LST_read_tests.c @@ -1,6 +1,6 @@ #include "../test.h" -int main(void) { +void run(void) { char str[STRLENMAX], *aux; cube_t cube; @@ -19,6 +19,4 @@ int main(void) { writecube("H48", cube, str); printf("%s\n", str); } - - return 0; } diff --git a/test/030_move/move_tests.c b/test/030_move/move_tests.c @@ -2,7 +2,7 @@ cube_t applymoves(cube_t, char *); -int main(void) { +void run(void) { char movestr[STRLENMAX], cubestr[STRLENMAX]; cube_t cube; @@ -20,6 +20,4 @@ int main(void) { writecube("H48", cube, cubestr); printf("%s\n", cubestr); } - - return 0; } diff --git a/test/040_inverse_cube/inverse_tests.c b/test/040_inverse_cube/inverse_tests.c @@ -2,7 +2,7 @@ cube_t inverse(cube_t); -int main(void) { +void run(void) { char str[STRLENMAX]; cube_t cube, inv; @@ -18,6 +18,4 @@ int main(void) { writecube("H48", inv, str); printf("%s\n", str); } - - return 0; } diff --git a/test/050_compose/compose_tests.c b/test/050_compose/compose_tests.c @@ -2,7 +2,7 @@ cube_t compose(cube_t, cube_t); -int main(void) { +void run(void) { char str[STRLENMAX]; cube_t c1, c2, c3; @@ -21,6 +21,4 @@ int main(void) { writecube("H48", c3, str); printf("%s\n", str); } - - return 0; } diff --git a/test/060_transform/transform_tests.c b/test/060_transform/transform_tests.c @@ -2,7 +2,7 @@ cube_t applytrans(cube_t, char *); -int main(void) { +void run(void) { char cubestr[STRLENMAX], transtr[STRLENMAX]; cube_t cube; @@ -20,6 +20,4 @@ int main(void) { writecube("H48", cube, cubestr); printf("%s\n", cubestr); } - - return 0; } diff --git a/test/061_inverse_trans/inverse_trans_tests.c b/test/061_inverse_trans/inverse_trans_tests.c @@ -6,7 +6,7 @@ cube_t applymoves(cube_t, char *); cube_t applytrans(cube_t, char *); extern char *transstr[]; -int main(void) { +void run(void) { uint8_t t, tinv; cube_t cube; @@ -33,6 +33,4 @@ int main(void) { transstr[t], tinv); } } - - return 0; } diff --git a/test/071_coord_eo/coord_eo_tests.c b/test/071_coord_eo/coord_eo_tests.c @@ -2,7 +2,7 @@ int64_t coord_eo(cube_t); -int main(void) { +void run(void) { char str[STRLENMAX]; cube_t cube; int64_t result; @@ -13,6 +13,4 @@ int main(void) { result = coord_eo(cube); printf("%" PRId64 "\n", result); - - return 0; } diff --git a/test/072_coord_co/coord_co_tests.c b/test/072_coord_co/coord_co_tests.c @@ -2,7 +2,7 @@ int64_t coord_co(cube_t); -int main(void) { +void run(void) { char str[STRLENMAX]; cube_t cube; int64_t result; @@ -13,6 +13,4 @@ int main(void) { result = coord_co(cube); printf("%" PRId64 "\n", result); - - return 0; } diff --git a/test/073_coord_csep/coord_csep_tests.c b/test/073_coord_csep/coord_csep_tests.c @@ -2,7 +2,7 @@ int64_t coord_csep(cube_t); -int main(void) { +void run(void) { char str[STRLENMAX]; cube_t cube; int64_t result; @@ -13,6 +13,4 @@ int main(void) { result = coord_csep(cube); printf("%" PRId64 "\n", result); - - return 0; } diff --git a/test/074_coord_esep/coord_esep_tests.c b/test/074_coord_esep/coord_esep_tests.c @@ -2,7 +2,7 @@ int64_t coord_esep(cube_t); -int main(void) { +void run(void) { char str[STRLENMAX]; cube_t cube; int64_t result; @@ -13,6 +13,4 @@ int main(void) { result = coord_esep(cube); printf("%" PRId64 "\n", result); - - return 0; } diff --git a/test/075_set_eo/set_eo_tests.c b/test/075_set_eo/set_eo_tests.c @@ -4,7 +4,7 @@ int64_t coord_eo(cube_t); void set_eo(cube_t *, int64_t); void pieces(cube_t *, uint8_t [static 8], uint8_t [static 12]); -int main(void) { +void run(void) { char str[STRLENMAX]; cube_t cube; uint8_t edge[12], corner[8]; @@ -33,6 +33,4 @@ int main(void) { writecube("H48", cube, str); printf("%s\n", str); } - - return 0; } diff --git a/test/076_copy_corners/copy_corners_tests.c b/test/076_copy_corners/copy_corners_tests.c @@ -2,7 +2,7 @@ void copy_corners(cube_t *, cube_t); -int main(void) { +void run(void) { char str[STRLENMAX]; cube_t c1, c2; @@ -22,6 +22,4 @@ int main(void) { writecube("H48", c1, str); printf("%s\n", str); } - - return 0; } diff --git a/test/077_copy_edges/copy_edges_tests.c b/test/077_copy_edges/copy_edges_tests.c @@ -2,7 +2,7 @@ void copy_edges(cube_t *, cube_t); -int main(void) { +void run(void) { char str[STRLENMAX]; cube_t c1, c2; @@ -22,6 +22,4 @@ int main(void) { writecube("H48", c1, str); printf("%s\n", str); } - - return 0; } diff --git a/test/078_invcoord_esep/invcoord_esep_tests.c b/test/078_invcoord_esep/invcoord_esep_tests.c @@ -3,7 +3,7 @@ int64_t coord_esep(cube_t); cube_t invcoord_esep(int64_t); -int main(void) { +void run(void) { char str[STRLENMAX]; cube_t cube; int64_t i; @@ -16,6 +16,4 @@ int main(void) { i = coord_esep(cube); printf("%" PRId64 "\n", i); - - return 0; } diff --git a/test/080_allowednext/allowednext_tests.c b/test/080_allowednext/allowednext_tests.c @@ -11,7 +11,7 @@ static char *moves[] = { "B", "B2", "B'", }; -int main(void) { +void run(void) { char movestr[STRLENMAX]; uint8_t m[100]; int n, i, j; @@ -32,6 +32,4 @@ int main(void) { n > 1 ? moves[m[n-2]] : "-", n > 0 ? moves[m[n-1]] : "-"); printf("%s\n", allowednextmove(m, n) ? "true" : "false"); - - return 0; } diff --git a/test/090_solve_simple/solve_simple_tests.c b/test/090_solve_simple/solve_simple_tests.c @@ -3,7 +3,7 @@ int64_t solve(cube_t, char *, char *, char *, int8_t, int8_t, int64_t, int8_t, void *, char *); -int main(void) { +void run(void) { char cubestr[STRLENMAX], solverstr[STRLENMAX], optionsstr[STRLENMAX]; char nisstypestr[STRLENMAX], minmovesstr[STRLENMAX]; char maxmovesstr[STRLENMAX], maxsolsstr[STRLENMAX]; @@ -45,6 +45,4 @@ int main(void) { ); printf("%s", solutionsstr); - - return 0; } diff --git a/test/100_gendata_cocsep/gendata_cocsep_tests.c b/test/100_gendata_cocsep/gendata_cocsep_tests.c @@ -4,7 +4,7 @@ size_t gendata_cocsep(void *, uint64_t *, cube_t *); -int main(void) { +void run(void) { uint32_t buf[300000], i; uint64_t selfsim[COCSEP_CLASSES]; cube_t rep[COCSEP_CLASSES]; @@ -17,6 +17,4 @@ int main(void) { printf("Max value: %" PRIu32 "\n", buf[result/4-11]); for (i = 0; i < 10; i++) printf("%" PRIu32 ": %" PRIu32 "\n", i, buf[result/4-10+i]); - - return 0; } diff --git a/test/101_cocsep_selfsim/cocsep_selfsim_tests.c b/test/101_cocsep_selfsim/cocsep_selfsim_tests.c @@ -12,7 +12,7 @@ size_t gendata_cocsep(void *, uint64_t *, cube_t *); int64_t coord_cocsep(cube_t); -int main(void) { +void run(void) { char str[STRLENMAX]; uint32_t buf[300000], data; int64_t coord, coclass; @@ -35,6 +35,4 @@ int main(void) { } printf("\n"); } - - return 0; } diff --git a/test/102_coord_invcoord_h48/coord_invcoord_h48_tests.c b/test/102_coord_invcoord_h48/coord_invcoord_h48_tests.c @@ -7,7 +7,7 @@ int64_t coord_h48(cube_t, const uint32_t *, uint8_t); cube_t invcoord_h48(int64_t, const cube_t *, uint8_t); cube_t transform(cube_t, uint8_t); -int main(void) { +void run(void) { char str[STRLENMAX]; int i; bool found; @@ -33,6 +33,4 @@ int main(void) { printf("%d %s\n", i, found ? "ok" : "ERROR"); i++; } - - return 0; } diff --git a/test/103_gendata_h48/gendata_h48_tests.c b/test/103_gendata_h48/gendata_h48_tests.c @@ -6,7 +6,7 @@ size_t gendata_h48(void *, uint8_t, uint8_t); -int main(void) { +void run(void) { char str[STRLENMAX]; uint8_t h, i; uint32_t *buf, *h48info; @@ -31,5 +31,4 @@ int main(void) { printf("%" PRIu32 ": %" PRIu32 "\n", i, h48info[i+1]); free(buf); - return 0; } diff --git a/test/test b/test/test @@ -1,41 +0,0 @@ -#!/bin/sh - -detectsan() { cc -fsanitize=$1 -dM -E -x c - </dev/null | grep "SANITIZE"; } - -re="${TEST:-$@}" - -CC="cc -DDEBUG -std=c99 -pedantic -Wall -Wextra \ - -Wno-unused-parameter -Wno-unused-function -g3 -D$CUBETYPE" - -[ "$CUBETYPE" = "CUBE_AVX2" ] && CC="$CC -mavx2" -[ -n "$(detectsan address)" ] && CC="$CC -fsanitize=address" -[ -n "$(detectsan undefined)" ] && CC="$CC -fsanitize=undefined" - -TESTBIN="test/run" -TESTOUT="test/last.out" -TESTERR="test/last.err" -CUBEOBJ="debugcube.o" - -for t in test/*; do - if [ -n "$re" ] && [ -z "$(echo "$t" | grep "$re")" ]; then - continue - fi - if [ ! -d $t ]; then continue; fi - $CC -o $TESTBIN $t/*.c $CUBEOBJ || exit 1; - for cin in $t/*.in; do - c=$(echo "$cin" | sed 's/\.in//') - cout=$c.out - printf "$c: " - $TESTBIN < "$cin" > $TESTOUT 2> $TESTERR - if diff $cout $TESTOUT; then - printf "OK\n" - else - printf "Test failed! stderr:\n" - cat $TESTERR - exit 1 - fi - done -done - -echo "All tests passed!" -rm -rf $TESTBIN $TESTOUT $TESTERR $CUBEOBJ diff --git a/test/test.h b/test/test.h @@ -1,4 +1,5 @@ #include <inttypes.h> +#include <stdarg.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -24,3 +25,15 @@ bool issolvable(cube_t); bool issolved(cube_t); cube_t readcube(char *, char *); void writecube(char *, cube_t, char *); +void nissy_setlogger(void (*logger_function)(const char *, va_list)); + +/* Test function to be implemented by all tests */ +void run(void); + +void log_stderr(const char *str, va_list vl) { vfprintf(stderr, str, vl); } + +int main(void) { + nissy_setlogger(log_stderr); + run(); + return 0; +}