summaryrefslogtreecommitdiff
path: root/lib/ccan/htable/test/run.c
blob: ece46a0fd7cf4025ae2249785a2fd9d4694a5ee8 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <ccan/htable/htable.h>
#include <ccan/htable/htable.c>
#include <ccan/tap/tap.h>
#include <stdbool.h>
#include <string.h>

#define NUM_VALS (1 << HTABLE_BASE_BITS)

/* We use the number divided by two as the hash (for lots of
   collisions), plus set all the higher bits so we can detect if they
   don't get masked out. */
static size_t hash(const void *elem, void *unused)
{
	size_t h = *(uint64_t *)elem / 2;
	h |= -1UL << HTABLE_BASE_BITS;
	return h;
}

static bool objcmp(const void *htelem, void *cmpdata)
{
	return *(uint64_t *)htelem == *(uint64_t *)cmpdata;
}

static void add_vals(struct htable *ht,
		     const uint64_t val[], unsigned int num)
{
	uint64_t i;

	for (i = 0; i < num; i++) {
		if (htable_get(ht, hash(&i, NULL), objcmp, &i)) {
			fail("%llu already in hash", (long long)i);
			return;
		}
		htable_add(ht, hash(&val[i], NULL), &val[i]);
		if (htable_get(ht, hash(&i, NULL), objcmp, &i) != &val[i]) {
			fail("%llu not added to hash", (long long)i);
			return;
		}
	}
	pass("Added %llu numbers to hash", (long long)i);
}

#if 0
static void refill_vals(struct htable *ht,
			const uint64_t val[], unsigned int num)
{
	uint64_t i;

	for (i = 0; i < num; i++) {
		if (htable_get(ht, hash(&i, NULL), objcmp, &i))
			continue;
		htable_add(ht, hash(&val[i], NULL), &val[i]);
	}
}
#endif

static void find_vals(struct htable *ht,
		      const uint64_t val[], unsigned int num)
{
	uint64_t i;

	for (i = 0; i < num; i++) {
		if (htable_get(ht, hash(&i, NULL), objcmp, &i) != &val[i]) {
			fail("%llu not found in hash", (long long)i);
			return;
		}
	}
	pass("Found %llu numbers in hash", (long long)i);
}

static void del_vals(struct htable *ht,
		     const uint64_t val[], unsigned int num)
{
	uint64_t i;

	for (i = 0; i < num; i++) {
		if (!htable_del(ht, hash(&val[i], NULL), &val[i])) {
			fail("%llu not deleted from hash", (long long)i);
			return;
		}
	}
	pass("Deleted %llu numbers in hash", (long long)i);
}

static bool check_mask(struct htable *ht, uint64_t val[], unsigned num)
{
	uint64_t i;

	for (i = 0; i < num; i++) {
		if (((uintptr_t)&val[i] & ht->common_mask) != ht->common_bits)
			return false;
	}
	return true;
}

int main(int argc, char *argv[])
{
	unsigned int i;
	uintptr_t perfect_bit;
	struct htable *ht;
	uint64_t val[NUM_VALS];
	uint64_t dne;
	void *p;
	struct htable_iter iter;

	plan_tests(23);
	for (i = 0; i < NUM_VALS; i++)
		val[i] = i;
	dne = i;

	ht = htable_new(hash, NULL);
	ok1(ht->max < (1 << ht->bits));
	ok1(ht->bits == HTABLE_BASE_BITS);

	/* We cannot find an entry which doesn't exist. */
	ok1(!htable_get(ht, hash(&dne, NULL), objcmp, &dne));

	/* Fill it, it should increase in size (once). */
	add_vals(ht, val, NUM_VALS);
	ok1(ht->bits == HTABLE_BASE_BITS + 1);
	ok1(ht->max < (1 << ht->bits));

	/* Mask should be set. */
	ok1(ht->common_mask != 0);
	ok1(ht->common_mask != -1);
	ok1(check_mask(ht, val, NUM_VALS));

	/* Find all. */
	find_vals(ht, val, NUM_VALS);
	ok1(!htable_get(ht, hash(&dne, NULL), objcmp, &dne));

	/* Walk once, should get them all. */
	i = 0;
	for (p = htable_first(ht,&iter); p; p = htable_next(ht, &iter))
		i++;
	ok1(i == NUM_VALS);

	/* Delete all. */
	del_vals(ht, val, NUM_VALS);
	ok1(!htable_get(ht, hash(&val[0], NULL), objcmp, &val[0]));

	/* Worst case, a "pointer" which doesn't have any matching bits. */
	htable_add(ht, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]);
	htable_add(ht, hash(&val[NUM_VALS-1], NULL), &val[NUM_VALS-1]);
	ok1(ht->common_mask == 0);
	ok1(ht->common_bits == 0);
	/* Get rid of bogus pointer before we trip over it! */
	htable_del(ht, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]);

	/* Add the rest. */
	add_vals(ht, val, NUM_VALS-1);

	/* Check we can find them all. */
	find_vals(ht, val, NUM_VALS);
	ok1(!htable_get(ht, hash(&dne, NULL), objcmp, &dne));

	htable_free(ht);

	/* Corner cases: wipe out the perfect bit using bogus pointer. */
	ht = htable_new(hash, NULL);
	htable_add(ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1]));
	ok1(ht->perfect_bit);
	perfect_bit = ht->perfect_bit;
	htable_add(ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1]
				   | perfect_bit));
	ok1(ht->perfect_bit == 0);
	htable_del(ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1] | perfect_bit));

	/* Enlarging should restore it... */
	add_vals(ht, val, NUM_VALS-1);

	ok1(ht->perfect_bit != 0);
	htable_free(ht);

	return exit_status();
}