From 3bfd0bb403d62bd942d1912d085ea59b156062fd Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Fri, 10 Feb 2023 23:30:20 +0100 Subject: Added (just a few) tests for alg and added fields to alg struct --- tests/alg_tests.c | 266 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/alg_tests.h | 20 ++++ tests/test.c | 3 + 3 files changed, 289 insertions(+) create mode 100644 tests/alg_tests.c create mode 100644 tests/alg_tests.h (limited to 'tests') 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 @@ +#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 new file mode 100644 index 0000000..518b33e --- /dev/null +++ b/tests/alg_tests.h @@ -0,0 +1,20 @@ +#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 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/test.c b/tests/test.c index a1a382c..8961de1 100644 --- a/tests/test.c +++ b/tests/test.c @@ -1,5 +1,6 @@ #include +#include "alg_tests.h" #include "coord_tests.h" #include "fst_tests.h" @@ -54,9 +55,11 @@ int main(int argc, char *argv[]) { 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 -- cgit v1.3