Merge pull request #5317 from alexrp/master
[mono.git] / mono / eglib / test / slist.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <glib.h>
4 #include "test.h"
5
6
7 RESULT
8 test_slist_nth ()
9 {
10         char *foo = "foo";
11         char *bar = "bar";
12         char *baz = "baz";
13         GSList *nth, *list;
14         list = g_slist_prepend (NULL, baz);
15         list = g_slist_prepend (list, bar);
16         list = g_slist_prepend (list, foo);
17
18         nth = g_slist_nth (list, 0);
19         if (nth->data != foo)
20                 return FAILED ("nth failed. #0");
21
22         nth = g_slist_nth (list, 1);
23         if (nth->data != bar)
24                 return FAILED ("nth failed. #1");
25         
26         nth = g_slist_nth (list, 2);
27         if (nth->data != baz)
28                 return FAILED ("nth failed. #2");
29
30         nth = g_slist_nth (list, 3);
31         if (nth)
32                 return FAILED ("nth failed. #3: %s", nth->data);
33
34         g_slist_free (list);
35         return OK;
36 }
37
38 RESULT
39 test_slist_index ()
40 {
41         int i;
42         char *foo = "foo";
43         char *bar = "bar";
44         char *baz = "baz";
45         GSList *list;
46         list = g_slist_prepend (NULL, baz);
47         list = g_slist_prepend (list, bar);
48         list = g_slist_prepend (list, foo);
49
50         i = g_slist_index (list, foo);
51         if (i != 0)
52                 return FAILED ("index failed. #0: %d", i);
53
54         i = g_slist_index (list, bar);
55         if (i != 1)
56                 return FAILED ("index failed. #1: %d", i);
57         
58         i = g_slist_index (list, baz);
59         if (i != 2)
60                 return FAILED ("index failed. #2: %d", i);
61
62         g_slist_free (list);
63         return OK;
64 }
65
66 RESULT
67 test_slist_append ()
68 {
69         GSList *foo;
70         GSList *list = g_slist_append (NULL, "first");
71         if (g_slist_length (list) != 1)
72                 return FAILED ("append(null,...) failed");
73
74         foo = g_slist_append (list, "second");
75         if (foo != list)
76                 return FAILED ("changed list head on non-empty");
77
78         if (g_slist_length (list) != 2)
79                 return FAILED ("Append failed");
80
81         g_slist_free (list);
82         return OK;
83 }
84
85 RESULT
86 test_slist_concat ()
87 {
88         GSList *foo = g_slist_prepend (NULL, "foo");
89         GSList *bar = g_slist_prepend (NULL, "bar");
90
91         GSList *list = g_slist_concat (foo, bar);
92
93         if (g_slist_length (list) != 2)
94                 return FAILED ("Concat failed.");
95
96         g_slist_free (list);
97         return OK;
98 }
99
100 RESULT
101 test_slist_find ()
102 {
103         GSList *list = g_slist_prepend (NULL, "three");
104         GSList *found;
105         char *data;
106                 
107         list = g_slist_prepend (list, "two");
108         list = g_slist_prepend (list, "one");
109
110         data = "four";
111         list = g_slist_append (list, data);
112
113         found = g_slist_find (list, data);
114
115         if (found->data != data)
116                 return FAILED ("Find failed");
117
118         g_slist_free (list);
119         return OK;
120 }
121
122 static gint
123 find_custom (gconstpointer a, gconstpointer b)
124 {
125         return(strcmp (a, b));
126 }
127
128 RESULT
129 test_slist_find_custom ()
130 {
131         GSList *list = NULL, *found;
132         char *foo = "foo";
133         char *bar = "bar";
134         char *baz = "baz";
135         
136         list = g_slist_prepend (list, baz);
137         list = g_slist_prepend (list, bar);
138         list = g_slist_prepend (list, foo);
139         
140         found = g_slist_find_custom (list, baz, find_custom);
141         
142         if (found == NULL)
143                 return FAILED ("Find failed");
144         
145         g_slist_free (list);
146         
147         return OK;
148 }
149
150 RESULT
151 test_slist_remove ()
152 {
153         GSList *list = g_slist_prepend (NULL, "three");
154         char *one = "one";
155         list = g_slist_prepend (list, "two");
156         list = g_slist_prepend (list, one);
157
158         list = g_slist_remove (list, one);
159
160         if (g_slist_length (list) != 2)
161                 return FAILED ("Remove failed");
162
163         if (strcmp ("two", list->data) != 0)
164                 return FAILED ("Remove failed");
165
166         g_slist_free (list);
167         return OK;
168 }
169
170 RESULT
171 test_slist_remove_link ()
172 {
173         GSList *foo = g_slist_prepend (NULL, "a");
174         GSList *bar = g_slist_prepend (NULL, "b");
175         GSList *baz = g_slist_prepend (NULL, "c");
176         GSList *list = foo;
177
178         foo = g_slist_concat (foo, bar);
179         foo = g_slist_concat (foo, baz);        
180
181         list = g_slist_remove_link (list, bar);
182
183         if (g_slist_length (list) != 2)
184                 return FAILED ("remove_link failed #1");
185
186         if (bar->next != NULL)
187                 return FAILED ("remove_link failed #2");
188
189         g_slist_free (list);    
190         g_slist_free (bar);
191
192         return OK;
193 }
194
195 static gint
196 compare (gconstpointer a, gconstpointer b)
197 {
198         char *foo = (char *) a;
199         char *bar = (char *) b;
200
201         if (strlen (foo) < strlen (bar))
202                 return -1;
203
204         return 1;
205 }
206
207 RESULT
208 test_slist_insert_sorted ()
209 {
210         GSList *list = g_slist_prepend (NULL, "a");
211         list = g_slist_append (list, "aaa");
212
213         /* insert at the middle */
214         list = g_slist_insert_sorted (list, "aa", compare);
215         if (strcmp ("aa", list->next->data))
216                 return FAILED("insert_sorted failed #1");
217
218         /* insert at the beginning */
219         list = g_slist_insert_sorted (list, "", compare);
220         if (strcmp ("", list->data))
221                 return FAILED ("insert_sorted failed #2");
222
223         /* insert at the end */
224         list = g_slist_insert_sorted (list, "aaaa", compare);
225         if (strcmp ("aaaa", g_slist_last (list)->data))
226                 return FAILED ("insert_sorted failed #3");
227
228         g_slist_free (list);    
229         return OK;
230 }
231
232 RESULT
233 test_slist_insert_before ()
234 {
235         GSList *foo, *bar, *baz;
236
237         foo = g_slist_prepend (NULL, "foo");
238         foo = g_slist_insert_before (foo, NULL, "bar");
239         bar = g_slist_last (foo);
240
241         if (strcmp (bar->data, "bar"))
242                 return FAILED ("1");
243
244         baz = g_slist_insert_before (foo, bar, "baz");
245         if (foo != baz)
246                 return FAILED ("2");
247
248         if (strcmp (foo->next->data, "baz"))
249                 return FAILED ("3: %s", foo->next->data);
250
251         g_slist_free (foo);
252         return OK;
253 }
254
255 #define N_ELEMS 100
256
257 static int intcompare (gconstpointer p1, gconstpointer p2)
258 {
259         return GPOINTER_TO_INT (p1) - GPOINTER_TO_INT (p2);
260 }
261
262 static gboolean verify_sort (GSList *list, int len)
263 {
264         int prev = GPOINTER_TO_INT (list->data);
265         len--;
266         for (list = list->next; list; list = list->next) {
267                 int curr = GPOINTER_TO_INT (list->data);
268                 if (prev > curr)
269                         return FALSE;
270                 prev = curr;
271
272                 if (len == 0)
273                         return FALSE;
274                 len--;
275         }
276         return len == 0;
277 }
278
279 RESULT
280 test_slist_sort ()
281 {
282         int i, j, mul;
283         GSList *list = NULL;
284
285         for (i = 0; i < N_ELEMS; ++i)
286                 list = g_slist_prepend (list, GINT_TO_POINTER (i));
287         list = g_slist_sort (list, intcompare);
288         if (!verify_sort (list, N_ELEMS))
289                 return FAILED ("decreasing list");
290
291         g_slist_free (list);
292
293         list = NULL;
294         for (i = 0; i < N_ELEMS; ++i)
295                 list = g_slist_prepend (list, GINT_TO_POINTER (-i));
296         list = g_slist_sort (list, intcompare);
297         if (!verify_sort (list, N_ELEMS))
298                 return FAILED ("increasing list");
299
300         g_slist_free (list);
301
302         list = g_slist_prepend (NULL, GINT_TO_POINTER (0));
303         for (i = 1; i < N_ELEMS; ++i) {
304                 list = g_slist_prepend (list, GINT_TO_POINTER (-i));
305                 list = g_slist_prepend (list, GINT_TO_POINTER (i));
306         }
307         list = g_slist_sort (list, intcompare);
308         if (!verify_sort (list, 2*N_ELEMS-1))
309                 return FAILED ("alternating list");
310
311         g_slist_free (list);
312
313         list = NULL;
314         mul = 1;
315         for (i = 1; i < N_ELEMS; ++i) {
316                 mul = -mul;
317                 for (j = 0; j < i; ++j)
318                         list = g_slist_prepend (list, GINT_TO_POINTER (mul * j));
319         }
320         list = g_slist_sort (list, intcompare);
321         if (!verify_sort (list, (N_ELEMS*N_ELEMS - N_ELEMS)/2))
322                 return FAILED ("wavering list");
323
324         g_slist_free (list);
325
326         return OK;
327 }
328
329 static Test slist_tests [] = {
330         {"nth", test_slist_nth},
331         {"index", test_slist_index},
332         {"append", test_slist_append},
333         {"concat", test_slist_concat},
334         {"find", test_slist_find},
335         {"find_custom", test_slist_find_custom},
336         {"remove", test_slist_remove},
337         {"remove_link", test_slist_remove_link},
338         {"insert_sorted", test_slist_insert_sorted},
339         {"insert_before", test_slist_insert_before},
340         {"sort", test_slist_sort},
341         {NULL, NULL}
342 };
343
344 DEFINE_TEST_GROUP_INIT(slist_tests_init, slist_tests)
345