Avoid warning
[mono.git] / eglib / test / list.c
index 7460ec7b53edcf611ca38d7247e2214baeac5fdc..68dadee612afd0912105c7d880468529ee79173f 100644 (file)
@@ -36,15 +36,19 @@ test_list_nth ()
 
        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;
@@ -64,15 +68,15 @@ test_list_index ()
 
        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;
@@ -282,7 +286,7 @@ test_list_remove_link ()
 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");
@@ -291,27 +295,143 @@ test_list_insert_before ()
        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_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");
 
-       if (strcmp (g_list_nth (foo, 1)->data, "baz"))
-               return FAILED ("2");    
+       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 gint
+find_custom (gconstpointer a, gconstpointer b)
+{
+       return(strcmp (a, b));
+}
+
+RESULT
+test_list_find_custom ()
+{
+       GList *list = NULL, *found;
+       char *foo = "foo";
+       char *bar = "bar";
+       char *baz = "baz";
+       
+       list = g_list_prepend (list, baz);
+       list = g_list_prepend (list, bar);
+       list = g_list_prepend (list, foo);
+       
+       found = g_list_find_custom (list, baz, find_custom);
+       
+       if (found == NULL)
+               return FAILED ("Find failed");
+       
+       g_list_free (list);
+       
        return OK;
 }
 
 static Test list_tests [] = {
-       {"list_length", test_list_length},
-       {"list_nth", test_list_nth},
-       {"list_index", test_list_index},        
-       {"list_last", test_list_last},  
-       {"list_append", test_list_append},
-       {"list_concat", test_list_concat},
-       {"list_insert_sorted", test_list_insert_sorted},
-       {"list_insert_before", test_list_insert_before},
-       {"list_copy", test_list_copy},
-       {"list_reverse", test_list_reverse},
-       {"list_remove", test_list_remove},
-       {"list_remove_link", test_list_remove_link},
+       {       "length", test_list_length},
+       {          "nth", test_list_nth},
+       {        "index", test_list_index},     
+       {         "last", test_list_last},      
+       {       "append", test_list_append},
+       {       "concat", test_list_concat},
+       {"insert_sorted", test_list_insert_sorted},
+       {"insert_before", test_list_insert_before},
+       {         "copy", test_list_copy},
+       {      "reverse", test_list_reverse},
+       {       "remove", test_list_remove},
+       {  "remove_link", test_list_remove_link},
+       {  "remove_link", test_list_remove_link},
+       {         "sort", test_list_sort},
+       {  "find_custom", test_list_find_custom},
        {NULL, NULL}
 };