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