Bump to 1.14.1
[platform/upstream/augeas.git] / tests / test-rbtree_list.c
1 /* Test of sequential list data type implementation.
2    Copyright (C) 2006-2016 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_rbtree_list.h"
21
22 #include <stdlib.h>
23
24 #include "gl_array_list.h"
25 #include "macros.h"
26
27 extern void gl_rbtree_list_check_invariants (gl_list_t list);
28
29 static const char *objects[15] =
30   {
31     "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"
32   };
33
34 #define RANDOM(n) (rand () % (n))
35 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
36
37 static void
38 check_equals (gl_list_t list1, gl_list_t list2)
39 {
40   size_t n, i;
41
42   n = gl_list_size (list1);
43   ASSERT (n == gl_list_size (list2));
44   for (i = 0; i < n; i++)
45     {
46       ASSERT (gl_list_get_at (list1, i) == gl_list_get_at (list2, i));
47     }
48 }
49
50 static void
51 check_all (gl_list_t list1, gl_list_t list2, gl_list_t list3)
52 {
53   gl_rbtree_list_check_invariants (list2);
54   gl_rbtree_list_check_invariants (list3);
55   check_equals (list1, list2);
56   check_equals (list1, list3);
57 }
58
59 int
60 main (int argc, char *argv[])
61 {
62   gl_list_t list1, list2, list3;
63
64   /* Allow the user to provide a non-default random seed on the command line.  */
65   if (argc > 1)
66     srand (atoi (argv[1]));
67
68   {
69     size_t initial_size = RANDOM (50);
70     const void **contents =
71       (const void **) malloc (initial_size * sizeof (const void *));
72     size_t i;
73     unsigned int repeat;
74
75     for (i = 0; i < initial_size; i++)
76       contents[i] = RANDOM_OBJECT ();
77
78     /* Create list1.  */
79     list1 = gl_list_nx_create (GL_ARRAY_LIST, NULL, NULL, NULL, true,
80                                initial_size, contents);
81     ASSERT (list1 != NULL);
82     /* Create list2.  */
83     list2 = gl_list_nx_create_empty (GL_RBTREE_LIST, NULL, NULL, NULL, true);
84     ASSERT (list2 != NULL);
85     for (i = 0; i < initial_size; i++)
86       ASSERT (gl_list_nx_add_last (list2, contents[i]) != NULL);
87
88     /* Create list3.  */
89     list3 = gl_list_nx_create (GL_RBTREE_LIST, NULL, NULL, NULL, true,
90                                initial_size, contents);
91     ASSERT (list3 != NULL);
92
93     check_all (list1, list2, list3);
94
95     for (repeat = 0; repeat < 10000; repeat++)
96       {
97         unsigned int operation = RANDOM (16);
98         switch (operation)
99           {
100           case 0:
101             if (gl_list_size (list1) > 0)
102               {
103                 size_t index = RANDOM (gl_list_size (list1));
104                 const char *obj = RANDOM_OBJECT ();
105                 gl_list_node_t node1, node2, node3;
106
107                 node1 = gl_list_nx_set_at (list1, index, obj);
108                 ASSERT (node1 != NULL);
109                 ASSERT (gl_list_get_at (list1, index) == obj);
110                 ASSERT (gl_list_node_value (list1, node1) == obj);
111
112                 node2 = gl_list_nx_set_at (list2, index, obj);
113                 ASSERT (node2 != NULL);
114                 ASSERT (gl_list_get_at (list2, index) == obj);
115                 ASSERT (gl_list_node_value (list2, node2) == obj);
116
117                 node3 = gl_list_nx_set_at (list3, index, obj);
118                 ASSERT (node3 != NULL);
119                 ASSERT (gl_list_get_at (list3, index) == obj);
120                 ASSERT (gl_list_node_value (list3, node3) == obj);
121
122                 if (index > 0)
123                   {
124                     ASSERT (gl_list_node_value (list1, gl_list_previous_node (list1, node1))
125                             == gl_list_get_at (list1, index - 1));
126                     ASSERT (gl_list_node_value (list2, gl_list_previous_node (list3, node3))
127                             == gl_list_get_at (list2, index - 1));
128                     ASSERT (gl_list_node_value (list3, gl_list_previous_node (list3, node3))
129                             == gl_list_get_at (list2, index - 1));
130                   }
131                 if (index + 1 < gl_list_size (list1))
132                   {
133                     ASSERT (gl_list_node_value (list1, gl_list_next_node (list1, node1))
134                             == gl_list_get_at (list1, index + 1));
135                     ASSERT (gl_list_node_value (list2, gl_list_next_node (list3, node3))
136                             == gl_list_get_at (list2, index + 1));
137                     ASSERT (gl_list_node_value (list3, gl_list_next_node (list3, node3))
138                             == gl_list_get_at (list2, index + 1));
139                   }
140               }
141             break;
142           case 1:
143             {
144               const char *obj = RANDOM_OBJECT ();
145               gl_list_node_t node1, node2, node3;
146               node1 = gl_list_search (list1, obj);
147               node2 = gl_list_search (list2, obj);
148               node3 = gl_list_search (list3, obj);
149               if (node1 == NULL)
150                 {
151                   ASSERT (node2 == NULL);
152                   ASSERT (node3 == NULL);
153                 }
154               else
155                 {
156                   ASSERT (node2 != NULL);
157                   ASSERT (node3 != NULL);
158                   ASSERT (gl_list_node_value (list1, node1) == obj);
159                   ASSERT (gl_list_node_value (list2, node2) == obj);
160                   ASSERT (gl_list_node_value (list3, node3) == obj);
161                 }
162             }
163             break;
164           case 2:
165             {
166               const char *obj = RANDOM_OBJECT ();
167               size_t index1, index2, index3;
168               index1 = gl_list_indexof (list1, obj);
169               index2 = gl_list_indexof (list2, obj);
170               index3 = gl_list_indexof (list3, obj);
171               if (index1 == (size_t)(-1))
172                 {
173                   ASSERT (index2 == (size_t)(-1));
174                   ASSERT (index3 == (size_t)(-1));
175                 }
176               else
177                 {
178                   ASSERT (index2 != (size_t)(-1));
179                   ASSERT (index3 != (size_t)(-1));
180                   ASSERT (gl_list_get_at (list1, index1) == obj);
181                   ASSERT (gl_list_get_at (list2, index2) == obj);
182                   ASSERT (gl_list_get_at (list3, index3) == obj);
183                   ASSERT (index2 == index1);
184                   ASSERT (index3 == index1);
185                 }
186             }
187             break;
188           case 3: /* add 1 element */
189             {
190               const char *obj = RANDOM_OBJECT ();
191               gl_list_node_t node1, node2, node3;
192               node1 = gl_list_nx_add_first (list1, obj);
193               ASSERT (node1 != NULL);
194               node2 = gl_list_nx_add_first (list2, obj);
195               ASSERT (node2 != NULL);
196               node3 = gl_list_nx_add_first (list3, obj);
197               ASSERT (node3 != NULL);
198               ASSERT (gl_list_node_value (list1, node1) == obj);
199               ASSERT (gl_list_node_value (list2, node2) == obj);
200               ASSERT (gl_list_node_value (list3, node3) == obj);
201               ASSERT (gl_list_get_at (list1, 0) == obj);
202               ASSERT (gl_list_get_at (list2, 0) == obj);
203               ASSERT (gl_list_get_at (list3, 0) == obj);
204             }
205             break;
206           case 4: /* add 1 element */
207             {
208               const char *obj = RANDOM_OBJECT ();
209               gl_list_node_t node1, node2, node3;
210               node1 = gl_list_nx_add_last (list1, obj);
211               ASSERT (node1 != NULL);
212               node2 = gl_list_nx_add_last (list2, obj);
213               ASSERT (node2 != NULL);
214               node3 = gl_list_nx_add_last (list3, obj);
215               ASSERT (node3 != NULL);
216               ASSERT (gl_list_node_value (list1, node1) == obj);
217               ASSERT (gl_list_node_value (list2, node2) == obj);
218               ASSERT (gl_list_node_value (list3, node3) == obj);
219               ASSERT (gl_list_get_at (list1, gl_list_size (list1) - 1) == obj);
220               ASSERT (gl_list_get_at (list2, gl_list_size (list2) - 1) == obj);
221               ASSERT (gl_list_get_at (list3, gl_list_size (list3) - 1) == obj);
222             }
223             break;
224           case 5: /* add 3 elements */
225             {
226               const char *obj0 = RANDOM_OBJECT ();
227               const char *obj1 = RANDOM_OBJECT ();
228               const char *obj2 = RANDOM_OBJECT ();
229               gl_list_node_t node1, node2, node3;
230               node1 = gl_list_nx_add_first (list1, obj2);
231               ASSERT (node1 != NULL);
232               node1 = gl_list_nx_add_before (list1, node1, obj0);
233               ASSERT (node1 != NULL);
234               node1 = gl_list_nx_add_after (list1, node1, obj1);
235               ASSERT (node1 != NULL);
236               node2 = gl_list_nx_add_first (list2, obj2);
237               ASSERT (node2 != NULL);
238               node2 = gl_list_nx_add_before (list2, node2, obj0);
239               ASSERT (node2 != NULL);
240               node2 = gl_list_nx_add_after (list2, node2, obj1);
241               ASSERT (node2 != NULL);
242               node3 = gl_list_nx_add_first (list3, obj2);
243               ASSERT (node3 != NULL);
244               node3 = gl_list_nx_add_before (list3, node3, obj0);
245               ASSERT (node3 != NULL);
246               node3 = gl_list_nx_add_after (list3, node3, obj1);
247               ASSERT (node3 != NULL);
248               ASSERT (gl_list_node_value (list1, node1) == obj1);
249               ASSERT (gl_list_node_value (list2, node2) == obj1);
250               ASSERT (gl_list_node_value (list3, node3) == obj1);
251               ASSERT (gl_list_get_at (list1, 0) == obj0);
252               ASSERT (gl_list_get_at (list1, 1) == obj1);
253               ASSERT (gl_list_get_at (list1, 2) == obj2);
254               ASSERT (gl_list_get_at (list2, 0) == obj0);
255               ASSERT (gl_list_get_at (list2, 1) == obj1);
256               ASSERT (gl_list_get_at (list2, 2) == obj2);
257               ASSERT (gl_list_get_at (list3, 0) == obj0);
258               ASSERT (gl_list_get_at (list3, 1) == obj1);
259               ASSERT (gl_list_get_at (list3, 2) == obj2);
260             }
261             break;
262           case 6: /* add 1 element */
263             {
264               size_t index = RANDOM (gl_list_size (list1) + 1);
265               const char *obj = RANDOM_OBJECT ();
266               gl_list_node_t node1, node2, node3;
267               node1 = gl_list_nx_add_at (list1, index, obj);
268               ASSERT (node1 != NULL);
269               node2 = gl_list_nx_add_at (list2, index, obj);
270               ASSERT (node2 != NULL);
271               node3 = gl_list_nx_add_at (list3, index, obj);
272               ASSERT (node3 != NULL);
273               ASSERT (gl_list_get_at (list1, index) == obj);
274               ASSERT (gl_list_node_value (list1, node1) == obj);
275               ASSERT (gl_list_get_at (list2, index) == obj);
276               ASSERT (gl_list_node_value (list2, node2) == obj);
277               ASSERT (gl_list_get_at (list3, index) == obj);
278               ASSERT (gl_list_node_value (list3, node3) == obj);
279               if (index > 0)
280                 {
281                   ASSERT (gl_list_node_value (list1, gl_list_previous_node (list1, node1))
282                           == gl_list_get_at (list1, index - 1));
283                   ASSERT (gl_list_node_value (list2, gl_list_previous_node (list3, node3))
284                           == gl_list_get_at (list2, index - 1));
285                   ASSERT (gl_list_node_value (list3, gl_list_previous_node (list3, node3))
286                           == gl_list_get_at (list2, index - 1));
287                 }
288               if (index + 1 < gl_list_size (list1))
289                 {
290                   ASSERT (gl_list_node_value (list1, gl_list_next_node (list1, node1))
291                           == gl_list_get_at (list1, index + 1));
292                   ASSERT (gl_list_node_value (list2, gl_list_next_node (list3, node3))
293                           == gl_list_get_at (list2, index + 1));
294                   ASSERT (gl_list_node_value (list3, gl_list_next_node (list3, node3))
295                           == gl_list_get_at (list2, index + 1));
296                 }
297             }
298             break;
299           case 7: case 8: /* remove 1 element */
300             if (gl_list_size (list1) > 0)
301               {
302                 size_t n = gl_list_size (list1);
303                 const char *obj = gl_list_get_at (list1, RANDOM (n));
304                 gl_list_node_t node1, node2, node3;
305                 node1 = gl_list_search (list1, obj);
306                 node2 = gl_list_search (list2, obj);
307                 node3 = gl_list_search (list3, obj);
308                 ASSERT (node1 != NULL);
309                 ASSERT (node2 != NULL);
310                 ASSERT (node3 != NULL);
311                 ASSERT (gl_list_remove_node (list1, node1));
312                 ASSERT (gl_list_remove_node (list2, node2));
313                 ASSERT (gl_list_remove_node (list3, node3));
314                 ASSERT (gl_list_size (list1) == n - 1);
315               }
316             break;
317           case 9: case 10: /* remove 1 element */
318             if (gl_list_size (list1) > 0)
319               {
320                 size_t n = gl_list_size (list1);
321                 size_t index = RANDOM (n);
322                 ASSERT (gl_list_remove_at (list1, index));
323                 ASSERT (gl_list_remove_at (list2, index));
324                 ASSERT (gl_list_remove_at (list3, index));
325                 ASSERT (gl_list_size (list1) == n - 1);
326               }
327             break;
328           case 11: case 12: /* remove 1 element */
329             if (gl_list_size (list1) > 0)
330               {
331                 size_t n = gl_list_size (list1);
332                 const char *obj = gl_list_get_at (list1, RANDOM (n));
333                 ASSERT (gl_list_remove (list1, obj));
334                 ASSERT (gl_list_remove (list2, obj));
335                 ASSERT (gl_list_remove (list3, obj));
336                 ASSERT (gl_list_size (list1) == n - 1);
337               }
338             break;
339           case 13:
340             if (gl_list_size (list1) > 0)
341               {
342                 size_t n = gl_list_size (list1);
343                 const char *obj = "xyzzy";
344                 ASSERT (!gl_list_remove (list1, obj));
345                 ASSERT (!gl_list_remove (list2, obj));
346                 ASSERT (!gl_list_remove (list3, obj));
347                 ASSERT (gl_list_size (list1) == n);
348               }
349             break;
350           case 14:
351             {
352               size_t n = gl_list_size (list1);
353               gl_list_iterator_t iter1, iter2, iter3;
354               const void *elt;
355               iter1 = gl_list_iterator (list1);
356               iter2 = gl_list_iterator (list2);
357               iter3 = gl_list_iterator (list3);
358               for (i = 0; i < n; i++)
359                 {
360                   ASSERT (gl_list_iterator_next (&iter1, &elt, NULL));
361                   ASSERT (gl_list_get_at (list1, i) == elt);
362                   ASSERT (gl_list_iterator_next (&iter2, &elt, NULL));
363                   ASSERT (gl_list_get_at (list2, i) == elt);
364                   ASSERT (gl_list_iterator_next (&iter3, &elt, NULL));
365                   ASSERT (gl_list_get_at (list3, i) == elt);
366                 }
367               ASSERT (!gl_list_iterator_next (&iter1, &elt, NULL));
368               ASSERT (!gl_list_iterator_next (&iter2, &elt, NULL));
369               ASSERT (!gl_list_iterator_next (&iter3, &elt, NULL));
370               gl_list_iterator_free (&iter1);
371               gl_list_iterator_free (&iter2);
372               gl_list_iterator_free (&iter3);
373             }
374             break;
375           case 15:
376             {
377               size_t end = RANDOM (gl_list_size (list1) + 1);
378               size_t start = RANDOM (end + 1);
379               gl_list_iterator_t iter1, iter2, iter3;
380               const void *elt;
381               iter1 = gl_list_iterator_from_to (list1, start, end);
382               iter2 = gl_list_iterator_from_to (list2, start, end);
383               iter3 = gl_list_iterator_from_to (list3, start, end);
384               for (i = start; i < end; i++)
385                 {
386                   ASSERT (gl_list_iterator_next (&iter1, &elt, NULL));
387                   ASSERT (gl_list_get_at (list1, i) == elt);
388                   ASSERT (gl_list_iterator_next (&iter2, &elt, NULL));
389                   ASSERT (gl_list_get_at (list2, i) == elt);
390                   ASSERT (gl_list_iterator_next (&iter3, &elt, NULL));
391                   ASSERT (gl_list_get_at (list3, i) == elt);
392                 }
393               ASSERT (!gl_list_iterator_next (&iter1, &elt, NULL));
394               ASSERT (!gl_list_iterator_next (&iter2, &elt, NULL));
395               ASSERT (!gl_list_iterator_next (&iter3, &elt, NULL));
396               gl_list_iterator_free (&iter1);
397               gl_list_iterator_free (&iter2);
398               gl_list_iterator_free (&iter3);
399             }
400             break;
401           }
402         check_all (list1, list2, list3);
403       }
404
405     gl_list_free (list1);
406     gl_list_free (list2);
407     gl_list_free (list3);
408     free (contents);
409   }
410
411   return 0;
412 }