Branch
Hash :
7b089321
Author :
Date :
2025-01-01T09:24:36
maint: run 'make update-copyright'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
/* Test of persistent hash array mapped trie implementation.
Copyright (C) 2021-2025 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Marc Nieper-Wißkirchen <marc@nieper-wisskirchen.de>, 2021. */
#include <config.h>
#include "hamt.h"
#include "macros.h"
#include "xalloc.h"
typedef struct
{
Hamt_entry entry;
int val;
} Element;
static int
entry_value (const void *elt)
{
return ((Element *) elt)->val;
}
static size_t
hash_element (const void *elt)
{
return entry_value (elt) & ~3; /* We drop the last bits so that we
can test hash collisions. */
}
static bool
compare_element (const void *elt1, const void *elt2)
{
return entry_value (elt1) == entry_value (elt2);
}
static void
free_element (Hamt_entry *elt)
{
free (elt);
}
static Hamt_entry *
make_element (int n)
{
Element *elt = XMALLOC (Element);
elt->val = n;
return hamt_element (&elt->entry);
}
static Hamt *
test_hamt_create (void)
{
return hamt_create (hash_element, compare_element, free_element);
}
static int sum = 0;
static int flag;
static bool
proc (Hamt_entry *elt, void *data)
{
if (data == &flag)
{
sum += entry_value (elt);
return true;
}
if (sum > 0)
{
sum = 0;
return true;
}
return false;
}
static void
test_general (void)
{
Hamt *hamt = test_hamt_create ();
Hamt_entry *x5 = make_element (5);
Hamt_entry *p = x5;
Hamt *hamt1 = hamt_insert (hamt, &p);
ASSERT (hamt1 != hamt);
ASSERT (hamt_lookup (hamt, x5) == NULL);
ASSERT (hamt_lookup (hamt1, x5) == x5);
hamt_free (hamt);
Hamt_entry *y5 = make_element (5);
p = y5;
Hamt *hamt2 = hamt_insert (hamt1, &p);
ASSERT (hamt2 == hamt1);
ASSERT (p == x5);
ASSERT (hamt_lookup (hamt1, y5) == x5);
p = y5;
hamt = hamt_replace (hamt1, &p);
ASSERT (p == x5);
ASSERT (hamt_lookup (hamt, y5) == y5);
hamt_free (hamt);
y5 = make_element (5);
Hamt_entry *z37 = make_element (37);
p = z37;
hamt2 = hamt_insert (hamt1, &p);
ASSERT (hamt2 != hamt1);
ASSERT (p == z37);
ASSERT (hamt_lookup (hamt1, z37) == NULL);
ASSERT (hamt_lookup (hamt2, z37) == z37);
hamt_free (hamt1);
ASSERT (hamt_lookup (hamt2, x5) == x5);
ASSERT (hamt_lookup (hamt2, z37) == z37);
ASSERT (hamt_do_while (hamt2, proc, &flag) == 2);
ASSERT (sum == 42);
ASSERT (hamt_do_while (hamt2, proc, NULL) == 1);
ASSERT (sum == 0);
p = y5;
hamt1 = hamt_remove (hamt2, &p);
ASSERT (hamt1 != hamt2);
ASSERT (p == x5);
ASSERT (hamt_lookup (hamt1, x5) == NULL);
ASSERT (hamt_lookup (hamt2, x5) == x5);
hamt_free (hamt1);
Hamt_entry *x4 = make_element (4);
hamt1 = hamt_insert (hamt2, &x4);
hamt_free (hamt2);
Hamt_entry *x6 = make_element (6);
hamt2 = hamt_insert (hamt1, &x6);
hamt_free (hamt1);
ASSERT (hamt_do_while (hamt2, proc, &flag) == 4);
ASSERT (sum == 52);
hamt1 = hamt_remove (hamt2, &x4);
sum = 0;
ASSERT (hamt_do_while (hamt2, proc, &flag) == 4);
ASSERT (sum == 52);
sum = 0;
ASSERT (hamt_do_while (hamt1, proc, &flag) == 3);
ASSERT (sum == 48);
hamt_free (hamt1);
hamt_free (hamt2);
free_element (y5);
}
static bool
true_processor (_GL_ATTRIBUTE_MAYBE_UNUSED Hamt_entry *elt,
_GL_ATTRIBUTE_MAYBE_UNUSED void *data)
{
return true;
}
static size_t
element_count (Hamt *hamt)
{
return hamt_do_while (hamt, true_processor, NULL);
}
struct find_values_context
{
size_t n;
int *elts;
bool *found;
};
static bool
find_values_processor (Hamt_entry *entry, void *data)
{
struct find_values_context *ctx = data;
int val = entry_value (entry);
for (size_t i = 0; i < ctx->n; ++i)
if (ctx->elts [i] == val && !ctx->found [i])
{
ctx->found [i] = true;
return true;
}
return false;
}
static bool
find_values (Hamt *hamt, size_t n, int *elts)
{
bool *found = XCALLOC (n, bool);
struct find_values_context ctx = {n, elts, found};
bool res = hamt_do_while (hamt, find_values_processor, &ctx) == n;
free (found);
return res;
}
static size_t
insert_values (Hamt **hamt, size_t n, int *elts, bool destructive)
{
size_t cnt = 0;
for (size_t i = 0; i < n; ++i)
{
Hamt_entry *p = make_element (elts [i]);
Hamt_entry *q = p;
if (destructive)
{
if (hamt_insert_x (*hamt, &p))
++cnt;
else
free_element (q);
}
else
{
Hamt *new_hamt = hamt_insert (*hamt, &p);
if (new_hamt != *hamt)
{
hamt_free (*hamt);
*hamt = new_hamt;
++cnt;
}
else
{
free_element (q);
}
}
}
return cnt;
}
static size_t
replace_values (Hamt **hamt, size_t n, int *elts, bool destructive)
{
size_t cnt = 0;
for (size_t i = 0; i < n; ++i)
{
Hamt_entry *p = make_element (elts [i]);
if (destructive)
{
if (hamt_replace_x (*hamt, p))
++cnt;
}
else
{
Hamt *new_hamt = hamt_replace (*hamt, &p);
hamt_free (*hamt);
*hamt = new_hamt;
if (p != NULL)
++cnt;
}
}
return cnt;
}
static size_t
remove_values (Hamt **hamt, size_t n, int *elts, bool destructive)
{
size_t cnt = 0;
for (size_t i = 0; i < n; ++i)
{
Hamt_entry *p = make_element (elts [i]);
Hamt_entry *q = p;
if (destructive)
{
if (hamt_remove_x (*hamt, p))
++cnt;
}
else
{
Hamt *new_hamt = hamt_remove (*hamt, &p);
if (new_hamt != *hamt)
{
hamt_free (*hamt);
*hamt = new_hamt;
++cnt;
}
}
free (q);
}
return cnt;
}
static int val_array1 [10] = {1, 2, 3, 4, 33, 34, 35, 36, 1024, 1025};
static int val_array2 [10] = {1, 2, 34, 36, 1025, 32768, 32769, 32770, 32771,
32772};
static void
test_functional_update (void)
{
Hamt *hamt = test_hamt_create ();
ASSERT (insert_values (&hamt, 10, val_array1, false) == 10);
ASSERT (element_count (hamt) == 10);
ASSERT (find_values (hamt, 10, val_array1));
ASSERT (insert_values (&hamt, 10, val_array2, false) == 5);
ASSERT (element_count (hamt) == 15);
ASSERT (remove_values (&hamt, 10, val_array1, false) == 10);
ASSERT (element_count (hamt) == 5);
ASSERT (remove_values (&hamt, 10, val_array2, false) == 5);
ASSERT (element_count (hamt) == 0);
ASSERT (replace_values (&hamt, 10, val_array1, false) == 0);
ASSERT (element_count (hamt) == 10);
ASSERT (find_values (hamt, 10, val_array1));
ASSERT (replace_values (&hamt, 10, val_array2, false) == 5);
ASSERT (element_count (hamt) == 15);
hamt_free (hamt);
}
static void
test_destructive_update (void)
{
Hamt *hamt = test_hamt_create ();
ASSERT (insert_values (&hamt, 10, val_array1, true) == 10);
ASSERT (element_count (hamt) == 10);
ASSERT (find_values (hamt, 10, val_array1));
ASSERT (insert_values (&hamt, 10, val_array2, true) == 5);
ASSERT (element_count (hamt) == 15);
ASSERT (remove_values (&hamt, 10, val_array1, true) == 10);
ASSERT (element_count (hamt) == 5);
ASSERT (remove_values (&hamt, 10, val_array2, true) == 5);
ASSERT (element_count (hamt) == 0);
ASSERT (replace_values (&hamt, 10, val_array1, true) == 0);
ASSERT (element_count (hamt) == 10);
ASSERT (find_values (hamt, 10, val_array1));
ASSERT (replace_values (&hamt, 10, val_array2, true) == 5);
ASSERT (element_count (hamt) == 15);
hamt_free (hamt);
}
static void
test_iterator (void)
{
Hamt *hamt = test_hamt_create ();
ASSERT (insert_values (&hamt, 10, val_array1, true) == 10);
Hamt_iterator iter[1];
iter[0] = hamt_iterator (hamt);
size_t cnt = 0;
bool found [10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Hamt_entry *p;
while (hamt_iterator_next (iter, &p))
{
for (size_t i = 0; i < 10; ++i)
if (val_array1 [i] == entry_value (p))
{
ASSERT (!found [i]);
found [i] = true;
++cnt;
break;
}
}
ASSERT (cnt == 10);
hamt_iterator_free (iter);
hamt_free (hamt);
}
int
main (void)
{
test_general ();
test_functional_update ();
test_destructive_update ();
test_iterator ();
return test_exit_status;
}