nissy-nx

A Rubik's cube optimal solver
git clone https://git.tronto.net/nissy-nx
Download | Log | Files | Refs | README | LICENSE

commit aaeb153ce869567f4f88f083dba28445216714f6
parent 6dc6d1004393b7c093a3aef7456af67662d56fee
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Sun, 22 Jan 2023 00:23:32 +0100

Started working on testing (broken for now)

Diffstat:
MMakefile | 6------
MTODO/testing.md | 13++-----------
Atest.sh | 19+++++++++++++++++++
Atests/fst_tests.c | 131+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atests/fst_tests.h | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atests/test.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
Atests/test.h | 19+++++++++++++++++++
7 files changed, 276 insertions(+), 17 deletions(-)

diff --git a/Makefile b/Makefile @@ -10,7 +10,6 @@ CFLAGS = -std=c99 -pthread -pedantic -Wall -Wextra \ -Wno-unused-parameter -O3 ${CPPFLAGS} DBGFLAGS = -std=c99 -pthread -pedantic -Wall -Wextra \ -Wno-unused-parameter -g ${CPPFLAGS} -TESTFLAGS = ${DBGFLAGS} -DTEST CC = cc @@ -20,11 +19,6 @@ all: nissy nissy: clean ${CC} ${CFLAGS} -o nissy src/*.c -test: - ${CC} ${TESTFLAGS} -o nissy-test src/*.c tests/*.c - ./nissy-test - rm nissy-test - nissy.exe: x86_64-w64-mingw32-gcc ${CFLAGS} -static -o nissy.exe src/*.c diff --git a/TODO/testing.md b/TODO/testing.md @@ -2,17 +2,8 @@ ## Architecture -* Folder structure: each module (.h file) has a corresponding test/module_name - folder containing the important tests. -* How to test pre / post -init()? -* Makefile: one target for each module with correct dependencies. -* Makefile: perhaps write a specific makefile for testing in test folder. - -## Test sttructure - -* Make consistent -* Little output for success -* Stop on first failed? (automatic with makefile) +* write makefile +* how to build everything except nissy.c? ## Write tests diff --git a/test.sh b/test.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +# Warning: tests should be in the correct order! +# Every testing module assumes that modules it depends on pass their tests. +all_modules=fst + +test_module() { + modules=$@ + for m in modules; do + ./tests/nissy_test $m + done +} + +if [ -n "$@" ]; then + test_module $@ +else + test_module $all_modules +fi +rm tests/nissy_test diff --git a/tests/fst_tests.c b/tests/fst_tests.c @@ -0,0 +1,131 @@ +#include "fst_tests.h" + +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, +}; + +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, &d); + 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 @@ -0,0 +1,57 @@ +#ifndef FST_TESTS_H +#define FST_TESTS_H + +#include "../src/fst.h" +#include "test.h" + +bool testmethod_fst_is_consistent(void *); +bool testmethod_cube_to_fst_to_cube(void *); +bool testmethod_fst_move(void *); +bool testmethod_fst_inverse(void *); + +extern char *algs[]; + +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 *pre_init[] = { + &test_fst_is_consistent, + &test_cube_to_fst_to_cube, + NULL +}; +TestSuite fst_pre_init_suite = { + .setup = NULL, + .tests = pre_init, + .teardown = NULL, +}; + +Test *post_init[] = { + &test_fst_move, + &test_fst_inverse, + NULL +}; +TestSuite fst_post_init_suite = { + .setup = init_fst, + .tests = post_init, + .teardown = NULL, +}; + +#endif diff --git a/tests/test.c b/tests/test.c @@ -0,0 +1,48 @@ +#include <stdio.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; +} + +int main() { + if (!run_suite(&fst_pre_init_suite)) + return 1; + if (!run_suite(&fst_post_init_suite)) + return 1; + + printf("All tests passed.\n"); + return 0; +} diff --git a/tests/test.h b/tests/test.h @@ -0,0 +1,19 @@ +#ifndef TEST_H +#define TEST_H + +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; + +#endif