Make roundf-tests module depend on floorf, ceilf.
[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 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           abort ();                                                          \
43         }                                                                    \
44     }                                                                        \
45   while (0)
46 #define RANDOM(n) (rand () % (n))
47 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
48
49 static void
50 check_equals (gl_oset_t set1, gl_list_t set2)
51 {
52   size_t n = gl_oset_size (set1);
53   gl_oset_iterator_t iter1;
54   gl_list_iterator_t iter2;
55   const void *elt1;
56   const void *elt2;
57   gl_list_node_t node2;
58   size_t i;
59
60   iter1 = gl_oset_iterator (set1);
61   iter2 = gl_list_iterator (set2);
62   for (i = 0; i < n; i++)
63     {
64       ASSERT (gl_oset_iterator_next (&iter1, &elt1));
65       ASSERT (gl_list_iterator_next (&iter2, &elt2, &node2));
66       ASSERT (elt1 == elt2);
67     }
68   ASSERT (!gl_oset_iterator_next (&iter1, &elt1));
69   ASSERT (!gl_list_iterator_next (&iter2, &elt2, &node2));
70   gl_oset_iterator_free (&iter1);
71   gl_list_iterator_free (&iter2);
72 }
73
74 static void
75 check_all (gl_oset_t set1, gl_list_t set2)
76 {
77   check_equals (set1, set2);
78 }
79
80 int
81 main (int argc, char *argv[])
82 {
83   gl_oset_t set1;
84   gl_list_t set2;
85
86   set_program_name (argv[0]);
87
88   /* Allow the user to provide a non-default random seed on the command line.  */
89   if (argc > 1)
90     srand (atoi (argv[1]));
91
92   {
93     size_t initial_size = RANDOM (20);
94     size_t i;
95     unsigned int repeat;
96
97     /* Create set1.  */
98     set1 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL);
99
100     /* Create set2.  */
101     set2 = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, false);
102
103     check_all (set1, set2);
104
105     /* Initialize them.  */
106     for (i = 0; i < initial_size; i++)
107       {
108         const char *obj = RANDOM_OBJECT ();
109         ASSERT (gl_oset_add (set1, obj)
110                 == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL
111                     ? false
112                     : (gl_sortedlist_add (set2, (gl_listelement_compar_fn)strcmp, obj), true)));
113         check_all (set1, set2);
114       }
115
116     for (repeat = 0; repeat < 100000; repeat++)
117       {
118         unsigned int operation = RANDOM (3);
119         switch (operation)
120           {
121           case 0:
122             {
123               const char *obj = RANDOM_OBJECT ();
124               ASSERT (gl_oset_search (set1, obj)
125                       == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL));
126             }
127             break;
128           case 1:
129             {
130               const char *obj = RANDOM_OBJECT ();
131               ASSERT (gl_oset_add (set1, obj)
132                       == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL
133                           ? false
134                           : (gl_sortedlist_add (set2, (gl_listelement_compar_fn)strcmp, obj), true)));
135             }
136             break;
137           case 2:
138             {
139               const char *obj = RANDOM_OBJECT ();
140               ASSERT (gl_oset_remove (set1, obj)
141                       == gl_sortedlist_remove (set2, (gl_listelement_compar_fn)strcmp, obj));
142             }
143             break;
144           }
145         check_all (set1, set2);
146       }
147
148     gl_oset_free (set1);
149     gl_list_free (set2);
150   }
151
152   return 0;
153 }