From 823e32655e2fc4810dee34ba1bdd5553bb383a42 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 3 Oct 2011 23:55:02 -0400 Subject: [PATCH] Add a few more tests This brings test coverage for glist.c and glist.c to the coveted 100% lines mark. --- glib/tests/list.c | 69 ++++++++++++++++++++++++++++++++++++++++++++-- glib/tests/slist.c | 53 ++++++++++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 3 deletions(-) diff --git a/glib/tests/list.c b/glib/tests/list.c index cf65f9387..648a70ee8 100644 --- a/glib/tests/list.c +++ b/glib/tests/list.c @@ -185,6 +185,15 @@ test_list_concat (void) g_assert (*((gint*) st->data) == i); } + list2 = g_list_concat (NULL, list1); + g_assert_cmpint (g_list_length (list2), ==, 10); + + list2 = g_list_concat (list1, NULL); + g_assert_cmpint (g_list_length (list2), ==, 10); + + list2 = g_list_concat (NULL, NULL); + g_assert (list2 == NULL); + g_list_free (list1); } @@ -235,9 +244,10 @@ test_list_remove_all (void) g_assert_cmpint (g_list_length (list), ==, 20); - for (i = 0; i < 10; i++) + for (i = 0; i < 5; i++) { - list = g_list_remove_all (list, &nums[i]); + list = g_list_remove_all (list, &nums[2 * i + 1]); + list = g_list_remove_all (list, &nums[8 - 2 * i]); } g_assert_cmpint (g_list_length (list), ==, 0); @@ -397,6 +407,59 @@ test_delete_link (void) g_list_free (l); } +static void +test_prepend (void) +{ + GList *l, *l2; + + l = NULL; + l = g_list_prepend (l, "c"); + l = g_list_prepend (l, "a"); + + g_assert (l->data == (gpointer)"a"); + g_assert (l->next->data == (gpointer)"c"); + g_assert (l->next->next == NULL); + + l2 = l->next; + l2 = g_list_prepend (l2, "b"); + g_assert (l2->prev == l); + + g_assert (l->data == (gpointer)"a"); + g_assert (l->next->data == (gpointer)"b"); + g_assert (l->next->next->data == (gpointer)"c"); + g_assert (l->next->next->next == NULL); + + g_list_free (l); +} + +static void +test_position (void) +{ + GList *l, *ll; + + l = NULL; + l = g_list_append (l, "a"); + l = g_list_append (l, "b"); + l = g_list_append (l, "c"); + + ll = g_list_find (l, "a"); + g_assert_cmpint (g_list_position (l, ll), ==, 0); + g_assert_cmpint (g_list_index (l, "a"), ==, 0); + ll = g_list_find (l, "b"); + g_assert_cmpint (g_list_position (l, ll), ==, 1); + g_assert_cmpint (g_list_index (l, "b"), ==, 1); + ll = g_list_find (l, "c"); + g_assert_cmpint (g_list_position (l, ll), ==, 2); + g_assert_cmpint (g_list_index (l, "c"), ==, 2); + + ll = g_list_append (NULL, "d"); + g_assert_cmpint (g_list_position (l, ll), ==, -1); + g_assert_cmpint (g_list_index (l, "d"), ==, -1); + + g_list_free (l); + g_list_free (ll); +} + int main (int argc, char *argv[]) { @@ -422,6 +485,8 @@ main (int argc, char *argv[]) g_test_add_func ("/list/free-full", test_free_full); g_test_add_func ("/list/copy", test_list_copy); g_test_add_func ("/list/delete-link", test_delete_link); + g_test_add_func ("/list/prepend", test_prepend); + g_test_add_func ("/list/position", test_position); return g_test_run (); } diff --git a/glib/tests/slist.c b/glib/tests/slist.c index 08bf17a3e..482d76abd 100644 --- a/glib/tests/slist.c +++ b/glib/tests/slist.c @@ -206,7 +206,8 @@ test_slist_remove_all (void) for (i = 0; i < 10; i++) { - slist = g_slist_remove_all (slist, &nums[i]); + slist = g_slist_remove_all (slist, &nums[2 * i + 1]); + slist = g_slist_remove_all (slist, &nums[8 - 2 * i]); } g_assert_cmpint (g_slist_length (slist), ==, 0); @@ -240,6 +241,26 @@ test_slist_insert (void) } g_slist_free (slist); + + slist = g_slist_insert (NULL, "a", 1); + g_assert (slist->data == (gpointer)"a"); + g_assert (slist->next == NULL); + g_slist_free (slist); + + slist = g_slist_append (NULL, "a"); + slist = g_slist_append (slist, "b"); + slist = g_slist_insert (slist, "c", 5); + + g_assert (slist->next->next->data == (gpointer)"c"); + g_assert (slist->next->next->next == NULL); + g_slist_free (slist); + + slist = g_slist_append (NULL, "a"); + slist = g_slist_insert_before (slist, slist, "b"); + g_assert (slist->data == (gpointer)"b"); + g_assert (slist->next->data == (gpointer)"a"); + g_assert (slist->next->next == NULL); + g_slist_free (slist); } static gint @@ -272,9 +293,38 @@ test_slist_position (void) g_assert_cmpint (g_slist_position (slist, st), ==, i); } + st = g_slist_find_custom (slist, GINT_TO_POINTER (1000), find_num); + g_assert (st == NULL); + g_slist_free (slist); } +static void +test_slist_concat (void) +{ + GSList *s1, *s2, *s; + + s1 = g_slist_append (NULL, "a"); + s2 = g_slist_append (NULL, "b"); + s = g_slist_concat (s1, s2); + g_assert (s->data == (gpointer)"a"); + g_assert (s->next->data == (gpointer)"b"); + g_assert (s->next->next == NULL); + g_slist_free (s); + + s1 = g_slist_append (NULL, "a"); + + s = g_slist_concat (NULL, s1); + g_assert_cmpint (g_slist_length (s), ==, 1); + s = g_slist_concat (s1, NULL); + g_assert_cmpint (g_slist_length (s), ==, 1); + + g_slist_free (s); + + s = g_slist_concat (NULL, NULL); + g_assert (s == NULL); +} + int main (int argc, char *argv[]) { @@ -296,6 +346,7 @@ main (int argc, char *argv[]) g_test_add_func ("/slist/remove-all", test_slist_remove_all); g_test_add_func ("/slist/insert", test_slist_insert); g_test_add_func ("/slist/position", test_slist_position); + g_test_add_func ("/slist/concat", test_slist_concat); return g_test_run (); }