Add an element disposal function.
[gnulib.git] / tests / test-avltree_oset.c
1 /* Test of ordered set 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 2, or (at your option)
8    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, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include "gl_avltree_oset.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "gl_array_oset.h"
29 #include "progname.h"
30
31 extern void gl_avltree_oset_check_invariants (gl_oset_t set);
32
33 static const char *objects[30] =
34   {
35     "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
36     "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "<", ">", "[", "]"
37   };
38
39 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
40 #define ASSERT(condition) if (!(condition)) abort ()
41 #define RANDOM(n) (rand () % (n))
42 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
43
44 static void
45 check_equals (gl_oset_t set1, gl_oset_t set2)
46 {
47   size_t n = gl_oset_size (set1);
48   gl_oset_iterator_t iter1, iter2;
49   const void *elt1;
50   const void *elt2;
51   size_t i;
52
53   iter1 = gl_oset_iterator (set1);
54   iter2 = gl_oset_iterator (set2);
55   for (i = 0; i < n; i++)
56     {
57       ASSERT (gl_oset_iterator_next (&iter1, &elt1));
58       ASSERT (gl_oset_iterator_next (&iter2, &elt2));
59       ASSERT (elt1 == elt2);
60     }
61   ASSERT (!gl_oset_iterator_next (&iter1, &elt1));
62   ASSERT (!gl_oset_iterator_next (&iter2, &elt2));
63   gl_oset_iterator_free (&iter1);
64   gl_oset_iterator_free (&iter2);
65 }
66
67 static void
68 check_all (gl_oset_t set1, gl_oset_t set2)
69 {
70   gl_avltree_oset_check_invariants (set2);
71   check_equals (set1, set2);
72 }
73
74 int
75 main (int argc, char *argv[])
76 {
77   gl_oset_t set1, set2;
78
79   /* Allow the user to provide a non-default random seed on the command line.  */
80   if (argc > 1)
81     srand (atoi (argv[1]));
82
83   {
84     size_t initial_size = RANDOM (20);
85     size_t i;
86     unsigned int repeat;
87
88     /* Create set1.  */
89     set1 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL);
90
91     /* Create set2.  */
92     set2 = gl_oset_create_empty (GL_AVLTREE_OSET, (gl_setelement_compar_fn) strcmp, NULL);
93
94     check_all (set1, set2);
95
96     /* Initialize them.  */
97     for (i = 0; i < initial_size; i++)
98       {
99         const char *obj = RANDOM_OBJECT ();
100         ASSERT (gl_oset_add (set1, obj) == gl_oset_add (set2, obj));
101         check_all (set1, set2);
102       }
103
104     for (repeat = 0; repeat < 100000; repeat++)
105       {
106         unsigned int operation = RANDOM (3);
107         switch (operation)
108           {
109           case 0:
110             {
111               const char *obj = RANDOM_OBJECT ();
112               ASSERT (gl_oset_search (set1, obj) == gl_oset_search (set2, obj));
113             }
114             break;
115           case 1:
116             {
117               const char *obj = RANDOM_OBJECT ();
118               ASSERT (gl_oset_add (set1, obj) == gl_oset_add (set2, obj));
119             }
120             break;
121           case 2:
122             {
123               const char *obj = RANDOM_OBJECT ();
124               ASSERT (gl_oset_remove (set1, obj) == gl_oset_remove (set2, obj));
125             }
126             break;
127           }
128         check_all (set1, set2);
129       }
130
131     gl_oset_free (set1);
132     gl_oset_free (set2);
133   }
134
135   return 0;
136 }