def testCreationFromBin(self):
     s = Bits(bin='1010000011111111')
     self.assertEqual((s.length, s.hex), (16, 'a0ff'))
     s = Bits(bin='00')[:1]
     self.assertEqual(s.bin, '0')
     s = Bits(bin=' 0000 \n 0001\r ')
     self.assertEqual(s.bin, '00000001')
 def testAdding(self):
     a = Bits(bin='0b0')
     b = Bits(bin='0b11')
     c = a + b
     self.assertEqual(c, Bits(bin='0b011'))
     self.assertEqual(a, Bits(bin='0b0'))
     self.assertEqual(b, Bits(bin='0b11'))
 def testUnorderable(self):
     a = Bits(5)
     b = Bits(5)
     with self.assertRaises(TypeError):
         a < b
     with self.assertRaises(TypeError):
         a > b
     with self.assertRaises(TypeError):
         a <= b
     with self.assertRaises(TypeError):
         a >= b
    def testMisc(self):
        a = Bits(int=-31111, length=32)
        b = Bits(int=-31111, length=32)

        assert a.len == 32

        assert a.int == -31111
        assert a.hex == 'ffff8679'
        assert a.bin == '11111111111111111000011001111001'
        assert a.bytes == b'\xff\xff\x86y'

        assert a.all(True, [1, 2, 3, 4]) is True
        assert a.all(True, [30]) is False

        assert a.any(True, [0, 30]) is True
        assert a.any(True, [29, 30]) is False

        assert a.count(1) == 24
        assert a.count(0) == 8

        assert a.tobytes() == b'\xff\xff\x86y'
        assert a[-4:].bin == '1001'

        assert a == b

        c = a + b
        assert c.hex == 'ffff8679ffff8679'

        assert str(a) == '0xffff8679'
        assert repr(a) == "Bits('0xffff8679')"
        assert hash(a) > 0

        f = Bits(float=3.1415, length=32)
        assert f.len == 32

        assert a != f
 def testCreationFromInt(self):
     s = Bits(int=0, length=4)
     self.assertEqual(s.bin, '0000')
     s = Bits(int=1, length=2)
     self.assertEqual(s.bin, '01')
     s = Bits(int=-1, length=11)
     self.assertEqual(s.bin, '11111111111')
     s = Bits(int=12, length=7)
     self.assertEqual(s.int, 12)
     s = Bits(int=-243, length=108)
     self.assertEqual((s.int, s.length), (-243, 108))
     for length in range(6, 10):
         for value in range(-17, 17):
             s = Bits(int=value, length=length)
             self.assertEqual((s.int, s.length), (value, length))
     s = Bits(int=10, length=8)
 def testCreationFromBinWithWhitespace(self):
     s = Bits(bin='  \r\r\n0   B    00   1 1 \t0 ')
     self.assertEqual(s.bin, '00110')
 def testCreationFromHexWithWhitespace(self):
     s = Bits(hex='  \n0 X a  4e       \r3  \n')
     self.assertEqual(s.hex, 'a4e3')
 def testCreationFromHex(self):
     s = Bits(hex='0xA0ff')
     self.assertEqual((s.len, s.hex), (16, 'a0ff'))
     s = Bits(hex='0x0x0X')
     self.assertEqual((s.length, s.hex), (0, ''))
 def testLongBool(self):
     a = Bits(1000)
     b = bool(a)
     self.assertTrue(b is False)
 def testCreationFromBool(self):
     a = Bits(bool=1)
     self.assertEqual(a, Bits(bool=1))
 def testCreationFromDataWithOffset(self):
     s1 = Bits(bytes=b'\x0b\x1c\x2f', offset=0, length=20)
     s2 = Bits(bytes=b'\xa0\xb1\xC2', offset=4)
     self.assertEqual((s2.len, s2.hex), (20, '0b1c2'))
     self.assertEqual((s1.len, s1.hex), (20, '0b1c2'))
     self.assertTrue(s1 == s2)
 def testCreationFromBytes(self):
     s = Bits(bytes=b'\xa0\xff')
     self.assertEqual((s.len, s.hex), (16, 'a0ff'))
 def testCreationFromUeErrors(self):
     self.assertRaises(bitstring.CreationError, Bits, ue=-1)
     self.assertRaises(bitstring.CreationError, Bits, ue=1, length=12)
     s = Bits(bin='10')
     with self.assertRaises(bitstring.InterpretError):
         s.ue
 def testCreationFromUe(self):
     [self.assertEqual(Bits(ue=i).ue, i) for i in range(0, 20)]
 def testCreationFromSeErrors(self):
     self.assertRaises(bitstring.CreationError, Bits, se=-5, length=33)
     s = Bits(bin='001000')
     with self.assertRaises(bitstring.InterpretError):
         s.se
 def testCreationFromSe(self):
     for i in range(-100, 10):
         s = Bits(se=i)
         self.assertEqual(s.se, i)