usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-array_set.c
blob1a9bdc680305aa85b71631fea9e94af84708fa3e
1 /* Test of set data type implementation.
2 Copyright (C) 2006-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2018.
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_array_set.h"
22 #include <stdlib.h>
23 #include <string.h>
25 #include "gl_xoset.h"
26 #include "gl_array_oset.h"
27 #include "xalloc.h"
28 #include "macros.h"
30 static const char *objects[30] =
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", "<", ">", "[", "]"
36 #define RANDOM(n) (rand () % (n))
37 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
39 static int
40 cmp_objects_in_array (const void *objptr1, const void *objptr2)
42 const void *obj1 = *(const void * const *)objptr1;
43 const void *obj2 = *(const void * const *)objptr2;
44 return strcmp ((const char *) obj1, (const char *) obj2);
47 static void
48 check_equals (gl_set_t set1, gl_oset_t set2)
50 size_t n = gl_set_size (set1);
51 const void **elements_of_set1 = XNMALLOC (n, const void *);
52 const void **elements_of_set2 = XNMALLOC (n, const void *);
54 gl_set_iterator_t iter1;
55 gl_oset_iterator_t iter2;
56 const void *elt1;
57 const void *elt2;
58 size_t i;
60 iter1 = gl_set_iterator (set1);
61 iter2 = gl_oset_iterator (set2);
62 for (i = 0; i < n; i++)
64 ASSERT (gl_set_iterator_next (&iter1, &elt1));
65 ASSERT (gl_oset_iterator_next (&iter2, &elt2));
66 elements_of_set1[i] = elt1;
67 elements_of_set2[i] = elt2;
69 ASSERT (!gl_set_iterator_next (&iter1, &elt1));
70 ASSERT (!gl_oset_iterator_next (&iter2, &elt2));
71 gl_set_iterator_free (&iter1);
72 gl_oset_iterator_free (&iter2);
74 if (n > 0)
76 qsort (elements_of_set1, n, sizeof (const void *), cmp_objects_in_array);
77 qsort (elements_of_set2, n, sizeof (const void *), cmp_objects_in_array);
79 for (i = 0; i < n; i++)
80 ASSERT (elements_of_set1[i] == elements_of_set2[i]);
81 free (elements_of_set2);
82 free (elements_of_set1);
85 static void
86 check_all (gl_set_t set1, gl_oset_t set2)
88 check_equals (set1, set2);
91 int
92 main (int argc, char *argv[])
94 gl_set_t set1;
95 gl_oset_t set2;
97 /* Allow the user to provide a non-default random seed on the command line. */
98 if (argc > 1)
99 srand (atoi (argv[1]));
102 size_t initial_size = RANDOM (20);
103 size_t i;
104 unsigned int repeat;
106 /* Create set1. */
107 set1 = gl_set_nx_create_empty (GL_ARRAY_SET, NULL, NULL, NULL);
108 ASSERT (set1 != NULL);
110 /* Create set2. */
111 set2 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL);
113 check_all (set1, set2);
115 /* Initialize them. */
116 for (i = 0; i < initial_size; i++)
118 const char *obj = RANDOM_OBJECT ();
119 ASSERT (gl_set_nx_add (set1, obj) == gl_oset_add (set2, obj));
120 check_all (set1, set2);
123 for (repeat = 0; repeat < 100000; repeat++)
125 unsigned int operation = RANDOM (3);
126 switch (operation)
128 case 0:
130 const char *obj = RANDOM_OBJECT ();
131 ASSERT (gl_set_search (set1, obj) == gl_oset_search (set2, obj));
133 break;
134 case 1:
136 const char *obj = RANDOM_OBJECT ();
137 ASSERT (gl_set_nx_add (set1, obj) == gl_oset_add (set2, obj));
139 break;
140 case 2:
142 const char *obj = RANDOM_OBJECT ();
143 ASSERT (gl_set_remove (set1, obj) == gl_oset_remove (set2, obj));
145 break;
147 check_all (set1, set2);
150 gl_set_free (set1);
151 gl_oset_free (set2);
154 return test_exit_status;