summaryrefslogtreecommitdiff
path: root/tests/common/bitfield.py
blob: ec61291a97116ae247e4cf084b063c82fd7823fb (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
#!/usr/bin/python3-dbg
# -*- coding: utf-8 -*-


# Tests minimalistes pour valider la construction de chemins relatifs et absolus.


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)


    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)

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

        bf_b = bitfield(75, 0)

        self.assertEqual(bf_a, bf_b)


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

        bf_a = bitfield(75, 1)

        bf_b = bitfield(75, 0)

        bf_f = bf_a & bf_b

        self.assertEqual(bf_f, bf_b)

        bf_f = bf_a | bf_b

        self.assertEqual(bf_f, bf_a)


    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)

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

        self.assertEqual(bf_t, bf_0)


    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))