예제 #1
0
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
예제 #2
0
    def test_float(self):
        def close(a, b):
            assert abs(a - b) < 0.0001

        close(Bits.from_float(1.5).float, 1.5)
        close(Bits.from_float(-1.5).float, -1.5)
        close(Bits.from_float(0.0).float, 0.0)
        close(Bits.from_float(1.2).float, 1.2)
예제 #3
0
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])
예제 #4
0
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
예제 #5
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
예제 #6
0
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])
예제 #7
0
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_booth_tertiary():
    A1 = Bits.from_dec(0b101111, 6)
    B1 = Bits.from_dec(0b011010, 6)
    assert Multiplication.booth_tertiary(A1, B1) == [
        Bits([1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0]),
        Bits([1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0]),
        Bits([1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0])
    ]

    A2 = Bits.from_dec(0b101111, 6)
    B2 = Bits.from_dec(0b110010, 6)
    assert Multiplication.booth_tertiary(A2, B2) == [
        Bits([1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0]),
        Bits([0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0]),
        Bits([0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0])
    ]
예제 #9
0
 def test_int_negative(self):
     assert Bits.from_dec(-1).int == -1
     assert Bits.from_dec(-1, 1).int == -1
     assert Bits.from_dec(-1, 5).int == -1
     assert Bits.from_dec(-10).int == -10
     assert Bits.from_dec(-10, 5).int == -10
     assert Bits.from_dec(-10, 10).int == -10
     assert Bits.from_dec(-100, 8).int == -100
     assert Bits.from_dec(-255).int == -255
예제 #10
0
 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])
예제 #11
0
 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'
예제 #12
0
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)
예제 #13
0
 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')
예제 #14
0
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])
예제 #15
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])
    ]
예제 #17
0
 def test_uint_positive(self):
     assert Bits.from_dec(0).uint == 0
     assert Bits.from_dec(0, 1).uint == 0
     assert Bits.from_dec(1, 1).uint == 1
     assert Bits.from_dec(10).uint == 10
     assert Bits.from_dec(10, 10).uint == 10
     assert Bits.from_dec(100).uint == 100
예제 #18
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_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])
예제 #20
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)
예제 #21
0
 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])
예제 #22
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])
예제 #23
0
 def test_int_positive(self):
     assert Bits.from_dec(0).int == 0
     assert Bits.from_dec(0, 1).int == 0
     assert Bits.from_dec(10, 10).int == 10
     assert Bits.from_dec(100, 32).int == 100
예제 #24
0
from pybit.bits import Bits
from pybit.multiplication import Multiplication

X = Bits.from_bin('10111101')
Y = Bits.from_bin('10101111')
P, G, CO, S = Multiplication.CLA(X, Y, CI0=0, size=8)

print("P: ", P.hex)
print("G: ", G.hex)
print("S: ", S.hex)
print("CO: ", CO.hex)
예제 #25
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
예제 #26
0
def test_bits_inv_simple():
    bits = Bits([1, 0, 1, 0])
    assert ~bits == Bits([0, 1, 0, 1])
예제 #27
0
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])
예제 #28
0
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])
예제 #29
0
 def test_zeros(self):
     bits = Bits(size=32)
     assert bits.int == 0
     assert bits.uint == 0
     assert bits.float == 0.0
예제 #30
0
def test_zero_construct():
    empty = Bits(size=32)
    assert empty == Bits([0 for x in range(32)])
    assert len(empty) == 32