1 /* Test of sequential list data type implementation.
2 Copyright (C) 2006-2012 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "gl_rbtree_list.h"
24 #include "gl_array_list.h"
28 extern void gl_rbtree_list_check_invariants (gl_list_t list);
30 static const char *objects[15] =
32 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"
35 #define RANDOM(n) (rand () % (n))
36 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
39 check_equals (gl_list_t list1, gl_list_t list2)
43 n = gl_list_size (list1);
44 ASSERT (n == gl_list_size (list2));
45 for (i = 0; i < n; i++)
47 ASSERT (gl_list_get_at (list1, i) == gl_list_get_at (list2, i));
52 check_all (gl_list_t list1, gl_list_t list2, gl_list_t list3)
54 gl_rbtree_list_check_invariants (list2);
55 gl_rbtree_list_check_invariants (list3);
56 check_equals (list1, list2);
57 check_equals (list1, list3);
61 main (int argc, char *argv[])
63 gl_list_t list1, list2, list3;
65 set_program_name (argv[0]);
67 /* Allow the user to provide a non-default random seed on the command line. */
69 srand (atoi (argv[1]));
72 size_t initial_size = RANDOM (50);
73 const void **contents =
74 (const void **) malloc (initial_size * sizeof (const void *));
78 for (i = 0; i < initial_size; i++)
79 contents[i] = RANDOM_OBJECT ();
82 list1 = gl_list_nx_create (GL_ARRAY_LIST, NULL, NULL, NULL, true,
83 initial_size, contents);
84 ASSERT (list1 != NULL);
86 list2 = gl_list_nx_create_empty (GL_RBTREE_LIST, NULL, NULL, NULL, true);
87 ASSERT (list2 != NULL);
88 for (i = 0; i < initial_size; i++)
89 ASSERT (gl_list_nx_add_last (list2, contents[i]) != NULL);
92 list3 = gl_list_nx_create (GL_RBTREE_LIST, NULL, NULL, NULL, true,
93 initial_size, contents);
94 ASSERT (list3 != NULL);
96 check_all (list1, list2, list3);
98 for (repeat = 0; repeat < 10000; repeat++)
100 unsigned int operation = RANDOM (16);
104 if (gl_list_size (list1) > 0)
106 size_t index = RANDOM (gl_list_size (list1));
107 const char *obj = RANDOM_OBJECT ();
108 gl_list_node_t node1, node2, node3;
110 node1 = gl_list_nx_set_at (list1, index, obj);
111 ASSERT (node1 != NULL);
112 ASSERT (gl_list_get_at (list1, index) == obj);
113 ASSERT (gl_list_node_value (list1, node1) == obj);
115 node2 = gl_list_nx_set_at (list2, index, obj);
116 ASSERT (node2 != NULL);
117 ASSERT (gl_list_get_at (list2, index) == obj);
118 ASSERT (gl_list_node_value (list2, node2) == obj);
120 node3 = gl_list_nx_set_at (list3, index, obj);
121 ASSERT (node3 != NULL);
122 ASSERT (gl_list_get_at (list3, index) == obj);
123 ASSERT (gl_list_node_value (list3, node3) == obj);
127 ASSERT (gl_list_node_value (list1, gl_list_previous_node (list1, node1))
128 == gl_list_get_at (list1, index - 1));
129 ASSERT (gl_list_node_value (list2, gl_list_previous_node (list3, node3))
130 == gl_list_get_at (list2, index - 1));
131 ASSERT (gl_list_node_value (list3, gl_list_previous_node (list3, node3))
132 == gl_list_get_at (list2, index - 1));
134 if (index + 1 < gl_list_size (list1))
136 ASSERT (gl_list_node_value (list1, gl_list_next_node (list1, node1))
137 == gl_list_get_at (list1, index + 1));
138 ASSERT (gl_list_node_value (list2, gl_list_next_node (list3, node3))
139 == gl_list_get_at (list2, index + 1));
140 ASSERT (gl_list_node_value (list3, gl_list_next_node (list3, node3))
141 == gl_list_get_at (list2, index + 1));
147 const char *obj = RANDOM_OBJECT ();
148 gl_list_node_t node1, node2, node3;
149 node1 = gl_list_search (list1, obj);
150 node2 = gl_list_search (list2, obj);
151 node3 = gl_list_search (list3, obj);
154 ASSERT (node2 == NULL);
155 ASSERT (node3 == NULL);
159 ASSERT (node2 != NULL);
160 ASSERT (node3 != NULL);
161 ASSERT (gl_list_node_value (list1, node1) == obj);
162 ASSERT (gl_list_node_value (list2, node2) == obj);
163 ASSERT (gl_list_node_value (list3, node3) == obj);
169 const char *obj = RANDOM_OBJECT ();
170 size_t index1, index2, index3;
171 index1 = gl_list_indexof (list1, obj);
172 index2 = gl_list_indexof (list2, obj);
173 index3 = gl_list_indexof (list3, obj);
174 if (index1 == (size_t)(-1))
176 ASSERT (index2 == (size_t)(-1));
177 ASSERT (index3 == (size_t)(-1));
181 ASSERT (index2 != (size_t)(-1));
182 ASSERT (index3 != (size_t)(-1));
183 ASSERT (gl_list_get_at (list1, index1) == obj);
184 ASSERT (gl_list_get_at (list2, index2) == obj);
185 ASSERT (gl_list_get_at (list3, index3) == obj);
186 ASSERT (index2 == index1);
187 ASSERT (index3 == index1);
191 case 3: /* add 1 element */
193 const char *obj = RANDOM_OBJECT ();
194 gl_list_node_t node1, node2, node3;
195 node1 = gl_list_nx_add_first (list1, obj);
196 ASSERT (node1 != NULL);
197 node2 = gl_list_nx_add_first (list2, obj);
198 ASSERT (node2 != NULL);
199 node3 = gl_list_nx_add_first (list3, obj);
200 ASSERT (node3 != NULL);
201 ASSERT (gl_list_node_value (list1, node1) == obj);
202 ASSERT (gl_list_node_value (list2, node2) == obj);
203 ASSERT (gl_list_node_value (list3, node3) == obj);
204 ASSERT (gl_list_get_at (list1, 0) == obj);
205 ASSERT (gl_list_get_at (list2, 0) == obj);
206 ASSERT (gl_list_get_at (list3, 0) == obj);
209 case 4: /* add 1 element */
211 const char *obj = RANDOM_OBJECT ();
212 gl_list_node_t node1, node2, node3;
213 node1 = gl_list_nx_add_last (list1, obj);
214 ASSERT (node1 != NULL);
215 node2 = gl_list_nx_add_last (list2, obj);
216 ASSERT (node2 != NULL);
217 node3 = gl_list_nx_add_last (list3, obj);
218 ASSERT (node3 != NULL);
219 ASSERT (gl_list_node_value (list1, node1) == obj);
220 ASSERT (gl_list_node_value (list2, node2) == obj);
221 ASSERT (gl_list_node_value (list3, node3) == obj);
222 ASSERT (gl_list_get_at (list1, gl_list_size (list1) - 1) == obj);
223 ASSERT (gl_list_get_at (list2, gl_list_size (list2) - 1) == obj);
224 ASSERT (gl_list_get_at (list3, gl_list_size (list3) - 1) == obj);
227 case 5: /* add 3 elements */
229 const char *obj0 = RANDOM_OBJECT ();
230 const char *obj1 = RANDOM_OBJECT ();
231 const char *obj2 = RANDOM_OBJECT ();
232 gl_list_node_t node1, node2, node3;
233 node1 = gl_list_nx_add_first (list1, obj2);
234 ASSERT (node1 != NULL);
235 node1 = gl_list_nx_add_before (list1, node1, obj0);
236 ASSERT (node1 != NULL);
237 node1 = gl_list_nx_add_after (list1, node1, obj1);
238 ASSERT (node1 != NULL);
239 node2 = gl_list_nx_add_first (list2, obj2);
240 ASSERT (node2 != NULL);
241 node2 = gl_list_nx_add_before (list2, node2, obj0);
242 ASSERT (node2 != NULL);
243 node2 = gl_list_nx_add_after (list2, node2, obj1);
244 ASSERT (node2 != NULL);
245 node3 = gl_list_nx_add_first (list3, obj2);
246 ASSERT (node3 != NULL);
247 node3 = gl_list_nx_add_before (list3, node3, obj0);
248 ASSERT (node3 != NULL);
249 node3 = gl_list_nx_add_after (list3, node3, obj1);
250 ASSERT (node3 != NULL);
251 ASSERT (gl_list_node_value (list1, node1) == obj1);
252 ASSERT (gl_list_node_value (list2, node2) == obj1);
253 ASSERT (gl_list_node_value (list3, node3) == obj1);
254 ASSERT (gl_list_get_at (list1, 0) == obj0);
255 ASSERT (gl_list_get_at (list1, 1) == obj1);
256 ASSERT (gl_list_get_at (list1, 2) == obj2);
257 ASSERT (gl_list_get_at (list2, 0) == obj0);
258 ASSERT (gl_list_get_at (list2, 1) == obj1);
259 ASSERT (gl_list_get_at (list2, 2) == obj2);
260 ASSERT (gl_list_get_at (list3, 0) == obj0);
261 ASSERT (gl_list_get_at (list3, 1) == obj1);
262 ASSERT (gl_list_get_at (list3, 2) == obj2);
265 case 6: /* add 1 element */
267 size_t index = RANDOM (gl_list_size (list1) + 1);
268 const char *obj = RANDOM_OBJECT ();
269 gl_list_node_t node1, node2, node3;
270 node1 = gl_list_nx_add_at (list1, index, obj);
271 ASSERT (node1 != NULL);
272 node2 = gl_list_nx_add_at (list2, index, obj);
273 ASSERT (node2 != NULL);
274 node3 = gl_list_nx_add_at (list3, index, obj);
275 ASSERT (node3 != NULL);
276 ASSERT (gl_list_get_at (list1, index) == obj);
277 ASSERT (gl_list_node_value (list1, node1) == obj);
278 ASSERT (gl_list_get_at (list2, index) == obj);
279 ASSERT (gl_list_node_value (list2, node2) == obj);
280 ASSERT (gl_list_get_at (list3, index) == obj);
281 ASSERT (gl_list_node_value (list3, node3) == obj);
284 ASSERT (gl_list_node_value (list1, gl_list_previous_node (list1, node1))
285 == gl_list_get_at (list1, index - 1));
286 ASSERT (gl_list_node_value (list2, gl_list_previous_node (list3, node3))
287 == gl_list_get_at (list2, index - 1));
288 ASSERT (gl_list_node_value (list3, gl_list_previous_node (list3, node3))
289 == gl_list_get_at (list2, index - 1));
291 if (index + 1 < gl_list_size (list1))
293 ASSERT (gl_list_node_value (list1, gl_list_next_node (list1, node1))
294 == gl_list_get_at (list1, index + 1));
295 ASSERT (gl_list_node_value (list2, gl_list_next_node (list3, node3))
296 == gl_list_get_at (list2, index + 1));
297 ASSERT (gl_list_node_value (list3, gl_list_next_node (list3, node3))
298 == gl_list_get_at (list2, index + 1));
302 case 7: case 8: /* remove 1 element */
303 if (gl_list_size (list1) > 0)
305 size_t n = gl_list_size (list1);
306 const char *obj = gl_list_get_at (list1, RANDOM (n));
307 gl_list_node_t node1, node2, node3;
308 node1 = gl_list_search (list1, obj);
309 node2 = gl_list_search (list2, obj);
310 node3 = gl_list_search (list3, obj);
311 ASSERT (node1 != NULL);
312 ASSERT (node2 != NULL);
313 ASSERT (node3 != NULL);
314 ASSERT (gl_list_remove_node (list1, node1));
315 ASSERT (gl_list_remove_node (list2, node2));
316 ASSERT (gl_list_remove_node (list3, node3));
317 ASSERT (gl_list_size (list1) == n - 1);
320 case 9: case 10: /* remove 1 element */
321 if (gl_list_size (list1) > 0)
323 size_t n = gl_list_size (list1);
324 size_t index = RANDOM (n);
325 ASSERT (gl_list_remove_at (list1, index));
326 ASSERT (gl_list_remove_at (list2, index));
327 ASSERT (gl_list_remove_at (list3, index));
328 ASSERT (gl_list_size (list1) == n - 1);
331 case 11: case 12: /* remove 1 element */
332 if (gl_list_size (list1) > 0)
334 size_t n = gl_list_size (list1);
335 const char *obj = gl_list_get_at (list1, RANDOM (n));
336 ASSERT (gl_list_remove (list1, obj));
337 ASSERT (gl_list_remove (list2, obj));
338 ASSERT (gl_list_remove (list3, obj));
339 ASSERT (gl_list_size (list1) == n - 1);
343 if (gl_list_size (list1) > 0)
345 size_t n = gl_list_size (list1);
346 const char *obj = "xyzzy";
347 ASSERT (!gl_list_remove (list1, obj));
348 ASSERT (!gl_list_remove (list2, obj));
349 ASSERT (!gl_list_remove (list3, obj));
350 ASSERT (gl_list_size (list1) == n);
355 size_t n = gl_list_size (list1);
356 gl_list_iterator_t iter1, iter2, iter3;
358 iter1 = gl_list_iterator (list1);
359 iter2 = gl_list_iterator (list2);
360 iter3 = gl_list_iterator (list3);
361 for (i = 0; i < n; i++)
363 ASSERT (gl_list_iterator_next (&iter1, &elt, NULL));
364 ASSERT (gl_list_get_at (list1, i) == elt);
365 ASSERT (gl_list_iterator_next (&iter2, &elt, NULL));
366 ASSERT (gl_list_get_at (list2, i) == elt);
367 ASSERT (gl_list_iterator_next (&iter3, &elt, NULL));
368 ASSERT (gl_list_get_at (list3, i) == elt);
370 ASSERT (!gl_list_iterator_next (&iter1, &elt, NULL));
371 ASSERT (!gl_list_iterator_next (&iter2, &elt, NULL));
372 ASSERT (!gl_list_iterator_next (&iter3, &elt, NULL));
373 gl_list_iterator_free (&iter1);
374 gl_list_iterator_free (&iter2);
375 gl_list_iterator_free (&iter3);
380 size_t end = RANDOM (gl_list_size (list1) + 1);
381 size_t start = RANDOM (end + 1);
382 gl_list_iterator_t iter1, iter2, iter3;
384 iter1 = gl_list_iterator_from_to (list1, start, end);
385 iter2 = gl_list_iterator_from_to (list2, start, end);
386 iter3 = gl_list_iterator_from_to (list3, start, end);
387 for (i = start; i < end; i++)
389 ASSERT (gl_list_iterator_next (&iter1, &elt, NULL));
390 ASSERT (gl_list_get_at (list1, i) == elt);
391 ASSERT (gl_list_iterator_next (&iter2, &elt, NULL));
392 ASSERT (gl_list_get_at (list2, i) == elt);
393 ASSERT (gl_list_iterator_next (&iter3, &elt, NULL));
394 ASSERT (gl_list_get_at (list3, i) == elt);
396 ASSERT (!gl_list_iterator_next (&iter1, &elt, NULL));
397 ASSERT (!gl_list_iterator_next (&iter2, &elt, NULL));
398 ASSERT (!gl_list_iterator_next (&iter3, &elt, NULL));
399 gl_list_iterator_free (&iter1);
400 gl_list_iterator_free (&iter2);
401 gl_list_iterator_free (&iter3);
405 check_all (list1, list2, list3);
408 gl_list_free (list1);
409 gl_list_free (list2);
410 gl_list_free (list3);