1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-list.c Generic linked list utility (internal to D-BUS implementation)
4 * Copyright (C) 2002 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-list.h"
26 #include "dbus-mempool.h"
27 #include "dbus-threads.h"
30 * @defgroup DBusList Linked list
31 * @ingroup DBusInternals
32 * @brief DBusList data structure
34 * Types and functions related to DBusList.
37 static DBusMemPool *list_pool;
38 _DBUS_DEFINE_GLOBAL_LOCK (list);
41 * @defgroup DBusListInternals Linked list implementation details
42 * @ingroup DBusInternals
43 * @brief DBusList implementation details
45 * The guts of DBusList.
50 /* the mem pool is probably a speed hit, with the thread
51 * lock, though it does still save memory - unknown.
54 alloc_link (void *data)
58 if (!_DBUS_LOCK (list))
61 if (list_pool == NULL)
63 list_pool = _dbus_mem_pool_new (sizeof (DBusList), TRUE);
65 if (list_pool == NULL)
71 link = _dbus_mem_pool_alloc (list_pool);
74 _dbus_mem_pool_free (list_pool);
82 link = _dbus_mem_pool_alloc (list_pool);
94 free_link (DBusList *link)
97 if (_dbus_mem_pool_dealloc (list_pool, link))
99 _dbus_mem_pool_free (list_pool);
107 link_before (DBusList **list,
108 DBusList *before_this_link,
119 link->next = before_this_link;
120 link->prev = before_this_link->prev;
121 before_this_link->prev = link;
122 link->prev->next = link;
124 if (before_this_link == *list)
130 link_after (DBusList **list,
131 DBusList *after_this_link,
142 link->prev = after_this_link;
143 link->next = after_this_link->next;
144 after_this_link->next = link;
145 link->next->prev = link;
152 * @addtogroup DBusList
159 * A node in a linked list.
161 * DBusList is a circular list; that is, the tail of the list
162 * points back to the head of the list. The empty list is
163 * represented by a #NULL pointer.
167 * @def _dbus_list_get_next_link
169 * Gets the next link in the list, or #NULL if
170 * there are no more links. Used for iteration.
174 * link = _dbus_list_get_first_link (&list);
175 * while (link != NULL)
177 * printf ("value is %p\n", link->data);
178 * link = _dbus_list_get_next_link (&link);
182 * @param list address of the list head.
183 * @param link current link.
184 * @returns the next link, or %NULL if none.
189 * @def _dbus_list_get_prev_link
191 * Gets the previous link in the list, or #NULL if
192 * there are no more links. Used for iteration.
196 * link = _dbus_list_get_last_link (&list);
197 * while (link != NULL)
199 * printf ("value is %p\n", link->data);
200 * link = _dbus_list_get_prev_link (&link);
204 * @param list address of the list head.
205 * @param link current link.
206 * @returns the previous link, or %NULL if none.
211 * Allocates a linked list node. Useful for preallocating
212 * nodes and using _dbus_list_append_link() to avoid
215 * @param data the value to store in the link.
216 * @returns a newly allocated link.
219 _dbus_list_alloc_link (void *data)
221 return alloc_link (data);
225 * Frees a linked list node allocated with _dbus_list_alloc_link.
226 * Does not free the data in the node.
228 * @param link the list node
231 _dbus_list_free_link (DBusList *link)
238 * Appends a value to the list. May return #FALSE
239 * if insufficient memory exists to add a list link.
240 * This is a constant-time operation.
242 * @param list address of the list head.
243 * @param data the value to append.
244 * @returns #TRUE on success.
247 _dbus_list_append (DBusList **list,
250 if (!_dbus_list_prepend (list, data))
253 /* Now cycle the list forward one so the prepended node is the tail */
254 *list = (*list)->next;
260 * Prepends a value to the list. May return #FALSE
261 * if insufficient memory exists to add a list link.
262 * This is a constant-time operation.
264 * @param list address of the list head.
265 * @param data the value to prepend.
266 * @returns #TRUE on success.
269 _dbus_list_prepend (DBusList **list,
274 link = alloc_link (data);
278 link_before (list, *list, link);
284 * Appends a link to the list.
285 * Cannot fail due to out of memory.
286 * This is a constant-time operation.
288 * @param list address of the list head.
289 * @param link the link to append.
292 _dbus_list_append_link (DBusList **list,
295 _dbus_list_prepend_link (list, link);
297 /* Now cycle the list forward one so the prepended node is the tail */
298 *list = (*list)->next;
302 * Prepends a link to the list.
303 * Cannot fail due to out of memory.
304 * This is a constant-time operation.
306 * @param list address of the list head.
307 * @param link the link to prepend.
310 _dbus_list_prepend_link (DBusList **list,
313 link_before (list, *list, link);
317 * Inserts data into the list before the given existing link.
319 * @param list the list to modify
320 * @param before_this_link existing link to insert before, or #NULL to append
321 * @param data the value to insert
322 * @returns #TRUE on success, #FALSE if memory allocation fails
325 _dbus_list_insert_before (DBusList **list,
326 DBusList *before_this_link,
331 if (before_this_link == NULL)
332 return _dbus_list_append (list, data);
335 link = alloc_link (data);
339 link_before (list, before_this_link, link);
346 * Inserts data into the list after the given existing link.
348 * @param list the list to modify
349 * @param after_this_link existing link to insert after, or #NULL to prepend
350 * @param data the value to insert
351 * @returns #TRUE on success, #FALSE if memory allocation fails
354 _dbus_list_insert_after (DBusList **list,
355 DBusList *after_this_link,
360 if (after_this_link == NULL)
361 return _dbus_list_prepend (list, data);
364 link = alloc_link (data);
368 link_after (list, after_this_link, link);
375 * Inserts a link into the list before the given existing link.
377 * @param list the list to modify
378 * @param before_this_link existing link to insert before, or #NULL to append
379 * @param link the link to insert
382 _dbus_list_insert_before_link (DBusList **list,
383 DBusList *before_this_link,
386 if (before_this_link == NULL)
387 _dbus_list_append_link (list, link);
389 link_before (list, before_this_link, link);
393 * Inserts a link into the list after the given existing link.
395 * @param list the list to modify
396 * @param after_this_link existing link to insert after, or #NULL to prepend
397 * @param link the link to insert
400 _dbus_list_insert_after_link (DBusList **list,
401 DBusList *after_this_link,
404 if (after_this_link == NULL)
405 _dbus_list_prepend_link (list, link);
407 link_after (list, after_this_link, link);
411 * Removes a value from the list. Only removes the
412 * first value equal to the given data pointer,
413 * even if multiple values exist which match.
414 * This is a linear-time operation.
416 * @param list address of the list head.
417 * @param data the value to remove.
418 * @returns #TRUE if a value was found to remove.
421 _dbus_list_remove (DBusList **list,
429 if (link->data == data)
431 _dbus_list_remove_link (list, link);
435 link = _dbus_list_get_next_link (list, link);
442 * Removes a value from the list. Only removes the
443 * last value equal to the given data pointer,
444 * even if multiple values exist which match.
445 * This is a linear-time operation.
447 * @param list address of the list head.
448 * @param data the value to remove.
449 * @returns #TRUE if a value was found to remove.
452 _dbus_list_remove_last (DBusList **list,
457 link = _dbus_list_get_last_link (list);
461 if (link->data == data)
463 _dbus_list_remove_link (list, link);
467 link = _dbus_list_get_prev_link (list, link);
474 * Removes the given link from the list, but doesn't
475 * free it. _dbus_list_remove_link() both removes the
476 * link and also frees it.
478 * @param list the list
479 * @param link the link in the list
482 _dbus_list_unlink (DBusList **list,
485 if (link->next == link)
487 /* one-element list */
492 link->prev->next = link->next;
493 link->next->prev = link->prev;
504 * Removes a link from the list. This is a constant-time operation.
506 * @param list address of the list head.
507 * @param link the list link to remove.
510 _dbus_list_remove_link (DBusList **list,
513 _dbus_list_unlink (list, link);
518 * Frees all links in the list and sets the list head to #NULL. Does
519 * not free the data in each link, for obvious reasons. This is a
520 * linear-time operation.
522 * @param list address of the list head.
525 _dbus_list_clear (DBusList **list)
532 DBusList *next = _dbus_list_get_next_link (list, link);
543 * Gets the first link in the list.
544 * This is a constant-time operation.
546 * @param list address of the list head.
547 * @returns the first link, or #NULL for an empty list.
550 _dbus_list_get_first_link (DBusList **list)
556 * Gets the last link in the list.
557 * This is a constant-time operation.
559 * @param list address of the list head.
560 * @returns the last link, or #NULL for an empty list.
563 _dbus_list_get_last_link (DBusList **list)
568 return (*list)->prev;
572 * Gets the last data in the list.
573 * This is a constant-time operation.
575 * @param list address of the list head.
576 * @returns the last data in the list, or #NULL for an empty list.
579 _dbus_list_get_last (DBusList **list)
584 return (*list)->prev->data;
588 * Gets the first data in the list.
589 * This is a constant-time operation.
591 * @param list address of the list head.
592 * @returns the first data in the list, or #NULL for an empty list.
595 _dbus_list_get_first (DBusList **list)
600 return (*list)->data;
604 * Removes the first link in the list and returns it. This is a
605 * constant-time operation.
607 * @param list address of the list head.
608 * @returns the first link in the list, or #NULL for an empty list.
611 _dbus_list_pop_first_link (DBusList **list)
615 link = _dbus_list_get_first_link (list);
619 _dbus_list_unlink (list, link);
625 * Removes the first value in the list and returns it. This is a
626 * constant-time operation.
628 * @param list address of the list head.
629 * @returns the first data in the list, or #NULL for an empty list.
632 _dbus_list_pop_first (DBusList **list)
637 link = _dbus_list_get_first_link (list);
642 _dbus_list_remove_link (list, link);
648 * Removes the last value in the list and returns it. This is a
649 * constant-time operation.
651 * @param list address of the list head.
652 * @returns the last data in the list, or #NULL for an empty list.
655 _dbus_list_pop_last (DBusList **list)
660 link = _dbus_list_get_last_link (list);
665 _dbus_list_remove_link (list, link);
671 * Removes the last link in the list and returns it. This is a
672 * constant-time operation.
674 * @param list address of the list head.
675 * @returns the last link in the list, or #NULL for an empty list.
678 _dbus_list_pop_last_link (DBusList **list)
682 link = _dbus_list_get_last_link (list);
686 _dbus_list_unlink (list, link);
692 * Copies a list. This is a linear-time operation. If there isn't
693 * enough memory to copy the entire list, the destination list will be
696 * @param list address of the head of the list to copy.
697 * @param dest address where the copied list should be placed.
698 * @returns #TRUE on success, #FALSE if not enough memory.
701 _dbus_list_copy (DBusList **list,
706 _dbus_assert (list != dest);
713 if (!_dbus_list_append (dest, link->data))
715 /* free what we have so far */
716 _dbus_list_clear (dest);
720 link = _dbus_list_get_next_link (list, link);
727 * Gets the length of a list. This is a linear-time
730 * @param list address of the head of the list
731 * @returns number of elements in the list.
734 _dbus_list_get_length (DBusList **list)
746 link = _dbus_list_get_next_link (list, link);
753 * Calls the given function for each element in the list. The
754 * function is passed the list element as its first argument, and the
755 * given data as its second argument.
757 * @param list address of the head of the list.
758 * @param function function to call for each element.
759 * @param data extra data for the function.
763 _dbus_list_foreach (DBusList **list,
764 DBusForeachFunction function,
772 DBusList *next = _dbus_list_get_next_link (list, link);
774 (* function) (link->data, data);
781 * Check whether length is exactly one.
783 * @param list the list
784 * @returns #TRUE if length is exactly one
787 _dbus_list_length_is_one (DBusList **list)
789 return (*list != NULL &&
790 (*list)->next == *list);
795 #ifdef DBUS_BUILD_TESTS
796 #include "dbus-test.h"
800 verify_list (DBusList **list)
810 if (link->next == link)
812 _dbus_assert (link->prev == link);
813 _dbus_assert (*list == link);
821 _dbus_assert (link->prev->next == link);
822 _dbus_assert (link->next->prev == link);
825 while (link != *list);
827 _dbus_assert (length == _dbus_list_get_length (list));
830 _dbus_assert (_dbus_list_length_is_one (list));
832 _dbus_assert (!_dbus_list_length_is_one (list));
836 is_ascending_sequence (DBusList **list)
841 prev = _DBUS_INT_MIN;
843 link = _dbus_list_get_first_link (list);
846 int v = _DBUS_POINTER_TO_INT (link->data);
853 link = _dbus_list_get_next_link (list, link);
860 is_descending_sequence (DBusList **list)
865 prev = _DBUS_INT_MAX;
867 link = _dbus_list_get_first_link (list);
870 int v = _DBUS_POINTER_TO_INT (link->data);
877 link = _dbus_list_get_next_link (list, link);
884 all_even_values (DBusList **list)
888 link = _dbus_list_get_first_link (list);
891 int v = _DBUS_POINTER_TO_INT (link->data);
896 link = _dbus_list_get_next_link (list, link);
903 all_odd_values (DBusList **list)
907 link = _dbus_list_get_first_link (list);
910 int v = _DBUS_POINTER_TO_INT (link->data);
915 link = _dbus_list_get_next_link (list, link);
922 lists_equal (DBusList **list1,
928 link1 = _dbus_list_get_first_link (list1);
929 link2 = _dbus_list_get_first_link (list2);
930 while (link1 && link2)
932 if (link1->data != link2->data)
935 link1 = _dbus_list_get_next_link (list1, link1);
936 link2 = _dbus_list_get_next_link (list2, link2);
946 * @ingroup DBusListInternals
947 * Unit test for DBusList
948 * @returns #TRUE on success.
951 _dbus_list_test (void)
964 /* Test append and prepend */
969 if (!_dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i)))
970 _dbus_assert_not_reached ("could not allocate for append");
972 if (!_dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i)))
973 _dbus_assert_not_reached ("count not allocate for prepend");
976 verify_list (&list1);
977 verify_list (&list2);
979 _dbus_assert (_dbus_list_get_length (&list1) == i);
980 _dbus_assert (_dbus_list_get_length (&list2) == i);
983 _dbus_assert (is_ascending_sequence (&list1));
984 _dbus_assert (is_descending_sequence (&list2));
986 /* Test list clear */
987 _dbus_list_clear (&list1);
988 _dbus_list_clear (&list2);
990 verify_list (&list1);
991 verify_list (&list2);
993 /* Test get_first, get_last, pop_first, pop_last */
998 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
999 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1012 got_data1 = _dbus_list_get_last (&list1);
1013 got_data2 = _dbus_list_get_first (&list2);
1015 data1 = _dbus_list_pop_last (&list1);
1016 data2 = _dbus_list_pop_first (&list2);
1018 _dbus_assert (got_data1 == data1);
1019 _dbus_assert (got_data2 == data2);
1021 _dbus_assert (_DBUS_POINTER_TO_INT (data1) == i);
1022 _dbus_assert (_DBUS_POINTER_TO_INT (data2) == i);
1024 verify_list (&list1);
1025 verify_list (&list2);
1027 _dbus_assert (is_ascending_sequence (&list1));
1028 _dbus_assert (is_descending_sequence (&list2));
1033 _dbus_assert (list1 == NULL);
1034 _dbus_assert (list2 == NULL);
1036 /* Test get_first_link, get_last_link, pop_first_link, pop_last_link */
1041 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1042 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1049 DBusList *got_link1;
1050 DBusList *got_link2;
1058 got_link1 = _dbus_list_get_last_link (&list1);
1059 got_link2 = _dbus_list_get_first_link (&list2);
1061 link1 = _dbus_list_pop_last_link (&list1);
1062 link2 = _dbus_list_pop_first_link (&list2);
1064 _dbus_assert (got_link1 == link1);
1065 _dbus_assert (got_link2 == link2);
1067 data1 = link1->data;
1068 data2 = link2->data;
1070 _dbus_list_free_link (link1);
1071 _dbus_list_free_link (link2);
1073 _dbus_assert (_DBUS_POINTER_TO_INT (data1) == i);
1074 _dbus_assert (_DBUS_POINTER_TO_INT (data2) == i);
1076 verify_list (&list1);
1077 verify_list (&list2);
1079 _dbus_assert (is_ascending_sequence (&list1));
1080 _dbus_assert (is_descending_sequence (&list2));
1085 _dbus_assert (list1 == NULL);
1086 _dbus_assert (list2 == NULL);
1088 /* Test iteration */
1093 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1094 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1097 verify_list (&list1);
1098 verify_list (&list2);
1100 _dbus_assert (_dbus_list_get_length (&list1) == i);
1101 _dbus_assert (_dbus_list_get_length (&list2) == i);
1104 _dbus_assert (is_ascending_sequence (&list1));
1105 _dbus_assert (is_descending_sequence (&list2));
1108 link2 = _dbus_list_get_first_link (&list2);
1109 while (link2 != NULL)
1111 verify_list (&link2); /* pretend this link is the head */
1113 _dbus_assert (_DBUS_POINTER_TO_INT (link2->data) == i);
1115 link2 = _dbus_list_get_next_link (&list2, link2);
1120 link1 = _dbus_list_get_first_link (&list1);
1121 while (link1 != NULL)
1123 verify_list (&link1); /* pretend this link is the head */
1125 _dbus_assert (_DBUS_POINTER_TO_INT (link1->data) == i);
1127 link1 = _dbus_list_get_next_link (&list1, link1);
1132 link1 = _dbus_list_get_last_link (&list1);
1133 while (link1 != NULL)
1135 verify_list (&link1); /* pretend this link is the head */
1137 _dbus_assert (_DBUS_POINTER_TO_INT (link1->data) == i);
1139 link1 = _dbus_list_get_prev_link (&list1, link1);
1143 _dbus_list_clear (&list1);
1144 _dbus_list_clear (&list2);
1151 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1152 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1161 if (!_dbus_list_remove (&list1, _DBUS_INT_TO_POINTER (i)))
1162 _dbus_assert_not_reached ("element should have been in list");
1163 if (!_dbus_list_remove (&list2, _DBUS_INT_TO_POINTER (i)))
1164 _dbus_assert_not_reached ("element should have been in list");
1166 verify_list (&list1);
1167 verify_list (&list2);
1172 _dbus_assert (all_odd_values (&list1));
1173 _dbus_assert (all_odd_values (&list2));
1175 _dbus_list_clear (&list1);
1176 _dbus_list_clear (&list2);
1178 /* test removing the other half of the elements */
1183 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1184 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1193 if (!_dbus_list_remove (&list1, _DBUS_INT_TO_POINTER (i)))
1194 _dbus_assert_not_reached ("element should have been in list");
1195 if (!_dbus_list_remove (&list2, _DBUS_INT_TO_POINTER (i)))
1196 _dbus_assert_not_reached ("element should have been in list");
1198 verify_list (&list1);
1199 verify_list (&list2);
1204 _dbus_assert (all_even_values (&list1));
1205 _dbus_assert (all_even_values (&list2));
1207 /* clear list using remove_link */
1208 while (list1 != NULL)
1210 _dbus_list_remove_link (&list1, list1);
1211 verify_list (&list1);
1213 while (list2 != NULL)
1215 _dbus_list_remove_link (&list2, list2);
1216 verify_list (&list2);
1219 /* Test remove link more generally */
1223 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1224 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1229 link2 = _dbus_list_get_first_link (&list2);
1230 while (link2 != NULL)
1232 DBusList *next = _dbus_list_get_next_link (&list2, link2);
1234 _dbus_assert (_DBUS_POINTER_TO_INT (link2->data) == i);
1237 _dbus_list_remove_link (&list2, link2);
1239 verify_list (&list2);
1245 _dbus_assert (all_odd_values (&list2));
1246 _dbus_list_clear (&list2);
1249 link1 = _dbus_list_get_first_link (&list1);
1250 while (link1 != NULL)
1252 DBusList *next = _dbus_list_get_next_link (&list1, link1);
1254 _dbus_assert (_DBUS_POINTER_TO_INT (link1->data) == i);
1257 _dbus_list_remove_link (&list1, link1);
1259 verify_list (&list1);
1265 _dbus_assert (all_even_values (&list1));
1266 _dbus_list_clear (&list1);
1268 /* Test copying a list */
1272 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1273 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1277 /* bad pointers, because they are allowed in the copy dest */
1278 copy1 = _DBUS_INT_TO_POINTER (0x342234);
1279 copy2 = _DBUS_INT_TO_POINTER (23);
1281 _dbus_list_copy (&list1, ©1);
1282 verify_list (&list1);
1283 verify_list (©1);
1284 _dbus_assert (lists_equal (&list1, ©1));
1286 _dbus_list_copy (&list2, ©2);
1287 verify_list (&list2);
1288 verify_list (©2);
1289 _dbus_assert (lists_equal (&list2, ©2));
1291 /* Now test copying empty lists */
1292 _dbus_list_clear (&list1);
1293 _dbus_list_clear (&list2);
1294 _dbus_list_clear (©1);
1295 _dbus_list_clear (©2);
1297 /* bad pointers, because they are allowed in the copy dest */
1298 copy1 = _DBUS_INT_TO_POINTER (0x342234);
1299 copy2 = _DBUS_INT_TO_POINTER (23);
1301 _dbus_list_copy (&list1, ©1);
1302 verify_list (&list1);
1303 verify_list (©1);
1304 _dbus_assert (lists_equal (&list1, ©1));
1306 _dbus_list_copy (&list2, ©2);
1307 verify_list (&list2);
1308 verify_list (©2);
1309 _dbus_assert (lists_equal (&list2, ©2));
1311 _dbus_list_clear (&list1);
1312 _dbus_list_clear (&list2);
1314 /* insert_before on empty list */
1315 _dbus_list_insert_before (&list1, NULL,
1316 _DBUS_INT_TO_POINTER (0));
1317 verify_list (&list1);
1319 /* inserting before first element */
1320 _dbus_list_insert_before (&list1, list1,
1321 _DBUS_INT_TO_POINTER (2));
1322 verify_list (&list1);
1323 _dbus_assert (is_descending_sequence (&list1));
1325 /* inserting in the middle */
1326 _dbus_list_insert_before (&list1, list1->next,
1327 _DBUS_INT_TO_POINTER (1));
1328 verify_list (&list1);
1329 _dbus_assert (is_descending_sequence (&list1));
1331 /* using insert_before to append */
1332 _dbus_list_insert_before (&list1, NULL,
1333 _DBUS_INT_TO_POINTER (-1));
1334 verify_list (&list1);
1335 _dbus_assert (is_descending_sequence (&list1));
1337 _dbus_list_clear (&list1);
1339 /* insert_after on empty list */
1340 _dbus_list_insert_after (&list1, NULL,
1341 _DBUS_INT_TO_POINTER (0));
1342 verify_list (&list1);
1344 /* inserting after first element */
1345 _dbus_list_insert_after (&list1, list1,
1346 _DBUS_INT_TO_POINTER (1));
1347 verify_list (&list1);
1348 _dbus_assert (is_ascending_sequence (&list1));
1350 /* inserting at the end */
1351 _dbus_list_insert_after (&list1, list1->next,
1352 _DBUS_INT_TO_POINTER (2));
1353 verify_list (&list1);
1354 _dbus_assert (is_ascending_sequence (&list1));
1356 /* using insert_after to prepend */
1357 _dbus_list_insert_after (&list1, NULL,
1358 _DBUS_INT_TO_POINTER (-1));
1359 verify_list (&list1);
1360 _dbus_assert (is_ascending_sequence (&list1));
1362 _dbus_list_clear (&list1);
1364 /* using remove_last */
1365 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (2));
1366 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (1));
1367 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (3));
1369 _dbus_list_remove_last (&list1, _DBUS_INT_TO_POINTER (2));
1371 verify_list (&list1);
1372 _dbus_assert (is_ascending_sequence (&list1));
1374 _dbus_list_clear (&list1);