Browse code

Move fzytest into test/ directory

John Hawthorn authored on 21/05/2016 22:00:39
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,265 +0,0 @@
1
-#include <stdio.h>
2
-#include <string.h>
3
-#include <signal.h>
4
-
5
-#include "match.h"
6
-#include "choices.h"
7
-
8
-int testsrun = 0, testsfailed = 0, assertionsrun = 0;
9
-
10
-#define assert(x)                                                                                  \
11
-	if (++assertionsrun && !(x)) {                                                             \
12
-		fprintf(stderr, "test \"%s\" failed\n   assert(%s) was false\n   at %s:%i\n\n",    \
13
-			__func__, #x, __FILE__, __LINE__);                                         \
14
-		raise(SIGTRAP);                                                                    \
15
-		testsfailed++;                                                                     \
16
-		return;                                                                            \
17
-	}
18
-
19
-#define assert_streq(a, b) assert(!strcmp(a, b))
20
-
21
-void runtest(void (*test)()) {
22
-	testsrun++;
23
-	test();
24
-}
25
-
26
-void test_match() {
27
-	assert(has_match("a", "a"));
28
-	assert(has_match("a", "ab"));
29
-	assert(has_match("a", "ba"));
30
-	assert(has_match("abc", "a|b|c"));
31
-
32
-	/* non-match */
33
-	assert(!has_match("a", ""));
34
-	assert(!has_match("a", "b"));
35
-	assert(!has_match("ass", "tags"));
36
-
37
-	/* match when query is empty */
38
-	assert(has_match("", ""));
39
-	assert(has_match("", "a"));
40
-}
41
-
42
-void test_scoring() {
43
-	/* App/Models/Order is better than App/MOdels/zRder  */
44
-	assert(match("amor", "app/models/order") > match("amor", "app/models/zrder"));
45
-
46
-	/* App/MOdels/foo is better than App/M/fOo  */
47
-	assert(match("amo", "app/m/foo") < match("amo", "app/models/foo"));
48
-
49
-	/* GEMFIle.Lock < GEMFILe  */
50
-	assert(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile"));
51
-
52
-	/* GEMFIle.Lock < GEMFILe  */
53
-	assert(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile"));
54
-
55
-	/* Prefer shorter matches */
56
-	assert(match("abce", "abcdef") > match("abce", "abc de"));
57
-
58
-	/* Prefer shorter candidates */
59
-	assert(match("test", "tests") > match("test", "testing"));
60
-
61
-	/* Scores first letter highly */
62
-	assert(match("test", "testing") > match("test", "/testing"));
63
-
64
-	/* Prefer shorter matches */
65
-	assert(match("abc", "    a b c ") > match("abc", " a  b  c "));
66
-	assert(match("abc", " a b c    ") > match("abc", " a  b  c "));
67
-}
68
-
69
-void test_positions_1() {
70
-	size_t positions[3];
71
-	match_positions("amo", "app/models/foo", positions);
72
-	assert(positions[0] == 0);
73
-	assert(positions[1] == 4);
74
-	assert(positions[2] == 5);
75
-}
76
-
77
-void test_positions_2() {
78
-	/*
79
-	 * We should prefer matching the 'o' in order, since it's the beginning
80
-	 * of a word.
81
-	 */
82
-	size_t positions[4];
83
-	match_positions("amor", "app/models/order", positions);
84
-	assert(positions[0] == 0);
85
-	assert(positions[1] == 4);
86
-	assert(positions[2] == 11);
87
-}
88
-
89
-void test_positions_3() {
90
-	size_t positions[2];
91
-	match_positions("as", "tags", positions);
92
-	assert(positions[0] == 1);
93
-	assert(positions[1] == 3);
94
-}
95
-
96
-void test_positions_4() {
97
-	size_t positions[2];
98
-	match_positions("as", "examples.txt", positions);
99
-	assert(positions[0] == 2);
100
-	assert(positions[1] == 7);
101
-}
102
-
103
-void test_positions_5() {
104
-	size_t positions[3];
105
-	match_positions("abc", "a/a/b/c/c", positions);
106
-	assert(positions[0] == 2);
107
-	assert(positions[1] == 4);
108
-	assert(positions[2] == 6);
109
-}
110
-
111
-void test_positions_exact() {
112
-	size_t positions[3];
113
-	match_positions("foo", "foo", positions);
114
-	assert(positions[0] == 0);
115
-	assert(positions[1] == 1);
116
-	assert(positions[2] == 2);
117
-}
118
-
119
-void test_choices_empty() {
120
-	choices_t choices;
121
-	choices_init(&choices);
122
-	assert(choices.size == 0);
123
-	assert(choices.available == 0);
124
-	assert(choices.selection == 0);
125
-
126
-	choices_prev(&choices);
127
-	assert(choices.selection == 0);
128
-
129
-	choices_next(&choices);
130
-	assert(choices.selection == 0);
131
-
132
-	choices_destroy(&choices);
133
-}
134
-
135
-void test_choices_1() {
136
-	choices_t choices;
137
-	choices_init(&choices);
138
-	choices_add(&choices, "tags");
139
-
140
-	choices_search(&choices, "");
141
-	assert(choices.available == 1);
142
-	assert(choices.selection == 0);
143
-
144
-	choices_search(&choices, "t");
145
-	assert(choices.available == 1);
146
-	assert(choices.selection == 0);
147
-
148
-	choices_prev(&choices);
149
-	assert(choices.selection == 0);
150
-
151
-	choices_next(&choices);
152
-	assert(choices.selection == 0);
153
-
154
-	assert(!strcmp(choices_get(&choices, 0), "tags"));
155
-	assert(choices_get(&choices, 1) == NULL);
156
-
157
-	choices_destroy(&choices);
158
-}
159
-
160
-void test_choices_2() {
161
-	choices_t choices;
162
-	choices_init(&choices);
163
-	choices_add(&choices, "tags");
164
-	choices_add(&choices, "test");
165
-
166
-	/* Empty search */
167
-	choices_search(&choices, "");
168
-	assert(choices.selection == 0);
169
-	assert(choices.available == 2);
170
-	assert_streq(choices_get(&choices, 0), "tags");
171
-	assert_streq(choices_get(&choices, 1), "test");
172
-
173
-	choices_next(&choices);
174
-	assert(choices.selection == 1);
175
-	choices_next(&choices);
176
-	assert(choices.selection == 0);
177
-
178
-	choices_prev(&choices);
179
-	assert(choices.selection == 1);
180
-	choices_prev(&choices);
181
-	assert(choices.selection == 0);
182
-
183
-	/* Filtered search */
184
-	choices_search(&choices, "te");
185
-	assert(choices.available == 1);
186
-	assert(choices.selection == 0);
187
-	assert_streq(choices_get(&choices, 0), "test");
188
-
189
-	choices_next(&choices);
190
-	assert(choices.selection == 0);
191
-
192
-	choices_prev(&choices);
193
-	assert(choices.selection == 0);
194
-
195
-	/* No results */
196
-	choices_search(&choices, "foobar");
197
-	assert(choices.available == 0);
198
-	assert(choices.selection == 0);
199
-
200
-	/* Different order due to scoring */
201
-	choices_search(&choices, "ts");
202
-	assert(choices.available == 2);
203
-	assert(choices.selection == 0);
204
-	assert_streq(choices_get(&choices, 0), "test");
205
-	assert_streq(choices_get(&choices, 1), "tags");
206
-
207
-	choices_destroy(&choices);
208
-}
209
-
210
-void test_choices_without_search() {
211
-	/* Before a search is run, it should return no results */
212
-
213
-	choices_t choices;
214
-	choices_init(&choices);
215
-
216
-	assert(choices.available == 0);
217
-	assert(choices.selection == 0);
218
-	assert(choices.size == 0);
219
-	assert(choices_get(&choices, 0) == NULL);
220
-
221
-	choices_add(&choices, "test");
222
-
223
-	assert(choices.available == 0);
224
-	assert(choices.selection == 0);
225
-	assert(choices.size == 1);
226
-	assert(choices_get(&choices, 0) == NULL);
227
-
228
-	choices_destroy(&choices);
229
-}
230
-
231
-void summary() {
232
-	printf("%i tests, %i assertions, %i failures\n", testsrun, assertionsrun, testsfailed);
233
-}
234
-
235
-static void ignore_signal(int signum) {
236
-	(void)signum;
237
-}
238
-
239
-int main(int argc, char *argv[]) {
240
-	(void)argc;
241
-	(void)argv;
242
-
243
-	/* We raise sigtrap on all assertion failures.
244
-	 * If we have no debugger running, we should ignore it */
245
-	signal(SIGTRAP, ignore_signal);
246
-
247
-	runtest(test_match);
248
-	runtest(test_scoring);
249
-	runtest(test_positions_1);
250
-	runtest(test_positions_2);
251
-	runtest(test_positions_3);
252
-	runtest(test_positions_4);
253
-	runtest(test_positions_5);
254
-	runtest(test_positions_exact);
255
-
256
-	runtest(test_choices_empty);
257
-	runtest(test_choices_1);
258
-	runtest(test_choices_2);
259
-	runtest(test_choices_without_search);
260
-
261
-	summary();
262
-
263
-	/* exit 0 if all tests pass */
264
-	return !!testsfailed;
265
-}
Browse code

Move sources into src directory

John Hawthorn authored on 21/05/2016 21:56:03
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,265 @@
1
+#include <stdio.h>
2
+#include <string.h>
3
+#include <signal.h>
4
+
5
+#include "match.h"
6
+#include "choices.h"
7
+
8
+int testsrun = 0, testsfailed = 0, assertionsrun = 0;
9
+
10
+#define assert(x)                                                                                  \
11
+	if (++assertionsrun && !(x)) {                                                             \
12
+		fprintf(stderr, "test \"%s\" failed\n   assert(%s) was false\n   at %s:%i\n\n",    \
13
+			__func__, #x, __FILE__, __LINE__);                                         \
14
+		raise(SIGTRAP);                                                                    \
15
+		testsfailed++;                                                                     \
16
+		return;                                                                            \
17
+	}
18
+
19
+#define assert_streq(a, b) assert(!strcmp(a, b))
20
+
21
+void runtest(void (*test)()) {
22
+	testsrun++;
23
+	test();
24
+}
25
+
26
+void test_match() {
27
+	assert(has_match("a", "a"));
28
+	assert(has_match("a", "ab"));
29
+	assert(has_match("a", "ba"));
30
+	assert(has_match("abc", "a|b|c"));
31
+
32
+	/* non-match */
33
+	assert(!has_match("a", ""));
34
+	assert(!has_match("a", "b"));
35
+	assert(!has_match("ass", "tags"));
36
+
37
+	/* match when query is empty */
38
+	assert(has_match("", ""));
39
+	assert(has_match("", "a"));
40
+}
41
+
42
+void test_scoring() {
43
+	/* App/Models/Order is better than App/MOdels/zRder  */
44
+	assert(match("amor", "app/models/order") > match("amor", "app/models/zrder"));
45
+
46
+	/* App/MOdels/foo is better than App/M/fOo  */
47
+	assert(match("amo", "app/m/foo") < match("amo", "app/models/foo"));
48
+
49
+	/* GEMFIle.Lock < GEMFILe  */
50
+	assert(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile"));
51
+
52
+	/* GEMFIle.Lock < GEMFILe  */
53
+	assert(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile"));
54
+
55
+	/* Prefer shorter matches */
56
+	assert(match("abce", "abcdef") > match("abce", "abc de"));
57
+
58
+	/* Prefer shorter candidates */
59
+	assert(match("test", "tests") > match("test", "testing"));
60
+
61
+	/* Scores first letter highly */
62
+	assert(match("test", "testing") > match("test", "/testing"));
63
+
64
+	/* Prefer shorter matches */
65
+	assert(match("abc", "    a b c ") > match("abc", " a  b  c "));
66
+	assert(match("abc", " a b c    ") > match("abc", " a  b  c "));
67
+}
68
+
69
+void test_positions_1() {
70
+	size_t positions[3];
71
+	match_positions("amo", "app/models/foo", positions);
72
+	assert(positions[0] == 0);
73
+	assert(positions[1] == 4);
74
+	assert(positions[2] == 5);
75
+}
76
+
77
+void test_positions_2() {
78
+	/*
79
+	 * We should prefer matching the 'o' in order, since it's the beginning
80
+	 * of a word.
81
+	 */
82
+	size_t positions[4];
83
+	match_positions("amor", "app/models/order", positions);
84
+	assert(positions[0] == 0);
85
+	assert(positions[1] == 4);
86
+	assert(positions[2] == 11);
87
+}
88
+
89
+void test_positions_3() {
90
+	size_t positions[2];
91
+	match_positions("as", "tags", positions);
92
+	assert(positions[0] == 1);
93
+	assert(positions[1] == 3);
94
+}
95
+
96
+void test_positions_4() {
97
+	size_t positions[2];
98
+	match_positions("as", "examples.txt", positions);
99
+	assert(positions[0] == 2);
100
+	assert(positions[1] == 7);
101
+}
102
+
103
+void test_positions_5() {
104
+	size_t positions[3];
105
+	match_positions("abc", "a/a/b/c/c", positions);
106
+	assert(positions[0] == 2);
107
+	assert(positions[1] == 4);
108
+	assert(positions[2] == 6);
109
+}
110
+
111
+void test_positions_exact() {
112
+	size_t positions[3];
113
+	match_positions("foo", "foo", positions);
114
+	assert(positions[0] == 0);
115
+	assert(positions[1] == 1);
116
+	assert(positions[2] == 2);
117
+}
118
+
119
+void test_choices_empty() {
120
+	choices_t choices;
121
+	choices_init(&choices);
122
+	assert(choices.size == 0);
123
+	assert(choices.available == 0);
124
+	assert(choices.selection == 0);
125
+
126
+	choices_prev(&choices);
127
+	assert(choices.selection == 0);
128
+
129
+	choices_next(&choices);
130
+	assert(choices.selection == 0);
131
+
132
+	choices_destroy(&choices);
133
+}
134
+
135
+void test_choices_1() {
136
+	choices_t choices;
137
+	choices_init(&choices);
138
+	choices_add(&choices, "tags");
139
+
140
+	choices_search(&choices, "");
141
+	assert(choices.available == 1);
142
+	assert(choices.selection == 0);
143
+
144
+	choices_search(&choices, "t");
145
+	assert(choices.available == 1);
146
+	assert(choices.selection == 0);
147
+
148
+	choices_prev(&choices);
149
+	assert(choices.selection == 0);
150
+
151
+	choices_next(&choices);
152
+	assert(choices.selection == 0);
153
+
154
+	assert(!strcmp(choices_get(&choices, 0), "tags"));
155
+	assert(choices_get(&choices, 1) == NULL);
156
+
157
+	choices_destroy(&choices);
158
+}
159
+
160
+void test_choices_2() {
161
+	choices_t choices;
162
+	choices_init(&choices);
163
+	choices_add(&choices, "tags");
164
+	choices_add(&choices, "test");
165
+
166
+	/* Empty search */
167
+	choices_search(&choices, "");
168
+	assert(choices.selection == 0);
169
+	assert(choices.available == 2);
170
+	assert_streq(choices_get(&choices, 0), "tags");
171
+	assert_streq(choices_get(&choices, 1), "test");
172
+
173
+	choices_next(&choices);
174
+	assert(choices.selection == 1);
175
+	choices_next(&choices);
176
+	assert(choices.selection == 0);
177
+
178
+	choices_prev(&choices);
179
+	assert(choices.selection == 1);
180
+	choices_prev(&choices);
181
+	assert(choices.selection == 0);
182
+
183
+	/* Filtered search */
184
+	choices_search(&choices, "te");
185
+	assert(choices.available == 1);
186
+	assert(choices.selection == 0);
187
+	assert_streq(choices_get(&choices, 0), "test");
188
+
189
+	choices_next(&choices);
190
+	assert(choices.selection == 0);
191
+
192
+	choices_prev(&choices);
193
+	assert(choices.selection == 0);
194
+
195
+	/* No results */
196
+	choices_search(&choices, "foobar");
197
+	assert(choices.available == 0);
198
+	assert(choices.selection == 0);
199
+
200
+	/* Different order due to scoring */
201
+	choices_search(&choices, "ts");
202
+	assert(choices.available == 2);
203
+	assert(choices.selection == 0);
204
+	assert_streq(choices_get(&choices, 0), "test");
205
+	assert_streq(choices_get(&choices, 1), "tags");
206
+
207
+	choices_destroy(&choices);
208
+}
209
+
210
+void test_choices_without_search() {
211
+	/* Before a search is run, it should return no results */
212
+
213
+	choices_t choices;
214
+	choices_init(&choices);
215
+
216
+	assert(choices.available == 0);
217
+	assert(choices.selection == 0);
218
+	assert(choices.size == 0);
219
+	assert(choices_get(&choices, 0) == NULL);
220
+
221
+	choices_add(&choices, "test");
222
+
223
+	assert(choices.available == 0);
224
+	assert(choices.selection == 0);
225
+	assert(choices.size == 1);
226
+	assert(choices_get(&choices, 0) == NULL);
227
+
228
+	choices_destroy(&choices);
229
+}
230
+
231
+void summary() {
232
+	printf("%i tests, %i assertions, %i failures\n", testsrun, assertionsrun, testsfailed);
233
+}
234
+
235
+static void ignore_signal(int signum) {
236
+	(void)signum;
237
+}
238
+
239
+int main(int argc, char *argv[]) {
240
+	(void)argc;
241
+	(void)argv;
242
+
243
+	/* We raise sigtrap on all assertion failures.
244
+	 * If we have no debugger running, we should ignore it */
245
+	signal(SIGTRAP, ignore_signal);
246
+
247
+	runtest(test_match);
248
+	runtest(test_scoring);
249
+	runtest(test_positions_1);
250
+	runtest(test_positions_2);
251
+	runtest(test_positions_3);
252
+	runtest(test_positions_4);
253
+	runtest(test_positions_5);
254
+	runtest(test_positions_exact);
255
+
256
+	runtest(test_choices_empty);
257
+	runtest(test_choices_1);
258
+	runtest(test_choices_2);
259
+	runtest(test_choices_without_search);
260
+
261
+	summary();
262
+
263
+	/* exit 0 if all tests pass */
264
+	return !!testsfailed;
265
+}