test-bitmap.c 4.6 KB

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