Esempio n. 1
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'
Esempio n. 2
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_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])
    ]
Esempio n. 4
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
Esempio n. 5
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])
Esempio n. 6
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
Esempio n. 7
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
Esempio n. 8
0
 def test_multiple_subtraction(self):
     assert Bits.from_dec(2, 8) - Bits.from_dec(1, 8) - Bits.from_dec(
         32, 8) == Bits.from_dec(2 - 1 - 32, 8)
     assert Bits.from_dec(32, 8) - Bits.from_dec(15, 8) - Bits.from_dec(
         66, 8) == Bits.from_dec(32 - 15 - 66, 8)
Esempio n. 9
0
 def test_simple(self):
     assert Bits.from_dec(2, 8) - Bits.from_dec(1, 8) == Bits.from_dec(
         2 - 1, 8)
     assert Bits.from_dec(127, 8) - Bits.from_dec(3, 8) == Bits.from_dec(
         127 - 3, 8)
     assert Bits.from_dec(3, 8) - Bits.from_dec(127, 8) == Bits.from_dec(
         3 - 127, 8)
     assert Bits.from_dec(1, 8) - Bits.from_dec(6, 8) == Bits.from_dec(
         1 - 6, 8)
     assert Bits.from_dec(12, 8) - Bits.from_dec(55, 8) == Bits.from_dec(
         12 - 55, 8)
     assert Bits.from_dec(0, 8) - Bits.from_dec(55, 8) == Bits.from_dec(
         0 - 55, 8)
Esempio n. 10
0
 def test_simple(self):
     assert -Bits.from_dec(1, 8) == Bits.from_dec(-1, 8)
     assert -Bits.from_dec(0, 8) == Bits.from_dec(-0, 8)
     assert -Bits.from_dec(7, 8) == Bits.from_dec(-7, 8)
     assert -Bits.from_dec(32, 8) == Bits.from_dec(-32, 8)
     assert -Bits.from_dec(127, 8) == Bits.from_dec(-127, 8)