Browse code

Test for choices with two candidates

John Hawthorn authored on 18/09/2014 02:21:57
Showing 1 changed files

... ...
@@ -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