diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | TODO/testing.md | 5 | ||||
| -rwxr-xr-x | test.sh | 20 | ||||
| -rw-r--r-- | tests/fst_tests.c | 10 | ||||
| -rw-r--r-- | tests/fst_tests.h | 2 | ||||
| -rw-r--r-- | tests/test.c | 43 | ||||
| -rw-r--r-- | tests/test.h | 7 | ||||
| -rw-r--r-- | tests/test_common.h | 34 |
8 files changed, 74 insertions, 49 deletions
| @@ -25,7 +25,7 @@ debug: | |||
| 25 | ${CC} ${DBGFLAGS} -o nissy src/*.c | 25 | ${CC} ${DBGFLAGS} -o nissy src/*.c |
| 26 | 26 | ||
| 27 | test: | 27 | test: |
| 28 | ${CC} ${DBGFLAGS} -DTEST -o tests/nissy_test src/*.c tests/*.c | 28 | ${CC} ${DBGFLAGS} -DTEST -o test src/*.c tests/*.c |
| 29 | 29 | ||
| 30 | clean: | 30 | clean: |
| 31 | rm -rf nissy nissy*.exe nissy*.tar.gz doc/nissy.html doc/nissy.pdf | 31 | rm -rf nissy nissy*.exe nissy*.tar.gz doc/nissy.html doc/nissy.pdf |
diff --git a/TODO/testing.md b/TODO/testing.md index 78e6fbb..8c62f87 100644 --- a/TODO/testing.md +++ b/TODO/testing.md | |||
| @@ -1,10 +1,5 @@ | |||
| 1 | # Testing | 1 | # Testing |
| 2 | 2 | ||
| 3 | ## Architecture | ||
| 4 | |||
| 5 | * write makefile | ||
| 6 | * how to build everything except nissy.c? | ||
| 7 | |||
| 8 | ## Write tests | 3 | ## Write tests |
| 9 | 4 | ||
| 10 | * Pretty much all are missing, except fst. | 5 | * Pretty much all are missing, except fst. |
diff --git a/test.sh b/test.sh deleted file mode 100755 index 6a30344..0000000 --- a/test.sh +++ /dev/null | |||
| @@ -1,20 +0,0 @@ | |||
| 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 | make test | ||
| 15 | if [ -n "$@" ]; then | ||
| 16 | test_module $@ | ||
| 17 | else | ||
| 18 | test_module $all_modules | ||
| 19 | fi | ||
| 20 | rm tests/nissy_test | ||
diff --git a/tests/fst_tests.c b/tests/fst_tests.c index 684a479..dfaab39 100644 --- a/tests/fst_tests.c +++ b/tests/fst_tests.c | |||
| @@ -3,9 +3,7 @@ | |||
| 3 | static bool testmethod_fst_is_consistent(void *); | 3 | static bool testmethod_fst_is_consistent(void *); |
| 4 | static bool testmethod_cube_to_fst_to_cube(void *); | 4 | static bool testmethod_cube_to_fst_to_cube(void *); |
| 5 | static bool testmethod_fst_move(void *); | 5 | static bool testmethod_fst_move(void *); |
| 6 | static bool testmethod_fst_inverse(void *); | 6 | static bool testmethod_fst_inverse(void *); static bool check_equal_and_log(Cube *, Cube *); |
| 7 | |||
| 8 | static bool check_equal_and_log(Cube *, Cube *); | ||
| 9 | static void void_to_cube(void *, Cube *); | 7 | static void void_to_cube(void *, Cube *); |
| 10 | 8 | ||
| 11 | char *algs[] = { | 9 | char *algs[] = { |
| @@ -66,6 +64,12 @@ TestSuite fst_post_init_suite = { | |||
| 66 | .teardown = NULL, | 64 | .teardown = NULL, |
| 67 | }; | 65 | }; |
| 68 | 66 | ||
| 67 | TestSuite *fst_testsuites[] = { | ||
| 68 | &fst_pre_init_suite, | ||
| 69 | &fst_post_init_suite, | ||
| 70 | NULL | ||
| 71 | }; | ||
| 72 | |||
| 69 | static bool | 73 | static bool |
| 70 | check_equal_and_log(Cube *c, Cube *d) | 74 | check_equal_and_log(Cube *c, Cube *d) |
| 71 | { | 75 | { |
diff --git a/tests/fst_tests.h b/tests/fst_tests.h index 3a7496e..324028e 100644 --- a/tests/fst_tests.h +++ b/tests/fst_tests.h | |||
| @@ -14,4 +14,6 @@ extern Test test_fst_inverse; | |||
| 14 | extern TestSuite fst_pre_init_suite; | 14 | extern TestSuite fst_pre_init_suite; |
| 15 | extern TestSuite fst_post_init_suite; | 15 | extern TestSuite fst_post_init_suite; |
| 16 | 16 | ||
| 17 | extern TestSuite *fst_testsuites[]; | ||
| 18 | |||
| 17 | #endif | 19 | #endif |
diff --git a/tests/test.c b/tests/test.c index 2055c78..cc14456 100644 --- a/tests/test.c +++ b/tests/test.c | |||
| @@ -1,4 +1,6 @@ | |||
| 1 | #include "test.h" | 1 | #include <stdio.h> |
| 2 | |||
| 3 | #include "fst_tests.h" | ||
| 2 | 4 | ||
| 3 | static bool run_test(Test *); | 5 | static bool run_test(Test *); |
| 4 | static bool run_suite(TestSuite *); | 6 | static bool run_suite(TestSuite *); |
| @@ -36,15 +38,42 @@ run_suite(TestSuite *suite) | |||
| 36 | return true; | 38 | return true; |
| 37 | } | 39 | } |
| 38 | 40 | ||
| 39 | int main() { | 41 | static bool |
| 42 | module_in_args(char *module, int argc, char *argv[]) | ||
| 43 | { | ||
| 44 | for (int i = 0; i < argc; i++) | ||
| 45 | if (!strcmp(module, argv[i])) | ||
| 46 | return true; | ||
| 47 | return false; | ||
| 48 | } | ||
| 49 | |||
| 50 | int main(int argc, char *argv[]) { | ||
| 51 | /* TODO: init should be in testsuites */ | ||
| 40 | init_env(); | 52 | init_env(); |
| 41 | init_trans(); | 53 | init_trans(); |
| 54 | /**************************************/ | ||
| 42 | 55 | ||
| 43 | if (!run_suite(&fst_pre_init_suite)) | 56 | TestModule fst = { .name = "fst", .suites = fst_testsuites }; |
| 44 | return 1; | 57 | TestModule *modules[999] = { |
| 45 | if (!run_suite(&fst_post_init_suite)) | 58 | &fst, |
| 46 | return 1; | 59 | NULL |
| 60 | }; | ||
| 61 | |||
| 62 | |||
| 63 | bool all = argc == 1 || module_in_args("all", argc, argv); | ||
| 64 | int count = 0; | ||
| 65 | for (int i = 0; modules[i] != NULL; i++) { | ||
| 66 | if (all || module_in_args(modules[i]->name, argc, argv)) { | ||
| 67 | for (int j = 0; modules[i]->suites[j] != NULL; j++) { | ||
| 68 | if (!run_suite(modules[i]->suites[j])) { | ||
| 69 | return 1; | ||
| 70 | } else { | ||
| 71 | count++; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 47 | 76 | ||
| 48 | printf("All tests passed.\n"); | 77 | printf("All tests passed (%d test suites).\n", count); |
| 49 | return 0; | 78 | return 0; |
| 50 | } | 79 | } |
diff --git a/tests/test.h b/tests/test.h deleted file mode 100644 index ac30cdb..0000000 --- a/tests/test.h +++ /dev/null | |||
| @@ -1,7 +0,0 @@ | |||
| 1 | #ifndef TEST_H | ||
| 2 | #define TEST_H | ||
| 3 | |||
| 4 | #include <stdio.h> | ||
| 5 | #include "fst_tests.h" | ||
| 6 | |||
| 7 | #endif | ||
diff --git a/tests/test_common.h b/tests/test_common.h index ea35ecd..03511ef 100644 --- a/tests/test_common.h +++ b/tests/test_common.h | |||
| @@ -1,19 +1,41 @@ | |||
| 1 | #ifndef TEST_COMMON_H | 1 | #ifndef TEST_COMMON_H |
| 2 | #define TEST_COMMON_H | 2 | #define TEST_COMMON_H |
| 3 | 3 | ||
| 4 | /* | ||
| 5 | * Common utilities for testing. | ||
| 6 | * A VoidMethod can be used as a setup or teardown method for testing, see | ||
| 7 | * the TestSuite struct. A TestMethod takes a void pointer (usually cast | ||
| 8 | * to some data to be used for testing, but can also be ignored) and returns | ||
| 9 | * a bool: true for pass, false for fail. | ||
| 10 | * A Test consists of a name (string), a TestMethod and an array of void | ||
| 11 | * pointers that describe the test cases. Each element of this array is | ||
| 12 | * passed to the test method sequentially, and the test session stops on | ||
| 13 | * the first failure. | ||
| 14 | * A test suite is just a list of tests with a setup and a teardown method. | ||
| 15 | * The setup method is run once and for all before the first test, and the | ||
| 16 | * teardown is run at the end of the testsuite. | ||
| 17 | * Finally, a TestModule roughly corresponds to a test file. This type is | ||
| 18 | * only used in test.c to collect multiple test modules. | ||
| 19 | */ | ||
| 20 | |||
| 4 | typedef void (*VoidMethod)(void); | 21 | typedef void (*VoidMethod)(void); |
| 5 | typedef bool (*TestMethod)(void *); | 22 | typedef bool (*TestMethod)(void *); |
| 6 | 23 | ||
| 7 | typedef struct { | 24 | typedef struct { |
| 8 | char * name; | 25 | char * name; |
| 9 | TestMethod t; | 26 | TestMethod t; |
| 10 | void ** cases; | 27 | void ** cases; |
| 11 | } Test; | 28 | } Test; |
| 12 | 29 | ||
| 13 | typedef struct { | 30 | typedef struct { |
| 14 | VoidMethod setup; | 31 | VoidMethod setup; |
| 15 | Test ** tests; | 32 | Test ** tests; |
| 16 | VoidMethod teardown; | 33 | VoidMethod teardown; |
| 17 | } TestSuite; | 34 | } TestSuite; |
| 18 | 35 | ||
| 36 | typedef struct { | ||
| 37 | char * name; | ||
| 38 | TestSuite ** suites; | ||
| 39 | } TestModule; | ||
| 40 | |||
| 19 | #endif | 41 | #endif |
