hw/cpu/a9mpcore: Fix introspection problems with the "a9mpcore_priv" device
[qemu/ar7.git] / tests / test-qht.c
blobdda6a067beb369cfe05efca28bb7db71067822b3
1 /*
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.
6 */
7 #include "qemu/osdep.h"
8 #include "qemu/qht.h"
9 #include "qemu/rcu.h"
11 #define N 5000
13 static struct qht ht;
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;
21 return *a == *b;
24 static void insert(int a, int b)
26 int i;
28 for (i = a; i < b; i++) {
29 uint32_t hash;
30 void *existing;
31 bool inserted;
33 arr[i] = i;
34 hash = 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)
46 int i;
48 for (i = init; i < end; i++) {
49 uint32_t hash;
51 hash = arr[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;
59 int i;
61 rcu_read_lock();
62 for (i = a; i < b; i++) {
63 void *p;
64 uint32_t hash;
65 int32_t val;
67 val = i;
68 hash = i;
69 /* test both lookup variants; results should be the same */
70 if (i % 2) {
71 p = qht_lookup(&ht, &val, hash);
72 } else {
73 p = qht_lookup_custom(&ht, &val, hash, is_equal);
75 g_assert_true(!!p == expected);
77 rcu_read_unlock();
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;
91 (*curr)++;
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 */
114 check_n(0);
116 qht_init(&ht, is_equal, 0, mode);
118 check_n(0);
119 insert(0, N);
120 check(0, N, true);
121 check_n(N);
122 check(-N, -1, false);
123 iter_check(N);
125 rm(101, 102);
126 check_n(N - 1);
127 insert(N, N * 2);
128 check_n(N + N - 1);
129 rm(N, N * 2);
130 check_n(N - 1);
131 insert(101, 102);
132 check_n(N);
134 rm(10, 200);
135 check_n(N - 190);
136 insert(150, 200);
137 check_n(N - 190 + 50);
138 insert(10, 150);
139 check_n(N);
141 rm(1, 2);
142 check_n(N - 1);
143 qht_reset_size(&ht, 0);
144 check_n(0);
145 check(0, N, false);
147 qht_destroy(&ht);
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)
163 qht_test(0);
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);
176 return g_test_run();