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