nth = g_list_nth (list, 0);
if (nth->data != foo)
- return FAILED ("nth failed. #1");
+ return FAILED ("nth failed. #0");
nth = g_list_nth (list, 1);
if (nth->data != bar)
- return FAILED ("nth failed. #2");
+ return FAILED ("nth failed. #1");
nth = g_list_nth (list, 2);
if (nth->data != baz)
- return FAILED ("nth failed. #3");
+ return FAILED ("nth failed. #2");
+
+ nth = g_list_nth (list, 3);
+ if (nth)
+ return FAILED ("nth failed. #3: %s", nth->data);
g_list_free (list);
return OK;
i = g_list_index (list, foo);
if (i != 0)
- return FAILED ("index failed. #1");
+ return FAILED ("index failed. #0: %d", i);
i = g_list_index (list, bar);
if (i != 1)
- return FAILED ("index failed. #2");
+ return FAILED ("index failed. #1: %d", i);
i = g_list_index (list, baz);
if (i != 2)
- return FAILED ("index failed. #3");
+ return FAILED ("index failed. #2: %d", i);
g_list_free (list);
return OK;
RESULT
test_list_insert_before ()
{
- GList *foo, *bar;
+ GList *foo, *bar, *baz;
foo = g_list_prepend (NULL, "foo");
foo = g_list_insert_before (foo, NULL, "bar");
if (strcmp (bar->data, "bar"))
return FAILED ("1");
- g_list_insert_before (foo, bar, "baz");
+ baz = g_list_insert_before (foo, bar, "baz");
+ if (foo != baz)
+ return FAILED ("2");
- if (strcmp (g_list_nth (foo, 1)->data, "baz"))
- return FAILED ("2");
+ if (strcmp (g_list_nth_data (foo, 1), "baz"))
+ return FAILED ("3: %s", g_list_nth_data (foo, 1));
g_list_free (foo);
return OK;
}
+#define N_ELEMS 101
+
+static int intcompare (gconstpointer p1, gconstpointer p2)
+{
+ return GPOINTER_TO_INT (p1) - GPOINTER_TO_INT (p2);
+}
+
+static gboolean verify_sort (GList *list, int len)
+{
+ int prev;
+
+ if (list->prev)
+ return FALSE;
+
+ prev = GPOINTER_TO_INT (list->data);
+ len--;
+ for (list = list->next; list; list = list->next) {
+ int curr = GPOINTER_TO_INT (list->data);
+ if (prev > curr)
+ return FALSE;
+ prev = curr;
+
+ if (!list->prev || list->prev->next != list)
+ return FALSE;
+
+ if (len == 0)
+ return FALSE;
+ len--;
+ }
+ return len == 0;
+}
+
+RESULT
+test_list_sort ()
+{
+ int i, j, mul;
+ GList *list = NULL;
+
+ for (i = 0; i < N_ELEMS; ++i)
+ list = g_list_prepend (list, GINT_TO_POINTER (i));
+ list = g_list_sort (list, intcompare);
+ if (!verify_sort (list, N_ELEMS))
+ return FAILED ("decreasing list");
+
+ g_list_free (list);
+
+ list = NULL;
+ for (i = 0; i < N_ELEMS; ++i)
+ list = g_list_prepend (list, GINT_TO_POINTER (-i));
+ list = g_list_sort (list, intcompare);
+ if (!verify_sort (list, N_ELEMS))
+ return FAILED ("increasing list");
+
+ g_list_free (list);
+
+ list = g_list_prepend (NULL, GINT_TO_POINTER (0));
+ for (i = 1; i < N_ELEMS; ++i) {
+ list = g_list_prepend (list, GINT_TO_POINTER (i));
+ list = g_list_prepend (list, GINT_TO_POINTER (-i));
+ }
+ list = g_list_sort (list, intcompare);
+ if (!verify_sort (list, 2*N_ELEMS-1))
+ return FAILED ("alternating list");
+
+ g_list_free (list);
+
+ list = NULL;
+ mul = 1;
+ for (i = 1; i < N_ELEMS; ++i) {
+ mul = -mul;
+ for (j = 0; j < i; ++j)
+ list = g_list_prepend (list, GINT_TO_POINTER (mul * j));
+ }
+ list = g_list_sort (list, intcompare);
+ if (!verify_sort (list, (N_ELEMS*N_ELEMS - N_ELEMS)/2))
+ return FAILED ("wavering list");
+
+ g_list_free (list);
+
+ return OK;
+}
+
static Test list_tests [] = {
{ "length", test_list_length},
{ "nth", test_list_nth},
{ "remove", test_list_remove},
{ "remove_link", test_list_remove_link},
{ "remove_link", test_list_remove_link},
+ { "sort", test_list_sort},
{NULL, NULL}
};