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);
20 return FAILED ("nth failed. #0");
22 nth = g_slist_nth (list, 1);
24 return FAILED ("nth failed. #1");
26 nth = g_slist_nth (list, 2);
28 return FAILED ("nth failed. #2");
30 nth = g_slist_nth (list, 3);
32 return FAILED ("nth failed. #3: %s", nth->data);
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);
52 return FAILED ("index failed. #0: %d", i);
54 i = g_slist_index (list, bar);
56 return FAILED ("index failed. #1: %d", i);
58 i = g_slist_index (list, baz);
60 return FAILED ("index failed. #2: %d", i);
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");
76 return FAILED ("changed list head on non-empty");
78 if (g_slist_length (list) != 2)
79 return FAILED ("Append failed");
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.");
103 GSList *list = g_slist_prepend (NULL, "three");
107 list = g_slist_prepend (list, "two");
108 list = g_slist_prepend (list, "one");
111 list = g_slist_append (list, data);
113 found = g_slist_find (list, data);
115 if (found->data != data)
116 return FAILED ("Find failed");
123 find_custom (gconstpointer a, gconstpointer b)
125 return(strcmp (a, b));
129 test_slist_find_custom ()
131 GSList *list = NULL, *found;
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);
143 return FAILED ("Find failed");
153 GSList *list = g_slist_prepend (NULL, "three");
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");
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");
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");
196 compare (gconstpointer a, gconstpointer b)
198 char *foo = (char *) a;
199 char *bar = (char *) b;
201 if (strlen (foo) < strlen (bar))
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");
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"))
244 baz = g_slist_insert_before (foo, bar, "baz");
248 if (strcmp (foo->next->data, "baz"))
249 return FAILED ("3: %s", foo->next->data);
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);
266 for (list = list->next; list; list = list->next) {
267 int curr = GPOINTER_TO_INT (list->data);
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");
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");
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");
315 for (i = 1; i < N_ELEMS; ++i) {
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");
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},
344 DEFINE_TEST_GROUP_INIT(slist_tests_init, slist_tests)