xref: /qemu/tests/unit/test-qht.c (revision 32359d529f30bea8124ed671b2e6a22f22540488)
1 /*
2  * Copyright (C) 2016, Emilio G. Cota <cota@braap.org>
3  *
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"
10 
11 #define N 5000
12 
13 static struct qht ht;
14 static int32_t arr[N * 2];
15 
16 static bool is_equal(const void *ap, const void *bp)
17 {
18     const int32_t *a = ap;
19     const int32_t *b = bp;
20 
21     return *a == *b;
22 }
23 
24 static void insert(int a, int b)
25 {
26     int i;
27 
28     for (i = a; i < b; i++) {
29         uint32_t hash;
30         void *existing;
31         bool inserted;
32 
33         arr[i] = i;
34         hash = i;
35 
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]);
41     }
42 }
43 
44 static void rm(int init, int end)
45 {
46     int i;
47 
48     for (i = init; i < end; i++) {
49         uint32_t hash;
50 
51         hash = arr[i];
52         g_assert_true(qht_remove(&ht, &arr[i], hash));
53     }
54 }
55 
56 static void check(int a, int b, bool expected)
57 {
58     struct qht_stats stats;
59     int i;
60 
61     rcu_read_lock();
62     for (i = a; i < b; i++) {
63         void *p;
64         uint32_t hash;
65         int32_t val;
66 
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);
74         }
75         g_assert_true(!!p == expected);
76     }
77     rcu_read_unlock();
78 
79     qht_statistics_init(&ht, &stats);
80     if (stats.used_head_buckets) {
81         g_assert_cmpfloat(qdist_avg(&stats.chain), >=, 1.0);
82     }
83     g_assert_cmpuint(stats.head_buckets, >, 0);
84     qht_statistics_destroy(&stats);
85 }
86 
87 static void count_func(struct qht *ht, void *p, uint32_t hash, void *userp)
88 {
89     unsigned int *curr = userp;
90 
91     (*curr)++;
92 }
93 
94 static void check_n(size_t expected)
95 {
96     struct qht_stats stats;
97 
98     qht_statistics_init(&ht, &stats);
99     g_assert_cmpuint(stats.entries, ==, expected);
100     qht_statistics_destroy(&stats);
101 }
102 
103 static void iter_check(unsigned int count)
104 {
105     unsigned int curr = 0;
106 
107     qht_iter(&ht, count_func, &curr);
108     g_assert_cmpuint(curr, ==, count);
109 }
110 
111 static void qht_do_test(unsigned int mode, size_t init_entries)
112 {
113     /* under KVM we might fetch stats from an uninitialized qht */
114     check_n(0);
115 
116     qht_init(&ht, is_equal, 0, mode);
117 
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);
124 
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);
133 
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);
140 
141     rm(1, 2);
142     check_n(N - 1);
143     qht_reset_size(&ht, 0);
144     check_n(0);
145     check(0, N, false);
146 
147     qht_destroy(&ht);
148 }
149 
150 static void qht_test(unsigned int mode)
151 {
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);
159 }
160 
161 static void test_default(void)
162 {
163     qht_test(0);
164 }
165 
166 static void test_resize(void)
167 {
168     qht_test(QHT_MODE_AUTO_RESIZE);
169 }
170 
171 int main(int argc, char *argv[])
172 {
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();
177 }
178