From c4a1d69a0c7ac634e2d46ca0ad117bf4e789f671 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 12 Nov 2015 00:46:42 +0000 Subject: [PATCH 1/2] Keep count of group members --- src/roster_list.c | 95 +++++++++++++++---- tests/unittests/test_roster_list.c | 145 +++++++++++++++++++++++++++++ tests/unittests/test_roster_list.h | 5 + tests/unittests/unittests.c | 5 + 4 files changed, 234 insertions(+), 16 deletions(-) diff --git a/src/roster_list.c b/src/roster_list.c index 26776c68..0ba20107 100644 --- a/src/roster_list.c +++ b/src/roster_list.c @@ -55,6 +55,7 @@ static Autocomplete fulljid_ac; // groups static Autocomplete groups_ac; +GHashTable *group_count; // contacts, indexed on barejid static GHashTable *contacts; @@ -76,11 +77,11 @@ roster_clear(void) autocomplete_clear(fulljid_ac); autocomplete_clear(groups_ac); g_hash_table_destroy(contacts); - contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, - (GDestroyNotify)p_contact_free); + contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, (GDestroyNotify)p_contact_free); g_hash_table_destroy(name_to_barejid); - name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, - g_free); + name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + g_hash_table_destroy(group_count); + group_count = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); } gboolean @@ -179,10 +180,9 @@ roster_init(void) barejid_ac = autocomplete_new(); fulljid_ac = autocomplete_new(); groups_ac = autocomplete_new(); - contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, - (GDestroyNotify)p_contact_free); - name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, - g_free); + contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, (GDestroyNotify)p_contact_free); + name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + group_count = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); } void @@ -230,6 +230,23 @@ roster_remove(const char *const name, const char *const barejid) resources = g_list_next(resources); } g_list_free(resources); + + GSList *groups = p_contact_groups(contact); + GSList *curr = groups; + while (curr) { + gchar *group = curr->data; + if (g_hash_table_contains(group_count, group)) { + int count = GPOINTER_TO_INT(g_hash_table_lookup(group_count, group)); + count--; + if (count < 1) { + g_hash_table_remove(group_count, group); + autocomplete_remove(groups_ac, group); + } else { + g_hash_table_insert(group_count, strdup(group), GINT_TO_POINTER(count)); + } + } + curr = g_slist_next(curr); + } } // remove the contact @@ -253,14 +270,51 @@ roster_update(const char *const barejid, const char *const name, GSList *groups, } p_contact_set_name(contact, new_name); - p_contact_set_groups(contact, groups); _replace_name(current_name, new_name, barejid); - // add groups - while (groups) { - autocomplete_add(groups_ac, groups->data); - groups = g_slist_next(groups); + GSList *curr_new_group = groups; + while (curr_new_group) { + char *new_group = curr_new_group->data; + + // contact added to group + if (!p_contact_in_group(contact, new_group)) { + + // group doesn't yet exist + if (!g_hash_table_contains(group_count, new_group)) { + g_hash_table_insert(group_count, strdup(new_group), GINT_TO_POINTER(1)); + autocomplete_add(groups_ac, curr_new_group->data); + + // increment count + } else { + int count = GPOINTER_TO_INT(g_hash_table_lookup(group_count, new_group)); + g_hash_table_insert(group_count, strdup(new_group), GINT_TO_POINTER(count + 1)); + } + } + curr_new_group = g_slist_next(curr_new_group); } + + GSList *old_groups = p_contact_groups(contact); + GSList *curr_old_group = old_groups; + while (curr_old_group) { + char *old_group = curr_old_group->data; + // removed from group + if (!g_slist_find_custom(groups, old_group, (GCompareFunc)g_strcmp0)) { + if (g_hash_table_contains(group_count, old_group)) { + int count = GPOINTER_TO_INT(g_hash_table_lookup(group_count, old_group)); + count--; + if (count < 1) { + g_hash_table_remove(group_count, old_group); + autocomplete_remove(groups_ac, old_group); + } else { + g_hash_table_insert(group_count, strdup(old_group), GINT_TO_POINTER(count)); + } + } + } + + curr_old_group = g_slist_next(curr_old_group); + } + + p_contact_set_groups(contact, groups); } gboolean @@ -276,9 +330,18 @@ roster_add(const char *const barejid, const char *const name, GSList *groups, co pending_out); // add groups - while (groups) { - autocomplete_add(groups_ac, groups->data); - groups = g_slist_next(groups); + GSList *curr_new_group = groups; + while (curr_new_group) { + char *new_group = curr_new_group->data; + if (g_hash_table_contains(group_count, new_group)) { + int count = GPOINTER_TO_INT(g_hash_table_lookup(group_count, new_group)); + g_hash_table_insert(group_count, strdup(new_group), GINT_TO_POINTER(count + 1)); + } else { + g_hash_table_insert(group_count, strdup(new_group), GINT_TO_POINTER(1)); + autocomplete_add(groups_ac, new_group); + } + + curr_new_group = g_slist_next(curr_new_group); } g_hash_table_insert(contacts, strdup(barejid), contact); diff --git a/tests/unittests/test_roster_list.c b/tests/unittests/test_roster_list.c index 41ccb8cf..5b626e74 100644 --- a/tests/unittests/test_roster_list.c +++ b/tests/unittests/test_roster_list.c @@ -14,6 +14,7 @@ void empty_list_when_none_added(void **state) roster_init(); GSList *list = roster_get_contacts(); assert_null(list); + roster_clear(); roster_free(); } @@ -23,6 +24,7 @@ void contains_one_element(void **state) roster_add("James", NULL, NULL, NULL, FALSE); GSList *list = roster_get_contacts(); assert_int_equal(1, g_slist_length(list)); + roster_clear(); roster_free(); } @@ -34,6 +36,7 @@ void first_element_correct(void **state) PContact james = list->data; assert_string_equal("James", p_contact_barejid(james)); + roster_clear(); roster_free(); } @@ -45,6 +48,7 @@ void contains_two_elements(void **state) GSList *list = roster_get_contacts(); assert_int_equal(2, g_slist_length(list)); + roster_clear(); roster_free(); } @@ -60,6 +64,7 @@ void first_and_second_elements_correct(void **state) assert_string_equal("Dave", p_contact_barejid(first)); assert_string_equal("James", p_contact_barejid(second)); + roster_clear(); roster_free(); } @@ -72,6 +77,7 @@ void contains_three_elements(void **state) GSList *list = roster_get_contacts(); assert_int_equal(3, g_slist_length(list)); + roster_clear(); roster_free(); } @@ -89,6 +95,7 @@ void first_three_elements_correct(void **state) assert_string_equal("James", p_contact_barejid(james)); assert_string_equal("Dave", p_contact_barejid(dave)); assert_string_equal("Bob", p_contact_barejid(bob)); + roster_clear(); roster_free(); } @@ -108,6 +115,7 @@ void add_twice_at_beginning_adds_once(void **state) assert_string_equal("Bob", p_contact_barejid(first)); assert_string_equal("Dave", p_contact_barejid(second)); assert_string_equal("James", p_contact_barejid(third)); + roster_clear(); roster_free(); } @@ -127,6 +135,7 @@ void add_twice_in_middle_adds_once(void **state) assert_string_equal("Bob", p_contact_barejid(first)); assert_string_equal("Dave", p_contact_barejid(second)); assert_string_equal("James", p_contact_barejid(third)); + roster_clear(); roster_free(); } @@ -146,6 +155,7 @@ void add_twice_at_end_adds_once(void **state) assert_string_equal("Bob", p_contact_barejid(first)); assert_string_equal("Dave", p_contact_barejid(second)); assert_string_equal("James", p_contact_barejid(third)); + roster_clear(); roster_free(); } @@ -162,6 +172,7 @@ void find_first_exists(void **state) assert_string_equal("Bob", result); free(result); free(search); + roster_clear(); roster_free(); } @@ -175,6 +186,7 @@ void find_second_exists(void **state) char *result = roster_contact_autocomplete("Dav"); assert_string_equal("Dave", result); free(result); + roster_clear(); roster_free(); } @@ -188,6 +200,7 @@ void find_third_exists(void **state) char *result = roster_contact_autocomplete("Ja"); assert_string_equal("James", result); free(result); + roster_clear(); roster_free(); } @@ -200,6 +213,7 @@ void find_returns_null(void **state) char *result = roster_contact_autocomplete("Mike"); assert_null(result); + roster_clear(); roster_free(); } @@ -208,6 +222,7 @@ void find_on_empty_returns_null(void **state) roster_init(); char *result = roster_contact_autocomplete("James"); assert_null(result); + roster_clear(); roster_free(); } @@ -223,6 +238,7 @@ void find_twice_returns_second_when_two_match(void **state) assert_string_equal("Jamie", result2); free(result1); free(result2); + roster_clear(); roster_free(); } @@ -251,6 +267,7 @@ void find_five_times_finds_fifth(void **state) free(result3); free(result4); free(result5); + roster_clear(); roster_free(); } @@ -267,5 +284,133 @@ void find_twice_returns_first_when_two_match_and_reset(void **state) assert_string_equal("James", result2); free(result1); free(result2); + roster_clear(); + roster_free(); +} + +void add_contact_with_no_group_returns_no_groups(void **state) +{ + roster_init(); + roster_add("person@server.org", NULL, NULL, NULL, FALSE); + + GSList *groups_res = roster_get_groups(); + assert_int_equal(g_slist_length(groups_res), 0); + + g_slist_free_full(groups_res, g_free); + roster_clear(); + roster_free(); +} + +void add_contact_with_group_returns_group(void **state) +{ + roster_init(); + + GSList *groups = NULL; + groups = g_slist_append(groups, strdup("friends")); + roster_add("person@server.org", NULL, groups, NULL, FALSE); + + GSList *groups_res = roster_get_groups(); + assert_int_equal(g_slist_length(groups_res), 1); + + GSList *found = g_slist_find_custom(groups_res, "friends", g_strcmp0); + assert_true(found != NULL); + assert_string_equal(found->data, "friends"); + + g_slist_free_full(groups_res, g_free); + roster_clear(); + roster_free(); +} + +void add_contact_with_two_groups_returns_groups(void **state) +{ + roster_init(); + + GSList *groups = NULL; + groups = g_slist_append(groups, strdup("friends")); + groups = g_slist_append(groups, strdup("work")); + roster_add("person@server.org", NULL, groups, NULL, FALSE); + + GSList *groups_res = roster_get_groups(); + assert_int_equal(g_slist_length(groups_res), 2); + + GSList *found = g_slist_find_custom(groups_res, "friends", g_strcmp0); + assert_true(found != NULL); + assert_string_equal(found->data, "friends"); + found = g_slist_find_custom(groups_res, "work", g_strcmp0); + assert_true(found != NULL); + assert_string_equal(found->data, "work"); + + g_slist_free_full(groups_res, g_free); + roster_clear(); + roster_free(); +} + +void add_contact_with_three_groups_returns_groups(void **state) +{ + roster_init(); + + GSList *groups = NULL; + groups = g_slist_append(groups, strdup("friends")); + groups = g_slist_append(groups, strdup("work")); + groups = g_slist_append(groups, strdup("stuff")); + roster_add("person@server.org", NULL, groups, NULL, FALSE); + + GSList *groups_res = roster_get_groups(); + assert_int_equal(g_slist_length(groups_res), 3); + + GSList *found = g_slist_find_custom(groups_res, "friends", g_strcmp0); + assert_true(found != NULL); + assert_string_equal(found->data, "friends"); + found = g_slist_find_custom(groups_res, "work", g_strcmp0); + assert_true(found != NULL); + assert_string_equal(found->data, "work"); + found = g_slist_find_custom(groups_res, "stuff", g_strcmp0); + assert_true(found != NULL); + assert_string_equal(found->data, "stuff"); + + g_slist_free_full(groups_res, g_free); + roster_clear(); + roster_free(); +} + +void add_contact_with_three_groups_update_adding_two_returns_groups(void **state) +{ + roster_init(); + + GSList *groups1 = NULL; + groups1 = g_slist_append(groups1, strdup("friends")); + groups1 = g_slist_append(groups1, strdup("work")); + groups1 = g_slist_append(groups1, strdup("stuff")); + roster_add("person@server.org", NULL, groups1, NULL, FALSE); + + GSList *groups2 = NULL; + groups2 = g_slist_append(groups2, strdup("friends")); + groups2 = g_slist_append(groups2, strdup("work")); + groups2 = g_slist_append(groups2, strdup("stuff")); + groups2 = g_slist_append(groups2, strdup("things")); + groups2 = g_slist_append(groups2, strdup("people")); + roster_update("person@server.org", NULL, groups2, NULL, FALSE); + + GSList *groups_res = roster_get_groups(); + assert_int_equal(g_slist_length(groups_res), 5); + + GSList *found = g_slist_find_custom(groups_res, "friends", g_strcmp0); + assert_true(found != NULL); + assert_string_equal(found->data, "friends"); + found = g_slist_find_custom(groups_res, "work", g_strcmp0); + assert_true(found != NULL); + assert_string_equal(found->data, "work"); + found = g_slist_find_custom(groups_res, "stuff", g_strcmp0); + assert_true(found != NULL); + assert_string_equal(found->data, "stuff"); + found = g_slist_find_custom(groups_res, "things", g_strcmp0); + assert_true(found != NULL); + assert_string_equal(found->data, "things"); + found = g_slist_find_custom(groups_res, "people", g_strcmp0); + assert_true(found != NULL); + assert_string_equal(found->data, "people"); + + g_slist_free_full(groups_res, g_free); + roster_clear(); roster_free(); } diff --git a/tests/unittests/test_roster_list.h b/tests/unittests/test_roster_list.h index 080bca9f..d0da560a 100644 --- a/tests/unittests/test_roster_list.h +++ b/tests/unittests/test_roster_list.h @@ -16,3 +16,8 @@ void find_on_empty_returns_null(void **state); void find_twice_returns_second_when_two_match(void **state); void find_five_times_finds_fifth(void **state); void find_twice_returns_first_when_two_match_and_reset(void **state); +void add_contact_with_no_group_returns_no_groups(void **state); +void add_contact_with_group_returns_group(void **state); +void add_contact_with_two_groups_returns_groups(void **state); +void add_contact_with_three_groups_returns_groups(void **state); +void add_contact_with_three_groups_update_adding_two_returns_groups(void **state); diff --git a/tests/unittests/unittests.c b/tests/unittests/unittests.c index 83aee297..55008b9c 100644 --- a/tests/unittests/unittests.c +++ b/tests/unittests/unittests.c @@ -210,6 +210,11 @@ int main(int argc, char* argv[]) { unit_test(find_twice_returns_second_when_two_match), unit_test(find_five_times_finds_fifth), unit_test(find_twice_returns_first_when_two_match_and_reset), + unit_test(add_contact_with_no_group_returns_no_groups), + unit_test(add_contact_with_group_returns_group), + unit_test(add_contact_with_two_groups_returns_groups), + unit_test(add_contact_with_three_groups_returns_groups), + unit_test(add_contact_with_three_groups_update_adding_two_returns_groups), unit_test_setup_teardown(returns_false_when_chat_session_does_not_exist, init_chat_sessions, From c60060fe5a7203daa72f4807c17d5dd86f780802 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 14 Nov 2015 22:21:57 +0000 Subject: [PATCH 2/2] Added more group tests --- tests/unittests/test_roster_list.c | 265 ++++++++++++++++++++++++++++- tests/unittests/test_roster_list.h | 19 ++- tests/unittests/unittests.c | 19 ++- 3 files changed, 288 insertions(+), 15 deletions(-) diff --git a/tests/unittests/test_roster_list.c b/tests/unittests/test_roster_list.c index 5b626e74..f12361e4 100644 --- a/tests/unittests/test_roster_list.c +++ b/tests/unittests/test_roster_list.c @@ -288,7 +288,7 @@ void find_twice_returns_first_when_two_match_and_reset(void **state) roster_free(); } -void add_contact_with_no_group_returns_no_groups(void **state) +void add_contact_with_no_group(void **state) { roster_init(); roster_add("person@server.org", NULL, NULL, NULL, FALSE); @@ -301,7 +301,7 @@ void add_contact_with_no_group_returns_no_groups(void **state) roster_free(); } -void add_contact_with_group_returns_group(void **state) +void add_contact_with_group(void **state) { roster_init(); @@ -321,7 +321,7 @@ void add_contact_with_group_returns_group(void **state) roster_free(); } -void add_contact_with_two_groups_returns_groups(void **state) +void add_contact_with_two_groups(void **state) { roster_init(); @@ -345,7 +345,7 @@ void add_contact_with_two_groups_returns_groups(void **state) roster_free(); } -void add_contact_with_three_groups_returns_groups(void **state) +void add_contact_with_three_groups(void **state) { roster_init(); @@ -373,7 +373,7 @@ void add_contact_with_three_groups_returns_groups(void **state) roster_free(); } -void add_contact_with_three_groups_update_adding_two_returns_groups(void **state) +void add_contact_with_three_groups_update_adding_two(void **state) { roster_init(); @@ -414,3 +414,258 @@ void add_contact_with_three_groups_update_adding_two_returns_groups(void **state roster_clear(); roster_free(); } + +void add_contact_with_three_groups_update_removing_one(void **state) +{ + roster_init(); + + GSList *groups1 = NULL; + groups1 = g_slist_append(groups1, strdup("friends")); + groups1 = g_slist_append(groups1, strdup("work")); + groups1 = g_slist_append(groups1, strdup("stuff")); + roster_add("person@server.org", NULL, groups1, NULL, FALSE); + + GSList *groups2 = NULL; + groups2 = g_slist_append(groups2, strdup("friends")); + groups2 = g_slist_append(groups2, strdup("stuff")); + roster_update("person@server.org", NULL, groups2, NULL, FALSE); + + GSList *groups_res = roster_get_groups(); + assert_int_equal(g_slist_length(groups_res), 2); + + GSList *found = g_slist_find_custom(groups_res, "friends", g_strcmp0); + assert_true(found != NULL); + assert_string_equal(found->data, "friends"); + found = g_slist_find_custom(groups_res, "stuff", g_strcmp0); + assert_true(found != NULL); + assert_string_equal(found->data, "stuff"); + + g_slist_free_full(groups_res, g_free); + roster_clear(); + roster_free(); +} + +void add_contact_with_three_groups_update_removing_two(void **state) +{ + roster_init(); + + GSList *groups1 = NULL; + groups1 = g_slist_append(groups1, strdup("friends")); + groups1 = g_slist_append(groups1, strdup("work")); + groups1 = g_slist_append(groups1, strdup("stuff")); + roster_add("person@server.org", NULL, groups1, NULL, FALSE); + + GSList *groups2 = NULL; + groups2 = g_slist_append(groups2, strdup("stuff")); + roster_update("person@server.org", NULL, groups2, NULL, FALSE); + + GSList *groups_res = roster_get_groups(); + assert_int_equal(g_slist_length(groups_res), 1); + + GSList *found = g_slist_find_custom(groups_res, "stuff", g_strcmp0); + assert_true(found != NULL); + assert_string_equal(found->data, "stuff"); + + g_slist_free_full(groups_res, g_free); + roster_clear(); + roster_free(); +} + +void add_contact_with_three_groups_update_removing_three(void **state) +{ + roster_init(); + + GSList *groups1 = NULL; + groups1 = g_slist_append(groups1, strdup("friends")); + groups1 = g_slist_append(groups1, strdup("work")); + groups1 = g_slist_append(groups1, strdup("stuff")); + roster_add("person@server.org", NULL, groups1, NULL, FALSE); + + roster_update("person@server.org", NULL, NULL, NULL, FALSE); + + GSList *groups_res = roster_get_groups(); + assert_int_equal(g_slist_length(groups_res), 0); + + g_slist_free_full(groups_res, g_free); + roster_clear(); + roster_free(); +} + +void add_contact_with_three_groups_update_two_new(void **state) +{ + roster_init(); + + GSList *groups1 = NULL; + groups1 = g_slist_append(groups1, strdup("friends")); + groups1 = g_slist_append(groups1, strdup("work")); + groups1 = g_slist_append(groups1, strdup("stuff")); + roster_add("person@server.org", NULL, groups1, NULL, FALSE); + + GSList *groups2 = NULL; + groups2 = g_slist_append(groups2, strdup("newfriends")); + groups2 = g_slist_append(groups2, strdup("somepeople")); + roster_update("person@server.org", NULL, groups2, NULL, FALSE); + + GSList *groups_res = roster_get_groups(); + assert_int_equal(g_slist_length(groups_res), 2); + + GSList *found = g_slist_find_custom(groups_res, "newfriends", g_strcmp0); + assert_true(found != NULL); + found = g_slist_find_custom(groups_res, "somepeople", g_strcmp0); + assert_true(found != NULL); + + g_slist_free_full(groups_res, g_free); + roster_clear(); + roster_free(); +} + +void add_remove_contact_groups(void **state) +{ + roster_init(); + + GSList *groups1 = NULL; + groups1 = g_slist_append(groups1, strdup("friends")); + groups1 = g_slist_append(groups1, strdup("work")); + groups1 = g_slist_append(groups1, strdup("stuff")); + roster_add("person@server.org", NULL, groups1, NULL, FALSE); + + roster_remove("person@server.org", "person@server.org"); + + GSList *groups_res = roster_get_groups(); + assert_int_equal(g_slist_length(groups_res), 0); + + g_slist_free_full(groups_res, g_free); + roster_clear(); + roster_free(); +} + +void add_contacts_with_different_groups(void **state) +{ + roster_init(); + + GSList *groups1 = NULL; + groups1 = g_slist_append(groups1, strdup("friends")); + groups1 = g_slist_append(groups1, strdup("work")); + groups1 = g_slist_append(groups1, strdup("stuff")); + roster_add("person@server.org", NULL, groups1, NULL, FALSE); + + GSList *groups2 = NULL; + groups2 = g_slist_append(groups2, strdup("newfriends")); + groups2 = g_slist_append(groups2, strdup("somepeople")); + roster_add("bob@server.org", NULL, groups2, NULL, FALSE); + + GSList *groups_res = roster_get_groups(); + assert_int_equal(g_slist_length(groups_res), 5); + + GSList *found = g_slist_find_custom(groups_res, "friends", g_strcmp0); + assert_true(found != NULL); + found = g_slist_find_custom(groups_res, "work", g_strcmp0); + assert_true(found != NULL); + found = g_slist_find_custom(groups_res, "stuff", g_strcmp0); + assert_true(found != NULL); + found = g_slist_find_custom(groups_res, "newfriends", g_strcmp0); + assert_true(found != NULL); + found = g_slist_find_custom(groups_res, "somepeople", g_strcmp0); + assert_true(found != NULL); + + g_slist_free_full(groups_res, g_free); + roster_clear(); + roster_free(); +} + +void add_contacts_with_same_groups(void **state) +{ + roster_init(); + + GSList *groups1 = NULL; + groups1 = g_slist_append(groups1, strdup("friends")); + groups1 = g_slist_append(groups1, strdup("work")); + groups1 = g_slist_append(groups1, strdup("stuff")); + roster_add("person@server.org", NULL, groups1, NULL, FALSE); + + GSList *groups2 = NULL; + groups2 = g_slist_append(groups2, strdup("friends")); + groups2 = g_slist_append(groups2, strdup("work")); + groups2 = g_slist_append(groups2, strdup("stuff")); + roster_add("bob@server.org", NULL, groups2, NULL, FALSE); + + GSList *groups_res = roster_get_groups(); + assert_int_equal(g_slist_length(groups_res), 3); + + GSList *found = g_slist_find_custom(groups_res, "friends", g_strcmp0); + assert_true(found != NULL); + found = g_slist_find_custom(groups_res, "work", g_strcmp0); + assert_true(found != NULL); + found = g_slist_find_custom(groups_res, "stuff", g_strcmp0); + assert_true(found != NULL); + + g_slist_free_full(groups_res, g_free); + roster_clear(); + roster_free(); +} + +void add_contacts_with_overlapping_groups(void **state) +{ + roster_init(); + + GSList *groups1 = NULL; + groups1 = g_slist_append(groups1, strdup("friends")); + groups1 = g_slist_append(groups1, strdup("work")); + groups1 = g_slist_append(groups1, strdup("stuff")); + roster_add("person@server.org", NULL, groups1, NULL, FALSE); + + GSList *groups2 = NULL; + groups2 = g_slist_append(groups2, strdup("friends")); + groups2 = g_slist_append(groups2, strdup("work")); + groups2 = g_slist_append(groups2, strdup("different")); + roster_add("bob@server.org", NULL, groups2, NULL, FALSE); + + GSList *groups_res = roster_get_groups(); + assert_int_equal(g_slist_length(groups_res), 4); + + GSList *found = g_slist_find_custom(groups_res, "friends", g_strcmp0); + assert_true(found != NULL); + found = g_slist_find_custom(groups_res, "work", g_strcmp0); + assert_true(found != NULL); + found = g_slist_find_custom(groups_res, "stuff", g_strcmp0); + assert_true(found != NULL); + found = g_slist_find_custom(groups_res, "different", g_strcmp0); + assert_true(found != NULL); + + g_slist_free_full(groups_res, g_free); + roster_clear(); + roster_free(); +} + +void remove_contact_with_remaining_in_group(void **state) +{ + roster_init(); + + GSList *groups1 = NULL; + groups1 = g_slist_append(groups1, strdup("friends")); + groups1 = g_slist_append(groups1, strdup("work")); + groups1 = g_slist_append(groups1, strdup("stuff")); + roster_add("person@server.org", NULL, groups1, NULL, FALSE); + + GSList *groups2 = NULL; + groups2 = g_slist_append(groups2, strdup("friends")); + groups2 = g_slist_append(groups2, strdup("work")); + groups2 = g_slist_append(groups2, strdup("different")); + roster_add("bob@server.org", NULL, groups2, NULL, FALSE); + + roster_remove("bob@server.org", "bob@server.org"); + + GSList *groups_res = roster_get_groups(); + assert_int_equal(g_slist_length(groups_res), 3); + + GSList *found = g_slist_find_custom(groups_res, "friends", g_strcmp0); + assert_true(found != NULL); + found = g_slist_find_custom(groups_res, "work", g_strcmp0); + assert_true(found != NULL); + found = g_slist_find_custom(groups_res, "stuff", g_strcmp0); + assert_true(found != NULL); + + g_slist_free_full(groups_res, g_free); + roster_clear(); + roster_free(); +} diff --git a/tests/unittests/test_roster_list.h b/tests/unittests/test_roster_list.h index d0da560a..464ecd55 100644 --- a/tests/unittests/test_roster_list.h +++ b/tests/unittests/test_roster_list.h @@ -16,8 +16,17 @@ void find_on_empty_returns_null(void **state); void find_twice_returns_second_when_two_match(void **state); void find_five_times_finds_fifth(void **state); void find_twice_returns_first_when_two_match_and_reset(void **state); -void add_contact_with_no_group_returns_no_groups(void **state); -void add_contact_with_group_returns_group(void **state); -void add_contact_with_two_groups_returns_groups(void **state); -void add_contact_with_three_groups_returns_groups(void **state); -void add_contact_with_three_groups_update_adding_two_returns_groups(void **state); +void add_contact_with_no_group(void **state); +void add_contact_with_group(void **state); +void add_contact_with_two_groups(void **state); +void add_contact_with_three_groups(void **state); +void add_contact_with_three_groups_update_adding_two(void **state); +void add_contact_with_three_groups_update_removing_one(void **state); +void add_contact_with_three_groups_update_removing_two(void **state); +void add_contact_with_three_groups_update_removing_three(void **state); +void add_contact_with_three_groups_update_two_new(void **state); +void add_remove_contact_groups(void **state); +void add_contacts_with_different_groups(void **state); +void add_contacts_with_same_groups(void **state); +void add_contacts_with_overlapping_groups(void **state); +void remove_contact_with_remaining_in_group(void **state); diff --git a/tests/unittests/unittests.c b/tests/unittests/unittests.c index 55008b9c..e511df9d 100644 --- a/tests/unittests/unittests.c +++ b/tests/unittests/unittests.c @@ -210,11 +210,20 @@ int main(int argc, char* argv[]) { unit_test(find_twice_returns_second_when_two_match), unit_test(find_five_times_finds_fifth), unit_test(find_twice_returns_first_when_two_match_and_reset), - unit_test(add_contact_with_no_group_returns_no_groups), - unit_test(add_contact_with_group_returns_group), - unit_test(add_contact_with_two_groups_returns_groups), - unit_test(add_contact_with_three_groups_returns_groups), - unit_test(add_contact_with_three_groups_update_adding_two_returns_groups), + unit_test(add_contact_with_no_group), + unit_test(add_contact_with_group), + unit_test(add_contact_with_two_groups), + unit_test(add_contact_with_three_groups), + unit_test(add_contact_with_three_groups_update_adding_two), + unit_test(add_contact_with_three_groups_update_removing_one), + unit_test(add_contact_with_three_groups_update_removing_two), + unit_test(add_contact_with_three_groups_update_removing_three), + unit_test(add_contact_with_three_groups_update_two_new), + unit_test(add_remove_contact_groups), + unit_test(add_contacts_with_different_groups), + unit_test(add_contacts_with_same_groups), + unit_test(add_contacts_with_overlapping_groups), + unit_test(remove_contact_with_remaining_in_group), unit_test_setup_teardown(returns_false_when_chat_session_does_not_exist, init_chat_sessions,