def test_bits_eq_mix(): mix1 = Bits([1, 0, 1, 0]) mix2 = Bits([1, 0, 1, 0]) mix3 = Bits([1, 0, 1, 1]) assert mix1 == mix2 assert mix2 != mix3 assert mix3 != mix1
def test_bits_list_reference(): lst = [1, 0, 1, 0] mix1 = Bits(lst) mix2 = Bits(lst) assert mix1 == mix2 lst[3] = 1 assert mix1 == mix2 assert mix1 == Bits([1, 0, 1, 1])
def test_bits_eq_one(): one1 = Bits([1, 1, 1, 1]) one2 = Bits([1, 1, 1, 1]) one3 = Bits([1, 1, 1, 1, 1, 1]) assert one1 == one2 with pytest.raises(BitsOperationError): one2 != one3 with pytest.raises(BitsOperationError): one3 != one1
def test_float12(self): assert Bits.from_float12(-0.5) == Bits( [1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]) assert Bits.from_float12(1.5) == Bits( [0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]) assert Bits.from_float12(10.5) == Bits( [0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0]) assert Bits.from_float12(5.0) == Bits( [0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0])
def test_bits_eq_zero(): zero1 = Bits(size=4) zero2 = Bits(size=4) zero3 = Bits(size=8) assert zero1 == zero2 with pytest.raises(BitsOperationError): zero2 != zero3 with pytest.raises(BitsOperationError): zero3 != zero1
def test_from_bin(self): cases = [('1010', [1, 0, 1, 0]), ('111111', [1, 1, 1, 1, 1, 1]), ('0100', [0, 1, 0, 0]), ('0', [0]), ('00', [0, 0])] for q, a in cases: assert Bits.from_bin(q) == Bits(a) assert Bits.from_bin('0b' + q) == Bits(a) with pytest.raises(BitsConstructError): Bits.from_bin('0c100') with pytest.raises(BitsConstructError): Bits.from_bin('0123')
def test_bits_right_arithmetic_shift(): bits = Bits([1, 1, 0, 1]) bits2 = Bits([1, 0, 1, 1, 0, 0]) assert bits >> ('a', 0) == Bits([1, 1, 0, 1]) assert bits >> ('a', 1) == Bits([1, 1, 1, 0]) assert bits >> ('a', 2) == Bits([1, 1, 1, 1]) assert bits >> ('a', 3) == Bits([1, 1, 1, 1]) assert bits >> ('a', 4) == Bits([1, 1, 1, 1]) assert bits2 >> ('a', 1) == Bits([1, 1, 0, 1, 1, 0]) assert bits2 >> ('a', 2) == Bits([1, 1, 1, 0, 1, 1]) assert bits2 >> ('a', 3) == Bits([1, 1, 1, 1, 0, 1])
def test_dec_to_bin(): assert RadixConvert.dec_to_bin(0) == Bits([0]) assert RadixConvert.dec_to_bin(10) == Bits([1, 0, 1, 0]) assert RadixConvert.dec_to_bin(10, 0) == Bits([1, 0, 1, 0]) assert RadixConvert.dec_to_bin(10, 1) == Bits([0]) assert RadixConvert.dec_to_bin(10, 2) == Bits([1, 0]) assert RadixConvert.dec_to_bin(10, 3) == Bits([0, 1, 0]) assert RadixConvert.dec_to_bin(10, 10) == Bits([0, 0, 0, 0, 0, 0, 1, 0, 1, 0]) assert RadixConvert.dec_to_bin(100) == Bits([1, 1, 0, 0, 1, 0, 0]) assert RadixConvert.dec_to_bin(-10, 5) == Bits([1, 0, 1, 1, 0]) assert RadixConvert.dec_to_bin(-100, 8) == Bits([1, 0, 0, 1, 1, 1, 0, 0])
def test_from_hex(self): assert Bits.from_hex(0x0) == Bits([0, 0, 0, 0]) assert Bits.from_hex(0x0, 1) == Bits([0]) assert Bits.from_hex(0x0, 2) == Bits([0, 0]) assert Bits.from_hex(0xa) == Bits([1, 0, 1, 0]) assert Bits.from_hex(0x48) == Bits([0, 1, 0, 0, 1, 0, 0, 0]) assert Bits.from_hex(0x100) == Bits( [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]) assert Bits.from_hex(0xffff) == Bits( [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) assert Bits.from_hex(0x0a, 6) == Bits([0, 0, 1, 0, 1, 0]) assert Bits.from_hex(0xa, 8) == Bits([0, 0, 0, 0, 1, 0, 1, 0]) assert Bits.from_hex(0x0a, 8) == Bits([0, 0, 0, 0, 1, 0, 1, 0])
def test_from_dec_positive(self): assert Bits.from_dec(0) == Bits([0]) assert Bits.from_dec(0, 1) == Bits([0]) assert Bits.from_dec(1, 1) == Bits([1]) assert Bits.from_dec(10) == Bits([1, 0, 1, 0]) assert Bits.from_dec(10, 1) == Bits([0]) assert Bits.from_dec(10, 2) == Bits([1, 0]) assert Bits.from_dec(10, 3) == Bits([0, 1, 0]) assert Bits.from_dec(10, 10) == Bits([0, 0, 0, 0, 0, 0, 1, 0, 1, 0]) assert Bits.from_dec(100) == Bits([1, 1, 0, 0, 1, 0, 0])
def test_from_dec_negative(self): assert Bits.from_dec(-1) == Bits([1]) assert Bits.from_dec(-1, 1) == Bits([1]) assert Bits.from_dec(-1, 5) == Bits([1, 1, 1, 1, 1]) assert Bits.from_dec(-10) == Bits([1, 0, 1, 1, 0]) assert Bits.from_dec(-10, 5) == Bits([1, 0, 1, 1, 0]) assert Bits.from_dec(-10, 10) == Bits([1, 1, 1, 1, 1, 1, 0, 1, 1, 0]) assert Bits.from_dec(-100, 8) == Bits([1, 0, 0, 1, 1, 1, 0, 0]) assert Bits.from_dec(-255) == Bits([1, 0, 0, 0, 0, 0, 0, 0, 1])
def test_float_to_bin(): assert RadixConvert.float_to_bin(1.5) == RadixConvert.hex_to_bin( 0x3fc00000, 32) assert RadixConvert.float_to_bin(-1.5) == RadixConvert.hex_to_bin( 0xbfc00000, 32) assert RadixConvert.float_to_bin(0.0) == Bits(size=32) assert RadixConvert.float_to_bin(1.2) == RadixConvert.hex_to_bin( 0x3f99999a, 32)
def test_hex_to_bin(): assert RadixConvert.hex_to_bin(0x0) == Bits([0, 0, 0, 0]) assert RadixConvert.hex_to_bin(0xa) == Bits([1, 0, 1, 0]) assert RadixConvert.hex_to_bin(0x48) == Bits([0, 1, 0, 0, 1, 0, 0, 0]) assert RadixConvert.hex_to_bin(0x100) == Bits( [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]) assert RadixConvert.hex_to_bin(0xffff) == Bits( [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) assert RadixConvert.hex_to_bin(0x0a, 6) == Bits([0, 0, 1, 0, 1, 0]) assert RadixConvert.hex_to_bin(0xa, 8) == Bits([0, 0, 0, 0, 1, 0, 1, 0]) assert RadixConvert.hex_to_bin(0x0a, 8) == Bits([0, 0, 0, 0, 1, 0, 1, 0])
def test_bits_left_logical_shift(): bits = Bits([1, 1, 0, 1]) assert bits << ('l', 0) == Bits([1, 1, 0, 1]) assert bits << ('l', 1) == Bits([1, 0, 1, 0]) assert bits << ('l', 2) == Bits([0, 1, 0, 0]) assert bits << ('l', 3) == Bits([1, 0, 0, 0]) assert bits << ('l', 4) == Bits([0, 0, 0, 0]) assert bits << ('l', 5) == Bits([0, 0, 0, 0])
def test_RCA(): A1 = Bits([1, 1, 0, 1, 0, 0, 1, 0]) B1 = Bits([1, 1, 1, 0, 0, 1, 0, 1]) assert Multiplication.RCA(A1, B1, 1, size=8) == [ Bits([1, 0, 1, 1, 1, 0, 0, 0]), Bits([1, 1, 0, 0, 0, 1, 1, 1]) ] A2 = Bits([1, 0, 1, 1, 1, 0, 0, 0]) B2 = Bits([1, 1, 1, 0, 1, 1, 0, 0]) assert Multiplication.RCA(A2, B2, 0, size=8) == [ Bits([1, 0, 1, 0, 0, 1, 0, 0]), Bits([1, 1, 1, 1, 1, 0, 0, 0]) ]
def hex_to_bin(value: int, size: int = 0) -> Bits: """ Convert hexadecimal number to binary. :param value: (int) Decimal number. :param size: (int) [Optional] Number of BINARY digits. :return: (Bits) Binary number """ if size == 0: size = len(list(hex(value))[2:]) * 4 binary = [ int(x) for x in list('{0:#0{1}b}'.format(value, size + 2))[2:] ] return Bits(binary)
def test_hexstr(self): assert Bits(size=4).hex == '0x0' assert Bits([0, 0, 0, 1]).hex == '0x1' assert Bits([1, 0, 1, 0]).hex == '0xa' assert Bits([1, 0, 0, 0, 0]).hex == '0x10' assert Bits([0, 1, 0, 0, 0, 0, 0, 0, 0, 0]).hex == '0x100' assert Bits([0, 0, 0, 0, 0, 0, 0, 0]).hex == '0x00' assert Bits.from_dec(31, size=32).hex == '0x0000001f' assert Bits.from_dec(234).hex == '0xea'
def test_wallace_tree(): A1 = Bits([0, 1, 1, 0, 1, 1]) B1 = Bits([0, 1, 1, 1, 0, 1]) assert Multiplication.wallace_tree(A1, B1, size=6) == Bits( [0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]) A2 = Bits([1, 0, 1, 1, 1, 1]) B2 = Bits([0, 1, 1, 0, 1, 0]) assert Multiplication.wallace_tree(A2, B2, size=6) == Bits( [1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0])
def test_booth_secondary(): A1 = Bits.from_dec(0b010111, 6) B1 = Bits.from_dec(0b001011, 6) assert Multiplication.booth_secondary(A1, B1) == [ Bits([1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1]), Bits([1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0]), Bits([0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0]), Bits([0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1]) ] A2 = Bits.from_dec(0b101111, 6) B2 = Bits.from_dec(0b011010, 6) assert Multiplication.booth_secondary(A2, B2) == [ Bits([0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0]), Bits([0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0]), Bits([1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0]), Bits([1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0]) ]
def dec_to_bin(value: int, size: int = 0) -> Bits: """ Convert decimal number to binary. :param value: (int) Decimal number. :param size: (int) [Optional] Number of BINARY digits. :return: (Bits) Binary number """ if value >= 0: binary = [ int(x) for x in list('{0:#0{1}b}'.format(value, size + 2))[2:] ] else: # Calc 2s compliment and mask bits for designated digits binary = [ int(x) for x in bin(((-value ^ (2**32 - 1)) + 0b1) & 2**size - 1)[2:] ] if size != 0: binary = collections.deque(binary, size) return Bits(binary)
def test_zeros(self): bits = Bits(size=32) assert bits.int == 0 assert bits.uint == 0 assert bits.float == 0.0
def test_has_carry(self): assert Bits([0, 0, 0, 1]) + Bits([0, 0, 0, 1]) == Bits([0, 0, 1, 0]) assert Bits([0, 0, 1, 1]) + Bits([0, 0, 0, 1]) == Bits([0, 1, 0, 0])
def test_no_carry_extend(self): assert Bits([0]) + Bits([0, 0, 0]) == Bits([0, 0, 0]) assert Bits([0, 0, 0]) + Bits([0]) == Bits([0, 0, 0]) assert Bits([0, 0, 0]) + Bits([1, 1]) == Bits([0, 1, 1]) assert Bits([1, 0, 0]) + Bits([0, 0, 0, 0]) == Bits([0, 1, 0, 0])
def test_multiple_addition(self): assert Bits([0, 0]) + Bits([0, 0, 0]) + Bits([0, 0, 0, 0]) == Bits( [0, 0, 0, 0]) assert Bits([0, 0]) + Bits([0, 0, 0]) + Bits([0, 0]) == Bits([0, 0, 0]) assert Bits([1, 0]) + Bits([1, 0, 0]) + Bits([0, 1, 1]) == Bits( [1, 0, 0, 1]) assert Bits([1, 1, 1, 1]) + Bits([1, 1, 1, 1]) + Bits( [1, 1, 1, 1]) == Bits([1, 0, 1, 1, 0, 1])
def test_has_carry_extend_overflow(self): assert Bits([1, 1, 1, 1]) + Bits([1]) == Bits([1, 0, 0, 0, 0]) assert Bits([1]) + Bits([1, 1, 1, 1]) == Bits([1, 0, 0, 0, 0])
def test_bits_and_different_len(): bits1 = Bits([1, 0, 1, 0]) bits2 = Bits([1, 0, 0, 1, 0]) with pytest.raises(BitsOperationError): bits1 ^ bits2
def test_bits_inv_simple(): bits = Bits([1, 0, 1, 0]) assert ~bits == Bits([0, 1, 0, 1])
def test_bits_xor_simple(): bits1 = Bits([1, 0, 1, 0]) bits2 = Bits([1, 0, 0, 1]) assert (bits1 ^ bits2) == Bits([0, 0, 1, 1])
def test_bits_and_simple(): bits1 = Bits([1, 0, 1, 0]) bits2 = Bits([1, 0, 0, 1]) assert (bits1 & bits2) == Bits([1, 0, 0, 0])
def test_zero_construct(): empty = Bits(size=32) assert empty == Bits([0 for x in range(32)]) assert len(empty) == 32