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
    if (size && size != virHashTableSize(hash))
        VIR_TEST_DEBUG("\n");
52

53 54 55
    return hash;
}

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

static int
67
testHashCheckCount(virHashTablePtr hash, size_t count)
68
{
69
    size_t iter_count = 0;
E
Eric Blake 已提交
70

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

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

84 85 86 87 88 89
    return 0;
}


struct testInfo {
    void *data;
90
    size_t count;
91 92 93
};


94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
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;

109
 cleanup:
110 111 112 113 114 115 116 117 118 119
    virHashFree(hash);
    return ret;
}


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

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

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

    ret = 0;

147
 cleanup:
148 149 150 151 152 153 154 155 156 157
    virHashFree(hash);
    return ret;
}


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

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

    ret = 0;

177
 cleanup:
178 179 180 181 182
    virHashFree(hash);
    return ret;
}


183 184 185
const int testHashCountRemoveForEachSome =
    ARRAY_CARDINALITY(uuids) - ARRAY_CARDINALITY(uuids_subset);

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

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


const int testHashCountRemoveForEachAll = 0;

209
static int
210 211 212 213 214 215 216
testHashRemoveForEachAll(void *payload ATTRIBUTE_UNUSED,
                         const void *name,
                         void *data)
{
    virHashTablePtr hash = data;

    virHashRemoveEntry(hash, name);
217
    return 0;
218 219 220 221 222 223 224 225 226 227 228 229 230
}


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

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

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

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

    ret = 0;

241
 cleanup:
242 243 244 245 246
    virHashFree(hash);
    return ret;
}


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

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

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

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

    ret = 0;

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

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

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

    ret = 0;

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

355
    entry = virHashSearch(hash, testHashSearchIter, NULL, NULL);
356 357

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

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

    ret = 0;

368
 cleanup:
369 370 371 372 373
    virHashFree(hash);
    return ret;
}


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

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

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

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

    ret = 0;

443
 cleanup:
E
Eric Blake 已提交
444 445 446 447 448
    VIR_FREE(array);
    virHashFree(hash);
    return ret;
}

449 450 451 452 453 454 455 456 457
static int
testHashEqualCompValue(const void *value1, const void *value2)
{
    return c_strcasecmp(value1, value2);
}

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

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

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

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

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

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

    ret = 0;

509
 cleanup:
510 511 512 513 514
    virHashFree(hash1);
    virHashFree(hash2);
    return ret;
}

E
Eric Blake 已提交
515

516
static int
E
Eric Blake 已提交
517
mymain(void)
518 519 520
{
    int ret = 0;

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

528 529 530 531
#define DO_TEST_DATA(name, cmd, data) \
    DO_TEST_FULL(name "(" #data ")", \
                 cmd, \
                 testHash ## cmd ## data, \
532 533
                 testHashCount ## cmd ## data)

534
#define DO_TEST_COUNT(name, cmd, count) \
535 536
    DO_TEST_FULL(name "(" #count ")", cmd, NULL, count)

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

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

556
VIR_TEST_MAIN(mymain)