1 /* Test of ordered set data type implementation.
2 Copyright (C) 2006-2007 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 2, or (at your option)
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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "gl_avltree_oset.h"
29 #include "gl_array_oset.h"
32 extern void gl_avltree_oset_check_invariants (gl_oset_t set);
34 static const char *objects[30] =
36 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
37 "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "<", ">", "[", "]"
40 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
41 #define ASSERT(expr) \
46 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
51 #define RANDOM(n) (rand () % (n))
52 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
55 check_equals (gl_oset_t set1, gl_oset_t set2)
57 size_t n = gl_oset_size (set1);
58 gl_oset_iterator_t iter1, iter2;
63 iter1 = gl_oset_iterator (set1);
64 iter2 = gl_oset_iterator (set2);
65 for (i = 0; i < n; i++)
67 ASSERT (gl_oset_iterator_next (&iter1, &elt1));
68 ASSERT (gl_oset_iterator_next (&iter2, &elt2));
69 ASSERT (elt1 == elt2);
71 ASSERT (!gl_oset_iterator_next (&iter1, &elt1));
72 ASSERT (!gl_oset_iterator_next (&iter2, &elt2));
73 gl_oset_iterator_free (&iter1);
74 gl_oset_iterator_free (&iter2);
78 check_all (gl_oset_t set1, gl_oset_t set2)
80 gl_avltree_oset_check_invariants (set2);
81 check_equals (set1, set2);
85 main (int argc, char *argv[])
89 /* Allow the user to provide a non-default random seed on the command line. */
91 srand (atoi (argv[1]));
94 size_t initial_size = RANDOM (20);
99 set1 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL);
102 set2 = gl_oset_create_empty (GL_AVLTREE_OSET, (gl_setelement_compar_fn) strcmp, NULL);
104 check_all (set1, set2);
106 /* Initialize them. */
107 for (i = 0; i < initial_size; i++)
109 const char *obj = RANDOM_OBJECT ();
110 ASSERT (gl_oset_add (set1, obj) == gl_oset_add (set2, obj));
111 check_all (set1, set2);
114 for (repeat = 0; repeat < 100000; repeat++)
116 unsigned int operation = RANDOM (3);
121 const char *obj = RANDOM_OBJECT ();
122 ASSERT (gl_oset_search (set1, obj) == gl_oset_search (set2, obj));
127 const char *obj = RANDOM_OBJECT ();
128 ASSERT (gl_oset_add (set1, obj) == gl_oset_add (set2, obj));
133 const char *obj = RANDOM_OBJECT ();
134 ASSERT (gl_oset_remove (set1, obj) == gl_oset_remove (set2, obj));
138 check_all (set1, set2);