Beispiel #1
0
    def test_clear_bit_with_valid_argument(self):
        bit_array = BitArray(12)
        bit_array.set_bit(11)
        bit_array.clear_bit(11)
        exp = 0

        self.assertEqual(exp, bit_array.get_bit(11))
Beispiel #2
0
    def test_eq_operation_with_not_equal_size_arrays(self):
        ba1 = BitArray(12)
        ba1.set_bit(0)
        ba2 = BitArray(8)
        ba1.set_bit(0)

        self.assertFalse(ba1 == ba2)
Beispiel #3
0
    def test_inversion(self):
        bit_array = BitArray(8)
        bit_array.set_bit(0)
        bit_array.set_bit(2)
        bit_array.set_bit(4)
        exp = '11101010'

        self.assertEqual(exp, (~bit_array).to_string())
Beispiel #4
0
    def test_or_operation_with_not_equal_size_arrays(self):
        ba1 = BitArray(5)
        ba1.set_bit(3)
        ba2 = BitArray(12)
        ba2.set_bit(10)
        exp = '0000010000001000'

        self.assertEqual(exp, (ba1 | ba2).to_string())
Beispiel #5
0
    def test_and_operation_with_equal_size_arrays(self):
        ba1 = BitArray(8)
        ba1.set_bit(3)
        ba1.set_bit(5)
        ba2 = BitArray(8)
        ba2.set_bit(5)
        exp = '00100000'

        self.assertEqual(exp, (ba1 & ba2).to_string())
Beispiel #6
0
    def test_convert_array_to_string(self):
        bit_array = BitArray(8)
        bit_array.set_bit(0)
        bit_array.set_bit(2)
        bit_array.set_bit(4)
        bit_array.set_bit(6)
        exp = '01010101'

        self.assertEqual(exp, bit_array.to_string())
 def bit_array_from_string(string):
     if not re.fullmatch('[0|1]+', string):
         raise ValueError('Wrong format of bit array.')
     bit_array = BitArray(len(string))
     reversed_string = string[::-1]
     for bit_index in range(0, len(string), 1):
         if reversed_string[bit_index] == '1':
             bit_array.set_bit(bit_index)
     return bit_array
Beispiel #8
0
    def test_xor_operation_with_equal_size_arrays(self):
        ba1 = BitArray(8)
        ba1.set_bit(1)
        ba1.set_bit(5)
        ba2 = BitArray(8)
        ba2.set_bit(2)
        ba2.set_bit(5)
        exp = '00000110'

        self.assertEqual(exp, (ba1 ^ ba2).to_string())
Beispiel #9
0
 def bit_array_from_string(self, string):
     if not re.fullmatch('[0|1]+', string):
         self.logger.log('Error:\n     '
                         'Can\'t convert "{}" '
                         'to bit array'.format(string))
         raise ValueError('Wrong format of bit array.')
     bit_array = BitArray(len(string))
     reversed_string = string[::-1]
     for bit_index in range(0, len(string), 1):
         if reversed_string[bit_index] == '1':
             bit_array.set_bit(bit_index)
     return bit_array
Beispiel #10
0
    def test_set_bit_with_valid_argument(self):
        bit_array = BitArray(12)
        bit_array.set_bit(11)
        exp = [0, 8]

        self.assertEqual(exp, bit_array._BitArray__array)
Beispiel #11
0
    def test_ne_operation(self):
        ba1 = BitArray(8)
        ba1.set_bit(0)
        ba2 = BitArray(8)

        self.assertTrue(ba1 != ba2)