virhashtest.c 13.1 KB
Newer Older
1 2
#include <config.h>

3
#include <time.h>
4 5

#include "internal.h"
6 7
#include "virhash.h"
#include "virhashdata.h"
8
#include "testutils.h"
9
#include "viralloc.h"
10
#include "virlog.h"
11
#include "virstring.h"
12

13 14
#define VIR_FROM_THIS VIR_FROM_NONE

15 16
VIR_LOG_INIT("tests.hashtest");

17 18 19 20
static virHashTablePtr
testHashInit(int size)
{
    virHashTablePtr hash;
21
    ssize_t i;
22 23 24 25

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

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

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

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

    return hash;
}

53
static int
E
Eric Blake 已提交
54 55 56 57
testHashCheckForEachCount(void *payload ATTRIBUTE_UNUSED,
                          const void *name ATTRIBUTE_UNUSED,
                          void *data ATTRIBUTE_UNUSED)
{
58 59
    size_t *count = data;
    *count += 1;
60
    return 0;
E
Eric Blake 已提交
61
}
62 63

static int
64
testHashCheckCount(virHashTablePtr hash, size_t count)
65
{
66
    size_t iter_count = 0;
E
Eric Blake 已提交
67

68
    if (virHashSize(hash) != count) {
69
        VIR_TEST_VERBOSE("\nhash contains %zd instead of %zu elements",
70
                         virHashSize(hash), count);
71 72 73
        return -1;
    }

74
    virHashForEach(hash, testHashCheckForEachCount, &iter_count);
E
Eric Blake 已提交
75
    if (count != iter_count) {
76
        VIR_TEST_VERBOSE("\nhash claims to have %zu elements but iteration"
77
                         "finds %zu", count, iter_count);
E
Eric Blake 已提交
78 79 80
        return -1;
    }

81 82 83 84 85 86
    return 0;
}


struct testInfo {
    void *data;
87
    size_t count;
88 89 90
};


91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
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;

106
 cleanup:
107 108 109 110 111 112 113 114 115 116
    virHashFree(hash);
    return ret;
}


static int
testHashUpdate(const void *data ATTRIBUTE_UNUSED)
{
    int count = ARRAY_CARDINALITY(uuids) + ARRAY_CARDINALITY(uuids_new);
    virHashTablePtr hash;
117
    size_t i;
118 119 120 121 122 123 124
    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) {
125
            VIR_TEST_VERBOSE("\nentry \"%s\" could not be updated",
126
                    uuids_subset[i]);
127 128 129 130 131 132
            goto cleanup;
        }
    }

    for (i = 0; i < ARRAY_CARDINALITY(uuids_new); i++) {
        if (virHashUpdateEntry(hash, uuids_new[i], (void *) 1) < 0) {
133
            VIR_TEST_VERBOSE("\nnew entry \"%s\" could not be updated",
134
                    uuids_new[i]);
135 136 137 138 139 140 141 142 143
            goto cleanup;
        }
    }

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

    ret = 0;

144
 cleanup:
145 146 147 148 149 150 151 152 153 154
    virHashFree(hash);
    return ret;
}


static int
testHashRemove(const void *data ATTRIBUTE_UNUSED)
{
    int count = ARRAY_CARDINALITY(uuids) - ARRAY_CARDINALITY(uuids_subset);
    virHashTablePtr hash;
155
    size_t i;
156 157 158 159 160 161 162
    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) {
163
            VIR_TEST_VERBOSE("\nentry \"%s\" could not be removed",
164
                    uuids_subset[i]);
165 166 167 168 169 170 171 172 173
            goto cleanup;
        }
    }

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

    ret = 0;

174
 cleanup:
175 176 177 178 179
    virHashFree(hash);
    return ret;
}


180 181 182
const int testHashCountRemoveForEachSome =
    ARRAY_CARDINALITY(uuids) - ARRAY_CARDINALITY(uuids_subset);

183
static int
184 185 186 187 188
testHashRemoveForEachSome(void *payload ATTRIBUTE_UNUSED,
                          const void *name,
                          void *data)
{
    virHashTablePtr hash = data;
189
    size_t i;
190 191 192

    for (i = 0; i < ARRAY_CARDINALITY(uuids_subset); i++) {
        if (STREQ(uuids_subset[i], name)) {
193 194
            if (virHashRemoveEntry(hash, name) < 0) {
                VIR_TEST_VERBOSE("\nentry \"%s\" could not be removed",
195 196 197 198 199
                        uuids_subset[i]);
            }
            break;
        }
    }
200
    return 0;
201 202 203 204 205
}


const int testHashCountRemoveForEachAll = 0;

206
static int
207 208 209 210 211 212 213
testHashRemoveForEachAll(void *payload ATTRIBUTE_UNUSED,
                         const void *name,
                         void *data)
{
    virHashTablePtr hash = data;

    virHashRemoveEntry(hash, name);
214
    return 0;
215 216 217 218 219 220 221 222 223 224 225 226 227
}


static int
testHashRemoveForEach(const void *data)
{
    const struct testInfo *info = data;
    virHashTablePtr hash;
    int ret = -1;

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

228 229
    if (virHashForEach(hash, (virHashIterator) info->data, hash)) {
        VIR_TEST_VERBOSE("\nvirHashForEach didn't go through all entries");
230 231 232 233 234 235 236 237
        goto cleanup;
    }

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

    ret = 0;

238
 cleanup:
239 240 241 242 243
    virHashFree(hash);
    return ret;
}


244 245 246 247 248
static int
testHashSteal(const void *data ATTRIBUTE_UNUSED)
{
    int count = ARRAY_CARDINALITY(uuids) - ARRAY_CARDINALITY(uuids_subset);
    virHashTablePtr hash;
249
    size_t i;
250 251 252 253 254 255 256
    int ret = -1;

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

    for (i = 0; i < ARRAY_CARDINALITY(uuids_subset); i++) {
        if (!virHashSteal(hash, uuids_subset[i])) {
257
            VIR_TEST_VERBOSE("\nentry \"%s\" could not be stolen",
258
                    uuids_subset[i]);
259 260 261 262 263 264 265 266 267
            goto cleanup;
        }
    }

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

    ret = 0;

268
 cleanup:
269 270 271 272 273 274 275 276 277 278 279 280
    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;
281
    size_t i;
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

    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) {
315
        VIR_TEST_VERBOSE("\nvirHashRemoveSet didn't remove expected number of"
316
                  " entries, %d != %u",
317
                  rcount, count);
318 319 320 321 322 323 324 325
        goto cleanup;
    }

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

    ret = 0;

326
 cleanup:
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
    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;

352
    entry = virHashSearch(hash, testHashSearchIter, NULL, NULL);
353 354

    if (!entry || STRNEQ(uuids_subset[testSearchIndex], entry)) {
355
        VIR_TEST_VERBOSE("\nvirHashSearch didn't find entry '%s'",
356
                  uuids_subset[testSearchIndex]);
357 358 359 360 361 362 363 364
        goto cleanup;
    }

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

    ret = 0;

365
 cleanup:
366 367 368 369 370
    virHashFree(hash);
    return ret;
}


E
Eric Blake 已提交
371
static int
E
Eric Blake 已提交
372 373
testHashGetItemsCompKey(const virHashKeyValuePair *a,
                        const virHashKeyValuePair *b)
E
Eric Blake 已提交
374
{
375
    return strcmp(a->key, b->key);
E
Eric Blake 已提交
376 377 378
}

static int
E
Eric Blake 已提交
379 380
testHashGetItemsCompValue(const virHashKeyValuePair *a,
                          const virHashKeyValuePair *b)
E
Eric Blake 已提交
381
{
382
    return strcmp(a->value, b->value);
E
Eric Blake 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
}

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) {
402
        VIR_TEST_VERBOSE("\nfailed to create hash");
E
Eric Blake 已提交
403 404 405 406 407
        goto cleanup;
    }

    if (!(array = virHashGetItems(hash, NULL)) ||
        array[3].key || array[3].value) {
408
        VIR_TEST_VERBOSE("\nfailed to get items with NULL sort");
E
Eric Blake 已提交
409 410 411 412 413 414 415 416 417 418 419 420
        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) {
421
        VIR_TEST_VERBOSE("\nfailed to get items with key sort");
E
Eric Blake 已提交
422 423 424 425 426 427 428 429 430 431 432 433
        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) {
434
        VIR_TEST_VERBOSE("\nfailed to get items with value sort");
E
Eric Blake 已提交
435 436 437 438 439
        goto cleanup;
    }

    ret = 0;

440
 cleanup:
E
Eric Blake 已提交
441 442 443 444 445
    VIR_FREE(array);
    virHashFree(hash);
    return ret;
}

446 447 448 449 450 451 452 453 454
static int
testHashEqualCompValue(const void *value1, const void *value2)
{
    return c_strcasecmp(value1, value2);
}

static int
testHashEqual(const void *data ATTRIBUTE_UNUSED)
{
455
    virHashTablePtr hash1, hash2 = NULL;
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    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) {
475
        VIR_TEST_VERBOSE("\nfailed to create hashes");
476 477 478 479
        goto cleanup;
    }

    if (virHashEqual(hash1, hash2, testHashEqualCompValue)) {
480
        VIR_TEST_VERBOSE("\nfailed equal test for different number of elements");
481 482 483 484
        goto cleanup;
    }

    if (virHashAddEntry(hash2, keyc, value4_u) < 0) {
485
        VIR_TEST_VERBOSE("\nfailed to add element to hash2");
486 487 488 489
        goto cleanup;
    }

    if (virHashEqual(hash1, hash2, testHashEqualCompValue)) {
490
        VIR_TEST_VERBOSE("\nfailed equal test for same number of elements");
491 492 493 494
        goto cleanup;
    }

    if (virHashUpdateEntry(hash2, keyc, value3_u) < 0) {
495
        VIR_TEST_VERBOSE("\nfailed to update element in hash2");
496 497 498 499
        goto cleanup;
    }

    if (!virHashEqual(hash1, hash2, testHashEqualCompValue)) {
500
        VIR_TEST_VERBOSE("\nfailed equal test for equal hash tables");
501 502 503 504 505
        goto cleanup;
    }

    ret = 0;

506
 cleanup:
507 508 509 510 511
    virHashFree(hash1);
    virHashFree(hash2);
    return ret;
}

E
Eric Blake 已提交
512

513
static int
E
Eric Blake 已提交
514
mymain(void)
515 516 517
{
    int ret = 0;

518 519 520 521 522
#define DO_TEST_FULL(name, cmd, data, count) \
    do { \
        struct testInfo info = { data, count }; \
        if (virTestRun(name, testHash ## cmd, &info) < 0) \
            ret = -1; \
523 524
    } while (0)

525 526 527 528
#define DO_TEST_DATA(name, cmd, data) \
    DO_TEST_FULL(name "(" #data ")", \
                 cmd, \
                 testHash ## cmd ## data, \
529 530
                 testHashCount ## cmd ## data)

531
#define DO_TEST_COUNT(name, cmd, count) \
532 533
    DO_TEST_FULL(name "(" #count ")", cmd, NULL, count)

534
#define DO_TEST(name, cmd) \
535 536 537 538 539 540 541
    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);
542 543
    DO_TEST_DATA("Remove in ForEach", RemoveForEach, Some);
    DO_TEST_DATA("Remove in ForEach", RemoveForEach, All);
544 545 546
    DO_TEST("Steal", Steal);
    DO_TEST("RemoveSet", RemoveSet);
    DO_TEST("Search", Search);
E
Eric Blake 已提交
547
    DO_TEST("GetItems", GetItems);
548
    DO_TEST("Equal", Equal);
549 550 551 552

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

553
VIR_TEST_MAIN(mymain)