* lib/string_.h: If the gnulib module XYZ is not present, undefine
[gnulib.git] / tests / test-avltree_oset.c
1 /* Test of ordered set data type implementation.
2    Copyright (C) 2006 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 <stdlib.h>
24 #include <string.h>
25
26 #include "gl_array_oset.h"
27 #include "gl_avltree_oset.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(condition) if (!(condition)) abort ()
39 #define RANDOM(n) (rand () % (n))
40 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
41
42 static void
43 check_equals (gl_oset_t set1, gl_oset_t set2)
44 {
45   size_t n = gl_oset_size (set1);
46   gl_oset_iterator_t iter1, iter2;
47   const void *elt1;
48   const void *elt2;
49   size_t i;
50
51   iter1 = gl_oset_iterator (set1);
52   iter2 = gl_oset_iterator (set2);
53   for (i = 0; i < n; i++)
54     {
55       ASSERT (gl_oset_iterator_next (&iter1, &elt1));
56       ASSERT (gl_oset_iterator_next (&iter2, &elt2));
57       ASSERT (elt1 == elt2);
58     }
59   ASSERT (!gl_oset_iterator_next (&iter1, &elt1));
60   ASSERT (!gl_oset_iterator_next (&iter2, &elt2));
61   gl_oset_iterator_free (&iter1);
62   gl_oset_iterator_free (&iter2);
63 }
64
65 static void
66 check_all (gl_oset_t set1, gl_oset_t set2)
67 {
68   gl_avltree_oset_check_invariants (set2);
69   check_equals (set1, set2);
70 }
71
72 int
73 main (int argc, char *argv[])
74 {
75   gl_oset_t set1, set2;
76
77   /* Allow the user to provide a non-default random seed on the command line.  */
78   if (argc > 1)
79     srand (atoi (argv[1]));
80
81   {
82     size_t initial_size = RANDOM (20);
83     size_t i;
84     unsigned int repeat;
85
86     /* Create set1.  */
87     set1 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp);
88
89     /* Create set2.  */
90     set2 = gl_oset_create_empty (GL_AVLTREE_OSET, (gl_setelement_compar_fn) strcmp);
91
92     check_all (set1, set2);
93
94     /* Initialize them.  */
95     for (i = 0; i < initial_size; i++)
96       {
97         const char *obj = RANDOM_OBJECT ();
98         ASSERT (gl_oset_add (set1, obj) == gl_oset_add (set2, obj));
99         check_all (set1, set2);
100       }
101
102     for (repeat = 0; repeat < 100000; repeat++)
103       {
104         unsigned int operation = RANDOM (3);
105         switch (operation)
106           {
107           case 0:
108             {
109               const char *obj = RANDOM_OBJECT ();
110               ASSERT (gl_oset_search (set1, obj) == gl_oset_search (set2, obj));
111             }
112             break;
113           case 1:
114             {
115               const char *obj = RANDOM_OBJECT ();
116               ASSERT (gl_oset_add (set1, obj) == gl_oset_add (set2, obj));
117             }
118             break;
119           case 2:
120             {
121               const char *obj = RANDOM_OBJECT ();
122               ASSERT (gl_oset_remove (set1, obj) == gl_oset_remove (set2, obj));
123             }
124             break;
125           }
126         check_all (set1, set2);
127       }
128
129     gl_oset_free (set1);
130     gl_oset_free (set2);
131   }
132
133   return 0;
134 }