]>
Commit | Line | Data |
---|---|---|
e7c033c3 PB |
1 | /* |
2 | * Hierarchical bitmap unit-tests. | |
3 | * | |
4 | * Copyright (C) 2012 Red Hat Inc. | |
5 | * | |
6 | * Author: Paolo Bonzini <pbonzini@redhat.com> | |
7 | * | |
8 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
9 | * See the COPYING file in the top-level directory. | |
10 | */ | |
11 | ||
12 | #include <glib.h> | |
13 | #include <stdarg.h> | |
a94e87c0 JS |
14 | #include <string.h> |
15 | #include <sys/types.h> | |
e7c033c3 PB |
16 | #include "qemu/hbitmap.h" |
17 | ||
18 | #define LOG_BITS_PER_LONG (BITS_PER_LONG == 32 ? 5 : 6) | |
19 | ||
20 | #define L1 BITS_PER_LONG | |
21 | #define L2 (BITS_PER_LONG * L1) | |
22 | #define L3 (BITS_PER_LONG * L2) | |
23 | ||
24 | typedef struct TestHBitmapData { | |
25 | HBitmap *hb; | |
26 | unsigned long *bits; | |
27 | size_t size; | |
a94e87c0 | 28 | size_t old_size; |
e7c033c3 PB |
29 | int granularity; |
30 | } TestHBitmapData; | |
31 | ||
32 | ||
33 | /* Check that the HBitmap and the shadow bitmap contain the same data, | |
34 | * ignoring the same "first" bits. | |
35 | */ | |
36 | static void hbitmap_test_check(TestHBitmapData *data, | |
37 | uint64_t first) | |
38 | { | |
39 | uint64_t count = 0; | |
40 | size_t pos; | |
41 | int bit; | |
42 | HBitmapIter hbi; | |
43 | int64_t i, next; | |
44 | ||
45 | hbitmap_iter_init(&hbi, data->hb, first); | |
46 | ||
47 | i = first; | |
48 | for (;;) { | |
49 | next = hbitmap_iter_next(&hbi); | |
50 | if (next < 0) { | |
51 | next = data->size; | |
52 | } | |
53 | ||
54 | while (i < next) { | |
55 | pos = i >> LOG_BITS_PER_LONG; | |
56 | bit = i & (BITS_PER_LONG - 1); | |
57 | i++; | |
58 | g_assert_cmpint(data->bits[pos] & (1UL << bit), ==, 0); | |
59 | } | |
60 | ||
61 | if (next == data->size) { | |
62 | break; | |
63 | } | |
64 | ||
65 | pos = i >> LOG_BITS_PER_LONG; | |
66 | bit = i & (BITS_PER_LONG - 1); | |
67 | i++; | |
68 | count++; | |
69 | g_assert_cmpint(data->bits[pos] & (1UL << bit), !=, 0); | |
70 | } | |
71 | ||
72 | if (first == 0) { | |
73 | g_assert_cmpint(count << data->granularity, ==, hbitmap_count(data->hb)); | |
74 | } | |
75 | } | |
76 | ||
77 | /* This is provided instead of a test setup function so that the sizes | |
78 | are kept in the test functions (and not in main()) */ | |
79 | static void hbitmap_test_init(TestHBitmapData *data, | |
80 | uint64_t size, int granularity) | |
81 | { | |
82 | size_t n; | |
83 | data->hb = hbitmap_alloc(size, granularity); | |
84 | ||
85 | n = (size + BITS_PER_LONG - 1) / BITS_PER_LONG; | |
86 | if (n == 0) { | |
87 | n = 1; | |
88 | } | |
89 | data->bits = g_new0(unsigned long, n); | |
90 | data->size = size; | |
91 | data->granularity = granularity; | |
1b095244 PB |
92 | if (size) { |
93 | hbitmap_test_check(data, 0); | |
94 | } | |
e7c033c3 PB |
95 | } |
96 | ||
a94e87c0 JS |
97 | static inline size_t hbitmap_test_array_size(size_t bits) |
98 | { | |
99 | size_t n = (bits + BITS_PER_LONG - 1) / BITS_PER_LONG; | |
100 | return n ? n : 1; | |
101 | } | |
102 | ||
103 | static void hbitmap_test_truncate_impl(TestHBitmapData *data, | |
104 | size_t size) | |
105 | { | |
106 | size_t n; | |
107 | size_t m; | |
108 | data->old_size = data->size; | |
109 | data->size = size; | |
110 | ||
111 | if (data->size == data->old_size) { | |
112 | return; | |
113 | } | |
114 | ||
115 | n = hbitmap_test_array_size(size); | |
116 | m = hbitmap_test_array_size(data->old_size); | |
117 | data->bits = g_realloc(data->bits, sizeof(unsigned long) * n); | |
118 | if (n > m) { | |
119 | memset(&data->bits[m], 0x00, sizeof(unsigned long) * (n - m)); | |
120 | } | |
121 | ||
122 | /* If we shrink to an uneven multiple of sizeof(unsigned long), | |
123 | * scrub the leftover memory. */ | |
124 | if (data->size < data->old_size) { | |
125 | m = size % (sizeof(unsigned long) * 8); | |
126 | if (m) { | |
127 | unsigned long mask = (1ULL << m) - 1; | |
128 | data->bits[n-1] &= mask; | |
129 | } | |
130 | } | |
131 | ||
132 | hbitmap_truncate(data->hb, size); | |
133 | } | |
134 | ||
e7c033c3 PB |
135 | static void hbitmap_test_teardown(TestHBitmapData *data, |
136 | const void *unused) | |
137 | { | |
138 | if (data->hb) { | |
139 | hbitmap_free(data->hb); | |
140 | data->hb = NULL; | |
141 | } | |
142 | if (data->bits) { | |
143 | g_free(data->bits); | |
144 | data->bits = NULL; | |
145 | } | |
146 | } | |
147 | ||
148 | /* Set a range in the HBitmap and in the shadow "simple" bitmap. | |
149 | * The two bitmaps are then tested against each other. | |
150 | */ | |
151 | static void hbitmap_test_set(TestHBitmapData *data, | |
152 | uint64_t first, uint64_t count) | |
153 | { | |
154 | hbitmap_set(data->hb, first, count); | |
155 | while (count-- != 0) { | |
156 | size_t pos = first >> LOG_BITS_PER_LONG; | |
157 | int bit = first & (BITS_PER_LONG - 1); | |
158 | first++; | |
159 | ||
160 | data->bits[pos] |= 1UL << bit; | |
161 | } | |
162 | ||
163 | if (data->granularity == 0) { | |
164 | hbitmap_test_check(data, 0); | |
165 | } | |
166 | } | |
167 | ||
168 | /* Reset a range in the HBitmap and in the shadow "simple" bitmap. | |
169 | */ | |
170 | static void hbitmap_test_reset(TestHBitmapData *data, | |
171 | uint64_t first, uint64_t count) | |
172 | { | |
173 | hbitmap_reset(data->hb, first, count); | |
174 | while (count-- != 0) { | |
175 | size_t pos = first >> LOG_BITS_PER_LONG; | |
176 | int bit = first & (BITS_PER_LONG - 1); | |
177 | first++; | |
178 | ||
179 | data->bits[pos] &= ~(1UL << bit); | |
180 | } | |
181 | ||
182 | if (data->granularity == 0) { | |
183 | hbitmap_test_check(data, 0); | |
184 | } | |
185 | } | |
186 | ||
187 | static void hbitmap_test_check_get(TestHBitmapData *data) | |
188 | { | |
189 | uint64_t count = 0; | |
190 | uint64_t i; | |
191 | ||
192 | for (i = 0; i < data->size; i++) { | |
193 | size_t pos = i >> LOG_BITS_PER_LONG; | |
194 | int bit = i & (BITS_PER_LONG - 1); | |
195 | unsigned long val = data->bits[pos] & (1UL << bit); | |
196 | count += hbitmap_get(data->hb, i); | |
197 | g_assert_cmpint(hbitmap_get(data->hb, i), ==, val != 0); | |
198 | } | |
199 | g_assert_cmpint(count, ==, hbitmap_count(data->hb)); | |
200 | } | |
201 | ||
202 | static void test_hbitmap_zero(TestHBitmapData *data, | |
203 | const void *unused) | |
204 | { | |
205 | hbitmap_test_init(data, 0, 0); | |
206 | } | |
207 | ||
208 | static void test_hbitmap_unaligned(TestHBitmapData *data, | |
209 | const void *unused) | |
210 | { | |
211 | hbitmap_test_init(data, L3 + 23, 0); | |
212 | hbitmap_test_set(data, 0, 1); | |
213 | hbitmap_test_set(data, L3 + 22, 1); | |
214 | } | |
215 | ||
216 | static void test_hbitmap_iter_empty(TestHBitmapData *data, | |
217 | const void *unused) | |
218 | { | |
219 | hbitmap_test_init(data, L1, 0); | |
220 | } | |
221 | ||
222 | static void test_hbitmap_iter_partial(TestHBitmapData *data, | |
223 | const void *unused) | |
224 | { | |
225 | hbitmap_test_init(data, L3, 0); | |
226 | hbitmap_test_set(data, 0, L3); | |
227 | hbitmap_test_check(data, 1); | |
228 | hbitmap_test_check(data, L1 - 1); | |
229 | hbitmap_test_check(data, L1); | |
230 | hbitmap_test_check(data, L1 * 2 - 1); | |
231 | hbitmap_test_check(data, L2 - 1); | |
232 | hbitmap_test_check(data, L2); | |
233 | hbitmap_test_check(data, L2 + 1); | |
234 | hbitmap_test_check(data, L2 + L1); | |
235 | hbitmap_test_check(data, L2 + L1 * 2 - 1); | |
236 | hbitmap_test_check(data, L2 * 2 - 1); | |
237 | hbitmap_test_check(data, L2 * 2); | |
238 | hbitmap_test_check(data, L2 * 2 + 1); | |
239 | hbitmap_test_check(data, L2 * 2 + L1); | |
240 | hbitmap_test_check(data, L2 * 2 + L1 * 2 - 1); | |
241 | hbitmap_test_check(data, L3 / 2); | |
242 | } | |
243 | ||
e7c033c3 PB |
244 | static void test_hbitmap_set_all(TestHBitmapData *data, |
245 | const void *unused) | |
246 | { | |
247 | hbitmap_test_init(data, L3, 0); | |
248 | hbitmap_test_set(data, 0, L3); | |
249 | } | |
250 | ||
251 | static void test_hbitmap_get_all(TestHBitmapData *data, | |
252 | const void *unused) | |
253 | { | |
254 | hbitmap_test_init(data, L3, 0); | |
255 | hbitmap_test_set(data, 0, L3); | |
256 | hbitmap_test_check_get(data); | |
257 | } | |
258 | ||
259 | static void test_hbitmap_get_some(TestHBitmapData *data, | |
260 | const void *unused) | |
261 | { | |
262 | hbitmap_test_init(data, 2 * L2, 0); | |
263 | hbitmap_test_set(data, 10, 1); | |
264 | hbitmap_test_check_get(data); | |
265 | hbitmap_test_set(data, L1 - 1, 1); | |
266 | hbitmap_test_check_get(data); | |
267 | hbitmap_test_set(data, L1, 1); | |
268 | hbitmap_test_check_get(data); | |
269 | hbitmap_test_set(data, L2 - 1, 1); | |
270 | hbitmap_test_check_get(data); | |
271 | hbitmap_test_set(data, L2, 1); | |
272 | hbitmap_test_check_get(data); | |
273 | } | |
274 | ||
275 | static void test_hbitmap_set_one(TestHBitmapData *data, | |
276 | const void *unused) | |
277 | { | |
278 | hbitmap_test_init(data, 2 * L2, 0); | |
279 | hbitmap_test_set(data, 10, 1); | |
280 | hbitmap_test_set(data, L1 - 1, 1); | |
281 | hbitmap_test_set(data, L1, 1); | |
282 | hbitmap_test_set(data, L2 - 1, 1); | |
283 | hbitmap_test_set(data, L2, 1); | |
284 | } | |
285 | ||
286 | static void test_hbitmap_set_two_elem(TestHBitmapData *data, | |
287 | const void *unused) | |
288 | { | |
289 | hbitmap_test_init(data, 2 * L2, 0); | |
290 | hbitmap_test_set(data, L1 - 1, 2); | |
291 | hbitmap_test_set(data, L1 * 2 - 1, 4); | |
292 | hbitmap_test_set(data, L1 * 4, L1 + 1); | |
293 | hbitmap_test_set(data, L1 * 8 - 1, L1 + 1); | |
294 | hbitmap_test_set(data, L2 - 1, 2); | |
295 | hbitmap_test_set(data, L2 + L1 - 1, 8); | |
296 | hbitmap_test_set(data, L2 + L1 * 4, L1 + 1); | |
297 | hbitmap_test_set(data, L2 + L1 * 8 - 1, L1 + 1); | |
298 | } | |
299 | ||
300 | static void test_hbitmap_set(TestHBitmapData *data, | |
301 | const void *unused) | |
302 | { | |
303 | hbitmap_test_init(data, L3 * 2, 0); | |
304 | hbitmap_test_set(data, L1 - 1, L1 + 2); | |
305 | hbitmap_test_set(data, L1 * 3 - 1, L1 + 2); | |
306 | hbitmap_test_set(data, L1 * 5, L1 * 2 + 1); | |
307 | hbitmap_test_set(data, L1 * 8 - 1, L1 * 2 + 1); | |
308 | hbitmap_test_set(data, L2 - 1, L1 + 2); | |
309 | hbitmap_test_set(data, L2 + L1 * 2 - 1, L1 + 2); | |
310 | hbitmap_test_set(data, L2 + L1 * 4, L1 * 2 + 1); | |
311 | hbitmap_test_set(data, L2 + L1 * 7 - 1, L1 * 2 + 1); | |
312 | hbitmap_test_set(data, L2 * 2 - 1, L3 * 2 - L2 * 2); | |
313 | } | |
314 | ||
315 | static void test_hbitmap_set_twice(TestHBitmapData *data, | |
316 | const void *unused) | |
317 | { | |
318 | hbitmap_test_init(data, L1 * 3, 0); | |
319 | hbitmap_test_set(data, 0, L1 * 3); | |
320 | hbitmap_test_set(data, L1, 1); | |
321 | } | |
322 | ||
323 | static void test_hbitmap_set_overlap(TestHBitmapData *data, | |
324 | const void *unused) | |
325 | { | |
326 | hbitmap_test_init(data, L3 * 2, 0); | |
327 | hbitmap_test_set(data, L1 - 1, L1 + 2); | |
328 | hbitmap_test_set(data, L1 * 2 - 1, L1 * 2 + 2); | |
329 | hbitmap_test_set(data, 0, L1 * 3); | |
330 | hbitmap_test_set(data, L1 * 8 - 1, L2); | |
331 | hbitmap_test_set(data, L2, L1); | |
332 | hbitmap_test_set(data, L2 - L1 - 1, L1 * 8 + 2); | |
333 | hbitmap_test_set(data, L2, L3 - L2 + 1); | |
334 | hbitmap_test_set(data, L3 - L1, L1 * 3); | |
335 | hbitmap_test_set(data, L3 - 1, 3); | |
336 | hbitmap_test_set(data, L3 - 1, L2); | |
337 | } | |
338 | ||
339 | static void test_hbitmap_reset_empty(TestHBitmapData *data, | |
340 | const void *unused) | |
341 | { | |
342 | hbitmap_test_init(data, L3, 0); | |
343 | hbitmap_test_reset(data, 0, L3); | |
344 | } | |
345 | ||
346 | static void test_hbitmap_reset(TestHBitmapData *data, | |
347 | const void *unused) | |
348 | { | |
349 | hbitmap_test_init(data, L3 * 2, 0); | |
350 | hbitmap_test_set(data, L1 - 1, L1 + 2); | |
351 | hbitmap_test_reset(data, L1 * 2 - 1, L1 * 2 + 2); | |
352 | hbitmap_test_set(data, 0, L1 * 3); | |
353 | hbitmap_test_reset(data, L1 * 8 - 1, L2); | |
354 | hbitmap_test_set(data, L2, L1); | |
355 | hbitmap_test_reset(data, L2 - L1 - 1, L1 * 8 + 2); | |
356 | hbitmap_test_set(data, L2, L3 - L2 + 1); | |
357 | hbitmap_test_reset(data, L3 - L1, L1 * 3); | |
358 | hbitmap_test_set(data, L3 - 1, 3); | |
359 | hbitmap_test_reset(data, L3 - 1, L2); | |
360 | hbitmap_test_set(data, 0, L3 * 2); | |
361 | hbitmap_test_reset(data, 0, L1); | |
362 | hbitmap_test_reset(data, 0, L2); | |
363 | hbitmap_test_reset(data, L3, L3); | |
364 | hbitmap_test_set(data, L3 / 2, L3); | |
365 | } | |
366 | ||
367 | static void test_hbitmap_granularity(TestHBitmapData *data, | |
368 | const void *unused) | |
369 | { | |
370 | /* Note that hbitmap_test_check has to be invoked manually in this test. */ | |
371 | hbitmap_test_init(data, L1, 1); | |
372 | hbitmap_test_set(data, 0, 1); | |
373 | g_assert_cmpint(hbitmap_count(data->hb), ==, 2); | |
374 | hbitmap_test_check(data, 0); | |
375 | hbitmap_test_set(data, 2, 1); | |
376 | g_assert_cmpint(hbitmap_count(data->hb), ==, 4); | |
377 | hbitmap_test_check(data, 0); | |
378 | hbitmap_test_set(data, 0, 3); | |
379 | g_assert_cmpint(hbitmap_count(data->hb), ==, 4); | |
380 | hbitmap_test_reset(data, 0, 1); | |
381 | g_assert_cmpint(hbitmap_count(data->hb), ==, 2); | |
382 | } | |
383 | ||
384 | static void test_hbitmap_iter_granularity(TestHBitmapData *data, | |
385 | const void *unused) | |
386 | { | |
387 | HBitmapIter hbi; | |
388 | ||
389 | /* Note that hbitmap_test_check has to be invoked manually in this test. */ | |
390 | hbitmap_test_init(data, 131072 << 7, 7); | |
391 | hbitmap_iter_init(&hbi, data->hb, 0); | |
392 | g_assert_cmpint(hbitmap_iter_next(&hbi), <, 0); | |
393 | ||
394 | hbitmap_test_set(data, ((L2 + L1 + 1) << 7) + 8, 8); | |
395 | hbitmap_iter_init(&hbi, data->hb, 0); | |
396 | g_assert_cmpint(hbitmap_iter_next(&hbi), ==, (L2 + L1 + 1) << 7); | |
397 | g_assert_cmpint(hbitmap_iter_next(&hbi), <, 0); | |
398 | ||
399 | hbitmap_iter_init(&hbi, data->hb, (L2 + L1 + 2) << 7); | |
400 | g_assert_cmpint(hbitmap_iter_next(&hbi), <, 0); | |
401 | ||
402 | hbitmap_test_set(data, (131072 << 7) - 8, 8); | |
403 | hbitmap_iter_init(&hbi, data->hb, 0); | |
404 | g_assert_cmpint(hbitmap_iter_next(&hbi), ==, (L2 + L1 + 1) << 7); | |
405 | g_assert_cmpint(hbitmap_iter_next(&hbi), ==, 131071 << 7); | |
406 | g_assert_cmpint(hbitmap_iter_next(&hbi), <, 0); | |
407 | ||
408 | hbitmap_iter_init(&hbi, data->hb, (L2 + L1 + 2) << 7); | |
409 | g_assert_cmpint(hbitmap_iter_next(&hbi), ==, 131071 << 7); | |
410 | g_assert_cmpint(hbitmap_iter_next(&hbi), <, 0); | |
411 | } | |
412 | ||
a94e87c0 JS |
413 | static void hbitmap_test_set_boundary_bits(TestHBitmapData *data, ssize_t diff) |
414 | { | |
415 | size_t size = data->size; | |
416 | ||
417 | /* First bit */ | |
418 | hbitmap_test_set(data, 0, 1); | |
419 | if (diff < 0) { | |
420 | /* Last bit in new, shortened map */ | |
421 | hbitmap_test_set(data, size + diff - 1, 1); | |
422 | ||
423 | /* First bit to be truncated away */ | |
424 | hbitmap_test_set(data, size + diff, 1); | |
425 | } | |
426 | /* Last bit */ | |
427 | hbitmap_test_set(data, size - 1, 1); | |
428 | if (data->granularity == 0) { | |
429 | hbitmap_test_check_get(data); | |
430 | } | |
431 | } | |
432 | ||
433 | static void hbitmap_test_check_boundary_bits(TestHBitmapData *data) | |
434 | { | |
435 | size_t size = MIN(data->size, data->old_size); | |
436 | ||
437 | if (data->granularity == 0) { | |
438 | hbitmap_test_check_get(data); | |
439 | hbitmap_test_check(data, 0); | |
440 | } else { | |
441 | /* If a granularity was set, note that every distinct | |
442 | * (bit >> granularity) value that was set will increase | |
443 | * the bit pop count by 2^granularity, not just 1. | |
444 | * | |
445 | * The hbitmap_test_check facility does not currently tolerate | |
446 | * non-zero granularities, so test the boundaries and the population | |
447 | * count manually. | |
448 | */ | |
449 | g_assert(hbitmap_get(data->hb, 0)); | |
450 | g_assert(hbitmap_get(data->hb, size - 1)); | |
451 | g_assert_cmpint(2 << data->granularity, ==, hbitmap_count(data->hb)); | |
452 | } | |
453 | } | |
454 | ||
455 | /* Generic truncate test. */ | |
456 | static void hbitmap_test_truncate(TestHBitmapData *data, | |
457 | size_t size, | |
458 | ssize_t diff, | |
459 | int granularity) | |
460 | { | |
461 | hbitmap_test_init(data, size, granularity); | |
462 | hbitmap_test_set_boundary_bits(data, diff); | |
463 | hbitmap_test_truncate_impl(data, size + diff); | |
464 | hbitmap_test_check_boundary_bits(data); | |
465 | } | |
466 | ||
467 | static void test_hbitmap_truncate_nop(TestHBitmapData *data, | |
468 | const void *unused) | |
469 | { | |
470 | hbitmap_test_truncate(data, L2, 0, 0); | |
471 | } | |
472 | ||
473 | /** | |
474 | * Grow by an amount smaller than the granularity, without crossing | |
475 | * a granularity alignment boundary. Effectively a NOP. | |
476 | */ | |
477 | static void test_hbitmap_truncate_grow_negligible(TestHBitmapData *data, | |
478 | const void *unused) | |
479 | { | |
480 | size_t size = L2 - 1; | |
481 | size_t diff = 1; | |
482 | int granularity = 1; | |
483 | ||
484 | hbitmap_test_truncate(data, size, diff, granularity); | |
485 | } | |
486 | ||
487 | /** | |
488 | * Shrink by an amount smaller than the granularity, without crossing | |
489 | * a granularity alignment boundary. Effectively a NOP. | |
490 | */ | |
491 | static void test_hbitmap_truncate_shrink_negligible(TestHBitmapData *data, | |
492 | const void *unused) | |
493 | { | |
494 | size_t size = L2; | |
495 | ssize_t diff = -1; | |
496 | int granularity = 1; | |
497 | ||
498 | hbitmap_test_truncate(data, size, diff, granularity); | |
499 | } | |
500 | ||
501 | /** | |
502 | * Grow by an amount smaller than the granularity, but crossing over | |
503 | * a granularity alignment boundary. | |
504 | */ | |
505 | static void test_hbitmap_truncate_grow_tiny(TestHBitmapData *data, | |
506 | const void *unused) | |
507 | { | |
508 | size_t size = L2 - 2; | |
509 | ssize_t diff = 1; | |
510 | int granularity = 1; | |
511 | ||
512 | hbitmap_test_truncate(data, size, diff, granularity); | |
513 | } | |
514 | ||
515 | /** | |
516 | * Shrink by an amount smaller than the granularity, but crossing over | |
517 | * a granularity alignment boundary. | |
518 | */ | |
519 | static void test_hbitmap_truncate_shrink_tiny(TestHBitmapData *data, | |
520 | const void *unused) | |
521 | { | |
522 | size_t size = L2 - 1; | |
523 | ssize_t diff = -1; | |
524 | int granularity = 1; | |
525 | ||
526 | hbitmap_test_truncate(data, size, diff, granularity); | |
527 | } | |
528 | ||
529 | /** | |
530 | * Grow by an amount smaller than sizeof(long), and not crossing over | |
531 | * a sizeof(long) alignment boundary. | |
532 | */ | |
533 | static void test_hbitmap_truncate_grow_small(TestHBitmapData *data, | |
534 | const void *unused) | |
535 | { | |
536 | size_t size = L2 + 1; | |
537 | size_t diff = sizeof(long) / 2; | |
538 | ||
539 | hbitmap_test_truncate(data, size, diff, 0); | |
540 | } | |
541 | ||
542 | /** | |
543 | * Shrink by an amount smaller than sizeof(long), and not crossing over | |
544 | * a sizeof(long) alignment boundary. | |
545 | */ | |
546 | static void test_hbitmap_truncate_shrink_small(TestHBitmapData *data, | |
547 | const void *unused) | |
548 | { | |
549 | size_t size = L2; | |
550 | size_t diff = sizeof(long) / 2; | |
551 | ||
552 | hbitmap_test_truncate(data, size, -diff, 0); | |
553 | } | |
554 | ||
555 | /** | |
556 | * Grow by an amount smaller than sizeof(long), while crossing over | |
557 | * a sizeof(long) alignment boundary. | |
558 | */ | |
559 | static void test_hbitmap_truncate_grow_medium(TestHBitmapData *data, | |
560 | const void *unused) | |
561 | { | |
562 | size_t size = L2 - 1; | |
563 | size_t diff = sizeof(long) / 2; | |
564 | ||
565 | hbitmap_test_truncate(data, size, diff, 0); | |
566 | } | |
567 | ||
568 | /** | |
569 | * Shrink by an amount smaller than sizeof(long), while crossing over | |
570 | * a sizeof(long) alignment boundary. | |
571 | */ | |
572 | static void test_hbitmap_truncate_shrink_medium(TestHBitmapData *data, | |
573 | const void *unused) | |
574 | { | |
575 | size_t size = L2 + 1; | |
576 | size_t diff = sizeof(long) / 2; | |
577 | ||
578 | hbitmap_test_truncate(data, size, -diff, 0); | |
579 | } | |
580 | ||
581 | /** | |
582 | * Grow by an amount larger than sizeof(long). | |
583 | */ | |
584 | static void test_hbitmap_truncate_grow_large(TestHBitmapData *data, | |
585 | const void *unused) | |
586 | { | |
587 | size_t size = L2; | |
588 | size_t diff = 8 * sizeof(long); | |
589 | ||
590 | hbitmap_test_truncate(data, size, diff, 0); | |
591 | } | |
592 | ||
593 | /** | |
594 | * Shrink by an amount larger than sizeof(long). | |
595 | */ | |
596 | static void test_hbitmap_truncate_shrink_large(TestHBitmapData *data, | |
597 | const void *unused) | |
598 | { | |
599 | size_t size = L2; | |
600 | size_t diff = 8 * sizeof(long); | |
601 | ||
602 | hbitmap_test_truncate(data, size, -diff, 0); | |
603 | } | |
604 | ||
e7c033c3 PB |
605 | static void hbitmap_test_add(const char *testpath, |
606 | void (*test_func)(TestHBitmapData *data, const void *user_data)) | |
607 | { | |
608 | g_test_add(testpath, TestHBitmapData, NULL, NULL, test_func, | |
609 | hbitmap_test_teardown); | |
610 | } | |
611 | ||
612 | int main(int argc, char **argv) | |
613 | { | |
614 | g_test_init(&argc, &argv, NULL); | |
615 | hbitmap_test_add("/hbitmap/size/0", test_hbitmap_zero); | |
616 | hbitmap_test_add("/hbitmap/size/unaligned", test_hbitmap_unaligned); | |
617 | hbitmap_test_add("/hbitmap/iter/empty", test_hbitmap_iter_empty); | |
e7c033c3 PB |
618 | hbitmap_test_add("/hbitmap/iter/partial", test_hbitmap_iter_partial); |
619 | hbitmap_test_add("/hbitmap/iter/granularity", test_hbitmap_iter_granularity); | |
620 | hbitmap_test_add("/hbitmap/get/all", test_hbitmap_get_all); | |
621 | hbitmap_test_add("/hbitmap/get/some", test_hbitmap_get_some); | |
622 | hbitmap_test_add("/hbitmap/set/all", test_hbitmap_set_all); | |
623 | hbitmap_test_add("/hbitmap/set/one", test_hbitmap_set_one); | |
624 | hbitmap_test_add("/hbitmap/set/two-elem", test_hbitmap_set_two_elem); | |
625 | hbitmap_test_add("/hbitmap/set/general", test_hbitmap_set); | |
626 | hbitmap_test_add("/hbitmap/set/twice", test_hbitmap_set_twice); | |
627 | hbitmap_test_add("/hbitmap/set/overlap", test_hbitmap_set_overlap); | |
628 | hbitmap_test_add("/hbitmap/reset/empty", test_hbitmap_reset_empty); | |
629 | hbitmap_test_add("/hbitmap/reset/general", test_hbitmap_reset); | |
630 | hbitmap_test_add("/hbitmap/granularity", test_hbitmap_granularity); | |
a94e87c0 JS |
631 | |
632 | hbitmap_test_add("/hbitmap/truncate/nop", test_hbitmap_truncate_nop); | |
633 | hbitmap_test_add("/hbitmap/truncate/grow/negligible", | |
634 | test_hbitmap_truncate_grow_negligible); | |
635 | hbitmap_test_add("/hbitmap/truncate/shrink/negligible", | |
636 | test_hbitmap_truncate_shrink_negligible); | |
637 | hbitmap_test_add("/hbitmap/truncate/grow/tiny", | |
638 | test_hbitmap_truncate_grow_tiny); | |
639 | hbitmap_test_add("/hbitmap/truncate/shrink/tiny", | |
640 | test_hbitmap_truncate_shrink_tiny); | |
641 | hbitmap_test_add("/hbitmap/truncate/grow/small", | |
642 | test_hbitmap_truncate_grow_small); | |
643 | hbitmap_test_add("/hbitmap/truncate/shrink/small", | |
644 | test_hbitmap_truncate_shrink_small); | |
645 | hbitmap_test_add("/hbitmap/truncate/grow/medium", | |
646 | test_hbitmap_truncate_grow_medium); | |
647 | hbitmap_test_add("/hbitmap/truncate/shrink/medium", | |
648 | test_hbitmap_truncate_shrink_medium); | |
649 | hbitmap_test_add("/hbitmap/truncate/grow/large", | |
650 | test_hbitmap_truncate_grow_large); | |
651 | hbitmap_test_add("/hbitmap/truncate/shrink/large", | |
652 | test_hbitmap_truncate_shrink_large); | |
e7c033c3 PB |
653 | g_test_run(); |
654 | ||
655 | return 0; | |
656 | } |