Example #1
0
    def test_isub(self):
        a1 = B(0b0001)
        a2 = B(0b1000)

        a1 -= 1
        self.assertEqual(a1, 0b0000)
        a2 -= 0b0001
        self.assertEqual(a2, 0b0111)
Example #2
0
    def test_iadd(self):
        a1 = B(0b0001)
        a2 = B(0b1000)

        a1 += 1
        self.assertEqual(a1, 0b0010)
        a1 += a2
        self.assertEqual(a1, 0b1010)
Example #3
0
    def test_ishift(self):
        a1 = B(0b1010)
        a2 = B(0b0101)

        a2 <<= 1
        self.assertEqual(a2, a1)
        a2 >>= 1
        self.assertEqual(a2, 0b0101)
        a1 <<= a2
        self.assertEqual(a1, 0b101000000)
Example #4
0
 def test_equality(self):
     # Bitfield and ints
     self.assertEqual(a, c)
     self.assertNotEqual(a, d)
     # Bitfield and Bools
     self.assertEqual(B(0b1), True)
     self.assertNotEqual(B(0b0), True)
     # Bitfield and Bitfield
     self.assertEqual(a, B(0b0011))
     self.assertNotEqual(a, b)
     # Bitfield and some non-number type
     self.assertNotEqual(a, [0, 0, 1, 1])
Example #5
0
    def test_setitem(self):
        bits = B(0b1111)
        bits[:] = 0b1010
        self.assertEqual(bits, 0b1010)
        self.assertTrue(isinstance(bits, B))

        bits[0:2] = 0b00
        self.assertEqual(bits, 0b1000)

        bits[:-1] = 0b111
        self.assertEqual(bits, 0b1111)
        bits[::2] = 0b00
        self.assertEqual(bits, 0b1010)
        # The following has a nasty side effect of setting the bitfield width to 0
        bits[:] = 0b0
        self.assertEqual(len(bits), 0)
        self.assertEqual(0b0.bit_length(), 0)
        bits = B(0b11001010)
        bits[::2] = 0b1111
        self.assertEqual(0b11011111, bits)
        bits[-1::-1] = 0b10101111
        self.assertEqual(bits, 0b11110101)
Example #6
0
 def test_delitem(self):
     bits = B(0b101010101)
     l = len(bits)
     del bits[0]
     self.assertEqual(bits, 0b10101010)
     self.assertEqual(len(bits), l - 1)
     l = len(bits)
     del bits[-1]
     self.assertEqual(bits, 0b101010)
     # We actually just removed two bits because the MSB will always be 1.
     self.assertEqual(len(bits), l - 2)
     l = len(bits)
     del bits[0::2]
     self.assertEqual(bits, 0b111)
     self.assertEqual(len(bits), l - 3)
     del bits[:]
     self.assertEqual(bits, 0b0)
     self.assertEqual(len(bits), 0)
     self.assertEqual(0b0.bit_length(), 0)
     self.assertEqual(0b1.bit_length(), 1)
Example #7
0
    def test_getitem(self):
        bits = B(0b1111)

        # Basic bit indexing:
        self.assertEqual(bits[0], 0b0001)
        # Bitfield indexing does *not* produce bits[1] --> 0b0010
        self.assertEqual(bits[1], 0b0001)
        self.assertEqual(bits[-4], 0b1)
        self.assertRaises(IndexError, bits.__getitem__, 4)
        self.assertRaises(IndexError, bits.__getitem__, -5)

        bits = B(0b11110101)
        self.assertEqual(bits[1], 0b0)
        self.assertEqual(bits[7], 0b1)

        # Providing __len__, __getitem__, and raising appropriate IndexError's make Bitfields
        # iterable. Iterates over the bits LSBF.
        self.assertSequenceEqual(bits, [1, 0, 1, 0, 1, 1, 1, 1])

        self.assertTrue(isinstance(bits[0:2], B))

        bits = B(0b101011)

        self.assertEqual(bits[:], bits)
        # Reverse endianness
        self.assertEqual(bits[::-1], 0b110101)
        # Test slicing subtleties
        self.assertEqual(bits[:2], 0b11)
        self.assertEqual(bits[:-1], 0b01011)
        self.assertEqual(bits[0:2], bits[:2])
        self.assertEqual(bits[2:], 0b1010)
        self.assertEqual(bits[2:-1], 0b010)
        # The most significant bit will *always* be 0b1, even in the case bits = B(0b01)
        self.assertEqual(B(0b01)[-1], 0b1)
        self.assertEqual(bits[-1:], 0b1)
        # 6 is not a valid index, but the endpoint is exclusive
        self.assertEqual(bits[2:6], 0b1010)
        self.assertRaises(IndexError, bits.__getitem__, slice(2, 7))
        self.assertEqual(bits[-3:], 0b101)
        self.assertEqual(bits[-4:-2], 0b10)
        self.assertEqual(B(0b10101)[::2], 0b111)
        self.assertEqual(B(0b101010)[1::2], 0b111)
        self.assertEqual(B(0b1100010001)[-2::-2], 0b10101)
Example #8
0
    def test_ipow(self):
        a1 = B(0b1000)
        a2 = B(0b0010)

        a1 **= a2
        self.assertEqual(a1, 0b1000000)
Example #9
0
    def test_imod(self):
        a1 = B(8)
        a2 = B(7)

        a1 %= a2
        self.assertEqual(a1, 1)
Example #10
0
 def test_itruediv(self):
     # It does not make sense for an integer bitfield to allow for implementing /=
     a1 = B(0b0001)
     a2 = B(0b1000)
     a1 /= a2
     self.assertTrue(isinstance(a1, B))
Example #11
0
import unittest
from bitfield import Bitfield as B

a = B(0b0011)
b = B(0b1100)
c = 0b0011
d = 0b1100


class BitfieldOperatorsTest(unittest.TestCase):
    def test_neg(self):
        self.assertEqual(-a, -c)
        self.assertNotEqual(a, -a)

    def test_pos(self):
        self.assertEqual(+a, a)
        self.assertNotEqual(+a, -a)

    def test_abs(self):
        self.assertEqual(abs(-a), a)
        self.assertEqual(abs(a), a)

    def test_invert(self):
        self.assertEqual(~a, ~c)
        self.assertEqual(~b, ~d)

    def test_equality(self):
        # Bitfield and ints
        self.assertEqual(a, c)
        self.assertNotEqual(a, d)
        # Bitfield and Bools