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>, 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 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_array_oset.h"
28 #include "gl_array_list.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 SIZEOF(array) (sizeof (array) / sizeof (array[0]))
38 #define ASSERT(condition) if (!(condition)) abort ()
39 #define RANDOM(n) (rand () % (n))
40 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
43 check_equals (gl_oset_t set1, gl_list_t set2)
45 size_t n = gl_oset_size (set1);
46 gl_oset_iterator_t iter1;
47 gl_list_iterator_t iter2;
53 iter1 = gl_oset_iterator (set1);
54 iter2 = gl_list_iterator (set2);
55 for (i = 0; i < n; i++)
57 ASSERT (gl_oset_iterator_next (&iter1, &elt1));
58 ASSERT (gl_list_iterator_next (&iter2, &elt2, &node2));
59 ASSERT (elt1 == elt2);
61 ASSERT (!gl_oset_iterator_next (&iter1, &elt1));
62 ASSERT (!gl_list_iterator_next (&iter2, &elt2, &node2));
63 gl_oset_iterator_free (&iter1);
64 gl_list_iterator_free (&iter2);
68 check_all (gl_oset_t set1, gl_list_t set2)
70 check_equals (set1, set2);
74 main (int argc, char *argv[])
79 set_program_name (argv[0]);
81 /* Allow the user to provide a non-default random seed on the command line. */
83 srand (atoi (argv[1]));
86 size_t initial_size = RANDOM (20);
91 set1 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL);
94 set2 = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, false);
96 check_all (set1, set2);
98 /* Initialize them. */
99 for (i = 0; i < initial_size; i++)
101 const char *obj = RANDOM_OBJECT ();
102 ASSERT (gl_oset_add (set1, obj)
103 == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL
105 : (gl_sortedlist_add (set2, (gl_listelement_compar_fn)strcmp, obj), true)));
106 check_all (set1, set2);
109 for (repeat = 0; repeat < 100000; repeat++)
111 unsigned int operation = RANDOM (3);
116 const char *obj = RANDOM_OBJECT ();
117 ASSERT (gl_oset_search (set1, obj)
118 == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL));
123 const char *obj = RANDOM_OBJECT ();
124 ASSERT (gl_oset_add (set1, obj)
125 == (gl_sortedlist_search (set2, (gl_listelement_compar_fn)strcmp, obj) != NULL
127 : (gl_sortedlist_add (set2, (gl_listelement_compar_fn)strcmp, obj), true)));
132 const char *obj = RANDOM_OBJECT ();
133 ASSERT (gl_oset_remove (set1, obj)
134 == gl_sortedlist_remove (set2, (gl_listelement_compar_fn)strcmp, obj));
138 check_all (set1, set2);