test system in place
authorNiki Roo <niki@nikiroo.be>
Sun, 27 Feb 2022 13:24:33 +0000 (14:24 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 27 Feb 2022 13:24:33 +0000 (14:24 +0100)
Makefile
README-fr.md
README.md
src/nsub.d
src/tests.d [new file with mode: 0644]
src/tests/launcher.c [new file with mode: 0644]
src/tests/launcher.h [new file with mode: 0644]
src/tests/utils.d [new file with mode: 0644]
src/tests/utils/main.c [new file with mode: 0644]
src/tests/utils/main.h [new file with mode: 0644]
src/utils.d

index dd1423fe3d22a22e5ae42a6c27657eb7af7a09e8..5117ae901f43b5537a7ac0308caa222b88868986 100644 (file)
--- 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
 
index c82b6caac4df891ab92021603deed0b125c4f101..6743527b65dfe2319c78b8e0a602527af7105a0f 100644 (file)
@@ -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
 
index 2e6217d5dbe9ab9932db68a47efd194bf452f304..295f017f6fe31610c96f531ddd488138f79d0f29 100644 (file)
--- 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
 
index d9d5c713899d01190578315f796e2b3f2ba0823a..e6b7ef450002940ac65e6d7e5061925599f0fb3f 100644 (file)
@@ -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 (file)
index 0000000..d04a965
--- /dev/null
@@ -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 (file)
index 0000000..fe34be1
--- /dev/null
@@ -0,0 +1,102 @@
+#include <check.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#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", "\b\b\b\b\b\b\b\b OK ]    ");
+}
+
+void test_failure() {
+       fprintf(stderr, "%s", "\b\b\b\b\b\b\b\bFAIL]    ");
+}
+
+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 (file)
index 0000000..3d28250
--- /dev/null
@@ -0,0 +1,42 @@
+#ifndef _LAUNCHER_H
+#define _LAUNCHER_H
+
+#include <check.h>
+
+#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 (file)
index 0000000..1a530fc
--- /dev/null
@@ -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 (file)
index 0000000..6354957
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.
+ */
+
+#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 (file)
index 0000000..fa1b559
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef SRC_TESTS_UTILS_MAIN_H_
+#define SRC_TESTS_UTILS_MAIN_H_
+
+#include <check.h>
+
+//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_ */
index ecec32c4ac1c83628e1cb546009b8a8cbe83ccbb..a362a0cd196ddd50b4bcf44d1af14873e7abc296 100644 (file)
@@ -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