9 GList *list = g_list_prepend (NULL, "foo");
11 if (g_list_length (list) != 1)
12 return "length failed. #1";
14 list = g_list_prepend (list, "bar");
15 if (g_list_length (list) != 2)
16 return "length failed. #2";
18 list = g_list_append (list, "bar");
19 if (g_list_length (list) != 3)
20 return "length failed. #3";
32 list = g_list_prepend (NULL, baz);
33 list = g_list_prepend (list, bar);
34 list = g_list_prepend (list, foo);
36 nth = g_list_nth (list, 0);
38 return "nth failed. #1";
40 nth = g_list_nth (list, 1);
42 return "nth failed. #2";
44 nth = g_list_nth (list, 2);
46 return "nth failed. #3";
59 list = g_list_prepend (NULL, baz);
60 list = g_list_prepend (list, bar);
61 list = g_list_prepend (list, foo);
63 i = g_list_index (list, foo);
65 return "index failed. #1";
67 i = g_list_index (list, bar);
69 return "index failed. #2";
71 i = g_list_index (list, baz);
73 return "index failed. #3";
81 GList *list = g_list_prepend (NULL, "first");
82 if (g_list_length (list) != 1)
83 return "Prepend failed";
85 g_list_append (list, g_list_prepend (NULL, "second"));
87 if (g_list_length (list) != 2)
88 return "Append failed";
96 GList *foo = g_list_prepend (NULL, "foo");
97 GList *bar = g_list_prepend (NULL, "bar");
100 foo = g_list_concat (foo, bar);
101 last = g_list_last (foo);
104 return "last failed. #1";
106 foo = g_list_concat (foo, g_list_prepend (NULL, "baz"));
107 foo = g_list_concat (foo, g_list_prepend (NULL, "quux"));
109 last = g_list_last (foo);
110 if (strcmp ("quux", last->data))
111 return "last failed. #2";
119 GList *foo = g_list_prepend (NULL, "foo");
120 GList *bar = g_list_prepend (NULL, "bar");
121 GList *list = g_list_concat (foo, bar);
123 if (g_list_length (list) != 2)
124 return "Concat failed. #1";
126 if (strcmp (list->data, "foo"))
127 return "Concat failed. #2";
129 if (strcmp (list->next->data, "bar"))
130 return "Concat failed. #3";
132 if (g_list_first (list) != foo)
133 return "Concat failed. #4";
135 if (g_list_last (list) != bar)
136 return "Concat failed. #5";
143 compare (gconstpointer a, gconstpointer b)
145 char *foo = (char *) a;
146 char *bar = (char *) b;
148 if (strlen (foo) < strlen (bar))
155 test_list_insert_sorted ()
157 GList *list = g_list_prepend (NULL, "a");
158 list = g_list_append (list, "aaa");
160 /* insert at the middle */
161 list = g_list_insert_sorted (list, "aa", compare);
162 if (strcmp ("aa", list->next->data))
163 return FAILED ("insert_sorted failed. #1");
165 /* insert at the beginning */
166 list = g_list_insert_sorted (list, "", compare);
168 if (strcmp ("", list->data))
169 return FAILED ("insert_sorted failed. #2");
171 /* insert at the end */
172 list = g_list_insert_sorted (list, "aaaa", compare);
173 if (strcmp ("aaaa", g_list_last (list)->data))
174 return FAILED ("insert_sorted failed. #3");
184 list = g_list_prepend (NULL, "a");
185 list = g_list_append (list, "aa");
186 list = g_list_append (list, "aaa");
187 list = g_list_append (list, "aaaa");
189 length = g_list_length (list);
190 copy = g_list_copy (list);
192 for (i = 0; i < length; i++)
193 if (strcmp (g_list_nth (list, i)->data,
194 g_list_nth (copy, i)->data))
195 return "copy failed.";
203 GList *list, *reverse;
204 list = g_list_prepend (NULL, "a");
205 list = g_list_append (list, "aa");
206 list = g_list_append (list, "aaa");
207 list = g_list_append (list, "aaaa");
209 length = g_list_length (list);
210 reverse = g_list_reverse (g_list_copy (list));
212 if (g_list_length (reverse) != length)
213 return "reverse failed #1";
215 for (i = 0; i < length; i++){
216 guint j = length - i - 1;
217 if (strcmp (g_list_nth (list, i)->data,
218 g_list_nth (reverse, j)->data))
219 return "reverse failed. #2";
224 static Test list_tests [] = {
225 {"list_length", test_list_length},
226 {"list_nth", test_list_nth},
227 {"list_index", test_list_index},
228 {"list_last", test_list_last},
229 {"list_append", test_list_append},
230 {"list_concat", test_list_concat},
231 {"list_insert_sorted", test_list_insert_sorted},
232 {"list_copy", test_list_copy},
233 {"list_reverse", test_list_reverse},
237 DEFINE_TEST_GROUP_INIT(list_tests_init, list_tests)