From f85ac77efc0feeeab183e509a7baca1c507da3c5 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 5 Sep 2024 17:52:41 +0200 Subject: Added test for invermoves and small refactor --- src/core/moves.h | 54 +++++++++++++++++++++++--------- test/032_invertmoves/001_onemove.in | 1 + test/032_invertmoves/001_onemove.out | 1 + test/032_invertmoves/002_20moves.in | 1 + test/032_invertmoves/002_20moves.out | 1 + test/032_invertmoves/invertmoves_tests.c | 21 +++++++++++++ 6 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 test/032_invertmoves/001_onemove.in create mode 100644 test/032_invertmoves/001_onemove.out create mode 100644 test/032_invertmoves/002_20moves.in create mode 100644 test/032_invertmoves/002_20moves.out create mode 100644 test/032_invertmoves/invertmoves_tests.c diff --git a/src/core/moves.h b/src/core/moves.h index 5d08a66..3f285fb 100644 --- a/src/core/moves.h +++ b/src/core/moves.h @@ -13,9 +13,27 @@ STATIC cube_t premove(cube_t, uint8_t); STATIC uint8_t inverse_move(uint8_t); STATIC void invertmoves(uint8_t *, uint8_t, uint8_t *); +STATIC int readmoves(const char *, int, uint8_t *); STATIC cube_t applymoves(cube_t, const char *); STATIC cube_t frommoves(const char *); +#define FOREACH_READMOVE(ARG_BUF, ARG_MOVE, ARG_C, ARG_MAX, \ + LABEL_ERROR, ARG_ACTION) \ + const char *VAR_B; \ + uint8_t VAR_MOVE_NOMOD, VAR_MOD; \ + for (VAR_B = ARG_BUF, ARG_C = 0; *VAR_B != '\0'; VAR_B++, ARG_C++) { \ + while (*VAR_B == ' ' || *VAR_B == '\t' || *VAR_B == '\n') \ + VAR_B++; \ + if (*VAR_B == '\0' || ARG_C == ARG_MAX) \ + break; \ + if ((VAR_MOVE_NOMOD = readmove(*VAR_B)) == UINT8_ERROR) \ + goto LABEL_ERROR; \ + if ((VAR_MOD = readmodifier(*(VAR_B+1))) != 0) \ + VAR_B++; \ + ARG_MOVE = VAR_MOVE_NOMOD + VAR_MOD; \ + ARG_ACTION \ + } + STATIC bool allowednextmove(uint8_t *moves, uint8_t n) { @@ -173,28 +191,36 @@ invertmoves(uint8_t *moves, uint8_t nmoves, uint8_t *ret) ret[i] = inverse_move(moves[nmoves - i - 1]); } +STATIC int +readmoves(const char *buf, int max, uint8_t *ret) +{ + uint8_t m; + int c; + + FOREACH_READMOVE(buf, m, c, max, readmoves_error, + ret[c] = m; + ) + + return c; + +readmoves_error: + LOG("readmoves error\n"); + return -1; +} + STATIC cube_t applymoves(cube_t cube, const char *buf) { - uint8_t r, m; - const char *b; + int c; + uint8_t m; DBG_ASSERT(isconsistent(cube), ZERO_CUBE, "move error: inconsistent cube\n"); - for (b = buf; *b != '\0'; b++) { - while (*b == ' ' || *b == '\t' || *b == '\n') - b++; - if (*b == '\0') - goto applymoves_finish; - if ((r = readmove(*b)) == UINT8_ERROR) - goto applymoves_error; - if ((m = readmodifier(*(b+1))) != 0) - b++; - cube = move(cube, r + m); - } + FOREACH_READMOVE(buf, m, c, -1, applymoves_error, + cube = move(cube, m); + ) -applymoves_finish: return cube; applymoves_error: diff --git a/test/032_invertmoves/001_onemove.in b/test/032_invertmoves/001_onemove.in new file mode 100644 index 0000000..cf84443 --- /dev/null +++ b/test/032_invertmoves/001_onemove.in @@ -0,0 +1 @@ +F diff --git a/test/032_invertmoves/001_onemove.out b/test/032_invertmoves/001_onemove.out new file mode 100644 index 0000000..6ab6e07 --- /dev/null +++ b/test/032_invertmoves/001_onemove.out @@ -0,0 +1 @@ +F' diff --git a/test/032_invertmoves/002_20moves.in b/test/032_invertmoves/002_20moves.in new file mode 100644 index 0000000..052ed94 --- /dev/null +++ b/test/032_invertmoves/002_20moves.in @@ -0,0 +1 @@ +B2 R D2 B2 R D2 F2 L2 F2 R' F2 D' L D2 L U2 L B2 F2 U diff --git a/test/032_invertmoves/002_20moves.out b/test/032_invertmoves/002_20moves.out new file mode 100644 index 0000000..bc6a079 --- /dev/null +++ b/test/032_invertmoves/002_20moves.out @@ -0,0 +1 @@ +U' F2 B2 L' U2 L' D2 L' D F2 R F2 L2 F2 D2 R' B2 D2 R' B2 diff --git a/test/032_invertmoves/invertmoves_tests.c b/test/032_invertmoves/invertmoves_tests.c new file mode 100644 index 0000000..acb2adc --- /dev/null +++ b/test/032_invertmoves/invertmoves_tests.c @@ -0,0 +1,21 @@ +#include "../test.h" + +#define MAXMOVES 20 + +int64_t readmoves(const char *, int, uint8_t *); +void writemoves(uint8_t *, int, char *); +void invertmoves(uint8_t *, uint8_t, uint8_t *); + +void run(void) { + char movestr[STRLENMAX], outstr[STRLENMAX]; + uint8_t moves[MAXMOVES], ret[MAXMOVES]; + int c; + + fgets(movestr, STRLENMAX, stdin); + c = readmoves(movestr, MAXMOVES, moves); + + invertmoves(moves, c, ret); + writemoves(ret, c, outstr); + + printf("%s\n", outstr); +} -- cgit v1.3