Bug 1091668 - Import bitstring-3.1.3; r=ted, r=gerv
This commit is contained in:
122
python/bitstring/PKG-INFO
Normal file
122
python/bitstring/PKG-INFO
Normal file
@@ -0,0 +1,122 @@
|
||||
Metadata-Version: 1.1
|
||||
Name: bitstring
|
||||
Version: 3.1.3
|
||||
Summary: Simple construction, analysis and modification of binary data.
|
||||
Home-page: http://python-bitstring.googlecode.com
|
||||
Author: Scott Griffiths
|
||||
Author-email: scott@griffiths.name
|
||||
License: The MIT License: http://www.opensource.org/licenses/mit-license.php
|
||||
Download-URL: http://python-bitstring.googlecode.com
|
||||
Description: ================
|
||||
bitstring module
|
||||
================
|
||||
|
||||
**bitstring** is a pure Python module designed to help make
|
||||
the creation and analysis of binary data as simple and natural as possible.
|
||||
|
||||
Bitstrings can be constructed from integers (big and little endian), hex,
|
||||
octal, binary, strings or files. They can be sliced, joined, reversed,
|
||||
inserted into, overwritten, etc. with simple functions or slice notation.
|
||||
They can also be read from, searched and replaced, and navigated in,
|
||||
similar to a file or stream.
|
||||
|
||||
bitstring is open source software, and has been released under the MIT
|
||||
licence.
|
||||
|
||||
This version supports Python 2.6 and later (including Python 3).
|
||||
For Python 2.4 and 2.5 you should instead download version 1.0.
|
||||
|
||||
Documentation
|
||||
-------------
|
||||
The manual for the bitstring module is available here
|
||||
<http://packages.python.org/bitstring>. It contains a walk-through of all
|
||||
the features and a complete reference section.
|
||||
|
||||
It is also available as a PDF as part of the source download.
|
||||
|
||||
Installation
|
||||
------------
|
||||
If you have downloaded and unzipped the package then you need to run the
|
||||
``setup.py`` script with the 'install' argument::
|
||||
|
||||
python setup.py install
|
||||
|
||||
You may need to run this with root privileges on Unix-like systems.
|
||||
|
||||
|
||||
If you haven't yet downloaded the package then you can just try::
|
||||
|
||||
easy_install bitstring
|
||||
|
||||
or ::
|
||||
|
||||
pip install bitstring
|
||||
|
||||
|
||||
Simple Examples
|
||||
---------------
|
||||
Creation::
|
||||
|
||||
>>> a = BitArray(bin='00101')
|
||||
>>> b = Bits(a_file_object)
|
||||
>>> c = BitArray('0xff, 0b101, 0o65, uint:6=22')
|
||||
>>> d = pack('intle:16, hex=a, 0b1', 100, a='0x34f')
|
||||
>>> e = pack('<16h', *range(16))
|
||||
|
||||
Different interpretations, slicing and concatenation::
|
||||
|
||||
>>> a = BitArray('0x1af')
|
||||
>>> a.hex, a.bin, a.uint
|
||||
('1af', '000110101111', 431)
|
||||
>>> a[10:3:-1].bin
|
||||
'1110101'
|
||||
>>> 3*a + '0b100'
|
||||
BitArray('0o0657056705674')
|
||||
|
||||
Reading data sequentially::
|
||||
|
||||
>>> b = BitStream('0x160120f')
|
||||
>>> b.read(12).hex
|
||||
'160'
|
||||
>>> b.pos = 0
|
||||
>>> b.read('uint:12')
|
||||
352
|
||||
>>> b.readlist('uint:12, bin:3')
|
||||
[288, '111']
|
||||
|
||||
Searching, inserting and deleting::
|
||||
|
||||
>>> c = BitArray('0b00010010010010001111') # c.hex == '0x1248f'
|
||||
>>> c.find('0x48')
|
||||
(8,)
|
||||
>>> c.replace('0b001', '0xabc')
|
||||
>>> c.insert('0b0000')
|
||||
>>> del c[12:16]
|
||||
|
||||
Unit Tests
|
||||
----------
|
||||
|
||||
The 400+ unit tests should all pass for Python 2.6 and later.
|
||||
|
||||
----
|
||||
|
||||
The bitstring module has been released as open source under the MIT License.
|
||||
Copyright (c) 2014 Scott Griffiths
|
||||
|
||||
For more information see the project's homepage on Google Code:
|
||||
<http://python-bitstring.googlecode.com>
|
||||
|
||||
|
||||
Platform: all
|
||||
Classifier: Development Status :: 5 - Production/Stable
|
||||
Classifier: Intended Audience :: Developers
|
||||
Classifier: Operating System :: OS Independent
|
||||
Classifier: License :: OSI Approved :: MIT License
|
||||
Classifier: Programming Language :: Python :: 2.6
|
||||
Classifier: Programming Language :: Python :: 2.7
|
||||
Classifier: Programming Language :: Python :: 3
|
||||
Classifier: Programming Language :: Python :: 3.0
|
||||
Classifier: Programming Language :: Python :: 3.1
|
||||
Classifier: Programming Language :: Python :: 3.2
|
||||
Classifier: Programming Language :: Python :: 3.3
|
||||
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
||||
99
python/bitstring/README.txt
Normal file
99
python/bitstring/README.txt
Normal file
@@ -0,0 +1,99 @@
|
||||
================
|
||||
bitstring module
|
||||
================
|
||||
|
||||
**bitstring** is a pure Python module designed to help make
|
||||
the creation and analysis of binary data as simple and natural as possible.
|
||||
|
||||
Bitstrings can be constructed from integers (big and little endian), hex,
|
||||
octal, binary, strings or files. They can be sliced, joined, reversed,
|
||||
inserted into, overwritten, etc. with simple functions or slice notation.
|
||||
They can also be read from, searched and replaced, and navigated in,
|
||||
similar to a file or stream.
|
||||
|
||||
bitstring is open source software, and has been released under the MIT
|
||||
licence.
|
||||
|
||||
This version supports Python 2.6 and later (including Python 3).
|
||||
For Python 2.4 and 2.5 you should instead download version 1.0.
|
||||
|
||||
Documentation
|
||||
-------------
|
||||
The manual for the bitstring module is available here
|
||||
<http://packages.python.org/bitstring>. It contains a walk-through of all
|
||||
the features and a complete reference section.
|
||||
|
||||
It is also available as a PDF as part of the source download.
|
||||
|
||||
Installation
|
||||
------------
|
||||
If you have downloaded and unzipped the package then you need to run the
|
||||
``setup.py`` script with the 'install' argument::
|
||||
|
||||
python setup.py install
|
||||
|
||||
You may need to run this with root privileges on Unix-like systems.
|
||||
|
||||
|
||||
If you haven't yet downloaded the package then you can just try::
|
||||
|
||||
easy_install bitstring
|
||||
|
||||
or ::
|
||||
|
||||
pip install bitstring
|
||||
|
||||
|
||||
Simple Examples
|
||||
---------------
|
||||
Creation::
|
||||
|
||||
>>> a = BitArray(bin='00101')
|
||||
>>> b = Bits(a_file_object)
|
||||
>>> c = BitArray('0xff, 0b101, 0o65, uint:6=22')
|
||||
>>> d = pack('intle:16, hex=a, 0b1', 100, a='0x34f')
|
||||
>>> e = pack('<16h', *range(16))
|
||||
|
||||
Different interpretations, slicing and concatenation::
|
||||
|
||||
>>> a = BitArray('0x1af')
|
||||
>>> a.hex, a.bin, a.uint
|
||||
('1af', '000110101111', 431)
|
||||
>>> a[10:3:-1].bin
|
||||
'1110101'
|
||||
>>> 3*a + '0b100'
|
||||
BitArray('0o0657056705674')
|
||||
|
||||
Reading data sequentially::
|
||||
|
||||
>>> b = BitStream('0x160120f')
|
||||
>>> b.read(12).hex
|
||||
'160'
|
||||
>>> b.pos = 0
|
||||
>>> b.read('uint:12')
|
||||
352
|
||||
>>> b.readlist('uint:12, bin:3')
|
||||
[288, '111']
|
||||
|
||||
Searching, inserting and deleting::
|
||||
|
||||
>>> c = BitArray('0b00010010010010001111') # c.hex == '0x1248f'
|
||||
>>> c.find('0x48')
|
||||
(8,)
|
||||
>>> c.replace('0b001', '0xabc')
|
||||
>>> c.insert('0b0000')
|
||||
>>> del c[12:16]
|
||||
|
||||
Unit Tests
|
||||
----------
|
||||
|
||||
The 400+ unit tests should all pass for Python 2.6 and later.
|
||||
|
||||
----
|
||||
|
||||
The bitstring module has been released as open source under the MIT License.
|
||||
Copyright (c) 2014 Scott Griffiths
|
||||
|
||||
For more information see the project's homepage on Google Code:
|
||||
<http://python-bitstring.googlecode.com>
|
||||
|
||||
4234
python/bitstring/bitstring.py
Normal file
4234
python/bitstring/bitstring.py
Normal file
File diff suppressed because it is too large
Load Diff
BIN
python/bitstring/doc/bitstring_manual.pdf
Normal file
BIN
python/bitstring/doc/bitstring_manual.pdf
Normal file
Binary file not shown.
1523
python/bitstring/release_notes.txt
Normal file
1523
python/bitstring/release_notes.txt
Normal file
File diff suppressed because it is too large
Load Diff
44
python/bitstring/setup.py
Normal file
44
python/bitstring/setup.py
Normal file
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python
|
||||
from distutils.core import setup
|
||||
# from distutils.extension import Extension
|
||||
# from Cython.Distutils import build_ext
|
||||
import sys
|
||||
|
||||
kwds = {'long_description': open('README.txt').read()}
|
||||
|
||||
if sys.version_info[:2] < (2, 6):
|
||||
raise Exception('This version of bitstring needs Python 2.6 or later. '
|
||||
'For Python 2.4 / 2.5 please use bitstring version 1.0 instead.')
|
||||
|
||||
# macros = [('PYREX_WITHOUT_ASSERTIONS', None)]
|
||||
# ext_modules = [Extension('bitstring', ["bitstring.pyx"], define_macros=macros)]
|
||||
|
||||
setup(name='bitstring',
|
||||
version='3.1.3',
|
||||
description='Simple construction, analysis and modification of binary data.',
|
||||
author='Scott Griffiths',
|
||||
author_email='scott@griffiths.name',
|
||||
url='http://python-bitstring.googlecode.com',
|
||||
download_url='http://python-bitstring.googlecode.com',
|
||||
license='The MIT License: http://www.opensource.org/licenses/mit-license.php',
|
||||
# cmdclass = {'build_ext': build_ext},
|
||||
# ext_modules = ext_modules,
|
||||
py_modules=['bitstring'],
|
||||
platforms='all',
|
||||
classifiers = [
|
||||
'Development Status :: 5 - Production/Stable',
|
||||
'Intended Audience :: Developers',
|
||||
'Operating System :: OS Independent',
|
||||
'License :: OSI Approved :: MIT License',
|
||||
'Programming Language :: Python :: 2.6',
|
||||
'Programming Language :: Python :: 2.7',
|
||||
'Programming Language :: Python :: 3',
|
||||
'Programming Language :: Python :: 3.0',
|
||||
'Programming Language :: Python :: 3.1',
|
||||
'Programming Language :: Python :: 3.2',
|
||||
'Programming Language :: Python :: 3.3',
|
||||
'Topic :: Software Development :: Libraries :: Python Modules',
|
||||
],
|
||||
**kwds
|
||||
)
|
||||
|
||||
1
python/bitstring/test/smalltestfile
Normal file
1
python/bitstring/test/smalltestfile
Normal file
@@ -0,0 +1 @@
|
||||
#Eg<45><67><EFBFBD><EFBFBD>
|
||||
BIN
python/bitstring/test/test.m1v
Normal file
BIN
python/bitstring/test/test.m1v
Normal file
Binary file not shown.
310
python/bitstring/test/test_bitarray.py
Normal file
310
python/bitstring/test/test_bitarray.py
Normal file
@@ -0,0 +1,310 @@
|
||||
#!/usr/bin/env python
|
||||
"""
|
||||
Unit tests for the bitarray module.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, '..')
|
||||
import bitstring
|
||||
from bitstring import BitArray
|
||||
|
||||
class All(unittest.TestCase):
|
||||
def testCreationFromUint(self):
|
||||
s = BitArray(uint=15, length=6)
|
||||
self.assertEqual(s.bin, '001111')
|
||||
s = BitArray(uint=0, length=1)
|
||||
self.assertEqual(s.bin, '0')
|
||||
s.uint = 1
|
||||
self.assertEqual(s.uint, 1)
|
||||
s = BitArray(length=8)
|
||||
s.uint = 0
|
||||
self.assertEqual(s.uint, 0)
|
||||
s.uint = 255
|
||||
self.assertEqual(s.uint, 255)
|
||||
self.assertEqual(s.len, 8)
|
||||
self.assertRaises(bitstring.CreationError, s._setuint, 256)
|
||||
|
||||
def testCreationFromOct(self):
|
||||
s = BitArray(oct='7')
|
||||
self.assertEqual(s.oct, '7')
|
||||
self.assertEqual(s.bin, '111')
|
||||
s.append('0o1')
|
||||
self.assertEqual(s.bin, '111001')
|
||||
s.oct = '12345670'
|
||||
self.assertEqual(s.length, 24)
|
||||
self.assertEqual(s.bin, '001010011100101110111000')
|
||||
s = BitArray('0o123')
|
||||
self.assertEqual(s.oct, '123')
|
||||
|
||||
|
||||
class NoPosAttribute(unittest.TestCase):
|
||||
def testReplace(self):
|
||||
s = BitArray('0b01')
|
||||
s.replace('0b1', '0b11')
|
||||
self.assertEqual(s, '0b011')
|
||||
|
||||
def testDelete(self):
|
||||
s = BitArray('0b000000001')
|
||||
del s[-1:]
|
||||
self.assertEqual(s, '0b00000000')
|
||||
|
||||
def testInsert(self):
|
||||
s = BitArray('0b00')
|
||||
s.insert('0xf', 1)
|
||||
self.assertEqual(s, '0b011110')
|
||||
|
||||
def testInsertParameters(self):
|
||||
s = BitArray('0b111')
|
||||
self.assertRaises(TypeError, s.insert, '0x4')
|
||||
|
||||
def testOverwrite(self):
|
||||
s = BitArray('0b01110')
|
||||
s.overwrite('0b000', 1)
|
||||
self.assertEqual(s, '0b00000')
|
||||
|
||||
def testOverwriteParameters(self):
|
||||
s = BitArray('0b0000')
|
||||
self.assertRaises(TypeError, s.overwrite, '0b111')
|
||||
|
||||
def testPrepend(self):
|
||||
s = BitArray('0b0')
|
||||
s.prepend([1])
|
||||
self.assertEqual(s, [1, 0])
|
||||
|
||||
def testRol(self):
|
||||
s = BitArray('0b0001')
|
||||
s.rol(1)
|
||||
self.assertEqual(s, '0b0010')
|
||||
|
||||
def testRor(self):
|
||||
s = BitArray('0b1000')
|
||||
s.ror(1)
|
||||
self.assertEqual(s, '0b0100')
|
||||
|
||||
def testSetItem(self):
|
||||
s = BitArray('0b000100')
|
||||
s[4:5] = '0xf'
|
||||
self.assertEqual(s, '0b000111110')
|
||||
s[0:1] = [1]
|
||||
self.assertEqual(s, '0b100111110')
|
||||
|
||||
|
||||
class Bugs(unittest.TestCase):
|
||||
def testAddingNonsense(self):
|
||||
a = BitArray([0])
|
||||
a += '0' # a uint of length 0 - so nothing gets added.
|
||||
self.assertEqual(a, [0])
|
||||
self.assertRaises(ValueError, a.__iadd__, '3')
|
||||
self.assertRaises(ValueError, a.__iadd__, 'se')
|
||||
self.assertRaises(ValueError, a.__iadd__, 'float:32')
|
||||
|
||||
def testPrependAfterCreationFromDataWithOffset(self):
|
||||
s1 = BitArray(bytes=b'\x00\x00\x07\xff\xf0\x00', offset=21, length=15)
|
||||
self.assertFalse(s1.any(0))
|
||||
s1.prepend('0b0')
|
||||
self.assertEqual(s1.bin, '0111111111111111')
|
||||
s1.prepend('0b0')
|
||||
self.assertEqual(s1.bin, '00111111111111111')
|
||||
|
||||
|
||||
class ByteAligned(unittest.TestCase):
|
||||
def testDefault(self, defaultbytealigned=bitstring.bytealigned):
|
||||
self.assertFalse(defaultbytealigned)
|
||||
|
||||
def testChangingIt(self):
|
||||
bitstring.bytealigned = True
|
||||
self.assertTrue(bitstring.bytealigned)
|
||||
bitstring.bytealigned = False
|
||||
|
||||
def testNotByteAligned(self):
|
||||
bitstring.bytealigned = False
|
||||
a = BitArray('0x00 ff 0f f')
|
||||
l = list(a.findall('0xff'))
|
||||
self.assertEqual(l, [8, 20])
|
||||
p = a.find('0x0f')[0]
|
||||
self.assertEqual(p, 4)
|
||||
p = a.rfind('0xff')[0]
|
||||
self.assertEqual(p, 20)
|
||||
s = list(a.split('0xff'))
|
||||
self.assertEqual(s, ['0x00', '0xff0', '0xff'])
|
||||
a.replace('0xff', '')
|
||||
self.assertEqual(a, '0x000')
|
||||
|
||||
def testByteAligned(self):
|
||||
bitstring.bytealigned = True
|
||||
a = BitArray('0x00 ff 0f f')
|
||||
l = list(a.findall('0xff'))
|
||||
self.assertEqual(l, [8])
|
||||
p = a.find('0x0f')[0]
|
||||
self.assertEqual(p, 16)
|
||||
p = a.rfind('0xff')[0]
|
||||
self.assertEqual(p, 8)
|
||||
s = list(a.split('0xff'))
|
||||
self.assertEqual(s, ['0x00', '0xff0ff'])
|
||||
a.replace('0xff', '')
|
||||
self.assertEqual(a, '0x000ff')
|
||||
|
||||
|
||||
class SliceAssignment(unittest.TestCase):
|
||||
|
||||
def testSliceAssignmentSingleBit(self):
|
||||
a = BitArray('0b000')
|
||||
a[2] = '0b1'
|
||||
self.assertEqual(a.bin, '001')
|
||||
a[0] = BitArray(bin='1')
|
||||
self.assertEqual(a.bin, '101')
|
||||
a[-1] = '0b0'
|
||||
self.assertEqual(a.bin, '100')
|
||||
a[-3] = '0b0'
|
||||
self.assertEqual(a.bin, '000')
|
||||
|
||||
def testSliceAssignmentSingleBitErrors(self):
|
||||
a = BitArray('0b000')
|
||||
self.assertRaises(IndexError, a.__setitem__, -4, '0b1')
|
||||
self.assertRaises(IndexError, a.__setitem__, 3, '0b1')
|
||||
self.assertRaises(TypeError, a.__setitem__, 1, 1.3)
|
||||
|
||||
def testSliceAssignmentMulipleBits(self):
|
||||
a = BitArray('0b0')
|
||||
a[0] = '0b110'
|
||||
self.assertEqual(a.bin, '110')
|
||||
a[0] = '0b000'
|
||||
self.assertEqual(a.bin, '00010')
|
||||
a[0:3] = '0b111'
|
||||
self.assertEqual(a.bin, '11110')
|
||||
a[-2:] = '0b011'
|
||||
self.assertEqual(a.bin, '111011')
|
||||
a[:] = '0x12345'
|
||||
self.assertEqual(a.hex, '12345')
|
||||
a[:] = ''
|
||||
self.assertFalse(a)
|
||||
|
||||
def testSliceAssignmentMultipleBitsErrors(self):
|
||||
a = BitArray()
|
||||
self.assertRaises(IndexError, a.__setitem__, 0, '0b00')
|
||||
a += '0b1'
|
||||
a[0:2] = '0b11'
|
||||
self.assertEqual(a, '0b11')
|
||||
|
||||
def testDelSliceStep(self):
|
||||
a = BitArray(bin='100111101001001110110100101')
|
||||
del a[::2]
|
||||
self.assertEqual(a.bin, '0110010101100')
|
||||
del a[3:9:3]
|
||||
self.assertEqual(a.bin, '01101101100')
|
||||
del a[2:7:1]
|
||||
self.assertEqual(a.bin, '011100')
|
||||
del a[::99]
|
||||
self.assertEqual(a.bin, '11100')
|
||||
del a[::1]
|
||||
self.assertEqual(a.bin, '')
|
||||
|
||||
def testDelSliceNegativeStep(self):
|
||||
a = BitArray('0b0001011101101100100110000001')
|
||||
del a[5:23:-3]
|
||||
self.assertEqual(a.bin, '0001011101101100100110000001')
|
||||
del a[25:3:-3]
|
||||
self.assertEqual(a.bin, '00011101010000100001')
|
||||
del a[:6:-7]
|
||||
self.assertEqual(a.bin, '000111010100010000')
|
||||
del a[15::-2]
|
||||
self.assertEqual(a.bin, '0010000000')
|
||||
del a[::-1]
|
||||
self.assertEqual(a.bin, '')
|
||||
|
||||
def testDelSliceErrors(self):
|
||||
a = BitArray(10)
|
||||
del a[5:3]
|
||||
self.assertEqual(a, 10)
|
||||
del a[3:5:-1]
|
||||
self.assertEqual(a, 10)
|
||||
|
||||
def testDelSingleElement(self):
|
||||
a = BitArray('0b0010011')
|
||||
del a[-1]
|
||||
self.assertEqual(a.bin, '001001')
|
||||
del a[2]
|
||||
self.assertEqual(a.bin, '00001')
|
||||
try:
|
||||
del a[5]
|
||||
self.assertTrue(False)
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
def testSetSliceStep(self):
|
||||
a = BitArray(bin='0000000000')
|
||||
a[::2] = '0b11111'
|
||||
self.assertEqual(a.bin, '1010101010')
|
||||
a[4:9:3] = [0, 0]
|
||||
self.assertEqual(a.bin, '1010001010')
|
||||
a[7:3:-1] = [1, 1, 1, 0]
|
||||
self.assertEqual(a.bin, '1010011110')
|
||||
a[7:1:-2] = [0, 0, 1]
|
||||
self.assertEqual(a.bin, '1011001010')
|
||||
a[::-5] = [1, 1]
|
||||
self.assertEqual(a.bin, '1011101011')
|
||||
a[::-1] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
|
||||
self.assertEqual(a.bin, '1000000000')
|
||||
|
||||
def testSetSliceErrors(self):
|
||||
a = BitArray(8)
|
||||
try:
|
||||
a[::3] = [1]
|
||||
self.assertTrue(False)
|
||||
except ValueError:
|
||||
pass
|
||||
class A(object): pass
|
||||
try:
|
||||
a[1:2] = A()
|
||||
self.assertTrue(False)
|
||||
except TypeError:
|
||||
pass
|
||||
try:
|
||||
a[1:4:-1] = [1, 2]
|
||||
self.assertTrue(False)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
|
||||
class Subclassing(unittest.TestCase):
|
||||
|
||||
def testIsInstance(self):
|
||||
class SubBits(BitArray): pass
|
||||
a = SubBits()
|
||||
self.assertTrue(isinstance(a, SubBits))
|
||||
|
||||
def testClassType(self):
|
||||
class SubBits(BitArray): pass
|
||||
self.assertEqual(SubBits().__class__, SubBits)
|
||||
|
||||
|
||||
class Clear(unittest.TestCase):
|
||||
|
||||
def testClear(self):
|
||||
s = BitArray('0xfff')
|
||||
s.clear()
|
||||
self.assertEqual(s.len, 0)
|
||||
|
||||
|
||||
class Copy(unittest.TestCase):
|
||||
|
||||
def testCopyMethod(self):
|
||||
s = BitArray(9)
|
||||
t = s.copy()
|
||||
self.assertEqual(s, t)
|
||||
t[0] = True
|
||||
self.assertEqual(t.bin, '100000000')
|
||||
self.assertEqual(s.bin, '000000000')
|
||||
|
||||
|
||||
class ModifiedByAddingBug(unittest.TestCase):
|
||||
|
||||
def testAdding(self):
|
||||
a = BitArray('0b0')
|
||||
b = BitArray('0b11')
|
||||
c = a + b
|
||||
self.assertEqual(c, '0b011')
|
||||
self.assertEqual(a, '0b0')
|
||||
self.assertEqual(b, '0b11')
|
||||
378
python/bitstring/test/test_bits.py
Normal file
378
python/bitstring/test/test_bits.py
Normal file
@@ -0,0 +1,378 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import unittest
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, '..')
|
||||
import bitstring
|
||||
from bitstring import MmapByteArray
|
||||
from bitstring import Bits, BitArray, ConstByteStore, ByteStore
|
||||
|
||||
class Creation(unittest.TestCase):
|
||||
def testCreationFromBytes(self):
|
||||
s = Bits(bytes=b'\xa0\xff')
|
||||
self.assertEqual((s.len, s.hex), (16, 'a0ff'))
|
||||
s = Bits(bytes=b'abc', length=0)
|
||||
self.assertEqual(s, '')
|
||||
|
||||
def testCreationFromBytesErrors(self):
|
||||
self.assertRaises(bitstring.CreationError, Bits, bytes=b'abc', length=25)
|
||||
|
||||
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 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 testCreationFromHexWithWhitespace(self):
|
||||
s = Bits(hex=' \n0 X a 4e \r3 \n')
|
||||
self.assertEqual(s.hex, 'a4e3')
|
||||
|
||||
def testCreationFromHexErrors(self):
|
||||
self.assertRaises(bitstring.CreationError, Bits, hex='0xx0')
|
||||
self.assertRaises(bitstring.CreationError, Bits, hex='0xX0')
|
||||
self.assertRaises(bitstring.CreationError, Bits, hex='0Xx0')
|
||||
self.assertRaises(bitstring.CreationError, Bits, hex='-2e')
|
||||
# These really should fail, but it's awkward and not a big deal...
|
||||
# self.assertRaises(bitstring.CreationError, Bits, '0x2', length=2)
|
||||
# self.assertRaises(bitstring.CreationError, Bits, '0x3', offset=1)
|
||||
|
||||
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 testCreationFromBinWithWhitespace(self):
|
||||
s = Bits(bin=' \r\r\n0 B 00 1 1 \t0 ')
|
||||
self.assertEqual(s.bin, '00110')
|
||||
|
||||
def testCreationFromOctErrors(self):
|
||||
s = Bits('0b00011')
|
||||
self.assertRaises(bitstring.InterpretError, s._getoct)
|
||||
self.assertRaises(bitstring.CreationError, s._setoct, '8')
|
||||
|
||||
def testCreationFromUintWithOffset(self):
|
||||
self.assertRaises(bitstring.Error, Bits, uint=12, length=8, offset=1)
|
||||
|
||||
def testCreationFromUintErrors(self):
|
||||
self.assertRaises(bitstring.CreationError, Bits, uint=-1, length=10)
|
||||
self.assertRaises(bitstring.CreationError, Bits, uint=12)
|
||||
self.assertRaises(bitstring.CreationError, Bits, uint=4, length=2)
|
||||
self.assertRaises(bitstring.CreationError, Bits, uint=0, length=0)
|
||||
self.assertRaises(bitstring.CreationError, Bits, uint=12, length=-12)
|
||||
|
||||
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 testCreationFromIntErrors(self):
|
||||
self.assertRaises(bitstring.CreationError, Bits, int=-1, length=0)
|
||||
self.assertRaises(bitstring.CreationError, Bits, int=12)
|
||||
self.assertRaises(bitstring.CreationError, Bits, int=4, length=3)
|
||||
self.assertRaises(bitstring.CreationError, Bits, int=-5, length=3)
|
||||
|
||||
def testCreationFromSe(self):
|
||||
for i in range(-100, 10):
|
||||
s = Bits(se=i)
|
||||
self.assertEqual(s.se, i)
|
||||
|
||||
def testCreationFromSeWithOffset(self):
|
||||
self.assertRaises(bitstring.CreationError, Bits, se=-13, offset=1)
|
||||
|
||||
def testCreationFromSeErrors(self):
|
||||
self.assertRaises(bitstring.CreationError, Bits, se=-5, length=33)
|
||||
s = Bits(bin='001000')
|
||||
self.assertRaises(bitstring.InterpretError, s._getse)
|
||||
|
||||
def testCreationFromUe(self):
|
||||
[self.assertEqual(Bits(ue=i).ue, i) for i in range(0, 20)]
|
||||
|
||||
def testCreationFromUeWithOffset(self):
|
||||
self.assertRaises(bitstring.CreationError, Bits, ue=104, offset=2)
|
||||
|
||||
def testCreationFromUeErrors(self):
|
||||
self.assertRaises(bitstring.CreationError, Bits, ue=-1)
|
||||
self.assertRaises(bitstring.CreationError, Bits, ue=1, length=12)
|
||||
s = Bits(bin='10')
|
||||
self.assertRaises(bitstring.InterpretError, s._getue)
|
||||
|
||||
def testCreationFromBool(self):
|
||||
a = Bits('bool=1')
|
||||
self.assertEqual(a, 'bool=1')
|
||||
b = Bits('bool=0')
|
||||
self.assertEqual(b, [0])
|
||||
c = bitstring.pack('2*bool', 0, 1)
|
||||
self.assertEqual(c, '0b01')
|
||||
|
||||
def testCreationKeywordError(self):
|
||||
self.assertRaises(bitstring.CreationError, Bits, squirrel=5)
|
||||
|
||||
def testDataStoreType(self):
|
||||
a = Bits('0xf')
|
||||
self.assertEqual(type(a._datastore), bitstring.ConstByteStore)
|
||||
|
||||
|
||||
class Initialisation(unittest.TestCase):
|
||||
def testEmptyInit(self):
|
||||
a = Bits()
|
||||
self.assertEqual(a, '')
|
||||
|
||||
def testNoPos(self):
|
||||
a = Bits('0xabcdef')
|
||||
try:
|
||||
a.pos
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
assert False
|
||||
|
||||
def testFind(self):
|
||||
a = Bits('0xabcd')
|
||||
r = a.find('0xbc')
|
||||
self.assertEqual(r[0], 4)
|
||||
r = a.find('0x23462346246', bytealigned=True)
|
||||
self.assertFalse(r)
|
||||
|
||||
def testRfind(self):
|
||||
a = Bits('0b11101010010010')
|
||||
b = a.rfind('0b010')
|
||||
self.assertEqual(b[0], 11)
|
||||
|
||||
def testFindAll(self):
|
||||
a = Bits('0b0010011')
|
||||
b = list(a.findall([1]))
|
||||
self.assertEqual(b, [2, 5, 6])
|
||||
|
||||
|
||||
class Cut(unittest.TestCase):
|
||||
def testCut(self):
|
||||
s = Bits(30)
|
||||
for t in s.cut(3):
|
||||
self.assertEqual(t, [0] * 3)
|
||||
|
||||
|
||||
class InterleavedExpGolomb(unittest.TestCase):
|
||||
def testCreation(self):
|
||||
s1 = Bits(uie=0)
|
||||
s2 = Bits(uie=1)
|
||||
self.assertEqual(s1, [1])
|
||||
self.assertEqual(s2, [0, 0, 1])
|
||||
s1 = Bits(sie=0)
|
||||
s2 = Bits(sie=-1)
|
||||
s3 = Bits(sie=1)
|
||||
self.assertEqual(s1, [1])
|
||||
self.assertEqual(s2, [0, 0, 1, 1])
|
||||
self.assertEqual(s3, [0, 0, 1, 0])
|
||||
|
||||
def testCreationFromProperty(self):
|
||||
s = BitArray()
|
||||
s.uie = 45
|
||||
self.assertEqual(s.uie, 45)
|
||||
s.sie = -45
|
||||
self.assertEqual(s.sie, -45)
|
||||
|
||||
def testInterpretation(self):
|
||||
for x in range(101):
|
||||
self.assertEqual(Bits(uie=x).uie, x)
|
||||
for x in range(-100, 100):
|
||||
self.assertEqual(Bits(sie=x).sie, x)
|
||||
|
||||
def testErrors(self):
|
||||
for f in ['sie=100, 0b1001', '0b00', 'uie=100, 0b1001']:
|
||||
s = Bits(f)
|
||||
self.assertRaises(bitstring.InterpretError, s._getsie)
|
||||
self.assertRaises(bitstring.InterpretError, s._getuie)
|
||||
self.assertRaises(ValueError, Bits, 'uie=-10')
|
||||
|
||||
|
||||
class FileBased(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.a = Bits(filename='smalltestfile')
|
||||
self.b = Bits(filename='smalltestfile', offset=16)
|
||||
self.c = Bits(filename='smalltestfile', offset=20, length=16)
|
||||
self.d = Bits(filename='smalltestfile', offset=20, length=4)
|
||||
|
||||
def testCreationWithOffset(self):
|
||||
self.assertEqual(self.a, '0x0123456789abcdef')
|
||||
self.assertEqual(self.b, '0x456789abcdef')
|
||||
self.assertEqual(self.c, '0x5678')
|
||||
|
||||
def testBitOperators(self):
|
||||
x = self.b[4:20]
|
||||
self.assertEqual(x, '0x5678')
|
||||
self.assertEqual((x & self.c).hex, self.c.hex)
|
||||
self.assertEqual(self.c ^ self.b[4:20], 16)
|
||||
self.assertEqual(self.a[23:36] | self.c[3:], self.c[3:])
|
||||
|
||||
def testAddition(self):
|
||||
h = self.d + '0x1'
|
||||
x = self.a[20:24] + self.c[-4:] + self.c[8:12]
|
||||
self.assertEqual(x, '0x587')
|
||||
x = self.b + x
|
||||
self.assertEqual(x.hex, '456789abcdef587')
|
||||
x = BitArray(x)
|
||||
del x[12:24]
|
||||
self.assertEqual(x, '0x456abcdef587')
|
||||
|
||||
class Mmap(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.f = open('smalltestfile', 'rb')
|
||||
|
||||
def tearDown(self):
|
||||
self.f.close()
|
||||
|
||||
def testByteArrayEquivalence(self):
|
||||
a = MmapByteArray(self.f)
|
||||
self.assertEqual(a.bytelength, 8)
|
||||
self.assertEqual(len(a), 8)
|
||||
self.assertEqual(a[0], 0x01)
|
||||
self.assertEqual(a[1], 0x23)
|
||||
self.assertEqual(a[7], 0xef)
|
||||
self.assertEqual(a[0:1], bytearray([1]))
|
||||
self.assertEqual(a[:], bytearray([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]))
|
||||
self.assertEqual(a[2:4], bytearray([0x45, 0x67]))
|
||||
|
||||
def testWithLength(self):
|
||||
a = MmapByteArray(self.f, 3)
|
||||
self.assertEqual(a[0], 0x01)
|
||||
self.assertEqual(len(a), 3)
|
||||
|
||||
def testWithOffset(self):
|
||||
a = MmapByteArray(self.f, None, 5)
|
||||
self.assertEqual(len(a), 3)
|
||||
self.assertEqual(a[0], 0xab)
|
||||
|
||||
def testWithLengthAndOffset(self):
|
||||
a = MmapByteArray(self.f, 3, 3)
|
||||
self.assertEqual(len(a), 3)
|
||||
self.assertEqual(a[0], 0x67)
|
||||
self.assertEqual(a[:], bytearray([0x67, 0x89, 0xab]))
|
||||
|
||||
|
||||
class Comparisons(unittest.TestCase):
|
||||
def testUnorderable(self):
|
||||
a = Bits(5)
|
||||
b = Bits(5)
|
||||
self.assertRaises(TypeError, a.__lt__, b)
|
||||
self.assertRaises(TypeError, a.__gt__, b)
|
||||
self.assertRaises(TypeError, a.__le__, b)
|
||||
self.assertRaises(TypeError, a.__ge__, b)
|
||||
|
||||
|
||||
class Subclassing(unittest.TestCase):
|
||||
|
||||
def testIsInstance(self):
|
||||
class SubBits(bitstring.Bits): pass
|
||||
a = SubBits()
|
||||
self.assertTrue(isinstance(a, SubBits))
|
||||
|
||||
def testClassType(self):
|
||||
class SubBits(bitstring.Bits): pass
|
||||
self.assertEqual(SubBits().__class__, SubBits)
|
||||
|
||||
|
||||
class LongBoolConversion(unittest.TestCase):
|
||||
|
||||
def testLongBool(self):
|
||||
a = Bits(1000)
|
||||
b = bool(a)
|
||||
self.assertTrue(b is False)
|
||||
|
||||
|
||||
# Some basic tests for the private ByteStore classes
|
||||
|
||||
class ConstByteStoreCreation(unittest.TestCase):
|
||||
|
||||
def testProperties(self):
|
||||
a = ConstByteStore(bytearray(b'abc'))
|
||||
self.assertEqual(a.bytelength, 3)
|
||||
self.assertEqual(a.offset, 0)
|
||||
self.assertEqual(a.bitlength, 24)
|
||||
self.assertEqual(a._rawarray, b'abc')
|
||||
|
||||
def testGetBit(self):
|
||||
a = ConstByteStore(bytearray([0x0f]))
|
||||
self.assertEqual(a.getbit(0), False)
|
||||
self.assertEqual(a.getbit(3), False)
|
||||
self.assertEqual(a.getbit(4), True)
|
||||
self.assertEqual(a.getbit(7), True)
|
||||
|
||||
b = ConstByteStore(bytearray([0x0f]), 7, 1)
|
||||
self.assertEqual(b.getbit(2), False)
|
||||
self.assertEqual(b.getbit(3), True)
|
||||
|
||||
def testGetByte(self):
|
||||
a = ConstByteStore(bytearray(b'abcde'), 1, 13)
|
||||
self.assertEqual(a.getbyte(0), 97)
|
||||
self.assertEqual(a.getbyte(1), 98)
|
||||
self.assertEqual(a.getbyte(4), 101)
|
||||
|
||||
|
||||
class PadToken(unittest.TestCase):
|
||||
|
||||
def testCreation(self):
|
||||
a = Bits('pad:10')
|
||||
self.assertEqual(a, Bits(10))
|
||||
b = Bits('pad:0')
|
||||
self.assertEqual(b, Bits())
|
||||
c = Bits('0b11, pad:1, 0b111')
|
||||
self.assertEqual(c, Bits('0b110111'))
|
||||
|
||||
def testPack(self):
|
||||
s = bitstring.pack('0b11, pad:3=5, 0b1')
|
||||
self.assertEqual(s.bin, '110001')
|
||||
d = bitstring.pack('pad:c', c=12)
|
||||
self.assertEqual(d, Bits(12))
|
||||
e = bitstring.pack('0xf, uint:12, pad:1, bin, pad:4, 0b10', 0, '111')
|
||||
self.assertEqual(e.bin, '11110000000000000111000010')
|
||||
|
||||
def testUnpack(self):
|
||||
s = Bits('0b111000111')
|
||||
x, y = s.unpack('3, pad:3, 3')
|
||||
self.assertEqual((x, y), (7, 7))
|
||||
x, y = s.unpack('2, pad:2, bin')
|
||||
self.assertEqual((x, y), (3, '00111'))
|
||||
x = s.unpack('pad:1, pad:2, pad:3')
|
||||
self.assertEqual(x, [])
|
||||
|
||||
|
||||
class ModifiedByAddingBug(unittest.TestCase):
|
||||
|
||||
def testAdding(self):
|
||||
a = Bits('0b0')
|
||||
b = Bits('0b11')
|
||||
c = a + b
|
||||
self.assertEqual(c, '0b011')
|
||||
self.assertEqual(a, '0b0')
|
||||
self.assertEqual(b, '0b11')
|
||||
|
||||
def testAdding2(self):
|
||||
a = Bits(100)
|
||||
b = Bits(101)
|
||||
c = a + b
|
||||
self.assertEqual(a, 100)
|
||||
self.assertEqual(b, 101)
|
||||
self.assertEqual(c, 201)
|
||||
37
python/bitstring/test/test_bitstore.py
Normal file
37
python/bitstring/test/test_bitstore.py
Normal file
@@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import unittest
|
||||
import sys
|
||||
sys.path.insert(0, '..')
|
||||
from bitstring import ByteStore, ConstByteStore, equal, offsetcopy
|
||||
|
||||
|
||||
class OffsetCopy(unittest.TestCase):
|
||||
def testStraightCopy(self):
|
||||
s = ByteStore(bytearray([10, 5, 1]), 24, 0)
|
||||
t = offsetcopy(s, 0)
|
||||
self.assertEqual(t._rawarray, bytearray([10, 5, 1]))
|
||||
|
||||
def testOffsetIncrease(self):
|
||||
s = ByteStore(bytearray([1, 1, 1]), 24, 0)
|
||||
t = offsetcopy(s, 4)
|
||||
self.assertEqual(t.bitlength, 24)
|
||||
self.assertEqual(t.offset, 4)
|
||||
self.assertEqual(t._rawarray, bytearray([0, 16, 16, 16]))
|
||||
|
||||
|
||||
class Equals(unittest.TestCase):
|
||||
|
||||
def testBothSingleByte(self):
|
||||
s = ByteStore(bytearray([128]), 3, 0)
|
||||
t = ByteStore(bytearray([64]), 3, 1)
|
||||
u = ByteStore(bytearray([32]), 3, 2)
|
||||
self.assertTrue(equal(s, t))
|
||||
self.assertTrue(equal(s, u))
|
||||
self.assertTrue(equal(u, t))
|
||||
|
||||
def testOneSingleByte(self):
|
||||
s = ByteStore(bytearray([1, 0]), 2, 7)
|
||||
t = ByteStore(bytearray([64]), 2, 1)
|
||||
self.assertTrue(equal(s, t))
|
||||
self.assertTrue(equal(t, s))
|
||||
3940
python/bitstring/test/test_bitstream.py
Normal file
3940
python/bitstring/test/test_bitstream.py
Normal file
File diff suppressed because it is too large
Load Diff
97
python/bitstring/test/test_bitstring.py
Normal file
97
python/bitstring/test/test_bitstring.py
Normal file
@@ -0,0 +1,97 @@
|
||||
#!/usr/bin/env python
|
||||
"""
|
||||
Module-level unit tests.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
import sys
|
||||
sys.path.insert(0, '..')
|
||||
import bitstring
|
||||
import copy
|
||||
|
||||
|
||||
class ModuleData(unittest.TestCase):
|
||||
def testVersion(self):
|
||||
self.assertEqual(bitstring.__version__, '3.1.3')
|
||||
|
||||
def testAll(self):
|
||||
exported = ['ConstBitArray', 'ConstBitStream', 'BitStream', 'BitArray',
|
||||
'Bits', 'BitString', 'pack', 'Error', 'ReadError',
|
||||
'InterpretError', 'ByteAlignError', 'CreationError', 'bytealigned']
|
||||
self.assertEqual(set(bitstring.__all__), set(exported))
|
||||
|
||||
def testReverseDict(self):
|
||||
d = bitstring.BYTE_REVERSAL_DICT
|
||||
for i in range(256):
|
||||
a = bitstring.Bits(uint=i, length=8)
|
||||
b = d[i]
|
||||
self.assertEqual(a.bin[::-1], bitstring.Bits(bytes=b).bin)
|
||||
|
||||
def testAliases(self):
|
||||
self.assertTrue(bitstring.Bits is bitstring.ConstBitArray)
|
||||
self.assertTrue(bitstring.BitStream is bitstring.BitString)
|
||||
|
||||
|
||||
class MemoryUsage(unittest.TestCase):
|
||||
def testBaselineMemory(self):
|
||||
try:
|
||||
import pympler.asizeof.asizeof as size
|
||||
except ImportError:
|
||||
return
|
||||
# These values might be platform dependent, so don't fret too much.
|
||||
self.assertEqual(size(bitstring.ConstBitStream([0])), 64)
|
||||
self.assertEqual(size(bitstring.Bits([0])), 64)
|
||||
self.assertEqual(size(bitstring.BitStream([0])), 64)
|
||||
self.assertEqual(size(bitstring.BitArray([0])), 64)
|
||||
from bitstring.bitstore import ByteStore
|
||||
self.assertEqual(size(ByteStore(bytearray())), 100)
|
||||
|
||||
|
||||
class Copy(unittest.TestCase):
|
||||
def testConstBitArrayCopy(self):
|
||||
import copy
|
||||
cba = bitstring.Bits(100)
|
||||
cba_copy = copy.copy(cba)
|
||||
self.assertTrue(cba is cba_copy)
|
||||
|
||||
def testBitArrayCopy(self):
|
||||
ba = bitstring.BitArray(100)
|
||||
ba_copy = copy.copy(ba)
|
||||
self.assertFalse(ba is ba_copy)
|
||||
self.assertFalse(ba._datastore is ba_copy._datastore)
|
||||
self.assertTrue(ba == ba_copy)
|
||||
|
||||
def testConstBitStreamCopy(self):
|
||||
cbs = bitstring.ConstBitStream(100)
|
||||
cbs.pos = 50
|
||||
cbs_copy = copy.copy(cbs)
|
||||
self.assertEqual(cbs_copy.pos, 0)
|
||||
self.assertTrue(cbs._datastore is cbs_copy._datastore)
|
||||
self.assertTrue(cbs == cbs_copy)
|
||||
|
||||
def testBitStreamCopy(self):
|
||||
bs = bitstring.BitStream(100)
|
||||
bs.pos = 50
|
||||
bs_copy = copy.copy(bs)
|
||||
self.assertEqual(bs_copy.pos, 0)
|
||||
self.assertFalse(bs._datastore is bs_copy._datastore)
|
||||
self.assertTrue(bs == bs_copy)
|
||||
|
||||
|
||||
class Interning(unittest.TestCase):
|
||||
def testBits(self):
|
||||
a = bitstring.Bits('0xf')
|
||||
b = bitstring.Bits('0xf')
|
||||
self.assertTrue(a is b)
|
||||
c = bitstring.Bits('0b1111')
|
||||
self.assertFalse(a is c)
|
||||
|
||||
def testCBS(self):
|
||||
a = bitstring.ConstBitStream('0b11000')
|
||||
b = bitstring.ConstBitStream('0b11000')
|
||||
self.assertFalse(a is b)
|
||||
# self.assertTrue(a._datastore is b._datastore)
|
||||
|
||||
|
||||
|
||||
|
||||
121
python/bitstring/test/test_constbitstream.py
Normal file
121
python/bitstring/test/test_constbitstream.py
Normal file
@@ -0,0 +1,121 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import unittest
|
||||
import sys
|
||||
sys.path.insert(0, '..')
|
||||
import bitstring
|
||||
from bitstring import ConstBitStream as CBS
|
||||
|
||||
class All(unittest.TestCase):
|
||||
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))
|
||||
|
||||
|
||||
class InterleavedExpGolomb(unittest.TestCase):
|
||||
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])
|
||||
|
||||
def testReadingErrors(self):
|
||||
s = CBS(10)
|
||||
self.assertRaises(bitstring.ReadError, s.read, 'uie')
|
||||
self.assertEqual(s.pos, 0)
|
||||
self.assertRaises(bitstring.ReadError, s.read, 'sie')
|
||||
self.assertEqual(s.pos, 0)
|
||||
|
||||
|
||||
class ReadTo(unittest.TestCase):
|
||||
def testByteAligned(self):
|
||||
a = CBS('0xaabb00aa00bb')
|
||||
b = a.readto('0x00', bytealigned=True)
|
||||
self.assertEqual(b, '0xaabb00')
|
||||
self.assertEqual(a.bytepos, 3)
|
||||
b = a.readto('0xaa', bytealigned=True)
|
||||
self.assertEqual(b, '0xaa')
|
||||
self.assertRaises(bitstring.ReadError, a.readto, '0xcc', bytealigned=True)
|
||||
|
||||
def testNotAligned(self):
|
||||
a = CBS('0b00111001001010011011')
|
||||
a.pos = 1
|
||||
self.assertEqual(a.readto('0b00'), '0b011100')
|
||||
self.assertEqual(a.readto('0b110'), '0b10010100110')
|
||||
self.assertRaises(ValueError, a.readto, '')
|
||||
|
||||
def testDisallowIntegers(self):
|
||||
a = CBS('0x0f')
|
||||
self.assertRaises(ValueError, a.readto, 4)
|
||||
|
||||
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')
|
||||
|
||||
|
||||
class Subclassing(unittest.TestCase):
|
||||
|
||||
def testIsInstance(self):
|
||||
class SubBits(CBS): pass
|
||||
a = SubBits()
|
||||
self.assertTrue(isinstance(a, SubBits))
|
||||
|
||||
def testClassType(self):
|
||||
class SubBits(CBS): pass
|
||||
self.assertEqual(SubBits().__class__, SubBits)
|
||||
|
||||
|
||||
class PadToken(unittest.TestCase):
|
||||
|
||||
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)
|
||||
|
||||
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])
|
||||
|
||||
class ReadingBytes(unittest.TestCase):
|
||||
|
||||
def testUnpackingBytes(self):
|
||||
s = CBS(80)
|
||||
t = s.unpack('bytes:1')
|
||||
self.assertEqual(t[0], b'\x00')
|
||||
a, b, c = s.unpack('bytes:1, bytes, bytes:2')
|
||||
self.assertEqual(a, b'\x00')
|
||||
self.assertEqual(b, b'\x00'*7)
|
||||
self.assertEqual(c, b'\x00'*2)
|
||||
|
||||
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])
|
||||
|
||||
Reference in New Issue
Block a user