diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-01-22 00:23:32 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-01-22 00:23:32 +0100 |
| commit | aaeb153ce869567f4f88f083dba28445216714f6 (patch) | |
| tree | 00848a7ee91478f68f26226d10685c579079fe88 | |
| parent | 6dc6d1004393b7c093a3aef7456af67662d56fee (diff) | |
| download | nissy-aaeb153ce869567f4f88f083dba28445216714f6.tar.gz nissy-aaeb153ce869567f4f88f083dba28445216714f6.zip | |
Started working on testing (broken for now)
| -rw-r--r-- | Makefile | 6 | ||||
| -rw-r--r-- | TODO/testing.md | 13 | ||||
| -rwxr-xr-x | test.sh | 19 | ||||
| -rw-r--r-- | tests/fst_tests.c | 131 | ||||
| -rw-r--r-- | tests/fst_tests.h | 57 | ||||
| -rw-r--r-- | tests/test.c | 48 | ||||
| -rw-r--r-- | tests/test.h | 19 |
7 files changed, 276 insertions, 17 deletions
| @@ -10,7 +10,6 @@ CFLAGS = -std=c99 -pthread -pedantic -Wall -Wextra \ | |||
| 10 | -Wno-unused-parameter -O3 ${CPPFLAGS} | 10 | -Wno-unused-parameter -O3 ${CPPFLAGS} |
| 11 | DBGFLAGS = -std=c99 -pthread -pedantic -Wall -Wextra \ | 11 | DBGFLAGS = -std=c99 -pthread -pedantic -Wall -Wextra \ |
| 12 | -Wno-unused-parameter -g ${CPPFLAGS} | 12 | -Wno-unused-parameter -g ${CPPFLAGS} |
| 13 | TESTFLAGS = ${DBGFLAGS} -DTEST | ||
| 14 | 13 | ||
| 15 | CC = cc | 14 | CC = cc |
| 16 | 15 | ||
| @@ -20,11 +19,6 @@ all: nissy | |||
| 20 | nissy: clean | 19 | nissy: clean |
| 21 | ${CC} ${CFLAGS} -o nissy src/*.c | 20 | ${CC} ${CFLAGS} -o nissy src/*.c |
| 22 | 21 | ||
| 23 | test: | ||
| 24 | ${CC} ${TESTFLAGS} -o nissy-test src/*.c tests/*.c | ||
| 25 | ./nissy-test | ||
| 26 | rm nissy-test | ||
| 27 | |||
| 28 | nissy.exe: | 22 | nissy.exe: |
| 29 | x86_64-w64-mingw32-gcc ${CFLAGS} -static -o nissy.exe src/*.c | 23 | x86_64-w64-mingw32-gcc ${CFLAGS} -static -o nissy.exe src/*.c |
| 30 | 24 | ||
diff --git a/TODO/testing.md b/TODO/testing.md index 9b6f23a..78e6fbb 100644 --- a/TODO/testing.md +++ b/TODO/testing.md | |||
| @@ -2,17 +2,8 @@ | |||
| 2 | 2 | ||
| 3 | ## Architecture | 3 | ## Architecture |
| 4 | 4 | ||
| 5 | * Folder structure: each module (.h file) has a corresponding test/module_name | 5 | * write makefile |
| 6 | folder containing the important tests. | 6 | * how to build everything except nissy.c? |
| 7 | * How to test pre / post -init()? | ||
| 8 | * Makefile: one target for each module with correct dependencies. | ||
| 9 | * Makefile: perhaps write a specific makefile for testing in test folder. | ||
| 10 | |||
| 11 | ## Test sttructure | ||
| 12 | |||
| 13 | * Make consistent | ||
| 14 | * Little output for success | ||
| 15 | * Stop on first failed? (automatic with makefile) | ||
| 16 | 7 | ||
| 17 | ## Write tests | 8 | ## Write tests |
| 18 | 9 | ||
| @@ -0,0 +1,19 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | # Warning: tests should be in the correct order! | ||
| 4 | # Every testing module assumes that modules it depends on pass their tests. | ||
| 5 | all_modules=fst | ||
| 6 | |||
| 7 | test_module() { | ||
| 8 | modules=$@ | ||
| 9 | for m in modules; do | ||
| 10 | ./tests/nissy_test $m | ||
| 11 | done | ||
| 12 | } | ||
| 13 | |||
| 14 | if [ -n "$@" ]; then | ||
| 15 | test_module $@ | ||
| 16 | else | ||
| 17 | test_module $all_modules | ||
| 18 | fi | ||
| 19 | rm tests/nissy_test | ||
diff --git a/tests/fst_tests.c b/tests/fst_tests.c new file mode 100644 index 0000000..14aee1c --- /dev/null +++ b/tests/fst_tests.c | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | #include "fst_tests.h" | ||
| 2 | |||
| 3 | static bool check_equal_and_log(Cube *, Cube *); | ||
| 4 | static void void_to_cube(void *, Cube *); | ||
| 5 | |||
| 6 | char *algs[] = { | ||
| 7 | "", | ||
| 8 | "U", "U2", "U'", "D", "D2", "D'", "R", "R2", "R'", | ||
| 9 | "L", "L2", "L'", "F", "F2", "F'", "B", "B2", "B'", | ||
| 10 | "U2 R2 U2 R2 U2", | ||
| 11 | "U2 F2 R2 B2 U2 D2 F2 L2 B2", | ||
| 12 | "RUR'URU2R'", | ||
| 13 | "L2 D R U2 B2 L", | ||
| 14 | "R'U'F", | ||
| 15 | "F2 U' R2 D' B2 D2 R2 D2 R2 U' F L' U' R B F2 R B' D2", | ||
| 16 | "D L2 F2 R2 D R2 U L2 U' B2 D L' F2 U2 B' L D' U' R' B2 F2", | ||
| 17 | "F' L2 F' D' R F2 L U L' D2 R2 F2 D2 R2 B' L2 B2 U2 F D2 B", | ||
| 18 | NULL, | ||
| 19 | }; | ||
| 20 | |||
| 21 | static bool | ||
| 22 | check_equal_and_log(Cube *c, Cube *d) | ||
| 23 | { | ||
| 24 | bool ret = equal(c, d); | ||
| 25 | |||
| 26 | if (!ret) { | ||
| 27 | printf("\n"); | ||
| 28 | printf("These cubes should be equal, but are not:\n\n"); | ||
| 29 | print_cube(c); | ||
| 30 | printf("\n"); | ||
| 31 | print_cube(d); | ||
| 32 | printf("\n"); | ||
| 33 | } | ||
| 34 | |||
| 35 | return ret; | ||
| 36 | } | ||
| 37 | |||
| 38 | static void | ||
| 39 | void_to_cube(void *a, Cube *c) | ||
| 40 | { | ||
| 41 | char *algstr; | ||
| 42 | Alg *alg; | ||
| 43 | |||
| 44 | algstr = (char *)a; | ||
| 45 | alg = new_alg(algstr); | ||
| 46 | make_solved(c); | ||
| 47 | apply_alg(alg, c); | ||
| 48 | free_alg(alg); | ||
| 49 | } | ||
| 50 | |||
| 51 | bool | ||
| 52 | testmethod_fst_is_consistent(void *a) | ||
| 53 | { | ||
| 54 | FstCube fst_uf, fst_fr, fst_rd; | ||
| 55 | Cube c, c_fr, c_rd; | ||
| 56 | bool consistent_fr, consistent_rd, result; | ||
| 57 | |||
| 58 | void_to_cube(a, &c); | ||
| 59 | copy_cube(&c, &c_fr); | ||
| 60 | apply_trans(fr, &c_fr); | ||
| 61 | |||
| 62 | copy_cube(&c, &c_rd); | ||
| 63 | apply_trans(rd, &c_rd); | ||
| 64 | |||
| 65 | fst_uf = cube_to_fst(&c); | ||
| 66 | fst_fr = cube_to_fst(&c_fr); | ||
| 67 | fst_rd = cube_to_fst(&c_rd); | ||
| 68 | |||
| 69 | consistent_fr = fst_uf.fr_eofb == fst_fr.uf_eofb && | ||
| 70 | fst_uf.fr_eposepe == fst_fr.uf_eposepe && | ||
| 71 | fst_uf.fr_coud == fst_fr.uf_coud; | ||
| 72 | |||
| 73 | consistent_rd = fst_uf.rd_eofb == fst_rd.uf_eofb && | ||
| 74 | fst_uf.rd_eposepe == fst_rd.uf_eposepe && | ||
| 75 | fst_uf.rd_coud == fst_rd.uf_coud; | ||
| 76 | |||
| 77 | result = consistent_fr && consistent_rd; | ||
| 78 | |||
| 79 | if (!result) | ||
| 80 | printf("\nFailed with alg %s\n", (char *)a); | ||
| 81 | |||
| 82 | return result; | ||
| 83 | } | ||
| 84 | |||
| 85 | bool | ||
| 86 | testmethod_cube_to_fst_to_cube(void *a) | ||
| 87 | { | ||
| 88 | Cube c, d; | ||
| 89 | FstCube fst; | ||
| 90 | |||
| 91 | void_to_cube(a, &c); | ||
| 92 | fst = cube_to_fst(&c); | ||
| 93 | fst_to_cube(fst, &d); | ||
| 94 | |||
| 95 | return check_equal_and_log(&c, &d);; | ||
| 96 | } | ||
| 97 | |||
| 98 | bool | ||
| 99 | testmethod_fst_move(void *a) | ||
| 100 | { | ||
| 101 | int i; | ||
| 102 | Alg *alg; | ||
| 103 | Cube c, d; | ||
| 104 | FstCube fst; | ||
| 105 | |||
| 106 | void_to_cube(a, &d); | ||
| 107 | alg = new_alg((char *)a); | ||
| 108 | make_solved(&d); | ||
| 109 | fst = cube_to_fst(&d); | ||
| 110 | |||
| 111 | for (i = 0; i < alg->len; i++) | ||
| 112 | fst = fst_move(alg->move[i], fst); | ||
| 113 | |||
| 114 | fst_to_cube(fst, &d); | ||
| 115 | |||
| 116 | free_alg(alg); | ||
| 117 | |||
| 118 | return check_equal_and_log(&c, &d); | ||
| 119 | } | ||
| 120 | |||
| 121 | bool | ||
| 122 | testmethod_fst_inverse(void *a) | ||
| 123 | { | ||
| 124 | Cube c, d; | ||
| 125 | |||
| 126 | void_to_cube(a, &c); | ||
| 127 | fst_to_cube(fst_inverse(cube_to_fst(&c)), &d); | ||
| 128 | invert_cube(&c); | ||
| 129 | |||
| 130 | return check_equal_and_log(&c, &d); | ||
| 131 | } | ||
diff --git a/tests/fst_tests.h b/tests/fst_tests.h new file mode 100644 index 0000000..2e11069 --- /dev/null +++ b/tests/fst_tests.h | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | #ifndef FST_TESTS_H | ||
| 2 | #define FST_TESTS_H | ||
| 3 | |||
| 4 | #include "../src/fst.h" | ||
| 5 | #include "test.h" | ||
| 6 | |||
| 7 | bool testmethod_fst_is_consistent(void *); | ||
| 8 | bool testmethod_cube_to_fst_to_cube(void *); | ||
| 9 | bool testmethod_fst_move(void *); | ||
| 10 | bool testmethod_fst_inverse(void *); | ||
| 11 | |||
| 12 | extern char *algs[]; | ||
| 13 | |||
| 14 | Test test_fst_is_consistent = { | ||
| 15 | .name = "Consitency of FST (converted from cube)", | ||
| 16 | .t = testmethod_fst_is_consistent, | ||
| 17 | .cases = (void **)algs, | ||
| 18 | }; | ||
| 19 | Test test_cube_to_fst_to_cube = { | ||
| 20 | .name = "Cube to FST to cube", | ||
| 21 | .t = testmethod_cube_to_fst_to_cube, | ||
| 22 | .cases = (void **)algs, | ||
| 23 | }; | ||
| 24 | Test test_fst_move = { | ||
| 25 | .name = "FST move", | ||
| 26 | .t = testmethod_fst_move, | ||
| 27 | .cases = (void **)algs, | ||
| 28 | }; | ||
| 29 | Test test_fst_inverse = { | ||
| 30 | .name = "FST inverse", | ||
| 31 | .t = testmethod_fst_inverse, | ||
| 32 | .cases = (void **)algs, | ||
| 33 | }; | ||
| 34 | |||
| 35 | Test *pre_init[] = { | ||
| 36 | &test_fst_is_consistent, | ||
| 37 | &test_cube_to_fst_to_cube, | ||
| 38 | NULL | ||
| 39 | }; | ||
| 40 | TestSuite fst_pre_init_suite = { | ||
| 41 | .setup = NULL, | ||
| 42 | .tests = pre_init, | ||
| 43 | .teardown = NULL, | ||
| 44 | }; | ||
| 45 | |||
| 46 | Test *post_init[] = { | ||
| 47 | &test_fst_move, | ||
| 48 | &test_fst_inverse, | ||
| 49 | NULL | ||
| 50 | }; | ||
| 51 | TestSuite fst_post_init_suite = { | ||
| 52 | .setup = init_fst, | ||
| 53 | .tests = post_init, | ||
| 54 | .teardown = NULL, | ||
| 55 | }; | ||
| 56 | |||
| 57 | #endif | ||
diff --git a/tests/test.c b/tests/test.c new file mode 100644 index 0000000..c122db8 --- /dev/null +++ b/tests/test.c | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include "fst_tests.h" | ||
| 3 | |||
| 4 | static bool run_test(Test *); | ||
| 5 | static bool run_suite(TestSuite *); | ||
| 6 | |||
| 7 | static bool | ||
| 8 | run_test(Test *test) | ||
| 9 | { | ||
| 10 | int i; | ||
| 11 | |||
| 12 | printf("Running test %s...", test->name); | ||
| 13 | for (i = 0; test->cases[i] != NULL; i++) { | ||
| 14 | if (!test->t(test->cases[i])) { | ||
| 15 | printf("FAILED!\n"); | ||
| 16 | return false; | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | printf("OK\n"); | ||
| 21 | return true; | ||
| 22 | } | ||
| 23 | |||
| 24 | static bool | ||
| 25 | run_suite(TestSuite *suite) | ||
| 26 | { | ||
| 27 | int i; | ||
| 28 | |||
| 29 | if (suite->setup != NULL) | ||
| 30 | suite->setup(); | ||
| 31 | for (i = 0; suite->tests[i] != NULL; i++) | ||
| 32 | if(!run_test(suite->tests[i])) | ||
| 33 | return false; | ||
| 34 | if (suite->teardown != NULL) | ||
| 35 | suite->teardown(); | ||
| 36 | |||
| 37 | return true; | ||
| 38 | } | ||
| 39 | |||
| 40 | int main() { | ||
| 41 | if (!run_suite(&fst_pre_init_suite)) | ||
| 42 | return 1; | ||
| 43 | if (!run_suite(&fst_post_init_suite)) | ||
| 44 | return 1; | ||
| 45 | |||
| 46 | printf("All tests passed.\n"); | ||
| 47 | return 0; | ||
| 48 | } | ||
diff --git a/tests/test.h b/tests/test.h new file mode 100644 index 0000000..665127c --- /dev/null +++ b/tests/test.h | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | #ifndef TEST_H | ||
| 2 | #define TEST_H | ||
| 3 | |||
| 4 | typedef void (*VoidMethod)(void); | ||
| 5 | typedef bool (*TestMethod)(void *); | ||
| 6 | |||
| 7 | typedef struct { | ||
| 8 | char * name; | ||
| 9 | TestMethod t; | ||
| 10 | void ** cases; | ||
| 11 | } Test; | ||
| 12 | |||
| 13 | typedef struct { | ||
| 14 | VoidMethod setup; | ||
| 15 | Test ** tests; | ||
| 16 | VoidMethod teardown; | ||
| 17 | } TestSuite; | ||
| 18 | |||
| 19 | #endif | ||
