2020-07-12 23:56:55 +08:00
|
|
|
import unittest
|
2017-03-30 17:49:29 +08:00
|
|
|
|
|
|
|
|
2020-07-12 23:56:55 +08:00
|
|
|
class TestBits(unittest.TestCase):
|
2017-03-30 17:49:29 +08:00
|
|
|
|
|
|
|
def test_flip_bit(self):
|
|
|
|
bits = Bits()
|
2020-07-12 23:56:55 +08:00
|
|
|
self.assertRaises(TypeError, bits.flip_bit, None)
|
|
|
|
self.assertEqual(bits.flip_bit(0), 1)
|
|
|
|
self.assertEqual(bits.flip_bit(-1), bits.MAX_BITS)
|
2017-03-30 17:49:29 +08:00
|
|
|
num = int('00001111110111011110001111110000', base=2)
|
|
|
|
expected = 10
|
2020-07-12 23:56:55 +08:00
|
|
|
self.assertEqual(bits.flip_bit(num), expected)
|
2017-03-30 17:49:29 +08:00
|
|
|
num = int('00000100111011101111100011111011', base=2)
|
|
|
|
expected = 9
|
2020-07-12 23:56:55 +08:00
|
|
|
self.assertEqual(bits.flip_bit(num), expected)
|
2017-03-30 17:49:29 +08:00
|
|
|
num = int('00010011101110111110001111101111', base=2)
|
|
|
|
expected = 10
|
2020-07-12 23:56:55 +08:00
|
|
|
self.assertEqual(bits.flip_bit(num), expected)
|
2017-03-30 17:49:29 +08:00
|
|
|
print('Success: test_print_binary')
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
test = TestBits()
|
|
|
|
test.test_flip_bit()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-07-12 23:56:55 +08:00
|
|
|
main()
|