From cecdb7c5a0fc4821bc56c6aae5428925ec1baa15 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 26 Dec 2022 12:41:36 +0100 Subject: Fixed fst_inverse --- tests/fst_post_init_tests.c | 114 +++++++++++++++++++++++++++ tests/fst_pre_init_tests.c | 89 +++++++++++++++++++++ tests/fst_test_util.c | 64 +++++++++++++++ tests/fst_test_util.h | 7 ++ tests/inc.h | 1 - tests/nissy_tests.c | 12 +++ tests/test_all.c | 10 --- tests/test_fst.c | 184 -------------------------------------------- 8 files changed, 286 insertions(+), 195 deletions(-) create mode 100644 tests/fst_post_init_tests.c create mode 100644 tests/fst_pre_init_tests.c create mode 100644 tests/fst_test_util.c create mode 100644 tests/fst_test_util.h delete mode 100644 tests/inc.h create mode 100644 tests/nissy_tests.c delete mode 100644 tests/test_all.c delete mode 100644 tests/test_fst.c (limited to 'tests') diff --git a/tests/fst_post_init_tests.c b/tests/fst_post_init_tests.c new file mode 100644 index 0000000..653e379 --- /dev/null +++ b/tests/fst_post_init_tests.c @@ -0,0 +1,114 @@ +#include "fst_test_util.h" + +static bool fst_move_testcase(Cube *c, Alg *a); +static bool fst_inverse_testcase(Cube *c, Alg *a); + +static bool fst_move_test(); +static bool fst_where_is_edge_test(); +static bool fst_inverse_test(); + +static Tester test[] = { + fst_move_test, + fst_where_is_edge_test, + fst_inverse_test, + NULL +}; + +static char *name[] = { + "FST move", + "FST where is edge", + "FST inverse", +}; + +static bool +fst_move_testcase(Cube *c, Alg *a) +{ + int i; + Cube d; + FstCube fst; + + make_solved(&d); + fst = cube_to_fst(&d); + + for (i = 0; i < a->len; i++) { + if (a->inv[i] || a->move[i] > B3) { + printf("Cannot apply the following alg to FST: "); + print_alg(a, false); + return false; + } + fst = fst_move(a->move[i], fst); + } + + fst_to_cube(fst, &d); + + return equal_and_log(c, &d); +} + +static bool +fst_inverse_testcase(Cube *c, Alg *a) +{ + Cube d; + + fst_to_cube(fst_inverse(cube_to_fst(c)), &d); + invert_cube(c); + + return equal_and_log(c, &d); +} + +static bool +fst_move_test() +{ + return try_all_str(fst_move_testcase, "FST move incorrect"); +} + +static bool +fst_where_is_edge_test() +{ + int i; + Alg *scr; + Cube c, d; + FstCube fst; + + /* Testing on a single scramble is fine for now */ + scr = new_alg("RUFDL2B2FRD"); + make_solved(&c); + apply_alg(scr, &c); + fst = cube_to_fst(&c); + + for (i = 0; i < 12; i++) { + if (fst_where_is_edge(c.ep[i], fst) != i) { + fst_to_cube(fst, &d); + printf("Alg: "); + print_alg(scr, false); + printf("Expected:\n"); + print_cube(&c); + printf("But got:\n"); + print_cube(&d); + return false; + } + } + + return true; +} + +static bool +fst_inverse_test() +{ + return try_all_str(fst_inverse_testcase, "FST test incorrect"); +} + +void fst_post_init_testall() { + int i; + + init_fst(); + + for (i = 0; test[i] != NULL; i++) { + printf("Test: %s\n", name[i]); + if (!test[i]()) { + printf("Failed!\n"); + exit(1); + } + printf("Passed.\n\n"); + } + printf("All FST post-init tests passed.\n\n"); +} diff --git a/tests/fst_pre_init_tests.c b/tests/fst_pre_init_tests.c new file mode 100644 index 0000000..6b7a440 --- /dev/null +++ b/tests/fst_pre_init_tests.c @@ -0,0 +1,89 @@ +#include "fst_test_util.h" + +static bool cube_to_fst_to_cube_testcase(Cube *c, Alg *a); +static bool fst_is_consistent_testcase(Cube *c, Alg *a); + +static bool fst_is_consistent_test(); +static bool cube_to_fst_to_cube_test(); + +static Tester test[] = { + fst_is_consistent_test, + cube_to_fst_to_cube_test, + NULL +}; + +static char *name[] = { + "Consistency of FST (converted from cube)", + "Cube to FST to cube", +}; + +static bool +fst_is_consistent_testcase(Cube *c, Alg *a) +{ + FstCube fst_uf, fst_fr, fst_rd; + Cube c_fr, c_rd; + bool consistent_fr, consistent_rd; + + 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; + + return consistent_fr && consistent_rd; +} + +static bool +cube_to_fst_to_cube_testcase(Cube *c, Alg *a) +{ + Cube d; + FstCube fst; + + fst = cube_to_fst(c); + fst_to_cube(fst, &d); + + return equal_and_log(c, &d);; +} + +static bool +cube_to_fst_to_cube_test() +{ + return try_all_str( + cube_to_fst_to_cube_testcase, "Cube to FST to cube failed"); +} + +static bool +fst_is_consistent_test() +{ + return try_all_str( + fst_is_consistent_testcase, "FST from cube not consistent"); +} + +void fst_pre_init_testall() { + int i; + + init_env(); + init_trans(); + + for (i = 0; test[i] != NULL; i++) { + printf("Test: %s\n", name[i]); + if (!test[i]()) { + printf("Failed!\n"); + exit(1); + } + printf("Passed.\n\n"); + } + printf("All FST pre-init tests passed.\n\n"); +} diff --git a/tests/fst_test_util.c b/tests/fst_test_util.c new file mode 100644 index 0000000..35c1e9f --- /dev/null +++ b/tests/fst_test_util.c @@ -0,0 +1,64 @@ +#include "fst_test_util.h" + +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, +}; + +bool +equal_and_log(Cube *c, Cube *d) +{ + bool ret = equal(c, d); + + if (!ret) { + printf("These cubes should be equal, but are not:\n\n"); + print_cube(c); + printf("\n"); + print_cube(d); + printf("\n"); + } + + return ret; +} + +bool +try_str(CubeTester f, char *algstr, char *msg) +{ + bool b; + Alg *a; + Cube c; + + a = new_alg(algstr); + make_solved(&c); + apply_alg(a, &c); + + if (!(b = f(&c, a))) + printf("%s with alg %s\n", msg, algstr); + + free_alg(a); + + return b; +} + +bool +try_all_str(CubeTester f, char *msg) +{ + bool b; + int i; + + b = true; + for (i = 0; algs[i] != NULL; i++) + b = b && try_str(f, algs[i], msg); + + return b; +} diff --git a/tests/fst_test_util.h b/tests/fst_test_util.h new file mode 100644 index 0000000..b67b945 --- /dev/null +++ b/tests/fst_test_util.h @@ -0,0 +1,7 @@ +#include "../src/fst.h" + +extern char *algs[]; + +bool equal_and_log(Cube *c, Cube *d); +bool try_str(CubeTester f, char *algstr, char *msg); +bool try_all_str(CubeTester f, char *msg); diff --git a/tests/inc.h b/tests/inc.h deleted file mode 100644 index 53c5fdf..0000000 --- a/tests/inc.h +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/tests/nissy_tests.c b/tests/nissy_tests.c new file mode 100644 index 0000000..d959423 --- /dev/null +++ b/tests/nissy_tests.c @@ -0,0 +1,12 @@ +#include + +void fst_pre_init_testall(); +void fst_post_init_testall(); + +int main() { + fst_pre_init_testall(); + fst_post_init_testall(); + + printf("All tests passed.\n"); + return 0; +} diff --git a/tests/test_all.c b/tests/test_all.c deleted file mode 100644 index 4daeec0..0000000 --- a/tests/test_all.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "inc.h" - -void test_fst_all(); - -int main() { - test_fst_all(); - - printf("All tests passed.\n"); - return 0; -} diff --git a/tests/test_fst.c b/tests/test_fst.c deleted file mode 100644 index ad1caba..0000000 --- a/tests/test_fst.c +++ /dev/null @@ -1,184 +0,0 @@ -#include "inc.h" -#include "../src/fst.h" - -static bool cube_to_fst_to_cube(Cube *c, Alg *a); -static bool fst_consistent(Cube *c, Alg *a); -static bool fst_move_test(Cube *c, Alg *a); -static bool fst_inverse_test(Cube *c, Alg *a); -static bool try_str(CubeTester f, char *algstr, char *msg); -static bool try_all_str(CubeTester f, char *msg); - -static bool test_fst_consistent_algs(); -static bool test_cube_to_fst_to_cube_algs(); -static bool test_fst_move_algs(); -static bool test_fst_inverse_algs(); - -static Tester test[] = { - test_fst_consistent_algs, - test_cube_to_fst_to_cube_algs, - test_fst_move_algs, - test_fst_inverse_algs, - NULL -}; - -static char *name[] = { - "Consistency of FST (converted from cube)", - "Cube to FST to cube", - "FST move", - "FST inverse", -}; - -static 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, -}; - -static bool -fst_consistent(Cube *c, Alg *a) -{ - FstCube fst; - - fst = cube_to_fst(c); - - /* TODO: check consistency of fr_* and rd_* with uf_* */ - - return true; -} - -static bool -cube_to_fst_to_cube(Cube *c, Alg *a) -{ - Cube d; - FstCube fst; - - fst = cube_to_fst(c); - fst_to_cube(fst, &d); - - if (!equal(c, &d)) { - printf("Cubes are different:\n\n"); - printf("Cube 1:\n"); - print_cube(c); - printf("\nCube 2:\n"); - print_cube(&d); - printf("\n"); - return false; - } - - return true; -} - -static bool -fst_move_test(Cube *c, Alg *a) -{ - int i; - Cube d; - FstCube fst; - - fst = cube_to_fst(c); - - for (i = 0; i < a->len; i++) { - if (a->inv[i] || a->move[i] > B3) { - printf("Cannot apply the following alg to FST: "); - print_alg(a, false); - return false; - } - fst = fst_move(a->move[i], fst); - } - - fst_to_cube(fst, &d); - - return equal(c, &d); -} - -static bool -fst_inverse_test(Cube *c, Alg *a) -{ - Cube d; - - fst_to_cube(fst_inverse(cube_to_fst(c)), &d); - invert_cube(c); - - return equal(c, &d); -} - -static bool -try_str(CubeTester f, char *algstr, char *msg) -{ - bool b; - Alg *a; - Cube c; - - a = new_alg(algstr); - make_solved(&c); - apply_alg(a, &c); - - if (!(b = f(&c, a))) - printf("%s with alg %s\n", msg, algstr); - - free_alg(a); - - return b; -} - -static bool -try_all_str(CubeTester f, char *msg) -{ - bool b; - int i; - - b = true; - for (i = 0; algs[i] != NULL; i++) - b = b && try_str(f, algs[i], msg); - - return b; -} - -static bool -test_cube_to_fst_to_cube_algs() -{ - return try_all_str(cube_to_fst_to_cube, "Cube to FST to cube failed"); -} - -static bool -test_fst_consistent_algs() -{ - return try_all_str(fst_consistent, "FST from cube not consistent"); -} - -static bool -test_fst_move_algs() -{ - return try_all_str(fst_move_test, "FST move incorrect"); -} - -static bool -test_fst_inverse_algs() -{ - return try_all_str(fst_inverse_test, "FST test incorrect"); -} - -void test_fst_all() { - int i; - - init_trans(); - - for (i = 0; test[i] != NULL; i++) { - printf("Test: %s\n", name[i]); - if (!test[i]()) { - printf("Failed!\n"); - exit(1); - } - printf("Passed.\n\n"); - } - printf("All FST tests passed.\n\n"); -} -- cgit v1.3