Browse code

Replace test "framework" with way less magic

John Hawthorn authored on 27/07/2014 04:09:53
Showing 1 changed files

... ...
@@ -1,14 +1,17 @@
1 1
 #include <stdio.h>
2 2
 #include "fzy.h"
3 3
 
4
-const char *testname;
5 4
 int testsrun = 0, testspassed = 0;
6 5
 
7
-#define TEST(name) int test_##name(){ testname = #name; testsrun++; do
8
-#define ENDTEST while(0); testspassed++; return 0;}
9
-#define assert(x) if(!(x)){fprintf(stderr, "test \"%s\" failed\n   assert(%s) was false\n   at %s:%i\n\n", testname, #x, __FILE__ ,__LINE__);return -1;}
6
+#define assert(x) if(!(x)){fprintf(stderr, "test \"%s\" failed\n   assert(%s) was false\n   at %s:%i\n\n", __func__, #x, __FILE__ ,__LINE__);return -1;}
10 7
 
11
-TEST(match){
8
+void runtest(int (*test)()){
9
+	testsrun++;
10
+	if(!test())
11
+		testspassed++;
12
+}
13
+
14
+int test_match(){
12 15
 	assert(has_match("a", "a"));
13 16
 	assert(has_match("a", "ab"));
14 17
 	assert(has_match("a", "ba"));
... ...
@@ -21,25 +24,31 @@ TEST(match){
21 24
 	/* match when query is empty */
22 25
 	assert(has_match("", ""));
23 26
 	assert(has_match("", "a"));
24
-}ENDTEST
25 27
 
26
-TEST(scoring){
28
+	return 0;
29
+}
30
+
31
+int test_scoring(){
27 32
 	/* App/Models/Order is better than App/MOdels/foo  */
28 33
 	assert(match("amo", "app/models/foo") < match("amo", "app/models/order"));
29 34
 
30 35
 	/* App/MOdels/foo is better than App/M/fOo  */
31 36
 	assert(match("amo", "app/m/foo") < match("amo", "app/models/foo"));
32
-}ENDTEST
33 37
 
34
-TEST(positions_1){
38
+	return 0;
39
+}
40
+
41
+int test_positions_1(){
35 42
 	size_t positions[3];
36 43
 	match_positions("amo", "app/models/foo", positions);
37 44
 	assert(positions[0] == 0);
38 45
 	assert(positions[1] == 4);
39 46
 	assert(positions[2] == 5);
40
-}ENDTEST
41 47
 
42
-TEST(positions_2){
48
+	return 0;
49
+}
50
+
51
+int test_positions_2(){
43 52
 	/*
44 53
 	 * We should prefer matching the 'o' in order, since it's the beginning
45 54
 	 * of a word.
... ...
@@ -49,7 +58,9 @@ TEST(positions_2){
49 58
 	assert(positions[0] == 0);
50 59
 	assert(positions[1] == 4);
51 60
 	assert(positions[2] == 11);
52
-}ENDTEST
61
+
62
+	return 0;
63
+}
53 64
 
54 65
 void summary(){
55 66
 	printf("%i tests run: %i passed  %i failed\n", testsrun, testspassed, testsrun - testspassed);
... ...
@@ -58,10 +69,11 @@ void summary(){
58 69
 int main(int argc, char *argv[]){
59 70
 	(void) argc;
60 71
 	(void) argv;
61
-	test_match();
62
-	test_scoring();
63
-	test_positions_1();
64
-	test_positions_2();
72
+
73
+	runtest(test_match);
74
+	runtest(test_scoring);
75
+	runtest(test_positions_1);
76
+	runtest(test_positions_2);
65 77
 
66 78
 	summary();
67 79