Skip to content

Commit 40a7b47

Browse files
kurtbrosemahmoud
andauthored
bugfix in mathutils.Bits.as_list (#315)
* bugfix in mathutils.Bits.as_list list(Bits(0, 2)) == [False, False] -- which is the correct behavior Bits(0, 2).as_list() == [False] -- which was a bug * DRY internal function and avoid blowing up length in tests * fix tests * bytes and hex both pad to 1 byte so combine test * fix roundtripping tests --------- Co-authored-by: Mahmoud Hashemi <[email protected]>
1 parent 895f7c4 commit 40a7b47

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

boltons/mathutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def __hash__(self):
215215
return hash(self.val)
216216

217217
def as_list(self):
218-
return [c == '1' for c in '{0:b}'.format(self.val)]
218+
return [c == '1' for c in self.as_bin()]
219219

220220
def as_bin(self):
221221
return '{{0:0{0}b}}'.format(self.len).format(self.val)

tests/test_mathutils.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,19 @@ def chk(a, b):
9595
chk(Bits('11') >> 1, Bits('1'))
9696
chk(Bits('1') << 1, Bits('10'))
9797
assert Bits('0') != Bits('00')
98+
# test roundtrip as_/from_hex
99+
chk(Bits.from_hex(Bits('10101010').as_hex()),
100+
Bits('10101010'))
101+
# test roundtrip as_/from_bytes
98102
chk(
99-
Bits.from_bytes(
100-
Bits.from_int(
101-
Bits.from_hex(
102-
Bits.from_bin(
103-
Bits.from_list(
104-
Bits('101').as_list()
105-
).as_bin()
106-
).as_hex()
107-
).as_int()
108-
).as_bytes()
109-
), Bits('00000101'))
103+
Bits.from_bytes(Bits('10101010').as_bytes()),
104+
Bits('10101010'))
105+
# pile of roundtripping
106+
chk(Bits.from_int(
107+
Bits.from_bin(
108+
Bits.from_list(
109+
Bits('101').as_list()
110+
).as_bin()
111+
).as_int()
112+
),
113+
Bits('101'))

0 commit comments

Comments
 (0)