1*da668aa1SThomas Huth /*
2*da668aa1SThomas Huth * SPDX-License-Identifier: GPL-2.0-or-later
3*da668aa1SThomas Huth *
4*da668aa1SThomas Huth * Bitmap.c unit-tests.
5*da668aa1SThomas Huth *
6*da668aa1SThomas Huth * Copyright (C) 2019, Red Hat, Inc.
7*da668aa1SThomas Huth *
8*da668aa1SThomas Huth * Author: Peter Xu <peterx@redhat.com>
9*da668aa1SThomas Huth */
10*da668aa1SThomas Huth
11*da668aa1SThomas Huth #include "qemu/osdep.h"
12*da668aa1SThomas Huth #include "qemu/bitmap.h"
13*da668aa1SThomas Huth
14*da668aa1SThomas Huth #define BMAP_SIZE 1024
15*da668aa1SThomas Huth
check_bitmap_copy_with_offset(void)16*da668aa1SThomas Huth static void check_bitmap_copy_with_offset(void)
17*da668aa1SThomas Huth {
18*da668aa1SThomas Huth unsigned long *bmap1, *bmap2, *bmap3, total;
19*da668aa1SThomas Huth
20*da668aa1SThomas Huth bmap1 = bitmap_new(BMAP_SIZE);
21*da668aa1SThomas Huth bmap2 = bitmap_new(BMAP_SIZE);
22*da668aa1SThomas Huth bmap3 = bitmap_new(BMAP_SIZE);
23*da668aa1SThomas Huth
24*da668aa1SThomas Huth bmap1[0] = g_test_rand_int();
25*da668aa1SThomas Huth bmap1[1] = g_test_rand_int();
26*da668aa1SThomas Huth bmap1[2] = g_test_rand_int();
27*da668aa1SThomas Huth bmap1[3] = g_test_rand_int();
28*da668aa1SThomas Huth total = BITS_PER_LONG * 4;
29*da668aa1SThomas Huth
30*da668aa1SThomas Huth /* Shift 115 bits into bmap2 */
31*da668aa1SThomas Huth bitmap_copy_with_dst_offset(bmap2, bmap1, 115, total);
32*da668aa1SThomas Huth /* Shift another 85 bits into bmap3 */
33*da668aa1SThomas Huth bitmap_copy_with_dst_offset(bmap3, bmap2, 85, total + 115);
34*da668aa1SThomas Huth /* Shift back 200 bits back */
35*da668aa1SThomas Huth bitmap_copy_with_src_offset(bmap2, bmap3, 200, total);
36*da668aa1SThomas Huth
37*da668aa1SThomas Huth g_assert_cmpmem(bmap1, total / BITS_PER_LONG,
38*da668aa1SThomas Huth bmap2, total / BITS_PER_LONG);
39*da668aa1SThomas Huth
40*da668aa1SThomas Huth bitmap_clear(bmap1, 0, BMAP_SIZE);
41*da668aa1SThomas Huth /* Set bits in bmap1 are 100-245 */
42*da668aa1SThomas Huth bitmap_set(bmap1, 100, 145);
43*da668aa1SThomas Huth
44*da668aa1SThomas Huth /* Set bits in bmap2 are 60-205 */
45*da668aa1SThomas Huth bitmap_copy_with_src_offset(bmap2, bmap1, 40, 250);
46*da668aa1SThomas Huth g_assert_cmpint(find_first_bit(bmap2, 60), ==, 60);
47*da668aa1SThomas Huth g_assert_cmpint(find_next_zero_bit(bmap2, 205, 60), ==, 205);
48*da668aa1SThomas Huth g_assert(test_bit(205, bmap2) == 0);
49*da668aa1SThomas Huth
50*da668aa1SThomas Huth /* Set bits in bmap3 are 135-280 */
51*da668aa1SThomas Huth bitmap_copy_with_dst_offset(bmap3, bmap1, 35, 250);
52*da668aa1SThomas Huth g_assert_cmpint(find_first_bit(bmap3, 135), ==, 135);
53*da668aa1SThomas Huth g_assert_cmpint(find_next_zero_bit(bmap3, 280, 135), ==, 280);
54*da668aa1SThomas Huth g_assert(test_bit(280, bmap3) == 0);
55*da668aa1SThomas Huth
56*da668aa1SThomas Huth g_free(bmap1);
57*da668aa1SThomas Huth g_free(bmap2);
58*da668aa1SThomas Huth g_free(bmap3);
59*da668aa1SThomas Huth }
60*da668aa1SThomas Huth
61*da668aa1SThomas Huth typedef void (*bmap_set_func)(unsigned long *map, long i, long len);
bitmap_set_case(bmap_set_func set_func)62*da668aa1SThomas Huth static void bitmap_set_case(bmap_set_func set_func)
63*da668aa1SThomas Huth {
64*da668aa1SThomas Huth unsigned long *bmap;
65*da668aa1SThomas Huth int offset;
66*da668aa1SThomas Huth
67*da668aa1SThomas Huth bmap = bitmap_new(BMAP_SIZE);
68*da668aa1SThomas Huth
69*da668aa1SThomas Huth /* Set one bit at offset in second word */
70*da668aa1SThomas Huth for (offset = 0; offset <= BITS_PER_LONG; offset++) {
71*da668aa1SThomas Huth bitmap_clear(bmap, 0, BMAP_SIZE);
72*da668aa1SThomas Huth set_func(bmap, BITS_PER_LONG + offset, 1);
73*da668aa1SThomas Huth g_assert_cmpint(find_first_bit(bmap, 2 * BITS_PER_LONG),
74*da668aa1SThomas Huth ==, BITS_PER_LONG + offset);
75*da668aa1SThomas Huth g_assert_cmpint(find_next_zero_bit(bmap,
76*da668aa1SThomas Huth 3 * BITS_PER_LONG,
77*da668aa1SThomas Huth BITS_PER_LONG + offset),
78*da668aa1SThomas Huth ==, BITS_PER_LONG + offset + 1);
79*da668aa1SThomas Huth }
80*da668aa1SThomas Huth
81*da668aa1SThomas Huth /* Both Aligned, set bits [BITS_PER_LONG, 3*BITS_PER_LONG] */
82*da668aa1SThomas Huth set_func(bmap, BITS_PER_LONG, 2 * BITS_PER_LONG);
83*da668aa1SThomas Huth g_assert_cmpuint(bmap[1], ==, -1ul);
84*da668aa1SThomas Huth g_assert_cmpuint(bmap[2], ==, -1ul);
85*da668aa1SThomas Huth g_assert_cmpint(find_first_bit(bmap, BITS_PER_LONG), ==, BITS_PER_LONG);
86*da668aa1SThomas Huth g_assert_cmpint(find_next_zero_bit(bmap, 3 * BITS_PER_LONG, BITS_PER_LONG),
87*da668aa1SThomas Huth ==, 3 * BITS_PER_LONG);
88*da668aa1SThomas Huth
89*da668aa1SThomas Huth for (offset = 0; offset <= BITS_PER_LONG; offset++) {
90*da668aa1SThomas Huth bitmap_clear(bmap, 0, BMAP_SIZE);
91*da668aa1SThomas Huth /* End Aligned, set bits [BITS_PER_LONG - offset, 3*BITS_PER_LONG] */
92*da668aa1SThomas Huth set_func(bmap, BITS_PER_LONG - offset, 2 * BITS_PER_LONG + offset);
93*da668aa1SThomas Huth g_assert_cmpuint(bmap[1], ==, -1ul);
94*da668aa1SThomas Huth g_assert_cmpuint(bmap[2], ==, -1ul);
95*da668aa1SThomas Huth g_assert_cmpint(find_first_bit(bmap, BITS_PER_LONG),
96*da668aa1SThomas Huth ==, BITS_PER_LONG - offset);
97*da668aa1SThomas Huth g_assert_cmpint(find_next_zero_bit(bmap,
98*da668aa1SThomas Huth 3 * BITS_PER_LONG,
99*da668aa1SThomas Huth BITS_PER_LONG - offset),
100*da668aa1SThomas Huth ==, 3 * BITS_PER_LONG);
101*da668aa1SThomas Huth }
102*da668aa1SThomas Huth
103*da668aa1SThomas Huth for (offset = 0; offset <= BITS_PER_LONG; offset++) {
104*da668aa1SThomas Huth bitmap_clear(bmap, 0, BMAP_SIZE);
105*da668aa1SThomas Huth /* Start Aligned, set bits [BITS_PER_LONG, 3*BITS_PER_LONG + offset] */
106*da668aa1SThomas Huth set_func(bmap, BITS_PER_LONG, 2 * BITS_PER_LONG + offset);
107*da668aa1SThomas Huth g_assert_cmpuint(bmap[1], ==, -1ul);
108*da668aa1SThomas Huth g_assert_cmpuint(bmap[2], ==, -1ul);
109*da668aa1SThomas Huth g_assert_cmpint(find_first_bit(bmap, BITS_PER_LONG),
110*da668aa1SThomas Huth ==, BITS_PER_LONG);
111*da668aa1SThomas Huth g_assert_cmpint(find_next_zero_bit(bmap,
112*da668aa1SThomas Huth 3 * BITS_PER_LONG + offset,
113*da668aa1SThomas Huth BITS_PER_LONG),
114*da668aa1SThomas Huth ==, 3 * BITS_PER_LONG + offset);
115*da668aa1SThomas Huth }
116*da668aa1SThomas Huth
117*da668aa1SThomas Huth g_free(bmap);
118*da668aa1SThomas Huth }
119*da668aa1SThomas Huth
check_bitmap_set(void)120*da668aa1SThomas Huth static void check_bitmap_set(void)
121*da668aa1SThomas Huth {
122*da668aa1SThomas Huth bitmap_set_case(bitmap_set);
123*da668aa1SThomas Huth bitmap_set_case(bitmap_set_atomic);
124*da668aa1SThomas Huth }
125*da668aa1SThomas Huth
main(int argc,char ** argv)126*da668aa1SThomas Huth int main(int argc, char **argv)
127*da668aa1SThomas Huth {
128*da668aa1SThomas Huth g_test_init(&argc, &argv, NULL);
129*da668aa1SThomas Huth
130*da668aa1SThomas Huth g_test_add_func("/bitmap/bitmap_copy_with_offset",
131*da668aa1SThomas Huth check_bitmap_copy_with_offset);
132*da668aa1SThomas Huth g_test_add_func("/bitmap/bitmap_set",
133*da668aa1SThomas Huth check_bitmap_set);
134*da668aa1SThomas Huth
135*da668aa1SThomas Huth g_test_run();
136*da668aa1SThomas Huth
137*da668aa1SThomas Huth return 0;
138*da668aa1SThomas Huth }
139