linkedhash-set: Add tests.
[gnulib.git] / tests / test-linkedhash_set.c
blob7ff92e8aaf1ffe1d53c29418d5f51a9a60738452
1 /* Test of set data type implementation.
2 Copyright (C) 2006-2018 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2018.
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/>. */
18 #include <config.h>
20 #include "gl_linkedhash_set.h"
22 #include <stdlib.h>
23 #include <string.h>
25 #include "gl_array_set.h"
26 #include "xalloc.h"
27 #include "macros.h"
29 static const char *objects[30] =
31 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
32 "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "<", ">", "[", "]"
35 #define RANDOM(n) (rand () % (n))
36 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
38 static void
39 check_equals (gl_set_t set1, gl_set_t set2)
41 size_t n = gl_set_size (set1);
42 const void **elements_of_set1 = XNMALLOC (n, const void *);
43 const void **elements_of_set2 = XNMALLOC (n, const void *);
45 gl_set_iterator_t iter1;
46 gl_set_iterator_t iter2;
47 const void *elt1;
48 const void *elt2;
49 size_t i;
51 iter1 = gl_set_iterator (set1);
52 iter2 = gl_set_iterator (set2);
53 for (i = 0; i < n; i++)
55 ASSERT (gl_set_iterator_next (&iter1, &elt1));
56 ASSERT (gl_set_iterator_next (&iter2, &elt2));
57 elements_of_set1[i] = elt1;
58 elements_of_set2[i] = elt2;
60 ASSERT (!gl_set_iterator_next (&iter1, &elt1));
61 ASSERT (!gl_set_iterator_next (&iter2, &elt2));
62 gl_set_iterator_free (&iter1);
63 gl_set_iterator_free (&iter2);
65 if (n > 0)
67 qsort (elements_of_set1, n, sizeof (const void *),
68 (int (*) (const void *, const void *)) strcmp);
69 qsort (elements_of_set2, n, sizeof (const void *),
70 (int (*) (const void *, const void *)) strcmp);
72 for (i = 0; i < n; i++)
73 ASSERT (elements_of_set1[i] == elements_of_set2[i]);
74 free (elements_of_set2);
75 free (elements_of_set1);
78 static void
79 check_all (gl_set_t set1, gl_set_t set2)
81 check_equals (set1, set2);
84 static bool
85 string_equals (const void *elt1, const void *elt2)
87 return strcmp ((const char *) elt1, (const char *) elt2) == 0;
90 static size_t
91 string_hashcode (const void *elt)
93 size_t hashcode = 0;
94 const char *s;
95 for (s = (const char *) elt; *s != '\0'; s++)
96 hashcode += (unsigned char) *s;
97 return hashcode;
101 main (int argc, char *argv[])
103 gl_set_t set1, set2;
105 /* Allow the user to provide a non-default random seed on the command line. */
106 if (argc > 1)
107 srand (atoi (argv[1]));
110 size_t initial_size = RANDOM (20);
111 size_t i;
112 unsigned int repeat;
114 /* Create set1. */
115 set1 = gl_set_nx_create_empty (GL_ARRAY_SET, string_equals, string_hashcode, NULL);
116 ASSERT (set1 != NULL);
118 /* Create set2. */
119 set2 = gl_set_nx_create_empty (GL_LINKEDHASH_SET, string_equals, string_hashcode, NULL);
120 ASSERT (set2 != NULL);
122 check_all (set1, set2);
124 /* Initialize them. */
125 for (i = 0; i < initial_size; i++)
127 const char *obj = RANDOM_OBJECT ();
128 ASSERT (gl_set_nx_add (set1, obj) == gl_set_nx_add (set2, obj));
129 check_all (set1, set2);
132 for (repeat = 0; repeat < 100000; repeat++)
134 unsigned int operation = RANDOM (3);
135 switch (operation)
137 case 0:
139 const char *obj = RANDOM_OBJECT ();
140 ASSERT (gl_set_search (set1, obj) == gl_set_search (set2, obj));
142 break;
143 case 1:
145 const char *obj = RANDOM_OBJECT ();
146 ASSERT (gl_set_nx_add (set1, obj) == gl_set_nx_add (set2, obj));
148 break;
149 case 2:
151 const char *obj = RANDOM_OBJECT ();
152 ASSERT (gl_set_remove (set1, obj) == gl_set_remove (set2, obj));
154 break;
156 check_all (set1, set2);
159 gl_set_free (set1);
160 gl_set_free (set2);
163 return 0;