maint: update copyright
[gnulib.git] / tests / test-array_oset.c
1 /* Test of ordered set data type implementation.
2    Copyright (C) 2006-2014 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 <stdlib.h>
23 #include <string.h>
24
25 #include "gl_xlist.h"
26 #include "gl_array_list.h"
27 #include "progname.h"
28 #include "macros.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 RANDOM(n) (rand () % (n))
37 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
38
39 static void
40 check_equals (gl_oset_t set1, gl_list_t set2)
41 {
42   size_t n = gl_oset_size (set1);
43   gl_oset_iterator_t iter1;
44   gl_list_iterator_t iter2;
45   const void *elt1;
46   const void *elt2;
47   gl_list_node_t node2;
48   size_t i;
49
50   iter1 = gl_oset_iterator (set1);
51   iter2 = gl_list_iterator (set2);
52   for (i = 0; i < n; i++)
53     {
54       ASSERT (gl_oset_iterator_next (&iter1, &elt1));
55       ASSERT (gl_list_iterator_next (&iter2, &elt2, &node2));
56       ASSERT (elt1 == elt2);
57     }
58   ASSERT (!gl_oset_iterator_next (&iter1, &elt1));
59   ASSERT (!gl_list_iterator_next (&iter2, &elt2, &node2));
60   gl_oset_iterator_free (&iter1);
61   gl_list_iterator_free (&iter2);
62 }
63
64 static void
65 check_all (gl_oset_t set1, gl_list_t set2)
66 {
67   check_equals (set1, set2);
68 }
69
70 int
71 main (int argc, char *argv[])
72 {
73   gl_oset_t set1;
74   gl_list_t set2;
75
76   set_program_name (argv[0]);
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_nx_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL);
89     ASSERT (set1 != NULL);
90
91     /* Create set2.  */
92     set2 = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, false);
93
94     check_all (set1, set2);
95
96     /* Initialize them.  */
97     for (i = 0; i < initial_size; i++)
98       {
99         const char *obj = RANDOM_OBJECT ();
100         ASSERT (gl_oset_nx_add (set1, obj)
101                 == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL
102                     ? false
103                     : (gl_sortedlist_add (set2, (gl_listelement_compar_fn)strcmp, obj), true)));
104         check_all (set1, set2);
105       }
106
107     for (repeat = 0; repeat < 100000; repeat++)
108       {
109         unsigned int operation = RANDOM (3);
110         switch (operation)
111           {
112           case 0:
113             {
114               const char *obj = RANDOM_OBJECT ();
115               ASSERT (gl_oset_search (set1, obj)
116                       == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL));
117             }
118             break;
119           case 1:
120             {
121               const char *obj = RANDOM_OBJECT ();
122               ASSERT (gl_oset_nx_add (set1, obj)
123                       == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL
124                           ? false
125                           : (gl_sortedlist_add (set2, (gl_listelement_compar_fn)strcmp, obj), true)));
126             }
127             break;
128           case 2:
129             {
130               const char *obj = RANDOM_OBJECT ();
131               ASSERT (gl_oset_remove (set1, obj)
132                       == gl_sortedlist_remove (set2, (gl_listelement_compar_fn)strcmp, obj));
133             }
134             break;
135           }
136         check_all (set1, set2);
137       }
138
139     gl_oset_free (set1);
140     gl_list_free (set2);
141   }
142
143   return 0;
144 }