initialize seed random seedtest def testaddbitwiseoperatorself self assertequal5432 97823 addbitwiseoperator5432 97823 self assertequal0 addbitwiseoperator0 0 self assertequal10 addbitwiseoperator10 0 self assertequal10 addbitwiseoperator0 10 def testcountonesrecurself 8 1000 self assertequal1 countonesrecur8 109 1101101 self assertequal5 countonesrecur109 63 111111 self assertequal6 countonesrecur63 0 0 self assertequal0 countonesrecur0 def testcountonesiterself 8 1000 self assertequal1 countonesiter8 109 1101101 self assertequal5 countonesiter109 63 111111 self assertequal6 countonesiter63 0 0 self assertequal0 countonesiter0 def testcountflipstoconvertself 29 11101 and 15 01111 self assertequal2 countflipstoconvert29 15 45 0000101101 and 987 1111011011 self assertequal8 countflipstoconvert45 987 34 100010 self assertequal0 countflipstoconvert34 34 34 100010 and 53 110101 self assertequal4 countflipstoconvert34 53 def testfindmissingnumberself self assertequal7 findmissingnumber4 1 3 0 6 5 2 self assertequal0 findmissingnumber1 self assertequal1 findmissingnumber0 nums i for i in range100000 if i 12345 random shufflenums self assertequal12345 findmissingnumbernums def testfindmissingnumber2self self assertequal7 findmissingnumber24 1 3 0 6 5 2 self assertequal0 findmissingnumber21 self assertequal1 findmissingnumber20 nums i for i in range100000 if i 12345 random shufflenums self assertequal12345 findmissingnumber2nums def testflipbitlongestseqself 1775 11011101111 self assertequal8 flipbitlongestseq1775 5 101 self assertequal3 flipbitlongestseq5 71 1000111 self assertequal4 flipbitlongestseq71 0 0 self assertequal1 flipbitlongestseq0 def testispoweroftwoself self asserttrueispoweroftwo64 self assertfalseispoweroftwo91 self asserttrueispoweroftwo21001 self asserttrueispoweroftwo1 self assertfalseispoweroftwo0 def testreversebitsself self assertequal43261596 reversebits964176192 self assertequal964176192 reversebits43261596 self assertequal1 reversebits2147483648 bin0 00000000000000000000000000000000 self assertequal0 reversebits0 bin232 1 11111111111111111111111111111111 self assertequal232 1 reversebits232 1 def testsinglenumberself random seed test self assertequal0 singlenumber1 0 2 1 2 3 3 self assertequal101 singlenumber101 single random randint1 100000 nums random randint1 100000 for in range1000 nums 2 nums contains pairs of random integers nums appendsingle random shufflenums self assertequalsingle singlenumbernums def testsinglenumber2self self assertequal3 singlenumber24 2 3 2 1 1 4 2 4 1 single random randint1 100000 nums random randint1 100000 for in range1000 nums 3 nums contains triplets of random integers nums appendsingle random shufflenums self assertequalsingle singlenumber2nums def testsinglenumber3self self assertequalsorted2 5 sortedsinglenumber32 1 5 6 6 1 self assertequalsorted4 3 sortedsinglenumber39 9 4 3 def testsubsetsself self assertsetequalsubsets1 2 3 1 2 3 1 2 1 3 2 3 1 2 3 self assertsetequalsubsets10 20 30 40 10 40 10 20 40 10 30 10 20 30 40 40 10 30 40 30 20 30 30 40 10 10 20 20 40 20 30 40 10 20 30 20 def testgetbitself 22 10110 self assertequal1 getbit22 2 self assertequal0 getbit22 3 def testsetbitself 22 10110 after set bit at 3th position 30 11110 self assertequal30 setbit22 3 def testclearbitself 22 10110 after clear bit at 2nd position 20 10010 self assertequal18 clearbit22 2 def testupdatebitself 22 10110 after update bit at 3th position with value 1 30 11110 self assertequal30 updatebit22 3 1 22 10110 after update bit at 2nd position with value 0 20 10010 self assertequal18 updatebit22 2 0 def testinttobytesbigendianself self assertequalb x11 inttobytesbigendian17 def testinttobyteslittleendianself self assertequalb x11 inttobyteslittleendian17 def testbytesbigendiantointself self assertequal17 bytesbigendiantointb x11 def testbyteslittleendiantointself self assertequal17 byteslittleendiantointb x11 def testswappairself 22 10110 41 101001 self assertequal41 swappair22 10 1010 5 0101 self assertequal5 swappair10 def testfinddifferenceself self assertequal e finddifferenceabcd abecd def testhasalternativebitself self asserttruehasalternativebit5 self assertfalsehasalternativebit7 self assertfalsehasalternativebit11 self asserttruehasalternativebit10 def testhasalternativebitfastself self asserttruehasalternativebitfast5 self assertfalsehasalternativebitfast7 self assertfalsehasalternativebitfast11 self asserttruehasalternativebitfast10 def testinsertonebitself self assertequal45 insertonebit21 1 2 self assertequal41 insertonebit21 0 2 self assertequal53 insertonebit21 1 5 self assertequal43 insertonebit21 1 0 def testinsertmultbitsself self assertequal47 insertmultbits5 7 3 1 self assertequal47 insertmultbits5 7 3 0 self assertequal61 insertmultbits5 7 3 3 def testremovebitself self assertequal9 removebit21 2 self assertequal5 removebit21 4 self assertequal10 removebit21 0 def testbinarygapself 22 10110 self assertequal2 binarygap22 6 110 self assertequal1 binarygap6 8 1000 self assertequal0 binarygap8 145 10010001 self assertequal4 binarygap145 if name main unittest main initialize seed 8 1000 109 1101101 63 111111 0 0 8 1000 109 1101101 63 111111 0 0 29 11101 and 15 01111 45 0000101101 and 987 1111011011 34 100010 34 100010 and 53 110101 1775 11011101111 5 101 71 1000111 0 0 bin 0 00000000000000000000000000000000 bin 2 32 1 11111111111111111111111111111111 nums contains pairs of random integers nums contains triplets of random integers 22 10110 22 10110 after set bit at 3th position 30 11110 22 10110 after clear bit at 2nd position 20 10010 22 10110 after update bit at 3th position with value 1 30 11110 22 10110 after update bit at 2nd position with value 0 20 10010 22 10110 41 101001 10 1010 5 0101 input num 10101 21 insert_one_bit num 1 2 101101 45 insert_one_bit num 0 2 101001 41 insert_one_bit num 1 5 110101 53 insert_one_bit num 1 0 101010 42 input num 101 5 insert_mult_bits num 7 3 1 101111 47 insert_mult_bits num 7 3 0 101111 47 insert_mult_bits num 7 3 3 111101 61 input num 10101 21 remove_bit num 2 output 1001 9 remove_bit num 4 output 101 5 remove_bit num 0 output 1010 10 22 10110 6 110 8 1000 145 10010001
from algorithms.bit import (
    add_bitwise_operator,
    count_ones_iter, count_ones_recur,
    count_flips_to_convert,
    find_missing_number, find_missing_number2,
    flip_bit_longest_seq,
    is_power_of_two,
    reverse_bits,
    single_number,
    single_number2,
    single_number3,
    subsets,
    get_bit, set_bit, clear_bit, update_bit,
    int_to_bytes_big_endian, int_to_bytes_little_endian,
    bytes_big_endian_to_int, bytes_little_endian_to_int,
    swap_pair,
    find_difference,
    has_alternative_bit, has_alternative_bit_fast,
    insert_one_bit, insert_mult_bits,
    remove_bit,
    binary_gap
)

import unittest
import random


class TestSuite(unittest.TestCase):

    def setUp(self):
        
        random.seed("test")

    def test_add_bitwise_operator(self):
        self.assertEqual(5432 + 97823, add_bitwise_operator(5432, 97823))
        self.assertEqual(0, add_bitwise_operator(0, 0))
        self.assertEqual(10, add_bitwise_operator(10, 0))
        self.assertEqual(10, add_bitwise_operator(0, 10))

    def test_count_ones_recur(self):

        
        self.assertEqual(1, count_ones_recur(8))

        
        self.assertEqual(5, count_ones_recur(109))

        
        self.assertEqual(6, count_ones_recur(63))

        
        self.assertEqual(0, count_ones_recur(0))

    def test_count_ones_iter(self):

        
        self.assertEqual(1, count_ones_iter(8))

        
        self.assertEqual(5, count_ones_iter(109))

        
        self.assertEqual(6, count_ones_iter(63))

        
        self.assertEqual(0, count_ones_iter(0))

    def test_count_flips_to_convert(self):
        
        self.assertEqual(2, count_flips_to_convert(29, 15))
        
        self.assertEqual(8, count_flips_to_convert(45, 987))
        
        self.assertEqual(0, count_flips_to_convert(34, 34))
        
        self.assertEqual(4, count_flips_to_convert(34, 53))

    def test_find_missing_number(self):

        self.assertEqual(7, find_missing_number([4, 1, 3, 0, 6, 5, 2]))
        self.assertEqual(0, find_missing_number([1]))
        self.assertEqual(1, find_missing_number([0]))

        nums = [i for i in range(100000) if i != 12345]
        random.shuffle(nums)
        self.assertEqual(12345, find_missing_number(nums))

    def test_find_missing_number2(self):

        self.assertEqual(7, find_missing_number2([4, 1, 3, 0, 6, 5, 2]))
        self.assertEqual(0, find_missing_number2([1]))
        self.assertEqual(1, find_missing_number2([0]))

        nums = [i for i in range(100000) if i != 12345]
        random.shuffle(nums)
        self.assertEqual(12345, find_missing_number2(nums))

    def test_flip_bit_longest_seq(self):
        
        self.assertEqual(8, flip_bit_longest_seq(1775))
        
        self.assertEqual(3, flip_bit_longest_seq(5))
        
        self.assertEqual(4, flip_bit_longest_seq(71))
        
        self.assertEqual(1, flip_bit_longest_seq(0))

    def test_is_power_of_two(self):

        self.assertTrue(is_power_of_two(64))
        self.assertFalse(is_power_of_two(91))
        self.assertTrue(is_power_of_two(2**1001))
        self.assertTrue(is_power_of_two(1))
        self.assertFalse(is_power_of_two(0))

    def test_reverse_bits(self):

        self.assertEqual(43261596, reverse_bits(964176192))
        self.assertEqual(964176192, reverse_bits(43261596))
        self.assertEqual(1, reverse_bits(2147483648))

        
        self.assertEqual(0, reverse_bits(0))

        
        self.assertEqual(2**32 - 1, reverse_bits(2**32 - 1))

    def test_single_number(self):

        random.seed('test')

        self.assertEqual(0, single_number([1, 0, 2, 1, 2, 3, 3]))
        self.assertEqual(101, single_number([101]))

        single = random.randint(1, 100000)
        nums = [random.randint(1, 100000) for _ in range(1000)]
        nums *= 2  
        nums.append(single)
        random.shuffle(nums)

        self.assertEqual(single, single_number(nums))

    def test_single_number2(self):

        self.assertEqual(3, single_number2([4, 2, 3, 2, 1, 1, 4, 2, 4, 1]))
        single = random.randint(1, 100000)
        nums = [random.randint(1, 100000) for _ in range(1000)]
        nums *= 3  
        nums.append(single)
        random.shuffle(nums)
        self.assertEqual(single, single_number2(nums))

    def test_single_number3(self):
        self.assertEqual(sorted([2, 5]),
                         sorted(single_number3([2, 1, 5, 6, 6, 1])))
        self.assertEqual(sorted([4, 3]),
                         sorted(single_number3([9, 9, 4, 3])))

    def test_subsets(self):

        self.assertSetEqual(subsets([1, 2, 3]),
                            {(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3),
                            (1, 2, 3)})

        self.assertSetEqual(subsets([10, 20, 30, 40]),
                            {(10, 40), (10, 20, 40), (10, 30),
                            (10, 20, 30, 40), (40,),
                             (10, 30, 40), (30,), (20, 30), (30, 40), (10,),
                             (),
                             (10, 20), (20, 40), (20, 30, 40), (10, 20, 30),
                             (20,)})

    def test_get_bit(self):
        
        self.assertEqual(1, get_bit(22, 2))
        self.assertEqual(0, get_bit(22, 3))

    def test_set_bit(self):
        
        self.assertEqual(30, set_bit(22, 3))

    def test_clear_bit(self):
        
        self.assertEqual(18, clear_bit(22, 2))

    def test_update_bit(self):
        
        
        self.assertEqual(30, update_bit(22, 3, 1))
        
        
        self.assertEqual(18, update_bit(22, 2, 0))

    def test_int_to_bytes_big_endian(self):
        self.assertEqual(b'\x11', int_to_bytes_big_endian(17))

    def test_int_to_bytes_little_endian(self):
        self.assertEqual(b'\x11', int_to_bytes_little_endian(17))

    def test_bytes_big_endian_to_int(self):
        self.assertEqual(17, bytes_big_endian_to_int(b'\x11'))

    def test_bytes_little_endian_to_int(self):
        self.assertEqual(17, bytes_little_endian_to_int(b'\x11'))

    def test_swap_pair(self):
        
        self.assertEqual(41, swap_pair(22))
        
        self.assertEqual(5, swap_pair(10))

    def test_find_difference(self):
        self.assertEqual('e', find_difference("abcd", "abecd"))

    def test_has_alternative_bit(self):
        self.assertTrue(has_alternative_bit(5))
        self.assertFalse(has_alternative_bit(7))
        self.assertFalse(has_alternative_bit(11))
        self.assertTrue(has_alternative_bit(10))

    def test_has_alternative_bit_fast(self):
        self.assertTrue(has_alternative_bit_fast(5))
        self.assertFalse(has_alternative_bit_fast(7))
        self.assertFalse(has_alternative_bit_fast(11))
        self.assertTrue(has_alternative_bit_fast(10))

    def test_insert_one_bit(self):
        
        self.assertEqual(45, insert_one_bit(21, 1, 2))
        self.assertEqual(41, insert_one_bit(21, 0, 2))
        self.assertEqual(53, insert_one_bit(21, 1, 5))
        self.assertEqual(43, insert_one_bit(21, 1, 0))

    def test_insert_mult_bits(self):
        
        self.assertEqual(47, insert_mult_bits(5, 7, 3, 1))
        self.assertEqual(47, insert_mult_bits(5, 7, 3, 0))
        self.assertEqual(61, insert_mult_bits(5, 7, 3, 3))

    def test_remove_bit(self):
        
        self.assertEqual(9, remove_bit(21, 2))
        self.assertEqual(5, remove_bit(21, 4))
        self.assertEqual(10, remove_bit(21, 0))

    def test_binary_gap(self):
        
        self.assertEqual(2, binary_gap(22))
        
        self.assertEqual(1, binary_gap(6))
        
        self.assertEqual(0, binary_gap(8))
        
        self.assertEqual(4, binary_gap(145))


if __name__ == '__main__':
    unittest.main()