Browse code

Raise SIGTRAP on all assertion failures

Makes it easy to run tests w/ gdb for debugging

John Hawthorn authored on 27/09/2014 21:55:00
Showing 2 changed files

... ...
@@ -5,7 +5,7 @@ CFLAGS+=-Wall -Wextra -g -std=c99 -O3 -pedantic
5 5
 PREFIX?=/usr/local
6 6
 MANDIR?=$(PREFIX)/share/man
7 7
 BINDIR?=$(PREFIX)/bin
8
-VALGRIND?=
8
+DEBUGGER?=
9 9
 
10 10
 INSTALL=install
11 11
 INSTALL_PROGRAM=$(INSTALL)
... ...
@@ -17,7 +17,7 @@ fzytest: fzytest.o match.o choices.o
17 17
 	$(CC) $(CFLAGS) $(CCFLAGS) -o $@ $^
18 18
 
19 19
 test: fzytest
20
-	-$(VALGRIND) ./fzytest
20
+	-$(DEBUGGER) ./fzytest
21 21
 
22 22
 fzy: fzy.o match.o tty.o choices.o
23 23
 	$(CC) $(CFLAGS) $(CCFLAGS) -o $@ $^
... ...
@@ -1,12 +1,13 @@
1 1
 #include <stdio.h>
2 2
 #include <string.h>
3
+#include <signal.h>
3 4
 
4 5
 #include "match.h"
5 6
 #include "choices.h"
6 7
 
7 8
 int testsrun = 0, testsfailed = 0, assertionsrun = 0;
8 9
 
9
-#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;}
10
+#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;}
10 11
 
11 12
 #define assert_streq(a, b) assert(!strcmp(a, b))
12 13
 
... ...
@@ -216,10 +217,18 @@ void summary(){
216 217
 	printf("%i tests, %i assertions, %i failures\n", testsrun, assertionsrun, testsfailed);
217 218
 }
218 219
 
220
+static void ignore_signal(int signum){
221
+	(void) signum;
222
+}
223
+
219 224
 int main(int argc, char *argv[]){
220 225
 	(void) argc;
221 226
 	(void) argv;
222 227
 
228
+	/* We raise sigtrap on all assertion failures.
229
+	 * If we have no debugger running, we should ignore it */
230
+	signal(SIGTRAP, ignore_signal);
231
+
223 232
 	runtest(test_match);
224 233
 	runtest(test_scoring);
225 234
 	runtest(test_positions_1);