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 2.1
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-internal.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)
129 #ifdef DBUS_BUILD_TESTS
131 link_after (DBusList **list,
132 DBusList *after_this_link,
143 link->prev = after_this_link;
144 link->next = after_this_link->next;
145 after_this_link->next = link;
146 link->next->prev = link;
149 #endif /* DBUS_BUILD_TESTS */
154 * @addtogroup DBusList
161 * A node in a linked list.
163 * DBusList is a circular list; that is, the tail of the list
164 * points back to the head of the list. The empty list is
165 * represented by a #NULL pointer.
169 * @def _dbus_list_get_next_link
171 * Gets the next link in the list, or #NULL if
172 * there are no more links. Used for iteration.
176 * link = _dbus_list_get_first_link (&list);
177 * while (link != NULL)
179 * printf ("value is %p\n", link->data);
180 * link = _dbus_list_get_next_link (&link);
184 * @param list address of the list head.
185 * @param link current link.
186 * @returns the next link, or %NULL if none.
191 * @def _dbus_list_get_prev_link
193 * Gets the previous link in the list, or #NULL if
194 * there are no more links. Used for iteration.
198 * link = _dbus_list_get_last_link (&list);
199 * while (link != NULL)
201 * printf ("value is %p\n", link->data);
202 * link = _dbus_list_get_prev_link (&link);
206 * @param list address of the list head.
207 * @param link current link.
208 * @returns the previous link, or %NULL if none.
213 * Allocates a linked list node. Useful for preallocating
214 * nodes and using _dbus_list_append_link() to avoid
217 * @param data the value to store in the link.
218 * @returns a newly allocated link.
221 _dbus_list_alloc_link (void *data)
223 return alloc_link (data);
227 * Frees a linked list node allocated with _dbus_list_alloc_link.
228 * Does not free the data in the node.
230 * @param link the list node
233 _dbus_list_free_link (DBusList *link)
240 * Appends a value to the list. May return #FALSE
241 * if insufficient memory exists to add a list link.
242 * This is a constant-time operation.
244 * @param list address of the list head.
245 * @param data the value to append.
246 * @returns #TRUE on success.
249 _dbus_list_append (DBusList **list,
252 if (!_dbus_list_prepend (list, data))
255 /* Now cycle the list forward one so the prepended node is the tail */
256 *list = (*list)->next;
262 * Prepends a value to the list. May return #FALSE
263 * if insufficient memory exists to add a list link.
264 * This is a constant-time operation.
266 * @param list address of the list head.
267 * @param data the value to prepend.
268 * @returns #TRUE on success.
271 _dbus_list_prepend (DBusList **list,
276 link = alloc_link (data);
280 link_before (list, *list, link);
286 * Appends a link to the list.
287 * Cannot fail due to out of memory.
288 * This is a constant-time operation.
290 * @param list address of the list head.
291 * @param link the link to append.
294 _dbus_list_append_link (DBusList **list,
297 _dbus_list_prepend_link (list, link);
299 /* Now cycle the list forward one so the prepended node is the tail */
300 *list = (*list)->next;
304 * Prepends a link to the list.
305 * Cannot fail due to out of memory.
306 * This is a constant-time operation.
308 * @param list address of the list head.
309 * @param link the link to prepend.
312 _dbus_list_prepend_link (DBusList **list,
315 link_before (list, *list, link);
318 #ifdef DBUS_BUILD_TESTS
320 * Inserts data into the list before the given existing link.
322 * @param list the list to modify
323 * @param before_this_link existing link to insert before, or #NULL to append
324 * @param data the value to insert
325 * @returns #TRUE on success, #FALSE if memory allocation fails
328 _dbus_list_insert_before (DBusList **list,
329 DBusList *before_this_link,
334 if (before_this_link == NULL)
335 return _dbus_list_append (list, data);
338 link = alloc_link (data);
342 link_before (list, before_this_link, link);
347 #endif /* DBUS_BUILD_TESTS */
350 * Inserts data into the list after the given existing link.
352 * @param list the list to modify
353 * @param after_this_link existing link to insert after, or #NULL to prepend
354 * @param data the value to insert
355 * @returns #TRUE on success, #FALSE if memory allocation fails
358 _dbus_list_insert_after (DBusList **list,
359 DBusList *after_this_link,
364 if (after_this_link == NULL)
365 return _dbus_list_prepend (list, data);
368 link = alloc_link (data);
372 link_after (list, after_this_link, link);
379 * Inserts a link into the list before the given existing link.
381 * @param list the list to modify
382 * @param before_this_link existing link to insert before, or #NULL to append
383 * @param link the link to insert
386 _dbus_list_insert_before_link (DBusList **list,
387 DBusList *before_this_link,
390 if (before_this_link == NULL)
391 _dbus_list_append_link (list, link);
393 link_before (list, before_this_link, link);
397 * Inserts a link into the list after the given existing link.
399 * @param list the list to modify
400 * @param after_this_link existing link to insert after, or #NULL to prepend
401 * @param link the link to insert
404 _dbus_list_insert_after_link (DBusList **list,
405 DBusList *after_this_link,
408 if (after_this_link == NULL)
409 _dbus_list_prepend_link (list, link);
411 link_after (list, after_this_link, link);
415 * Removes a value from the list. Only removes the
416 * first value equal to the given data pointer,
417 * even if multiple values exist which match.
418 * This is a linear-time operation.
420 * @param list address of the list head.
421 * @param data the value to remove.
422 * @returns #TRUE if a value was found to remove.
425 _dbus_list_remove (DBusList **list,
433 if (link->data == data)
435 _dbus_list_remove_link (list, link);
439 link = _dbus_list_get_next_link (list, link);
446 * Removes a value from the list. Only removes the
447 * last value equal to the given data pointer,
448 * even if multiple values exist which match.
449 * This is a linear-time operation.
451 * @param list address of the list head.
452 * @param data the value to remove.
453 * @returns #TRUE if a value was found to remove.
456 _dbus_list_remove_last (DBusList **list,
461 link = _dbus_list_find_last (list, data);
464 _dbus_list_remove_link (list, link);
472 * Finds a value in the list. Returns the last link
473 * with value equal to the given data pointer.
474 * This is a linear-time operation.
475 * Returns #NULL if no value found that matches.
477 * @param list address of the list head.
478 * @param data the value to find.
479 * @returns the link if found
482 _dbus_list_find_last (DBusList **list,
487 link = _dbus_list_get_last_link (list);
491 if (link->data == data)
494 link = _dbus_list_get_prev_link (list, link);
501 * Removes the given link from the list, but doesn't
502 * free it. _dbus_list_remove_link() both removes the
503 * link and also frees it.
505 * @param list the list
506 * @param link the link in the list
509 _dbus_list_unlink (DBusList **list,
512 if (link->next == link)
514 /* one-element list */
519 link->prev->next = link->next;
520 link->next->prev = link->prev;
531 * Removes a link from the list. This is a constant-time operation.
533 * @param list address of the list head.
534 * @param link the list link to remove.
537 _dbus_list_remove_link (DBusList **list,
540 _dbus_list_unlink (list, link);
545 * Frees all links in the list and sets the list head to #NULL. Does
546 * not free the data in each link, for obvious reasons. This is a
547 * linear-time operation.
549 * @param list address of the list head.
552 _dbus_list_clear (DBusList **list)
559 DBusList *next = _dbus_list_get_next_link (list, link);
570 * Gets the first link in the list.
571 * This is a constant-time operation.
573 * @param list address of the list head.
574 * @returns the first link, or #NULL for an empty list.
577 _dbus_list_get_first_link (DBusList **list)
583 * Gets the last link in the list.
584 * This is a constant-time operation.
586 * @param list address of the list head.
587 * @returns the last link, or #NULL for an empty list.
590 _dbus_list_get_last_link (DBusList **list)
595 return (*list)->prev;
599 * Gets the last data in the list.
600 * This is a constant-time operation.
602 * @param list address of the list head.
603 * @returns the last data in the list, or #NULL for an empty list.
606 _dbus_list_get_last (DBusList **list)
611 return (*list)->prev->data;
615 * Gets the first data in the list.
616 * This is a constant-time operation.
618 * @param list address of the list head.
619 * @returns the first data in the list, or #NULL for an empty list.
622 _dbus_list_get_first (DBusList **list)
627 return (*list)->data;
631 * Removes the first link in the list and returns it. This is a
632 * constant-time operation.
634 * @param list address of the list head.
635 * @returns the first link in the list, or #NULL for an empty list.
638 _dbus_list_pop_first_link (DBusList **list)
642 link = _dbus_list_get_first_link (list);
646 _dbus_list_unlink (list, link);
652 * Removes the first value in the list and returns it. This is a
653 * constant-time operation.
655 * @param list address of the list head.
656 * @returns the first data in the list, or #NULL for an empty list.
659 _dbus_list_pop_first (DBusList **list)
664 link = _dbus_list_get_first_link (list);
669 _dbus_list_remove_link (list, link);
675 * Removes the last value in the list and returns it. This is a
676 * constant-time operation.
678 * @param list address of the list head.
679 * @returns the last data in the list, or #NULL for an empty list.
682 _dbus_list_pop_last (DBusList **list)
687 link = _dbus_list_get_last_link (list);
692 _dbus_list_remove_link (list, link);
697 #ifdef DBUS_BUILD_TESTS
699 * Removes the last link in the list and returns it. This is a
700 * constant-time operation.
702 * @param list address of the list head.
703 * @returns the last link in the list, or #NULL for an empty list.
706 _dbus_list_pop_last_link (DBusList **list)
710 link = _dbus_list_get_last_link (list);
714 _dbus_list_unlink (list, link);
718 #endif /* DBUS_BUILD_TESTS */
721 * Copies a list. This is a linear-time operation. If there isn't
722 * enough memory to copy the entire list, the destination list will be
725 * @param list address of the head of the list to copy.
726 * @param dest address where the copied list should be placed.
727 * @returns #TRUE on success, #FALSE if not enough memory.
730 _dbus_list_copy (DBusList **list,
735 _dbus_assert (list != dest);
742 if (!_dbus_list_append (dest, link->data))
744 /* free what we have so far */
745 _dbus_list_clear (dest);
749 link = _dbus_list_get_next_link (list, link);
756 * Gets the length of a list. This is a linear-time
759 * @param list address of the head of the list
760 * @returns number of elements in the list.
763 _dbus_list_get_length (DBusList **list)
775 link = _dbus_list_get_next_link (list, link);
782 * Calls the given function for each element in the list. The
783 * function is passed the list element as its first argument, and the
784 * given data as its second argument.
786 * @param list address of the head of the list.
787 * @param function function to call for each element.
788 * @param data extra data for the function.
792 _dbus_list_foreach (DBusList **list,
793 DBusForeachFunction function,
801 DBusList *next = _dbus_list_get_next_link (list, link);
803 (* function) (link->data, data);
810 * Check whether length is exactly one.
812 * @param list the list
813 * @returns #TRUE if length is exactly one
816 _dbus_list_length_is_one (DBusList **list)
818 return (*list != NULL &&
819 (*list)->next == *list);
824 #ifdef DBUS_BUILD_TESTS
825 #include "dbus-test.h"
829 verify_list (DBusList **list)
839 if (link->next == link)
841 _dbus_assert (link->prev == link);
842 _dbus_assert (*list == link);
850 _dbus_assert (link->prev->next == link);
851 _dbus_assert (link->next->prev == link);
854 while (link != *list);
856 _dbus_assert (length == _dbus_list_get_length (list));
859 _dbus_assert (_dbus_list_length_is_one (list));
861 _dbus_assert (!_dbus_list_length_is_one (list));
865 is_ascending_sequence (DBusList **list)
870 prev = _DBUS_INT_MIN;
872 link = _dbus_list_get_first_link (list);
875 int v = _DBUS_POINTER_TO_INT (link->data);
882 link = _dbus_list_get_next_link (list, link);
889 is_descending_sequence (DBusList **list)
894 prev = _DBUS_INT_MAX;
896 link = _dbus_list_get_first_link (list);
899 int v = _DBUS_POINTER_TO_INT (link->data);
906 link = _dbus_list_get_next_link (list, link);
913 all_even_values (DBusList **list)
917 link = _dbus_list_get_first_link (list);
920 int v = _DBUS_POINTER_TO_INT (link->data);
925 link = _dbus_list_get_next_link (list, link);
932 all_odd_values (DBusList **list)
936 link = _dbus_list_get_first_link (list);
939 int v = _DBUS_POINTER_TO_INT (link->data);
944 link = _dbus_list_get_next_link (list, link);
951 lists_equal (DBusList **list1,
957 link1 = _dbus_list_get_first_link (list1);
958 link2 = _dbus_list_get_first_link (list2);
959 while (link1 && link2)
961 if (link1->data != link2->data)
964 link1 = _dbus_list_get_next_link (list1, link1);
965 link2 = _dbus_list_get_next_link (list2, link2);
975 * @ingroup DBusListInternals
976 * Unit test for DBusList
977 * @returns #TRUE on success.
980 _dbus_list_test (void)
993 /* Test append and prepend */
998 if (!_dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i)))
999 _dbus_assert_not_reached ("could not allocate for append");
1001 if (!_dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i)))
1002 _dbus_assert_not_reached ("count not allocate for prepend");
1005 verify_list (&list1);
1006 verify_list (&list2);
1008 _dbus_assert (_dbus_list_get_length (&list1) == i);
1009 _dbus_assert (_dbus_list_get_length (&list2) == i);
1012 _dbus_assert (is_ascending_sequence (&list1));
1013 _dbus_assert (is_descending_sequence (&list2));
1015 /* Test list clear */
1016 _dbus_list_clear (&list1);
1017 _dbus_list_clear (&list2);
1019 verify_list (&list1);
1020 verify_list (&list2);
1022 /* Test get_first, get_last, pop_first, pop_last */
1027 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1028 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1041 got_data1 = _dbus_list_get_last (&list1);
1042 got_data2 = _dbus_list_get_first (&list2);
1044 data1 = _dbus_list_pop_last (&list1);
1045 data2 = _dbus_list_pop_first (&list2);
1047 _dbus_assert (got_data1 == data1);
1048 _dbus_assert (got_data2 == data2);
1050 _dbus_assert (_DBUS_POINTER_TO_INT (data1) == i);
1051 _dbus_assert (_DBUS_POINTER_TO_INT (data2) == i);
1053 verify_list (&list1);
1054 verify_list (&list2);
1056 _dbus_assert (is_ascending_sequence (&list1));
1057 _dbus_assert (is_descending_sequence (&list2));
1062 _dbus_assert (list1 == NULL);
1063 _dbus_assert (list2 == NULL);
1065 /* Test get_first_link, get_last_link, pop_first_link, pop_last_link */
1070 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1071 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1078 DBusList *got_link1;
1079 DBusList *got_link2;
1087 got_link1 = _dbus_list_get_last_link (&list1);
1088 got_link2 = _dbus_list_get_first_link (&list2);
1090 link1 = _dbus_list_pop_last_link (&list1);
1091 link2 = _dbus_list_pop_first_link (&list2);
1093 _dbus_assert (got_link1 == link1);
1094 _dbus_assert (got_link2 == link2);
1096 data1 = link1->data;
1097 data2 = link2->data;
1099 _dbus_list_free_link (link1);
1100 _dbus_list_free_link (link2);
1102 _dbus_assert (_DBUS_POINTER_TO_INT (data1) == i);
1103 _dbus_assert (_DBUS_POINTER_TO_INT (data2) == i);
1105 verify_list (&list1);
1106 verify_list (&list2);
1108 _dbus_assert (is_ascending_sequence (&list1));
1109 _dbus_assert (is_descending_sequence (&list2));
1114 _dbus_assert (list1 == NULL);
1115 _dbus_assert (list2 == NULL);
1117 /* Test iteration */
1122 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1123 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1126 verify_list (&list1);
1127 verify_list (&list2);
1129 _dbus_assert (_dbus_list_get_length (&list1) == i);
1130 _dbus_assert (_dbus_list_get_length (&list2) == i);
1133 _dbus_assert (is_ascending_sequence (&list1));
1134 _dbus_assert (is_descending_sequence (&list2));
1137 link2 = _dbus_list_get_first_link (&list2);
1138 while (link2 != NULL)
1140 verify_list (&link2); /* pretend this link is the head */
1142 _dbus_assert (_DBUS_POINTER_TO_INT (link2->data) == i);
1144 link2 = _dbus_list_get_next_link (&list2, link2);
1149 link1 = _dbus_list_get_first_link (&list1);
1150 while (link1 != NULL)
1152 verify_list (&link1); /* pretend this link is the head */
1154 _dbus_assert (_DBUS_POINTER_TO_INT (link1->data) == i);
1156 link1 = _dbus_list_get_next_link (&list1, link1);
1161 link1 = _dbus_list_get_last_link (&list1);
1162 while (link1 != NULL)
1164 verify_list (&link1); /* pretend this link is the head */
1166 _dbus_assert (_DBUS_POINTER_TO_INT (link1->data) == i);
1168 link1 = _dbus_list_get_prev_link (&list1, link1);
1172 _dbus_list_clear (&list1);
1173 _dbus_list_clear (&list2);
1180 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1181 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1190 if (!_dbus_list_remove (&list1, _DBUS_INT_TO_POINTER (i)))
1191 _dbus_assert_not_reached ("element should have been in list");
1192 if (!_dbus_list_remove (&list2, _DBUS_INT_TO_POINTER (i)))
1193 _dbus_assert_not_reached ("element should have been in list");
1195 verify_list (&list1);
1196 verify_list (&list2);
1201 _dbus_assert (all_odd_values (&list1));
1202 _dbus_assert (all_odd_values (&list2));
1204 _dbus_list_clear (&list1);
1205 _dbus_list_clear (&list2);
1207 /* test removing the other half of the elements */
1212 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1213 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1222 if (!_dbus_list_remove (&list1, _DBUS_INT_TO_POINTER (i)))
1223 _dbus_assert_not_reached ("element should have been in list");
1224 if (!_dbus_list_remove (&list2, _DBUS_INT_TO_POINTER (i)))
1225 _dbus_assert_not_reached ("element should have been in list");
1227 verify_list (&list1);
1228 verify_list (&list2);
1233 _dbus_assert (all_even_values (&list1));
1234 _dbus_assert (all_even_values (&list2));
1236 /* clear list using remove_link */
1237 while (list1 != NULL)
1239 _dbus_list_remove_link (&list1, list1);
1240 verify_list (&list1);
1242 while (list2 != NULL)
1244 _dbus_list_remove_link (&list2, list2);
1245 verify_list (&list2);
1248 /* Test remove link more generally */
1252 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1253 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1258 link2 = _dbus_list_get_first_link (&list2);
1259 while (link2 != NULL)
1261 DBusList *next = _dbus_list_get_next_link (&list2, link2);
1263 _dbus_assert (_DBUS_POINTER_TO_INT (link2->data) == i);
1266 _dbus_list_remove_link (&list2, link2);
1268 verify_list (&list2);
1274 _dbus_assert (all_odd_values (&list2));
1275 _dbus_list_clear (&list2);
1278 link1 = _dbus_list_get_first_link (&list1);
1279 while (link1 != NULL)
1281 DBusList *next = _dbus_list_get_next_link (&list1, link1);
1283 _dbus_assert (_DBUS_POINTER_TO_INT (link1->data) == i);
1286 _dbus_list_remove_link (&list1, link1);
1288 verify_list (&list1);
1294 _dbus_assert (all_even_values (&list1));
1295 _dbus_list_clear (&list1);
1297 /* Test copying a list */
1301 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1302 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1306 /* bad pointers, because they are allowed in the copy dest */
1307 copy1 = _DBUS_INT_TO_POINTER (0x342234);
1308 copy2 = _DBUS_INT_TO_POINTER (23);
1310 _dbus_list_copy (&list1, ©1);
1311 verify_list (&list1);
1312 verify_list (©1);
1313 _dbus_assert (lists_equal (&list1, ©1));
1315 _dbus_list_copy (&list2, ©2);
1316 verify_list (&list2);
1317 verify_list (©2);
1318 _dbus_assert (lists_equal (&list2, ©2));
1320 /* Now test copying empty lists */
1321 _dbus_list_clear (&list1);
1322 _dbus_list_clear (&list2);
1323 _dbus_list_clear (©1);
1324 _dbus_list_clear (©2);
1326 /* bad pointers, because they are allowed in the copy dest */
1327 copy1 = _DBUS_INT_TO_POINTER (0x342234);
1328 copy2 = _DBUS_INT_TO_POINTER (23);
1330 _dbus_list_copy (&list1, ©1);
1331 verify_list (&list1);
1332 verify_list (©1);
1333 _dbus_assert (lists_equal (&list1, ©1));
1335 _dbus_list_copy (&list2, ©2);
1336 verify_list (&list2);
1337 verify_list (©2);
1338 _dbus_assert (lists_equal (&list2, ©2));
1340 _dbus_list_clear (&list1);
1341 _dbus_list_clear (&list2);
1343 /* insert_before on empty list */
1344 _dbus_list_insert_before (&list1, NULL,
1345 _DBUS_INT_TO_POINTER (0));
1346 verify_list (&list1);
1348 /* inserting before first element */
1349 _dbus_list_insert_before (&list1, list1,
1350 _DBUS_INT_TO_POINTER (2));
1351 verify_list (&list1);
1352 _dbus_assert (is_descending_sequence (&list1));
1354 /* inserting in the middle */
1355 _dbus_list_insert_before (&list1, list1->next,
1356 _DBUS_INT_TO_POINTER (1));
1357 verify_list (&list1);
1358 _dbus_assert (is_descending_sequence (&list1));
1360 /* using insert_before to append */
1361 _dbus_list_insert_before (&list1, NULL,
1362 _DBUS_INT_TO_POINTER (-1));
1363 verify_list (&list1);
1364 _dbus_assert (is_descending_sequence (&list1));
1366 _dbus_list_clear (&list1);
1368 /* insert_after on empty list */
1369 _dbus_list_insert_after (&list1, NULL,
1370 _DBUS_INT_TO_POINTER (0));
1371 verify_list (&list1);
1373 /* inserting after first element */
1374 _dbus_list_insert_after (&list1, list1,
1375 _DBUS_INT_TO_POINTER (1));
1376 verify_list (&list1);
1377 _dbus_assert (is_ascending_sequence (&list1));
1379 /* inserting at the end */
1380 _dbus_list_insert_after (&list1, list1->next,
1381 _DBUS_INT_TO_POINTER (2));
1382 verify_list (&list1);
1383 _dbus_assert (is_ascending_sequence (&list1));
1385 /* using insert_after to prepend */
1386 _dbus_list_insert_after (&list1, NULL,
1387 _DBUS_INT_TO_POINTER (-1));
1388 verify_list (&list1);
1389 _dbus_assert (is_ascending_sequence (&list1));
1391 _dbus_list_clear (&list1);
1393 /* using remove_last */
1394 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (2));
1395 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (1));
1396 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (3));
1398 _dbus_list_remove_last (&list1, _DBUS_INT_TO_POINTER (2));
1400 verify_list (&list1);
1401 _dbus_assert (is_ascending_sequence (&list1));
1403 _dbus_list_clear (&list1);