aboutsummaryrefslogtreecommitdiff
path: root/tests/test57.c
blob: 7ba3b81ae845ef4b55e7ac1341cb981076ea881c (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
#include <assert.h>
#include <stddef.h>
#include "uthash.h"

typedef struct {
    void *key;
    int i;
    UT_hash_handle hh;
} el_t;

el_t *findit(el_t *hash, void *keytofind)
{
    el_t *found;
    HASH_FIND_PTR(hash, &keytofind, found);
    return found;
}

int main()
{
    el_t *hash = NULL;
    el_t e1;
    el_t e2;

    e1.key = NULL;
    e1.i = 1;

    e2.key = &e2;
    e2.i = 2;

    assert(findit(hash, NULL) == NULL);
    assert(findit(hash, &e1) == NULL);
    assert(findit(hash, &e2) == NULL);

    HASH_ADD_PTR(hash, key, &e1);
    assert(findit(hash, NULL) == &e1);
    assert(findit(hash, &e1) == NULL);
    assert(findit(hash, &e2) == NULL);

    HASH_ADD_PTR(hash, key, &e2);
    assert(findit(hash, NULL) == &e1);
    assert(findit(hash, &e1) == NULL);
    assert(findit(hash, &e2) == &e2);

    HASH_DEL(hash, &e1);
    assert(findit(hash, NULL) == NULL);
    assert(findit(hash, &e1) == NULL);
    assert(findit(hash, &e2) == &e2);

    HASH_CLEAR(hh, hash);
    assert(hash == NULL);

    return 0;
}