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
141
142
143
144
145
146
|
#!/usr/bin/python3-dbg
# -*- coding: utf-8 -*-
# Tests pour valider la manipulation des champs de bits.
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)
|