expl: Work around inaccurate implementation on NetBSD.
[gnulib.git] / tests / test-array_oset.c
blob35a63c7df2f3cf6930c8eb0cf5bbedd0ba60c59a
1 /* Test of ordered set data type implementation.
2 Copyright (C) 2006-2019 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2007.
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_oset.h"
22 #include <stdlib.h>
23 #include <string.h>
25 #include "gl_xlist.h"
26 #include "gl_array_list.h"
27 #include "macros.h"
29 static const char *objects[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", "<", ">", "[", "]"
35 #define RANDOM(n) (rand () % (n))
36 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
38 static void
39 check_equals (gl_oset_t set1, gl_list_t set2)
41 size_t n = gl_oset_size (set1);
42 gl_oset_iterator_t iter1;
43 gl_list_iterator_t iter2;
44 const void *elt1;
45 const void *elt2;
46 gl_list_node_t node2;
47 size_t i;
49 iter1 = gl_oset_iterator (set1);
50 iter2 = gl_list_iterator (set2);
51 for (i = 0; i < n; i++)
53 ASSERT (gl_oset_iterator_next (&iter1, &elt1));
54 ASSERT (gl_list_iterator_next (&iter2, &elt2, &node2));
55 ASSERT (elt1 == elt2);
57 ASSERT (!gl_oset_iterator_next (&iter1, &elt1));
58 ASSERT (!gl_list_iterator_next (&iter2, &elt2, &node2));
59 gl_oset_iterator_free (&iter1);
60 gl_list_iterator_free (&iter2);
63 static void
64 check_all (gl_oset_t set1, gl_list_t set2)
66 check_equals (set1, set2);
69 int
70 main (int argc, char *argv[])
72 gl_oset_t set1;
73 gl_list_t set2;
75 /* Allow the user to provide a non-default random seed on the command line. */
76 if (argc > 1)
77 srand (atoi (argv[1]));
80 size_t initial_size = RANDOM (20);
81 size_t i;
82 unsigned int repeat;
84 /* Create set1. */
85 set1 = gl_oset_nx_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL);
86 ASSERT (set1 != NULL);
88 /* Create set2. */
89 set2 = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, false);
91 check_all (set1, set2);
93 /* Initialize them. */
94 for (i = 0; i < initial_size; i++)
96 const char *obj = RANDOM_OBJECT ();
97 ASSERT (gl_oset_nx_add (set1, obj)
98 == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL
99 ? false
100 : (gl_sortedlist_add (set2, (gl_listelement_compar_fn)strcmp, obj), true)));
101 check_all (set1, set2);
104 for (repeat = 0; repeat < 100000; repeat++)
106 unsigned int operation = RANDOM (3);
107 switch (operation)
109 case 0:
111 const char *obj = RANDOM_OBJECT ();
112 ASSERT (gl_oset_search (set1, obj)
113 == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL));
115 break;
116 case 1:
118 const char *obj = RANDOM_OBJECT ();
119 ASSERT (gl_oset_nx_add (set1, obj)
120 == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL
121 ? false
122 : (gl_sortedlist_add (set2, (gl_listelement_compar_fn)strcmp, obj), true)));
124 break;
125 case 2:
127 const char *obj = RANDOM_OBJECT ();
128 ASSERT (gl_oset_remove (set1, obj)
129 == gl_sortedlist_remove (set2, (gl_listelement_compar_fn)strcmp, obj));
131 break;
133 check_all (set1, set2);
136 gl_oset_free (set1);
137 gl_list_free (set2);
140 return 0;