pc-dimm: pass in the machine and to the MemoryHotplugState
[qemu/ar7.git] / tests / test-qht.c
blob9b7423abb625fac2ed39e5548d35bd9a66dc68ee
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 *obj, const void *userp)
18 const int32_t *a = obj;
19 const int32_t *b = userp;
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;
31 arr[i] = i;
32 hash = i;
34 qht_insert(&ht, &arr[i], hash);
38 static void rm(int init, int end)
40 int i;
42 for (i = init; i < end; i++) {
43 uint32_t hash;
45 hash = arr[i];
46 g_assert_true(qht_remove(&ht, &arr[i], hash));
50 static void check(int a, int b, bool expected)
52 struct qht_stats stats;
53 int i;
55 rcu_read_lock();
56 for (i = a; i < b; i++) {
57 void *p;
58 uint32_t hash;
59 int32_t val;
61 val = i;
62 hash = i;
63 p = qht_lookup(&ht, is_equal, &val, hash);
64 g_assert_true(!!p == expected);
66 rcu_read_unlock();
68 qht_statistics_init(&ht, &stats);
69 if (stats.used_head_buckets) {
70 g_assert_cmpfloat(qdist_avg(&stats.chain), >=, 1.0);
72 g_assert_cmpuint(stats.head_buckets, >, 0);
73 qht_statistics_destroy(&stats);
76 static void count_func(struct qht *ht, void *p, uint32_t hash, void *userp)
78 unsigned int *curr = userp;
80 (*curr)++;
83 static void check_n(size_t expected)
85 struct qht_stats stats;
87 qht_statistics_init(&ht, &stats);
88 g_assert_cmpuint(stats.entries, ==, expected);
89 qht_statistics_destroy(&stats);
92 static void iter_check(unsigned int count)
94 unsigned int curr = 0;
96 qht_iter(&ht, count_func, &curr);
97 g_assert_cmpuint(curr, ==, count);
100 static void qht_do_test(unsigned int mode, size_t init_entries)
102 /* under KVM we might fetch stats from an uninitialized qht */
103 check_n(0);
105 qht_init(&ht, 0, mode);
107 check_n(0);
108 insert(0, N);
109 check(0, N, true);
110 check_n(N);
111 check(-N, -1, false);
112 iter_check(N);
114 rm(101, 102);
115 check_n(N - 1);
116 insert(N, N * 2);
117 check_n(N + N - 1);
118 rm(N, N * 2);
119 check_n(N - 1);
120 insert(101, 102);
121 check_n(N);
123 rm(10, 200);
124 check_n(N - 190);
125 insert(150, 200);
126 check_n(N - 190 + 50);
127 insert(10, 150);
128 check_n(N);
130 rm(1, 2);
131 check_n(N - 1);
132 qht_reset_size(&ht, 0);
133 check_n(0);
134 check(0, N, false);
136 qht_destroy(&ht);
139 static void qht_test(unsigned int mode)
141 qht_do_test(mode, 0);
142 qht_do_test(mode, 1);
143 qht_do_test(mode, 2);
144 qht_do_test(mode, 8);
145 qht_do_test(mode, 16);
146 qht_do_test(mode, 8192);
147 qht_do_test(mode, 16384);
150 static void test_default(void)
152 qht_test(0);
155 static void test_resize(void)
157 qht_test(QHT_MODE_AUTO_RESIZE);
160 int main(int argc, char *argv[])
162 g_test_init(&argc, &argv, NULL);
163 g_test_add_func("/qht/mode/default", test_default);
164 g_test_add_func("/qht/mode/resize", test_resize);
165 return g_test_run();