Debian Codegraph @

tests/linux_kernel/helpers/test_bitops.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 from drgn import Object
5 from drgn.helpers.linux.bitmap import bitmap_weight
6 from drgn.helpers.linux.bitops import for_each_clear_bit, for_each_set_bit, test_bit
7 from tests import MockProgramTestCase
8
9
10 class TestBitOps(MockProgramTestCase):
11 BITMAP = [0xB351BC986648A680, 0x80DDB6615A80BC63]
12 # fmt: off
13 SET_BITS = [
14 7, 9, 10, 13, 15, 19, 22, 25, 26, 29, 30, 35, 36, 39, 42, 43, 44, 45,
15 47, 48, 52, 54, 56, 57, 60, 61, 63, 64, 65, 69, 70, 74, 75, 76, 77, 79,
16 87, 89, 91, 92, 94, 96, 101, 102, 105, 106, 108, 109, 111, 112, 114,
17 115, 116, 118, 119, 127,
18 ]
19 CLEAR_BITS = [
20 0, 1, 2, 3, 4, 5, 6, 8, 11, 12, 14, 16, 17, 18, 20, 21, 23, 24, 27, 28,
21 31, 32, 33, 34, 37, 38, 40, 41, 46, 49, 50, 51, 53, 55, 58, 59, 62, 66,
22 67, 68, 71, 72, 73, 78, 80, 81, 82, 83, 84, 85, 86, 88, 90, 93, 95, 97,
23 98, 99, 100, 103, 104, 107, 110, 113, 117, 120, 121, 122, 123, 124,
24 125, 126,
25 ]
26 # fmt: on
27
28 def test_for_each_set_bit(self):
29 bitmap = Object(self.prog, "unsigned long [2]", self.BITMAP)
30 self.assertEqual(list(for_each_set_bit(bitmap, 128)), self.SET_BITS)
31 self.assertEqual(
32 list(for_each_set_bit(bitmap, 101)),
33 [bit for bit in self.SET_BITS if bit < 101],
34 )
35
36 def test_for_each_clear_bit(self):
37 bitmap = Object(self.prog, "unsigned long [2]", self.BITMAP)
38 self.assertEqual(list(for_each_clear_bit(bitmap, 128)), self.CLEAR_BITS)
39 self.assertEqual(
40 list(for_each_clear_bit(bitmap, 100)),
41 [bit for bit in self.CLEAR_BITS if bit < 100],
42 )
43
44 def test_test_bit(self):
45 bitmap = Object(self.prog, "unsigned long [2]", self.BITMAP)
46 for bit in self.SET_BITS:
47 self.assertTrue(test_bit(bit, bitmap))
48 for bit in self.CLEAR_BITS:
49 self.assertFalse(test_bit(bit, bitmap))
50
51 def test_bitmap_weight(self):
52 bitmap = Object(self.prog, "unsigned long [2]", self.BITMAP)
53 self.assertEqual(bitmap_weight(bitmap, 128), len(self.SET_BITS))
54 self.assertEqual(
55 bitmap_weight(bitmap, 101), sum(1 for bit in self.SET_BITS if bit < 101)
56 )