maint: update copyright
[gnulib.git] / tests / test-avltree_list.c
1 /* Test of sequential list data type implementation.
2    Copyright (C) 2006-2014 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2006.
4
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.
9
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.
14
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/>.  */
17
18 #include <config.h>
19
20 #include "gl_avltree_list.h"
21
22 #include <stdlib.h>
23
24 #include "gl_array_list.h"
25 #include "progname.h"
26 #include "macros.h"
27
28 extern void gl_avltree_list_check_invariants (gl_list_t list);
29
30 static const char *objects[15] =
31   {
32     "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"
33   };
34
35 #define RANDOM(n) (rand () % (n))
36 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
37
38 static void
39 check_equals (gl_list_t list1, gl_list_t list2)
40 {
41   size_t n, i;
42
43   n = gl_list_size (list1);
44   ASSERT (n == gl_list_size (list2));
45   for (i = 0; i < n; i++)
46     {
47       ASSERT (gl_list_get_at (list1, i) == gl_list_get_at (list2, i));
48     }
49 }
50
51 static void
52 check_all (gl_list_t list1, gl_list_t list2, gl_list_t list3)
53 {
54   gl_avltree_list_check_invariants (list2);
55   gl_avltree_list_check_invariants (list3);
56   check_equals (list1, list2);
57   check_equals (list1, list3);
58 }
59
60 int
61 main (int argc, char *argv[])
62 {
63   gl_list_t list1, list2, list3;
64
65   set_program_name (argv[0]);
66
67   /* Allow the user to provide a non-default random seed on the command line.  */
68   if (argc > 1)
69     srand (atoi (argv[1]));
70
71   {
72     size_t initial_size = RANDOM (50);
73     const void **contents =
74       (const void **) malloc (initial_size * sizeof (const void *));
75     size_t i;
76     unsigned int repeat;
77
78     for (i = 0; i < initial_size; i++)
79       contents[i] = RANDOM_OBJECT ();
80
81     /* Create list1.  */
82     list1 = gl_list_nx_create (GL_ARRAY_LIST, NULL, NULL, NULL, true,
83                                initial_size, contents);
84     ASSERT (list1 != NULL);
85     /* Create list2.  */
86     list2 = gl_list_nx_create_empty (GL_AVLTREE_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);
90
91     /* Create list3.  */
92     list3 = gl_list_nx_create (GL_AVLTREE_LIST, NULL, NULL, NULL, true,
93                                initial_size, contents);
94     ASSERT (list3 != NULL);
95
96     check_all (list1, list2, list3);
97
98     for (repeat = 0; repeat < 10000; repeat++)
99       {
100         unsigned int operation = RANDOM (16);
101         switch (operation)
102           {
103           case 0:
104             if (gl_list_size (list1) > 0)
105               {
106                 size_t index = RANDOM (gl_list_size (list1));
107                 const char *obj = RANDOM_OBJECT ();
108                 gl_list_node_t node1, node2, node3;
109
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);
114
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);
119
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);
124
125                 if (index > 0)
126                   {
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));
133                   }
134                 if (index + 1 < gl_list_size (list1))
135                   {
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));
142                   }
143               }
144             break;
145           case 1:
146             {
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);
152               if (node1 == NULL)
153                 {
154                   ASSERT (node2 == NULL);
155                   ASSERT (node3 == NULL);
156                 }
157               else
158                 {
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);
164                 }
165             }
166             break;
167           case 2:
168             {
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))
175                 {
176                   ASSERT (index2 == (size_t)(-1));
177                   ASSERT (index3 == (size_t)(-1));
178                 }
179               else
180                 {
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);
188                 }
189             }
190             break;
191           case 3: /* add 1 element */
192             {
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);
207             }
208             break;
209           case 4: /* add 1 element */
210             {
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);
225             }
226             break;
227           case 5: /* add 3 elements */
228             {
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);
263             }
264             break;
265           case 6: /* add 1 element */
266             {
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);
282               if (index > 0)
283                 {
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));
290                 }
291               if (index + 1 < gl_list_size (list1))
292                 {
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));
299                 }
300             }
301             break;
302           case 7: case 8: /* remove 1 element */
303             if (gl_list_size (list1) > 0)
304               {
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);
318               }
319             break;
320           case 9: case 10: /* remove 1 element */
321             if (gl_list_size (list1) > 0)
322               {
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);
329               }
330             break;
331           case 11: case 12: /* remove 1 element */
332             if (gl_list_size (list1) > 0)
333               {
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);
340               }
341             break;
342           case 13:
343             if (gl_list_size (list1) > 0)
344               {
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);
351               }
352             break;
353           case 14:
354             {
355               size_t n = gl_list_size (list1);
356               gl_list_iterator_t iter1, iter2, iter3;
357               const void *elt;
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++)
362                 {
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);
369                 }
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);
376             }
377             break;
378           case 15:
379             {
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;
383               const void *elt;
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++)
388                 {
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);
395                 }
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);
402             }
403             break;
404           }
405         check_all (list1, list2, list3);
406       }
407
408     gl_list_free (list1);
409     gl_list_free (list2);
410     gl_list_free (list3);
411     free (contents);
412   }
413
414   return 0;
415 }