9 GList *list = g_list_prepend (NULL, "foo");
11 if (g_list_length (list) != 1)
12 return FAILED ("length failed. #1");
14 list = g_list_prepend (list, "bar");
15 if (g_list_length (list) != 2)
16 return FAILED ("length failed. #2");
18 list = g_list_append (list, "bar");
19 if (g_list_length (list) != 3)
20 return FAILED ("length failed. #3");
33 list = g_list_prepend (NULL, baz);
34 list = g_list_prepend (list, bar);
35 list = g_list_prepend (list, foo);
37 nth = g_list_nth (list, 0);
39 return FAILED ("nth failed. #0");
41 nth = g_list_nth (list, 1);
43 return FAILED ("nth failed. #1");
45 nth = g_list_nth (list, 2);
47 return FAILED ("nth failed. #2");
49 nth = g_list_nth (list, 3);
51 return FAILED ("nth failed. #3: %s", nth->data);
65 list = g_list_prepend (NULL, baz);
66 list = g_list_prepend (list, bar);
67 list = g_list_prepend (list, foo);
69 i = g_list_index (list, foo);
71 return FAILED ("index failed. #0: %d", i);
73 i = g_list_index (list, bar);
75 return FAILED ("index failed. #1: %d", i);
77 i = g_list_index (list, baz);
79 return FAILED ("index failed. #2: %d", i);
88 GList *list = g_list_prepend (NULL, "first");
89 if (g_list_length (list) != 1)
90 return FAILED ("Prepend failed");
92 list = g_list_append (list, "second");
94 if (g_list_length (list) != 2)
95 return FAILED ("Append failed");
104 GList *foo = g_list_prepend (NULL, "foo");
105 GList *bar = g_list_prepend (NULL, "bar");
108 foo = g_list_concat (foo, bar);
109 last = g_list_last (foo);
112 return FAILED ("last failed. #1");
114 foo = g_list_concat (foo, g_list_prepend (NULL, "baz"));
115 foo = g_list_concat (foo, g_list_prepend (NULL, "quux"));
117 last = g_list_last (foo);
118 if (strcmp ("quux", last->data))
119 return FAILED ("last failed. #2");
129 GList *foo = g_list_prepend (NULL, "foo");
130 GList *bar = g_list_prepend (NULL, "bar");
131 GList *list = g_list_concat (foo, bar);
133 if (g_list_length (list) != 2)
134 return FAILED ("Concat failed. #1");
136 if (strcmp (list->data, "foo"))
137 return FAILED ("Concat failed. #2");
139 if (strcmp (list->next->data, "bar"))
140 return FAILED ("Concat failed. #3");
142 if (g_list_first (list) != foo)
143 return FAILED ("Concat failed. #4");
145 if (g_list_last (list) != bar)
146 return FAILED ("Concat failed. #5");
155 compare (gconstpointer a, gconstpointer b)
157 char *foo = (char *) a;
158 char *bar = (char *) b;
160 if (strlen (foo) < strlen (bar))
167 test_list_insert_sorted ()
169 GList *list = g_list_prepend (NULL, "a");
170 list = g_list_append (list, "aaa");
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");
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");
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");
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");
201 length = g_list_length (list);
202 copy = g_list_copy (list);
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.");
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");
224 length = g_list_length (list);
225 reverse = g_list_reverse (g_list_copy (list));
227 if (g_list_length (reverse) != length)
228 return FAILED ("reverse failed #1");
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");
238 g_list_free (reverse);
245 GList *list = g_list_prepend (NULL, "three");
247 list = g_list_prepend (list, "two");
248 list = g_list_prepend (list, one);
250 list = g_list_remove (list, one);
252 if (g_list_length (list) != 2)
253 return FAILED ("Remove failed");
255 if (strcmp ("two", list->data) != 0)
256 return FAILED ("Remove failed");
263 test_list_remove_link ()
265 GList *foo = g_list_prepend (NULL, "a");
266 GList *bar = g_list_prepend (NULL, "b");
267 GList *baz = g_list_prepend (NULL, "c");
270 foo = g_list_concat (foo, bar);
271 foo = g_list_concat (foo, baz);
273 list = g_list_remove_link (list, bar);
275 if (g_list_length (list) != 2)
276 return FAILED ("remove_link failed #1");
278 if (bar->next != NULL)
279 return FAILED ("remove_link failed #2");
287 test_list_insert_before ()
289 GList *foo, *bar, *baz;
291 foo = g_list_prepend (NULL, "foo");
292 foo = g_list_insert_before (foo, NULL, "bar");
293 bar = g_list_last (foo);
295 if (strcmp (bar->data, "bar"))
298 baz = g_list_insert_before (foo, bar, "baz");
302 if (strcmp (g_list_nth_data (foo, 1), "baz"))
303 return FAILED ("3: %s", g_list_nth_data (foo, 1));
311 static int intcompare (gconstpointer p1, gconstpointer p2)
313 return GPOINTER_TO_INT (p1) - GPOINTER_TO_INT (p2);
316 static gboolean verify_sort (GList *list, int len)
323 prev = GPOINTER_TO_INT (list->data);
325 for (list = list->next; list; list = list->next) {
326 int curr = GPOINTER_TO_INT (list->data);
331 if (!list->prev || list->prev->next != list)
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");
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");
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));
369 list = g_list_sort (list, intcompare);
370 if (!verify_sort (list, 2*N_ELEMS-1))
371 return FAILED ("alternating list");
377 for (i = 1; i < N_ELEMS; ++i) {
379 for (j = 0; j < i; ++j)
380 list = g_list_prepend (list, GINT_TO_POINTER (mul * j));
382 list = g_list_sort (list, intcompare);
383 if (!verify_sort (list, (N_ELEMS*N_ELEMS - N_ELEMS)/2))
384 return FAILED ("wavering list");
392 find_custom (gconstpointer a, gconstpointer b)
394 return(strcmp (a, b));
398 test_list_find_custom ()
400 GList *list = NULL, *found;
405 list = g_list_prepend (list, baz);
406 list = g_list_prepend (list, bar);
407 list = g_list_prepend (list, foo);
409 found = g_list_find_custom (list, baz, find_custom);
412 return FAILED ("Find failed");
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},
438 DEFINE_TEST_GROUP_INIT(list_tests_init, list_tests)