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