5 #define NUMBER_MIN 0000
6 #define NUMBER_MAX 9999
9 static guint32 array[SIZE];
13 sort (gconstpointer p1, gconstpointer p2)
17 a = GPOINTER_TO_INT (p1);
18 b = GPOINTER_TO_INT (p2);
20 return (a > b ? +1 : a == b ? 0 : -1);
32 for (i = 0; i < SIZE; i++)
33 list = g_list_append (list, GINT_TO_POINTER (array[i]));
35 list = g_list_sort (list, sort);
36 for (i = 0; i < SIZE - 1; i++)
40 p1 = g_list_nth_data (list, i);
41 p2 = g_list_nth_data (list, i+1);
43 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
50 test_list_sort_with_data (void)
55 for (i = 0; i < SIZE; i++)
56 list = g_list_append (list, GINT_TO_POINTER (array[i]));
58 list = g_list_sort_with_data (list, (GCompareDataFunc)sort, NULL);
59 for (i = 0; i < SIZE - 1; i++)
63 p1 = g_list_nth_data (list, i);
64 p2 = g_list_nth_data (list, i+1);
66 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
73 test_list_insert_sorted (void)
78 for (i = 0; i < SIZE; i++)
79 list = g_list_insert_sorted (list, GINT_TO_POINTER (array[i]), sort);
81 for (i = 0; i < SIZE - 1; i++)
85 p1 = g_list_nth_data (list, i);
86 p2 = g_list_nth_data (list, i+1);
88 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
95 test_list_insert_sorted_with_data (void)
100 for (i = 0; i < SIZE; i++)
101 list = g_list_insert_sorted_with_data (list,
102 GINT_TO_POINTER (array[i]),
103 (GCompareDataFunc)sort,
106 for (i = 0; i < SIZE - 1; i++)
110 p1 = g_list_nth_data (list, i);
111 p2 = g_list_nth_data (list, i+1);
113 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
120 test_list_reverse (void)
124 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
127 for (i = 0; i < 10; i++)
128 list = g_list_append (list, &nums[i]);
130 list = g_list_reverse (list);
132 for (i = 0; i < 10; i++)
134 st = g_list_nth (list, i);
135 g_assert (*((gint*) st->data) == (9 - i));
146 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
149 for (i = 0; i < 10; i++)
150 list = g_list_append (list, &nums[i]);
152 for (i = 0; i < 10; i++)
154 st = g_list_nth (list, i);
155 g_assert (*((gint*) st->data) == i);
162 test_list_concat (void)
167 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
170 for (i = 0; i < 5; i++)
172 list1 = g_list_append (list1, &nums[i]);
173 list2 = g_list_append (list2, &nums[i+5]);
176 g_assert_cmpint (g_list_length (list1), ==, 5);
177 g_assert_cmpint (g_list_length (list2), ==, 5);
179 list1 = g_list_concat (list1, list2);
181 g_assert_cmpint (g_list_length (list1), ==, 10);
183 for (i = 0; i < 10; i++)
185 st = g_list_nth (list1, i);
186 g_assert (*((gint*) st->data) == i);
189 list2 = g_list_concat (NULL, list1);
190 g_assert_cmpint (g_list_length (list2), ==, 10);
192 list2 = g_list_concat (list1, NULL);
193 g_assert_cmpint (g_list_length (list2), ==, 10);
195 list2 = g_list_concat (NULL, NULL);
196 g_assert (list2 == NULL);
202 test_list_remove (void)
206 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
209 for (i = 0; i < 10; i++)
211 list = g_list_append (list, &nums[i]);
212 list = g_list_append (list, &nums[i]);
215 g_assert_cmpint (g_list_length (list), ==, 20);
217 for (i = 0; i < 10; i++)
219 list = g_list_remove (list, &nums[i]);
222 g_assert_cmpint (g_list_length (list), ==, 10);
224 for (i = 0; i < 10; i++)
226 st = g_list_nth (list, i);
227 g_assert (*((gint*) st->data) == i);
234 test_list_remove_all (void)
237 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
240 for (i = 0; i < 10; i++)
242 list = g_list_append (list, &nums[i]);
243 list = g_list_append (list, &nums[i]);
246 g_assert_cmpint (g_list_length (list), ==, 20);
248 for (i = 0; i < 5; i++)
250 list = g_list_remove_all (list, &nums[2 * i + 1]);
251 list = g_list_remove_all (list, &nums[8 - 2 * i]);
254 g_assert_cmpint (g_list_length (list), ==, 0);
255 g_assert (list == NULL);
259 test_list_first_last (void)
263 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
266 for (i = 0; i < 10; i++)
267 list = g_list_append (list, &nums[i]);
269 st = g_list_last (list);
270 g_assert (*((gint*) st->data) == 9);
271 st = g_list_nth_prev (st, 3);
272 g_assert (*((gint*) st->data) == 6);
273 st = g_list_first (st);
274 g_assert (*((gint*) st->data) == 0);
280 test_list_insert (void)
284 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
287 list = g_list_insert_before (NULL, NULL, &nums[1]);
288 list = g_list_insert (list, &nums[3], 1);
289 list = g_list_insert (list, &nums[4], -1);
290 list = g_list_insert (list, &nums[0], 0);
291 list = g_list_insert (list, &nums[5], 100);
292 list = g_list_insert_before (list, NULL, &nums[6]);
293 list = g_list_insert_before (list, list->next->next, &nums[2]);
295 list = g_list_insert (list, &nums[9], 7);
296 list = g_list_insert (list, &nums[8], 7);
297 list = g_list_insert (list, &nums[7], 7);
299 for (i = 0; i < 10; i++)
301 st = g_list_nth (list, i);
302 g_assert (*((gint*) st->data) == i);
315 free_func (gpointer data)
317 ListItem *item = data;
327 item = g_slice_new (ListItem);
335 test_free_full (void)
337 ListItem *one, *two, *three;
338 GSList *slist = NULL;
341 slist = g_slist_prepend (slist, one = new_item (1));
342 slist = g_slist_prepend (slist, two = new_item (2));
343 slist = g_slist_prepend (slist, three = new_item (3));
344 g_assert (!one->freed);
345 g_assert (!two->freed);
346 g_assert (!three->freed);
347 g_slist_free_full (slist, free_func);
348 g_assert (one->freed);
349 g_assert (two->freed);
350 g_assert (three->freed);
351 g_slice_free (ListItem, one);
352 g_slice_free (ListItem, two);
353 g_slice_free (ListItem, three);
355 list = g_list_prepend (list, one = new_item (1));
356 list = g_list_prepend (list, two = new_item (2));
357 list = g_list_prepend (list, three = new_item (3));
358 g_assert (!one->freed);
359 g_assert (!two->freed);
360 g_assert (!three->freed);
361 g_list_free_full (list, free_func);
362 g_assert (one->freed);
363 g_assert (two->freed);
364 g_assert (three->freed);
365 g_slice_free (ListItem, one);
366 g_slice_free (ListItem, two);
367 g_slice_free (ListItem, three);
371 test_list_copy (void)
377 l = g_list_append (l, GINT_TO_POINTER (1));
378 l = g_list_append (l, GINT_TO_POINTER (2));
379 l = g_list_append (l, GINT_TO_POINTER (3));
381 l2 = g_list_copy (l);
383 for (u = l, v = l2; u && v; u = u->next, v = v->next)
385 g_assert (u->data == v->data);
393 multiply_value (gconstpointer value, gpointer data)
395 return GINT_TO_POINTER (GPOINTER_TO_INT (value) * GPOINTER_TO_INT (data));
399 test_list_copy_deep (void)
405 l = g_list_append (l, GINT_TO_POINTER (1));
406 l = g_list_append (l, GINT_TO_POINTER (2));
407 l = g_list_append (l, GINT_TO_POINTER (3));
409 l2 = g_list_copy_deep (l, multiply_value, GINT_TO_POINTER (2));
411 for (u = l, v = l2; u && v; u = u->next, v = v->next)
413 g_assert_cmpint (GPOINTER_TO_INT (u->data) * 2, ==, GPOINTER_TO_INT (v->data));
421 test_delete_link (void)
426 l = g_list_append (l, GINT_TO_POINTER (1));
427 l = g_list_append (l, GINT_TO_POINTER (2));
428 l = g_list_append (l, GINT_TO_POINTER (3));
432 l = g_list_delete_link (l, l2);
433 g_assert (l->data == GINT_TO_POINTER (1));
434 g_assert (l->next->data == GINT_TO_POINTER (3));
445 l = g_list_prepend (l, "c");
446 l = g_list_prepend (l, "a");
448 g_assert (l->data == (gpointer)"a");
449 g_assert (l->next->data == (gpointer)"c");
450 g_assert (l->next->next == NULL);
453 l2 = g_list_prepend (l2, "b");
454 g_assert (l2->prev == l);
456 g_assert (l->data == (gpointer)"a");
457 g_assert (l->next->data == (gpointer)"b");
458 g_assert (l->next->next->data == (gpointer)"c");
459 g_assert (l->next->next->next == NULL);
470 l = g_list_append (l, "a");
471 l = g_list_append (l, "b");
472 l = g_list_append (l, "c");
474 ll = g_list_find (l, "a");
475 g_assert_cmpint (g_list_position (l, ll), ==, 0);
476 g_assert_cmpint (g_list_index (l, "a"), ==, 0);
477 ll = g_list_find (l, "b");
478 g_assert_cmpint (g_list_position (l, ll), ==, 1);
479 g_assert_cmpint (g_list_index (l, "b"), ==, 1);
480 ll = g_list_find (l, "c");
481 g_assert_cmpint (g_list_position (l, ll), ==, 2);
482 g_assert_cmpint (g_list_index (l, "c"), ==, 2);
484 ll = g_list_append (NULL, "d");
485 g_assert_cmpint (g_list_position (l, ll), ==, -1);
486 g_assert_cmpint (g_list_index (l, "d"), ==, -1);
493 test_double_free (void)
496 GList intruder = { NULL, (gpointer)0xDEADBEEF, (gpointer)0xDEADBEEF };
498 if (g_test_subprocess ())
501 list = g_list_append (list, "a");
502 link = list = g_list_append (list, "b");
503 list = g_list_append (list, "c");
505 list = g_list_remove_link (list, link);
507 link->next = &intruder;
508 list = g_list_remove_link (list, link);
514 g_test_trap_subprocess (NULL, 0, 0);
515 g_test_trap_assert_failed ();
516 g_test_trap_assert_stderr ("*corrupted double-linked list detected*");
520 main (int argc, char *argv[])
524 g_test_init (&argc, &argv, NULL);
526 /* Create an array of random numbers. */
527 for (i = 0; i < SIZE; i++)
528 array[i] = g_test_rand_int_range (NUMBER_MIN, NUMBER_MAX);
530 g_test_add_func ("/list/sort", test_list_sort);
531 g_test_add_func ("/list/sort-with-data", test_list_sort_with_data);
532 g_test_add_func ("/list/insert-sorted", test_list_insert_sorted);
533 g_test_add_func ("/list/insert-sorted-with-data", test_list_insert_sorted_with_data);
534 g_test_add_func ("/list/reverse", test_list_reverse);
535 g_test_add_func ("/list/nth", test_list_nth);
536 g_test_add_func ("/list/concat", test_list_concat);
537 g_test_add_func ("/list/remove", test_list_remove);
538 g_test_add_func ("/list/remove-all", test_list_remove_all);
539 g_test_add_func ("/list/first-last", test_list_first_last);
540 g_test_add_func ("/list/insert", test_list_insert);
541 g_test_add_func ("/list/free-full", test_free_full);
542 g_test_add_func ("/list/copy", test_list_copy);
543 g_test_add_func ("/list/copy-deep", test_list_copy_deep);
544 g_test_add_func ("/list/delete-link", test_delete_link);
545 g_test_add_func ("/list/prepend", test_prepend);
546 g_test_add_func ("/list/position", test_position);
547 g_test_add_func ("/list/double-free", test_double_free);
549 return g_test_run ();