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. #1");
41 nth = g_list_nth (list, 1);
43 return FAILED ("nth failed. #2");
45 nth = g_list_nth (list, 2);
47 return FAILED ("nth failed. #3");
61 list = g_list_prepend (NULL, baz);
62 list = g_list_prepend (list, bar);
63 list = g_list_prepend (list, foo);
65 i = g_list_index (list, foo);
67 return FAILED ("index failed. #1");
69 i = g_list_index (list, bar);
71 return FAILED ("index failed. #2");
73 i = g_list_index (list, baz);
75 return FAILED ("index failed. #3");
84 GList *list = g_list_prepend (NULL, "first");
85 if (g_list_length (list) != 1)
86 return FAILED ("Prepend failed");
88 list = g_list_append (list, "second");
90 if (g_list_length (list) != 2)
91 return FAILED ("Append failed");
100 GList *foo = g_list_prepend (NULL, "foo");
101 GList *bar = g_list_prepend (NULL, "bar");
104 foo = g_list_concat (foo, bar);
105 last = g_list_last (foo);
108 return FAILED ("last failed. #1");
110 foo = g_list_concat (foo, g_list_prepend (NULL, "baz"));
111 foo = g_list_concat (foo, g_list_prepend (NULL, "quux"));
113 last = g_list_last (foo);
114 if (strcmp ("quux", last->data))
115 return FAILED ("last failed. #2");
125 GList *foo = g_list_prepend (NULL, "foo");
126 GList *bar = g_list_prepend (NULL, "bar");
127 GList *list = g_list_concat (foo, bar);
129 if (g_list_length (list) != 2)
130 return FAILED ("Concat failed. #1");
132 if (strcmp (list->data, "foo"))
133 return FAILED ("Concat failed. #2");
135 if (strcmp (list->next->data, "bar"))
136 return FAILED ("Concat failed. #3");
138 if (g_list_first (list) != foo)
139 return FAILED ("Concat failed. #4");
141 if (g_list_last (list) != bar)
142 return FAILED ("Concat failed. #5");
151 compare (gconstpointer a, gconstpointer b)
153 char *foo = (char *) a;
154 char *bar = (char *) b;
156 if (strlen (foo) < strlen (bar))
163 test_list_insert_sorted ()
165 GList *list = g_list_prepend (NULL, "a");
166 list = g_list_append (list, "aaa");
168 /* insert at the middle */
169 list = g_list_insert_sorted (list, "aa", compare);
170 if (strcmp ("aa", list->next->data))
171 return FAILED ("insert_sorted failed. #1");
173 /* insert at the beginning */
174 list = g_list_insert_sorted (list, "", compare);
175 if (strcmp ("", list->data))
176 return FAILED ("insert_sorted failed. #2");
178 /* insert at the end */
179 list = g_list_insert_sorted (list, "aaaa", compare);
180 if (strcmp ("aaaa", g_list_last (list)->data))
181 return FAILED ("insert_sorted failed. #3");
192 list = g_list_prepend (NULL, "a");
193 list = g_list_append (list, "aa");
194 list = g_list_append (list, "aaa");
195 list = g_list_append (list, "aaaa");
197 length = g_list_length (list);
198 copy = g_list_copy (list);
200 for (i = 0; i < length; i++)
201 if (strcmp (g_list_nth (list, i)->data,
202 g_list_nth (copy, i)->data))
203 return FAILED ("copy failed.");
214 GList *list, *reverse;
215 list = g_list_prepend (NULL, "a");
216 list = g_list_append (list, "aa");
217 list = g_list_append (list, "aaa");
218 list = g_list_append (list, "aaaa");
220 length = g_list_length (list);
221 reverse = g_list_reverse (g_list_copy (list));
223 if (g_list_length (reverse) != length)
224 return FAILED ("reverse failed #1");
226 for (i = 0; i < length; i++){
227 guint j = length - i - 1;
228 if (strcmp (g_list_nth (list, i)->data,
229 g_list_nth (reverse, j)->data))
230 return FAILED ("reverse failed. #2");
234 g_list_free (reverse);
241 GList *list = g_list_prepend (NULL, "three");
243 list = g_list_prepend (list, "two");
244 list = g_list_prepend (list, one);
246 list = g_list_remove (list, one);
248 if (g_list_length (list) != 2)
249 return FAILED ("Remove failed");
251 if (strcmp ("two", list->data) != 0)
252 return FAILED ("Remove failed");
259 test_list_remove_link ()
261 GList *foo = g_list_prepend (NULL, "a");
262 GList *bar = g_list_prepend (NULL, "b");
263 GList *baz = g_list_prepend (NULL, "c");
266 foo = g_list_concat (foo, bar);
267 foo = g_list_concat (foo, baz);
269 list = g_list_remove_link (list, bar);
271 if (g_list_length (list) != 2)
272 return FAILED ("remove_link failed #1");
274 if (bar->next != NULL)
275 return FAILED ("remove_link failed #2");
283 test_list_insert_before ()
287 foo = g_list_prepend (NULL, "foo");
288 foo = g_list_insert_before (foo, NULL, "bar");
289 bar = g_list_last (foo);
291 if (strcmp (bar->data, "bar"))
294 g_list_insert_before (foo, bar, "baz");
296 if (strcmp (g_list_nth (foo, 1)->data, "baz"))
302 static Test list_tests [] = {
303 {"list_length", test_list_length},
304 {"list_nth", test_list_nth},
305 {"list_index", test_list_index},
306 {"list_last", test_list_last},
307 {"list_append", test_list_append},
308 {"list_concat", test_list_concat},
309 {"list_insert_sorted", test_list_insert_sorted},
310 {"list_insert_before", test_list_insert_before},
311 {"list_copy", test_list_copy},
312 {"list_reverse", test_list_reverse},
313 {"list_remove", test_list_remove},
314 {"list_remove_link", test_list_remove_link},
318 DEFINE_TEST_GROUP_INIT(list_tests_init, list_tests)