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