From e9b24fd125833c77a110e78398f2429eff2fc1e6 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sun, 15 Jan 2023 22:09:47 +0100 Subject: [PATCH] increase test coverage --- ascii.c | 5 ++++- atoi.c | 4 ++-- makefile | 50 ++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/ascii.c b/ascii.c index c61554b..dd54596 100644 --- a/ascii.c +++ b/ascii.c @@ -134,13 +134,16 @@ int main (int argc, char *argv[]) // test: ascii.exe -h // test: ascii.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }' -// test: ascii.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }' +// test: ascii.exe -_ 2>/dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }' +// test: ascii.exe error 2>&1 | grep -q 'invalid option' // test: ascii.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }' // test: ascii.exe | awk '/ 64\[40\] @/ { rc=1 } END { exit (1-rc) }' // test: ascii.exe | awk '/127\[7f\] DEL/ { rc=1 } END { exit (1-rc) }' +// test: ascii.exe -v | grep -q version // test: ascii.exe -c 4 | tail -1 | awk '/63/ { rc=1 } END { exit (1-rc) }' // test: ascii.exe -c 5 | tail -1 | awk '/51/ { rc=1 } END { exit (1-rc) }' // test: ascii.exe -c 6 | tail -1 | awk '/42/ { rc=1 } END { exit (1-rc) }' +// test: ascii.exe -c 2>&1 | grep -q 'missing number of columns' /* vim: set ts=4 sw=4 et: */ diff --git a/atoi.c b/atoi.c index 5547020..50ec270 100644 --- a/atoi.c +++ b/atoi.c @@ -7,7 +7,7 @@ int atoi (char *str) if (*str == '-') { s = 0; - str++; + str++; } while (*str != 0) { @@ -20,4 +20,4 @@ int atoi (char *str) return (s) ? i : -i; } -/* vim: set ts=4 sw=4 et */ +/* vim: set ts=4 sw=4 et: */ diff --git a/makefile b/makefile index c1855b2..0f8ca5f 100644 --- a/makefile +++ b/makefile @@ -11,19 +11,20 @@ OFLAGS = -O4 -Os #OFLAGS += -malign-double CFLAGS += -W -Wall -Wextra -g CFLAGS += -std=c99 -D_XOPEN_SOURCE=500 -CFLAGS += $(OFLAGS) $(INCLUDES) -LDFLAGS += -g +CFLAGS += $(OFLAGS) $(INCLUDES) $(GCOV) +LDFLAGS += -g $(GCOV) # Targets ALLEXE = ALLEXE += ascii -ALLEXE += skel +#ALLEXE += skel SHELL = bash -MAKE = mingw32-make +#MAKE = mingw32-make MAKEFLAGS += -s +include $(wildcard .makefile) # Functions @@ -43,11 +44,6 @@ VALID = $(call TITLE, $(1)) && $(2) && $(call PASS, SUCCESS) || { $(call FAIL, F all: depends $(MAKE) $(ALLEXE:%=%.exe) -alltests: all - $(MAKE) $(addprefix test_,$(ALLEXE:%.exe=%)) - -depends: $(patsubst %.c, %.d, $(wildcard *.c)) $(patsubst %, %.ld, $(ALLEXE)) - count: wc $(wildcard *.c *.h) $(MAKEFILE_LIST) @@ -57,17 +53,43 @@ clean: rm -f clean $(wildcard *.d *.ld *.log *.o *.test *~ .exec_*) $(call PASS, SUCCESS) +depends: $(patsubst %.c, %.d, $(wildcard *.c)) $(patsubst %, %.ld, $(ALLEXE)) + +gcovs: + $(MAKE) $(addprefix gcov_,$(ALLEXE)) + purge: clean $(call TITLE, "Purging") touch purge - rm -f purge $(ALLEXE) $(shell [ -f .targets ] && { cat .targets | sort | uniq; echo .targets; }) + rm -f purge $(ALLEXE:%=%.exe) $(call PASS, SUCCESS) +valgrinds: + $(MAKE) $(addprefix valgrind_,$(ALLEXE)) + +wipe: purge + $(call TITLE, "Wiping") + touch wipe + rm -f wipe $(wildcard *.gcda *.gcno *.gcov) + $(call PASS, SUCCESS) + +tests: all + $(MAKE) $(addprefix test_,$(ALLEXE)) + ## Main rules include $(wildcard *.d) include $(wildcard *.ld) +gcov_%: + $(MAKE) purge + GCOV="-fprofile-arcs -ftest-coverage -O0" $(MAKE) + $(MAKE) test_$(@:gcov_%=%) + gcov `sed -e 's/\.exe:/.c/;s/\.o/.c/g' $(@:gcov_%=%.ld)` + touch gcov + rm -f gcov $(wildcard *.gcda *.gcno) + $(MAKE) purge + %.test: %.c $(call TITLE, "Building $@") # awk '/\/\* *test:.*\*\// { sub(/^.*\/\* *test: */, ""); sub(/ *\*\/.*$$/, ""); print }' $< > $@ @@ -82,12 +104,12 @@ test_%: %.test %.exe [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \ || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \ done; \ - test "$$RC" -ne 1 + test "$$RC" -ne 1 -valgrind_%: % +valgrind_%: %.exe VALGRIND="valgrind -v --leak-check=full --show-reachable=yes --log-fd=2"; \ export VALGRIND; \ - $(MAKE) test_$< + $(MAKE) $(@:valgrind_%=test_%) %.d: %.c $(call TITLE, "Building $@") @@ -114,7 +136,7 @@ valgrind_%: % ## Phony -.PHONY: clean count purge +.PHONY: all clean count depends gcovs purge tests ## Precious -- 2.30.2