Skip to content

Commit

Permalink
added tests for Util bit functions
Browse files Browse the repository at this point in the history
  • Loading branch information
toomanybrians committed Oct 17, 2023
1 parent 5dfd032 commit 043d899
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions mpf/core/utility_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,11 +802,12 @@ def set_bit(hex_string, bit):
bit (_type_): Bit to set, e.g. 3
Returns:
_type_: Returns the hex string with the bit set, e.g. '89'
_type_: Returns the hex string with the bit set, e.g. '89'.
The return string will be the same length as the input string.
"""
num = int(hex_string, 16)
num |= 1 << bit
return Util.int_to_hex_string(num)
return Util.int_to_hex_string(num).zfill(len(hex_string))

@staticmethod
def clear_bit(hex_string, bit):
Expand Down
8 changes: 8 additions & 0 deletions mpf/tests/test_Utility_Functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,11 @@ def test_power_of_2(self):
self.assertTrue(Util.is_power2(256))
self.assertFalse(Util.is_power2(3))
self.assertFalse(Util.is_power2(222))

def test_bit_functions(self):
self.assertEqual(Util.set_bit('00', 0), '01')
self.assertEqual(Util.set_bit('02', 2), '06')
self.assertEqual(Util.clear_bit('91', 4), '81')
self.assertEqual(Util.clear_bit('81', 3), '81') # no change
self.assertEqual(Util.check_bit('01', 0), True)
self.assertEqual(Util.check_bit('91', 5), False)

0 comments on commit 043d899

Please sign in to comment.