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>, 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/>. */
20 #include "gl_avltree_oset.h"
25 #include "gl_array_oset.h"
28 #include "test-oset-update.h"
30 extern void gl_avltree_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))]
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
;
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
);
65 check_all (gl_oset_t set1
, gl_oset_t set2
)
67 gl_avltree_oset_check_invariants (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 main (int argc
, char *argv
[])
82 /* Allow the user to provide a non-default random seed on the command line. */
84 srand (atoi (argv
[1]));
87 size_t initial_size
= RANDOM (20);
92 set1
= gl_oset_nx_create_empty (GL_ARRAY_OSET
, (gl_setelement_compar_fn
) strcmp
, NULL
);
93 ASSERT (set1
!= NULL
);
96 set2
= gl_oset_nx_create_empty (GL_AVLTREE_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);
116 const char *obj
= RANDOM_OBJECT ();
117 ASSERT (gl_oset_search (set1
, obj
) == gl_oset_search (set2
, obj
));
122 const char *obj
= RANDOM_OBJECT ();
123 ASSERT (gl_oset_nx_add (set1
, obj
) == gl_oset_nx_add (set2
, obj
));
128 const char *obj
= RANDOM_OBJECT ();
129 ASSERT (gl_oset_remove (set1
, obj
) == gl_oset_remove (set2
, obj
));
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
);
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
);
147 ASSERT (elt1
== elt2
);
148 havenext1
= gl_oset_iterator_next (&iter1
, &elt1
);
149 havenext2
= gl_oset_iterator_next (&iter2
, &elt2
);
150 ASSERT (havenext1
== havenext2
);
152 ASSERT (elt1
== elt2
);
154 gl_oset_iterator_free (&iter1
);
155 gl_oset_iterator_free (&iter2
);
159 check_all (set1
, set2
);
166 test_update (GL_AVLTREE_OSET
);