4 * License: GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
7 #include "qemu/osdep.h"
13 static int32_t arr[N * 2];
15 static bool is_equal(const void *obj, const void *userp)
17 const int32_t *a = obj;
18 const int32_t *b = userp;
23 static void insert(int a, int b)
27 for (i = a; i < b; i++) {
33 qht_insert(&ht, &arr[i], hash);
37 static void rm(int init, int end)
41 for (i = init; i < end; i++) {
45 g_assert_true(qht_remove(&ht, &arr[i], hash));
49 static void check(int a, int b, bool expected)
51 struct qht_stats stats;
54 for (i = a; i < b; i++) {
61 p = qht_lookup(&ht, is_equal, &val, hash);
62 g_assert_true(!!p == expected);
64 qht_statistics_init(&ht, &stats);
65 if (stats.used_head_buckets) {
66 g_assert_cmpfloat(qdist_avg(&stats.chain), >=, 1.0);
68 g_assert_cmpuint(stats.head_buckets, >, 0);
69 qht_statistics_destroy(&stats);
72 static void count_func(struct qht *ht, void *p, uint32_t hash, void *userp)
74 unsigned int *curr = userp;
79 static void check_n(size_t expected)
81 struct qht_stats stats;
83 qht_statistics_init(&ht, &stats);
84 g_assert_cmpuint(stats.entries, ==, expected);
85 qht_statistics_destroy(&stats);
88 static void iter_check(unsigned int count)
90 unsigned int curr = 0;
92 qht_iter(&ht, count_func, &curr);
93 g_assert_cmpuint(curr, ==, count);
96 static void qht_do_test(unsigned int mode, size_t init_entries)
98 qht_init(&ht, 0, mode);
103 check(-N, -1, false);
118 check_n(N - 190 + 50);
124 qht_reset_size(&ht, 0);
131 static void qht_test(unsigned int mode)
133 qht_do_test(mode, 0);
134 qht_do_test(mode, 1);
135 qht_do_test(mode, 2);
136 qht_do_test(mode, 8);
137 qht_do_test(mode, 16);
138 qht_do_test(mode, 8192);
139 qht_do_test(mode, 16384);
142 static void test_default(void)
147 static void test_resize(void)
149 qht_test(QHT_MODE_AUTO_RESIZE);
152 int main(int argc, char *argv[])
154 g_test_init(&argc, &argv, NULL);
155 g_test_add_func("/qht/mode/default", test_default);
156 g_test_add_func("/qht/mode/resize", test_resize);