about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--prof_autocomplete.c14
-rw-r--r--test_prof_autocomplete.c112
2 files changed, 123 insertions, 3 deletions
diff --git a/prof_autocomplete.c b/prof_autocomplete.c
index aed720e1..ec6b4e23 100644
--- a/prof_autocomplete.c
+++ b/prof_autocomplete.c
@@ -36,6 +36,7 @@ struct p_autocomplete_t {
 
 static gchar * _search_from(PAutocomplete ac, GSList *curr,  PStrFunc str_func);
 static const char *_str_func_default(const char *orig);
+static const char *_copy_func_default(const char *orig);
 
 PAutocomplete p_autocomplete_new(void)
 {
@@ -49,6 +50,9 @@ PAutocomplete p_autocomplete_new(void)
 
 void p_autocomplete_clear(PAutocomplete ac, GDestroyNotify free_func)
 {
+    if (free_func == NULL)
+        free_func = (GDestroyNotify)free;
+
     g_slist_free_full(ac->items, free_func);
     ac->items = NULL;
 
@@ -69,6 +73,8 @@ void p_autocomplete_add(PAutocomplete ac, void *item, PStrFunc str_func,
 {
     if (str_func == NULL)
         str_func = (PStrFunc)_str_func_default;
+    if (free_func == NULL)
+        free_func = (GDestroyNotify)free;
 
     if (ac->items == NULL) {
         ac->items = g_slist_append(ac->items, item);
@@ -135,6 +141,9 @@ void p_autocomplete_remove(PAutocomplete ac, const char * const item,
 
 GSList * p_autocomplete_get_list(PAutocomplete ac, PCopyFunc copy_func)
 {
+    if (copy_func == NULL)
+        copy_func = (PCopyFunc)_copy_func_default;
+
     GSList *copy = NULL;
     GSList *curr = ac->items;
 
@@ -215,3 +224,8 @@ static const char *_str_func_default(const char *orig)
     return orig;
 }
 
+static const char *_copy_func_default(const char *orig)
+{
+    return strdup(orig);
+}
+
diff --git a/test_prof_autocomplete.c b/test_prof_autocomplete.c
index b03d8bb8..ef867ff7 100644
--- a/test_prof_autocomplete.c
+++ b/test_prof_autocomplete.c
@@ -1,3 +1,4 @@
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <head-unit.h>
@@ -61,7 +62,7 @@ static void add_one_and_complete(void)
 
     assert_string_equals("Hello", result);
 
-    p_autocomplete_clear(ac, (GDestroyNotify)free);
+    p_autocomplete_clear(ac, NULL);
 }
 
 static void add_one_and_complete_with_funcs(void)
@@ -88,7 +89,7 @@ static void add_two_and_complete_returns_first(void)
 
     assert_string_equals("Hello", result);
     
-    p_autocomplete_clear(ac, (GDestroyNotify)free);
+    p_autocomplete_clear(ac, NULL);
 }
 
 static void add_two_and_complete_returns_first_with_funcs(void)
@@ -119,7 +120,7 @@ static void add_two_and_complete_returns_second(void)
 
     assert_string_equals("Help", result2);
     
-    p_autocomplete_clear(ac, (GDestroyNotify)free);
+    p_autocomplete_clear(ac, NULL);
 }
 
 static void add_two_and_complete_returns_second_with_funcs(void)
@@ -139,6 +140,105 @@ static void add_two_and_complete_returns_second_with_funcs(void)
     p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
 }
 
+static void add_two_adds_two(void)
+{
+    char *item1 = strdup("Hello");
+    char *item2 = strdup("Help");
+    PAutocomplete ac = p_autocomplete_new();
+    p_autocomplete_add(ac, item1, NULL, NULL);
+    p_autocomplete_add(ac, item2, NULL, NULL);
+    GSList *result = p_autocomplete_get_list(ac, NULL);
+
+    assert_int_equals(2, g_slist_length(result));
+
+    p_autocomplete_clear(ac, NULL);
+}
+
+static void add_two_adds_two_with_funcs(void)
+{
+    PContact contact1 = p_contact_new("James", "Online", "I'm here");
+    PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch");
+    PAutocomplete ac = p_autocomplete_new();
+    p_autocomplete_add(ac, contact1, (PStrFunc)p_contact_name,
+        (GDestroyNotify)p_contact_free);
+    p_autocomplete_add(ac, contact2, (PStrFunc)p_contact_name,
+        (GDestroyNotify)p_contact_free);
+    GSList *result = p_autocomplete_get_list(ac, (PCopyFunc)p_contact_copy);
+
+    assert_int_equals(2, g_slist_length(result));
+    
+    p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
+}
+
+static void add_two_same_adds_one(void)
+{
+    char *item1 = strdup("Hello");
+    char *item2 = strdup("Hello");
+    PAutocomplete ac = p_autocomplete_new();
+    p_autocomplete_add(ac, item1, NULL, NULL);
+    p_autocomplete_add(ac, item2, NULL, NULL);
+    GSList *result = p_autocomplete_get_list(ac, NULL);
+
+    assert_int_equals(1, g_slist_length(result));
+
+    p_autocomplete_clear(ac, NULL);
+}
+
+static void add_two_same_adds_one_with_funcs(void)
+{
+    PContact contact1 = p_contact_new("James", "Online", "I'm here");
+    PContact contact2 = p_contact_new("James", "Away", "Out to lunch");
+    PAutocomplete ac = p_autocomplete_new();
+    p_autocomplete_add(ac, contact1, (PStrFunc)p_contact_name,
+        (GDestroyNotify)p_contact_free);
+    p_autocomplete_add(ac, contact2, (PStrFunc)p_contact_name,
+        (GDestroyNotify)p_contact_free);
+    GSList *result = p_autocomplete_get_list(ac, (PCopyFunc)p_contact_copy);
+
+    assert_int_equals(1, g_slist_length(result));
+    
+    p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
+}
+
+static void add_two_same_updates(void)
+{
+    char *item1 = strdup("Hello");
+    char *item2 = strdup("Hello");
+    PAutocomplete ac = p_autocomplete_new();
+    p_autocomplete_add(ac, item1, NULL, NULL);
+    p_autocomplete_add(ac, item2, NULL, NULL);
+    GSList *result = p_autocomplete_get_list(ac, NULL);
+
+    GSList *first = g_slist_nth(result, 0);
+
+    char *str = first->data;
+
+    assert_string_equals("Hello", str);
+
+    p_autocomplete_clear(ac, NULL);
+}
+
+static void add_two_same_updates_with_funcs(void)
+{
+    PContact contact1 = p_contact_new("James", "Online", "I'm here");
+    PContact contact2 = p_contact_new("James", "Away", "Out to lunch");
+    PAutocomplete ac = p_autocomplete_new();
+    p_autocomplete_add(ac, contact1, (PStrFunc)p_contact_name,
+        (GDestroyNotify)p_contact_free);
+    p_autocomplete_add(ac, contact2, (PStrFunc)p_contact_name,
+        (GDestroyNotify)p_contact_free);
+    GSList *result = p_autocomplete_get_list(ac, (PCopyFunc)p_contact_copy);
+
+    GSList *first = g_slist_nth(result, 0);
+    PContact contact = first->data;
+
+    assert_string_equals("James", p_contact_name(contact));
+    assert_string_equals("Away", p_contact_show(contact));
+    assert_string_equals("Out to lunch", p_contact_status(contact));
+
+    p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
+}
+
 void register_prof_autocomplete_tests(void)
 {
     TEST_MODULE("prof_autocomplete tests");
@@ -154,4 +254,10 @@ void register_prof_autocomplete_tests(void)
     TEST(add_two_and_complete_returns_first_with_funcs);
     TEST(add_two_and_complete_returns_second);
     TEST(add_two_and_complete_returns_second_with_funcs);
+    TEST(add_two_adds_two);
+    TEST(add_two_adds_two_with_funcs);
+    TEST(add_two_same_adds_one);
+    TEST(add_two_same_adds_one_with_funcs);
+    TEST(add_two_same_updates);
+    TEST(add_two_same_updates_with_funcs);
 }