2020-07-12 11:56:55 -04:00
|
|
|
import unittest
|
2017-03-30 05:43:38 -04:00
|
|
|
|
|
|
|
|
2020-07-12 11:56:55 -04:00
|
|
|
class TestBit(unittest.TestCase):
|
2017-03-30 05:43:38 -04:00
|
|
|
|
|
|
|
def test_bit(self):
|
|
|
|
number = int('10001110', base=2)
|
|
|
|
bit = Bit(number)
|
2020-07-12 11:56:55 -04:00
|
|
|
self.assertEqual(bit.get_bit(index=3), True)
|
2017-03-30 05:43:38 -04:00
|
|
|
expected = int('10011110', base=2)
|
2020-07-12 11:56:55 -04:00
|
|
|
self.assertEqual(bit.set_bit(index=4), expected)
|
2017-03-30 05:43:38 -04:00
|
|
|
bit = Bit(number)
|
|
|
|
expected = int('10000110', base=2)
|
2020-07-12 11:56:55 -04:00
|
|
|
self.assertEqual(bit.clear_bit(index=3), expected)
|
2017-03-30 05:43:38 -04:00
|
|
|
bit = Bit(number)
|
|
|
|
expected = int('00000110', base=2)
|
2020-07-12 11:56:55 -04:00
|
|
|
self.assertEqual(bit.clear_bits_msb_to_index(index=3), expected)
|
2017-03-30 05:43:38 -04:00
|
|
|
bit = Bit(number)
|
|
|
|
expected = int('10000000', base=2)
|
2020-07-12 11:56:55 -04:00
|
|
|
self.assertEqual(bit.clear_bits_index_to_lsb(index=3), expected)
|
2017-03-30 05:43:38 -04:00
|
|
|
bit = Bit(number)
|
2020-07-12 11:56:55 -04:00
|
|
|
self.assertEqual(bit.update_bit(index=3, value=1), number)
|
2017-03-30 05:43:38 -04:00
|
|
|
bit = Bit(number)
|
|
|
|
expected = int('10000110', base=2)
|
2020-07-12 11:56:55 -04:00
|
|
|
self.assertEqual(bit.update_bit(index=3, value=0), expected)
|
2017-03-30 05:43:38 -04:00
|
|
|
bit = Bit(number)
|
|
|
|
expected = int('10001111', base=2)
|
2020-07-12 11:56:55 -04:00
|
|
|
self.assertEqual(bit.update_bit(index=0, value=1), expected)
|
2017-03-30 05:43:38 -04:00
|
|
|
print('Success: test_bit')
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
test = TestBit()
|
|
|
|
test.test_bit()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-07-12 11:56:55 -04:00
|
|
|
main()
|