Change copyright notice from GPLv2+ to GPLv3+.
[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 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_oset.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "gl_array_oset.h"
27 #include "progname.h"
28
29 extern void gl_avltree_oset_check_invariants (gl_oset_t set);
30
31 static const char *objects[30] =
32   {
33     "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
34     "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "<", ">", "[", "]"
35   };
36
37 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
38 #define ASSERT(expr) \
39   do                                                                         \
40     {                                                                        \
41       if (!(expr))                                                           \
42         {                                                                    \
43           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
44           abort ();                                                          \
45         }                                                                    \
46     }                                                                        \
47   while (0)
48 #define RANDOM(n) (rand () % (n))
49 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
50
51 static void
52 check_equals (gl_oset_t set1, gl_oset_t set2)
53 {
54   size_t n = gl_oset_size (set1);
55   gl_oset_iterator_t iter1, iter2;
56   const void *elt1;
57   const void *elt2;
58   size_t i;
59
60   iter1 = gl_oset_iterator (set1);
61   iter2 = gl_oset_iterator (set2);
62   for (i = 0; i < n; i++)
63     {
64       ASSERT (gl_oset_iterator_next (&iter1, &elt1));
65       ASSERT (gl_oset_iterator_next (&iter2, &elt2));
66       ASSERT (elt1 == elt2);
67     }
68   ASSERT (!gl_oset_iterator_next (&iter1, &elt1));
69   ASSERT (!gl_oset_iterator_next (&iter2, &elt2));
70   gl_oset_iterator_free (&iter1);
71   gl_oset_iterator_free (&iter2);
72 }
73
74 static void
75 check_all (gl_oset_t set1, gl_oset_t set2)
76 {
77   gl_avltree_oset_check_invariants (set2);
78   check_equals (set1, set2);
79 }
80
81 int
82 main (int argc, char *argv[])
83 {
84   gl_oset_t set1, set2;
85
86   /* Allow the user to provide a non-default random seed on the command line.  */
87   if (argc > 1)
88     srand (atoi (argv[1]));
89
90   {
91     size_t initial_size = RANDOM (20);
92     size_t i;
93     unsigned int repeat;
94
95     /* Create set1.  */
96     set1 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL);
97
98     /* Create set2.  */
99     set2 = gl_oset_create_empty (GL_AVLTREE_OSET, (gl_setelement_compar_fn) strcmp, NULL);
100
101     check_all (set1, set2);
102
103     /* Initialize them.  */
104     for (i = 0; i < initial_size; i++)
105       {
106         const char *obj = RANDOM_OBJECT ();
107         ASSERT (gl_oset_add (set1, obj) == gl_oset_add (set2, obj));
108         check_all (set1, set2);
109       }
110
111     for (repeat = 0; repeat < 100000; repeat++)
112       {
113         unsigned int operation = RANDOM (3);
114         switch (operation)
115           {
116           case 0:
117             {
118               const char *obj = RANDOM_OBJECT ();
119               ASSERT (gl_oset_search (set1, obj) == gl_oset_search (set2, obj));
120             }
121             break;
122           case 1:
123             {
124               const char *obj = RANDOM_OBJECT ();
125               ASSERT (gl_oset_add (set1, obj) == gl_oset_add (set2, obj));
126             }
127             break;
128           case 2:
129             {
130               const char *obj = RANDOM_OBJECT ();
131               ASSERT (gl_oset_remove (set1, obj) == gl_oset_remove (set2, obj));
132             }
133             break;
134           }
135         check_all (set1, set2);
136       }
137
138     gl_oset_free (set1);
139     gl_oset_free (set2);
140   }
141
142   return 0;
143 }