Browse code

Switch test framework to greatest

John Hawthorn authored on 03/04/2017 05:32:33
Showing 1 changed files

... ...
@@ -9,200 +9,206 @@
9 9
 #include "choices.h"
10 10
 #include "options.h"
11 11
 
12
-int testsrun = 0, testsfailed = 0, assertionsrun = 0;
13
-
14
-#define assert(x)                                                                                  \
15
-	if (++assertionsrun && !(x)) {                                                             \
16
-		fprintf(stderr, "test \"%s\" failed\n   assert(%s) was false\n   at %s:%i\n\n",    \
17
-			__func__, #x, __FILE__, __LINE__);                                         \
18
-		raise(SIGTRAP);                                                                    \
19
-		testsfailed++;                                                                     \
20
-		return;                                                                            \
21
-	}
22
-
23
-#define assert_streq(a, b) assert(!strcmp(a, b))
12
+#include "greatest/greatest.h"
24 13
 
25 14
 static options_t default_options;
26 15
 
27
-void runtest(void (*test)()) {
28
-	testsrun++;
29
-	test();
30
-}
31
-
32
-void test_match() {
33
-	assert(has_match("a", "a"));
34
-	assert(has_match("a", "ab"));
35
-	assert(has_match("a", "ba"));
36
-	assert(has_match("abc", "a|b|c"));
16
+TEST test_match() {
17
+	ASSERT(has_match("a", "a"));
18
+	ASSERT(has_match("a", "ab"));
19
+	ASSERT(has_match("a", "ba"));
20
+	ASSERT(has_match("abc", "a|b|c"));
37 21
 
38 22
 	/* non-match */
39
-	assert(!has_match("a", ""));
40
-	assert(!has_match("a", "b"));
41
-	assert(!has_match("ass", "tags"));
23
+	ASSERT(!has_match("a", ""));
24
+	ASSERT(!has_match("a", "b"));
25
+	ASSERT(!has_match("ass", "tags"));
42 26
 
43 27
 	/* match when query is empty */
44
-	assert(has_match("", ""));
45
-	assert(has_match("", "a"));
28
+	ASSERT(has_match("", ""));
29
+	ASSERT(has_match("", "a"));
30
+
31
+	PASS();
46 32
 }
47 33
 
48
-void test_relative_scores() {
34
+TEST test_relative_scores() {
49 35
 	/* App/Models/Order is better than App/MOdels/zRder  */
50
-	assert(match("amor", "app/models/order") > match("amor", "app/models/zrder"));
36
+	ASSERT(match("amor", "app/models/order") > match("amor", "app/models/zrder"));
51 37
 
52 38
 	/* App/MOdels/foo is better than App/M/fOo  */
53
-	assert(match("amo", "app/m/foo") < match("amo", "app/models/foo"));
39
+	ASSERT(match("amo", "app/m/foo") < match("amo", "app/models/foo"));
54 40
 
55 41
 	/* GEMFIle.Lock < GEMFILe  */
56
-	assert(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile"));
42
+	ASSERT(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile"));
57 43
 
58 44
 	/* GEMFIle.Lock < GEMFILe  */
59
-	assert(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile"));
45
+	ASSERT(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile"));
60 46
 
61 47
 	/* Prefer shorter matches */
62
-	assert(match("abce", "abcdef") > match("abce", "abc de"));
48
+	ASSERT(match("abce", "abcdef") > match("abce", "abc de"));
63 49
 
64 50
 	/* Prefer shorter candidates */
65
-	assert(match("test", "tests") > match("test", "testing"));
51
+	ASSERT(match("test", "tests") > match("test", "testing"));
66 52
 
67 53
 	/* Scores first letter highly */
68
-	assert(match("test", "testing") > match("test", "/testing"));
54
+	ASSERT(match("test", "testing") > match("test", "/testing"));
69 55
 
70 56
 	/* Prefer shorter matches */
71
-	assert(match("abc", "    a b c ") > match("abc", " a  b  c "));
72
-	assert(match("abc", " a b c    ") > match("abc", " a  b  c "));
57
+	ASSERT(match("abc", "    a b c ") > match("abc", " a  b  c "));
58
+	ASSERT(match("abc", " a b c    ") > match("abc", " a  b  c "));
59
+
60
+	PASS();
73 61
 }
74 62
 
75
-void test_exact_scores() {
63
+TEST test_exact_scores() {
76 64
 	/* Exact match is SCORE_MAX */
77
-	assert(match("abc", "abc") == SCORE_MAX);
78
-	assert(match("aBc", "abC") == SCORE_MAX);
65
+	ASSERT(match("abc", "abc") == SCORE_MAX);
66
+	ASSERT(match("aBc", "abC") == SCORE_MAX);
79 67
 
80 68
 	/* Empty query always results in SCORE_MIN */
81
-	assert(match("", "") == SCORE_MIN);
82
-	assert(match("", "a") == SCORE_MIN);
83
-	assert(match("", "bb") == SCORE_MIN);
69
+	ASSERT(match("", "") == SCORE_MIN);
70
+	ASSERT(match("", "a") == SCORE_MIN);
71
+	ASSERT(match("", "bb") == SCORE_MIN);
84 72
 
85 73
 	/* Gaps */
86
-	assert(match("a", "*a") == SCORE_GAP_LEADING);
87
-	assert(match("a", "*ba") == SCORE_GAP_LEADING*2);
88
-	assert(match("a", "**a*") == SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING);
89
-	assert(match("a", "**a**") == SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING*2);
90
-	assert(match("aa", "**aa**") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CONSECUTIVE + SCORE_GAP_TRAILING*2);
91
-	assert(match("aa", "**a*a**") == SCORE_GAP_LEADING + SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_GAP_TRAILING + SCORE_GAP_TRAILING);
74
+	ASSERT(match("a", "*a") == SCORE_GAP_LEADING);
75
+	ASSERT(match("a", "*ba") == SCORE_GAP_LEADING*2);
76
+	ASSERT(match("a", "**a*") == SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING);
77
+	ASSERT(match("a", "**a**") == SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING*2);
78
+	ASSERT(match("aa", "**aa**") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CONSECUTIVE + SCORE_GAP_TRAILING*2);
79
+	ASSERT(match("aa", "**a*a**") == SCORE_GAP_LEADING + SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_GAP_TRAILING + SCORE_GAP_TRAILING);
92 80
 
93 81
 	/* Consecutive */
94
-	assert(match("aa", "*aa") == SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE);
95
-	assert(match("aaa", "*aaa") == SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE*2);
96
-	assert(match("aaa", "*a*aa") == SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_CONSECUTIVE);
82
+	ASSERT(match("aa", "*aa") == SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE);
83
+	ASSERT(match("aaa", "*aaa") == SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE*2);
84
+	ASSERT(match("aaa", "*a*aa") == SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_CONSECUTIVE);
97 85
 
98 86
 	/* Slash */
99
-	assert(match("a", "/a") == SCORE_GAP_LEADING + SCORE_MATCH_SLASH);
100
-	assert(match("a", "*/a") == SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH);
101
-	assert(match("aa", "a/aa") == SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH + SCORE_MATCH_CONSECUTIVE);
87
+	ASSERT(match("a", "/a") == SCORE_GAP_LEADING + SCORE_MATCH_SLASH);
88
+	ASSERT(match("a", "*/a") == SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH);
89
+	ASSERT(match("aa", "a/aa") == SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH + SCORE_MATCH_CONSECUTIVE);
102 90
 
103 91
 	/* Capital */
104
-	assert(match("a", "bA") == SCORE_GAP_LEADING + SCORE_MATCH_CAPITAL);
105
-	assert(match("a", "baA") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL);
106
-	assert(match("aa", "baAa") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL + SCORE_MATCH_CONSECUTIVE);
92
+	ASSERT(match("a", "bA") == SCORE_GAP_LEADING + SCORE_MATCH_CAPITAL);
93
+	ASSERT(match("a", "baA") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL);
94
+	ASSERT(match("aa", "baAa") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL + SCORE_MATCH_CONSECUTIVE);
107 95
 
108 96
 	/* Dot */
109
-	assert(match("a", ".a") == SCORE_GAP_LEADING + SCORE_MATCH_DOT);
110
-	assert(match("a", "*a.a") == SCORE_GAP_LEADING*3 + SCORE_MATCH_DOT);
111
-	assert(match("a", "*a.a") == SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_DOT);
97
+	ASSERT(match("a", ".a") == SCORE_GAP_LEADING + SCORE_MATCH_DOT);
98
+	ASSERT(match("a", "*a.a") == SCORE_GAP_LEADING*3 + SCORE_MATCH_DOT);
99
+	ASSERT(match("a", "*a.a") == SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_DOT);
100
+
101
+	PASS();
112 102
 }
113 103
 
114
-void test_positions_1() {
104
+TEST test_positions_1() {
115 105
 	size_t positions[3];
116 106
 	match_positions("amo", "app/models/foo", positions);
117
-	assert(positions[0] == 0);
118
-	assert(positions[1] == 4);
119
-	assert(positions[2] == 5);
107
+	ASSERT(positions[0] == 0);
108
+	ASSERT(positions[1] == 4);
109
+	ASSERT(positions[2] == 5);
110
+
111
+	PASS();
120 112
 }
121 113
 
122
-void test_positions_2() {
114
+TEST test_positions_2() {
123 115
 	/*
124 116
 	 * We should prefer matching the 'o' in order, since it's the beginning
125 117
 	 * of a word.
126 118
 	 */
127 119
 	size_t positions[4];
128 120
 	match_positions("amor", "app/models/order", positions);
129
-	assert(positions[0] == 0);
130
-	assert(positions[1] == 4);
131
-	assert(positions[2] == 11);
121
+	ASSERT(positions[0] == 0);
122
+	ASSERT(positions[1] == 4);
123
+	ASSERT(positions[2] == 11);
124
+
125
+	PASS();
132 126
 }
133 127
 
134
-void test_positions_3() {
128
+TEST test_positions_3() {
135 129
 	size_t positions[2];
136 130
 	match_positions("as", "tags", positions);
137
-	assert(positions[0] == 1);
138
-	assert(positions[1] == 3);
131
+	ASSERT(positions[0] == 1);
132
+	ASSERT(positions[1] == 3);
133
+
134
+	PASS();
139 135
 }
140 136
 
141
-void test_positions_4() {
137
+TEST test_positions_4() {
142 138
 	size_t positions[2];
143 139
 	match_positions("as", "examples.txt", positions);
144
-	assert(positions[0] == 2);
145
-	assert(positions[1] == 7);
140
+	ASSERT(positions[0] == 2);
141
+	ASSERT(positions[1] == 7);
142
+
143
+	PASS();
146 144
 }
147 145
 
148
-void test_positions_5() {
146
+TEST test_positions_5() {
149 147
 	size_t positions[3];
150 148
 	match_positions("abc", "a/a/b/c/c", positions);
151
-	assert(positions[0] == 2);
152
-	assert(positions[1] == 4);
153
-	assert(positions[2] == 6);
149
+	ASSERT(positions[0] == 2);
150
+	ASSERT(positions[1] == 4);
151
+	ASSERT(positions[2] == 6);
152
+
153
+	PASS();
154 154
 }
155 155
 
156
-void test_positions_exact() {
156
+TEST test_positions_exact() {
157 157
 	size_t positions[3];
158 158
 	match_positions("foo", "foo", positions);
159
-	assert(positions[0] == 0);
160
-	assert(positions[1] == 1);
161
-	assert(positions[2] == 2);
159
+	ASSERT(positions[0] == 0);
160
+	ASSERT(positions[1] == 1);
161
+	ASSERT(positions[2] == 2);
162
+
163
+	PASS();
162 164
 }
163 165
 
164
-void test_choices_empty() {
166
+TEST test_choices_empty() {
165 167
 	choices_t choices;
166 168
 	choices_init(&choices, &default_options);
167
-	assert(choices.size == 0);
168
-	assert(choices.available == 0);
169
-	assert(choices.selection == 0);
169
+	ASSERT(choices.size == 0);
170
+	ASSERT(choices.available == 0);
171
+	ASSERT(choices.selection == 0);
170 172
 
171 173
 	choices_prev(&choices);
172
-	assert(choices.selection == 0);
174
+	ASSERT(choices.selection == 0);
173 175
 
174 176
 	choices_next(&choices);
175
-	assert(choices.selection == 0);
177
+	ASSERT(choices.selection == 0);
176 178
 
177 179
 	choices_destroy(&choices);
180
+
181
+	PASS();
178 182
 }
179 183
 
180
-void test_choices_1() {
184
+TEST test_choices_1() {
181 185
 	choices_t choices;
182 186
 	choices_init(&choices, &default_options);
183 187
 	choices_add(&choices, "tags");
184 188
 
185 189
 	choices_search(&choices, "");
186
-	assert(choices.available == 1);
187
-	assert(choices.selection == 0);
190
+	ASSERT(choices.available == 1);
191
+	ASSERT(choices.selection == 0);
188 192
 
189 193
 	choices_search(&choices, "t");
190
-	assert(choices.available == 1);
191
-	assert(choices.selection == 0);
194
+	ASSERT(choices.available == 1);
195
+	ASSERT(choices.selection == 0);
192 196
 
193 197
 	choices_prev(&choices);
194
-	assert(choices.selection == 0);
198
+	ASSERT(choices.selection == 0);
195 199
 
196 200
 	choices_next(&choices);
197
-	assert(choices.selection == 0);
201
+	ASSERT(choices.selection == 0);
198 202
 
199
-	assert(!strcmp(choices_get(&choices, 0), "tags"));
200
-	assert(choices_get(&choices, 1) == NULL);
203
+	ASSERT(!strcmp(choices_get(&choices, 0), "tags"));
204
+	ASSERT(choices_get(&choices, 1) == NULL);
201 205
 
202 206
 	choices_destroy(&choices);
207
+
208
+	PASS();
203 209
 }
204 210
 
205
-void test_choices_2() {
211
+TEST test_choices_2() {
206 212
 	choices_t choices;
207 213
 	choices_init(&choices, &default_options);
208 214
 	choices_add(&choices, "tags");
... ...
@@ -210,71 +216,73 @@ void test_choices_2() {
210 216
 
211 217
 	/* Empty search */
212 218
 	choices_search(&choices, "");
213
-	assert(choices.selection == 0);
214
-	assert(choices.available == 2);
215
-	assert_streq(choices_get(&choices, 0), "tags");
216
-	assert_streq(choices_get(&choices, 1), "test");
219
+	ASSERT(choices.selection == 0);
220
+	ASSERT(choices.available == 2);
217 221
 
218 222
 	choices_next(&choices);
219
-	assert(choices.selection == 1);
223
+	ASSERT(choices.selection == 1);
220 224
 	choices_next(&choices);
221
-	assert(choices.selection == 0);
225
+	ASSERT(choices.selection == 0);
222 226
 
223 227
 	choices_prev(&choices);
224
-	assert(choices.selection == 1);
228
+	ASSERT(choices.selection == 1);
225 229
 	choices_prev(&choices);
226
-	assert(choices.selection == 0);
230
+	ASSERT(choices.selection == 0);
227 231
 
228 232
 	/* Filtered search */
229 233
 	choices_search(&choices, "te");
230
-	assert(choices.available == 1);
231
-	assert(choices.selection == 0);
232
-	assert_streq(choices_get(&choices, 0), "test");
234
+	ASSERT(choices.available == 1);
235
+	ASSERT(choices.selection == 0);
236
+	ASSERT_STR_EQ(choices_get(&choices, 0), "test");
233 237
 
234 238
 	choices_next(&choices);
235
-	assert(choices.selection == 0);
239
+	ASSERT(choices.selection == 0);
236 240
 
237 241
 	choices_prev(&choices);
238
-	assert(choices.selection == 0);
242
+	ASSERT(choices.selection == 0);
239 243
 
240 244
 	/* No results */
241 245
 	choices_search(&choices, "foobar");
242
-	assert(choices.available == 0);
243
-	assert(choices.selection == 0);
246
+	ASSERT(choices.available == 0);
247
+	ASSERT(choices.selection == 0);
244 248
 
245 249
 	/* Different order due to scoring */
246 250
 	choices_search(&choices, "ts");
247
-	assert(choices.available == 2);
248
-	assert(choices.selection == 0);
249
-	assert_streq(choices_get(&choices, 0), "test");
250
-	assert_streq(choices_get(&choices, 1), "tags");
251
+	ASSERT(choices.available == 2);
252
+	ASSERT(choices.selection == 0);
253
+	ASSERT_STR_EQ(choices_get(&choices, 0), "test");
254
+	ASSERT_STR_EQ(choices_get(&choices, 1), "tags");
251 255
 
252 256
 	choices_destroy(&choices);
257
+
258
+	PASS();
253 259
 }
254 260
 
255
-void test_choices_without_search() {
261
+TEST test_choices_without_search() {
256 262
 	/* Before a search is run, it should return no results */
257 263
 
258 264
 	choices_t choices;
259 265
 	choices_init(&choices, &default_options);
260 266
 
261
-	assert(choices.available == 0);
262
-	assert(choices.selection == 0);
263
-	assert(choices.size == 0);
264
-	assert(choices_get(&choices, 0) == NULL);
267
+	ASSERT(choices.available == 0);
268
+	ASSERT(choices.selection == 0);
269
+	ASSERT(choices.size == 0);
270
+	ASSERT(choices_get(&choices, 0) == NULL);
265 271
 
266 272
 	choices_add(&choices, "test");
267 273
 
268
-	assert(choices.available == 0);
269
-	assert(choices.selection == 0);
270
-	assert(choices.size == 1);
271
-	assert(choices_get(&choices, 0) == NULL);
274
+	ASSERT(choices.available == 0);
275
+	ASSERT(choices.selection == 0);
276
+	ASSERT(choices.size == 1);
277
+	ASSERT(choices_get(&choices, 0) == NULL);
272 278
 
273 279
 	choices_destroy(&choices);
280
+
281
+	PASS();
274 282
 }
275 283
 
276 284
 /* Regression test for segfault */
277
-void test_choices_unicode() {
285
+TEST test_choices_unicode() {
278 286
 	choices_t choices;
279 287
 	choices_init(&choices, &default_options);
280 288
 
... ...
@@ -282,9 +290,10 @@ void test_choices_unicode() {
282 290
 	choices_search(&choices, "e");
283 291
 
284 292
 	choices_destroy(&choices);
293
+	PASS();
285 294
 }
286 295
 
287
-void test_choices_large_input() {
296
+TEST test_choices_large_input() {
288 297
 	choices_t choices;
289 298
 	choices_init(&choices, &default_options);
290 299
 
... ...
@@ -299,54 +308,42 @@ void test_choices_large_input() {
299 308
 	choices_search(&choices, "12");
300 309
 
301 310
 	/* Must match `seq 0 99999 | grep '.*1.*2.*' | wc -l` */
302
-	assert(choices.available == 8146);
311
+	ASSERT(choices.available == 8146);
303 312
 
304
-	assert_streq(choices_get(&choices, 0), "12")
313
+	ASSERT_STR_EQ(choices_get(&choices, 0), "12");
305 314
 
306 315
 	for(int i = 0; i < N; i++) {
307 316
 		free(strings[i]);
308 317
 	}
309 318
 
310 319
 	choices_destroy(&choices);
311
-}
312 320
 
313
-void summary() {
314
-	printf("%i tests, %i assertions, %i failures\n", testsrun, assertionsrun, testsfailed);
321
+	PASS();
315 322
 }
316 323
 
317
-static void ignore_signal(int signum) {
318
-	(void)signum;
319
-}
324
+GREATEST_MAIN_DEFS();
320 325
 
321 326
 int main(int argc, char *argv[]) {
322
-	(void)argc;
323
-	(void)argv;
324
-
325
-	/* We raise sigtrap on all assertion failures.
326
-	 * If we have no debugger running, we should ignore it */
327
-	signal(SIGTRAP, ignore_signal);
327
+	GREATEST_MAIN_BEGIN();
328 328
 
329 329
 	options_init(&default_options);
330 330
 
331
-	runtest(test_match);
332
-	runtest(test_relative_scores);
333
-	runtest(test_exact_scores);
334
-	runtest(test_positions_1);
335
-	runtest(test_positions_2);
336
-	runtest(test_positions_3);
337
-	runtest(test_positions_4);
338
-	runtest(test_positions_5);
339
-	runtest(test_positions_exact);
340
-
341
-	runtest(test_choices_empty);
342
-	runtest(test_choices_1);
343
-	runtest(test_choices_2);
344
-	runtest(test_choices_without_search);
345
-	runtest(test_choices_unicode);
346
-	runtest(test_choices_large_input);
347
-
348
-	summary();
349
-
350
-	/* exit 0 if all tests pass */
351
-	return !!testsfailed;
331
+	RUN_TEST(test_match);
332
+	RUN_TEST(test_relative_scores);
333
+	RUN_TEST(test_exact_scores);
334
+	RUN_TEST(test_positions_1);
335
+	RUN_TEST(test_positions_2);
336
+	RUN_TEST(test_positions_3);
337
+	RUN_TEST(test_positions_4);
338
+	RUN_TEST(test_positions_5);
339
+	RUN_TEST(test_positions_exact);
340
+
341
+	RUN_TEST(test_choices_empty);
342
+	RUN_TEST(test_choices_1);
343
+	RUN_TEST(test_choices_2);
344
+	RUN_TEST(test_choices_without_search);
345
+	RUN_TEST(test_choices_unicode);
346
+	RUN_TEST(test_choices_large_input);
347
+
348
+	GREATEST_MAIN_END();
352 349
 }