From 78bf67c0d30cbd121a34c9fee5def678f0c027e7 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Sat, 27 Sep 2014 14:55:00 -0700 Subject: Raise SIGTRAP on all assertion failures Makes it easy to run tests w/ gdb for debugging --- Makefile | 4 ++-- fzytest.c | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 7b3cead..4854fca 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ CFLAGS+=-Wall -Wextra -g -std=c99 -O3 -pedantic PREFIX?=/usr/local MANDIR?=$(PREFIX)/share/man BINDIR?=$(PREFIX)/bin -VALGRIND?= +DEBUGGER?= INSTALL=install INSTALL_PROGRAM=$(INSTALL) @@ -17,7 +17,7 @@ fzytest: fzytest.o match.o choices.o $(CC) $(CFLAGS) $(CCFLAGS) -o $@ $^ test: fzytest - -$(VALGRIND) ./fzytest + -$(DEBUGGER) ./fzytest fzy: fzy.o match.o tty.o choices.o $(CC) $(CFLAGS) $(CCFLAGS) -o $@ $^ diff --git a/fzytest.c b/fzytest.c index 2c28fb5..ec81170 100644 --- a/fzytest.c +++ b/fzytest.c @@ -1,12 +1,13 @@ #include #include +#include #include "match.h" #include "choices.h" int testsrun = 0, testsfailed = 0, assertionsrun = 0; -#define assert(x) if(++assertionsrun && !(x)){fprintf(stderr, "test \"%s\" failed\n assert(%s) was false\n at %s:%i\n\n", __func__, #x, __FILE__ ,__LINE__);testsfailed++;return;} +#define assert(x) if(++assertionsrun && !(x)){fprintf(stderr, "test \"%s\" failed\n assert(%s) was false\n at %s:%i\n\n", __func__, #x, __FILE__ ,__LINE__);raise(SIGTRAP);testsfailed++;return;} #define assert_streq(a, b) assert(!strcmp(a, b)) @@ -216,10 +217,18 @@ void summary(){ printf("%i tests, %i assertions, %i failures\n", testsrun, assertionsrun, testsfailed); } +static void ignore_signal(int signum){ + (void) signum; +} + int main(int argc, char *argv[]){ (void) argc; (void) argv; + /* We raise sigtrap on all assertion failures. + * If we have no debugger running, we should ignore it */ + signal(SIGTRAP, ignore_signal); + runtest(test_match); runtest(test_scoring); runtest(test_positions_1); -- cgit v1.2.3