diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-05-01 16:33:51 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-05-01 16:33:51 +0200 |
| commit | bf44088d4373a9520e860152c56a958332819c4b (patch) | |
| tree | ea35c847a1d82aa715e1fc1c76e4a24462255051 /tests | |
| parent | 1a5bfe9b08707b0aef748d7921a419ba4a046fba (diff) | |
| download | nissy-bf44088d4373a9520e860152c56a958332819c4b.tar.gz nissy-bf44088d4373a9520e860152c56a958332819c4b.zip | |
Split nissy in other repos, see README.md
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/alg_tests.c | 266 | ||||
| -rw-r--r-- | tests/alg_tests.h | 21 | ||||
| -rw-r--r-- | tests/coord_tests.c | 50 | ||||
| -rw-r--r-- | tests/coord_tests.h | 13 | ||||
| -rw-r--r-- | tests/fst_tests.c | 184 | ||||
| -rw-r--r-- | tests/fst_tests.h | 19 | ||||
| -rw-r--r-- | tests/test.c | 85 | ||||
| -rw-r--r-- | tests/test_common.h | 41 |
8 files changed, 0 insertions, 679 deletions
diff --git a/tests/alg_tests.c b/tests/alg_tests.c deleted file mode 100644 index 9ac1595..0000000 --- a/tests/alg_tests.c +++ /dev/null | |||
| @@ -1,266 +0,0 @@ | |||
| 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 deleted file mode 100644 index a24b51f..0000000 --- a/tests/alg_tests.h +++ /dev/null | |||
| @@ -1,21 +0,0 @@ | |||
| 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 remove_last_move; | ||
| 11 | extern Test test_compose_alg; | ||
| 12 | extern Test test_inverse_alg; | ||
| 13 | extern Test test_on_inverse; | ||
| 14 | extern Test test_unniss; | ||
| 15 | */ | ||
| 16 | |||
| 17 | extern TestSuite alg_suite; | ||
| 18 | |||
| 19 | extern TestSuite *alg_suites[]; | ||
| 20 | |||
| 21 | #endif | ||
diff --git a/tests/coord_tests.c b/tests/coord_tests.c deleted file mode 100644 index 8d6b6d3..0000000 --- a/tests/coord_tests.c +++ /dev/null | |||
| @@ -1,50 +0,0 @@ | |||
| 1 | #include "coord_tests.h" | ||
| 2 | |||
| 3 | bool testmethod_indexes_consistent(void *); | ||
| 4 | |||
| 5 | Test test_indexes_consistent = { | ||
| 6 | .name = "Consitency of index and anti-index", | ||
| 7 | .t = testmethod_indexes_consistent, | ||
| 8 | .cases = (void **)all_coordinates, | ||
| 9 | }; | ||
| 10 | Test *coord_pre_init[] = { | ||
| 11 | &test_indexes_consistent, | ||
| 12 | NULL | ||
| 13 | }; | ||
| 14 | TestSuite coord_pre_init_suite = { | ||
| 15 | .setup = NULL, | ||
| 16 | .tests = coord_pre_init, | ||
| 17 | .teardown = NULL, | ||
| 18 | }; | ||
| 19 | |||
| 20 | TestSuite *coord_suites[] = { | ||
| 21 | &coord_pre_init_suite, | ||
| 22 | NULL | ||
| 23 | }; | ||
| 24 | |||
| 25 | bool | ||
| 26 | testmethod_indexes_consistent(void *a) | ||
| 27 | { | ||
| 28 | uint64_t ui, uj; | ||
| 29 | Cube c; | ||
| 30 | Coordinate *coord; | ||
| 31 | |||
| 32 | coord = (Coordinate *)a; | ||
| 33 | |||
| 34 | if (coord->type != COMP_COORD) | ||
| 35 | return true; /* Not applicable */ | ||
| 36 | |||
| 37 | gen_coord(coord); | ||
| 38 | for (ui = 0; ui < coord->max; ui++) { | ||
| 39 | indexers_makecube(coord->i, ui, &c); | ||
| 40 | uj = indexers_getind(coord->i, &c); | ||
| 41 | if (ui != uj) { | ||
| 42 | fprintf(stderr, "Error with coordinate %s: " | ||
| 43 | "%" PRIu64 " != %" PRIu64 "\n", | ||
| 44 | coord->name, uj, ui); | ||
| 45 | return false; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | return true; | ||
| 50 | } | ||
diff --git a/tests/coord_tests.h b/tests/coord_tests.h deleted file mode 100644 index 0cfd145..0000000 --- a/tests/coord_tests.h +++ /dev/null | |||
| @@ -1,13 +0,0 @@ | |||
| 1 | #ifndef COORD_TESTS_H | ||
| 2 | #define COORD_TESTS_H | ||
| 3 | |||
| 4 | #include "../src/coord.h" | ||
| 5 | #include "test_common.h" | ||
| 6 | |||
| 7 | extern Test test_indexes_consistent; | ||
| 8 | |||
| 9 | extern TestSuite coord_pre_init_suite; | ||
| 10 | |||
| 11 | extern TestSuite *coord_suites[]; | ||
| 12 | |||
| 13 | #endif | ||
diff --git a/tests/fst_tests.c b/tests/fst_tests.c deleted file mode 100644 index 1a9bedd..0000000 --- a/tests/fst_tests.c +++ /dev/null | |||
| @@ -1,184 +0,0 @@ | |||
| 1 | #include "fst_tests.h" | ||
| 2 | |||
| 3 | static bool testmethod_fst_is_consistent(void *); | ||
| 4 | static bool testmethod_cube_to_fst_to_cube(void *); | ||
| 5 | static bool testmethod_fst_move(void *); | ||
| 6 | static bool testmethod_fst_inverse(void *); | ||
| 7 | static bool check_equal_and_log(Cube *, Cube *); | ||
| 8 | static void void_to_cube(void *, Cube *); | ||
| 9 | |||
| 10 | char *algs[] = { | ||
| 11 | "", | ||
| 12 | "U", "U2", "U'", "D", "D2", "D'", "R", "R2", "R'", | ||
| 13 | "L", "L2", "L'", "F", "F2", "F'", "B", "B2", "B'", | ||
| 14 | "U2 R2 U2 R2 U2", | ||
| 15 | "U2 F2 R2 B2 U2 D2 F2 L2 B2", | ||
| 16 | "RUR'URU2R'", | ||
| 17 | "L2 D R U2 B2 L", | ||
| 18 | "R'U'F", | ||
| 19 | "F2 U' R2 D' B2 D2 R2 D2 R2 U' F L' U' R B F2 R B' D2", | ||
| 20 | "D L2 F2 R2 D R2 U L2 U' B2 D L' F2 U2 B' L D' U' R' B2 F2", | ||
| 21 | "F' L2 F' D' R F2 L U L' D2 R2 F2 D2 R2 B' L2 B2 U2 F D2 B", | ||
| 22 | NULL, | ||
| 23 | }; | ||
| 24 | |||
| 25 | Test test_fst_is_consistent = { | ||
| 26 | .name = "Consitency of FST (converted from cube)", | ||
| 27 | .t = testmethod_fst_is_consistent, | ||
| 28 | .cases = (void **)algs, | ||
| 29 | }; | ||
| 30 | Test test_cube_to_fst_to_cube = { | ||
| 31 | .name = "Cube to FST to cube", | ||
| 32 | .t = testmethod_cube_to_fst_to_cube, | ||
| 33 | .cases = (void **)algs, | ||
| 34 | }; | ||
| 35 | Test test_fst_move = { | ||
| 36 | .name = "FST move", | ||
| 37 | .t = testmethod_fst_move, | ||
| 38 | .cases = (void **)algs, | ||
| 39 | }; | ||
| 40 | Test test_fst_inverse = { | ||
| 41 | .name = "FST inverse", | ||
| 42 | .t = testmethod_fst_inverse, | ||
| 43 | .cases = (void **)algs, | ||
| 44 | }; | ||
| 45 | |||
| 46 | Test *fst_pre_init[] = { | ||
| 47 | &test_fst_is_consistent, | ||
| 48 | &test_cube_to_fst_to_cube, | ||
| 49 | NULL | ||
| 50 | }; | ||
| 51 | TestSuite fst_pre_init_suite = { | ||
| 52 | .setup = NULL, | ||
| 53 | .tests = fst_pre_init, | ||
| 54 | .teardown = NULL, | ||
| 55 | }; | ||
| 56 | |||
| 57 | Test *fst_post_init[] = { | ||
| 58 | &test_fst_move, | ||
| 59 | &test_fst_inverse, | ||
| 60 | NULL | ||
| 61 | }; | ||
| 62 | TestSuite fst_post_init_suite = { | ||
| 63 | .setup = init_fst, | ||
| 64 | .tests = fst_post_init, | ||
| 65 | .teardown = NULL, | ||
| 66 | }; | ||
| 67 | |||
| 68 | TestSuite *fst_suites[] = { | ||
| 69 | &fst_pre_init_suite, | ||
| 70 | &fst_post_init_suite, | ||
| 71 | NULL | ||
| 72 | }; | ||
| 73 | |||
| 74 | static bool | ||
| 75 | check_equal_and_log(Cube *c, Cube *d) | ||
| 76 | { | ||
| 77 | bool ret = equal(c, d); | ||
| 78 | |||
| 79 | if (!ret) { | ||
| 80 | printf("\n"); | ||
| 81 | printf("These cubes should be equal, but are not:\n\n"); | ||
| 82 | print_cube(c); | ||
| 83 | printf("\n"); | ||
| 84 | print_cube(d); | ||
| 85 | printf("\n"); | ||
| 86 | } | ||
| 87 | |||
| 88 | return ret; | ||
| 89 | } | ||
| 90 | |||
| 91 | static void | ||
| 92 | void_to_cube(void *a, Cube *c) | ||
| 93 | { | ||
| 94 | char *algstr; | ||
| 95 | Alg *alg; | ||
| 96 | |||
| 97 | algstr = (char *)a; | ||
| 98 | alg = new_alg(algstr); | ||
| 99 | make_solved(c); | ||
| 100 | apply_alg(alg, c); | ||
| 101 | free_alg(alg); | ||
| 102 | } | ||
| 103 | |||
| 104 | bool | ||
| 105 | testmethod_fst_is_consistent(void *a) | ||
| 106 | { | ||
| 107 | FstCube fst_uf, fst_fr, fst_rd; | ||
| 108 | Cube c, c_fr, c_rd; | ||
| 109 | bool consistent_fr, consistent_rd, result; | ||
| 110 | |||
| 111 | void_to_cube(a, &c); | ||
| 112 | copy_cube(&c, &c_fr); | ||
| 113 | apply_trans(fr, &c_fr); | ||
| 114 | |||
| 115 | copy_cube(&c, &c_rd); | ||
| 116 | apply_trans(rd, &c_rd); | ||
| 117 | |||
| 118 | fst_uf = cube_to_fst(&c); | ||
| 119 | fst_fr = cube_to_fst(&c_fr); | ||
| 120 | fst_rd = cube_to_fst(&c_rd); | ||
| 121 | |||
| 122 | consistent_fr = fst_uf.fr_eofb == fst_fr.uf_eofb && | ||
| 123 | fst_uf.fr_eposepe == fst_fr.uf_eposepe && | ||
| 124 | fst_uf.fr_coud == fst_fr.uf_coud; | ||
| 125 | |||
| 126 | consistent_rd = fst_uf.rd_eofb == fst_rd.uf_eofb && | ||
| 127 | fst_uf.rd_eposepe == fst_rd.uf_eposepe && | ||
| 128 | fst_uf.rd_coud == fst_rd.uf_coud; | ||
| 129 | |||
| 130 | result = consistent_fr && consistent_rd; | ||
| 131 | |||
| 132 | if (!result) | ||
| 133 | printf("\nFailed with alg %s\n", (char *)a); | ||
| 134 | |||
| 135 | return result; | ||
| 136 | } | ||
| 137 | |||
| 138 | bool | ||
| 139 | testmethod_cube_to_fst_to_cube(void *a) | ||
| 140 | { | ||
| 141 | Cube c, d; | ||
| 142 | FstCube fst; | ||
| 143 | |||
| 144 | void_to_cube(a, &c); | ||
| 145 | fst = cube_to_fst(&c); | ||
| 146 | fst_to_cube(fst, &d); | ||
| 147 | |||
| 148 | return check_equal_and_log(&c, &d);; | ||
| 149 | } | ||
| 150 | |||
| 151 | bool | ||
| 152 | testmethod_fst_move(void *a) | ||
| 153 | { | ||
| 154 | int i; | ||
| 155 | Alg *alg; | ||
| 156 | Cube c, d; | ||
| 157 | FstCube fst; | ||
| 158 | |||
| 159 | void_to_cube(a, &c); | ||
| 160 | alg = new_alg((char *)a); | ||
| 161 | make_solved(&d); | ||
| 162 | fst = cube_to_fst(&d); | ||
| 163 | |||
| 164 | for (i = 0; i < alg->len; i++) | ||
| 165 | fst = fst_move(alg->move[i], fst); | ||
| 166 | |||
| 167 | fst_to_cube(fst, &d); | ||
| 168 | |||
| 169 | free_alg(alg); | ||
| 170 | |||
| 171 | return check_equal_and_log(&c, &d); | ||
| 172 | } | ||
| 173 | |||
| 174 | bool | ||
| 175 | testmethod_fst_inverse(void *a) | ||
| 176 | { | ||
| 177 | Cube c, d; | ||
| 178 | |||
| 179 | void_to_cube(a, &c); | ||
| 180 | fst_to_cube(fst_inverse(cube_to_fst(&c)), &d); | ||
| 181 | invert_cube(&c); | ||
| 182 | |||
| 183 | return check_equal_and_log(&c, &d); | ||
| 184 | } | ||
diff --git a/tests/fst_tests.h b/tests/fst_tests.h deleted file mode 100644 index 68bbf04..0000000 --- a/tests/fst_tests.h +++ /dev/null | |||
| @@ -1,19 +0,0 @@ | |||
| 1 | #ifndef FST_TESTS_H | ||
| 2 | #define FST_TESTS_H | ||
| 3 | |||
| 4 | #include "../src/fst.h" | ||
| 5 | #include "test_common.h" | ||
| 6 | |||
| 7 | extern char *algs[]; | ||
| 8 | |||
| 9 | extern Test test_fst_is_consistent; | ||
| 10 | extern Test test_cube_to_fst_to_cube; | ||
| 11 | extern Test test_fst_move; | ||
| 12 | extern Test test_fst_inverse; | ||
| 13 | |||
| 14 | extern TestSuite fst_pre_init_suite; | ||
| 15 | extern TestSuite fst_post_init_suite; | ||
| 16 | |||
| 17 | extern TestSuite *fst_suites[]; | ||
| 18 | |||
| 19 | #endif | ||
diff --git a/tests/test.c b/tests/test.c deleted file mode 100644 index 8961de1..0000000 --- a/tests/test.c +++ /dev/null | |||
| @@ -1,85 +0,0 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | |||
| 3 | #include "alg_tests.h" | ||
| 4 | #include "coord_tests.h" | ||
| 5 | #include "fst_tests.h" | ||
| 6 | |||
| 7 | static bool run_test(Test *); | ||
| 8 | static bool run_suite(TestSuite *); | ||
| 9 | |||
| 10 | static bool | ||
| 11 | run_test(Test *test) | ||
| 12 | { | ||
| 13 | int i; | ||
| 14 | |||
| 15 | printf("Running test %s...", test->name); | ||
| 16 | for (i = 0; test->cases[i] != NULL; i++) { | ||
| 17 | if (!test->t(test->cases[i])) { | ||
| 18 | printf("FAILED!\n"); | ||
| 19 | return false; | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | printf("OK\n"); | ||
| 24 | return true; | ||
| 25 | } | ||
| 26 | |||
| 27 | static bool | ||
| 28 | run_suite(TestSuite *suite) | ||
| 29 | { | ||
| 30 | int i; | ||
| 31 | |||
| 32 | if (suite->setup != NULL) | ||
| 33 | suite->setup(); | ||
| 34 | for (i = 0; suite->tests[i] != NULL; i++) | ||
| 35 | if(!run_test(suite->tests[i])) | ||
| 36 | return false; | ||
| 37 | if (suite->teardown != NULL) | ||
| 38 | suite->teardown(); | ||
| 39 | |||
| 40 | return true; | ||
| 41 | } | ||
| 42 | |||
| 43 | static bool | ||
| 44 | module_in_args(char *module, int argc, char *argv[]) | ||
| 45 | { | ||
| 46 | for (int i = 0; i < argc; i++) | ||
| 47 | if (!strcmp(module, argv[i])) | ||
| 48 | return true; | ||
| 49 | return false; | ||
| 50 | } | ||
| 51 | |||
| 52 | int main(int argc, char *argv[]) { | ||
| 53 | /* TODO: init should be in testsuites */ | ||
| 54 | init_env(); | ||
| 55 | init_trans(); | ||
| 56 | /**************************************/ | ||
| 57 | |||
| 58 | TestModule alg = { .name = "alg", .suites = alg_suites }; | ||
| 59 | TestModule fst = { .name = "fst", .suites = fst_suites }; | ||
| 60 | TestModule coord = { .name = "coord", .suites = coord_suites }; | ||
| 61 | TestModule *modules[999] = { | ||
| 62 | &alg, | ||
| 63 | &fst, | ||
| 64 | &coord, | ||
| 65 | NULL | ||
| 66 | }; | ||
| 67 | |||
| 68 | |||
| 69 | bool all = argc == 1 || module_in_args("all", argc, argv); | ||
| 70 | int count = 0; | ||
| 71 | for (int i = 0; modules[i] != NULL; i++) { | ||
| 72 | if (all || module_in_args(modules[i]->name, argc, argv)) { | ||
| 73 | for (int j = 0; modules[i]->suites[j] != NULL; j++) { | ||
| 74 | if (!run_suite(modules[i]->suites[j])) { | ||
| 75 | return 1; | ||
| 76 | } else { | ||
| 77 | count++; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | printf("All tests passed (%d test suites).\n", count); | ||
| 84 | return 0; | ||
| 85 | } | ||
diff --git a/tests/test_common.h b/tests/test_common.h deleted file mode 100644 index 03511ef..0000000 --- a/tests/test_common.h +++ /dev/null | |||
| @@ -1,41 +0,0 @@ | |||
| 1 | #ifndef TEST_COMMON_H | ||
| 2 | #define TEST_COMMON_H | ||
| 3 | |||
| 4 | /* | ||
| 5 | * Common utilities for testing. | ||
| 6 | * A VoidMethod can be used as a setup or teardown method for testing, see | ||
| 7 | * the TestSuite struct. A TestMethod takes a void pointer (usually cast | ||
| 8 | * to some data to be used for testing, but can also be ignored) and returns | ||
| 9 | * a bool: true for pass, false for fail. | ||
| 10 | * A Test consists of a name (string), a TestMethod and an array of void | ||
| 11 | * pointers that describe the test cases. Each element of this array is | ||
| 12 | * passed to the test method sequentially, and the test session stops on | ||
| 13 | * the first failure. | ||
| 14 | * A test suite is just a list of tests with a setup and a teardown method. | ||
| 15 | * The setup method is run once and for all before the first test, and the | ||
| 16 | * teardown is run at the end of the testsuite. | ||
| 17 | * Finally, a TestModule roughly corresponds to a test file. This type is | ||
| 18 | * only used in test.c to collect multiple test modules. | ||
| 19 | */ | ||
| 20 | |||
| 21 | typedef void (*VoidMethod)(void); | ||
| 22 | typedef bool (*TestMethod)(void *); | ||
| 23 | |||
| 24 | typedef struct { | ||
| 25 | char * name; | ||
| 26 | TestMethod t; | ||
| 27 | void ** cases; | ||
| 28 | } Test; | ||
| 29 | |||
| 30 | typedef struct { | ||
| 31 | VoidMethod setup; | ||
| 32 | Test ** tests; | ||
| 33 | VoidMethod teardown; | ||
| 34 | } TestSuite; | ||
| 35 | |||
| 36 | typedef struct { | ||
| 37 | char * name; | ||
| 38 | TestSuite ** suites; | ||
| 39 | } TestModule; | ||
| 40 | |||
| 41 | #endif | ||
