From bf44088d4373a9520e860152c56a958332819c4b Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 1 May 2023 16:33:51 +0200 Subject: Split nissy in other repos, see README.md --- tests/alg_tests.c | 266 ---------------------------------------------------- tests/alg_tests.h | 21 ----- tests/coord_tests.c | 50 ---------- tests/coord_tests.h | 13 --- tests/fst_tests.c | 184 ------------------------------------ tests/fst_tests.h | 19 ---- tests/test.c | 85 ----------------- tests/test_common.h | 41 -------- 8 files changed, 679 deletions(-) delete mode 100644 tests/alg_tests.c delete mode 100644 tests/alg_tests.h delete mode 100644 tests/coord_tests.c delete mode 100644 tests/coord_tests.h delete mode 100644 tests/fst_tests.c delete mode 100644 tests/fst_tests.h delete mode 100644 tests/test.c delete mode 100644 tests/test_common.h (limited to 'tests') 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 @@ -#include "alg_tests.h" - -bool testmethod_append_move(void *); -bool testmethod_new_alg(void *); -/* -bool testmethod_compose_alg(void *); -bool testmethod_inverse_alg(void *); -bool testmethod_on_inverse(void *); -bool testmethod_unniss(void *); -*/ - -typedef struct { - Move *move; - bool *inv; - int len; - Move m; - bool inverse; -} append_move_t; - -Move m_app1[] = {F, x, D3}; -bool i_app1[] = {true, false, false}; -append_move_t append_move_case1 = { - .move = m_app1, - .inv = i_app1, - .len = 3, - .m = L3, - .inverse = false, -}; - -Move m_app2[] = {S, U, x2, M2}; -bool i_app2[] = {true, false, true, true}; -append_move_t append_move_case2 = { - .move = m_app2, - .inv = i_app2, - .len = 4, - .m = R, - .inverse = true, -}; - -Move m_app3[] = {U, U, U, U, U}; -bool i_app3[] = {false, false, false, false, false}; -append_move_t append_move_case3 = { - .move = m_app3, - .inv = i_app3, - .len = 5, - .m = U, - .inverse = false, -}; - -append_move_t *append_move_cases[] = { - &append_move_case1, - &append_move_case2, - &append_move_case3, -}; - -Test test_append_move = { - .name = "Appending a move to and alg", - .t = testmethod_append_move, - .cases = (void **)append_move_cases, -}; - -typedef struct { - char *str; - Move *move; - bool *inv; - int len; - Move *move_normal; - int len_normal; - Move *move_inverse; - int len_inverse; -} new_alg_t; - -/* Alg F U B' (L3 D) x M (S) y3 */ -Move m_new1[] = {F, U, B3, L3, D, x, M, S, y3}; -Move mn_new1[] = {F, U, B3, x, M, y3}; -Move mi_new1[] = {L3, D, S}; -bool i_new1[] = {false, false, false, true, true, false, false, true, false}; -new_alg_t new_alg_case1 = { - .str = "F U B' (L3 D) x M (S) y3", - .move = m_new1, - .inv = i_new1, - .len = 9, - .move_normal = mn_new1, - .len_normal = 6, - .move_inverse = mi_new1, - .len_inverse = 3, -}; -new_alg_t *new_alg_cases[] = {&new_alg_case1}; - -Test test_new_alg = { - .name = "Initializing an alg from a string", - .t = testmethod_new_alg, - .cases = (void **)new_alg_cases, -}; - -Test *alg_all_tests[] = { - &test_append_move, - &test_new_alg, -/* - &test_append_alg, - &test_compose_alg, - &test_inverse_alg, - &test_on_inverse, - &test_unniss, -*/ - NULL -}; -TestSuite alg_suite = { - .setup = NULL, - .tests = alg_all_tests, - .teardown = NULL, -}; - -TestSuite *alg_suites[] = { - &alg_suite, - NULL -}; - -bool -testmethod_append_move(void *a) -{ - append_move_t *b = (append_move_t *)a; - int li, ln; - Alg *alg; - - /* Small to test reallocation */ - alg = malloc(sizeof(Alg)); - alg->allocated = 5; - alg->move = malloc(alg->allocated * sizeof(Move)); - alg->inv = malloc(alg->allocated * sizeof(bool)); - alg->len = b->len; - memcpy(alg->move, b->move, alg->len * sizeof(Move)); - memcpy(alg->inv, b->inv, alg->len * sizeof(bool)); - alg->move_normal = malloc(alg->allocated * sizeof(Move)); - alg->move_inverse = malloc(alg->allocated * sizeof(Move)); - - li = ln = 0; - for (int i = 0; i < alg->len; i++) { - if (alg->inv[i]) - alg->move_inverse[li++] = alg->move[i]; - else - alg->move_normal[ln++] = alg->move[i]; - } - alg->len_inverse = li; - alg->len_normal = ln; - - append_move(alg, b->m, b->inverse); - - if (alg->len != b->len + 1) { - printf("Alg has wrong len (%d instead of %d)\n", - alg->len, b->len + 1); - goto append_move_fail; - } - if (alg->move[alg->len-1] != b->m) { - printf("Wrong last move (%s instead of %s)\n", - move_string(alg->move[alg->len-1]), - move_string(b->m)); - goto append_move_fail; - } - if (alg->inv[alg->len-1] != b->inverse) { - printf("Wrong inverse flag for last move " - "(%s instead of %s)\n", - b->inverse ? "normal" : "inverse", - b->inverse ? "inverse" : "normal"); - goto append_move_fail; - } - if (b->inverse) { - if (alg->len_inverse != li + 1 || - alg->len_normal != ln) { - printf("%d moves on normal (should be %d)" - " and %d on inverse (should be %d)\n", - alg->len_normal, ln, - alg->len_inverse, li + 1); - goto append_move_fail; - } - if (alg->move_inverse[alg->len_inverse-1] != b->m) { - printf("Wrong move on inverse (%s instead of %s)\n", - move_string(alg->move_inverse[alg->len-1]), - move_string(b->m)); - goto append_move_fail; - } - } else { - if (alg->len_inverse != li || - alg->len_normal != ln + 1) { - printf("%d moves on normal (should be %d)" - " and %d on inverse (should be %d)\n", - alg->len_normal, ln, - alg->len_inverse, li + 1); - goto append_move_fail; - } - if (alg->move_normal[alg->len_normal-1] != b->m) { - printf("Wrong move on normal (%s instead of %s)\n", - move_string(alg->move_normal[alg->len-1]), - move_string(b->m)); - goto append_move_fail; - } - } - - free(alg); - return true; - -append_move_fail: - free(alg); - return false; -} - -bool -testmethod_new_alg(void *a) -{ - new_alg_t *b = (new_alg_t *)a; - Alg *alg = new_alg(b->str); - - if (alg->len != b->len) { - printf("Algs have different length (%d instead of %d)\n", - alg->len, b->len); - goto new_alg_fail; - } - - for (int i = 0; i < alg->len; i++) { - if (alg->move[i] != b->move[i] || alg->inv[i] != b->inv[i]) { - printf("Algs differ on move %d\n", i); - printf("Expected: %s\nActual: ", b->str); - print_alg(alg, false); - goto new_alg_fail; - } - } - - if (alg->len_normal != b->len_normal) { - printf("Algs have different number of moves on normal" - " (%d instead of %d)\n", - alg->len_normal, b->len_normal); - goto new_alg_fail; - } - for (int i = 0; i < alg->len_normal; i++) { - if (alg->move_normal[i] != b->move_normal[i]) { - printf("Algs have different move %d on normal" - " (%s instead of %s)\n", i, - move_string(alg->move_normal[i]), - move_string(b->move_normal[i])); - goto new_alg_fail; - } - } - - if (alg->len_inverse != b->len_inverse) { - printf("Algs have different number of moves on inverse" - " (%d instead of %d)\n", - alg->len_inverse, b->len_inverse); - goto new_alg_fail; - } - for (int i = 0; i < alg->len_inverse; i++) { - if (alg->move_inverse[i] != b->move_inverse[i]) { - printf("Algs have different move %d on inverse:\n" - " (%s instead of %s)\n", i, - move_string(alg->move_inverse[i]), - move_string(b->move_inverse[i])); - goto new_alg_fail; - } - } - - free(alg); - return true; - -new_alg_fail: - free(alg); - return false; -} 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 @@ -#ifndef ALG_TESTS_H -#define ALG_TESTS_H - -#include "../src/alg.h" -#include "test_common.h" - -extern Test test_append_move; -extern Test test_new_alg; -/* -extern Test remove_last_move; -extern Test test_compose_alg; -extern Test test_inverse_alg; -extern Test test_on_inverse; -extern Test test_unniss; -*/ - -extern TestSuite alg_suite; - -extern TestSuite *alg_suites[]; - -#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 @@ -#include "coord_tests.h" - -bool testmethod_indexes_consistent(void *); - -Test test_indexes_consistent = { - .name = "Consitency of index and anti-index", - .t = testmethod_indexes_consistent, - .cases = (void **)all_coordinates, -}; -Test *coord_pre_init[] = { - &test_indexes_consistent, - NULL -}; -TestSuite coord_pre_init_suite = { - .setup = NULL, - .tests = coord_pre_init, - .teardown = NULL, -}; - -TestSuite *coord_suites[] = { - &coord_pre_init_suite, - NULL -}; - -bool -testmethod_indexes_consistent(void *a) -{ - uint64_t ui, uj; - Cube c; - Coordinate *coord; - - coord = (Coordinate *)a; - - if (coord->type != COMP_COORD) - return true; /* Not applicable */ - - gen_coord(coord); - for (ui = 0; ui < coord->max; ui++) { - indexers_makecube(coord->i, ui, &c); - uj = indexers_getind(coord->i, &c); - if (ui != uj) { - fprintf(stderr, "Error with coordinate %s: " - "%" PRIu64 " != %" PRIu64 "\n", - coord->name, uj, ui); - return false; - } - } - - return true; -} 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 @@ -#ifndef COORD_TESTS_H -#define COORD_TESTS_H - -#include "../src/coord.h" -#include "test_common.h" - -extern Test test_indexes_consistent; - -extern TestSuite coord_pre_init_suite; - -extern TestSuite *coord_suites[]; - -#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 @@ -#include "fst_tests.h" - -static bool testmethod_fst_is_consistent(void *); -static bool testmethod_cube_to_fst_to_cube(void *); -static bool testmethod_fst_move(void *); -static bool testmethod_fst_inverse(void *); -static bool check_equal_and_log(Cube *, Cube *); -static void void_to_cube(void *, Cube *); - -char *algs[] = { - "", - "U", "U2", "U'", "D", "D2", "D'", "R", "R2", "R'", - "L", "L2", "L'", "F", "F2", "F'", "B", "B2", "B'", - "U2 R2 U2 R2 U2", - "U2 F2 R2 B2 U2 D2 F2 L2 B2", - "RUR'URU2R'", - "L2 D R U2 B2 L", - "R'U'F", - "F2 U' R2 D' B2 D2 R2 D2 R2 U' F L' U' R B F2 R B' D2", - "D L2 F2 R2 D R2 U L2 U' B2 D L' F2 U2 B' L D' U' R' B2 F2", - "F' L2 F' D' R F2 L U L' D2 R2 F2 D2 R2 B' L2 B2 U2 F D2 B", - NULL, -}; - -Test test_fst_is_consistent = { - .name = "Consitency of FST (converted from cube)", - .t = testmethod_fst_is_consistent, - .cases = (void **)algs, -}; -Test test_cube_to_fst_to_cube = { - .name = "Cube to FST to cube", - .t = testmethod_cube_to_fst_to_cube, - .cases = (void **)algs, -}; -Test test_fst_move = { - .name = "FST move", - .t = testmethod_fst_move, - .cases = (void **)algs, -}; -Test test_fst_inverse = { - .name = "FST inverse", - .t = testmethod_fst_inverse, - .cases = (void **)algs, -}; - -Test *fst_pre_init[] = { - &test_fst_is_consistent, - &test_cube_to_fst_to_cube, - NULL -}; -TestSuite fst_pre_init_suite = { - .setup = NULL, - .tests = fst_pre_init, - .teardown = NULL, -}; - -Test *fst_post_init[] = { - &test_fst_move, - &test_fst_inverse, - NULL -}; -TestSuite fst_post_init_suite = { - .setup = init_fst, - .tests = fst_post_init, - .teardown = NULL, -}; - -TestSuite *fst_suites[] = { - &fst_pre_init_suite, - &fst_post_init_suite, - NULL -}; - -static bool -check_equal_and_log(Cube *c, Cube *d) -{ - bool ret = equal(c, d); - - if (!ret) { - printf("\n"); - printf("These cubes should be equal, but are not:\n\n"); - print_cube(c); - printf("\n"); - print_cube(d); - printf("\n"); - } - - return ret; -} - -static void -void_to_cube(void *a, Cube *c) -{ - char *algstr; - Alg *alg; - - algstr = (char *)a; - alg = new_alg(algstr); - make_solved(c); - apply_alg(alg, c); - free_alg(alg); -} - -bool -testmethod_fst_is_consistent(void *a) -{ - FstCube fst_uf, fst_fr, fst_rd; - Cube c, c_fr, c_rd; - bool consistent_fr, consistent_rd, result; - - void_to_cube(a, &c); - copy_cube(&c, &c_fr); - apply_trans(fr, &c_fr); - - copy_cube(&c, &c_rd); - apply_trans(rd, &c_rd); - - fst_uf = cube_to_fst(&c); - fst_fr = cube_to_fst(&c_fr); - fst_rd = cube_to_fst(&c_rd); - - consistent_fr = fst_uf.fr_eofb == fst_fr.uf_eofb && - fst_uf.fr_eposepe == fst_fr.uf_eposepe && - fst_uf.fr_coud == fst_fr.uf_coud; - - consistent_rd = fst_uf.rd_eofb == fst_rd.uf_eofb && - fst_uf.rd_eposepe == fst_rd.uf_eposepe && - fst_uf.rd_coud == fst_rd.uf_coud; - - result = consistent_fr && consistent_rd; - - if (!result) - printf("\nFailed with alg %s\n", (char *)a); - - return result; -} - -bool -testmethod_cube_to_fst_to_cube(void *a) -{ - Cube c, d; - FstCube fst; - - void_to_cube(a, &c); - fst = cube_to_fst(&c); - fst_to_cube(fst, &d); - - return check_equal_and_log(&c, &d);; -} - -bool -testmethod_fst_move(void *a) -{ - int i; - Alg *alg; - Cube c, d; - FstCube fst; - - void_to_cube(a, &c); - alg = new_alg((char *)a); - make_solved(&d); - fst = cube_to_fst(&d); - - for (i = 0; i < alg->len; i++) - fst = fst_move(alg->move[i], fst); - - fst_to_cube(fst, &d); - - free_alg(alg); - - return check_equal_and_log(&c, &d); -} - -bool -testmethod_fst_inverse(void *a) -{ - Cube c, d; - - void_to_cube(a, &c); - fst_to_cube(fst_inverse(cube_to_fst(&c)), &d); - invert_cube(&c); - - return check_equal_and_log(&c, &d); -} 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 @@ -#ifndef FST_TESTS_H -#define FST_TESTS_H - -#include "../src/fst.h" -#include "test_common.h" - -extern char *algs[]; - -extern Test test_fst_is_consistent; -extern Test test_cube_to_fst_to_cube; -extern Test test_fst_move; -extern Test test_fst_inverse; - -extern TestSuite fst_pre_init_suite; -extern TestSuite fst_post_init_suite; - -extern TestSuite *fst_suites[]; - -#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 @@ -#include - -#include "alg_tests.h" -#include "coord_tests.h" -#include "fst_tests.h" - -static bool run_test(Test *); -static bool run_suite(TestSuite *); - -static bool -run_test(Test *test) -{ - int i; - - printf("Running test %s...", test->name); - for (i = 0; test->cases[i] != NULL; i++) { - if (!test->t(test->cases[i])) { - printf("FAILED!\n"); - return false; - } - } - - printf("OK\n"); - return true; -} - -static bool -run_suite(TestSuite *suite) -{ - int i; - - if (suite->setup != NULL) - suite->setup(); - for (i = 0; suite->tests[i] != NULL; i++) - if(!run_test(suite->tests[i])) - return false; - if (suite->teardown != NULL) - suite->teardown(); - - return true; -} - -static bool -module_in_args(char *module, int argc, char *argv[]) -{ - for (int i = 0; i < argc; i++) - if (!strcmp(module, argv[i])) - return true; - return false; -} - -int main(int argc, char *argv[]) { - /* TODO: init should be in testsuites */ - init_env(); - init_trans(); - /**************************************/ - - TestModule alg = { .name = "alg", .suites = alg_suites }; - TestModule fst = { .name = "fst", .suites = fst_suites }; - TestModule coord = { .name = "coord", .suites = coord_suites }; - TestModule *modules[999] = { - &alg, - &fst, - &coord, - NULL - }; - - - bool all = argc == 1 || module_in_args("all", argc, argv); - int count = 0; - for (int i = 0; modules[i] != NULL; i++) { - if (all || module_in_args(modules[i]->name, argc, argv)) { - for (int j = 0; modules[i]->suites[j] != NULL; j++) { - if (!run_suite(modules[i]->suites[j])) { - return 1; - } else { - count++; - } - } - } - } - - printf("All tests passed (%d test suites).\n", count); - return 0; -} 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 @@ -#ifndef TEST_COMMON_H -#define TEST_COMMON_H - -/* - * Common utilities for testing. - * A VoidMethod can be used as a setup or teardown method for testing, see - * the TestSuite struct. A TestMethod takes a void pointer (usually cast - * to some data to be used for testing, but can also be ignored) and returns - * a bool: true for pass, false for fail. - * A Test consists of a name (string), a TestMethod and an array of void - * pointers that describe the test cases. Each element of this array is - * passed to the test method sequentially, and the test session stops on - * the first failure. - * A test suite is just a list of tests with a setup and a teardown method. - * The setup method is run once and for all before the first test, and the - * teardown is run at the end of the testsuite. - * Finally, a TestModule roughly corresponds to a test file. This type is - * only used in test.c to collect multiple test modules. - */ - -typedef void (*VoidMethod)(void); -typedef bool (*TestMethod)(void *); - -typedef struct { - char * name; - TestMethod t; - void ** cases; -} Test; - -typedef struct { - VoidMethod setup; - Test ** tests; - VoidMethod teardown; -} TestSuite; - -typedef struct { - char * name; - TestSuite ** suites; -} TestModule; - -#endif -- cgit v1.3