[runtime] Use the AC_FUNC_STRERROR_R() autoconf macro to distinguish between the...
[mono.git] / mono / 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. #0");
40
41         nth = g_list_nth (list, 1);
42         if (nth->data != bar)
43                 return FAILED ("nth failed. #1");
44         
45         nth = g_list_nth (list, 2);
46         if (nth->data != baz)
47                 return FAILED ("nth failed. #2");
48
49         nth = g_list_nth (list, 3);
50         if (nth)
51                 return FAILED ("nth failed. #3: %s", nth->data);
52
53         g_list_free (list);
54         return OK;
55 }
56
57 RESULT
58 test_list_index ()
59 {
60         int i;
61         char *foo = "foo";
62         char *bar = "bar";
63         char *baz = "baz";
64         GList *list;
65         list = g_list_prepend (NULL, baz);
66         list = g_list_prepend (list, bar);
67         list = g_list_prepend (list, foo);
68
69         i = g_list_index (list, foo);
70         if (i != 0)
71                 return FAILED ("index failed. #0: %d", i);
72
73         i = g_list_index (list, bar);
74         if (i != 1)
75                 return FAILED ("index failed. #1: %d", i);
76         
77         i = g_list_index (list, baz);
78         if (i != 2)
79                 return FAILED ("index failed. #2: %d", i);
80
81         g_list_free (list);
82         return OK;
83 }
84
85 RESULT
86 test_list_append ()
87 {
88         GList *list = g_list_prepend (NULL, "first");
89         if (g_list_length (list) != 1)
90                 return FAILED ("Prepend failed");
91
92         list = g_list_append (list, "second");
93
94         if (g_list_length (list) != 2)
95                 return FAILED ("Append failed");
96
97         g_list_free (list);
98         return OK;
99 }
100
101 RESULT
102 test_list_last ()
103 {
104         GList *foo = g_list_prepend (NULL, "foo");
105         GList *bar = g_list_prepend (NULL, "bar");
106         GList *last;
107         
108         foo = g_list_concat (foo, bar);
109         last = g_list_last (foo);
110
111         if (last != bar)
112                 return FAILED ("last failed. #1");
113
114         foo = g_list_concat (foo, g_list_prepend (NULL, "baz"));
115         foo = g_list_concat (foo, g_list_prepend (NULL, "quux"));
116
117         last = g_list_last (foo);       
118         if (strcmp ("quux", last->data))
119                 return FAILED ("last failed. #2");
120
121         g_list_free (foo);
122
123         return OK;
124 }
125
126 RESULT
127 test_list_concat ()
128 {
129         GList *foo = g_list_prepend (NULL, "foo");
130         GList *bar = g_list_prepend (NULL, "bar");
131         GList *list = g_list_concat (foo, bar);
132
133         if (g_list_length (list) != 2)
134                 return FAILED ("Concat failed. #1");
135
136         if (strcmp (list->data, "foo"))
137                 return FAILED ("Concat failed. #2");
138
139         if (strcmp (list->next->data, "bar"))
140                 return FAILED ("Concat failed. #3");
141
142         if (g_list_first (list) != foo)
143                 return FAILED ("Concat failed. #4");
144         
145         if (g_list_last (list) != bar)
146                 return FAILED ("Concat failed. #5");
147
148         g_list_free (list);
149
150         return OK;
151 }
152
153
154 static gint
155 compare (gconstpointer a, gconstpointer b)
156 {
157         char *foo = (char *) a;
158         char *bar = (char *) b;
159
160         if (strlen (foo) < strlen (bar))
161                 return -1;
162
163         return 1;
164 }
165
166 RESULT
167 test_list_insert_sorted ()
168 {
169         GList *list = g_list_prepend (NULL, "a");
170         list = g_list_append (list, "aaa");
171
172         /* insert at the middle */
173         list = g_list_insert_sorted (list, "aa", compare);
174         if (strcmp ("aa", list->next->data))
175                 return FAILED ("insert_sorted failed. #1");
176
177         /* insert at the beginning */
178         list = g_list_insert_sorted (list, "", compare);
179         if (strcmp ("", list->data))
180                 return FAILED ("insert_sorted failed. #2");             
181
182         /* insert at the end */
183         list = g_list_insert_sorted (list, "aaaa", compare);
184         if (strcmp ("aaaa", g_list_last (list)->data))
185                 return FAILED ("insert_sorted failed. #3");
186
187         g_list_free (list);
188         return OK;
189 }
190
191 RESULT
192 test_list_copy ()
193 {
194         int i, length;
195         GList *list, *copy;
196         list = g_list_prepend (NULL, "a");
197         list = g_list_append  (list, "aa");
198         list = g_list_append  (list, "aaa");
199         list = g_list_append  (list, "aaaa");
200
201         length = g_list_length (list);
202         copy = g_list_copy (list);
203
204         for (i = 0; i < length; i++)
205                 if (strcmp (g_list_nth (list, i)->data,
206                             g_list_nth (copy, i)->data))
207                         return FAILED ("copy failed.");
208
209         g_list_free (list);
210         g_list_free (copy);     
211         return OK;
212 }
213
214 RESULT
215 test_list_reverse ()
216 {
217         guint i, length;
218         GList *list, *reverse;
219         list = g_list_prepend (NULL, "a");
220         list = g_list_append  (list, "aa");
221         list = g_list_append  (list, "aaa");
222         list = g_list_append  (list, "aaaa");
223
224         length  = g_list_length (list);
225         reverse = g_list_reverse (g_list_copy (list));
226
227         if (g_list_length (reverse) != length)
228                 return FAILED ("reverse failed #1");
229
230         for (i = 0; i < length; i++){
231                 guint j = length - i - 1;
232                 if (strcmp (g_list_nth (list, i)->data,
233                             g_list_nth (reverse, j)->data))
234                         return FAILED ("reverse failed. #2");
235         }
236
237         g_list_free (list);
238         g_list_free (reverse);  
239         return OK;
240 }
241
242 RESULT
243 test_list_remove ()
244 {
245         GList *list = g_list_prepend (NULL, "three");
246         char *one = "one";
247         list = g_list_prepend (list, "two");
248         list = g_list_prepend (list, one);
249
250         list = g_list_remove (list, one);
251
252         if (g_list_length (list) != 2)
253                 return FAILED ("Remove failed");
254
255         if (strcmp ("two", list->data) != 0)
256                 return FAILED ("Remove failed");
257
258         g_list_free (list);
259         return OK;
260 }
261
262 RESULT
263 test_list_remove_link ()
264 {
265         GList *foo = g_list_prepend (NULL, "a");
266         GList *bar = g_list_prepend (NULL, "b");
267         GList *baz = g_list_prepend (NULL, "c");
268         GList *list = foo;
269
270         foo = g_list_concat (foo, bar);
271         foo = g_list_concat (foo, baz); 
272
273         list = g_list_remove_link (list, bar);
274
275         if (g_list_length (list) != 2)
276                 return FAILED ("remove_link failed #1");
277
278         if (bar->next != NULL)
279                 return FAILED ("remove_link failed #2");
280
281         g_list_free (list);     
282         g_list_free (bar);
283         return OK;
284 }
285
286 RESULT
287 test_list_insert_before ()
288 {
289         GList *foo, *bar, *baz;
290
291         foo = g_list_prepend (NULL, "foo");
292         foo = g_list_insert_before (foo, NULL, "bar");
293         bar = g_list_last (foo);
294
295         if (strcmp (bar->data, "bar"))
296                 return FAILED ("1");
297
298         baz = g_list_insert_before (foo, bar, "baz");
299         if (foo != baz)
300                 return FAILED ("2");
301
302         if (strcmp (g_list_nth_data (foo, 1), "baz"))
303                 return FAILED ("3: %s", g_list_nth_data (foo, 1));      
304
305         g_list_free (foo);
306         return OK;
307 }
308
309 #define N_ELEMS 101
310
311 static int intcompare (gconstpointer p1, gconstpointer p2)
312 {
313         return GPOINTER_TO_INT (p1) - GPOINTER_TO_INT (p2);
314 }
315
316 static gboolean verify_sort (GList *list, int len)
317 {
318         int prev;
319
320         if (list->prev)
321                 return FALSE;
322
323         prev = GPOINTER_TO_INT (list->data);
324         len--;
325         for (list = list->next; list; list = list->next) {
326                 int curr = GPOINTER_TO_INT (list->data);
327                 if (prev > curr)
328                         return FALSE;
329                 prev = curr;
330
331                 if (!list->prev || list->prev->next != list)
332                         return FALSE;
333
334                 if (len == 0)
335                         return FALSE;
336                 len--;
337         }
338         return len == 0;
339 }
340
341 RESULT
342 test_list_sort ()
343 {
344         int i, j, mul;
345         GList *list = NULL;
346
347         for (i = 0; i < N_ELEMS; ++i)
348                 list = g_list_prepend (list, GINT_TO_POINTER (i));
349         list = g_list_sort (list, intcompare);
350         if (!verify_sort (list, N_ELEMS))
351                 return FAILED ("decreasing list");
352
353         g_list_free (list);
354
355         list = NULL;
356         for (i = 0; i < N_ELEMS; ++i)
357                 list = g_list_prepend (list, GINT_TO_POINTER (-i));
358         list = g_list_sort (list, intcompare);
359         if (!verify_sort (list, N_ELEMS))
360                 return FAILED ("increasing list");
361
362         g_list_free (list);
363
364         list = g_list_prepend (NULL, GINT_TO_POINTER (0));
365         for (i = 1; i < N_ELEMS; ++i) {
366                 list = g_list_prepend (list, GINT_TO_POINTER (i));
367                 list = g_list_prepend (list, GINT_TO_POINTER (-i));
368         }
369         list = g_list_sort (list, intcompare);
370         if (!verify_sort (list, 2*N_ELEMS-1))
371                 return FAILED ("alternating list");
372
373         g_list_free (list);
374
375         list = NULL;
376         mul = 1;
377         for (i = 1; i < N_ELEMS; ++i) {
378                 mul = -mul;
379                 for (j = 0; j < i; ++j)
380                         list = g_list_prepend (list, GINT_TO_POINTER (mul * j));
381         }
382         list = g_list_sort (list, intcompare);
383         if (!verify_sort (list, (N_ELEMS*N_ELEMS - N_ELEMS)/2))
384                 return FAILED ("wavering list");
385
386         g_list_free (list);
387
388         return OK;
389 }
390
391 static gint
392 find_custom (gconstpointer a, gconstpointer b)
393 {
394         return(strcmp (a, b));
395 }
396
397 RESULT
398 test_list_find_custom ()
399 {
400         GList *list = NULL, *found;
401         char *foo = "foo";
402         char *bar = "bar";
403         char *baz = "baz";
404         
405         list = g_list_prepend (list, baz);
406         list = g_list_prepend (list, bar);
407         list = g_list_prepend (list, foo);
408         
409         found = g_list_find_custom (list, baz, find_custom);
410         
411         if (found == NULL)
412                 return FAILED ("Find failed");
413         
414         g_list_free (list);
415         
416         return OK;
417 }
418
419 static Test list_tests [] = {
420         {       "length", test_list_length},
421         {          "nth", test_list_nth},
422         {        "index", test_list_index},     
423         {         "last", test_list_last},      
424         {       "append", test_list_append},
425         {       "concat", test_list_concat},
426         {"insert_sorted", test_list_insert_sorted},
427         {"insert_before", test_list_insert_before},
428         {         "copy", test_list_copy},
429         {      "reverse", test_list_reverse},
430         {       "remove", test_list_remove},
431         {  "remove_link", test_list_remove_link},
432         {  "remove_link", test_list_remove_link},
433         {         "sort", test_list_sort},
434         {  "find_custom", test_list_find_custom},
435         {NULL, NULL}
436 };
437
438 DEFINE_TEST_GROUP_INIT(list_tests_init, list_tests)