diff options
| -rw-r--r-- | TODO/refactoring.md | 2 | ||||
| -rw-r--r-- | src/coord.c | 37 | ||||
| -rw-r--r-- | src/coord.h | 27 | ||||
| -rw-r--r-- | tests/coord_tests.c | 50 | ||||
| -rw-r--r-- | tests/coord_tests.h | 13 | ||||
| -rw-r--r-- | tests/fst_tests.c | 13 | ||||
| -rw-r--r-- | tests/fst_tests.h | 2 | ||||
| -rw-r--r-- | tests/test.c | 5 |
8 files changed, 107 insertions, 42 deletions
diff --git a/TODO/refactoring.md b/TODO/refactoring.md index eedbd20..206b402 100644 --- a/TODO/refactoring.md +++ b/TODO/refactoring.md | |||
| @@ -25,4 +25,6 @@ | |||
| 25 | * Sort function implementations alphabetically, ignore static vs non static. | 25 | * Sort function implementations alphabetically, ignore static vs non static. |
| 26 | * Rename functions and variable to have a consistent naming scheme. | 26 | * Rename functions and variable to have a consistent naming scheme. |
| 27 | * Functions that copy data: swap src and dest, follow memcpy standard. | 27 | * Functions that copy data: swap src and dest, follow memcpy standard. |
| 28 | * The way coord uses define guards to organize the .h file is good, apply it | ||
| 29 | to other modules too - including tests. | ||
| 28 | * Read style(9) and decide what to implement. | 30 | * Read style(9) and decide what to implement. |
diff --git a/src/coord.c b/src/coord.c index 06f7474..673434f 100644 --- a/src/coord.c +++ b/src/coord.c | |||
| @@ -2,9 +2,6 @@ | |||
| 2 | 2 | ||
| 3 | #include "coord.h" | 3 | #include "coord.h" |
| 4 | 4 | ||
| 5 | static uint64_t indexers_getind(Indexer **is, Cube *c); | ||
| 6 | static uint64_t indexers_getmax(Indexer **is); | ||
| 7 | static void indexers_makecube(Indexer **is, uint64_t ind, Cube *c); | ||
| 8 | static void gen_coord_comp(Coordinate *coord); | 5 | static void gen_coord_comp(Coordinate *coord); |
| 9 | static void gen_coord_sym(Coordinate *coord); | 6 | static void gen_coord_sym(Coordinate *coord); |
| 10 | static bool read_coord_mtable(Coordinate *coord); | 7 | static bool read_coord_mtable(Coordinate *coord); |
| @@ -169,7 +166,7 @@ invindex_eposepe(uint64_t ind, Cube *cube) | |||
| 169 | 166 | ||
| 170 | /* Other local functions *****************************************************/ | 167 | /* Other local functions *****************************************************/ |
| 171 | 168 | ||
| 172 | static uint64_t | 169 | uint64_t |
| 173 | indexers_getmax(Indexer **is) | 170 | indexers_getmax(Indexer **is) |
| 174 | { | 171 | { |
| 175 | int i; | 172 | int i; |
| @@ -181,7 +178,7 @@ indexers_getmax(Indexer **is) | |||
| 181 | return max; | 178 | return max; |
| 182 | } | 179 | } |
| 183 | 180 | ||
| 184 | static uint64_t | 181 | uint64_t |
| 185 | indexers_getind(Indexer **is, Cube *c) | 182 | indexers_getind(Indexer **is, Cube *c) |
| 186 | { | 183 | { |
| 187 | int i; | 184 | int i; |
| @@ -195,12 +192,12 @@ indexers_getind(Indexer **is, Cube *c) | |||
| 195 | return max; | 192 | return max; |
| 196 | } | 193 | } |
| 197 | 194 | ||
| 198 | static void | 195 | void |
| 199 | indexers_makecube(Indexer **is, uint64_t ind, Cube *c) | 196 | indexers_makecube(Indexer **is, uint64_t ind, Cube *c) |
| 200 | { | 197 | { |
| 201 | /* Warning: anti-indexers are applied in the same order as indexers. */ | 198 | /* Warning: anti-indexers are applied in the same order as indexers. */ |
| 202 | /* We assume order does not matter, but it would make more sense to */ | 199 | /* We assume order does not matter, but it would make more sense to */ |
| 203 | /* Apply them in reverse. */ | 200 | /* apply them in reverse. */ |
| 204 | 201 | ||
| 205 | int i; | 202 | int i; |
| 206 | uint64_t m; | 203 | uint64_t m; |
| @@ -629,32 +626,6 @@ move_coord(Coordinate *coord, Move m, uint64_t ind, Trans *offtrans) | |||
| 629 | return coord->max; /* Only reached in case of error */ | 626 | return coord->max; /* Only reached in case of error */ |
| 630 | } | 627 | } |
| 631 | 628 | ||
| 632 | bool | ||
| 633 | test_coord(Coordinate *coord) | ||
| 634 | { | ||
| 635 | uint64_t ui, uj; | ||
| 636 | Cube c; | ||
| 637 | |||
| 638 | if (coord->type != COMP_COORD) { | ||
| 639 | fprintf(stderr, "Can only test COMP_COORD\n"); | ||
| 640 | return false; | ||
| 641 | } | ||
| 642 | |||
| 643 | gen_coord(coord); | ||
| 644 | for (ui = 0; ui < coord->max; ui++) { | ||
| 645 | indexers_makecube(coord->i, ui, &c); | ||
| 646 | uj = indexers_getind(coord->i, &c); | ||
| 647 | if (ui != uj) { | ||
| 648 | fprintf(stderr, "%s: error: %" PRIu64 " different" | ||
| 649 | " from %" PRIu64 "\n", coord->name, uj, ui); | ||
| 650 | return false; | ||
| 651 | } | ||
| 652 | } | ||
| 653 | |||
| 654 | fprintf(stderr, "%s: test passed\n", coord->name); | ||
| 655 | return true; | ||
| 656 | } | ||
| 657 | |||
| 658 | uint64_t | 629 | uint64_t |
| 659 | trans_coord(Coordinate *coord, Trans t, uint64_t ind) | 630 | trans_coord(Coordinate *coord, Trans t, uint64_t ind) |
| 660 | { | 631 | { |
diff --git a/src/coord.h b/src/coord.h index 47c0579..4cd7e47 100644 --- a/src/coord.h +++ b/src/coord.h | |||
| @@ -6,9 +6,10 @@ | |||
| 6 | void gen_coord(Coordinate *coord); | 6 | void gen_coord(Coordinate *coord); |
| 7 | uint64_t index_coord(Coordinate *coord, Cube *cube, | 7 | uint64_t index_coord(Coordinate *coord, Cube *cube, |
| 8 | Trans *offtrans); | 8 | Trans *offtrans); |
| 9 | uint64_t indexers_getind(Indexer **is, Cube *c); | ||
| 10 | void indexers_makecube(Indexer **is, uint64_t ind, Cube *c); | ||
| 9 | uint64_t move_coord(Coordinate *coord, Move m, | 11 | uint64_t move_coord(Coordinate *coord, Move m, |
| 10 | uint64_t ind, Trans *offtrans); | 12 | uint64_t ind, Trans *offtrans); |
| 11 | bool test_coord(Coordinate *coord); | ||
| 12 | uint64_t trans_coord(Coordinate *coord, Trans t, uint64_t ind); | 13 | uint64_t trans_coord(Coordinate *coord, Trans t, uint64_t ind); |
| 13 | 14 | ||
| 14 | /* Base coordinates and their index functions ********************************/ | 15 | /* Base coordinates and their index functions ********************************/ |
| @@ -32,6 +33,8 @@ extern Coordinate coord_drud_sym16; | |||
| 32 | extern Coordinate coord_drudfin_noE_sym16; | 33 | extern Coordinate coord_drudfin_noE_sym16; |
| 33 | extern Coordinate coord_nxopt31; | 34 | extern Coordinate coord_nxopt31; |
| 34 | 35 | ||
| 36 | extern Coordinate *all_coordinates[]; | ||
| 37 | |||
| 35 | #else | 38 | #else |
| 36 | 39 | ||
| 37 | /* Indexers ******************************************************************/ | 40 | /* Indexers ******************************************************************/ |
| @@ -228,6 +231,28 @@ coord_nxopt31 = { | |||
| 228 | .base = {&coord_eofbepos_sym16, &coord_coud_cpudsep}, | 231 | .base = {&coord_eofbepos_sym16, &coord_coud_cpudsep}, |
| 229 | }; | 232 | }; |
| 230 | 233 | ||
| 234 | /* All coordinates ***********************************************************/ | ||
| 235 | |||
| 236 | Coordinate *all_coordinates[] = { | ||
| 237 | &coord_eofb, | ||
| 238 | &coord_coud, | ||
| 239 | &coord_cp, | ||
| 240 | &coord_cpudsep, | ||
| 241 | &coord_epos, | ||
| 242 | &coord_epe, | ||
| 243 | &coord_eposepe, | ||
| 244 | &coord_epud, | ||
| 245 | &coord_eofbepos, | ||
| 246 | &coord_coud_cpudsep, | ||
| 247 | &coord_eofbepos_sym16, | ||
| 248 | &coord_cp_sym16, | ||
| 249 | &coord_corners_sym16, | ||
| 250 | &coord_drud_sym16, | ||
| 251 | &coord_drudfin_noE_sym16, | ||
| 252 | &coord_nxopt31, | ||
| 253 | NULL | ||
| 254 | }; | ||
| 255 | |||
| 231 | #endif | 256 | #endif |
| 232 | 257 | ||
| 233 | #endif | 258 | #endif |
diff --git a/tests/coord_tests.c b/tests/coord_tests.c new file mode 100644 index 0000000..8d6b6d3 --- /dev/null +++ b/tests/coord_tests.c | |||
| @@ -0,0 +1,50 @@ | |||
| 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 new file mode 100644 index 0000000..0cfd145 --- /dev/null +++ b/tests/coord_tests.h | |||
| @@ -0,0 +1,13 @@ | |||
| 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 index dfaab39..1a9bedd 100644 --- a/tests/fst_tests.c +++ b/tests/fst_tests.c | |||
| @@ -3,7 +3,8 @@ | |||
| 3 | static bool testmethod_fst_is_consistent(void *); | 3 | static bool testmethod_fst_is_consistent(void *); |
| 4 | static bool testmethod_cube_to_fst_to_cube(void *); | 4 | static bool testmethod_cube_to_fst_to_cube(void *); |
| 5 | static bool testmethod_fst_move(void *); | 5 | static bool testmethod_fst_move(void *); |
| 6 | static bool testmethod_fst_inverse(void *); static bool check_equal_and_log(Cube *, Cube *); | 6 | static bool testmethod_fst_inverse(void *); |
| 7 | static bool check_equal_and_log(Cube *, Cube *); | ||
| 7 | static void void_to_cube(void *, Cube *); | 8 | static void void_to_cube(void *, Cube *); |
| 8 | 9 | ||
| 9 | char *algs[] = { | 10 | char *algs[] = { |
| @@ -42,29 +43,29 @@ Test test_fst_inverse = { | |||
| 42 | .cases = (void **)algs, | 43 | .cases = (void **)algs, |
| 43 | }; | 44 | }; |
| 44 | 45 | ||
| 45 | Test *pre_init[] = { | 46 | Test *fst_pre_init[] = { |
| 46 | &test_fst_is_consistent, | 47 | &test_fst_is_consistent, |
| 47 | &test_cube_to_fst_to_cube, | 48 | &test_cube_to_fst_to_cube, |
| 48 | NULL | 49 | NULL |
| 49 | }; | 50 | }; |
| 50 | TestSuite fst_pre_init_suite = { | 51 | TestSuite fst_pre_init_suite = { |
| 51 | .setup = NULL, | 52 | .setup = NULL, |
| 52 | .tests = pre_init, | 53 | .tests = fst_pre_init, |
| 53 | .teardown = NULL, | 54 | .teardown = NULL, |
| 54 | }; | 55 | }; |
| 55 | 56 | ||
| 56 | Test *post_init[] = { | 57 | Test *fst_post_init[] = { |
| 57 | &test_fst_move, | 58 | &test_fst_move, |
| 58 | &test_fst_inverse, | 59 | &test_fst_inverse, |
| 59 | NULL | 60 | NULL |
| 60 | }; | 61 | }; |
| 61 | TestSuite fst_post_init_suite = { | 62 | TestSuite fst_post_init_suite = { |
| 62 | .setup = init_fst, | 63 | .setup = init_fst, |
| 63 | .tests = post_init, | 64 | .tests = fst_post_init, |
| 64 | .teardown = NULL, | 65 | .teardown = NULL, |
| 65 | }; | 66 | }; |
| 66 | 67 | ||
| 67 | TestSuite *fst_testsuites[] = { | 68 | TestSuite *fst_suites[] = { |
| 68 | &fst_pre_init_suite, | 69 | &fst_pre_init_suite, |
| 69 | &fst_post_init_suite, | 70 | &fst_post_init_suite, |
| 70 | NULL | 71 | NULL |
diff --git a/tests/fst_tests.h b/tests/fst_tests.h index 324028e..68bbf04 100644 --- a/tests/fst_tests.h +++ b/tests/fst_tests.h | |||
| @@ -14,6 +14,6 @@ extern Test test_fst_inverse; | |||
| 14 | extern TestSuite fst_pre_init_suite; | 14 | extern TestSuite fst_pre_init_suite; |
| 15 | extern TestSuite fst_post_init_suite; | 15 | extern TestSuite fst_post_init_suite; |
| 16 | 16 | ||
| 17 | extern TestSuite *fst_testsuites[]; | 17 | extern TestSuite *fst_suites[]; |
| 18 | 18 | ||
| 19 | #endif | 19 | #endif |
diff --git a/tests/test.c b/tests/test.c index cc14456..a1a382c 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 "coord_tests.h" | ||
| 3 | #include "fst_tests.h" | 4 | #include "fst_tests.h" |
| 4 | 5 | ||
| 5 | static bool run_test(Test *); | 6 | static bool run_test(Test *); |
| @@ -53,9 +54,11 @@ int main(int argc, char *argv[]) { | |||
| 53 | init_trans(); | 54 | init_trans(); |
| 54 | /**************************************/ | 55 | /**************************************/ |
| 55 | 56 | ||
| 56 | TestModule fst = { .name = "fst", .suites = fst_testsuites }; | 57 | TestModule fst = { .name = "fst", .suites = fst_suites }; |
| 58 | TestModule coord = { .name = "coord", .suites = coord_suites }; | ||
| 57 | TestModule *modules[999] = { | 59 | TestModule *modules[999] = { |
| 58 | &fst, | 60 | &fst, |
| 61 | &coord, | ||
| 59 | NULL | 62 | NULL |
| 60 | }; | 63 | }; |
| 61 | 64 | ||
