| ... | ... |
@@ -5,95 +5,129 @@ |
| 5 | 5 |
|
| 6 | 6 |
#include "greatest/greatest.h" |
| 7 | 7 |
|
| 8 |
-TEST test_match() {
|
|
| 8 |
+/* has_match(char *needle, char *haystack) */ |
|
| 9 |
+TEST exact_match_should_return_true() {
|
|
| 9 | 10 |
ASSERT(has_match("a", "a"));
|
| 11 |
+ PASS(); |
|
| 12 |
+} |
|
| 13 |
+ |
|
| 14 |
+TEST partial_match_should_return_true() {
|
|
| 10 | 15 |
ASSERT(has_match("a", "ab"));
|
| 11 | 16 |
ASSERT(has_match("a", "ba"));
|
| 17 |
+ PASS(); |
|
| 18 |
+} |
|
| 19 |
+ |
|
| 20 |
+TEST match_with_delimiters_in_between() {
|
|
| 12 | 21 |
ASSERT(has_match("abc", "a|b|c"));
|
| 22 |
+ PASS(); |
|
| 23 |
+} |
|
| 13 | 24 |
|
| 14 |
- /* non-match */ |
|
| 25 |
+TEST non_match_should_return_false() {
|
|
| 15 | 26 |
ASSERT(!has_match("a", ""));
|
| 16 | 27 |
ASSERT(!has_match("a", "b"));
|
| 17 | 28 |
ASSERT(!has_match("ass", "tags"));
|
| 29 |
+ PASS(); |
|
| 30 |
+} |
|
| 18 | 31 |
|
| 32 |
+TEST empty_query_should_always_match() {
|
|
| 19 | 33 |
/* match when query is empty */ |
| 20 | 34 |
ASSERT(has_match("", ""));
|
| 21 | 35 |
ASSERT(has_match("", "a"));
|
| 22 |
- |
|
| 23 | 36 |
PASS(); |
| 24 | 37 |
} |
| 25 | 38 |
|
| 26 |
-TEST test_relative_scores() {
|
|
| 39 |
+/* match(char *needle, char *haystack) */ |
|
| 40 |
+ |
|
| 41 |
+TEST should_prefer_starts_of_words() {
|
|
| 27 | 42 |
/* App/Models/Order is better than App/MOdels/zRder */ |
| 28 | 43 |
ASSERT(match("amor", "app/models/order") > match("amor", "app/models/zrder"));
|
| 44 |
+ PASS(); |
|
| 45 |
+} |
|
| 29 | 46 |
|
| 47 |
+TEST should_prefer_consecutive_letters() {
|
|
| 30 | 48 |
/* App/MOdels/foo is better than App/M/fOo */ |
| 31 | 49 |
ASSERT(match("amo", "app/m/foo") < match("amo", "app/models/foo"));
|
| 50 |
+ PASS(); |
|
| 51 |
+} |
|
| 32 | 52 |
|
| 53 |
+TEST should_prefer_contiguous_over_letter_following_period() {
|
|
| 33 | 54 |
/* GEMFIle.Lock < GEMFILe */ |
| 34 | 55 |
ASSERT(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile"));
|
| 56 |
+ PASS(); |
|
| 57 |
+} |
|
| 35 | 58 |
|
| 36 |
- /* GEMFIle.Lock < GEMFILe */ |
|
| 37 |
- ASSERT(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile"));
|
|
| 38 |
- |
|
| 39 |
- /* Prefer shorter matches */ |
|
| 59 |
+TEST should_prefer_shorter_matches() {
|
|
| 40 | 60 |
ASSERT(match("abce", "abcdef") > match("abce", "abc de"));
|
| 61 |
+ ASSERT(match("abc", " a b c ") > match("abc", " a b c "));
|
|
| 62 |
+ ASSERT(match("abc", " a b c ") > match("abc", " a b c "));
|
|
| 63 |
+ PASS(); |
|
| 64 |
+} |
|
| 41 | 65 |
|
| 42 |
- /* Prefer shorter candidates */ |
|
| 66 |
+TEST should_prefer_shorter_candidates() {
|
|
| 43 | 67 |
ASSERT(match("test", "tests") > match("test", "testing"));
|
| 68 |
+ PASS(); |
|
| 69 |
+} |
|
| 44 | 70 |
|
| 71 |
+TEST should_prefer_start_of_candidate() {
|
|
| 45 | 72 |
/* Scores first letter highly */ |
| 46 | 73 |
ASSERT(match("test", "testing") > match("test", "/testing"));
|
| 47 |
- |
|
| 48 |
- /* Prefer shorter matches */ |
|
| 49 |
- ASSERT(match("abc", " a b c ") > match("abc", " a b c "));
|
|
| 50 |
- ASSERT(match("abc", " a b c ") > match("abc", " a b c "));
|
|
| 51 |
- |
|
| 52 | 74 |
PASS(); |
| 53 | 75 |
} |
| 54 | 76 |
|
| 55 |
-TEST test_exact_scores() {
|
|
| 77 |
+TEST score_exact_match() {
|
|
| 56 | 78 |
/* Exact match is SCORE_MAX */ |
| 57 | 79 |
ASSERT_EQ(SCORE_MAX, match("abc", "abc"));
|
| 58 | 80 |
ASSERT_EQ(SCORE_MAX, match("aBc", "abC"));
|
| 81 |
+ PASS(); |
|
| 82 |
+} |
|
| 59 | 83 |
|
| 84 |
+TEST score_empty_query() {
|
|
| 60 | 85 |
/* Empty query always results in SCORE_MIN */ |
| 61 | 86 |
ASSERT_EQ(SCORE_MIN, match("", ""));
|
| 62 | 87 |
ASSERT_EQ(SCORE_MIN, match("", "a"));
|
| 63 | 88 |
ASSERT_EQ(SCORE_MIN, match("", "bb"));
|
| 89 |
+ PASS(); |
|
| 90 |
+} |
|
| 64 | 91 |
|
| 65 |
- /* Gaps */ |
|
| 92 |
+TEST score_gaps() {
|
|
| 66 | 93 |
ASSERT_EQ(SCORE_GAP_LEADING, match("a", "*a"));
|
| 67 | 94 |
ASSERT_EQ(SCORE_GAP_LEADING*2, match("a", "*ba"));
|
| 68 | 95 |
ASSERT_EQ(SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING, match("a", "**a*"));
|
| 69 | 96 |
ASSERT_EQ(SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING*2, match("a", "**a**"));
|
| 70 | 97 |
ASSERT_EQ(SCORE_GAP_LEADING*2 + SCORE_MATCH_CONSECUTIVE + SCORE_GAP_TRAILING*2, match("aa", "**aa**"));
|
| 71 | 98 |
ASSERT_EQ(SCORE_GAP_LEADING + SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_GAP_TRAILING + SCORE_GAP_TRAILING, match("aa", "**a*a**"));
|
| 99 |
+ PASS(); |
|
| 100 |
+} |
|
| 72 | 101 |
|
| 73 |
- /* Consecutive */ |
|
| 102 |
+TEST score_consecutive() {
|
|
| 74 | 103 |
ASSERT_EQ(SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE, match("aa", "*aa"));
|
| 75 | 104 |
ASSERT_EQ(SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE*2, match("aaa", "*aaa"));
|
| 76 | 105 |
ASSERT_EQ(SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_CONSECUTIVE, match("aaa", "*a*aa"));
|
| 106 |
+ PASS(); |
|
| 107 |
+} |
|
| 77 | 108 |
|
| 78 |
- /* Slash */ |
|
| 109 |
+TEST score_slash() {
|
|
| 79 | 110 |
ASSERT_EQ(SCORE_GAP_LEADING + SCORE_MATCH_SLASH, match("a", "/a"));
|
| 80 | 111 |
ASSERT_EQ(SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH, match("a", "*/a"));
|
| 81 | 112 |
ASSERT_EQ(SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH + SCORE_MATCH_CONSECUTIVE, match("aa", "a/aa"));
|
| 113 |
+ PASS(); |
|
| 114 |
+} |
|
| 82 | 115 |
|
| 83 |
- /* Capital */ |
|
| 116 |
+TEST score_capital() {
|
|
| 84 | 117 |
ASSERT_EQ(SCORE_GAP_LEADING + SCORE_MATCH_CAPITAL, match("a", "bA"));
|
| 85 | 118 |
ASSERT_EQ(SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL, match("a", "baA"));
|
| 86 | 119 |
ASSERT_EQ(SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL + SCORE_MATCH_CONSECUTIVE, match("aa", "baAa"));
|
| 120 |
+ PASS(); |
|
| 121 |
+} |
|
| 87 | 122 |
|
| 88 |
- /* Dot */ |
|
| 123 |
+TEST score_dot() {
|
|
| 89 | 124 |
ASSERT_EQ(SCORE_GAP_LEADING + SCORE_MATCH_DOT, match("a", ".a"));
|
| 90 | 125 |
ASSERT_EQ(SCORE_GAP_LEADING*3 + SCORE_MATCH_DOT, match("a", "*a.a"));
|
| 91 | 126 |
ASSERT_EQ(SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_DOT, match("a", "*a.a"));
|
| 92 |
- |
|
| 93 | 127 |
PASS(); |
| 94 | 128 |
} |
| 95 | 129 |
|
| 96 |
-TEST test_positions_1() {
|
|
| 130 |
+TEST positions_consecutive() {
|
|
| 97 | 131 |
size_t positions[3]; |
| 98 | 132 |
match_positions("amo", "app/models/foo", positions);
|
| 99 | 133 |
ASSERT_EQ(0, positions[0]); |
| ... | ... |
@@ -103,7 +137,7 @@ TEST test_positions_1() {
|
| 103 | 137 |
PASS(); |
| 104 | 138 |
} |
| 105 | 139 |
|
| 106 |
-TEST test_positions_2() {
|
|
| 140 |
+TEST positions_start_of_word() {
|
|
| 107 | 141 |
/* |
| 108 | 142 |
* We should prefer matching the 'o' in order, since it's the beginning |
| 109 | 143 |
* of a word. |
| ... | ... |
@@ -113,21 +147,17 @@ TEST test_positions_2() {
|
| 113 | 147 |
ASSERT_EQ(0, positions[0]); |
| 114 | 148 |
ASSERT_EQ(4, positions[1]); |
| 115 | 149 |
ASSERT_EQ(11, positions[2]); |
| 150 |
+ ASSERT_EQ(12, positions[3]); |
|
| 116 | 151 |
|
| 117 | 152 |
PASS(); |
| 118 | 153 |
} |
| 119 | 154 |
|
| 120 |
-TEST test_positions_3() {
|
|
| 155 |
+TEST positions_no_bonuses() {
|
|
| 121 | 156 |
size_t positions[2]; |
| 122 | 157 |
match_positions("as", "tags", positions);
|
| 123 | 158 |
ASSERT_EQ(1, positions[0]); |
| 124 | 159 |
ASSERT_EQ(3, positions[1]); |
| 125 | 160 |
|
| 126 |
- PASS(); |
|
| 127 |
-} |
|
| 128 |
- |
|
| 129 |
-TEST test_positions_4() {
|
|
| 130 |
- size_t positions[2]; |
|
| 131 | 161 |
match_positions("as", "examples.txt", positions);
|
| 132 | 162 |
ASSERT_EQ(2, positions[0]); |
| 133 | 163 |
ASSERT_EQ(7, positions[1]); |
| ... | ... |
@@ -135,7 +165,7 @@ TEST test_positions_4() {
|
| 135 | 165 |
PASS(); |
| 136 | 166 |
} |
| 137 | 167 |
|
| 138 |
-TEST test_positions_5() {
|
|
| 168 |
+TEST positions_multiple_candidates_start_of_words() {
|
|
| 139 | 169 |
size_t positions[3]; |
| 140 | 170 |
match_positions("abc", "a/a/b/c/c", positions);
|
| 141 | 171 |
ASSERT_EQ(2, positions[0]); |
| ... | ... |
@@ -145,7 +175,7 @@ TEST test_positions_5() {
|
| 145 | 175 |
PASS(); |
| 146 | 176 |
} |
| 147 | 177 |
|
| 148 |
-TEST test_positions_exact() {
|
|
| 178 |
+TEST positions_exact_match() {
|
|
| 149 | 179 |
size_t positions[3]; |
| 150 | 180 |
match_positions("foo", "foo", positions);
|
| 151 | 181 |
ASSERT_EQ(0, positions[0]); |
| ... | ... |
@@ -156,13 +186,30 @@ TEST test_positions_exact() {
|
| 156 | 186 |
} |
| 157 | 187 |
|
| 158 | 188 |
SUITE(match_suite) {
|
| 159 |
- RUN_TEST(test_match); |
|
| 160 |
- RUN_TEST(test_relative_scores); |
|
| 161 |
- RUN_TEST(test_exact_scores); |
|
| 162 |
- RUN_TEST(test_positions_1); |
|
| 163 |
- RUN_TEST(test_positions_2); |
|
| 164 |
- RUN_TEST(test_positions_3); |
|
| 165 |
- RUN_TEST(test_positions_4); |
|
| 166 |
- RUN_TEST(test_positions_5); |
|
| 167 |
- RUN_TEST(test_positions_exact); |
|
| 189 |
+ RUN_TEST(exact_match_should_return_true); |
|
| 190 |
+ RUN_TEST(partial_match_should_return_true); |
|
| 191 |
+ RUN_TEST(empty_query_should_always_match); |
|
| 192 |
+ RUN_TEST(non_match_should_return_false); |
|
| 193 |
+ RUN_TEST(match_with_delimiters_in_between); |
|
| 194 |
+ |
|
| 195 |
+ RUN_TEST(should_prefer_starts_of_words); |
|
| 196 |
+ RUN_TEST(should_prefer_consecutive_letters); |
|
| 197 |
+ RUN_TEST(should_prefer_contiguous_over_letter_following_period); |
|
| 198 |
+ RUN_TEST(should_prefer_shorter_matches); |
|
| 199 |
+ RUN_TEST(should_prefer_shorter_candidates); |
|
| 200 |
+ RUN_TEST(should_prefer_start_of_candidate); |
|
| 201 |
+ |
|
| 202 |
+ RUN_TEST(score_exact_match); |
|
| 203 |
+ RUN_TEST(score_empty_query); |
|
| 204 |
+ RUN_TEST(score_gaps); |
|
| 205 |
+ RUN_TEST(score_consecutive); |
|
| 206 |
+ RUN_TEST(score_slash); |
|
| 207 |
+ RUN_TEST(score_capital); |
|
| 208 |
+ RUN_TEST(score_dot); |
|
| 209 |
+ |
|
| 210 |
+ RUN_TEST(positions_consecutive); |
|
| 211 |
+ RUN_TEST(positions_start_of_word); |
|
| 212 |
+ RUN_TEST(positions_no_bonuses); |
|
| 213 |
+ RUN_TEST(positions_multiple_candidates_start_of_words); |
|
| 214 |
+ RUN_TEST(positions_exact_match); |
|
| 168 | 215 |
} |