From: Niki Roo Date: Sun, 27 Feb 2022 13:24:33 +0000 (+0100) Subject: test system in place X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=c4fd1063436d588930788aefc7ef611d5e117e57;p=nsub.git test system in place --- diff --git a/Makefile b/Makefile index dd1423f..5117ae9 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,27 @@ NAME=nsub .PHONY: all build rebuild install uninstall clean mrpropre mrpropre love debug \ - doc man + doc man run tests test run-test run-test-more all: build build: bin/nsub @echo Build successful. +run: bin/nsub + bin/nsub + +tests: test + +test: + $(MAKE) -C src -f tests.d + +run-test: + $(MAKE) -C src -f tests.d run + +run-test-more: + $(MAKE) -C src -f tests.d run-more + rebuild: clean build doc: @@ -25,6 +39,7 @@ debug: mrproper: mrpropre mrpropre: clean + $(MAKE) -C src -f tests.d mrpropre $(MAKE) -C src -f utils.d mrpropre $(MAKE) -C src -f nsub.d mrpropre rm -f man/man1/*.1 man/*/man1/*.1 @@ -33,6 +48,7 @@ mrpropre: clean rmdir doc || true clean: + $(MAKE) -C src -f tests.d clean $(MAKE) -C src -f utils.d clean $(MAKE) -C src -f nsub.d clean diff --git a/README-fr.md b/README-fr.md index c82b6ca..6743527 100644 --- a/README-fr.md +++ b/README-fr.md @@ -51,12 +51,15 @@ Lancez simplement `make`. Vous pouvez aussi utiliser ces options make : -- `make install PREFIX=/usr/local` : installe le programme dans PREFIX (par défaut, /usr/local) +- `make doc` : génère la documentation Doxygen (`doxygen` est requis) +- `make man` : génère le manuel (`pandoc` est requis) +- `make install PREFIX=/usr/local` : installe le programme dans PREFIX (par défaut, /usr/local) et le manuel si généré - `make uninstall` : désinstalle le programme de PREFIX - `make clear` : efface les fichiers temporaires - `make mrpropre` : efface les fichiers temporaires mais aussi l'exécutable principal et la documentation -- `make doc` : génère la documentation Doxygen (`doxygen` est requis) -- `make man` : génère le manuel (`pandoc` est requis) +- `make test` : compile les tests unitaires (`check` est requis) +- `make run-test` : démarre les tests unitaires +- `make run-test-more` : démarre les tests unitaires supplémentaires (peut être long) ## Auteur diff --git a/README.md b/README.md index 2e6217d..295f017 100644 --- a/README.md +++ b/README.md @@ -51,12 +51,15 @@ Just run `make`. You can also use those make targets: -- `make install PREFIX=/usr/local`: install the program into PREFIX (default is `/usr/local`) +- `make doc`: build the Doxygen documentation (`doxygen` required) +- `make man`: build the man page (`pandoc` required) +- `make install PREFIX=/usr/local`: install the program into PREFIX (default is `/usr/local`) and the manual if built - `make uninstall`: uninstall the program from the given PREFIX - `make clear`: clear the temporary files - `make mrpropre`: clear everything, including the main executable and the documentation -- `make doc`: build the Doxygen documentation (`doxygen` required) -- `make man`: build the man page (`pandoc` required) +- `make test`: build the unit tests (`check` required) +- `make run-test`: start the unit tests +- `make run-test-more`: start the extra unit tests (can be long) ## Author diff --git a/src/nsub.d b/src/nsub.d index d9d5c71..e6b7ef4 100644 --- a/src/nsub.d +++ b/src/nsub.d @@ -1,5 +1,7 @@ -CFLAGS += -Wall -I./ -std=c99 -CXXFLAGS += -Wall -I./ +# Note: 99+ required for-loop initial declaration (CentOS 6) + +CFLAGS += -Wall -pedantic -I./ -std=c99 +CXXFLAGS += -Wall -pedantic -I./ PREFIX = /usr/local ifdef DEBUG diff --git a/src/tests.d b/src/tests.d new file mode 100644 index 0000000..d04a965 --- /dev/null +++ b/src/tests.d @@ -0,0 +1,34 @@ +# Note: 99+ required for-loop initial declaration (CentOS 6) + +CFLAGS += -Wall -pedantic -I./ -I ../ -std=c99 +CXXFLAGS += -Wall -pedantic -I./ -I ../ +LDFLAGS += -lcheck + +ifdef DEBUG +CFLAGS += -ggdb -O0 +CXXFLAGS += -ggdb -O0 +endif + +.PHONY: all clean mrpropre mrpropre test tests run run-more + +all: test + +tests: test + +test: tests/launcher.o + $(MAKE) -C tests/ -f utils.d + +clean: + $(MAKE) -C tests/ -f utils.d clean + rm -f tests/launcher.o + +mrproper: mrpropre + +mrpropre: clean + $(MAKE) -C tests/ -f utils.d mrpropre + +run: + $(MAKE) -C tests/ -f utils.d run + +run-more: + $(MAKE) -C tests/ -f utils.d run-more diff --git a/src/tests/launcher.c b/src/tests/launcher.c new file mode 100644 index 0000000..fe34be1 --- /dev/null +++ b/src/tests/launcher.c @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +#include "launcher.h" + +// CK_ENV : Gets the print mode from the environment variable CK_VERBOSITY, +// which can have the values "silent", "minimal", "normal", "verbose". If the +// variable is not found or the value is not recognized, the print mode is set +// to CK_NORMAL. + +// How to start the program: +// ======================== +// +// $0 +// test the code (normal tests only) +// $0 --more +// run extra tests (if available; usually, they are specified as extra if they +// take long to process) +// +// +// $0 --name NAME +// do not test, just format the name as if it was tested +// $0 --passed +// format the line as if it was passes +// $0 --failed +// format the line as if it was failed + +char *tests_name = NULL; + +void test_init(const char * const name) { + int i; + int cols; + struct winsize ws; + + ioctl(1, TIOCGWINSZ, &ws); + cols = ws.ws_col; + + if (!tests_name) { + tests_name = malloc(sizeof(char) * (cols + 1)); + } + + for (i = 0; i < (cols + 1); i++) { + if (i < 4) + tests_name[i] = ' '; + else + tests_name[i] = '.'; + } + + strcpy(tests_name + 4, name); + tests_name[strlen(name) + 4] = ' '; + + strcpy(tests_name + cols - 6 - 1 - 4, " [ ?? ] "); + + fprintf(stderr, "%s", tests_name); +} + +void test_success() { + fprintf(stderr, "%s", " OK ] "); +} + +void test_failure() { + fprintf(stderr, "%s", "FAIL] "); +} + +int main(int argc, char **argv) { + int failed; + SRunner *runner; + int more; + + if (argc > 1 && !strcmp("--name", argv[1])) { + test_init(argv[2]); + return 0; + } + if (argc > 1 && !strcmp("--passed", argv[1])) { + test_success(); + return 0; + } + if (argc > 1 && !strcmp("--failed", argv[1])) { + test_failure(); + return 0; + } + + more = 0; + if (argc > 1 && !strcmp("--more", argv[1])) + more = 1; + runner = get_tests(more); + + failed = 0; + if (runner) { + srunner_run_all(runner, CK_ENV); + + failed = srunner_ntests_failed(runner); + srunner_free(runner); + } else { + printf(">>> No tests have been found <<<\n"); + } + + return failed; +} + diff --git a/src/tests/launcher.h b/src/tests/launcher.h new file mode 100644 index 0000000..3d28250 --- /dev/null +++ b/src/tests/launcher.h @@ -0,0 +1,42 @@ +#ifndef _LAUNCHER_H +#define _LAUNCHER_H + +#include + +#define START(name) \ +START_TEST(name) {\ + test_init(#name);\ + +#define END \ + test_success();\ +}\ +END_TEST\ + +#define FAIL(...) \ +ck_abort_msg(__VA_ARGS__)\ + +#define ASSERT_EQUALS_STR(title, un, deux) \ + if (strcmp(un, deux)) { \ +ck_abort_msg("%s\n\tExpected: <%s>\n\tReceived: <%s>", title, un, deux); \ +} + +#define ASSERT_EQUALS_INT(title, un, deux) \ + if (un != deux) { \ +ck_abort_msg("%s\n\tExpected: %d\n\tReceived: %d", title, un, deux); \ +} + +#define ASSERT_EQUALS_SIZE(title, un, deux) \ + if (un != deux) { \ +ck_abort_msg("%s\n\tExpected: %zu\n\tReceived: %zu", title, un, deux); \ +} + +SRunner *get_tests(int more); + +void test_init(const char * const name); + +void test_success(); + +void test_failure(); + +#endif + diff --git a/src/tests/utils.d b/src/tests/utils.d new file mode 100644 index 0000000..1a530fc --- /dev/null +++ b/src/tests/utils.d @@ -0,0 +1,38 @@ +# Note: 99+ required for-loop initial declaration (CentOS 6) + +CFLAGS += -Wall -pedantic -I./ -I ../ -std=c99 +CXXFLAGS += -Wall -pedantic -I./ -I ../ +LDFLAGS += -lcheck + +ifdef DEBUG +CFLAGS += -ggdb -O0 +CXXFLAGS += -ggdb -O0 +endif + +.PHONY: all clean mrpropre mrpropre test tests run run-more + +SOURCES=$(wildcard utils/*.c) +OBJECTS=$(SOURCES:%.c=%.o) + +all: test + +tests: test + +test: launcher.o ../../bin/libutils.o $(OBJECTS) + mkdir -p bin + $(CC) $(CFLAGS) $^ -o bin/utils $(LDFLAGS) + +run: + bin/utils + +run-more: + bin/utils --more + +clean: + rm -f utils/*.o + +mrproper: mrpropre + +mrpropre: clean + rm -f bin/utils + rmdir bin || true diff --git a/src/tests/utils/main.c b/src/tests/utils/main.c new file mode 100644 index 0000000..6354957 --- /dev/null +++ b/src/tests/utils/main.c @@ -0,0 +1,33 @@ +/* + * CUtils: some small C utilities + * + * Copyright (C) 2022 Niki Roo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "main.h" +#include "launcher.h" + +SRunner *get_tests(int more) { + //TODO: add tests (the code for those is not ready yet) + /* + SRunner *runner = srunner_create(test_cstring("cstring")); + if (more) + srunner_add_suite(runner, test_cstring_more("more tests (longer)")); + + return runner; + */ + return NULL; +} diff --git a/src/tests/utils/main.h b/src/tests/utils/main.h new file mode 100644 index 0000000..fa1b559 --- /dev/null +++ b/src/tests/utils/main.h @@ -0,0 +1,29 @@ +/* + * CUtils: some small C utilities + * + * Copyright (C) 2022 Niki Roo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef SRC_TESTS_UTILS_MAIN_H_ +#define SRC_TESTS_UTILS_MAIN_H_ + +#include + +//TODO: add tests (the code for those is not ready yet) +//Suite *test_cstring(const char title[]); +//Suite *test_cstring_more(const char title[]); + +#endif /* SRC_TESTS_UTILS_MAIN_H_ */ diff --git a/src/utils.d b/src/utils.d index ecec32c..a362a0c 100644 --- a/src/utils.d +++ b/src/utils.d @@ -1,5 +1,8 @@ -CFLAGS += -Wall -I./ -std=c99 -CXXFLAGS += -Wall -I./ +# Note: 99+ required for-loop initial declaration (CentOS 6) +# Note: gnu required for net.c/net.h + +CFLAGS += -Wall -pedantic -I./ -std=gnu99 +CXXFLAGS += -Wall -pedantic -I./ PREFIX = /usr/local ifdef DEBUG