[gc] Fix crash when doing WaitForPendingFinalizers
[mono-project.git] / eglib / test / slist.c
blob3f8360ecae9536cc9f3749543a442693071f3afa
1 #include <stdio.h>
2 #include <string.h>
3 #include <glib.h>
4 #include "test.h"
7 RESULT
8 test_slist_nth ()
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);
18 nth = g_slist_nth (list, 0);
19 if (nth->data != foo)
20 return FAILED ("nth failed. #0");
22 nth = g_slist_nth (list, 1);
23 if (nth->data != bar)
24 return FAILED ("nth failed. #1");
26 nth = g_slist_nth (list, 2);
27 if (nth->data != baz)
28 return FAILED ("nth failed. #2");
30 nth = g_slist_nth (list, 3);
31 if (nth)
32 return FAILED ("nth failed. #3: %s", nth->data);
34 g_slist_free (list);
35 return OK;
38 RESULT
39 test_slist_index ()
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);
50 i = g_slist_index (list, foo);
51 if (i != 0)
52 return FAILED ("index failed. #0: %d", i);
54 i = g_slist_index (list, bar);
55 if (i != 1)
56 return FAILED ("index failed. #1: %d", i);
58 i = g_slist_index (list, baz);
59 if (i != 2)
60 return FAILED ("index failed. #2: %d", i);
62 g_slist_free (list);
63 return OK;
66 RESULT
67 test_slist_append ()
69 GSList *foo;
70 GSList *list = g_slist_append (NULL, "first");
71 if (g_slist_length (list) != 1)
72 return FAILED ("append(null,...) failed");
74 foo = g_slist_append (list, "second");
75 if (foo != list)
76 return FAILED ("changed list head on non-empty");
78 if (g_slist_length (list) != 2)
79 return FAILED ("Append failed");
81 g_slist_free (list);
82 return OK;
85 RESULT
86 test_slist_concat ()
88 GSList *foo = g_slist_prepend (NULL, "foo");
89 GSList *bar = g_slist_prepend (NULL, "bar");
91 GSList *list = g_slist_concat (foo, bar);
93 if (g_slist_length (list) != 2)
94 return FAILED ("Concat failed.");
96 g_slist_free (list);
97 return OK;
100 RESULT
101 test_slist_find ()
103 GSList *list = g_slist_prepend (NULL, "three");
104 GSList *found;
105 char *data;
107 list = g_slist_prepend (list, "two");
108 list = g_slist_prepend (list, "one");
110 data = "four";
111 list = g_slist_append (list, data);
113 found = g_slist_find (list, data);
115 if (found->data != data)
116 return FAILED ("Find failed");
118 g_slist_free (list);
119 return OK;
122 static gint
123 find_custom (gconstpointer a, gconstpointer b)
125 return(strcmp (a, b));
128 RESULT
129 test_slist_find_custom ()
131 GSList *list = NULL, *found;
132 char *foo = "foo";
133 char *bar = "bar";
134 char *baz = "baz";
136 list = g_slist_prepend (list, baz);
137 list = g_slist_prepend (list, bar);
138 list = g_slist_prepend (list, foo);
140 found = g_slist_find_custom (list, baz, find_custom);
142 if (found == NULL)
143 return FAILED ("Find failed");
145 g_slist_free (list);
147 return OK;
150 RESULT
151 test_slist_remove ()
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);
158 list = g_slist_remove (list, one);
160 if (g_slist_length (list) != 2)
161 return FAILED ("Remove failed");
163 if (strcmp ("two", list->data) != 0)
164 return FAILED ("Remove failed");
166 g_slist_free (list);
167 return OK;
170 RESULT
171 test_slist_remove_link ()
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;
178 foo = g_slist_concat (foo, bar);
179 foo = g_slist_concat (foo, baz);
181 list = g_slist_remove_link (list, bar);
183 if (g_slist_length (list) != 2)
184 return FAILED ("remove_link failed #1");
186 if (bar->next != NULL)
187 return FAILED ("remove_link failed #2");
189 g_slist_free (list);
190 g_slist_free (bar);
192 return OK;
195 static gint
196 compare (gconstpointer a, gconstpointer b)
198 char *foo = (char *) a;
199 char *bar = (char *) b;
201 if (strlen (foo) < strlen (bar))
202 return -1;
204 return 1;
207 RESULT
208 test_slist_insert_sorted ()
210 GSList *list = g_slist_prepend (NULL, "a");
211 list = g_slist_append (list, "aaa");
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");
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");
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");
228 g_slist_free (list);
229 return OK;
232 RESULT
233 test_slist_insert_before ()
235 GSList *foo, *bar, *baz;
237 foo = g_slist_prepend (NULL, "foo");
238 foo = g_slist_insert_before (foo, NULL, "bar");
239 bar = g_slist_last (foo);
241 if (strcmp (bar->data, "bar"))
242 return FAILED ("1");
244 baz = g_slist_insert_before (foo, bar, "baz");
245 if (foo != baz)
246 return FAILED ("2");
248 if (strcmp (foo->next->data, "baz"))
249 return FAILED ("3: %s", foo->next->data);
251 g_slist_free (foo);
252 return OK;
255 #define N_ELEMS 100
257 static int intcompare (gconstpointer p1, gconstpointer p2)
259 return GPOINTER_TO_INT (p1) - GPOINTER_TO_INT (p2);
262 static gboolean verify_sort (GSList *list, int len)
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;
272 if (len == 0)
273 return FALSE;
274 len--;
276 return len == 0;
279 RESULT
280 test_slist_sort ()
282 int i, j, mul;
283 GSList *list = NULL;
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");
291 g_slist_free (list);
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");
300 g_slist_free (list);
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));
307 list = g_slist_sort (list, intcompare);
308 if (!verify_sort (list, 2*N_ELEMS-1))
309 return FAILED ("alternating list");
311 g_slist_free (list);
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));
320 list = g_slist_sort (list, intcompare);
321 if (!verify_sort (list, (N_ELEMS*N_ELEMS - N_ELEMS)/2))
322 return FAILED ("wavering list");
324 g_slist_free (list);
326 return OK;
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}
344 DEFINE_TEST_GROUP_INIT(slist_tests_init, slist_tests)