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