7460ec7b53edcf611ca38d7247e2214baeac5fdc
[mono.git] / eglib / test / list.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <glib.h>
4 #include "test.h"
5
6 RESULT
7 test_list_length ()
8 {
9         GList *list = g_list_prepend (NULL, "foo");
10
11         if (g_list_length (list) != 1)
12                 return FAILED ("length failed. #1");
13
14         list = g_list_prepend (list, "bar");
15         if (g_list_length (list) != 2)
16                 return FAILED ("length failed. #2");
17
18         list = g_list_append (list, "bar");
19         if (g_list_length (list) != 3)
20                 return FAILED ("length failed. #3");
21
22         g_list_free (list);
23         return NULL;
24 }
25
26 RESULT
27 test_list_nth ()
28 {
29         char *foo = "foo";
30         char *bar = "bar";
31         char *baz = "baz";
32         GList *nth, *list;
33         list = g_list_prepend (NULL, baz);
34         list = g_list_prepend (list, bar);
35         list = g_list_prepend (list, foo);
36
37         nth = g_list_nth (list, 0);
38         if (nth->data != foo)
39                 return FAILED ("nth failed. #1");
40
41         nth = g_list_nth (list, 1);
42         if (nth->data != bar)
43                 return FAILED ("nth failed. #2");
44         
45         nth = g_list_nth (list, 2);
46         if (nth->data != baz)
47                 return FAILED ("nth failed. #3");
48
49         g_list_free (list);
50         return OK;
51 }
52
53 RESULT
54 test_list_index ()
55 {
56         int i;
57         char *foo = "foo";
58         char *bar = "bar";
59         char *baz = "baz";
60         GList *list;
61         list = g_list_prepend (NULL, baz);
62         list = g_list_prepend (list, bar);
63         list = g_list_prepend (list, foo);
64
65         i = g_list_index (list, foo);
66         if (i != 0)
67                 return FAILED ("index failed. #1");
68
69         i = g_list_index (list, bar);
70         if (i != 1)
71                 return FAILED ("index failed. #2");
72         
73         i = g_list_index (list, baz);
74         if (i != 2)
75                 return FAILED ("index failed. #3");
76
77         g_list_free (list);
78         return OK;
79 }
80
81 RESULT
82 test_list_append ()
83 {
84         GList *list = g_list_prepend (NULL, "first");
85         if (g_list_length (list) != 1)
86                 return FAILED ("Prepend failed");
87
88         list = g_list_append (list, "second");
89
90         if (g_list_length (list) != 2)
91                 return FAILED ("Append failed");
92
93         g_list_free (list);
94         return OK;
95 }
96
97 RESULT
98 test_list_last ()
99 {
100         GList *foo = g_list_prepend (NULL, "foo");
101         GList *bar = g_list_prepend (NULL, "bar");
102         GList *last;
103         
104         foo = g_list_concat (foo, bar);
105         last = g_list_last (foo);
106
107         if (last != bar)
108                 return FAILED ("last failed. #1");
109
110         foo = g_list_concat (foo, g_list_prepend (NULL, "baz"));
111         foo = g_list_concat (foo, g_list_prepend (NULL, "quux"));
112
113         last = g_list_last (foo);       
114         if (strcmp ("quux", last->data))
115                 return FAILED ("last failed. #2");
116
117         g_list_free (foo);
118
119         return OK;
120 }
121
122 RESULT
123 test_list_concat ()
124 {
125         GList *foo = g_list_prepend (NULL, "foo");
126         GList *bar = g_list_prepend (NULL, "bar");
127         GList *list = g_list_concat (foo, bar);
128
129         if (g_list_length (list) != 2)
130                 return FAILED ("Concat failed. #1");
131
132         if (strcmp (list->data, "foo"))
133                 return FAILED ("Concat failed. #2");
134
135         if (strcmp (list->next->data, "bar"))
136                 return FAILED ("Concat failed. #3");
137
138         if (g_list_first (list) != foo)
139                 return FAILED ("Concat failed. #4");
140         
141         if (g_list_last (list) != bar)
142                 return FAILED ("Concat failed. #5");
143
144         g_list_free (list);
145
146         return OK;
147 }
148
149
150 static gint
151 compare (gconstpointer a, gconstpointer b)
152 {
153         char *foo = (char *) a;
154         char *bar = (char *) b;
155
156         if (strlen (foo) < strlen (bar))
157                 return -1;
158
159         return 1;
160 }
161
162 RESULT
163 test_list_insert_sorted ()
164 {
165         GList *list = g_list_prepend (NULL, "a");
166         list = g_list_append (list, "aaa");
167
168         /* insert at the middle */
169         list = g_list_insert_sorted (list, "aa", compare);
170         if (strcmp ("aa", list->next->data))
171                 return FAILED ("insert_sorted failed. #1");
172
173         /* insert at the beginning */
174         list = g_list_insert_sorted (list, "", compare);
175         if (strcmp ("", list->data))
176                 return FAILED ("insert_sorted failed. #2");             
177
178         /* insert at the end */
179         list = g_list_insert_sorted (list, "aaaa", compare);
180         if (strcmp ("aaaa", g_list_last (list)->data))
181                 return FAILED ("insert_sorted failed. #3");
182
183         g_list_free (list);
184         return OK;
185 }
186
187 RESULT
188 test_list_copy ()
189 {
190         int i, length;
191         GList *list, *copy;
192         list = g_list_prepend (NULL, "a");
193         list = g_list_append  (list, "aa");
194         list = g_list_append  (list, "aaa");
195         list = g_list_append  (list, "aaaa");
196
197         length = g_list_length (list);
198         copy = g_list_copy (list);
199
200         for (i = 0; i < length; i++)
201                 if (strcmp (g_list_nth (list, i)->data,
202                             g_list_nth (copy, i)->data))
203                         return FAILED ("copy failed.");
204
205         g_list_free (list);
206         g_list_free (copy);     
207         return OK;
208 }
209
210 RESULT
211 test_list_reverse ()
212 {
213         guint i, length;
214         GList *list, *reverse;
215         list = g_list_prepend (NULL, "a");
216         list = g_list_append  (list, "aa");
217         list = g_list_append  (list, "aaa");
218         list = g_list_append  (list, "aaaa");
219
220         length  = g_list_length (list);
221         reverse = g_list_reverse (g_list_copy (list));
222
223         if (g_list_length (reverse) != length)
224                 return FAILED ("reverse failed #1");
225
226         for (i = 0; i < length; i++){
227                 guint j = length - i - 1;
228                 if (strcmp (g_list_nth (list, i)->data,
229                             g_list_nth (reverse, j)->data))
230                         return FAILED ("reverse failed. #2");
231         }
232
233         g_list_free (list);
234         g_list_free (reverse);  
235         return OK;
236 }
237
238 RESULT
239 test_list_remove ()
240 {
241         GList *list = g_list_prepend (NULL, "three");
242         char *one = "one";
243         list = g_list_prepend (list, "two");
244         list = g_list_prepend (list, one);
245
246         list = g_list_remove (list, one);
247
248         if (g_list_length (list) != 2)
249                 return FAILED ("Remove failed");
250
251         if (strcmp ("two", list->data) != 0)
252                 return FAILED ("Remove failed");
253
254         g_list_free (list);
255         return OK;
256 }
257
258 RESULT
259 test_list_remove_link ()
260 {
261         GList *foo = g_list_prepend (NULL, "a");
262         GList *bar = g_list_prepend (NULL, "b");
263         GList *baz = g_list_prepend (NULL, "c");
264         GList *list = foo;
265
266         foo = g_list_concat (foo, bar);
267         foo = g_list_concat (foo, baz); 
268
269         list = g_list_remove_link (list, bar);
270
271         if (g_list_length (list) != 2)
272                 return FAILED ("remove_link failed #1");
273
274         if (bar->next != NULL)
275                 return FAILED ("remove_link failed #2");
276
277         g_list_free (list);     
278         g_list_free (bar);
279         return OK;
280 }
281
282 RESULT
283 test_list_insert_before ()
284 {
285         GList *foo, *bar;
286
287         foo = g_list_prepend (NULL, "foo");
288         foo = g_list_insert_before (foo, NULL, "bar");
289         bar = g_list_last (foo);
290
291         if (strcmp (bar->data, "bar"))
292                 return FAILED ("1");
293
294         g_list_insert_before (foo, bar, "baz");
295
296         if (strcmp (g_list_nth (foo, 1)->data, "baz"))
297                 return FAILED ("2");    
298
299         return OK;
300 }
301
302 static Test list_tests [] = {
303         {"list_length", test_list_length},
304         {"list_nth", test_list_nth},
305         {"list_index", test_list_index},        
306         {"list_last", test_list_last},  
307         {"list_append", test_list_append},
308         {"list_concat", test_list_concat},
309         {"list_insert_sorted", test_list_insert_sorted},
310         {"list_insert_before", test_list_insert_before},
311         {"list_copy", test_list_copy},
312         {"list_reverse", test_list_reverse},
313         {"list_remove", test_list_remove},
314         {"list_remove_link", test_list_remove_link},
315         {NULL, NULL}
316 };
317
318 DEFINE_TEST_GROUP_INIT(list_tests_init, list_tests)