Merge remote-tracking branch 'remotes/maxreitz/tags/pull-block-2019-07-15' into staging
[qemu/ar7.git] / tests / test-bitmap.c
blobcb7c5e462df64031f3922efafe15580324fb9289
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
4 * Bitmap.c unit-tests.
6 * Copyright (C) 2019, Red Hat, Inc.
8 * Author: Peter Xu <peterx@redhat.com>
9 */
11 #include <stdlib.h>
12 #include "qemu/osdep.h"
13 #include "qemu/bitmap.h"
15 #define BMAP_SIZE 1024
17 static void check_bitmap_copy_with_offset(void)
19 unsigned long *bmap1, *bmap2, *bmap3, total;
21 bmap1 = bitmap_new(BMAP_SIZE);
22 bmap2 = bitmap_new(BMAP_SIZE);
23 bmap3 = bitmap_new(BMAP_SIZE);
25 bmap1[0] = random();
26 bmap1[1] = random();
27 bmap1[2] = random();
28 bmap1[3] = random();
29 total = BITS_PER_LONG * 4;
31 /* Shift 115 bits into bmap2 */
32 bitmap_copy_with_dst_offset(bmap2, bmap1, 115, total);
33 /* Shift another 85 bits into bmap3 */
34 bitmap_copy_with_dst_offset(bmap3, bmap2, 85, total + 115);
35 /* Shift back 200 bits back */
36 bitmap_copy_with_src_offset(bmap2, bmap3, 200, total);
38 g_assert_cmpmem(bmap1, total / BITS_PER_LONG,
39 bmap2, total / BITS_PER_LONG);
41 bitmap_clear(bmap1, 0, BMAP_SIZE);
42 /* Set bits in bmap1 are 100-245 */
43 bitmap_set(bmap1, 100, 145);
45 /* Set bits in bmap2 are 60-205 */
46 bitmap_copy_with_src_offset(bmap2, bmap1, 40, 250);
47 g_assert_cmpint(find_first_bit(bmap2, 60), ==, 60);
48 g_assert_cmpint(find_next_zero_bit(bmap2, 205, 60), ==, 205);
49 g_assert(test_bit(205, bmap2) == 0);
51 /* Set bits in bmap3 are 135-280 */
52 bitmap_copy_with_dst_offset(bmap3, bmap1, 35, 250);
53 g_assert_cmpint(find_first_bit(bmap3, 135), ==, 135);
54 g_assert_cmpint(find_next_zero_bit(bmap3, 280, 135), ==, 280);
55 g_assert(test_bit(280, bmap3) == 0);
57 g_free(bmap1);
58 g_free(bmap2);
59 g_free(bmap3);
62 int main(int argc, char **argv)
64 g_test_init(&argc, &argv, NULL);
66 g_test_add_func("/bitmap/bitmap_copy_with_offset",
67 check_bitmap_copy_with_offset);
69 g_test_run();
71 return 0;