virhashtest.c 15.8 KB
Newer Older
1 2 3 4 5
#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
6
#include <time.h>
7 8

#include "internal.h"
9 10
#include "virhash.h"
#include "virhashdata.h"
11
#include "testutils.h"
12
#include "viralloc.h"
13
#include "virlog.h"
14
#include "virstring.h"
15

16 17
#define VIR_FROM_THIS VIR_FROM_NONE

18 19
VIR_LOG_INIT("tests.hashtest");

20 21 22 23
static virHashTablePtr
testHashInit(int size)
{
    virHashTablePtr hash;
24
    ssize_t i;
25 26 27 28 29 30 31 32

    if (!(hash = virHashCreate(size, NULL)))
        return NULL;

    /* entires are added in reverse order so that they will be linked in
     * collision list in the same order as in the uuids array
     */
    for (i = ARRAY_CARDINALITY(uuids) - 1; i >= 0; i--) {
33
        ssize_t oldsize = virHashTableSize(hash);
34 35 36 37
        if (virHashAddEntry(hash, uuids[i], (void *) uuids[i]) < 0) {
            virHashFree(hash);
            return NULL;
        }
38

39 40
        if (virHashTableSize(hash) != oldsize) {
            VIR_TEST_DEBUG("hash grown from %zd to %zd",
41
                     (size_t)oldsize, (size_t)virHashTableSize(hash));
42 43 44 45 46
        }
    }

    for (i = 0; i < ARRAY_CARDINALITY(uuids); i++) {
        if (!virHashLookup(hash, uuids[i])) {
47
            VIR_TEST_VERBOSE("\nentry \"%s\" could not be found\n", uuids[i]);
48 49 50
            virHashFree(hash);
            return NULL;
        }
51 52
    }

53 54
    if (size && size != virHashTableSize(hash))
        VIR_TEST_DEBUG("\n");
55

56 57 58
    return hash;
}

59
static int
E
Eric Blake 已提交
60 61 62 63
testHashCheckForEachCount(void *payload ATTRIBUTE_UNUSED,
                          const void *name ATTRIBUTE_UNUSED,
                          void *data ATTRIBUTE_UNUSED)
{
64 65
    size_t *count = data;
    *count += 1;
66
    return 0;
E
Eric Blake 已提交
67
}
68 69

static int
70
testHashCheckCount(virHashTablePtr hash, size_t count)
71
{
72
    size_t iter_count = 0;
E
Eric Blake 已提交
73

74
    if (virHashSize(hash) != count) {
75 76
        VIR_TEST_VERBOSE("\nhash contains %zd instead of %zu elements\n",
                         virHashSize(hash), count);
77 78 79
        return -1;
    }

80
    virHashForEach(hash, testHashCheckForEachCount, &iter_count);
E
Eric Blake 已提交
81
    if (count != iter_count) {
82 83
        VIR_TEST_VERBOSE("\nhash claims to have %zu elements but iteration"
                         "finds %zu\n", count, iter_count);
E
Eric Blake 已提交
84 85 86
        return -1;
    }

87 88 89 90 91 92
    return 0;
}


struct testInfo {
    void *data;
93
    size_t count;
94 95 96
};


97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
static int
testHashGrow(const void *data)
{
    const struct testInfo *info = data;
    virHashTablePtr hash;
    int ret = -1;

    if (!(hash = testHashInit(info->count)))
        return -1;

    if (testHashCheckCount(hash, ARRAY_CARDINALITY(uuids)) < 0)
        goto cleanup;

    ret = 0;

112
 cleanup:
113 114 115 116 117 118 119 120 121 122
    virHashFree(hash);
    return ret;
}


static int
testHashUpdate(const void *data ATTRIBUTE_UNUSED)
{
    int count = ARRAY_CARDINALITY(uuids) + ARRAY_CARDINALITY(uuids_new);
    virHashTablePtr hash;
123
    size_t i;
124 125 126 127 128 129 130
    int ret = -1;

    if (!(hash = testHashInit(0)))
        return -1;

    for (i = 0; i < ARRAY_CARDINALITY(uuids_subset); i++) {
        if (virHashUpdateEntry(hash, uuids_subset[i], (void *) 1) < 0) {
131 132
            VIR_TEST_VERBOSE("\nentry \"%s\" could not be updated\n",
                    uuids_subset[i]);
133 134 135 136 137 138
            goto cleanup;
        }
    }

    for (i = 0; i < ARRAY_CARDINALITY(uuids_new); i++) {
        if (virHashUpdateEntry(hash, uuids_new[i], (void *) 1) < 0) {
139 140
            VIR_TEST_VERBOSE("\nnew entry \"%s\" could not be updated\n",
                    uuids_new[i]);
141 142 143 144 145 146 147 148 149
            goto cleanup;
        }
    }

    if (testHashCheckCount(hash, count) < 0)
        goto cleanup;

    ret = 0;

150
 cleanup:
151 152 153 154 155 156 157 158 159 160
    virHashFree(hash);
    return ret;
}


static int
testHashRemove(const void *data ATTRIBUTE_UNUSED)
{
    int count = ARRAY_CARDINALITY(uuids) - ARRAY_CARDINALITY(uuids_subset);
    virHashTablePtr hash;
161
    size_t i;
162 163 164 165 166 167 168
    int ret = -1;

    if (!(hash = testHashInit(0)))
        return -1;

    for (i = 0; i < ARRAY_CARDINALITY(uuids_subset); i++) {
        if (virHashRemoveEntry(hash, uuids_subset[i]) < 0) {
169 170
            VIR_TEST_VERBOSE("\nentry \"%s\" could not be removed\n",
                    uuids_subset[i]);
171 172 173 174 175 176 177 178 179
            goto cleanup;
        }
    }

    if (testHashCheckCount(hash, count) < 0)
        goto cleanup;

    ret = 0;

180
 cleanup:
181 182 183 184 185
    virHashFree(hash);
    return ret;
}


186 187 188
const int testHashCountRemoveForEachSome =
    ARRAY_CARDINALITY(uuids) - ARRAY_CARDINALITY(uuids_subset);

189
static int
190 191 192 193 194
testHashRemoveForEachSome(void *payload ATTRIBUTE_UNUSED,
                          const void *name,
                          void *data)
{
    virHashTablePtr hash = data;
195
    size_t i;
196 197 198

    for (i = 0; i < ARRAY_CARDINALITY(uuids_subset); i++) {
        if (STREQ(uuids_subset[i], name)) {
199 200
            if (virHashRemoveEntry(hash, name) < 0) {
                VIR_TEST_VERBOSE("\nentry \"%s\" could not be removed",
201 202 203 204 205
                        uuids_subset[i]);
            }
            break;
        }
    }
206
    return 0;
207 208 209 210 211
}


const int testHashCountRemoveForEachAll = 0;

212
static int
213 214 215 216 217 218 219
testHashRemoveForEachAll(void *payload ATTRIBUTE_UNUSED,
                         const void *name,
                         void *data)
{
    virHashTablePtr hash = data;

    virHashRemoveEntry(hash, name);
220
    return 0;
221 222 223
}


224 225
const int testHashCountRemoveForEachForbidden = ARRAY_CARDINALITY(uuids);

226
static int
227 228 229 230 231
testHashRemoveForEachForbidden(void *payload ATTRIBUTE_UNUSED,
                               const void *name,
                               void *data)
{
    virHashTablePtr hash = data;
232
    size_t i;
233 234 235 236 237

    for (i = 0; i < ARRAY_CARDINALITY(uuids_subset); i++) {
        if (STREQ(uuids_subset[i], name)) {
            int next = (i + 1) % ARRAY_CARDINALITY(uuids_subset);

238 239
            if (virHashRemoveEntry(hash, uuids_subset[next]) == 0) {
                VIR_TEST_VERBOSE(
240 241 242 243 244 245
                        "\nentry \"%s\" should not be allowed to be removed",
                        uuids_subset[next]);
            }
            break;
        }
    }
246
    return 0;
247 248 249
}


250 251 252 253 254 255 256 257 258 259
static int
testHashRemoveForEach(const void *data)
{
    const struct testInfo *info = data;
    virHashTablePtr hash;
    int ret = -1;

    if (!(hash = testHashInit(0)))
        return -1;

260 261
    if (virHashForEach(hash, (virHashIterator) info->data, hash)) {
        VIR_TEST_VERBOSE("\nvirHashForEach didn't go through all entries");
262 263 264 265 266 267 268 269
        goto cleanup;
    }

    if (testHashCheckCount(hash, info->count) < 0)
        goto cleanup;

    ret = 0;

270
 cleanup:
271 272 273 274 275
    virHashFree(hash);
    return ret;
}


276 277 278 279 280
static int
testHashSteal(const void *data ATTRIBUTE_UNUSED)
{
    int count = ARRAY_CARDINALITY(uuids) - ARRAY_CARDINALITY(uuids_subset);
    virHashTablePtr hash;
281
    size_t i;
282 283 284 285 286 287 288
    int ret = -1;

    if (!(hash = testHashInit(0)))
        return -1;

    for (i = 0; i < ARRAY_CARDINALITY(uuids_subset); i++) {
        if (!virHashSteal(hash, uuids_subset[i])) {
289 290
            VIR_TEST_VERBOSE("\nentry \"%s\" could not be stolen\n",
                    uuids_subset[i]);
291 292 293 294 295 296 297 298 299
            goto cleanup;
        }
    }

    if (testHashCheckCount(hash, count) < 0)
        goto cleanup;

    ret = 0;

300
 cleanup:
301 302 303 304 305
    virHashFree(hash);
    return ret;
}


306
static int
307 308 309 310
testHashIter(void *payload ATTRIBUTE_UNUSED,
             const void *name ATTRIBUTE_UNUSED,
             void *data ATTRIBUTE_UNUSED)
{
311
    return 0;
312 313
}

314
static int
315 316 317 318 319 320
testHashForEachIter(void *payload ATTRIBUTE_UNUSED,
                    const void *name ATTRIBUTE_UNUSED,
                    void *data)
{
    virHashTablePtr hash = data;

321 322
    if (virHashAddEntry(hash, uuids_new[0], NULL) == 0)
        VIR_TEST_VERBOSE("\nadding entries in ForEach should be forbidden");
323

324 325
    if (virHashUpdateEntry(hash, uuids_new[0], NULL) == 0)
        VIR_TEST_VERBOSE("\nupdating entries in ForEach should be forbidden");
326

327 328
    if (virHashSteal(hash, uuids_new[0]) != NULL)
        VIR_TEST_VERBOSE("\nstealing entries in ForEach should be forbidden");
329

330 331
    if (virHashSteal(hash, uuids_new[0]) != NULL)
        VIR_TEST_VERBOSE("\nstealing entries in ForEach should be forbidden");
332

333 334
    if (virHashForEach(hash, testHashIter, NULL) >= 0)
        VIR_TEST_VERBOSE("\niterating through hash in ForEach"
335
                " should be forbidden");
336
    return 0;
337 338 339 340 341 342 343 344 345 346 347
}

static int
testHashForEach(const void *data ATTRIBUTE_UNUSED)
{
    virHashTablePtr hash;
    int ret = -1;

    if (!(hash = testHashInit(0)))
        return -1;

348 349
    if (virHashForEach(hash, testHashForEachIter, hash)) {
        VIR_TEST_VERBOSE("\nvirHashForEach didn't go through all entries");
350 351 352 353 354
        goto cleanup;
    }

    ret = 0;

355
 cleanup:
356 357 358 359 360 361 362 363 364 365 366 367
    virHashFree(hash);
    return ret;
}


static int
testHashRemoveSetIter(const void *payload ATTRIBUTE_UNUSED,
                      const void *name,
                      const void *data)
{
    int *count = (int *) data;
    bool rem = false;
368
    size_t i;
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401

    for (i = 0; i < ARRAY_CARDINALITY(uuids_subset); i++) {
        if (STREQ(uuids_subset[i], name)) {
            rem = true;
            break;
        }
    }

    if (rem || rand() % 2) {
        (*count)++;
        return 1;
    } else {
        return 0;
    }
}

static int
testHashRemoveSet(const void *data ATTRIBUTE_UNUSED)
{
    virHashTablePtr hash;
    int count = 0;
    int rcount;
    int ret = -1;

    if (!(hash = testHashInit(0)))
        return -1;

    /* seed the generator so that rand() provides reproducible sequence */
    srand(9000);

    rcount = virHashRemoveSet(hash, testHashRemoveSetIter, &count);

    if (count != rcount) {
402 403 404
        VIR_TEST_VERBOSE("\nvirHashRemoveSet didn't remove expected number of"
                  " entries, %d != %u\n",
                  rcount, count);
405 406 407 408 409 410 411 412
        goto cleanup;
    }

    if (testHashCheckCount(hash, ARRAY_CARDINALITY(uuids) - count) < 0)
        goto cleanup;

    ret = 0;

413
 cleanup:
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    virHashFree(hash);
    return ret;
}


const int testSearchIndex = ARRAY_CARDINALITY(uuids_subset) / 2;

static int
testHashSearchIter(const void *payload ATTRIBUTE_UNUSED,
                   const void *name,
                   const void *data ATTRIBUTE_UNUSED)
{
    return STREQ(uuids_subset[testSearchIndex], name);
}

static int
testHashSearch(const void *data ATTRIBUTE_UNUSED)
{
    virHashTablePtr hash;
    void *entry;
    int ret = -1;

    if (!(hash = testHashInit(0)))
        return -1;

439
    entry = virHashSearch(hash, testHashSearchIter, NULL, NULL);
440 441

    if (!entry || STRNEQ(uuids_subset[testSearchIndex], entry)) {
442 443
        VIR_TEST_VERBOSE("\nvirHashSearch didn't find entry '%s'\n",
                  uuids_subset[testSearchIndex]);
444 445 446 447 448 449 450 451
        goto cleanup;
    }

    if (testHashCheckCount(hash, ARRAY_CARDINALITY(uuids)) < 0)
        goto cleanup;

    ret = 0;

452
 cleanup:
453 454 455 456 457
    virHashFree(hash);
    return ret;
}


E
Eric Blake 已提交
458
static int
E
Eric Blake 已提交
459 460
testHashGetItemsCompKey(const virHashKeyValuePair *a,
                        const virHashKeyValuePair *b)
E
Eric Blake 已提交
461
{
462
    return strcmp(a->key, b->key);
E
Eric Blake 已提交
463 464 465
}

static int
E
Eric Blake 已提交
466 467
testHashGetItemsCompValue(const virHashKeyValuePair *a,
                          const virHashKeyValuePair *b)
E
Eric Blake 已提交
468
{
469
    return strcmp(a->value, b->value);
E
Eric Blake 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
}

static int
testHashGetItems(const void *data ATTRIBUTE_UNUSED)
{
    virHashTablePtr hash;
    virHashKeyValuePairPtr array = NULL;
    int ret = -1;
    char keya[] = "a";
    char keyb[] = "b";
    char keyc[] = "c";
    char value1[] = "1";
    char value2[] = "2";
    char value3[] = "3";

    if (!(hash = virHashCreate(0, NULL)) ||
        virHashAddEntry(hash, keya, value3) < 0 ||
        virHashAddEntry(hash, keyc, value1) < 0 ||
        virHashAddEntry(hash, keyb, value2) < 0) {
489
        VIR_TEST_VERBOSE("\nfailed to create hash");
E
Eric Blake 已提交
490 491 492 493 494
        goto cleanup;
    }

    if (!(array = virHashGetItems(hash, NULL)) ||
        array[3].key || array[3].value) {
495
        VIR_TEST_VERBOSE("\nfailed to get items with NULL sort");
E
Eric Blake 已提交
496 497 498 499 500 501 502 503 504 505 506 507
        goto cleanup;
    }
    VIR_FREE(array);

    if (!(array = virHashGetItems(hash, testHashGetItemsCompKey)) ||
        STRNEQ(array[0].key, "a") ||
        STRNEQ(array[0].value, "3") ||
        STRNEQ(array[1].key, "b") ||
        STRNEQ(array[1].value, "2") ||
        STRNEQ(array[2].key, "c") ||
        STRNEQ(array[2].value, "1") ||
        array[3].key || array[3].value) {
508
        VIR_TEST_VERBOSE("\nfailed to get items with key sort");
E
Eric Blake 已提交
509 510 511 512 513 514 515 516 517 518 519 520
        goto cleanup;
    }
    VIR_FREE(array);

    if (!(array = virHashGetItems(hash, testHashGetItemsCompValue)) ||
        STRNEQ(array[0].key, "c") ||
        STRNEQ(array[0].value, "1") ||
        STRNEQ(array[1].key, "b") ||
        STRNEQ(array[1].value, "2") ||
        STRNEQ(array[2].key, "a") ||
        STRNEQ(array[2].value, "3") ||
        array[3].key || array[3].value) {
521
        VIR_TEST_VERBOSE("\nfailed to get items with value sort");
E
Eric Blake 已提交
522 523 524 525 526
        goto cleanup;
    }

    ret = 0;

527
 cleanup:
E
Eric Blake 已提交
528 529 530 531 532
    VIR_FREE(array);
    virHashFree(hash);
    return ret;
}

533 534 535 536 537 538 539 540 541
static int
testHashEqualCompValue(const void *value1, const void *value2)
{
    return c_strcasecmp(value1, value2);
}

static int
testHashEqual(const void *data ATTRIBUTE_UNUSED)
{
542
    virHashTablePtr hash1, hash2 = NULL;
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    int ret = -1;
    char keya[] = "a";
    char keyb[] = "b";
    char keyc[] = "c";
    char value1_l[] = "m";
    char value2_l[] = "n";
    char value3_l[] = "o";
    char value1_u[] = "M";
    char value2_u[] = "N";
    char value3_u[] = "O";
    char value4_u[] = "P";

    if (!(hash1 = virHashCreate(0, NULL)) ||
        !(hash2 = virHashCreate(0, NULL)) ||
        virHashAddEntry(hash1, keya, value1_l) < 0 ||
        virHashAddEntry(hash1, keyb, value2_l) < 0 ||
        virHashAddEntry(hash1, keyc, value3_l) < 0 ||
        virHashAddEntry(hash2, keya, value1_u) < 0 ||
        virHashAddEntry(hash2, keyb, value2_u) < 0) {
562
        VIR_TEST_VERBOSE("\nfailed to create hashes");
563 564 565 566
        goto cleanup;
    }

    if (virHashEqual(hash1, hash2, testHashEqualCompValue)) {
567
        VIR_TEST_VERBOSE("\nfailed equal test for different number of elements");
568 569 570 571
        goto cleanup;
    }

    if (virHashAddEntry(hash2, keyc, value4_u) < 0) {
572
        VIR_TEST_VERBOSE("\nfailed to add element to hash2");
573 574 575 576
        goto cleanup;
    }

    if (virHashEqual(hash1, hash2, testHashEqualCompValue)) {
577
        VIR_TEST_VERBOSE("\nfailed equal test for same number of elements");
578 579 580 581
        goto cleanup;
    }

    if (virHashUpdateEntry(hash2, keyc, value3_u) < 0) {
582
        VIR_TEST_VERBOSE("\nfailed to update element in hash2");
583 584 585 586
        goto cleanup;
    }

    if (!virHashEqual(hash1, hash2, testHashEqualCompValue)) {
587
        VIR_TEST_VERBOSE("\nfailed equal test for equal hash tables");
588 589 590 591 592
        goto cleanup;
    }

    ret = 0;

593
 cleanup:
594 595 596 597 598
    virHashFree(hash1);
    virHashFree(hash2);
    return ret;
}

E
Eric Blake 已提交
599

600
static int
E
Eric Blake 已提交
601
mymain(void)
602 603 604 605 606 607
{
    int ret = 0;

#define DO_TEST_FULL(name, cmd, data, count)                        \
    do {                                                            \
        struct testInfo info = { data, count };                     \
608
        if (virTestRun(name, testHash ## cmd, &info) < 0)           \
609 610 611 612 613 614 615 616 617
            ret = -1;                                               \
    } while (0)

#define DO_TEST_DATA(name, cmd, data)                               \
    DO_TEST_FULL(name "(" #data ")",                                \
                 cmd,                                               \
                 testHash ## cmd ## data,                           \
                 testHashCount ## cmd ## data)

618 619 620 621 622 623 624 625 626 627 628
#define DO_TEST_COUNT(name, cmd, count)                             \
    DO_TEST_FULL(name "(" #count ")", cmd, NULL, count)

#define DO_TEST(name, cmd)                                          \
    DO_TEST_FULL(name, cmd, NULL, -1)

    DO_TEST_COUNT("Grow", Grow, 1);
    DO_TEST_COUNT("Grow", Grow, 10);
    DO_TEST_COUNT("Grow", Grow, 42);
    DO_TEST("Update", Update);
    DO_TEST("Remove", Remove);
629 630
    DO_TEST_DATA("Remove in ForEach", RemoveForEach, Some);
    DO_TEST_DATA("Remove in ForEach", RemoveForEach, All);
631 632 633 634 635
    DO_TEST_DATA("Remove in ForEach", RemoveForEach, Forbidden);
    DO_TEST("Steal", Steal);
    DO_TEST("Forbidden ops in ForEach", ForEach);
    DO_TEST("RemoveSet", RemoveSet);
    DO_TEST("Search", Search);
E
Eric Blake 已提交
636
    DO_TEST("GetItems", GetItems);
637
    DO_TEST("Equal", Equal);
638 639 640 641

    return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

642
VIR_TEST_MAIN(mymain)