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