From 8f77fd4f0a73a92087b185af0b45d70607268107 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Tue, 8 Jan 2019 08:45:45 +0100 Subject: Fixed a bug in bitfield comparison. --- src/common/bits.c | 4 ++-- tests/common/bitfield.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/common/bits.c b/src/common/bits.c index 7b49bb8..2714293 100644 --- a/src/common/bits.c +++ b/src/common/bits.c @@ -233,9 +233,9 @@ int compare_bit_fields(const bitfield_t *a, const bitfield_t *b) else if (a->length < b->length) result = -1; - if (result == 0) + else { - final = a->length % sizeof(unsigned long); + final = a->length % (8 * sizeof(unsigned long)); if (final == 0) final = ~0lu; diff --git a/tests/common/bitfield.py b/tests/common/bitfield.py index cb82580..717181c 100644 --- a/tests/common/bitfield.py +++ b/tests/common/bitfield.py @@ -132,3 +132,15 @@ class TestBitfields(ChrysalideTestCase): 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) -- cgit v0.11.2-87-g4458