summaryrefslogtreecommitdiff
path: root/ccan/htable/test/run-zero-hash-first-entry.c
blob: fdd18569e7cc516f2d5b7fb5bbc25a7d0a35bacf (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
#include <ccan/htable/htable.h>
#include <ccan/htable/htable.c>
#include <ccan/tap/tap.h>
#include <stdbool.h>

struct data {
	size_t key;
};

/* Hash is simply key itself. */
static size_t hash(const void *e, void *unused)
{
	struct data *d = (struct data *)e;

	return d->key;
}

static bool eq(const void *e, void *k)
{
	struct data *d = (struct data *)e;
	size_t *key = (size_t *)k;

	return (d->key == *key);
}

int main(void)
{
	struct htable table;
	struct data *d0, *d1;

	plan_tests(6);

	d1 = malloc(sizeof(struct data));
	d1->key = 1;
	d0 = malloc(sizeof(struct data));
	d0->key = 0;

	htable_init(&table, hash, NULL);

	htable_add(&table, d0->key, d0);
	htable_add(&table, d1->key, d1);

	ok1(table.elems == 2);
	ok1(htable_get(&table, 1, eq, &d1->key) == d1);
	ok1(htable_get(&table, 0, eq, &d0->key) == d0);
	htable_clear(&table);

	/* Now add in reverse order, should still be OK. */
	htable_add(&table, d1->key, d1);
	htable_add(&table, d0->key, d0);

	ok1(table.elems == 2);
	ok1(htable_get(&table, 1, eq, &d1->key) == d1);
	ok1(htable_get(&table, 0, eq, &d0->key) == d0);
	htable_clear(&table);

	free(d0);
	free(d1);
	return exit_status();
}