diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2023-01-03 14:37:09 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2023-01-03 14:37:09 (GMT) |
commit | 7b739f256cb88cb627954d4a1ff04bdb4fc49515 (patch) | |
tree | e0e2ecd90f8004d44f320c78a69edbe37d6169a6 /tests | |
parent | 755acfaa09e08a355b29081b75f9d547d264ce53 (diff) |
Fix some mistakes in bitfield operations.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/common/bitfield.py | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/common/bitfield.py b/tests/common/bitfield.py index 8c4a470..aff5de6 100644 --- a/tests/common/bitfield.py +++ b/tests/common/bitfield.py @@ -106,6 +106,19 @@ class TestBitFields(ChrysalideTestCase): self.assertFalse(bf_a.test(67)) + bf_a = BitField(75, 0) + + bf_a.or_at(bf_b, 60) + + self.assertFalse(bf_a.test(59)) + + self.assertTrue(bf_a.test(60)) + self.assertTrue(bf_a.test(61)) + self.assertFalse(bf_a.test(62)) + self.assertTrue(bf_a.test(63)) + + self.assertFalse(bf_a.test(64)) + def testBitFieldSwitch(self): """Switch various bits in bitfields.""" @@ -216,3 +229,57 @@ class TestBitFields(ChrysalideTestCase): bf_b = BitField(9, 1) self.assertNotEqual(bf_a, bf_b) + + + def testRealCase00(self): + """Test bits in bitfields against other bitfields in a real case (#02).""" + + bf = BitField(128, 0) + + for b in [ 0, 50, 54, 58, 66, 70, 98 ]: + bf.set(b, 1) + + mask = BitField(128, 0) + + for b in [ 0, 51 ]: + mask.set(b, 1) + + self.assertFalse(bf.test_zeros_with(0, mask)) + + self.assertTrue(bf.test_zeros_with(1, mask)) + + bf = BitField(32, 0) + + mask = BitField(32, 0) + + self.assertTrue(bf.test_zeros_with(0, mask)) + + for b in [ 0, 8, 9, 10 ]: + mask.set(b, 1) + + self.assertTrue(bf.test_zeros_with(0, mask)) + + bf = BitField(32, 1) + + self.assertFalse(bf.test_zeros_with(0, mask)) + + self.assertTrue(bf.test_ones_with(0, mask)) + + + def testRealCase01(self): + """Test bits in bitfields against other bitfields in a real case (#01).""" + + bf = BitField(128, 0) + + mask = BitField(128, 0) + + bits = [ 0, 50, 54, 58, 66, 70, 98 ] + + for b in bits: + mask.set(b, 1) + + bf.or_at(mask, 0) + + self.assertEqual(mask.popcount, len(bits)) + + self.assertEqual(mask.popcount, bf.popcount) |