summaryrefslogtreecommitdiff
path: root/tests/common/bitfield.py
blob: e014111a8d193dc8afd0050e91d49ac7dd82d4fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

from chrysacase import ChrysalideTestCase
from pychrysalide.common import BitField


class TestBitFields(ChrysalideTestCase):
    """TestCase for common.BitField*"""

    def testDuplicateBitField(self):
        """Check duplicated bitfield value."""

        bf = BitField(10, 0)

        bf2 = bf.dup()

        self.assertEqual(bf, bf2)

        self.assertEqual(bf.size, bf2.size)

        self.assertEqual(bf.popcount, bf2.popcount)


    def testBitFieldValues(self):
        """Evaluate bitfields basic values."""

        bf_a = BitField(75, 1)

        bf_b = BitField(75, 0)

        self.assertNotEqual(bf_a, bf_b)

        bf_a = BitField(75, 1)

        bf_b = BitField(75, 0)
        bf_b.set_all()

        self.assertEqual(bf_a, bf_b)

        self.assertEqual(bf_a.popcount, bf_b.popcount)

        bf_a = BitField(75, 1)
        bf_a.reset_all()

        bf_b = BitField(75, 0)

        self.assertEqual(bf_a, bf_b)

        self.assertEqual(bf_a.popcount, bf_b.popcount)


    def testBitFieldLogicalOperations(self):
        """Perform logical operations on bitfields."""

        bf_a = BitField(75, 1)

        bf_b = BitField(75, 0)

        self.assertEqual(bf_a.size, bf_b.size)

        bf_f = bf_a & bf_b

        self.assertEqual(bf_f, bf_b)

        self.assertEqual(bf_f.popcount, bf_b.popcount)

        bf_f = bf_a | bf_b

        self.assertEqual(bf_f, bf_a)

        self.assertEqual(bf_f.popcount, bf_a.popcount)


    def testBitFieldSwitch(self):
        """Switch various bits in bitfields."""

        bf_1 = BitField(75, 1)

        bf_0 = BitField(75, 0)

        bf_t = BitField(75, 0)

        for i in range(75):
            bf_t.set(i, 1)

        self.assertEqual(bf_t, bf_1)

        self.assertEqual(bf_t.popcount, bf_1.popcount)

        for i in range(75):
            bf_t.reset(i, 1)

        self.assertEqual(bf_t, bf_0)

        self.assertEqual(bf_t.popcount, bf_0.popcount)


    def testBitFieldBits(self):
        """Test bits in bitfields."""

        bf = BitField(54, 1)

        self.assertTrue(bf.test(0))

        self.assertTrue(bf.test(53))

        self.assertTrue(bf.test_all(0, 54))

        self.assertFalse(bf.test_none(0, 54))

        bf = BitField(54, 0)

        self.assertFalse(bf.test(0))

        self.assertFalse(bf.test(53))

        self.assertFalse(bf.test_all(0, 54))

        self.assertTrue(bf.test_none(0, 54))


    def testPopCountForBitField(self):
        """Count bits set to 1 in bitfield."""

        bf = BitField(65, 1)

        self.assertEqual(bf.size, 65)

        self.assertEqual(bf.popcount, 65)


    def testBitFieldComparison(self):
        """Check bitfield comparison."""

        bf_a = BitField(9, 0)
        bf_a.set(0, 1)
        bf_a.set(5, 1)

        bf_b = BitField(9, 1)

        self.assertNotEqual(bf_a, bf_b)