1 /* Test of ordered set data type implementation.
2 Copyright (C) 2006-2009 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 <http://www.gnu.org/licenses/>. */
20 #include "gl_array_oset.h"
27 #include "gl_array_list.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 SIZEOF(array) (sizeof (array) / sizeof (array[0]))
37 #define ASSERT(expr) \
42 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
48 #define RANDOM(n) (rand () % (n))
49 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
52 check_equals (gl_oset_t set1, gl_list_t set2)
54 size_t n = gl_oset_size (set1);
55 gl_oset_iterator_t iter1;
56 gl_list_iterator_t iter2;
62 iter1 = gl_oset_iterator (set1);
63 iter2 = gl_list_iterator (set2);
64 for (i = 0; i < n; i++)
66 ASSERT (gl_oset_iterator_next (&iter1, &elt1));
67 ASSERT (gl_list_iterator_next (&iter2, &elt2, &node2));
68 ASSERT (elt1 == elt2);
70 ASSERT (!gl_oset_iterator_next (&iter1, &elt1));
71 ASSERT (!gl_list_iterator_next (&iter2, &elt2, &node2));
72 gl_oset_iterator_free (&iter1);
73 gl_list_iterator_free (&iter2);
77 check_all (gl_oset_t set1, gl_list_t set2)
79 check_equals (set1, set2);
83 main (int argc, char *argv[])
88 set_program_name (argv[0]);
90 /* Allow the user to provide a non-default random seed on the command line. */
92 srand (atoi (argv[1]));
95 size_t initial_size = RANDOM (20);
100 set1 = gl_oset_nx_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL);
101 ASSERT (set1 != NULL);
104 set2 = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, false);
106 check_all (set1, set2);
108 /* Initialize them. */
109 for (i = 0; i < initial_size; i++)
111 const char *obj = RANDOM_OBJECT ();
112 ASSERT (gl_oset_nx_add (set1, obj)
113 == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL
115 : (gl_sortedlist_add (set2, (gl_listelement_compar_fn)strcmp, obj), true)));
116 check_all (set1, set2);
119 for (repeat = 0; repeat < 100000; repeat++)
121 unsigned int operation = RANDOM (3);
126 const char *obj = RANDOM_OBJECT ();
127 ASSERT (gl_oset_search (set1, obj)
128 == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL));
133 const char *obj = RANDOM_OBJECT ();
134 ASSERT (gl_oset_nx_add (set1, obj)
135 == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL
137 : (gl_sortedlist_add (set2, (gl_listelement_compar_fn)strcmp, obj), true)));
142 const char *obj = RANDOM_OBJECT ();
143 ASSERT (gl_oset_remove (set1, obj)
144 == gl_sortedlist_remove (set2, (gl_listelement_compar_fn)strcmp, obj));
148 check_all (set1, set2);