Preparing for some changes to the match method
| ... | ... |
@@ -2,6 +2,7 @@ |
| 2 | 2 |
#include <string.h> |
| 3 | 3 |
#include <signal.h> |
| 4 | 4 |
|
| 5 |
+#include "../config.h" |
|
| 5 | 6 |
#include "match.h" |
| 6 | 7 |
#include "choices.h" |
| 7 | 8 |
|
| ... | ... |
@@ -39,7 +40,7 @@ void test_match() {
|
| 39 | 40 |
assert(has_match("", "a"));
|
| 40 | 41 |
} |
| 41 | 42 |
|
| 42 |
-void test_scoring() {
|
|
| 43 |
+void test_relative_scores() {
|
|
| 43 | 44 |
/* App/Models/Order is better than App/MOdels/zRder */ |
| 44 | 45 |
assert(match("amor", "app/models/order") > match("amor", "app/models/zrder"));
|
| 45 | 46 |
|
| ... | ... |
@@ -66,6 +67,45 @@ void test_scoring() {
|
| 66 | 67 |
assert(match("abc", " a b c ") > match("abc", " a b c "));
|
| 67 | 68 |
} |
| 68 | 69 |
|
| 70 |
+void test_exact_scores() {
|
|
| 71 |
+ /* Exact match is SCORE_MAX */ |
|
| 72 |
+ assert(match("abc", "abc") == SCORE_MAX);
|
|
| 73 |
+ assert(match("aBc", "abC") == SCORE_MAX);
|
|
| 74 |
+ |
|
| 75 |
+ /* Empty query always results in SCORE_MIN */ |
|
| 76 |
+ assert(match("", "") == SCORE_MIN);
|
|
| 77 |
+ assert(match("", "a") == SCORE_MIN);
|
|
| 78 |
+ assert(match("", "bb") == SCORE_MIN);
|
|
| 79 |
+ |
|
| 80 |
+ /* Gaps */ |
|
| 81 |
+ assert(match("a", "*a") == SCORE_GAP_LEADING);
|
|
| 82 |
+ assert(match("a", "*ba") == SCORE_GAP_LEADING*2);
|
|
| 83 |
+ assert(match("a", "**a*") == SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING);
|
|
| 84 |
+ assert(match("a", "**a**") == SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING*2);
|
|
| 85 |
+ assert(match("aa", "**aa**") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CONSECUTIVE + SCORE_GAP_TRAILING*2);
|
|
| 86 |
+ assert(match("aa", "**a*a**") == SCORE_GAP_LEADING + SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_GAP_TRAILING + SCORE_GAP_TRAILING);
|
|
| 87 |
+ |
|
| 88 |
+ /* Consecutive */ |
|
| 89 |
+ assert(match("aa", "*aa") == SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE);
|
|
| 90 |
+ assert(match("aaa", "*aaa") == SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE*2);
|
|
| 91 |
+ assert(match("aaa", "*a*aa") == SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_CONSECUTIVE);
|
|
| 92 |
+ |
|
| 93 |
+ /* Slash */ |
|
| 94 |
+ assert(match("a", "/a") == SCORE_GAP_LEADING + SCORE_MATCH_SLASH);
|
|
| 95 |
+ assert(match("a", "*/a") == SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH);
|
|
| 96 |
+ assert(match("aa", "a/aa") == SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH + SCORE_MATCH_CONSECUTIVE);
|
|
| 97 |
+ |
|
| 98 |
+ /* Capital */ |
|
| 99 |
+ assert(match("a", "bA") == SCORE_GAP_LEADING + SCORE_MATCH_CAPITAL);
|
|
| 100 |
+ assert(match("a", "baA") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL);
|
|
| 101 |
+ assert(match("aa", "baAa") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL + SCORE_MATCH_CONSECUTIVE);
|
|
| 102 |
+ |
|
| 103 |
+ /* Dot */ |
|
| 104 |
+ assert(match("a", ".a") == SCORE_GAP_LEADING + SCORE_MATCH_DOT);
|
|
| 105 |
+ assert(match("a", "*a.a") == SCORE_GAP_LEADING*3 + SCORE_MATCH_DOT);
|
|
| 106 |
+ assert(match("a", "*a.a") == SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_DOT);
|
|
| 107 |
+} |
|
| 108 |
+ |
|
| 69 | 109 |
void test_positions_1() {
|
| 70 | 110 |
size_t positions[3]; |
| 71 | 111 |
match_positions("amo", "app/models/foo", positions);
|
| ... | ... |
@@ -245,7 +285,8 @@ int main(int argc, char *argv[]) {
|
| 245 | 285 |
signal(SIGTRAP, ignore_signal); |
| 246 | 286 |
|
| 247 | 287 |
runtest(test_match); |
| 248 |
- runtest(test_scoring); |
|
| 288 |
+ runtest(test_relative_scores); |
|
| 289 |
+ runtest(test_exact_scores); |
|
| 249 | 290 |
runtest(test_positions_1); |
| 250 | 291 |
runtest(test_positions_2); |
| 251 | 292 |
runtest(test_positions_3); |