Bump to 1.14.1
[platform/upstream/augeas.git] / tests / test-avltree_oset.c
1 /* Test of ordered set data type implementation.
2    Copyright (C) 2006-2016 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2006.
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_avltree_oset.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "gl_array_oset.h"
26 #include "macros.h"
27
28 extern void gl_avltree_oset_check_invariants (gl_oset_t set);
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_oset_t set2)
41 {
42   size_t n = gl_oset_size (set1);
43   gl_oset_iterator_t iter1, iter2;
44   const void *elt1;
45   const void *elt2;
46   size_t i;
47
48   iter1 = gl_oset_iterator (set1);
49   iter2 = gl_oset_iterator (set2);
50   for (i = 0; i < n; i++)
51     {
52       ASSERT (gl_oset_iterator_next (&iter1, &elt1));
53       ASSERT (gl_oset_iterator_next (&iter2, &elt2));
54       ASSERT (elt1 == elt2);
55     }
56   ASSERT (!gl_oset_iterator_next (&iter1, &elt1));
57   ASSERT (!gl_oset_iterator_next (&iter2, &elt2));
58   gl_oset_iterator_free (&iter1);
59   gl_oset_iterator_free (&iter2);
60 }
61
62 static void
63 check_all (gl_oset_t set1, gl_oset_t set2)
64 {
65   gl_avltree_oset_check_invariants (set2);
66   check_equals (set1, set2);
67 }
68
69 int
70 main (int argc, char *argv[])
71 {
72   gl_oset_t set1, set2;
73
74   /* Allow the user to provide a non-default random seed on the command line.  */
75   if (argc > 1)
76     srand (atoi (argv[1]));
77
78   {
79     size_t initial_size = RANDOM (20);
80     size_t i;
81     unsigned int repeat;
82
83     /* Create set1.  */
84     set1 = gl_oset_nx_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL);
85     ASSERT (set1 != NULL);
86
87     /* Create set2.  */
88     set2 = gl_oset_nx_create_empty (GL_AVLTREE_OSET, (gl_setelement_compar_fn) strcmp, NULL);
89     ASSERT (set2 != NULL);
90
91     check_all (set1, set2);
92
93     /* Initialize them.  */
94     for (i = 0; i < initial_size; i++)
95       {
96         const char *obj = RANDOM_OBJECT ();
97         ASSERT (gl_oset_nx_add (set1, obj) == gl_oset_nx_add (set2, obj));
98         check_all (set1, set2);
99       }
100
101     for (repeat = 0; repeat < 100000; repeat++)
102       {
103         unsigned int operation = RANDOM (3);
104         switch (operation)
105           {
106           case 0:
107             {
108               const char *obj = RANDOM_OBJECT ();
109               ASSERT (gl_oset_search (set1, obj) == gl_oset_search (set2, obj));
110             }
111             break;
112           case 1:
113             {
114               const char *obj = RANDOM_OBJECT ();
115               ASSERT (gl_oset_nx_add (set1, obj) == gl_oset_nx_add (set2, obj));
116             }
117             break;
118           case 2:
119             {
120               const char *obj = RANDOM_OBJECT ();
121               ASSERT (gl_oset_remove (set1, obj) == gl_oset_remove (set2, obj));
122             }
123             break;
124           }
125         check_all (set1, set2);
126       }
127
128     gl_oset_free (set1);
129     gl_oset_free (set2);
130   }
131
132   return 0;
133 }