2020-07-12 23:56:55 +08:00
|
|
|
import unittest
|
2017-03-30 17:49:03 +08:00
|
|
|
|
|
|
|
|
2020-07-12 23:56:55 +08:00
|
|
|
class TestBits(unittest.TestCase):
|
2017-03-30 17:49:03 +08:00
|
|
|
|
|
|
|
def test_get_next_largest(self):
|
|
|
|
bits = Bits()
|
2020-07-12 23:56:55 +08:00
|
|
|
self.assertRaises(Exception, bits.get_next_largest, None)
|
|
|
|
self.assertRaises(Exception, bits.get_next_largest, 0)
|
|
|
|
self.assertRaises(Exception, bits.get_next_largest, -1)
|
2017-03-30 17:49:03 +08:00
|
|
|
num = int('011010111', base=2)
|
|
|
|
expected = int('011011011', base=2)
|
2020-07-12 23:56:55 +08:00
|
|
|
self.assertEqual(bits.get_next_largest(num), expected)
|
2017-03-30 17:49:03 +08:00
|
|
|
print('Success: test_get_next_largest')
|
|
|
|
|
|
|
|
def test_get_next_smallest(self):
|
|
|
|
bits = Bits()
|
2020-07-12 23:56:55 +08:00
|
|
|
self.assertRaises(Exception, bits.get_next_smallest, None)
|
|
|
|
self.assertRaises(Exception, bits.get_next_smallest, 0)
|
|
|
|
self.assertRaises(Exception, bits.get_next_smallest, -1)
|
2017-03-30 17:49:03 +08:00
|
|
|
num = int('011010111', base=2)
|
|
|
|
expected = int('011001111', base=2)
|
2020-07-12 23:56:55 +08:00
|
|
|
self.assertEqual(bits.get_next_smallest(num), expected)
|
2017-03-30 17:49:03 +08:00
|
|
|
print('Success: test_get_next_smallest')
|
|
|
|
|
|
|
|
def main():
|
|
|
|
test = TestBits()
|
|
|
|
test.test_get_next_largest()
|
|
|
|
test.test_get_next_smallest()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-07-12 23:56:55 +08:00
|
|
|
main()
|