usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-rbtree_oset.c
blobce3bbc1a72155ca4e925d0666038ce482ffbc92d
1 /* Test of ordered set data type implementation.
2 Copyright (C) 2006-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 #include "gl_rbtree_oset.h"
22 #include <stdlib.h>
23 #include <string.h>
25 #include "gl_array_oset.h"
26 #include "macros.h"
28 #include "test-oset-update.h"
30 extern void gl_rbtree_oset_check_invariants (gl_oset_t set);
32 static const char *objects[30] =
34 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
35 "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "<", ">", "[", "]"
38 #define RANDOM(n) (rand () % (n))
39 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
41 static void
42 check_equals (gl_oset_t set1, gl_oset_t set2)
44 size_t n = gl_oset_size (set1);
45 gl_oset_iterator_t iter1, iter2;
46 const void *elt1;
47 const void *elt2;
48 size_t i;
50 iter1 = gl_oset_iterator (set1);
51 iter2 = gl_oset_iterator (set2);
52 for (i = 0; i < n; i++)
54 ASSERT (gl_oset_iterator_next (&iter1, &elt1));
55 ASSERT (gl_oset_iterator_next (&iter2, &elt2));
56 ASSERT (elt1 == elt2);
58 ASSERT (!gl_oset_iterator_next (&iter1, &elt1));
59 ASSERT (!gl_oset_iterator_next (&iter2, &elt2));
60 gl_oset_iterator_free (&iter1);
61 gl_oset_iterator_free (&iter2);
64 static void
65 check_all (gl_oset_t set1, gl_oset_t set2)
67 gl_rbtree_oset_check_invariants (set2);
68 check_equals (set1, set2);
71 static bool
72 is_at_least (const void *elt, const void *threshold)
74 return strcmp ((const char *) elt, (const char *) threshold) >= 0;
77 int
78 main (int argc, char *argv[])
80 gl_oset_t set1, set2;
82 /* Allow the user to provide a non-default random seed on the command line. */
83 if (argc > 1)
84 srand (atoi (argv[1]));
87 size_t initial_size = RANDOM (20);
88 size_t i;
89 unsigned int repeat;
91 /* Create set1. */
92 set1 = gl_oset_nx_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL);
93 ASSERT (set1 != NULL);
95 /* Create set2. */
96 set2 = gl_oset_nx_create_empty (GL_RBTREE_OSET, (gl_setelement_compar_fn) strcmp, NULL);
97 ASSERT (set2 != NULL);
99 check_all (set1, set2);
101 /* Initialize them. */
102 for (i = 0; i < initial_size; i++)
104 const char *obj = RANDOM_OBJECT ();
105 ASSERT (gl_oset_nx_add (set1, obj) == gl_oset_nx_add (set2, obj));
106 check_all (set1, set2);
109 for (repeat = 0; repeat < 100000; repeat++)
111 unsigned int operation = RANDOM (4);
112 switch (operation)
114 case 0:
116 const char *obj = RANDOM_OBJECT ();
117 ASSERT (gl_oset_search (set1, obj) == gl_oset_search (set2, obj));
119 break;
120 case 1:
122 const char *obj = RANDOM_OBJECT ();
123 ASSERT (gl_oset_nx_add (set1, obj) == gl_oset_nx_add (set2, obj));
125 break;
126 case 2:
128 const char *obj = RANDOM_OBJECT ();
129 ASSERT (gl_oset_remove (set1, obj) == gl_oset_remove (set2, obj));
131 break;
132 case 3:
134 const char *obj = RANDOM_OBJECT ();
135 gl_oset_iterator_t iter1 = gl_oset_iterator_atleast (set1, is_at_least, obj);
136 gl_oset_iterator_t iter2 = gl_oset_iterator_atleast (set2, is_at_least, obj);
137 const void *elt1;
138 const void *elt2;
139 /* Check the first two values that the iterator produces.
140 Checking them all would make this part of the test dominate the
141 run time of the test. */
142 bool havenext1 = gl_oset_iterator_next (&iter1, &elt1);
143 bool havenext2 = gl_oset_iterator_next (&iter2, &elt2);
144 ASSERT (havenext1 == havenext2);
145 if (havenext1)
147 ASSERT (elt1 == elt2);
148 havenext1 = gl_oset_iterator_next (&iter1, &elt1);
149 havenext2 = gl_oset_iterator_next (&iter2, &elt2);
150 ASSERT (havenext1 == havenext2);
151 if (havenext1)
152 ASSERT (elt1 == elt2);
154 gl_oset_iterator_free (&iter1);
155 gl_oset_iterator_free (&iter2);
157 break;
159 check_all (set1, set2);
162 gl_oset_free (set1);
163 gl_oset_free (set2);
166 test_update (GL_RBTREE_OSET);
168 return test_exit_status;