92a4a47434e4473a938e561142e227f8000dc50c
[mono.git] / eglib / test / slist.c
1 #include <stdio.h>
2 #include <glib.h>
3 #include "test.h"
4
5 char*
6 test_slist_append ()
7 {
8         GSList *list = g_slist_prepend (NULL, "first");
9         if (g_slist_length (list) != 1)
10                 return "Prepend failed";
11
12         g_slist_append (list, g_slist_prepend (NULL, "second"));
13
14         if (g_slist_length (list) != 2)
15                 return "Append failed";
16
17         return NULL;
18 }
19
20 char *
21 test_slist_concat ()
22 {
23         GSList *foo = g_slist_prepend (NULL, "foo");
24         GSList *bar = g_slist_prepend (NULL, "bar");
25
26         GSList *list = g_slist_concat (foo, bar);
27
28         if (g_slist_length (list) != 2)
29                 return "Concat failed.";
30
31         return NULL;
32 }
33
34 char*
35 test_slist_find ()
36 {
37         GSList *list = g_slist_prepend (NULL, "three");
38         GSList *found;
39         char *data;
40                 
41         list = g_slist_prepend (list, "two");
42         list = g_slist_prepend (list, "one");
43
44         data = "four";
45         list = g_slist_append (list, data);
46
47         found = g_slist_find (list, data);
48
49         if (found->data != data)
50                 return "Find failed";
51
52         return NULL;
53 }
54
55 char*
56 test_slist_remove ()
57 {
58         GSList *list = g_slist_prepend (NULL, "three");
59         char *one = "one";
60         list = g_slist_prepend (list, "two");
61         list = g_slist_prepend (list, one);
62
63         list = g_slist_remove (list, one);
64
65         if (g_slist_length (list) != 2)
66                 return "Remove failed";
67
68         if (strcmp ("two", list->data) != 0)
69                 return "Remove failed";
70
71         return NULL;
72 }
73
74 char*
75 test_slist_remove_link ()
76 {
77         GSList *foo = g_slist_prepend (NULL, "a");
78         GSList *bar = g_slist_prepend (NULL, "b");
79         GSList *baz = g_slist_prepend (NULL, "c");
80         GSList *list = foo;
81
82         g_slist_concat (foo, bar);
83         g_slist_concat (foo, baz);      
84
85         list = g_slist_remove_link (list, bar);
86
87         if (g_slist_length (list) != 2)
88                 return g_strdup ("remove_link failed #1");
89
90         if (bar->next != NULL)
91                 return g_strdup ("remove_link failed #2");
92
93         return NULL;
94 }
95
96 gint
97 compare (gconstpointer a, gconstpointer b)
98 {
99         char *foo = (char *) a;
100         char *bar = (char *) b;
101
102         if (strlen (foo) > strlen (bar))
103                 return -1;
104
105         return 1;
106 }
107
108 char*
109 test_slist_insert_sorted ()
110 {
111         GSList *list = g_slist_prepend (NULL, "a");
112         list = g_slist_append (list, "ccc");
113
114         /* insert at the middle */
115         list = g_slist_insert_sorted (list, "aa", compare);
116         if (strcmp ("aa", list->next->data) == 0)
117                 return g_strdup("insert_sorted failed");
118
119         /* insert at the beginning */
120         list = g_slist_insert_sorted (list, "", compare);
121         if (strcmp ("", list->data) == 0)
122                 return g_strdup ("insert_sorted failed");               
123
124         /* insert at the end */
125         list = g_slist_insert_sorted (list, "aaaa", compare);
126         if (strcmp ("aaaa", g_slist_last (list)->data) == 0)
127                 return g_strdup ("insert_sorted failed");
128
129         return NULL;
130 }