]> Git Repo - qemu.git/blame - tests/test-qht.c
tests/migration: Add migration-test header file
[qemu.git] / tests / test-qht.c
CommitLineData
1a95404f
EC
1/*
2 * Copyright (C) 2016, Emilio G. Cota <[email protected]>
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"
1a95404f 8#include "qemu/qht.h"
9c7d64eb 9#include "qemu/rcu.h"
1a95404f
EC
10
11#define N 5000
12
13static struct qht ht;
14static int32_t arr[N * 2];
15
61b8cef1 16static bool is_equal(const void *ap, const void *bp)
1a95404f 17{
61b8cef1
EC
18 const int32_t *a = ap;
19 const int32_t *b = bp;
1a95404f
EC
20
21 return *a == *b;
22}
23
24static void insert(int a, int b)
25{
26 int i;
27
28 for (i = a; i < b; i++) {
29 uint32_t hash;
32359d52
EC
30 void *existing;
31 bool inserted;
1a95404f
EC
32
33 arr[i] = i;
34 hash = i;
35
32359d52
EC
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]);
1a95404f
EC
41 }
42}
43
44static 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
56static void check(int a, int b, bool expected)
57{
58 struct qht_stats stats;
59 int i;
60
9c7d64eb 61 rcu_read_lock();
1a95404f
EC
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;
61b8cef1
EC
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 }
1a95404f
EC
75 g_assert_true(!!p == expected);
76 }
9c7d64eb
EC
77 rcu_read_unlock();
78
1a95404f
EC
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
87static void count_func(struct qht *ht, void *p, uint32_t hash, void *userp)
88{
89 unsigned int *curr = userp;
90
91 (*curr)++;
92}
93
94static 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
103static 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
111static void qht_do_test(unsigned int mode, size_t init_entries)
112{
7266ae91
EC
113 /* under KVM we might fetch stats from an uninitialized qht */
114 check_n(0);
115
61b8cef1 116 qht_init(&ht, is_equal, 0, mode);
1a95404f 117
7266ae91 118 check_n(0);
1a95404f
EC
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
150static 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
161static void test_default(void)
162{
163 qht_test(0);
164}
165
166static void test_resize(void)
167{
168 qht_test(QHT_MODE_AUTO_RESIZE);
169}
170
171int 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}
This page took 0.182876 seconds and 4 git commands to generate.