9
#include "greatest/greatest.h"
11
#define ASSERT_INT_EQ(a,b) ASSERT_EQ_FMT((a), (b), "%d")
13
static options_t default_options;
14
static choices_t choices;
16
static void setup(void *udata) {
19
options_init(&default_options);
20
choices_init(&choices, &default_options);
23
static void teardown(void *udata) {
25
choices_destroy(&choices);
28
TEST test_choices_empty() {
29
ASSERT_INT_EQ(0, choices.size);
30
ASSERT_INT_EQ(0, choices.available);
31
ASSERT_INT_EQ(0, choices.selection);
33
choices_prev(&choices);
34
ASSERT_INT_EQ(0, choices.selection);
36
choices_next(&choices);
37
ASSERT_INT_EQ(0, choices.selection);
42
TEST test_choices_1() {
43
choices_add(&choices, "tags");
45
choices_search(&choices, "");
46
ASSERT_INT_EQ(1, choices.available);
47
ASSERT_INT_EQ(0, choices.selection);
49
choices_search(&choices, "t");
50
ASSERT_INT_EQ(1, choices.available);
51
ASSERT_INT_EQ(0, choices.selection);
53
choices_prev(&choices);
54
ASSERT_INT_EQ(0, choices.selection);
56
choices_next(&choices);
57
ASSERT_INT_EQ(0, choices.selection);
59
ASSERT(!strcmp(choices_get(&choices, 0), "tags"));
60
ASSERT_INT_EQ(NULL, choices_get(&choices, 1));
65
TEST test_choices_2() {
66
choices_add(&choices, "tags");
67
choices_add(&choices, "test");
70
choices_search(&choices, "");
71
ASSERT_INT_EQ(0, choices.selection);
72
ASSERT_INT_EQ(2, choices.available);
74
choices_next(&choices);
75
ASSERT_INT_EQ(1, choices.selection);
76
choices_next(&choices);
77
ASSERT_INT_EQ(0, choices.selection);
79
choices_prev(&choices);
80
ASSERT_INT_EQ(1, choices.selection);
81
choices_prev(&choices);
82
ASSERT_INT_EQ(0, choices.selection);
85
choices_search(&choices, "te");
86
ASSERT_INT_EQ(1, choices.available);
87
ASSERT_INT_EQ(0, choices.selection);
88
ASSERT_STR_EQ("test", choices_get(&choices, 0));
90
choices_next(&choices);
91
ASSERT_INT_EQ(0, choices.selection);
93
choices_prev(&choices);
94
ASSERT_INT_EQ(0, choices.selection);
97
choices_search(&choices, "foobar");
98
ASSERT_INT_EQ(0, choices.available);
99
ASSERT_INT_EQ(0, choices.selection);
101
/* Different order due to scoring */
102
choices_search(&choices, "ts");
103
ASSERT_INT_EQ(2, choices.available);
104
ASSERT_INT_EQ(0, choices.selection);
105
ASSERT_STR_EQ("test", choices_get(&choices, 0));
106
ASSERT_STR_EQ("tags", choices_get(&choices, 1));
111
TEST test_choices_without_search() {
112
/* Before a search is run, it should return no results */
114
ASSERT_INT_EQ(0, choices.available);
115
ASSERT_INT_EQ(0, choices.selection);
116
ASSERT_INT_EQ(0, choices.size);
117
ASSERT_INT_EQ(NULL, choices_get(&choices, 0));
119
choices_add(&choices, "test");
121
ASSERT_INT_EQ(0, choices.available);
122
ASSERT_INT_EQ(0, choices.selection);
123
ASSERT_INT_EQ(1, choices.size);
124
ASSERT_INT_EQ(NULL, choices_get(&choices, 0));
129
/* Regression test for segfault */
130
TEST test_choices_unicode() {
131
choices_add(&choices, "Edmund Husserl - Méditations cartésiennes - Introduction a la phénoménologie.pdf");
132
choices_search(&choices, "e");
137
TEST test_choices_large_input() {
141
for(int i = 0; i < N; i++) {
142
asprintf(&strings[i], "%i", i);
143
choices_add(&choices, strings[i]);
146
choices_search(&choices, "12");
148
/* Must match `seq 0 99999 | grep '.*1.*2.*' | wc -l` */
149
ASSERT_INT_EQ(8146, choices.available);
151
ASSERT_STR_EQ("12", choices_get(&choices, 0));
153
for(int i = 0; i < N; i++) {
160
SUITE(choices_suite) {
161
SET_SETUP(setup, NULL);
162
SET_TEARDOWN(teardown, NULL);
164
RUN_TEST(test_choices_empty);
165
RUN_TEST(test_choices_1);
166
RUN_TEST(test_choices_2);
167
RUN_TEST(test_choices_without_search);
168
RUN_TEST(test_choices_unicode);
169
RUN_TEST(test_choices_large_input);