diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-22 16:12:21 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-22 16:12:21 +0200 |
| commit | bab22e1946a656088804b61cf765461b48744971 (patch) | |
| tree | 43ae3798e5c2f4fc63e68df72d134e742fcaf834 /src/core | |
| parent | 93522effba2f200257141189de5925a23fca93b8 (diff) | |
| download | nissy-core-bab22e1946a656088804b61cf765461b48744971.tar.gz nissy-core-bab22e1946a656088804b61cf765461b48744971.zip | |
Update tests for oriented_cube_t
Diffstat (limited to '')
| -rw-r--r-- | src/core/core.h | 1 | ||||
| -rw-r--r-- | src/core/core_types.h | 4 | ||||
| -rw-r--r-- | src/core/cube.h | 99 | ||||
| -rw-r--r-- | src/core/moves.h | 12 | ||||
| -rw-r--r-- | src/core/transform.h | 13 |
5 files changed, 75 insertions, 54 deletions
diff --git a/src/core/core.h b/src/core/core.h index fce1751..c211788 100644 --- a/src/core/core.h +++ b/src/core/core.h | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | #include "core_types.h" | ||
| 1 | #include "constant_cubes.h" | 2 | #include "constant_cubes.h" |
| 2 | #include "cube.h" | 3 | #include "cube.h" |
| 3 | #include "moves.h" | 4 | #include "moves.h" |
diff --git a/src/core/core_types.h b/src/core/core_types.h new file mode 100644 index 0000000..2ad8938 --- /dev/null +++ b/src/core/core_types.h | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | typedef struct { | ||
| 2 | cube_t cube; | ||
| 3 | uint8_t orientation; | ||
| 4 | } oriented_cube_t; | ||
diff --git a/src/core/cube.h b/src/core/cube.h index a045e55..76c3ff1 100644 --- a/src/core/cube.h +++ b/src/core/cube.h | |||
| @@ -1,17 +1,33 @@ | |||
| 1 | STATIC cube_t solvedcube(void); | 1 | #define ZERO_ORIENTED_CUBE ((oriented_cube_t) {0}) |
| 2 | #define SOLVED_ORIENTED_CUBE \ | ||
| 3 | ((oriented_cube_t) { .cube = SOLVED_CUBE, .orientation = 0 }) | ||
| 4 | |||
| 5 | STATIC oriented_cube_t solvedcube(void); | ||
| 2 | STATIC cube_t cubefromarray(uint8_t [static 8], uint8_t [static 12]); | 6 | STATIC cube_t cubefromarray(uint8_t [static 8], uint8_t [static 12]); |
| 3 | STATIC bool isconsistent(cube_t); | 7 | STATIC bool isconsistent(oriented_cube_t); |
| 4 | STATIC bool issolvable(cube_t); | 8 | STATIC bool issolvable(oriented_cube_t); |
| 5 | STATIC bool issolved(cube_t); | 9 | STATIC bool issolved(oriented_cube_t); |
| 6 | STATIC bool iserror(cube_t); | 10 | STATIC bool iserror(oriented_cube_t); |
| 7 | STATIC void getcube_fix(long long *, long long *, long long *, long long *); | 11 | STATIC void getcube_fix(long long *, long long *, long long *, long long *); |
| 8 | STATIC cube_t getcube(int64_t, int64_t, int64_t, int64_t); | 12 | STATIC cube_t getcube(int64_t, int64_t, int64_t, int64_t); |
| 9 | 13 | ||
| 10 | /* This is used only in tests, use SOLVED_CUBE directly everywhere else */ | 14 | STATIC oriented_cube_t readcube(const char *); |
| 11 | STATIC cube_t | 15 | STATIC int64_t writecube(oriented_cube_t, size_t n, char [n]); |
| 16 | STATIC uint8_t readco(const char *); | ||
| 17 | STATIC uint8_t readcp(const char *); | ||
| 18 | STATIC uint8_t readeo(const char *); | ||
| 19 | STATIC uint8_t readep(const char *); | ||
| 20 | |||
| 21 | STATIC uint8_t b32toedge(char); | ||
| 22 | STATIC uint8_t b32tocorner(char); | ||
| 23 | STATIC char edgetob32(uint8_t); | ||
| 24 | STATIC char cornertob32(uint8_t); | ||
| 25 | |||
| 26 | /* This is used only in tests, use SOLVED_ORIENTED_CUBE everywhere else */ | ||
| 27 | STATIC oriented_cube_t | ||
| 12 | solvedcube(void) | 28 | solvedcube(void) |
| 13 | { | 29 | { |
| 14 | return SOLVED_CUBE; | 30 | return SOLVED_ORIENTED_CUBE; |
| 15 | } | 31 | } |
| 16 | 32 | ||
| 17 | STATIC cube_t | 33 | STATIC cube_t |
| @@ -24,12 +40,12 @@ cubefromarray(uint8_t c[static 8], uint8_t e[static 12]) | |||
| 24 | } | 40 | } |
| 25 | 41 | ||
| 26 | STATIC bool | 42 | STATIC bool |
| 27 | isconsistent(cube_t cube) | 43 | isconsistent(oriented_cube_t cube) |
| 28 | { | 44 | { |
| 29 | uint8_t i, p, e, piece, corner[8], edge[12]; | 45 | uint8_t i, p, e, piece, corner[8], edge[12]; |
| 30 | bool found[12]; | 46 | bool found[12]; |
| 31 | 47 | ||
| 32 | pieces(&cube, corner, edge); | 48 | pieces(&cube.cube, corner, edge); |
| 33 | 49 | ||
| 34 | for (i = 0; i < 12; i++) | 50 | for (i = 0; i < 12; i++) |
| 35 | found[i] = false; | 51 | found[i] = false; |
| @@ -63,25 +79,29 @@ isconsistent(cube_t cube) | |||
| 63 | if (!found[i]) | 79 | if (!found[i]) |
| 64 | goto inconsistent_cp; | 80 | goto inconsistent_cp; |
| 65 | 81 | ||
| 82 | if (cube.orientation >= 24) | ||
| 83 | goto inconsistent_orientation; | ||
| 84 | |||
| 66 | return true; | 85 | return true; |
| 67 | 86 | ||
| 68 | inconsistent_ep: | 87 | inconsistent_ep: |
| 69 | inconsistent_cp: | 88 | inconsistent_cp: |
| 70 | inconsistent_eo: | 89 | inconsistent_eo: |
| 71 | inconsistent_co: | 90 | inconsistent_co: |
| 72 | /* We used to do more logging here, hence the 4 different labels */ | 91 | inconsistent_orientation: |
| 92 | /* We used to do more logging here, hence the different labels */ | ||
| 73 | return false; | 93 | return false; |
| 74 | } | 94 | } |
| 75 | 95 | ||
| 76 | STATIC bool | 96 | STATIC bool |
| 77 | issolvable(cube_t cube) | 97 | issolvable(oriented_cube_t cube) |
| 78 | { | 98 | { |
| 79 | uint8_t i, eo, co, piece, edge[12], corner[8], ep[12], cp[8]; | 99 | uint8_t i, eo, co, piece, edge[12], corner[8], ep[12], cp[8]; |
| 80 | 100 | ||
| 81 | DBG_ASSERT(isconsistent(cube), false, | 101 | DBG_ASSERT(isconsistent(cube), false, |
| 82 | "issolvable: cube is inconsistent\n"); | 102 | "issolvable: cube is inconsistent\n"); |
| 83 | 103 | ||
| 84 | pieces(&cube, corner, edge); | 104 | pieces(&cube.cube, corner, edge); |
| 85 | for (i = 0; i < 12; i++) | 105 | for (i = 0; i < 12; i++) |
| 86 | ep[i] = edge[i] & PBITS; | 106 | ep[i] = edge[i] & PBITS; |
| 87 | for (i = 0; i < 8; i++) | 107 | for (i = 0; i < 8; i++) |
| @@ -120,15 +140,15 @@ issolvable_co: | |||
| 120 | } | 140 | } |
| 121 | 141 | ||
| 122 | bool | 142 | bool |
| 123 | issolved(cube_t cube) | 143 | issolved(oriented_cube_t cube) |
| 124 | { | 144 | { |
| 125 | return equal(cube, SOLVED_CUBE); | 145 | return equal(cube.cube, SOLVED_CUBE); |
| 126 | } | 146 | } |
| 127 | 147 | ||
| 128 | bool | 148 | bool |
| 129 | iserror(cube_t cube) | 149 | iserror(oriented_cube_t cube) |
| 130 | { | 150 | { |
| 131 | return equal(cube, ZERO_CUBE); | 151 | return equal(cube.cube, ZERO_CUBE); |
| 132 | } | 152 | } |
| 133 | 153 | ||
| 134 | STATIC void | 154 | STATIC void |
| @@ -175,21 +195,6 @@ getcube(int64_t ep, int64_t eo, int64_t cp, int64_t co) | |||
| 175 | return cubefromarray(carr, earr); | 195 | return cubefromarray(carr, earr); |
| 176 | } | 196 | } |
| 177 | 197 | ||
| 178 | |||
| 179 | /******************************************************************************/ | ||
| 180 | |||
| 181 | STATIC cube_t readcube(const char *); | ||
| 182 | STATIC int64_t writecube(cube_t, size_t n, char [n]); | ||
| 183 | STATIC uint8_t readco(const char *); | ||
| 184 | STATIC uint8_t readcp(const char *); | ||
| 185 | STATIC uint8_t readeo(const char *); | ||
| 186 | STATIC uint8_t readep(const char *); | ||
| 187 | |||
| 188 | STATIC uint8_t b32toedge(char); | ||
| 189 | STATIC uint8_t b32tocorner(char); | ||
| 190 | STATIC char edgetob32(uint8_t); | ||
| 191 | STATIC char cornertob32(uint8_t); | ||
| 192 | |||
| 193 | STATIC uint8_t | 198 | STATIC uint8_t |
| 194 | readco(const char *str) | 199 | readco(const char *str) |
| 195 | { | 200 | { |
| @@ -243,11 +248,11 @@ readep(const char *str) | |||
| 243 | return UINT8_ERROR; | 248 | return UINT8_ERROR; |
| 244 | } | 249 | } |
| 245 | 250 | ||
| 246 | STATIC cube_t | 251 | STATIC oriented_cube_t |
| 247 | readcube(const char *buf) | 252 | readcube(const char *buf) |
| 248 | { | 253 | { |
| 249 | int i; | 254 | int i; |
| 250 | uint8_t c[8], e[12]; | 255 | uint8_t c[8], e[12], orientation; |
| 251 | 256 | ||
| 252 | for (i = 0; i < 8; i++) { | 257 | for (i = 0; i < 8; i++) { |
| 253 | c[i] = b32tocorner(buf[i]); | 258 | c[i] = b32tocorner(buf[i]); |
| @@ -258,14 +263,14 @@ readcube(const char *buf) | |||
| 258 | } else { | 263 | } else { |
| 259 | LOG("(char '%c')\n", buf[i]); | 264 | LOG("(char '%c')\n", buf[i]); |
| 260 | } | 265 | } |
| 261 | return ZERO_CUBE; | 266 | return ZERO_ORIENTED_CUBE; |
| 262 | } | 267 | } |
| 263 | } | 268 | } |
| 264 | 269 | ||
| 265 | if (buf[8] != '=') { | 270 | if (buf[8] != '=') { |
| 266 | LOG("Error reading separator: a single '=' " | 271 | LOG("Error reading separator: a single '=' " |
| 267 | "must be used to separate edges and corners\n"); | 272 | "must be used to separate edges and corners\n"); |
| 268 | return ZERO_CUBE; | 273 | return ZERO_ORIENTED_CUBE; |
| 269 | } | 274 | } |
| 270 | 275 | ||
| 271 | for (i = 0; i < 12; i++) { | 276 | for (i = 0; i < 12; i++) { |
| @@ -277,15 +282,25 @@ readcube(const char *buf) | |||
| 277 | } else { | 282 | } else { |
| 278 | LOG("(char '%c')\n", buf[i+9]); | 283 | LOG("(char '%c')\n", buf[i+9]); |
| 279 | } | 284 | } |
| 280 | return ZERO_CUBE; | 285 | return ZERO_ORIENTED_CUBE; |
| 281 | } | 286 | } |
| 282 | } | 287 | } |
| 283 | 288 | ||
| 284 | return cubefromarray(c, e); | 289 | orientation = (uint8_t)(buf[22] - 'A'); |
| 290 | if (orientation >= 24) { | ||
| 291 | LOG("Error reading orientation: impossible value %" PRIu8 | ||
| 292 | " (%c)\n", orientation, buf[22]); | ||
| 293 | return ZERO_ORIENTED_CUBE; | ||
| 294 | } | ||
| 295 | |||
| 296 | return (oriented_cube_t) { | ||
| 297 | .cube = cubefromarray(c, e), | ||
| 298 | .orientation = orientation | ||
| 299 | }; | ||
| 285 | } | 300 | } |
| 286 | 301 | ||
| 287 | STATIC int64_t | 302 | STATIC int64_t |
| 288 | writecube(cube_t cube, size_t buf_size, char buf[buf_size]) | 303 | writecube(oriented_cube_t cube, size_t buf_size, char buf[buf_size]) |
| 289 | { | 304 | { |
| 290 | int i; | 305 | int i; |
| 291 | uint8_t corner[8], edge[12]; | 306 | uint8_t corner[8], edge[12]; |
| @@ -297,7 +312,7 @@ writecube(cube_t cube, size_t buf_size, char buf[buf_size]) | |||
| 297 | return NISSY_ERROR_BUFFER_SIZE; | 312 | return NISSY_ERROR_BUFFER_SIZE; |
| 298 | } | 313 | } |
| 299 | 314 | ||
| 300 | pieces(&cube, corner, edge); | 315 | pieces(&cube.cube, corner, edge); |
| 301 | 316 | ||
| 302 | for (i = 0; i < 8; i++) | 317 | for (i = 0; i < 8; i++) |
| 303 | buf[i] = cornertob32(corner[i]); | 318 | buf[i] = cornertob32(corner[i]); |
| @@ -307,9 +322,8 @@ writecube(cube_t cube, size_t buf_size, char buf[buf_size]) | |||
| 307 | for (i = 0; i < 12; i++) | 322 | for (i = 0; i < 12; i++) |
| 308 | buf[i+9] = edgetob32(edge[i]); | 323 | buf[i+9] = edgetob32(edge[i]); |
| 309 | 324 | ||
| 310 | /* TODO */ | ||
| 311 | buf[21] = '='; | 325 | buf[21] = '='; |
| 312 | buf[22] = 'A'; | 326 | buf[22] = (char)cube.orientation + 'A'; |
| 313 | buf[23] = '\0'; | 327 | buf[23] = '\0'; |
| 314 | 328 | ||
| 315 | return NISSY_OK; | 329 | return NISSY_OK; |
| @@ -351,4 +365,3 @@ cornertob32(uint8_t corner) | |||
| 351 | 365 | ||
| 352 | return val < 26 ? 'A' + (char)val : 'a' + (char)(val - 26); | 366 | return val < 26 ? 'A' + (char)val : 'a' + (char)(val - 26); |
| 353 | } | 367 | } |
| 354 | /******************************************************************************/ | ||
diff --git a/src/core/moves.h b/src/core/moves.h index 8f91167..7e80946 100644 --- a/src/core/moves.h +++ b/src/core/moves.h | |||
| @@ -21,7 +21,7 @@ STATIC uint8_t inverse_move(uint8_t); | |||
| 21 | STATIC void sortparallel_moves(size_t n, uint8_t [n]); | 21 | STATIC void sortparallel_moves(size_t n, uint8_t [n]); |
| 22 | STATIC bool are_lastmoves_singlecw(size_t n, const uint8_t [n]); | 22 | STATIC bool are_lastmoves_singlecw(size_t n, const uint8_t [n]); |
| 23 | 23 | ||
| 24 | STATIC cube_t applymoves(cube_t, const char *); | 24 | STATIC oriented_cube_t applymoves(oriented_cube_t, const char *); |
| 25 | 25 | ||
| 26 | #define FOREACH_READMOVE(ARG_BUF, ARG_MOVE, ARG_C, ARG_MAX, \ | 26 | #define FOREACH_READMOVE(ARG_BUF, ARG_MOVE, ARG_C, ARG_MAX, \ |
| 27 | RET_ERROR, ARG_ACTION) \ | 27 | RET_ERROR, ARG_ACTION) \ |
| @@ -319,17 +319,17 @@ are_lastmoves_singlecw(size_t n, const uint8_t moves[n]) | |||
| 319 | return isbase(moves[n-1]) && (!two || isbase(moves[n-2])); | 319 | return isbase(moves[n-1]) && (!two || isbase(moves[n-2])); |
| 320 | } | 320 | } |
| 321 | 321 | ||
| 322 | STATIC cube_t | 322 | STATIC oriented_cube_t |
| 323 | applymoves(cube_t cube, const char *buf) | 323 | applymoves(oriented_cube_t cube, const char *buf) |
| 324 | { | 324 | { |
| 325 | int c; | 325 | int c; |
| 326 | uint8_t m; | 326 | uint8_t m; |
| 327 | 327 | ||
| 328 | DBG_ASSERT(isconsistent(cube), ZERO_CUBE, | 328 | DBG_ASSERT(isconsistent(cube), ZERO_ORIENTED_CUBE, |
| 329 | "move error: inconsistent cube\n"); | 329 | "move error: inconsistent cube\n"); |
| 330 | 330 | ||
| 331 | FOREACH_READMOVE(buf, m, c, -1, ZERO_CUBE, | 331 | FOREACH_READMOVE(buf, m, c, -1, ZERO_ORIENTED_CUBE, |
| 332 | cube = move(cube, m); | 332 | cube.cube = move(cube.cube, m); |
| 333 | ) | 333 | ) |
| 334 | 334 | ||
| 335 | return cube; | 335 | return cube; |
diff --git a/src/core/transform.h b/src/core/transform.h index 28b8d93..763a4ae 100644 --- a/src/core/transform.h +++ b/src/core/transform.h | |||
| @@ -23,7 +23,7 @@ STATIC void writetrans(uint8_t, char [static NISSY_SIZE_TRANSFORMATION]); | |||
| 23 | STATIC cube_t transform_edges(cube_t, uint8_t); | 23 | STATIC cube_t transform_edges(cube_t, uint8_t); |
| 24 | STATIC cube_t transform_corners(cube_t, uint8_t); | 24 | STATIC cube_t transform_corners(cube_t, uint8_t); |
| 25 | STATIC cube_t transform(cube_t, uint8_t); | 25 | STATIC cube_t transform(cube_t, uint8_t); |
| 26 | STATIC cube_t applytrans(cube_t, const char *); | 26 | STATIC oriented_cube_t applytrans(oriented_cube_t, const char *); |
| 27 | STATIC_INLINE uint8_t inverse_trans(uint8_t); | 27 | STATIC_INLINE uint8_t inverse_trans(uint8_t); |
| 28 | STATIC uint8_t transform_move(uint8_t, uint8_t); | 28 | STATIC uint8_t transform_move(uint8_t, uint8_t); |
| 29 | STATIC uint64_t symmetry_mask(cube_t); | 29 | STATIC uint64_t symmetry_mask(cube_t); |
| @@ -367,12 +367,12 @@ transform(cube_t c, uint8_t t) | |||
| 367 | } | 367 | } |
| 368 | } | 368 | } |
| 369 | 369 | ||
| 370 | STATIC cube_t | 370 | STATIC oriented_cube_t |
| 371 | applytrans(cube_t cube, const char *buf) | 371 | applytrans(oriented_cube_t cube, const char *buf) |
| 372 | { | 372 | { |
| 373 | uint8_t t; | 373 | uint8_t t; |
| 374 | 374 | ||
| 375 | DBG_ASSERT(isconsistent(cube), ZERO_CUBE, | 375 | DBG_ASSERT(isconsistent(cube), ZERO_ORIENTED_CUBE, |
| 376 | "transformation error: inconsistent cube\n"); | 376 | "transformation error: inconsistent cube\n"); |
| 377 | 377 | ||
| 378 | t = readtrans(buf); | 378 | t = readtrans(buf); |
| @@ -380,7 +380,10 @@ applytrans(cube_t cube, const char *buf) | |||
| 380 | if (t == UINT8_ERROR) | 380 | if (t == UINT8_ERROR) |
| 381 | LOG("Unknown transformation: %s\n", buf); | 381 | LOG("Unknown transformation: %s\n", buf); |
| 382 | 382 | ||
| 383 | return transform(cube, t); | 383 | return (oriented_cube_t){ |
| 384 | .cube = transform(cube.cube, t), | ||
| 385 | .orientation = cube.orientation | ||
| 386 | }; | ||
| 384 | } | 387 | } |
| 385 | 388 | ||
| 386 | STATIC_INLINE uint8_t | 389 | STATIC_INLINE uint8_t |
