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:
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
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
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
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
-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
--- /dev/null
+# 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
--- /dev/null
+#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;
+}
+
--- /dev/null
+#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
+
--- /dev/null
+# 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
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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_ */
-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