1 /* Test of ordered set data type implementation.
2 Copyright (C) 2006-2020 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/>. */
20 #include "gl_array_oset.h"
26 #include "gl_array_list.h"
29 #include "test-oset-update.h"
31 static const char *objects
[30] =
33 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
34 "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "<", ">", "[", "]"
37 #define RANDOM(n) (rand () % (n))
38 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
41 check_equals (gl_oset_t set1
, gl_list_t set2
)
43 size_t n
= gl_oset_size (set1
);
44 gl_oset_iterator_t iter1
;
45 gl_list_iterator_t iter2
;
51 iter1
= gl_oset_iterator (set1
);
52 iter2
= gl_list_iterator (set2
);
53 for (i
= 0; i
< n
; i
++)
55 ASSERT (gl_oset_iterator_next (&iter1
, &elt1
));
56 ASSERT (gl_list_iterator_next (&iter2
, &elt2
, &node2
));
57 ASSERT (elt1
== elt2
);
59 ASSERT (!gl_oset_iterator_next (&iter1
, &elt1
));
60 ASSERT (!gl_list_iterator_next (&iter2
, &elt2
, &node2
));
61 gl_oset_iterator_free (&iter1
);
62 gl_list_iterator_free (&iter2
);
66 check_all (gl_oset_t set1
, gl_list_t set2
)
68 check_equals (set1
, set2
);
72 is_at_least (const void *elt
, const void *threshold
)
74 return strcmp ((const char *) elt
, (const char *) threshold
) >= 0;
78 gl_sortedlist_indexof_atleast (gl_list_t set
,
79 gl_setelement_threshold_fn threshold_fn
,
80 const void *threshold
)
82 /* This implementation is slow, but easy to verify. */
83 size_t count
= gl_list_size (set
);
86 for (index
= 0; index
< count
; index
++)
87 if (threshold_fn (gl_list_get_at (set
, index
), threshold
))
93 main (int argc
, char *argv
[])
98 /* Allow the user to provide a non-default random seed on the command line. */
100 srand (atoi (argv
[1]));
103 size_t initial_size
= RANDOM (20);
108 set1
= gl_oset_nx_create_empty (GL_ARRAY_OSET
, (gl_setelement_compar_fn
) strcmp
, NULL
);
109 ASSERT (set1
!= NULL
);
112 set2
= gl_list_create_empty (GL_ARRAY_LIST
, NULL
, NULL
, NULL
, false);
114 check_all (set1
, set2
);
116 /* Initialize them. */
117 for (i
= 0; i
< initial_size
; i
++)
119 const char *obj
= RANDOM_OBJECT ();
120 ASSERT (gl_oset_nx_add (set1
, obj
)
121 == (gl_sortedlist_search (set2
, (gl_listelement_compar_fn
)strcmp
, obj
) != NULL
123 : (gl_sortedlist_add (set2
, (gl_listelement_compar_fn
)strcmp
, obj
), true)));
124 check_all (set1
, set2
);
127 for (repeat
= 0; repeat
< 100000; repeat
++)
129 unsigned int operation
= RANDOM (4);
134 const char *obj
= RANDOM_OBJECT ();
135 ASSERT (gl_oset_search (set1
, obj
)
136 == (gl_sortedlist_search (set2
, (gl_listelement_compar_fn
)strcmp
, obj
) != NULL
));
141 const char *obj
= RANDOM_OBJECT ();
142 ASSERT (gl_oset_nx_add (set1
, obj
)
143 == (gl_sortedlist_search (set2
, (gl_listelement_compar_fn
)strcmp
, obj
) != NULL
145 : (gl_sortedlist_add (set2
, (gl_listelement_compar_fn
)strcmp
, obj
), true)));
150 const char *obj
= RANDOM_OBJECT ();
151 ASSERT (gl_oset_remove (set1
, obj
)
152 == gl_sortedlist_remove (set2
, (gl_listelement_compar_fn
)strcmp
, obj
));
157 const char *obj
= RANDOM_OBJECT ();
158 gl_oset_iterator_t iter
= gl_oset_iterator_atleast (set1
, is_at_least
, obj
);
159 size_t index
= gl_sortedlist_indexof_atleast (set2
, is_at_least
, obj
);
161 /* Check the first two values that the iterator produces.
162 Checking them all would make this part of the test dominate the
163 run time of the test. */
164 if (index
== (size_t)(-1))
165 ASSERT (!gl_oset_iterator_next (&iter
, &elt
));
168 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
169 ASSERT (elt
== gl_list_get_at (set2
, index
));
170 if (index
+ 1 == gl_list_size (set2
))
171 ASSERT (!gl_oset_iterator_next (&iter
, &elt
));
174 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
175 ASSERT (elt
== gl_list_get_at (set2
, index
+ 1));
178 gl_oset_iterator_free (&iter
);
182 check_all (set1
, set2
);
189 test_update (GL_ARRAY_OSET
);