aboutsummaryrefslogtreecommitdiff
path: root/tests/test62.c
blob: ad8ce45e63b2d44f0c190f9c993b406e3172e2cb (plain)
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
#include <assert.h>
#include <stdlib.h>

#define HASH_FUNCTION(s, len, hashv) (hashv) = TrivialHash((const char *)s, len)
#include "uthash.h"

unsigned int TrivialHash(const char *s, size_t len)
{
    unsigned int h = 0;
    size_t i;
    for (i=0; i < len; ++i) {
        h += (unsigned char)s[i];
    }
    return h;
}

struct test_t {
    int a;
    int b;
    UT_hash_handle hh;
};

struct test_t *make_test(int value)
{
    struct test_t *test = (struct test_t *)malloc(sizeof *test);
    assert(test != NULL);
    test->a = value;
    return test;
}

int main()
{
    struct test_t *tests = NULL;
    struct test_t *test = NULL;
    int x;
    unsigned int h;

    x = 0x0042;
    HASH_VALUE(&x, sizeof x, h);
    assert(h == 0x42);

    x = 0x4002;
    HASH_VALUE(&x, sizeof x, h);
    assert(h == 0x42);

    test = make_test(0x0042);
    HASH_ADD_INT(tests, a, test);
    test = make_test(0x4002);
    HASH_ADD_INT(tests, a, test);

    x = 0x4002;
    test = NULL;
    HASH_FIND_BYHASHVALUE(hh, tests, &x, sizeof x, 0x42, test);
    assert(test != NULL);
    assert(test->a == 0x4002);

    x = 0x0042;
    test = NULL;
    HASH_FIND_BYHASHVALUE(hh, tests, &x, sizeof x, 0x42, test);
    assert(test != NULL);
    assert(test->a == 0x0042);

    x = 0x4002;
    test = NULL;
    HASH_FIND_BYHASHVALUE(hh, tests, &x, sizeof x, 0x43, test);
    assert(test == NULL);

    x = 0x0042;
    test = NULL;
    HASH_FIND_BYHASHVALUE(hh, tests, &x, sizeof x, 0x43, test);
    assert(test == NULL);

    x = 0x4003;
    test = NULL;
    HASH_FIND_BYHASHVALUE(hh, tests, &x, sizeof x, 0x42, test);
    assert(test == NULL);

    HASH_CLEAR(hh, tests);
}