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