4 #define NUMBER_MIN 0000
5 #define NUMBER_MAX 9999
8 static guint32 array[SIZE];
12 sort (gconstpointer p1, gconstpointer p2)
16 a = GPOINTER_TO_INT (p1);
17 b = GPOINTER_TO_INT (p2);
19 return (a > b ? +1 : a == b ? 0 : -1);
31 for (i = 0; i < SIZE; i++)
32 list = g_list_append (list, GINT_TO_POINTER (array[i]));
34 list = g_list_sort (list, sort);
35 for (i = 0; i < SIZE - 1; i++)
39 p1 = g_list_nth_data (list, i);
40 p2 = g_list_nth_data (list, i+1);
42 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
49 test_list_sort_with_data (void)
54 for (i = 0; i < SIZE; i++)
55 list = g_list_append (list, GINT_TO_POINTER (array[i]));
57 list = g_list_sort_with_data (list, (GCompareDataFunc)sort, NULL);
58 for (i = 0; i < SIZE - 1; i++)
62 p1 = g_list_nth_data (list, i);
63 p2 = g_list_nth_data (list, i+1);
65 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
72 test_list_insert_sorted (void)
77 for (i = 0; i < SIZE; i++)
78 list = g_list_insert_sorted (list, GINT_TO_POINTER (array[i]), sort);
80 for (i = 0; i < SIZE - 1; i++)
84 p1 = g_list_nth_data (list, i);
85 p2 = g_list_nth_data (list, i+1);
87 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
94 test_list_insert_sorted_with_data (void)
99 for (i = 0; i < SIZE; i++)
100 list = g_list_insert_sorted_with_data (list,
101 GINT_TO_POINTER (array[i]),
102 (GCompareDataFunc)sort,
105 for (i = 0; i < SIZE - 1; i++)
109 p1 = g_list_nth_data (list, i);
110 p2 = g_list_nth_data (list, i+1);
112 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
119 test_list_reverse (void)
123 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
126 for (i = 0; i < 10; i++)
127 list = g_list_append (list, &nums[i]);
129 list = g_list_reverse (list);
131 for (i = 0; i < 10; i++)
133 st = g_list_nth (list, i);
134 g_assert (*((gint*) st->data) == (9 - i));
145 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
148 for (i = 0; i < 10; i++)
149 list = g_list_append (list, &nums[i]);
151 for (i = 0; i < 10; i++)
153 st = g_list_nth (list, i);
154 g_assert (*((gint*) st->data) == i);
161 test_list_concat (void)
166 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
169 for (i = 0; i < 5; i++)
171 list1 = g_list_append (list1, &nums[i]);
172 list2 = g_list_append (list2, &nums[i+5]);
175 g_assert_cmpint (g_list_length (list1), ==, 5);
176 g_assert_cmpint (g_list_length (list2), ==, 5);
178 list1 = g_list_concat (list1, list2);
180 g_assert_cmpint (g_list_length (list1), ==, 10);
182 for (i = 0; i < 10; i++)
184 st = g_list_nth (list1, i);
185 g_assert (*((gint*) st->data) == i);
188 list2 = g_list_concat (NULL, list1);
189 g_assert_cmpint (g_list_length (list2), ==, 10);
191 list2 = g_list_concat (list1, NULL);
192 g_assert_cmpint (g_list_length (list2), ==, 10);
194 list2 = g_list_concat (NULL, NULL);
195 g_assert (list2 == NULL);
201 test_list_remove (void)
205 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
208 for (i = 0; i < 10; i++)
210 list = g_list_append (list, &nums[i]);
211 list = g_list_append (list, &nums[i]);
214 g_assert_cmpint (g_list_length (list), ==, 20);
216 for (i = 0; i < 10; i++)
218 list = g_list_remove (list, &nums[i]);
221 g_assert_cmpint (g_list_length (list), ==, 10);
223 for (i = 0; i < 10; i++)
225 st = g_list_nth (list, i);
226 g_assert (*((gint*) st->data) == i);
233 test_list_remove_all (void)
236 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
239 for (i = 0; i < 10; i++)
241 list = g_list_append (list, &nums[i]);
242 list = g_list_append (list, &nums[i]);
245 g_assert_cmpint (g_list_length (list), ==, 20);
247 for (i = 0; i < 5; i++)
249 list = g_list_remove_all (list, &nums[2 * i + 1]);
250 list = g_list_remove_all (list, &nums[8 - 2 * i]);
253 g_assert_cmpint (g_list_length (list), ==, 0);
254 g_assert (list == NULL);
258 test_list_first_last (void)
262 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
265 for (i = 0; i < 10; i++)
266 list = g_list_append (list, &nums[i]);
268 st = g_list_last (list);
269 g_assert (*((gint*) st->data) == 9);
270 st = g_list_nth_prev (st, 3);
271 g_assert (*((gint*) st->data) == 6);
272 st = g_list_first (st);
273 g_assert (*((gint*) st->data) == 0);
279 test_list_insert (void)
283 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
286 list = g_list_insert_before (NULL, NULL, &nums[1]);
287 list = g_list_insert (list, &nums[3], 1);
288 list = g_list_insert (list, &nums[4], -1);
289 list = g_list_insert (list, &nums[0], 0);
290 list = g_list_insert (list, &nums[5], 100);
291 list = g_list_insert_before (list, NULL, &nums[6]);
292 list = g_list_insert_before (list, list->next->next, &nums[2]);
294 list = g_list_insert (list, &nums[9], 7);
295 list = g_list_insert (list, &nums[8], 7);
296 list = g_list_insert (list, &nums[7], 7);
298 for (i = 0; i < 10; i++)
300 st = g_list_nth (list, i);
301 g_assert (*((gint*) st->data) == i);
314 free_func (gpointer data)
316 ListItem *item = data;
326 item = g_slice_new (ListItem);
334 test_free_full (void)
336 ListItem *one, *two, *three;
337 GSList *slist = NULL;
340 slist = g_slist_prepend (slist, one = new_item (1));
341 slist = g_slist_prepend (slist, two = new_item (2));
342 slist = g_slist_prepend (slist, three = new_item (3));
343 g_assert (!one->freed);
344 g_assert (!two->freed);
345 g_assert (!three->freed);
346 g_slist_free_full (slist, free_func);
347 g_assert (one->freed);
348 g_assert (two->freed);
349 g_assert (three->freed);
350 g_slice_free (ListItem, one);
351 g_slice_free (ListItem, two);
352 g_slice_free (ListItem, three);
354 list = g_list_prepend (list, one = new_item (1));
355 list = g_list_prepend (list, two = new_item (2));
356 list = g_list_prepend (list, three = new_item (3));
357 g_assert (!one->freed);
358 g_assert (!two->freed);
359 g_assert (!three->freed);
360 g_list_free_full (list, free_func);
361 g_assert (one->freed);
362 g_assert (two->freed);
363 g_assert (three->freed);
364 g_slice_free (ListItem, one);
365 g_slice_free (ListItem, two);
366 g_slice_free (ListItem, three);
370 test_list_copy (void)
376 l = g_list_append (l, GINT_TO_POINTER (1));
377 l = g_list_append (l, GINT_TO_POINTER (2));
378 l = g_list_append (l, GINT_TO_POINTER (3));
380 l2 = g_list_copy (l);
382 for (u = l, v = l2; u && v; u = u->next, v = v->next)
384 g_assert (u->data == v->data);
392 test_delete_link (void)
397 l = g_list_append (l, GINT_TO_POINTER (1));
398 l = g_list_append (l, GINT_TO_POINTER (2));
399 l = g_list_append (l, GINT_TO_POINTER (3));
403 l = g_list_delete_link (l, l2);
404 g_assert (l->data == GINT_TO_POINTER (1));
405 g_assert (l->next->data == GINT_TO_POINTER (3));
416 l = g_list_prepend (l, "c");
417 l = g_list_prepend (l, "a");
419 g_assert (l->data == (gpointer)"a");
420 g_assert (l->next->data == (gpointer)"c");
421 g_assert (l->next->next == NULL);
424 l2 = g_list_prepend (l2, "b");
425 g_assert (l2->prev == l);
427 g_assert (l->data == (gpointer)"a");
428 g_assert (l->next->data == (gpointer)"b");
429 g_assert (l->next->next->data == (gpointer)"c");
430 g_assert (l->next->next->next == NULL);
441 l = g_list_append (l, "a");
442 l = g_list_append (l, "b");
443 l = g_list_append (l, "c");
445 ll = g_list_find (l, "a");
446 g_assert_cmpint (g_list_position (l, ll), ==, 0);
447 g_assert_cmpint (g_list_index (l, "a"), ==, 0);
448 ll = g_list_find (l, "b");
449 g_assert_cmpint (g_list_position (l, ll), ==, 1);
450 g_assert_cmpint (g_list_index (l, "b"), ==, 1);
451 ll = g_list_find (l, "c");
452 g_assert_cmpint (g_list_position (l, ll), ==, 2);
453 g_assert_cmpint (g_list_index (l, "c"), ==, 2);
455 ll = g_list_append (NULL, "d");
456 g_assert_cmpint (g_list_position (l, ll), ==, -1);
457 g_assert_cmpint (g_list_index (l, "d"), ==, -1);
464 main (int argc, char *argv[])
468 g_test_init (&argc, &argv, NULL);
470 /* Create an array of random numbers. */
471 for (i = 0; i < SIZE; i++)
472 array[i] = g_test_rand_int_range (NUMBER_MIN, NUMBER_MAX);
474 g_test_add_func ("/list/sort", test_list_sort);
475 g_test_add_func ("/list/sort-with-data", test_list_sort_with_data);
476 g_test_add_func ("/list/insert-sorted", test_list_insert_sorted);
477 g_test_add_func ("/list/insert-sorted-with-data", test_list_insert_sorted_with_data);
478 g_test_add_func ("/list/reverse", test_list_reverse);
479 g_test_add_func ("/list/nth", test_list_nth);
480 g_test_add_func ("/list/concat", test_list_concat);
481 g_test_add_func ("/list/remove", test_list_remove);
482 g_test_add_func ("/list/remove-all", test_list_remove_all);
483 g_test_add_func ("/list/first-last", test_list_first_last);
484 g_test_add_func ("/list/insert", test_list_insert);
485 g_test_add_func ("/list/free-full", test_free_full);
486 g_test_add_func ("/list/copy", test_list_copy);
487 g_test_add_func ("/list/delete-link", test_delete_link);
488 g_test_add_func ("/list/prepend", test_prepend);
489 g_test_add_func ("/list/position", test_position);
491 return g_test_run ();