mirror of
https://github.com/profanity-im/profanity.git
synced 2024-12-04 14:46:46 -05:00
Simplified autocomplete api
Now stores memory management functions, passed on p_autocomplete_new
This commit is contained in:
parent
4be250ae12
commit
b02b52d86a
@ -32,16 +32,16 @@
|
|||||||
|
|
||||||
static PAutocomplete ac;
|
static PAutocomplete ac;
|
||||||
|
|
||||||
static PContact _copy(PContact contact);
|
|
||||||
|
|
||||||
void contact_list_init(void)
|
void contact_list_init(void)
|
||||||
{
|
{
|
||||||
ac = p_autocomplete_new();
|
ac = p_autocomplete_new((PStrFunc)p_contact_name,
|
||||||
|
(PCopyFunc)p_contact_copy,
|
||||||
|
(GDestroyNotify)p_contact_free);
|
||||||
}
|
}
|
||||||
|
|
||||||
void contact_list_clear(void)
|
void contact_list_clear(void)
|
||||||
{
|
{
|
||||||
p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset_search_attempts(void)
|
void reset_search_attempts(void)
|
||||||
@ -51,30 +51,21 @@ void reset_search_attempts(void)
|
|||||||
|
|
||||||
void contact_list_remove(const char * const name)
|
void contact_list_remove(const char * const name)
|
||||||
{
|
{
|
||||||
p_autocomplete_remove(ac, name, (PStrFunc)p_contact_name, (GDestroyNotify)p_contact_free);
|
p_autocomplete_remove(ac, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void contact_list_add(const char * const name, const char * const show,
|
void contact_list_add(const char * const name, const char * const show,
|
||||||
const char * const status)
|
const char * const status)
|
||||||
{
|
{
|
||||||
p_autocomplete_add(ac, p_contact_new(name, show, status), (PStrFunc)p_contact_name,
|
p_autocomplete_add(ac, p_contact_new(name, show, status));
|
||||||
(GDestroyNotify)p_contact_free);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GSList * get_contact_list(void)
|
GSList * get_contact_list(void)
|
||||||
{
|
{
|
||||||
return p_autocomplete_get_list(ac, (PCopyFunc)_copy);
|
return p_autocomplete_get_list(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
char * find_contact(char *search_str)
|
char * find_contact(char *search_str)
|
||||||
{
|
{
|
||||||
return p_autocomplete_complete(ac, search_str, (PStrFunc)p_contact_name);
|
return p_autocomplete_complete(ac, search_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
static PContact _copy(PContact contact)
|
|
||||||
{
|
|
||||||
return p_contact_new(p_contact_name(contact),
|
|
||||||
p_contact_show(contact),
|
|
||||||
p_contact_status(contact));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -32,28 +32,44 @@ struct p_autocomplete_t {
|
|||||||
GSList *items;
|
GSList *items;
|
||||||
GSList *last_found;
|
GSList *last_found;
|
||||||
gchar *search_str;
|
gchar *search_str;
|
||||||
|
PStrFunc str_func;
|
||||||
|
PCopyFunc copy_func;
|
||||||
|
GDestroyNotify free_func;
|
||||||
};
|
};
|
||||||
|
|
||||||
static gchar * _search_from(PAutocomplete ac, GSList *curr, PStrFunc str_func);
|
static gchar * _search_from(PAutocomplete ac, GSList *curr);
|
||||||
static const char *_str_func_default(const char *orig);
|
static const char *_str_func_default(const char *orig);
|
||||||
static const char *_copy_func_default(const char *orig);
|
static const char *_copy_func_default(const char *orig);
|
||||||
|
|
||||||
PAutocomplete p_autocomplete_new(void)
|
PAutocomplete p_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func,
|
||||||
|
GDestroyNotify free_func)
|
||||||
{
|
{
|
||||||
PAutocomplete new = malloc(sizeof(struct p_autocomplete_t));
|
PAutocomplete new = malloc(sizeof(struct p_autocomplete_t));
|
||||||
new->items = NULL;
|
new->items = NULL;
|
||||||
new->last_found = NULL;
|
new->last_found = NULL;
|
||||||
new->search_str = NULL;
|
new->search_str = NULL;
|
||||||
|
|
||||||
|
if (str_func)
|
||||||
|
new->str_func = str_func;
|
||||||
|
else
|
||||||
|
new->str_func = (PStrFunc)_str_func_default;
|
||||||
|
|
||||||
|
if (copy_func)
|
||||||
|
new->copy_func = copy_func;
|
||||||
|
else
|
||||||
|
new->copy_func = (PCopyFunc)_copy_func_default;
|
||||||
|
|
||||||
|
if (free_func)
|
||||||
|
new->free_func = free_func;
|
||||||
|
else
|
||||||
|
new->free_func = (GDestroyNotify)free;
|
||||||
|
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
void p_autocomplete_clear(PAutocomplete ac, GDestroyNotify free_func)
|
void p_autocomplete_clear(PAutocomplete ac)
|
||||||
{
|
{
|
||||||
if (free_func == NULL)
|
g_slist_free_full(ac->items, ac->free_func);
|
||||||
free_func = (GDestroyNotify)free;
|
|
||||||
|
|
||||||
g_slist_free_full(ac->items, free_func);
|
|
||||||
ac->items = NULL;
|
ac->items = NULL;
|
||||||
|
|
||||||
p_autocomplete_reset(ac);
|
p_autocomplete_reset(ac);
|
||||||
@ -68,14 +84,8 @@ void p_autocomplete_reset(PAutocomplete ac)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void p_autocomplete_add(PAutocomplete ac, void *item, PStrFunc str_func,
|
void p_autocomplete_add(PAutocomplete ac, void *item)
|
||||||
GDestroyNotify free_func)
|
|
||||||
{
|
{
|
||||||
if (str_func == NULL)
|
|
||||||
str_func = (PStrFunc)_str_func_default;
|
|
||||||
if (free_func == NULL)
|
|
||||||
free_func = (GDestroyNotify)free;
|
|
||||||
|
|
||||||
if (ac->items == NULL) {
|
if (ac->items == NULL) {
|
||||||
ac->items = g_slist_append(ac->items, item);
|
ac->items = g_slist_append(ac->items, item);
|
||||||
return;
|
return;
|
||||||
@ -85,14 +95,14 @@ void p_autocomplete_add(PAutocomplete ac, void *item, PStrFunc str_func,
|
|||||||
while(curr) {
|
while(curr) {
|
||||||
|
|
||||||
// insert
|
// insert
|
||||||
if (g_strcmp0(str_func(curr->data), str_func(item)) > 0) {
|
if (g_strcmp0(ac->str_func(curr->data), ac->str_func(item)) > 0) {
|
||||||
ac->items = g_slist_insert_before(ac->items,
|
ac->items = g_slist_insert_before(ac->items,
|
||||||
curr, item);
|
curr, item);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// update
|
// update
|
||||||
} else if (g_strcmp0(str_func(curr->data), str_func(item)) == 0) {
|
} else if (g_strcmp0(ac->str_func(curr->data), ac->str_func(item)) == 0) {
|
||||||
free_func(curr->data);
|
ac->free_func(curr->data);
|
||||||
curr->data = item;
|
curr->data = item;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -107,15 +117,11 @@ void p_autocomplete_add(PAutocomplete ac, void *item, PStrFunc str_func,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void p_autocomplete_remove(PAutocomplete ac, const char * const item,
|
void p_autocomplete_remove(PAutocomplete ac, const char * const item)
|
||||||
PStrFunc str_func, GDestroyNotify free_func)
|
|
||||||
{
|
{
|
||||||
if (str_func == NULL)
|
|
||||||
str_func = (PStrFunc)_str_func_default;
|
|
||||||
|
|
||||||
// reset last found if it points to the item to be removed
|
// reset last found if it points to the item to be removed
|
||||||
if (ac->last_found != NULL)
|
if (ac->last_found != NULL)
|
||||||
if (g_strcmp0(str_func(ac->last_found->data), item) == 0)
|
if (g_strcmp0(ac->str_func(ac->last_found->data), item) == 0)
|
||||||
ac->last_found = NULL;
|
ac->last_found = NULL;
|
||||||
|
|
||||||
if (!ac->items) {
|
if (!ac->items) {
|
||||||
@ -124,10 +130,10 @@ void p_autocomplete_remove(PAutocomplete ac, const char * const item,
|
|||||||
GSList *curr = ac->items;
|
GSList *curr = ac->items;
|
||||||
|
|
||||||
while(curr) {
|
while(curr) {
|
||||||
if (g_strcmp0(str_func(curr->data), item) == 0) {
|
if (g_strcmp0(ac->str_func(curr->data), item) == 0) {
|
||||||
void *current_item = curr->data;
|
void *current_item = curr->data;
|
||||||
ac->items = g_slist_remove(ac->items, curr->data);
|
ac->items = g_slist_remove(ac->items, curr->data);
|
||||||
free_func(current_item);
|
ac->free_func(current_item);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -139,28 +145,21 @@ void p_autocomplete_remove(PAutocomplete ac, const char * const item,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GSList * p_autocomplete_get_list(PAutocomplete ac, PCopyFunc copy_func)
|
GSList * p_autocomplete_get_list(PAutocomplete ac)
|
||||||
{
|
{
|
||||||
if (copy_func == NULL)
|
|
||||||
copy_func = (PCopyFunc)_copy_func_default;
|
|
||||||
|
|
||||||
GSList *copy = NULL;
|
GSList *copy = NULL;
|
||||||
GSList *curr = ac->items;
|
GSList *curr = ac->items;
|
||||||
|
|
||||||
while(curr) {
|
while(curr) {
|
||||||
copy = g_slist_append(copy, copy_func(curr->data));
|
copy = g_slist_append(copy, ac->copy_func(curr->data));
|
||||||
curr = g_slist_next(curr);
|
curr = g_slist_next(curr);
|
||||||
}
|
}
|
||||||
|
|
||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
gchar * p_autocomplete_complete(PAutocomplete ac, gchar *search_str,
|
gchar * p_autocomplete_complete(PAutocomplete ac, gchar *search_str)
|
||||||
PStrFunc str_func)
|
|
||||||
{
|
{
|
||||||
if (str_func == NULL)
|
|
||||||
str_func = (PStrFunc)_str_func_default;
|
|
||||||
|
|
||||||
gchar *found = NULL;
|
gchar *found = NULL;
|
||||||
|
|
||||||
// no items to search
|
// no items to search
|
||||||
@ -173,18 +172,18 @@ gchar * p_autocomplete_complete(PAutocomplete ac, gchar *search_str,
|
|||||||
(gchar *) malloc((strlen(search_str) + 1) * sizeof(gchar));
|
(gchar *) malloc((strlen(search_str) + 1) * sizeof(gchar));
|
||||||
strcpy(ac->search_str, search_str);
|
strcpy(ac->search_str, search_str);
|
||||||
|
|
||||||
found = _search_from(ac, ac->items, str_func);
|
found = _search_from(ac, ac->items);
|
||||||
return found;
|
return found;
|
||||||
|
|
||||||
// subsequent search attempt
|
// subsequent search attempt
|
||||||
} else {
|
} else {
|
||||||
// search from here+1 tp end
|
// search from here+1 tp end
|
||||||
found = _search_from(ac, g_slist_next(ac->last_found), str_func);
|
found = _search_from(ac, g_slist_next(ac->last_found));
|
||||||
if (found != NULL)
|
if (found != NULL)
|
||||||
return found;
|
return found;
|
||||||
|
|
||||||
// search from beginning
|
// search from beginning
|
||||||
found = _search_from(ac, ac->items, str_func);
|
found = _search_from(ac, ac->items);
|
||||||
if (found != NULL)
|
if (found != NULL)
|
||||||
return found;
|
return found;
|
||||||
|
|
||||||
@ -194,22 +193,22 @@ gchar * p_autocomplete_complete(PAutocomplete ac, gchar *search_str,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static gchar * _search_from(PAutocomplete ac, GSList *curr, PStrFunc str_func)
|
static gchar * _search_from(PAutocomplete ac, GSList *curr)
|
||||||
{
|
{
|
||||||
while(curr) {
|
while(curr) {
|
||||||
|
|
||||||
// match found
|
// match found
|
||||||
if (strncmp(str_func(curr->data),
|
if (strncmp(ac->str_func(curr->data),
|
||||||
ac->search_str,
|
ac->search_str,
|
||||||
strlen(ac->search_str)) == 0) {
|
strlen(ac->search_str)) == 0) {
|
||||||
gchar *result =
|
gchar *result =
|
||||||
(gchar *) malloc((strlen(str_func(curr->data)) + 1) * sizeof(gchar));
|
(gchar *) malloc((strlen(ac->str_func(curr->data)) + 1) * sizeof(gchar));
|
||||||
|
|
||||||
// set pointer to last found
|
// set pointer to last found
|
||||||
ac->last_found = curr;
|
ac->last_found = curr;
|
||||||
|
|
||||||
// return the string, must be free'd by caller
|
// return the string, must be free'd by caller
|
||||||
strcpy(result, str_func(curr->data));
|
strcpy(result, ac->str_func(curr->data));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,15 +29,13 @@ typedef struct p_autocomplete_t *PAutocomplete;
|
|||||||
typedef const char * (*PStrFunc)(const void *obj);
|
typedef const char * (*PStrFunc)(const void *obj);
|
||||||
typedef void * (*PCopyFunc)(const void *obj);
|
typedef void * (*PCopyFunc)(const void *obj);
|
||||||
|
|
||||||
PAutocomplete p_autocomplete_new(void);
|
PAutocomplete p_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func,
|
||||||
void p_autocomplete_clear(PAutocomplete ac, GDestroyNotify free_func);
|
|
||||||
void p_autocomplete_reset(PAutocomplete ac);
|
|
||||||
void p_autocomplete_add(PAutocomplete ac, void *item, PStrFunc str_func,
|
|
||||||
GDestroyNotify free_func);
|
GDestroyNotify free_func);
|
||||||
void p_autocomplete_remove(PAutocomplete ac, const char * const item,
|
void p_autocomplete_clear(PAutocomplete ac);
|
||||||
PStrFunc str_func, GDestroyNotify free_func);
|
void p_autocomplete_reset(PAutocomplete ac);
|
||||||
GSList * p_autocomplete_get_list(PAutocomplete ac, PCopyFunc copy_func);
|
void p_autocomplete_add(PAutocomplete ac, void *item);
|
||||||
gchar * p_autocomplete_complete(PAutocomplete ac, gchar *search_str,
|
void p_autocomplete_remove(PAutocomplete ac, const char * const item);
|
||||||
PStrFunc str_func);
|
GSList * p_autocomplete_get_list(PAutocomplete ac);
|
||||||
|
gchar * p_autocomplete_complete(PAutocomplete ac, gchar *search_str);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -9,205 +9,202 @@
|
|||||||
|
|
||||||
static void clear_empty(void)
|
static void clear_empty(void)
|
||||||
{
|
{
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
|
||||||
p_autocomplete_clear(ac, NULL);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void clear_empty_with_free_func(void)
|
static void clear_empty_with_free_func(void)
|
||||||
{
|
{
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new(NULL, NULL, (GDestroyNotify)p_contact_free);
|
||||||
p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void reset_after_create(void)
|
static void reset_after_create(void)
|
||||||
{
|
{
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
|
||||||
p_autocomplete_reset(ac);
|
p_autocomplete_reset(ac);
|
||||||
p_autocomplete_clear(ac, NULL);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void find_after_create(void)
|
static void find_after_create(void)
|
||||||
{
|
{
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
|
||||||
p_autocomplete_complete(ac, "hello", NULL);
|
p_autocomplete_complete(ac, "hello");
|
||||||
p_autocomplete_clear(ac, NULL);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void get_after_create_returns_null(void)
|
static void get_after_create_returns_null(void)
|
||||||
{
|
{
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
|
||||||
GSList *result = p_autocomplete_get_list(ac, NULL);
|
GSList *result = p_autocomplete_get_list(ac);
|
||||||
|
|
||||||
assert_is_null(result);
|
assert_is_null(result);
|
||||||
|
|
||||||
p_autocomplete_clear(ac, NULL);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void get_after_create_with_copy_func_returns_null(void)
|
static void get_after_create_with_copy_func_returns_null(void)
|
||||||
{
|
{
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new(NULL, (PCopyFunc)p_contact_copy,
|
||||||
GSList *result = p_autocomplete_get_list(ac, (PCopyFunc)p_contact_copy);
|
(GDestroyNotify)p_contact_free);
|
||||||
|
GSList *result = p_autocomplete_get_list(ac);
|
||||||
|
|
||||||
assert_is_null(result);
|
assert_is_null(result);
|
||||||
|
|
||||||
p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_one_and_complete(void)
|
static void add_one_and_complete(void)
|
||||||
{
|
{
|
||||||
char *item = strdup("Hello");
|
char *item = strdup("Hello");
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
|
||||||
p_autocomplete_add(ac, item, NULL, NULL);
|
p_autocomplete_add(ac, item);
|
||||||
char *result = p_autocomplete_complete(ac, "Hel", NULL);
|
char *result = p_autocomplete_complete(ac, "Hel");
|
||||||
|
|
||||||
assert_string_equals("Hello", result);
|
assert_string_equals("Hello", result);
|
||||||
|
|
||||||
p_autocomplete_clear(ac, NULL);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_one_and_complete_with_funcs(void)
|
static void add_one_and_complete_with_funcs(void)
|
||||||
{
|
{
|
||||||
PContact contact = p_contact_new("James", "Online", "I'm here");
|
PContact contact = p_contact_new("James", "Online", "I'm here");
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name, NULL,
|
||||||
p_autocomplete_add(ac, contact, (PStrFunc)p_contact_name,
|
|
||||||
(GDestroyNotify)p_contact_free);
|
(GDestroyNotify)p_contact_free);
|
||||||
char *result = p_autocomplete_complete(ac, "Jam", (PStrFunc)p_contact_name);
|
p_autocomplete_add(ac, contact);
|
||||||
|
char *result = p_autocomplete_complete(ac, "Jam");
|
||||||
|
|
||||||
assert_string_equals("James", result);
|
assert_string_equals("James", result);
|
||||||
|
|
||||||
p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_two_and_complete_returns_first(void)
|
static void add_two_and_complete_returns_first(void)
|
||||||
{
|
{
|
||||||
char *item1 = strdup("Hello");
|
char *item1 = strdup("Hello");
|
||||||
char *item2 = strdup("Help");
|
char *item2 = strdup("Help");
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
|
||||||
p_autocomplete_add(ac, item1, NULL, NULL);
|
p_autocomplete_add(ac, item1);
|
||||||
p_autocomplete_add(ac, item2, NULL, NULL);
|
p_autocomplete_add(ac, item2);
|
||||||
char *result = p_autocomplete_complete(ac, "Hel", NULL);
|
char *result = p_autocomplete_complete(ac, "Hel");
|
||||||
|
|
||||||
assert_string_equals("Hello", result);
|
assert_string_equals("Hello", result);
|
||||||
|
|
||||||
p_autocomplete_clear(ac, NULL);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_two_and_complete_returns_first_with_funcs(void)
|
static void add_two_and_complete_returns_first_with_funcs(void)
|
||||||
{
|
{
|
||||||
PContact contact1 = p_contact_new("James", "Online", "I'm here");
|
PContact contact1 = p_contact_new("James", "Online", "I'm here");
|
||||||
PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch");
|
PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch");
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name, NULL,
|
||||||
p_autocomplete_add(ac, contact1, (PStrFunc)p_contact_name,
|
|
||||||
(GDestroyNotify)p_contact_free);
|
(GDestroyNotify)p_contact_free);
|
||||||
p_autocomplete_add(ac, contact2, (PStrFunc)p_contact_name,
|
p_autocomplete_add(ac, contact1);
|
||||||
(GDestroyNotify)p_contact_free);
|
p_autocomplete_add(ac, contact2);
|
||||||
char *result = p_autocomplete_complete(ac, "Jam", (PStrFunc)p_contact_name);
|
char *result = p_autocomplete_complete(ac, "Jam");
|
||||||
|
|
||||||
assert_string_equals("James", result);
|
assert_string_equals("James", result);
|
||||||
|
|
||||||
p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_two_and_complete_returns_second(void)
|
static void add_two_and_complete_returns_second(void)
|
||||||
{
|
{
|
||||||
char *item1 = strdup("Hello");
|
char *item1 = strdup("Hello");
|
||||||
char *item2 = strdup("Help");
|
char *item2 = strdup("Help");
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
|
||||||
p_autocomplete_add(ac, item1, NULL, NULL);
|
p_autocomplete_add(ac, item1);
|
||||||
p_autocomplete_add(ac, item2, NULL, NULL);
|
p_autocomplete_add(ac, item2);
|
||||||
char *result1 = p_autocomplete_complete(ac, "Hel", NULL);
|
char *result1 = p_autocomplete_complete(ac, "Hel");
|
||||||
char *result2 = p_autocomplete_complete(ac, result1, NULL);
|
char *result2 = p_autocomplete_complete(ac, result1);
|
||||||
|
|
||||||
assert_string_equals("Help", result2);
|
assert_string_equals("Help", result2);
|
||||||
|
|
||||||
p_autocomplete_clear(ac, NULL);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_two_and_complete_returns_second_with_funcs(void)
|
static void add_two_and_complete_returns_second_with_funcs(void)
|
||||||
{
|
{
|
||||||
PContact contact1 = p_contact_new("James", "Online", "I'm here");
|
PContact contact1 = p_contact_new("James", "Online", "I'm here");
|
||||||
PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch");
|
PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch");
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name, NULL,
|
||||||
p_autocomplete_add(ac, contact1, (PStrFunc)p_contact_name,
|
|
||||||
(GDestroyNotify)p_contact_free);
|
(GDestroyNotify)p_contact_free);
|
||||||
p_autocomplete_add(ac, contact2, (PStrFunc)p_contact_name,
|
p_autocomplete_add(ac, contact1);
|
||||||
(GDestroyNotify)p_contact_free);
|
p_autocomplete_add(ac, contact2);
|
||||||
char *result1 = p_autocomplete_complete(ac, "Jam", (PStrFunc)p_contact_name);
|
char *result1 = p_autocomplete_complete(ac, "Jam");
|
||||||
char *result2 = p_autocomplete_complete(ac, result1, (PStrFunc)p_contact_name);
|
char *result2 = p_autocomplete_complete(ac, result1);
|
||||||
|
|
||||||
assert_string_equals("Jamie", result2);
|
assert_string_equals("Jamie", result2);
|
||||||
|
|
||||||
p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_two_adds_two(void)
|
static void add_two_adds_two(void)
|
||||||
{
|
{
|
||||||
char *item1 = strdup("Hello");
|
char *item1 = strdup("Hello");
|
||||||
char *item2 = strdup("Help");
|
char *item2 = strdup("Help");
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
|
||||||
p_autocomplete_add(ac, item1, NULL, NULL);
|
p_autocomplete_add(ac, item1);
|
||||||
p_autocomplete_add(ac, item2, NULL, NULL);
|
p_autocomplete_add(ac, item2);
|
||||||
GSList *result = p_autocomplete_get_list(ac, NULL);
|
GSList *result = p_autocomplete_get_list(ac);
|
||||||
|
|
||||||
assert_int_equals(2, g_slist_length(result));
|
assert_int_equals(2, g_slist_length(result));
|
||||||
|
|
||||||
p_autocomplete_clear(ac, NULL);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_two_adds_two_with_funcs(void)
|
static void add_two_adds_two_with_funcs(void)
|
||||||
{
|
{
|
||||||
PContact contact1 = p_contact_new("James", "Online", "I'm here");
|
PContact contact1 = p_contact_new("James", "Online", "I'm here");
|
||||||
PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch");
|
PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch");
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name,
|
||||||
p_autocomplete_add(ac, contact1, (PStrFunc)p_contact_name,
|
(PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free);
|
||||||
(GDestroyNotify)p_contact_free);
|
p_autocomplete_add(ac, contact1);
|
||||||
p_autocomplete_add(ac, contact2, (PStrFunc)p_contact_name,
|
p_autocomplete_add(ac, contact2);
|
||||||
(GDestroyNotify)p_contact_free);
|
GSList *result = p_autocomplete_get_list(ac);
|
||||||
GSList *result = p_autocomplete_get_list(ac, (PCopyFunc)p_contact_copy);
|
|
||||||
|
|
||||||
assert_int_equals(2, g_slist_length(result));
|
assert_int_equals(2, g_slist_length(result));
|
||||||
|
|
||||||
p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_two_same_adds_one(void)
|
static void add_two_same_adds_one(void)
|
||||||
{
|
{
|
||||||
char *item1 = strdup("Hello");
|
char *item1 = strdup("Hello");
|
||||||
char *item2 = strdup("Hello");
|
char *item2 = strdup("Hello");
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
|
||||||
p_autocomplete_add(ac, item1, NULL, NULL);
|
p_autocomplete_add(ac, item1);
|
||||||
p_autocomplete_add(ac, item2, NULL, NULL);
|
p_autocomplete_add(ac, item2);
|
||||||
GSList *result = p_autocomplete_get_list(ac, NULL);
|
GSList *result = p_autocomplete_get_list(ac);
|
||||||
|
|
||||||
assert_int_equals(1, g_slist_length(result));
|
assert_int_equals(1, g_slist_length(result));
|
||||||
|
|
||||||
p_autocomplete_clear(ac, NULL);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_two_same_adds_one_with_funcs(void)
|
static void add_two_same_adds_one_with_funcs(void)
|
||||||
{
|
{
|
||||||
PContact contact1 = p_contact_new("James", "Online", "I'm here");
|
PContact contact1 = p_contact_new("James", "Online", "I'm here");
|
||||||
PContact contact2 = p_contact_new("James", "Away", "Out to lunch");
|
PContact contact2 = p_contact_new("James", "Away", "Out to lunch");
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name,
|
||||||
p_autocomplete_add(ac, contact1, (PStrFunc)p_contact_name,
|
(PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free);
|
||||||
(GDestroyNotify)p_contact_free);
|
p_autocomplete_add(ac, contact1);
|
||||||
p_autocomplete_add(ac, contact2, (PStrFunc)p_contact_name,
|
p_autocomplete_add(ac, contact2);
|
||||||
(GDestroyNotify)p_contact_free);
|
GSList *result = p_autocomplete_get_list(ac);
|
||||||
GSList *result = p_autocomplete_get_list(ac, (PCopyFunc)p_contact_copy);
|
|
||||||
|
|
||||||
assert_int_equals(1, g_slist_length(result));
|
assert_int_equals(1, g_slist_length(result));
|
||||||
|
|
||||||
p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_two_same_updates(void)
|
static void add_two_same_updates(void)
|
||||||
{
|
{
|
||||||
char *item1 = strdup("Hello");
|
char *item1 = strdup("Hello");
|
||||||
char *item2 = strdup("Hello");
|
char *item2 = strdup("Hello");
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
|
||||||
p_autocomplete_add(ac, item1, NULL, NULL);
|
p_autocomplete_add(ac, item1);
|
||||||
p_autocomplete_add(ac, item2, NULL, NULL);
|
p_autocomplete_add(ac, item2);
|
||||||
GSList *result = p_autocomplete_get_list(ac, NULL);
|
GSList *result = p_autocomplete_get_list(ac);
|
||||||
|
|
||||||
GSList *first = g_slist_nth(result, 0);
|
GSList *first = g_slist_nth(result, 0);
|
||||||
|
|
||||||
@ -215,19 +212,18 @@ static void add_two_same_updates(void)
|
|||||||
|
|
||||||
assert_string_equals("Hello", str);
|
assert_string_equals("Hello", str);
|
||||||
|
|
||||||
p_autocomplete_clear(ac, NULL);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_two_same_updates_with_funcs(void)
|
static void add_two_same_updates_with_funcs(void)
|
||||||
{
|
{
|
||||||
PContact contact1 = p_contact_new("James", "Online", "I'm here");
|
PContact contact1 = p_contact_new("James", "Online", "I'm here");
|
||||||
PContact contact2 = p_contact_new("James", "Away", "Out to lunch");
|
PContact contact2 = p_contact_new("James", "Away", "Out to lunch");
|
||||||
PAutocomplete ac = p_autocomplete_new();
|
PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name,
|
||||||
p_autocomplete_add(ac, contact1, (PStrFunc)p_contact_name,
|
(PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free);
|
||||||
(GDestroyNotify)p_contact_free);
|
p_autocomplete_add(ac, contact1);
|
||||||
p_autocomplete_add(ac, contact2, (PStrFunc)p_contact_name,
|
p_autocomplete_add(ac, contact2);
|
||||||
(GDestroyNotify)p_contact_free);
|
GSList *result = p_autocomplete_get_list(ac);
|
||||||
GSList *result = p_autocomplete_get_list(ac, (PCopyFunc)p_contact_copy);
|
|
||||||
|
|
||||||
GSList *first = g_slist_nth(result, 0);
|
GSList *first = g_slist_nth(result, 0);
|
||||||
PContact contact = first->data;
|
PContact contact = first->data;
|
||||||
@ -236,7 +232,7 @@ static void add_two_same_updates_with_funcs(void)
|
|||||||
assert_string_equals("Away", p_contact_show(contact));
|
assert_string_equals("Away", p_contact_show(contact));
|
||||||
assert_string_equals("Out to lunch", p_contact_status(contact));
|
assert_string_equals("Out to lunch", p_contact_status(contact));
|
||||||
|
|
||||||
p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
|
p_autocomplete_clear(ac);
|
||||||
}
|
}
|
||||||
|
|
||||||
void register_prof_autocomplete_tests(void)
|
void register_prof_autocomplete_tests(void)
|
||||||
|
Loading…
Reference in New Issue
Block a user