diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-02-10 23:30:20 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-02-10 23:30:20 +0100 |
| commit | 3bfd0bb403d62bd942d1912d085ea59b156062fd (patch) | |
| tree | 913ddcf693d89009fa033e245b997e27211d00ef | |
| parent | 374c33d1412f8f3a35dd3eb874dcaef82d60fff4 (diff) | |
| download | nissy-3bfd0bb403d62bd942d1912d085ea59b156062fd.tar.gz nissy-3bfd0bb403d62bd942d1912d085ea59b156062fd.zip | |
Added (just a few) tests for alg and added fields to alg struct
| -rw-r--r-- | TODO/2.1.md | 27 | ||||
| -rw-r--r-- | src/alg.c | 107 | ||||
| -rw-r--r-- | src/alg.h | 10 | ||||
| -rw-r--r-- | src/cubetypes.h | 4 | ||||
| -rw-r--r-- | src/solve.c | 24 | ||||
| -rw-r--r-- | tests/alg_tests.c | 266 | ||||
| -rw-r--r-- | tests/alg_tests.h | 20 | ||||
| -rw-r--r-- | tests/test.c | 3 |
8 files changed, 392 insertions, 69 deletions
diff --git a/TODO/2.1.md b/TODO/2.1.md index 92e4555..d65eb93 100644 --- a/TODO/2.1.md +++ b/TODO/2.1.md | |||
| @@ -1,5 +1,31 @@ | |||
| 1 | # TODO-list for version 2.1 (or is it 3.0 at this point?) | 1 | # TODO-list for version 2.1 (or is it 3.0 at this point?) |
| 2 | 2 | ||
| 3 | ## Alg and moveset changes (prerequisite for solve.h) | ||
| 4 | |||
| 5 | ### moveset.h | ||
| 6 | * split off from alg.h | ||
| 7 | |||
| 8 | ### alg.h | ||
| 9 | * There is a (future) bug in the way the solver checks if a move can | ||
| 10 | be appended (allowed_next and similar): the last two moves are not enough. | ||
| 11 | * Example: using QTM we have last 3 moves U U D. Considering only last 2, | ||
| 12 | U could be appended, but it cannot (cancel to U'). | ||
| 13 | * Solution: the per-moveset bool allowed_next() should take an alg as | ||
| 14 | parameter. There are going to be basically two versions, one for QTM and | ||
| 15 | one for HTM (but more may be added). | ||
| 16 | * Alg should be extended to remember the list of moves on inverse / normal | ||
| 17 | separately (without looping over moves). | ||
| 18 | * Maybe another parameter to know if it can assume there has not been | ||
| 19 | any double switching, i.e. if the last moves are the only ones to | ||
| 20 | be checked and there is no need to go back further (e.g. if alg is | ||
| 21 | U (... stuff on inverse ...) D I don't want to have to check back | ||
| 22 | to the U, but in practice we can often assume this does not happen). | ||
| 23 | * Then we can remove last and lastinv from dfsdata. | ||
| 24 | * move also can_niss to alg.h | ||
| 25 | * the check for the order of the moves (to avoid counting L R and R L as | ||
| 26 | different) can be made separately. Maybe add a "compare" function for moves, | ||
| 27 | such that non-commuting moves are not comparable (return -1 0 1). | ||
| 28 | |||
| 3 | ## Rework solver | 29 | ## Rework solver |
| 4 | 30 | ||
| 5 | * The architecture is the following: solve.h contains a solve() public | 31 | * The architecture is the following: solve.h contains a solve() public |
| @@ -24,7 +50,6 @@ necessary). Maybe cleanup solveoptions too (e.g. threads not necessary). | |||
| 24 | * solve.h depends only on moves (dependency on step and trans is removed). | 50 | * solve.h depends only on moves (dependency on step and trans is removed). |
| 25 | * preparation step should be reworked, maybe removed or delegated to the | 51 | * preparation step should be reworked, maybe removed or delegated to the |
| 26 | specific implementations. | 52 | specific implementations. |
| 27 | * allowed_moves and cancel_niss are moved to move.h. | ||
| 28 | * All dfs stuff in the same function. Maybe remove also solvestop. | 53 | * All dfs stuff in the same function. Maybe remove also solvestop. |
| 29 | * Move two-step solve to a different module | 54 | * Move two-step solve to a different module |
| 30 | 55 | ||
| @@ -84,30 +84,42 @@ append_move(Alg *alg, Move m, bool inverse) | |||
| 84 | alg->move[alg->len] = m; | 84 | alg->move[alg->len] = m; |
| 85 | alg->inv [alg->len] = inverse; | 85 | alg->inv [alg->len] = inverse; |
| 86 | alg->len++; | 86 | alg->len++; |
| 87 | |||
| 88 | if (inverse) | ||
| 89 | alg->move_inverse[alg->len_inverse++] = m; | ||
| 90 | else | ||
| 91 | alg->move_normal[alg->len_normal++] = m; | ||
| 87 | } | 92 | } |
| 88 | 93 | ||
| 89 | static int | 94 | static int |
| 90 | axis(Move m) | 95 | axis(Move m) |
| 91 | { | 96 | { |
| 92 | if (m == NULLMOVE) | 97 | static int aux[] = { |
| 93 | return 0; | 98 | [NULLMOVE] = 0, |
| 94 | 99 | ||
| 95 | if (m >= U && m <= B3) | 100 | [U] = 1, [U2] = 1, [U3] = 1, |
| 96 | return (m-1)/6 + 1; | 101 | [D] = 1, [D2] = 1, [D3] = 1, |
| 97 | 102 | [Uw] = 1, [Uw2] = 1, [Uw3] = 1, | |
| 98 | if (m >= Uw && m <= Bw3) | 103 | [Dw] = 1, [Dw2] = 1, [Dw3] = 1, |
| 99 | return (m-1)/6 - 2; | 104 | [E] = 1, [E2] = 1, [E3] = 1, |
| 105 | [y] = 1, [y2] = 1, [y3] = 1, | ||
| 100 | 106 | ||
| 101 | if (base_move(m) == E || base_move(m) == y) | 107 | [R] = 2, [R2] = 2, [R3] = 2, |
| 102 | return 1; | 108 | [L] = 2, [L2] = 2, [L3] = 2, |
| 109 | [Rw] = 2, [Rw2] = 2, [Rw3] = 2, | ||
| 110 | [Lw] = 2, [Lw2] = 2, [Lw3] = 2, | ||
| 111 | [M] = 2, [M2] = 2, [M3] = 2, | ||
| 112 | [x] = 2, [x2] = 2, [x3] = 2, | ||
| 103 | 113 | ||
| 104 | if (base_move(m) == M || base_move(m) == x) | 114 | [F] = 3, [F2] = 3, [F3] = 3, |
| 105 | return 2; | 115 | [B] = 3, [B2] = 3, [B3] = 3, |
| 106 | 116 | [Fw] = 3, [Fw2] = 3, [Fw3] = 3, | |
| 107 | if (base_move(m) == S || base_move(m) == z) | 117 | [Bw] = 3, [Bw2] = 3, [Bw3] = 3, |
| 108 | return 3; | 118 | [S] = 3, [S2] = 3, [S3] = 3, |
| 119 | [z] = 3, [z2] = 3, [z3] = 3, | ||
| 120 | }; | ||
| 109 | 121 | ||
| 110 | return -1; | 122 | return aux[m]; |
| 111 | } | 123 | } |
| 112 | 124 | ||
| 113 | Move | 125 | Move |
| @@ -137,7 +149,7 @@ compose_alg(Alg *alg1, Alg *alg2) | |||
| 137 | void | 149 | void |
| 138 | copy_alg(Alg *src, Alg *dst) | 150 | copy_alg(Alg *src, Alg *dst) |
| 139 | { | 151 | { |
| 140 | dst->len = 0; /* Overwrites */ | 152 | dst->len = dst->len_normal = dst->len_inverse = 0; |
| 141 | compose_alg(dst, src); | 153 | compose_alg(dst, src); |
| 142 | } | 154 | } |
| 143 | 155 | ||
| @@ -169,16 +181,6 @@ free_alglistnode(AlgListNode *aln) | |||
| 169 | free(aln); | 181 | free(aln); |
| 170 | } | 182 | } |
| 171 | 183 | ||
| 172 | void | ||
| 173 | inplace(Alg * (*f)(Alg *), Alg *alg) | ||
| 174 | { | ||
| 175 | Alg *aux; | ||
| 176 | |||
| 177 | aux = f(alg); | ||
| 178 | copy_alg(aux, alg); | ||
| 179 | free(aux); | ||
| 180 | } | ||
| 181 | |||
| 182 | Alg * | 184 | Alg * |
| 183 | inverse_alg(Alg *alg) | 185 | inverse_alg(Alg *alg) |
| 184 | { | 186 | { |
| @@ -234,10 +236,14 @@ new_alg(char *str) | |||
| 234 | Move j, m; | 236 | Move j, m; |
| 235 | 237 | ||
| 236 | alg = malloc(sizeof(Alg)); | 238 | alg = malloc(sizeof(Alg)); |
| 237 | alg->move = malloc(30 * sizeof(Move)); | 239 | alg->allocated = 30; |
| 238 | alg->inv = malloc(30 * sizeof(bool)); | 240 | alg->move = malloc(alg->allocated * sizeof(Move)); |
| 239 | alg->allocated = 30; | 241 | alg->inv = malloc(alg->allocated * sizeof(bool)); |
| 240 | alg->len = 0; | 242 | alg->move_normal = malloc(alg->allocated * sizeof(Move)); |
| 243 | alg->move_inverse = malloc(alg->allocated * sizeof(Move)); | ||
| 244 | alg->len = 0; | ||
| 245 | alg->len_normal = 0; | ||
| 246 | alg->len_inverse = 0; | ||
| 241 | 247 | ||
| 242 | niss = false; | 248 | niss = false; |
| 243 | for (i = 0; str[i]; i++) { | 249 | for (i = 0; str[i]; i++) { |
| @@ -246,13 +252,13 @@ new_alg(char *str) | |||
| 246 | 252 | ||
| 247 | if (str[i] == '(' && niss) { | 253 | if (str[i] == '(' && niss) { |
| 248 | fprintf(stderr, "Error reading moves: nested ( )\n"); | 254 | fprintf(stderr, "Error reading moves: nested ( )\n"); |
| 249 | alg->len = 0; | 255 | alg->len = alg->len_normal = alg->len_inverse = 0; |
| 250 | return alg; | 256 | return alg; |
| 251 | } | 257 | } |
| 252 | 258 | ||
| 253 | if (str[i] == ')' && !niss) { | 259 | if (str[i] == ')' && !niss) { |
| 254 | fprintf(stderr, "Error reading moves: unmatched )\n"); | 260 | fprintf(stderr, "Error reading moves: unmatched )\n"); |
| 255 | alg->len = 0; | 261 | alg->len = alg->len_normal = alg->len_inverse = 0; |
| 256 | return alg; | 262 | return alg; |
| 257 | } | 263 | } |
| 258 | 264 | ||
| @@ -319,7 +325,7 @@ new_alg(char *str) | |||
| 319 | 325 | ||
| 320 | if (niss) { | 326 | if (niss) { |
| 321 | fprintf(stderr, "Error reading moves: unmatched (\n"); | 327 | fprintf(stderr, "Error reading moves: unmatched (\n"); |
| 322 | alg->len = 0; | 328 | alg->len = alg->len_normal = alg->len_inverse = 0; |
| 323 | } | 329 | } |
| 324 | 330 | ||
| 325 | return alg; | 331 | return alg; |
| @@ -349,6 +355,19 @@ on_inverse(Alg *alg) | |||
| 349 | return ret; | 355 | return ret; |
| 350 | } | 356 | } |
| 351 | 357 | ||
| 358 | bool | ||
| 359 | possible_next(Move m, Moveset *ms, Move l0, Move l1) | ||
| 360 | { | ||
| 361 | bool allowed, order; | ||
| 362 | uint64_t mbit; | ||
| 363 | |||
| 364 | mbit = ((uint64_t)1) << m; | ||
| 365 | allowed = mbit & ms->mask[l1][l0]; | ||
| 366 | order = !commute(l0, m) || l0 < m; | ||
| 367 | |||
| 368 | return allowed && order; | ||
| 369 | } | ||
| 370 | |||
| 352 | void | 371 | void |
| 353 | print_alg(Alg *alg, bool l) | 372 | print_alg(Alg *alg, bool l) |
| 354 | { | 373 | { |
| @@ -404,8 +423,10 @@ realloc_alg(Alg *alg, int n) | |||
| 404 | fprintf(stderr, "something might go wrong.\n"); | 423 | fprintf(stderr, "something might go wrong.\n"); |
| 405 | } | 424 | } |
| 406 | 425 | ||
| 407 | alg->move = realloc(alg->move, n * sizeof(int)); | 426 | alg->move = realloc(alg->move, n * sizeof(int)); |
| 408 | alg->inv = realloc(alg->inv, n * sizeof(int)); | 427 | alg->inv = realloc(alg->inv, n * sizeof(int)); |
| 428 | alg->move_normal = realloc(alg->move_normal, n * sizeof(int)); | ||
| 429 | alg->move_inverse = realloc(alg->move_inverse, n * sizeof(int)); | ||
| 409 | alg->allocated = n; | 430 | alg->allocated = n; |
| 410 | } | 431 | } |
| 411 | 432 | ||
| @@ -455,14 +476,12 @@ unniss(Alg *alg) | |||
| 455 | 476 | ||
| 456 | ret = new_alg(""); | 477 | ret = new_alg(""); |
| 457 | 478 | ||
| 458 | for (i = 0; i < alg->len; i++) | 479 | for (i = 0; i < alg->len_normal; i++) |
| 459 | if (!alg->inv[i]) | 480 | append_move(ret, alg->move_normal[i], false); |
| 460 | append_move(ret, alg->move[i], false); | 481 | |
| 461 | 482 | for (i = 0; i < alg->len_inverse; i++) | |
| 462 | for (i = alg->len-1; i >= 0; i--) | 483 | append_move(ret, inverse_move(alg->move_inverse[i]), false); |
| 463 | if (alg->inv[i]) | 484 | |
| 464 | append_move(ret, inverse_move(alg->move[i]), false); | ||
| 465 | |||
| 466 | return ret; | 485 | return ret; |
| 467 | } | 486 | } |
| 468 | 487 | ||
| @@ -483,7 +502,7 @@ init_moveset(Moveset *ms) | |||
| 483 | for (l1 = 0; l1 < NMOVES; l1++) { | 502 | for (l1 = 0; l1 < NMOVES; l1++) { |
| 484 | for (l2 = 0; l2 < NMOVES; l2++) { | 503 | for (l2 = 0; l2 < NMOVES; l2++) { |
| 485 | ms->mask[l2][l1] = 0; | 504 | ms->mask[l2][l1] = 0; |
| 486 | for (l=0; ms->sorted_moves[l]!=NULLMOVE; l++) { | 505 | for (l = 0; ms->sorted_moves[l] != NULLMOVE; l++) { |
| 487 | m = ms->sorted_moves[l]; | 506 | m = ms->sorted_moves[l]; |
| 488 | if (ms->allowed_next(l2, l1, m)) | 507 | if (ms->allowed_next(l2, l1, m)) |
| 489 | ms->mask[l2][l1] |= (one<<m); | 508 | ms->mask[l2][l1] |= (one<<m); |
| @@ -15,6 +15,11 @@ bool allowed_eofb(Move m); | |||
| 15 | bool allowed_drud(Move m); | 15 | bool allowed_drud(Move m); |
| 16 | bool allowed_htr(Move m); | 16 | bool allowed_htr(Move m); |
| 17 | bool allowed_next_all(Move l2, Move l1, Move m); | 17 | bool allowed_next_all(Move l2, Move l1, Move m); |
| 18 | |||
| 19 | void moveset_to_list(Moveset ms, Move *lst); | ||
| 20 | void init_moveset(Moveset *ms); | ||
| 21 | bool possible_next(Move m, Moveset *ms, Move l0, Move l1); | ||
| 22 | |||
| 18 | void append_alg(AlgList *l, Alg *alg); | 23 | void append_alg(AlgList *l, Alg *alg); |
| 19 | void append_move(Alg *alg, Move m, bool inverse); | 24 | void append_move(Alg *alg, Move m, bool inverse); |
| 20 | Move base_move(Move m); | 25 | Move base_move(Move m); |
| @@ -23,12 +28,9 @@ bool commute(Move m1, Move m2); | |||
| 23 | void copy_alg(Alg *src, Alg *dst); | 28 | void copy_alg(Alg *src, Alg *dst); |
| 24 | void free_alg(Alg *alg); | 29 | void free_alg(Alg *alg); |
| 25 | void free_alglist(AlgList *l); | 30 | void free_alglist(AlgList *l); |
| 26 | void inplace(Alg * (*f)(Alg *), Alg *alg); | ||
| 27 | Alg * inverse_alg(Alg *alg); | 31 | Alg * inverse_alg(Alg *alg); |
| 28 | Move inverse_move(Move m); | 32 | Move inverse_move(Move m); |
| 29 | char * move_string(Move m); | 33 | char * move_string(Move m); |
| 30 | void movelist_to_position(Move *ml, int *pos); | ||
| 31 | void moveset_to_list(Moveset ms, Move *lst); | ||
| 32 | Alg * new_alg(char *str); | 34 | Alg * new_alg(char *str); |
| 33 | AlgList * new_alglist(); | 35 | AlgList * new_alglist(); |
| 34 | Alg * on_inverse(Alg *alg); | 36 | Alg * on_inverse(Alg *alg); |
| @@ -38,8 +40,6 @@ void swapmove(Move *m1, Move *m2); | |||
| 38 | char * trans_string(Trans t); /* Here because similar to move_string, move? */ | 40 | char * trans_string(Trans t); /* Here because similar to move_string, move? */ |
| 39 | Alg * unniss(Alg *alg); | 41 | Alg * unniss(Alg *alg); |
| 40 | 42 | ||
| 41 | void init_moveset(Moveset *ms); | ||
| 42 | |||
| 43 | /* Movesets ******************************************************************/ | 43 | /* Movesets ******************************************************************/ |
| 44 | 44 | ||
| 45 | #ifndef ALG_C | 45 | #ifndef ALG_C |
diff --git a/src/cubetypes.h b/src/cubetypes.h index 565dddb..8d38904 100644 --- a/src/cubetypes.h +++ b/src/cubetypes.h | |||
| @@ -122,6 +122,10 @@ alg | |||
| 122 | bool * inv; | 122 | bool * inv; |
| 123 | int len; | 123 | int len; |
| 124 | int allocated; | 124 | int allocated; |
| 125 | Move * move_normal; | ||
| 126 | int len_normal; | ||
| 127 | Move * move_inverse; | ||
| 128 | int len_inverse; | ||
| 125 | }; | 129 | }; |
| 126 | 130 | ||
| 127 | struct | 131 | struct |
diff --git a/src/solve.c b/src/solve.c index df15cca..d5a8f3d 100644 --- a/src/solve.c +++ b/src/solve.c | |||
| @@ -4,7 +4,6 @@ | |||
| 4 | 4 | ||
| 5 | /* Local functions ***********************************************************/ | 5 | /* Local functions ***********************************************************/ |
| 6 | 6 | ||
| 7 | static bool allowed_next(Move move, Step *s, Move l0, Move l1); | ||
| 8 | static bool cancel_niss(DfsArg *arg); | 7 | static bool cancel_niss(DfsArg *arg); |
| 9 | static void copy_dfsarg(DfsArg *src, DfsArg *dst); | 8 | static void copy_dfsarg(DfsArg *src, DfsArg *dst); |
| 10 | static void dfs(DfsArg *arg); | 9 | static void dfs(DfsArg *arg); |
| @@ -19,19 +18,6 @@ static bool solvestop(int d, int op, SolveOptions *opts, AlgList *sols); | |||
| 19 | /* Local functions ***********************************************************/ | 18 | /* Local functions ***********************************************************/ |
| 20 | 19 | ||
| 21 | static bool | 20 | static bool |
| 22 | allowed_next(Move m, Step *s, Move l0, Move l1) | ||
| 23 | { | ||
| 24 | bool allowed, order; | ||
| 25 | uint64_t mbit; | ||
| 26 | |||
| 27 | mbit = ((uint64_t)1) << m; | ||
| 28 | allowed = mbit & s->moveset->mask[l1][l0]; | ||
| 29 | order = !commute(l0, m) || l0 < m; | ||
| 30 | |||
| 31 | return allowed && order; | ||
| 32 | } | ||
| 33 | |||
| 34 | static bool | ||
| 35 | cancel_niss(DfsArg *arg) | 21 | cancel_niss(DfsArg *arg) |
| 36 | { | 22 | { |
| 37 | Moveset *ms; | 23 | Moveset *ms; |
| @@ -82,16 +68,14 @@ copy_dfsarg(DfsArg *src, DfsArg *dst) | |||
| 82 | dst->ind[i].t = src->ind[i].t; | 68 | dst->ind[i].t = src->ind[i].t; |
| 83 | } | 69 | } |
| 84 | 70 | ||
| 85 | /* | ||
| 86 | src->s->copy_extra(src, dst); | 71 | src->s->copy_extra(src, dst); |
| 87 | */ | ||
| 88 | } | 72 | } |
| 89 | 73 | ||
| 90 | static void | 74 | static void |
| 91 | dfs(DfsArg *arg) | 75 | dfs(DfsArg *arg) |
| 92 | { | 76 | { |
| 93 | int i; | 77 | int i; |
| 94 | Move m; | 78 | Move m, l0, l1; |
| 95 | DfsArg newarg; | 79 | DfsArg newarg; |
| 96 | 80 | ||
| 97 | if (dfs_move_checkstop(arg)) | 81 | if (dfs_move_checkstop(arg)) |
| @@ -105,9 +89,11 @@ dfs(DfsArg *arg) | |||
| 105 | 89 | ||
| 106 | for (i = 0; arg->s->moveset->sorted_moves[i] != NULLMOVE; i++) { | 90 | for (i = 0; arg->s->moveset->sorted_moves[i] != NULLMOVE; i++) { |
| 107 | m = arg->s->moveset->sorted_moves[i]; | 91 | m = arg->s->moveset->sorted_moves[i]; |
| 108 | if (allowed_next(m, arg->s, arg->last[0], arg->last[1])) { | 92 | l0 = arg->last[0]; |
| 93 | l1 = arg->last[1]; | ||
| 94 | if (possible_next(m, arg->s->moveset, l0, l1)) { | ||
| 109 | copy_dfsarg(arg, &newarg); | 95 | copy_dfsarg(arg, &newarg); |
| 110 | newarg.last[1] = arg->last[0]; | 96 | newarg.last[1] = l0; |
| 111 | newarg.last[0] = m; | 97 | newarg.last[0] = m; |
| 112 | append_move(arg->current_alg, m, newarg.niss); | 98 | append_move(arg->current_alg, m, newarg.niss); |
| 113 | dfs(&newarg); | 99 | dfs(&newarg); |
diff --git a/tests/alg_tests.c b/tests/alg_tests.c new file mode 100644 index 0000000..9ac1595 --- /dev/null +++ b/tests/alg_tests.c | |||
| @@ -0,0 +1,266 @@ | |||
| 1 | #include "alg_tests.h" | ||
| 2 | |||
| 3 | bool testmethod_append_move(void *); | ||
| 4 | bool testmethod_new_alg(void *); | ||
| 5 | /* | ||
| 6 | bool testmethod_compose_alg(void *); | ||
| 7 | bool testmethod_inverse_alg(void *); | ||
| 8 | bool testmethod_on_inverse(void *); | ||
| 9 | bool testmethod_unniss(void *); | ||
| 10 | */ | ||
| 11 | |||
| 12 | typedef struct { | ||
| 13 | Move *move; | ||
| 14 | bool *inv; | ||
| 15 | int len; | ||
| 16 | Move m; | ||
| 17 | bool inverse; | ||
| 18 | } append_move_t; | ||
| 19 | |||
| 20 | Move m_app1[] = {F, x, D3}; | ||
| 21 | bool i_app1[] = {true, false, false}; | ||
| 22 | append_move_t append_move_case1 = { | ||
| 23 | .move = m_app1, | ||
| 24 | .inv = i_app1, | ||
| 25 | .len = 3, | ||
| 26 | .m = L3, | ||
| 27 | .inverse = false, | ||
| 28 | }; | ||
| 29 | |||
| 30 | Move m_app2[] = {S, U, x2, M2}; | ||
| 31 | bool i_app2[] = {true, false, true, true}; | ||
| 32 | append_move_t append_move_case2 = { | ||
| 33 | .move = m_app2, | ||
| 34 | .inv = i_app2, | ||
| 35 | .len = 4, | ||
| 36 | .m = R, | ||
| 37 | .inverse = true, | ||
| 38 | }; | ||
| 39 | |||
| 40 | Move m_app3[] = {U, U, U, U, U}; | ||
| 41 | bool i_app3[] = {false, false, false, false, false}; | ||
| 42 | append_move_t append_move_case3 = { | ||
| 43 | .move = m_app3, | ||
| 44 | .inv = i_app3, | ||
| 45 | .len = 5, | ||
| 46 | .m = U, | ||
| 47 | .inverse = false, | ||
| 48 | }; | ||
| 49 | |||
| 50 | append_move_t *append_move_cases[] = { | ||
| 51 | &append_move_case1, | ||
| 52 | &append_move_case2, | ||
| 53 | &append_move_case3, | ||
| 54 | }; | ||
| 55 | |||
| 56 | Test test_append_move = { | ||
| 57 | .name = "Appending a move to and alg", | ||
| 58 | .t = testmethod_append_move, | ||
| 59 | .cases = (void **)append_move_cases, | ||
| 60 | }; | ||
| 61 | |||
| 62 | typedef struct { | ||
| 63 | char *str; | ||
| 64 | Move *move; | ||
| 65 | bool *inv; | ||
| 66 | int len; | ||
| 67 | Move *move_normal; | ||
| 68 | int len_normal; | ||
| 69 | Move *move_inverse; | ||
| 70 | int len_inverse; | ||
| 71 | } new_alg_t; | ||
| 72 | |||
| 73 | /* Alg F U B' (L3 D) x M (S) y3 */ | ||
| 74 | Move m_new1[] = {F, U, B3, L3, D, x, M, S, y3}; | ||
| 75 | Move mn_new1[] = {F, U, B3, x, M, y3}; | ||
| 76 | Move mi_new1[] = {L3, D, S}; | ||
| 77 | bool i_new1[] = {false, false, false, true, true, false, false, true, false}; | ||
| 78 | new_alg_t new_alg_case1 = { | ||
| 79 | .str = "F U B' (L3 D) x M (S) y3", | ||
| 80 | .move = m_new1, | ||
| 81 | .inv = i_new1, | ||
| 82 | .len = 9, | ||
| 83 | .move_normal = mn_new1, | ||
| 84 | .len_normal = 6, | ||
| 85 | .move_inverse = mi_new1, | ||
| 86 | .len_inverse = 3, | ||
| 87 | }; | ||
| 88 | new_alg_t *new_alg_cases[] = {&new_alg_case1}; | ||
| 89 | |||
| 90 | Test test_new_alg = { | ||
| 91 | .name = "Initializing an alg from a string", | ||
| 92 | .t = testmethod_new_alg, | ||
| 93 | .cases = (void **)new_alg_cases, | ||
| 94 | }; | ||
| 95 | |||
| 96 | Test *alg_all_tests[] = { | ||
| 97 | &test_append_move, | ||
| 98 | &test_new_alg, | ||
| 99 | /* | ||
| 100 | &test_append_alg, | ||
| 101 | &test_compose_alg, | ||
| 102 | &test_inverse_alg, | ||
| 103 | &test_on_inverse, | ||
| 104 | &test_unniss, | ||
| 105 | */ | ||
| 106 | NULL | ||
| 107 | }; | ||
| 108 | TestSuite alg_suite = { | ||
| 109 | .setup = NULL, | ||
| 110 | .tests = alg_all_tests, | ||
| 111 | .teardown = NULL, | ||
| 112 | }; | ||
| 113 | |||
| 114 | TestSuite *alg_suites[] = { | ||
| 115 | &alg_suite, | ||
| 116 | NULL | ||
| 117 | }; | ||
| 118 | |||
| 119 | bool | ||
| 120 | testmethod_append_move(void *a) | ||
| 121 | { | ||
| 122 | append_move_t *b = (append_move_t *)a; | ||
| 123 | int li, ln; | ||
| 124 | Alg *alg; | ||
| 125 | |||
| 126 | /* Small to test reallocation */ | ||
| 127 | alg = malloc(sizeof(Alg)); | ||
| 128 | alg->allocated = 5; | ||
| 129 | alg->move = malloc(alg->allocated * sizeof(Move)); | ||
| 130 | alg->inv = malloc(alg->allocated * sizeof(bool)); | ||
| 131 | alg->len = b->len; | ||
| 132 | memcpy(alg->move, b->move, alg->len * sizeof(Move)); | ||
| 133 | memcpy(alg->inv, b->inv, alg->len * sizeof(bool)); | ||
| 134 | alg->move_normal = malloc(alg->allocated * sizeof(Move)); | ||
| 135 | alg->move_inverse = malloc(alg->allocated * sizeof(Move)); | ||
| 136 | |||
| 137 | li = ln = 0; | ||
| 138 | for (int i = 0; i < alg->len; i++) { | ||
| 139 | if (alg->inv[i]) | ||
| 140 | alg->move_inverse[li++] = alg->move[i]; | ||
| 141 | else | ||
| 142 | alg->move_normal[ln++] = alg->move[i]; | ||
| 143 | } | ||
| 144 | alg->len_inverse = li; | ||
| 145 | alg->len_normal = ln; | ||
| 146 | |||
| 147 | append_move(alg, b->m, b->inverse); | ||
| 148 | |||
| 149 | if (alg->len != b->len + 1) { | ||
| 150 | printf("Alg has wrong len (%d instead of %d)\n", | ||
| 151 | alg->len, b->len + 1); | ||
| 152 | goto append_move_fail; | ||
| 153 | } | ||
| 154 | if (alg->move[alg->len-1] != b->m) { | ||
| 155 | printf("Wrong last move (%s instead of %s)\n", | ||
| 156 | move_string(alg->move[alg->len-1]), | ||
| 157 | move_string(b->m)); | ||
| 158 | goto append_move_fail; | ||
| 159 | } | ||
| 160 | if (alg->inv[alg->len-1] != b->inverse) { | ||
| 161 | printf("Wrong inverse flag for last move " | ||
| 162 | "(%s instead of %s)\n", | ||
| 163 | b->inverse ? "normal" : "inverse", | ||
| 164 | b->inverse ? "inverse" : "normal"); | ||
| 165 | goto append_move_fail; | ||
| 166 | } | ||
| 167 | if (b->inverse) { | ||
| 168 | if (alg->len_inverse != li + 1 || | ||
| 169 | alg->len_normal != ln) { | ||
| 170 | printf("%d moves on normal (should be %d)" | ||
| 171 | " and %d on inverse (should be %d)\n", | ||
| 172 | alg->len_normal, ln, | ||
| 173 | alg->len_inverse, li + 1); | ||
| 174 | goto append_move_fail; | ||
| 175 | } | ||
| 176 | if (alg->move_inverse[alg->len_inverse-1] != b->m) { | ||
| 177 | printf("Wrong move on inverse (%s instead of %s)\n", | ||
| 178 | move_string(alg->move_inverse[alg->len-1]), | ||
| 179 | move_string(b->m)); | ||
| 180 | goto append_move_fail; | ||
| 181 | } | ||
| 182 | } else { | ||
| 183 | if (alg->len_inverse != li || | ||
| 184 | alg->len_normal != ln + 1) { | ||
| 185 | printf("%d moves on normal (should be %d)" | ||
| 186 | " and %d on inverse (should be %d)\n", | ||
| 187 | alg->len_normal, ln, | ||
| 188 | alg->len_inverse, li + 1); | ||
| 189 | goto append_move_fail; | ||
| 190 | } | ||
| 191 | if (alg->move_normal[alg->len_normal-1] != b->m) { | ||
| 192 | printf("Wrong move on normal (%s instead of %s)\n", | ||
| 193 | move_string(alg->move_normal[alg->len-1]), | ||
| 194 | move_string(b->m)); | ||
| 195 | goto append_move_fail; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | free(alg); | ||
| 200 | return true; | ||
| 201 | |||
| 202 | append_move_fail: | ||
| 203 | free(alg); | ||
| 204 | return false; | ||
| 205 | } | ||
| 206 | |||
| 207 | bool | ||
| 208 | testmethod_new_alg(void *a) | ||
| 209 | { | ||
| 210 | new_alg_t *b = (new_alg_t *)a; | ||
| 211 | Alg *alg = new_alg(b->str); | ||
| 212 | |||
| 213 | if (alg->len != b->len) { | ||
| 214 | printf("Algs have different length (%d instead of %d)\n", | ||
| 215 | alg->len, b->len); | ||
| 216 | goto new_alg_fail; | ||
| 217 | } | ||
| 218 | |||
| 219 | for (int i = 0; i < alg->len; i++) { | ||
| 220 | if (alg->move[i] != b->move[i] || alg->inv[i] != b->inv[i]) { | ||
| 221 | printf("Algs differ on move %d\n", i); | ||
| 222 | printf("Expected: %s\nActual: ", b->str); | ||
| 223 | print_alg(alg, false); | ||
| 224 | goto new_alg_fail; | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | if (alg->len_normal != b->len_normal) { | ||
| 229 | printf("Algs have different number of moves on normal" | ||
| 230 | " (%d instead of %d)\n", | ||
| 231 | alg->len_normal, b->len_normal); | ||
| 232 | goto new_alg_fail; | ||
| 233 | } | ||
| 234 | for (int i = 0; i < alg->len_normal; i++) { | ||
| 235 | if (alg->move_normal[i] != b->move_normal[i]) { | ||
| 236 | printf("Algs have different move %d on normal" | ||
| 237 | " (%s instead of %s)\n", i, | ||
| 238 | move_string(alg->move_normal[i]), | ||
| 239 | move_string(b->move_normal[i])); | ||
| 240 | goto new_alg_fail; | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | if (alg->len_inverse != b->len_inverse) { | ||
| 245 | printf("Algs have different number of moves on inverse" | ||
| 246 | " (%d instead of %d)\n", | ||
| 247 | alg->len_inverse, b->len_inverse); | ||
| 248 | goto new_alg_fail; | ||
| 249 | } | ||
| 250 | for (int i = 0; i < alg->len_inverse; i++) { | ||
| 251 | if (alg->move_inverse[i] != b->move_inverse[i]) { | ||
| 252 | printf("Algs have different move %d on inverse:\n" | ||
| 253 | " (%s instead of %s)\n", i, | ||
| 254 | move_string(alg->move_inverse[i]), | ||
| 255 | move_string(b->move_inverse[i])); | ||
| 256 | goto new_alg_fail; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | free(alg); | ||
| 261 | return true; | ||
| 262 | |||
| 263 | new_alg_fail: | ||
| 264 | free(alg); | ||
| 265 | return false; | ||
| 266 | } | ||
diff --git a/tests/alg_tests.h b/tests/alg_tests.h new file mode 100644 index 0000000..518b33e --- /dev/null +++ b/tests/alg_tests.h | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | #ifndef ALG_TESTS_H | ||
| 2 | #define ALG_TESTS_H | ||
| 3 | |||
| 4 | #include "../src/alg.h" | ||
| 5 | #include "test_common.h" | ||
| 6 | |||
| 7 | extern Test test_append_move; | ||
| 8 | extern Test test_new_alg; | ||
| 9 | /* | ||
| 10 | extern Test test_compose_alg; | ||
| 11 | extern Test test_inverse_alg; | ||
| 12 | extern Test test_on_inverse; | ||
| 13 | extern Test test_unniss; | ||
| 14 | */ | ||
| 15 | |||
| 16 | extern TestSuite alg_suite; | ||
| 17 | |||
| 18 | extern TestSuite *alg_suites[]; | ||
| 19 | |||
| 20 | #endif | ||
diff --git a/tests/test.c b/tests/test.c index a1a382c..8961de1 100644 --- a/tests/test.c +++ b/tests/test.c | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | #include <stdio.h> | 1 | #include <stdio.h> |
| 2 | 2 | ||
| 3 | #include "alg_tests.h" | ||
| 3 | #include "coord_tests.h" | 4 | #include "coord_tests.h" |
| 4 | #include "fst_tests.h" | 5 | #include "fst_tests.h" |
| 5 | 6 | ||
| @@ -54,9 +55,11 @@ int main(int argc, char *argv[]) { | |||
| 54 | init_trans(); | 55 | init_trans(); |
| 55 | /**************************************/ | 56 | /**************************************/ |
| 56 | 57 | ||
| 58 | TestModule alg = { .name = "alg", .suites = alg_suites }; | ||
| 57 | TestModule fst = { .name = "fst", .suites = fst_suites }; | 59 | TestModule fst = { .name = "fst", .suites = fst_suites }; |
| 58 | TestModule coord = { .name = "coord", .suites = coord_suites }; | 60 | TestModule coord = { .name = "coord", .suites = coord_suites }; |
| 59 | TestModule *modules[999] = { | 61 | TestModule *modules[999] = { |
| 62 | &alg, | ||
| 60 | &fst, | 63 | &fst, |
| 61 | &coord, | 64 | &coord, |
| 62 | NULL | 65 | NULL |
