diff --git a/makefile b/makefile index 39ed478..676ca82 100644 --- a/makefile +++ b/makefile @@ -94,10 +94,16 @@ mkdirs: -@${MKDIR} ${builddir} -@${MKDIR} test_data -test: all +FILTER ?= + +test: $(BINARY) +ifdef $(FILTER) ./build/$(BINARY) - -test_failed: all +else + ./build/$(BINARY) -f $(FILTER) +endif + +test_failed: $(BINARY) ./build/$(BINARY) _tests_fail clean: diff --git a/src/test/test_bugreports.c b/src/test/test_bugreports.c index 8f50c5b..70ce9fe 100644 --- a/src/test/test_bugreports.c +++ b/src/test/test_bugreports.c @@ -17,7 +17,6 @@ #include #include - SUITE(bug_tests) void setup() { _setup_test_only(); @@ -233,7 +232,6 @@ TEST(nodemcu_309) { } TEST_END(nodemcu_309) - TEST(robert) { // create a clean file system starting at address 0, 2 megabytes big, // sector size 65536, block size 65536, page size 256 @@ -241,30 +239,23 @@ TEST(robert) { int res; spiffs_file fd; - int j; + char fname[32]; - // create three files - for (j = 1; j <= 3; j++) { - char fname[32]; + sprintf(fname, "test.txt"); + fd = SPIFFS_open(FS, fname, SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC, 0); + TEST_CHECK(fd > 0); + int i; + res = SPIFFS_OK; + char buf[500]; + memset(buf, 0xaa, 500); + res = SPIFFS_write(FS, fd, buf, 500); + TEST_CHECK(res >= SPIFFS_OK); + SPIFFS_close(FS, fd); - sprintf(fname, "test%i.txt", j); - fd = SPIFFS_open(FS, fname, SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC, 0); - TEST_CHECK(fd > 0); - int i; - res = SPIFFS_OK; - for (i = 0; i <= 999; i++) { - char *buf = "0123456789ABCDEF"; - res = SPIFFS_write(FS, fd, buf, 16); - } - SPIFFS_close(FS, fd); - - spiffs_stat s; - TEST_CHECK(SPIFFS_stat(FS, fname, &s) == SPIFFS_OK); - printf("file %s stat size %i\n", s.name, s.size); - } int errno = SPIFFS_errno(FS); TEST_CHECK(errno == SPIFFS_OK); + //SPIFFS_vis(FS); // unmount SPIFFS_unmount(FS); @@ -272,16 +263,12 @@ TEST(robert) { res = fs_mount_specific(0, 1024*1024*2, 65536, 65536, 256); TEST_CHECK(res== SPIFFS_OK); + //SPIFFS_vis(FS); - spiffs_DIR d; - struct spiffs_dirent e; - struct spiffs_dirent *pe = &e; - - SPIFFS_opendir(FS, "/", &d); - while ((pe = SPIFFS_readdir(&d, pe))) { - printf("%s [%04x] size:%i\n", pe->name, pe->obj_id, pe->size); - } - SPIFFS_closedir(&d); + spiffs_stat s; + TEST_CHECK(SPIFFS_stat(FS, fname, &s) == SPIFFS_OK); + printf("file %s stat size %i\n", s.name, s.size); + TEST_CHECK(s.size == 500); return TEST_RES_OK; @@ -341,6 +328,4 @@ TEST(spiffs_12) { } TEST_END(spiffs_12) - - SUITE_END(bug_tests) diff --git a/src/test/testrunner.c b/src/test/testrunner.c index ad4fd1f..80e9c72 100644 --- a/src/test/testrunner.c +++ b/src/test/testrunner.c @@ -28,6 +28,8 @@ static struct { test_res *stopped; test_res *stopped_last; FILE *spec; + char incl_filter[256]; + char excl_filter[256]; } test_main; void test_init(void (*on_stop)(test *t)) { @@ -41,7 +43,7 @@ static char check_spec(char *name) { size_t sz; ssize_t read; while ((read = getline(&line, &sz, test_main.spec)) != -1) { - if (strncmp(line, name, strlen(name)) == 0) { + if (strncmp(line, name, strlen(line)-1) == 0) { free(line); return 1; } @@ -53,9 +55,21 @@ static char check_spec(char *name) { } } +static char check_incl_filter(char *name) { + if (strlen(test_main.incl_filter)== 0) return 1; + return strstr(name, test_main.incl_filter) == 0 ? 0 : 1; +} + +static char check_excl_filter(char *name) { + if (strlen(test_main.excl_filter)== 0) return 1; + return strstr(name, test_main.excl_filter) == 0 ? 1 : 0; +} + void add_test(test_f f, char *name, void (*setup)(test *t), void (*teardown)(test *t)) { if (f == 0) return; if (!check_spec(name)) return; + if (!check_incl_filter(name)) return; + if (!check_excl_filter(name)) return; DBGT("adding test %s\n", name); test *t = malloc(sizeof(test)); memset(t, 0, sizeof(test)); @@ -94,16 +108,36 @@ static void dump_res(test_res **head) { } } -void run_tests(int argc, char **args) { +int run_tests(int argc, char **args) { memset(&test_main, 0, sizeof(test_main)); - if (argc > 1) { - printf("running tests from %s\n", args[1]); - FILE *fd = fopen(args[1], "r"); - if (fd == NULL) { - printf("%s not found\n", args[1]); - exit(EXIT_FAILURE); + int arg; + int incl_filter = 0; + int excl_filter = 0; + for (arg = 1; arg < argc; arg++) { + if (strlen(args[arg]) == 0) continue; + if (0 == strcmp("-f", args[arg])) { + incl_filter = 1; + continue; + } + if (0 == strcmp("-e", args[arg])) { + excl_filter = 1; + continue; + } + if (incl_filter) { + strcpy(test_main.incl_filter, args[arg]); + incl_filter = 0; + } else if (excl_filter) { + strcpy(test_main.excl_filter, args[arg]); + excl_filter = 0; + } else { + printf("running tests from %s\n", args[arg]); + FILE *fd = fopen(args[1], "r"); + if (fd == NULL) { + printf("%s not found\n", args[arg]); + return -2; + } + test_main.spec = fd; } - test_main.spec = fd; } DBGT("adding suites...\n"); @@ -115,7 +149,7 @@ void run_tests(int argc, char **args) { if (test_main.test_count == 0) { printf("No tests to run\n"); - return; + return 0; } int fd_success = open("_tests_ok", O_APPEND | O_TRUNC | O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); @@ -133,6 +167,7 @@ void run_tests(int argc, char **args) { DBGT("TEST %i/%i : running test %s\n", i, test_main.test_count, cur_t->name); i++; int res = cur_t->f(cur_t); + cur_t->test_result = res; cur_t->teardown(cur_t); int fd = res == TEST_RES_OK ? fd_success : fd_bad; write(fd, cur_t->name, strlen(cur_t->name)); @@ -169,7 +204,9 @@ void run_tests(int argc, char **args) { dump_res(&test_main.stopped); if (ok < test_main.test_count) { printf("\nFAILED\n"); + return -1; } else { printf("\nALL TESTS OK\n"); + return 0; } } diff --git a/src/test/testrunner.h b/src/test/testrunner.h index 20ae606..be9b555 100644 --- a/src/test/testrunner.h +++ b/src/test/testrunner.h @@ -48,8 +48,8 @@ void add_suites() { } */ -#ifndef TESTS_H_ -#define TESTS_H_ +#ifndef TESTRUNNER_H_ +#define TESTRUNNER_H_ #define TEST_RES_OK 0 #define TEST_RES_FAIL -1 @@ -66,6 +66,7 @@ typedef struct test_s { void (*setup)(struct test_s *t); void (*teardown)(struct test_s *t); struct test_s *_next; + unsigned char test_result; } test; typedef struct test_res_s { @@ -77,6 +78,30 @@ typedef struct test_res_s { printf(" TEST FAIL %s:%i\n", __FILE__, __LINE__); \ goto __fail_stop; \ } +#define TEST_CHECK_EQ(x, y) if ((x) != (y)) { \ + printf(" TEST FAIL %s:%i, %i != %i\n", __FILE__, __LINE__, (x), (y)); \ + goto __fail_stop; \ +} +#define TEST_CHECK_NEQ(x, y) if ((x) == (y)) { \ + printf(" TEST FAIL %s:%i, %i == %i\n", __FILE__, __LINE__, (x), (y)); \ + goto __fail_stop; \ +} +#define TEST_CHECK_GT(x, y) if ((x) <= (y)) { \ + printf(" TEST FAIL %s:%i, %i <= %i\n", __FILE__, __LINE__, (x), (y)); \ + goto __fail_stop; \ +} +#define TEST_CHECK_LT(x, y) if ((x) >= (y)) { \ + printf(" TEST FAIL %s:%i, %i >= %i\n", __FILE__, __LINE__, (x), (y)); \ + goto __fail_stop; \ +} +#define TEST_CHECK_GE(x, y) if ((x) < (y)) { \ + printf(" TEST FAIL %s:%i, %i < %i\n", __FILE__, __LINE__, (x), (y)); \ + goto __fail_stop; \ +} +#define TEST_CHECK_LE(x, y) if ((x) > (y)) { \ + printf(" TEST FAIL %s:%i, %i > %i\n", __FILE__, __LINE__, (x), (y)); \ + goto __fail_stop; \ +} #define TEST_ASSERT(x) if (!(x)) { \ printf(" TEST ASSERT %s:%i\n", __FILE__, __LINE__); \ goto __fail_assert; \ @@ -105,6 +130,7 @@ typedef struct test_res_s { void add_suites(); void test_init(void (*on_stop)(test *t)); void add_test(test_f f, char *name, void (*setup)(test *t), void (*teardown)(test *t)); -void run_tests(int argc, char **args); +// returns 0 if all tests ok, -1 if any test failed, -2 on badness +int run_tests(int argc, char **args); -#endif /* TESTS_H_ */ +#endif /* TESTRUNNER_H_ */