Python bitstring.ConstBitStream() Examples

The following are 30 code examples of bitstring.ConstBitStream(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module bitstring , or try the search function .
Example #1
Source File: test_bitstream.py    From bitstring with MIT License 6 votes vote down vote up
def testLengthKeywords(self):
        s = BitStream('0x0102')
        x, y = s.peeklist('a, hex:b', a=8, b=4)
        self.assertEqual((x, y), (1, '0'))
        self.assertEqual(s.pos, 0)

##class Miscellany(unittest.TestCase):
##
##    def testNumpyInt(self):
##        try:
##            import numpy
##            a = ConstBitStream(uint=numpy.uint8(5), length=3)
##            self.assertEqual(a.uint, 5)
##        except ImportError:
##            # Not to worry
##            pass 
Example #2
Source File: test_bitstream.py    From bitstring with MIT License 6 votes vote down vote up
def testRepr(self):
        max = bitstring.MAX_CHARS
        bls = ['', '0b1', '0o5', '0x43412424f41', '0b00101001010101']
        for bs in bls:
            a = BitStream(bs)
            b = eval(a.__repr__())
            self.assertTrue(a == b)
        for f in [ConstBitStream(filename='test.m1v'),
                  ConstBitStream(filename='test.m1v', length=17),
                  ConstBitStream(filename='test.m1v', length=23, offset=23102)]:
            f2 = eval(f.__repr__())
            self.assertEqual(f._datastore._rawarray.source.name, f2._datastore._rawarray.source.name)
            self.assertTrue(f2.tobytes() == f.tobytes())
        a = BitStream('0b1')
        self.assertEqual(repr(a), "BitStream('0b1')")
        a += '0b11'
        self.assertEqual(repr(a), "BitStream('0b111')")
        a += '0b1'
        self.assertEqual(repr(a), "BitStream('0xf')")
        a *= max
        self.assertEqual(repr(a), "BitStream('0x" + "f" * max + "')")
        a += '0xf'
        self.assertEqual(repr(a), "BitStream('0x" + "f" * max + "...') # length=%d" % (max * 4 + 4)) 
Example #3
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testCount(self):
        a = ConstBitStream('0xf0f')
        self.assertEqual(a.count(True), 8)
        self.assertEqual(a.count(False), 4)

        b = BitStream()
        self.assertEqual(b.count(True), 0)
        self.assertEqual(b.count(False), 0) 
Example #4
Source File: test_constbitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testReading(self):
        s = CBS(uie=333)
        a = s.read('uie')
        self.assertEqual(a, 333)
        s = CBS('uie=12, sie=-9, sie=9, uie=1000000')
        u = s.unpack('uie, 2*sie, uie')
        self.assertEqual(u, [12, -9, 9, 1000000]) 
Example #5
Source File: test_constbitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testFromFile(self):
        s = CBS(filename='test.m1v')
        self.assertEqual(s[0:32].hex, '000001b3')
        self.assertEqual(s.read(8 * 4).hex, '000001b3')
        width = s.read(12).uint
        height = s.read(12).uint
        self.assertEqual((width, height), (352, 288)) 
Example #6
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testBytearrayBehaviour(self):
        a = ConstBitStream(bytearray(b'uint:5=2'))
        b = ConstBitStream(bytearray(4))
        c = ConstBitStream(bytes=bytearray(b'uint:5=2'))
        self.assertEqual(a.bytes, b'uint:5=2')
        self.assertEqual(b, '0x00000000')
        self.assertEqual(c.bytes, b'uint:5=2') 
Example #7
Source File: test_constbitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testIsInstance(self):
        class SubBits(CBS): pass
        a = SubBits()
        self.assertTrue(isinstance(a, SubBits)) 
Example #8
Source File: test_constbitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testClassType(self):
        class SubBits(CBS): pass
        self.assertEqual(SubBits().__class__, SubBits) 
Example #9
Source File: test_constbitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testRead(self):
        s = CBS('0b100011110001')
        a = s.read('pad:1')
        self.assertEqual(a, None)
        self.assertEqual(s.pos, 1)
        a = s.read(3)
        self.assertEqual(a, CBS('0b000'))
        a = s.read('pad:0')
        self.assertEqual(a, None)
        self.assertEqual(s.pos, 4) 
Example #10
Source File: test_constbitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testReadList(self):
        s = CBS('0b10001111001')
        t = s.readlist('pad:1, uint:3, pad:4, uint:3')
        self.assertEqual(t, [0, 1])
        s.pos = 0
        t = s.readlist('pad:1, pad:5')
        self.assertEqual(t, [])
        self.assertEqual(s.pos, 6)
        s.pos = 0
        t = s.readlist('pad:1, bin, pad:4, uint:3')
        self.assertEqual(t, ['000', 1])
        s.pos = 0
        t = s.readlist('pad, bin:3, pad:4, uint:3')
        self.assertEqual(t, ['000', 1]) 
Example #11
Source File: test_constbitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testUnpackingBytesWithKeywords(self):
        s = CBS('0x55'*10)
        t = s.unpack('pad:a, bytes:b, bytes, pad:a', a=4, b=6)
        self.assertEqual(t, [b'\x55'*6, b'\x55'*3]) 
Example #12
Source File: test_constbitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testReadingHex(self):
        s = CBS('0xabcdef')
        self.assertEqual(s.read(4), '0xf')
        self.assertEqual(s.read(4), '0xe')
        self.assertEqual(s.pos, 8)

    # TODO: Add more tests 
Example #13
Source File: test_constbitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testSimpleCreation(self):
        f = io.BytesIO(b"\x12\xff\x77helloworld")
        s = CBS(f)
        self.assertEqual(s[0:8], '0x12')
        self.assertEqual(s.len, 13 * 8)
        s = CBS(f, offset=8, length=12)
        self.assertEqual(s, '0xff7') 
Example #14
Source File: test_constbitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testExceptions(self):
        f = io.BytesIO(b"123456789")
        s = CBS(f, length=9*8)
        with self.assertRaises(bitstring.CreationError):
            s = CBS(f, length=9*8 + 1)
        with self.assertRaises(bitstring.CreationError):
            s = CBS(f, length=9*8, offset=1) 
Example #15
Source File: test_constbitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testReadingLines(self):
        s = b"This is a test\nof reading lines\nof text\n"
        b = CBS(bytes=s)
        n = bitstring.Bits(bytes=b'\n')
        self.assertEqual(b.readto(n).bytes, b'This is a test\n')
        self.assertEqual(b.readto(n).bytes, b'of reading lines\n')
        self.assertEqual(b.readto(n).bytes, b'of text\n') 
Example #16
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testCountWithOffsetData(self):
        a = ConstBitStream('0xff0120ff')
        b = a[1:-1]
        self.assertEqual(b.count(1), 16)
        self.assertEqual(b.count(0), 14) 
Example #17
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testInteger(self):
        a = ConstBitStream('0x123456')
        self.assertRaises(bitstring.InterpretError, a.read, 'uint:0')
        self.assertRaises(bitstring.InterpretError, a.read, 'float:0') 
Example #18
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testReadIntList(self):
        a = ConstBitStream('0xab, 0b110')
        b, c = a.readlist([8, 3])
        self.assertEqual(b.hex, 'ab')
        self.assertEqual(c.bin, '110') 
Example #19
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testReadInt(self):
        a = ConstBitStream('0xffeedd')
        b = a.read(8)
        self.assertEqual(b.hex, 'ff')
        self.assertEqual(a.pos, 8)
        b = a.peek(8)
        self.assertEqual(b.hex, 'ee')
        self.assertEqual(a.pos, 8)
        b = a.peek(1)
        self.assertEqual(b, '0b1')
        b = a.read(1)
        self.assertEqual(b, '0b1') 
Example #20
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testLengthWithBoolRead(self):
        a = ConstBitStream('0xf')
        self.assertRaises(ValueError, a.read, 'bool:0')
        self.assertRaises(ValueError, a.read, 'bool:2') 
Example #21
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testInterpretation(self):
        a = ConstBitStream('0b1')
        self.assertEqual(a.bool, True)
        self.assertEqual(a.read('bool'), True)
        self.assertEqual(a.unpack('bool')[0], True)
        b = ConstBitStream('0b0')
        self.assertEqual(b.bool, False)
        self.assertEqual(b.peek('bool'), False)
        self.assertEqual(b.unpack('bool')[0], False) 
Example #22
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testLengthKeywordsWithStretch(self):
        a = ConstBitStream('0xff, 0b000, 0xf')
        x, y, z = a.unpack('hex:a, bin, hex:b', a=8, b=4)
        self.assertEqual(y, '000') 
Example #23
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testLengthKeywords(self):
        a = ConstBitStream('2*13=100, 0b111')
        x, y, z = a.unpack('n, uint:m, bin:q', n=13, m=13, q=3)
        self.assertEqual(x, 100)
        self.assertEqual(y, 100)
        self.assertEqual(z, '111') 
Example #24
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testPackCodeDicts(self):
        self.assertEqual(sorted(bitstring.REPLACEMENTS_BE.keys()),
                         sorted(bitstring.REPLACEMENTS_LE.keys()))
        self.assertEqual(sorted(bitstring.REPLACEMENTS_BE.keys()),
                         sorted(bitstring.PACK_CODE_SIZE.keys()))
        for key in bitstring.PACK_CODE_SIZE:
            be = pack(bitstring.REPLACEMENTS_BE[key], 0)
            le = pack(bitstring.REPLACEMENTS_LE[key], 0)
            self.assertEqual(be.len, bitstring.PACK_CODE_SIZE[key] * 8)
            self.assertEqual(le.len, be.len)

            # These tests don't compile for Python 3, so they're commented out to save me stress.
            #def testUnicode(self):
            #a = ConstBitStream(u'uint:12=34')
            #self.assertEqual(a.uint, 34)
            #a += u'0xfe'
            #self.assertEqual(a[12:], '0xfe')
            #a = BitStream('0x1122')
            #c = a.byteswap(u'h')
            #self.assertEqual(c, 1)
            #self.assertEqual(a, u'0x2211')

            #def testLongInt(self):
            #a = BitStream(4L)
            #self.assertEqual(a, '0b0000')
            #a[1:3] = -1L
            #self.assertEqual(a, '0b0110')
            #a[0] = 1L
            #self.assertEqual(a, '0b1110')
            #a *= 4L
            #self.assertEqual(a, '0xeeee')
            #c = a.byteswap(2L)
            #self.assertEqual(c, 1)
            #a = BitStream('0x11223344')
            #a.byteswap([1, 2L])
            #self.assertEqual(a, '0x11332244')
            #b = a*2L
            #self.assertEqual(b, '0x1133224411332244')
            #s = pack('uint:12', 46L)
            #self.assertEqual(s.uint, 46) 
Example #25
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testInitFromIterable(self):
        self.assertTrue(isinstance(range(10), collectionsAbc.Iterable))
        s = ConstBitStream(range(12))
        self.assertEqual(s, '0x7ff') 
Example #26
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testMultiplicativeFactorsUnpacking(self):
        s = ConstBitStream('0b10111')
        a, b, c, d = s.unpack('3*bool, bin')
        self.assertEqual((a, b, c), (True, False, True))
        self.assertEqual(d, '11') 
Example #27
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testNullBits(self):
        s = ConstBitStream(bin='')
        t = ConstBitStream(oct='')
        u = ConstBitStream(hex='')
        v = ConstBitStream(bytes=b'')
        self.assertFalse(s)
        self.assertFalse(t)
        self.assertFalse(u)
        self.assertFalse(v) 
Example #28
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testAutoFromBool(self):
        a = ConstBitStream() + True + False + True
        self.assertEqual(a, '0b00')
        #    self.assertEqual(a, '0b101')
        #    b = ConstBitStream(False)
        #    self.assertEqual(b, '0b0')
        #    c = ConstBitStream(True)
        #    self.assertEqual(c, '0b1')
        #    self.assertEqual(b, False)
        #    self.assertEqual(c, True)
        #    self.assertEqual(b & True, False) 
Example #29
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testRshiftVersesInPlaceRshift(self):
        a1 = ConstBitStream('0xabc')
        b1 = a1
        a1 >>= 4
        self.assertEqual(a1, '0x0ab')
        self.assertEqual(b1, '0xabc')

        a2 = BitStream('0xabc')
        b2 = a2
        c2 = a2 >> 8
        a2 >>= 4
        self.assertEqual(a2, '0x0ab')
        self.assertEqual(b2, '0x0ab')
        self.assertEqual(c2, '0x00a') 
Example #30
Source File: test_bitstream.py    From bitstring with MIT License 5 votes vote down vote up
def testLshiftVersesInPlaceLshift(self):
        a1 = ConstBitStream('0xabc')
        b1 = a1
        a1 <<= 4
        self.assertEqual(a1, '0xbc0')
        self.assertEqual(b1, '0xabc')

        a2 = BitStream('0xabc')
        b2 = a2
        c2 = a2 << 8
        a2 <<= 4
        self.assertEqual(a2, '0xbc0')
        self.assertEqual(b2, '0xbc0')
        self.assertEqual(c2, '0xc00')