From 123ba622d3511509f7cce9459ab48db8a0d28c3d Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 5 Jul 2010 22:40:46 -0400 Subject: [PATCH] Add more list tests --- glib/tests/list.c | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++ glib/tests/slist.c | 122 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 264 insertions(+) diff --git a/glib/tests/list.c b/glib/tests/list.c index 15b46ee..05bc8e4 100644 --- a/glib/tests/list.c +++ b/glib/tests/list.c @@ -157,6 +157,143 @@ test_list_nth (void) g_list_free (list); } +static void +test_list_concat (void) +{ + GList *list1 = NULL; + GList *list2 = NULL; + GList *st; + gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + gint i; + + for (i = 0; i < 5; i++) + { + list1 = g_list_append (list1, &nums[i]); + list2 = g_list_append (list2, &nums[i+5]); + } + + g_assert_cmpint (g_list_length (list1), ==, 5); + g_assert_cmpint (g_list_length (list2), ==, 5); + + list1 = g_list_concat (list1, list2); + + g_assert_cmpint (g_list_length (list1), ==, 10); + + for (i = 0; i < 10; i++) + { + st = g_list_nth (list1, i); + g_assert (*((gint*) st->data) == i); + } + + g_list_free (list1); +} + +static void +test_list_remove (void) +{ + GList *list = NULL; + GList *st; + gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + gint i; + + for (i = 0; i < 10; i++) + { + list = g_list_append (list, &nums[i]); + list = g_list_append (list, &nums[i]); + } + + g_assert_cmpint (g_list_length (list), ==, 20); + + for (i = 0; i < 10; i++) + { + list = g_list_remove (list, &nums[i]); + } + + g_assert_cmpint (g_list_length (list), ==, 10); + + for (i = 0; i < 10; i++) + { + st = g_list_nth (list, i); + g_assert (*((gint*) st->data) == i); + } + + g_list_free (list); +} + +static void +test_list_remove_all (void) +{ + GList *list = NULL; + gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + gint i; + + for (i = 0; i < 10; i++) + { + list = g_list_append (list, &nums[i]); + list = g_list_append (list, &nums[i]); + } + + g_assert_cmpint (g_list_length (list), ==, 20); + + for (i = 0; i < 10; i++) + { + list = g_list_remove_all (list, &nums[i]); + } + + g_assert_cmpint (g_list_length (list), ==, 0); + g_assert (list == NULL); +} + +static void +test_list_first_last (void) +{ + GList *list = NULL; + GList *st; + gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + gint i; + + for (i = 0; i < 10; i++) + list = g_list_append (list, &nums[i]); + + st = g_list_last (list); + g_assert (*((gint*) st->data) == 9); + st = g_list_nth_prev (st, 3); + g_assert (*((gint*) st->data) == 6); + st = g_list_first (st); + g_assert (*((gint*) st->data) == 0); + + g_list_free (list); +} + +static void +test_list_insert (void) +{ + GList *list = NULL; + GList *st; + gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + gint i; + + list = g_list_insert_before (NULL, NULL, &nums[1]); + list = g_list_insert (list, &nums[3], 1); + list = g_list_insert (list, &nums[4], -1); + list = g_list_insert (list, &nums[0], 0); + list = g_list_insert (list, &nums[5], 100); + list = g_list_insert_before (list, NULL, &nums[6]); + list = g_list_insert_before (list, list->next->next, &nums[2]); + + list = g_list_insert (list, &nums[9], 7); + list = g_list_insert (list, &nums[8], 7); + list = g_list_insert (list, &nums[7], 7); + + for (i = 0; i < 10; i++) + { + st = g_list_nth (list, i); + g_assert (*((gint*) st->data) == i); + } + + g_list_free (list); +} + int main (int argc, char *argv[]) { @@ -174,6 +311,11 @@ main (int argc, char *argv[]) g_test_add_func ("/list/insert-sorted-with-data", test_list_insert_sorted_with_data); g_test_add_func ("/list/reverse", test_list_reverse); g_test_add_func ("/list/nth", test_list_nth); + g_test_add_func ("/list/concat", test_list_concat); + g_test_add_func ("/list/remove", test_list_remove); + g_test_add_func ("/list/remove-all", test_list_remove_all); + g_test_add_func ("/list/first-last", test_list_first_last); + g_test_add_func ("/list/insert", test_list_insert); return g_test_run (); } diff --git a/glib/tests/slist.c b/glib/tests/slist.c index 071f5d5..08bf17a 100644 --- a/glib/tests/slist.c +++ b/glib/tests/slist.c @@ -157,6 +157,124 @@ test_slist_nth (void) g_slist_free (slist); } +static void +test_slist_remove (void) +{ + GSList *slist = NULL; + GSList *st; + gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + gint i; + + for (i = 0; i < 10; i++) + { + slist = g_slist_append (slist, &nums[i]); + slist = g_slist_append (slist, &nums[i]); + } + + g_assert_cmpint (g_slist_length (slist), ==, 20); + + for (i = 0; i < 10; i++) + { + slist = g_slist_remove (slist, &nums[i]); + } + + g_assert_cmpint (g_slist_length (slist), ==, 10); + + for (i = 0; i < 10; i++) + { + st = g_slist_nth (slist, i); + g_assert (*((gint*) st->data) == i); + } + + g_slist_free (slist); +} + +static void +test_slist_remove_all (void) +{ + GSList *slist = NULL; + gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + gint i; + + for (i = 0; i < 10; i++) + { + slist = g_slist_append (slist, &nums[i]); + slist = g_slist_append (slist, &nums[i]); + } + + g_assert_cmpint (g_slist_length (slist), ==, 20); + + for (i = 0; i < 10; i++) + { + slist = g_slist_remove_all (slist, &nums[i]); + } + + g_assert_cmpint (g_slist_length (slist), ==, 0); + g_assert (slist == NULL); +} + +static void +test_slist_insert (void) +{ + GSList *slist = NULL; + GSList *st; + gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + gint i; + + slist = g_slist_insert_before (NULL, NULL, &nums[1]); + slist = g_slist_insert (slist, &nums[3], 1); + slist = g_slist_insert (slist, &nums[4], -1); + slist = g_slist_insert (slist, &nums[0], 0); + slist = g_slist_insert (slist, &nums[5], 100); + slist = g_slist_insert_before (slist, NULL, &nums[6]); + slist = g_slist_insert_before (slist, slist->next->next, &nums[2]); + + slist = g_slist_insert (slist, &nums[9], 7); + slist = g_slist_insert (slist, &nums[8], 7); + slist = g_slist_insert (slist, &nums[7], 7); + + for (i = 0; i < 10; i++) + { + st = g_slist_nth (slist, i); + g_assert (*((gint*) st->data) == i); + } + + g_slist_free (slist); +} + +static gint +find_num (gconstpointer l, gconstpointer data) +{ + return *(gint*)l - GPOINTER_TO_INT(data); +} + +static void +test_slist_position (void) +{ + GSList *slist = NULL; + GSList *st; + gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + gint i; + + for (i = 0; i < 10; i++) + { + slist = g_slist_append (slist, &nums[i]); + } + + g_assert_cmpint (g_slist_index (slist, NULL), ==, -1); + g_assert_cmpint (g_slist_position (slist, NULL), ==, -1); + + for (i = 0; i < 10; i++) + { + g_assert_cmpint (g_slist_index (slist, &nums[i]), ==, i); + st = g_slist_find_custom (slist, GINT_TO_POINTER(i), find_num); + g_assert (st != NULL); + g_assert_cmpint (g_slist_position (slist, st), ==, i); + } + + g_slist_free (slist); +} + int main (int argc, char *argv[]) { @@ -174,6 +292,10 @@ main (int argc, char *argv[]) g_test_add_func ("/slist/insert-sorted-with-data", test_slist_insert_sorted_with_data); g_test_add_func ("/slist/reverse", test_slist_reverse); g_test_add_func ("/slist/nth", test_slist_nth); + g_test_add_func ("/slist/remove", test_slist_remove); + 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); return g_test_run (); } -- 2.7.4