2 * Copyright (C) 2016, Emilio G. Cota <cota@braap.org>
4 * License: GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
7 #include "qemu/osdep.h"
14 static int32_t arr
[N
* 2];
16 static bool is_equal(const void *ap
, const void *bp
)
18 const int32_t *a
= ap
;
19 const int32_t *b
= bp
;
24 static void insert(int a
, int b
)
28 for (i
= a
; i
< b
; i
++) {
36 inserted
= qht_insert(&ht
, &arr
[i
], hash
, NULL
);
37 g_assert_true(inserted
);
38 inserted
= qht_insert(&ht
, &arr
[i
], hash
, &existing
);
39 g_assert_false(inserted
);
40 g_assert_true(existing
== &arr
[i
]);
44 static void rm(int init
, int end
)
48 for (i
= init
; i
< end
; i
++) {
52 g_assert_true(qht_remove(&ht
, &arr
[i
], hash
));
56 static void check(int a
, int b
, bool expected
)
58 struct qht_stats stats
;
62 for (i
= a
; i
< b
; i
++) {
69 /* test both lookup variants; results should be the same */
71 p
= qht_lookup(&ht
, &val
, hash
);
73 p
= qht_lookup_custom(&ht
, &val
, hash
, is_equal
);
75 g_assert_true(!!p
== expected
);
79 qht_statistics_init(&ht
, &stats
);
80 if (stats
.used_head_buckets
) {
81 g_assert_cmpfloat(qdist_avg(&stats
.chain
), >=, 1.0);
83 g_assert_cmpuint(stats
.head_buckets
, >, 0);
84 qht_statistics_destroy(&stats
);
87 static void count_func(struct qht
*ht
, void *p
, uint32_t hash
, void *userp
)
89 unsigned int *curr
= userp
;
94 static void check_n(size_t expected
)
96 struct qht_stats stats
;
98 qht_statistics_init(&ht
, &stats
);
99 g_assert_cmpuint(stats
.entries
, ==, expected
);
100 qht_statistics_destroy(&stats
);
103 static void iter_check(unsigned int count
)
105 unsigned int curr
= 0;
107 qht_iter(&ht
, count_func
, &curr
);
108 g_assert_cmpuint(curr
, ==, count
);
111 static void qht_do_test(unsigned int mode
, size_t init_entries
)
113 /* under KVM we might fetch stats from an uninitialized qht */
116 qht_init(&ht
, is_equal
, 0, mode
);
122 check(-N
, -1, false);
137 check_n(N
- 190 + 50);
143 qht_reset_size(&ht
, 0);
150 static void qht_test(unsigned int mode
)
152 qht_do_test(mode
, 0);
153 qht_do_test(mode
, 1);
154 qht_do_test(mode
, 2);
155 qht_do_test(mode
, 8);
156 qht_do_test(mode
, 16);
157 qht_do_test(mode
, 8192);
158 qht_do_test(mode
, 16384);
161 static void test_default(void)
166 static void test_resize(void)
168 qht_test(QHT_MODE_AUTO_RESIZE
);
171 int main(int argc
, char *argv
[])
173 g_test_init(&argc
, &argv
, NULL
);
174 g_test_add_func("/qht/mode/default", test_default
);
175 g_test_add_func("/qht/mode/resize", test_resize
);