list.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Copyright (c) 2013 Nir Soffer <nirsof@gmail.com>
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"),
  5. to deal in the Software without restriction, including without limitation
  6. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. and/or sell copies of the Software, and to permit persons to whom
  8. the Software is furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included
  10. in all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  14. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  16. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  17. IN THE SOFTWARE.
  18. */
  19. #include "../src/utils/cont.h"
  20. #include "../src/utils/err.c"
  21. #include "../src/utils/list.c"
  22. static struct nn_list_item sentinel;
  23. /* Typical object that can be added to a list. */
  24. struct item {
  25. int value;
  26. struct nn_list_item item;
  27. };
  28. /* Initializing list items statically so they can be inserted into a list. */
  29. static struct item that = {1, NN_LIST_ITEM_INITIALIZER};
  30. static struct item other = {2, NN_LIST_ITEM_INITIALIZER};
  31. int main ()
  32. {
  33. int rc;
  34. struct nn_list list;
  35. struct nn_list_item *list_item;
  36. struct item *item;
  37. /* List item life cycle. */
  38. /* Initialize the item. Make sure it's not part of any list. */
  39. nn_list_item_init (&that.item);
  40. nn_assert (!nn_list_item_isinlist (&that.item));
  41. /* That may be part of some list, or uninitialized memory. */
  42. that.item.prev = &sentinel;
  43. that.item.next = &sentinel;
  44. nn_assert (nn_list_item_isinlist (&that.item));
  45. that.item.prev = NULL;
  46. that.item.next = NULL;
  47. nn_assert (nn_list_item_isinlist (&that.item));
  48. /* Before termination, item must be removed from the list. */
  49. nn_list_item_init (&that.item);
  50. nn_list_item_term (&that.item);
  51. /* Initializing a list. */
  52. /* Uninitialized list has random content. */
  53. list.first = &sentinel;
  54. list.last = &sentinel;
  55. nn_list_init (&list);
  56. nn_assert (list.first == NULL);
  57. nn_assert (list.last == NULL);
  58. nn_list_term (&list);
  59. /* Empty list. */
  60. nn_list_init (&list);
  61. rc = nn_list_empty (&list);
  62. nn_assert (rc == 1);
  63. list_item = nn_list_begin (&list);
  64. nn_assert (list_item == NULL);
  65. list_item = nn_list_end (&list);
  66. nn_assert (list_item == NULL);
  67. nn_list_term (&list);
  68. /* Inserting and erasing items. */
  69. nn_list_init (&list);
  70. nn_list_item_init (&that.item);
  71. /* Item doesn'tt belong to list yet. */
  72. nn_assert (!nn_list_item_isinlist (&that.item));
  73. nn_list_insert (&list, &that.item, nn_list_end (&list));
  74. /* Item is now part of a list. */
  75. nn_assert (nn_list_item_isinlist (&that.item));
  76. /* Single item does not have prev or next item. */
  77. nn_assert (that.item.prev == NULL);
  78. nn_assert (that.item.next == NULL);
  79. /* Item is both first and list item. */
  80. nn_assert (list.first == &that.item);
  81. nn_assert (list.last == &that.item);
  82. /* Removing an item. */
  83. nn_list_erase (&list, &that.item);
  84. nn_assert (!nn_list_item_isinlist (&that.item));
  85. nn_assert (list.first == NULL);
  86. nn_assert (list.last == NULL);
  87. nn_list_item_term (&that.item);
  88. nn_list_term (&list);
  89. /* Iterating items. */
  90. nn_list_init (&list);
  91. nn_list_item_init (&that.item);
  92. nn_list_insert (&list, &that.item, nn_list_end (&list));
  93. list_item = nn_list_begin (&list);
  94. nn_assert (list_item == &that.item);
  95. item = nn_cont (list_item, struct item, item);
  96. nn_assert (item == &that);
  97. list_item = nn_list_end (&list);
  98. nn_assert (list_item == NULL);
  99. list_item = nn_list_prev (&list, &that.item);
  100. nn_assert (list_item == NULL);
  101. list_item = nn_list_next (&list, &that.item);
  102. nn_assert (list_item == NULL);
  103. rc = nn_list_empty (&list);
  104. nn_assert (rc == 0);
  105. nn_list_erase (&list, &that.item);
  106. nn_list_item_term (&that.item);
  107. nn_list_term (&list);
  108. /* Appending items. */
  109. nn_list_init (&list);
  110. nn_list_item_init (&that.item);
  111. nn_list_item_init (&other.item);
  112. nn_list_insert (&list, &that.item, nn_list_end (&list));
  113. nn_list_insert (&list, &other.item, nn_list_end (&list));
  114. list_item = nn_list_begin (&list);
  115. nn_assert (list_item == &that.item);
  116. list_item = nn_list_next (&list, list_item);
  117. nn_assert (list_item == &other.item);
  118. nn_list_erase (&list, &that.item);
  119. nn_list_erase (&list, &other.item);
  120. nn_list_item_term (&that.item);
  121. nn_list_item_term (&other.item);
  122. nn_list_term (&list);
  123. /* Prepending items. */
  124. nn_list_init (&list);
  125. nn_list_item_init (&that.item);
  126. nn_list_item_init (&other.item);
  127. nn_list_insert (&list, &that.item, nn_list_begin (&list));
  128. nn_list_insert (&list, &other.item, nn_list_begin (&list));
  129. list_item = nn_list_begin (&list);
  130. nn_assert (list_item == &other.item);
  131. list_item = nn_list_next (&list, list_item);
  132. nn_assert (list_item == &that.item);
  133. nn_list_erase (&list, &that.item);
  134. nn_list_erase (&list, &other.item);
  135. nn_list_item_term (&that.item);
  136. nn_list_item_term (&other.item);
  137. nn_list_term (&list);
  138. return 0;
  139. }