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