hash: minor optimization
[pspp] / tests / test-hash.c
1 /*
2  * Copyright (C) 2009 Free Software Foundation
3  * Written by Jim Meyering
4  *
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.
9  *
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.
14  *
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/>.  */
17
18 #include <config.h>
19
20 #include "hash.h"
21 #include "hash-pjw.h"
22 #include "inttostr.h"
23 #include "xalloc.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdbool.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #define STREQ(a, b) (strcmp (a, b) == 0)
32 #define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
33
34 #define ASSERT(expr) \
35   do                                                                         \
36     {                                                                        \
37       if (!(expr))                                                           \
38         {                                                                    \
39           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
40           fflush (stderr);                                                   \
41           abort ();                                                          \
42         }                                                                    \
43     }                                                                        \
44   while (0)
45
46 static bool
47 hash_compare_strings (void const *x, void const *y)
48 {
49   ASSERT (x != y);
50   return STREQ (x, y) ? true : false;
51 }
52
53 static void
54 hash_freer (void *x)
55 {
56   free (x);
57 }
58
59 static void
60 insert_new (Hash_table *ht, void *ent)
61 {
62   void *e = hash_insert (ht, ent);
63   ASSERT (e == ent);
64 }
65
66 static bool
67 walk (void *ent, void *data)
68 {
69   char *str = ent;
70   unsigned int *map = data;
71   switch (*str)
72     {
73     case 'a': *map |= 1; return true;
74     case 'b': *map |= 2; return true;
75     case 'c': *map |= 4; return true;
76     }
77   *map |= 8;
78   return false;
79 }
80
81 int
82 main (void)
83 {
84   unsigned int i;
85   unsigned int table_size[] = {1, 2, 3, 4, 5, 23, 53};
86   Hash_table *ht;
87   Hash_tuning tuning;
88
89   for (i = 0; i < ARRAY_CARDINALITY (table_size); i++)
90     {
91       size_t sz = table_size[i];
92       ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL);
93       ASSERT (ht);
94       insert_new (ht, "a");
95       {
96         char *str1 = xstrdup ("a");
97         char *str2 = hash_insert (ht, str1);
98         ASSERT (str1 != str2);
99         ASSERT (STREQ (str1, str2));
100         free (str1);
101       }
102       insert_new (ht, "b");
103       insert_new (ht, "c");
104       i = 0;
105       ASSERT (hash_do_for_each (ht, walk, &i) == 3);
106       ASSERT (i == 7);
107       {
108         void *buf[5] = { NULL };
109         ASSERT (hash_get_entries (ht, NULL, 0) == 0);
110         ASSERT (hash_get_entries (ht, buf, 5) == 3);
111         ASSERT (STREQ (buf[0], "a") || STREQ (buf[0], "b") || STREQ (buf[0], "c"));
112       }
113       ASSERT (hash_delete (ht, "a"));
114       ASSERT (hash_delete (ht, "a") == NULL);
115       ASSERT (hash_delete (ht, "b"));
116       ASSERT (hash_delete (ht, "c"));
117
118       ASSERT (hash_rehash (ht, 47));
119       ASSERT (hash_rehash (ht, 467));
120
121       /* Free an empty table. */
122       hash_clear (ht);
123       hash_free (ht);
124
125       ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL);
126       ASSERT (ht);
127
128       insert_new (ht, "z");
129       insert_new (ht, "y");
130       insert_new (ht, "x");
131       insert_new (ht, "w");
132       insert_new (ht, "v");
133       insert_new (ht, "u");
134
135       hash_clear (ht);
136       ASSERT (hash_get_n_entries (ht) == 0);
137       hash_free (ht);
138     }
139
140   /* Now, each entry is malloc'd.  */
141   ht = hash_initialize (4651, NULL, hash_pjw, hash_compare_strings, hash_freer);
142   ASSERT (ht);
143   for (i = 0; i < 10000; i++)
144     {
145       unsigned int op = rand () % 10;
146       switch (op)
147         {
148         case 0:
149         case 1:
150         case 2:
151         case 3:
152         case 4:
153         case 5:
154           {
155             char buf[50];
156             char const *p = uinttostr (i, buf);
157             insert_new (ht, xstrdup (p));
158           }
159           break;
160
161         case 6:
162           {
163             size_t n = hash_get_n_entries (ht);
164             ASSERT (hash_rehash (ht, n + rand () % 20));
165           }
166           break;
167
168         case 7:
169           {
170             size_t n = hash_get_n_entries (ht);
171             size_t delta = rand () % 20;
172             if (delta < n)
173               ASSERT (hash_rehash (ht, n - delta));
174           }
175           break;
176
177         case 8:
178         case 9:
179           {
180             /* Delete a random entry.  */
181             size_t n = hash_get_n_entries (ht);
182             if (n)
183               {
184                 size_t k = rand () % n;
185                 void const *p;
186                 void *v;
187                 for (p = hash_get_first (ht); k; --k, p = hash_get_next (ht, p))
188                   {
189                     /* empty */
190                   }
191                 ASSERT (p);
192                 v = hash_delete (ht, p);
193                 ASSERT (v);
194                 free (v);
195               }
196             break;
197           }
198         }
199       ASSERT (hash_table_ok (ht));
200     }
201
202   hash_free (ht);
203
204   hash_reset_tuning (&tuning);
205   tuning.shrink_threshold = 0.3;
206   tuning.shrink_factor = 0.707;
207   tuning.growth_threshold = 1.5;
208   tuning.growth_factor = 2.0;
209   tuning.is_n_buckets = true;
210   /* Invalid tuning.  */
211   ht = hash_initialize (4651, &tuning, hash_pjw, hash_compare_strings,
212                         hash_freer);
213   ASSERT (!ht);
214
215   /* Alternate tuning.  */
216   tuning.growth_threshold = 0.89;
217   ht = hash_initialize (4651, &tuning, hash_pjw, hash_compare_strings,
218                         hash_freer);
219   ASSERT (ht);
220   for (i = 0; i < 10000; i++)
221     {
222       unsigned int op = rand () % 10;
223       switch (op)
224         {
225         case 0:
226         case 1:
227         case 2:
228         case 3:
229         case 4:
230         case 5:
231           {
232             char buf[50];
233             char const *p = uinttostr (i, buf);
234             insert_new (ht, xstrdup (p));
235           }
236           break;
237
238         case 6:
239           {
240             size_t n = hash_get_n_entries (ht);
241             ASSERT (hash_rehash (ht, n + rand () % 20));
242           }
243           break;
244
245         case 7:
246           {
247             size_t n = hash_get_n_entries (ht);
248             size_t delta = rand () % 20;
249             if (delta < n)
250               ASSERT (hash_rehash (ht, n - delta));
251           }
252           break;
253
254         case 8:
255         case 9:
256           {
257             /* Delete a random entry.  */
258             size_t n = hash_get_n_entries (ht);
259             if (n)
260               {
261                 size_t k = rand () % n;
262                 void const *p;
263                 void *v;
264                 for (p = hash_get_first (ht); k; --k, p = hash_get_next (ht, p))
265                   {
266                     /* empty */
267                   }
268                 ASSERT (p);
269                 v = hash_delete (ht, p);
270                 ASSERT (v);
271                 free (v);
272               }
273             break;
274           }
275         }
276       ASSERT (hash_table_ok (ht));
277     }
278
279   hash_free (ht);
280
281   return 0;
282 }