Esempio n. 1
0
    def test_example(self):
        data = ['a', 'b', 'c', '\x00', 1, 2, 3, 'x', 'y', 'z', '\x00']
        fmt = self.endianness + '4c iqf 4c'
        binary_data = struct.pack(fmt, *data)
        bytestr = tdf_file.TDFByteStream(cStringIO.StringIO(binary_data))
        bitstr = tdf_file.TDFBitStream(bytes=binary_data)

        # test reading a two character string
        self.assertEqual(bytestr.read_string(2), bitstr.read_string(2))
        # test reading until the null character
        self.assertEqual(bytestr.read_string(), bitstr.read_string())
        # test reading numbers
        self.assertEqual(bytestr.read_integer(), bitstr.read_integer())
        self.assertEqual(bytestr.read_long(), bitstr.read_long())
        self.assertEqual(bytestr.read_float(), bitstr.read_float())
        # test reading until the EOF
        self.assertEqual(bytestr.read_string(), bitstr.read_string())
        # read bytes (first reset the offset to the beginning of the binary
        # data stream)
        bytestr = tdf_file.TDFByteStream(cStringIO.StringIO(binary_data))
        bitstr = tdf_file.TDFBitStream(bytes=binary_data)
        self.assertEqual(bytestr.read_bytes(3), bitstr.read_bytes(3))
        # test update offset (
        bytestr = tdf_file.TDFByteStream(cStringIO.StringIO(binary_data))
        bitstr = tdf_file.TDFBitStream(bytes=binary_data)
        self.assertEqual(bytestr.update_offset(None),
                         bitstr.update_offset(None))
        self.assertEqual(bytestr.update_offset(5), bitstr.update_offset(5))
Esempio n. 2
0
 def setUp(self):
     self.data = ['a', 'b', 'c']
     self.fmt = self.endianness + 'ccc'  # three one-byte characters
     self.binary_data = struct.pack(self.fmt, *self.data)
     self.file_object = cStringIO.StringIO(self.binary_data)
     self.tdf = tdf_file.TDFByteStream(self.file_object)
     self.length = struct.calcsize(self.fmt)  # total number of bytes used to encode the data
Esempio n. 3
0
 def test_read_null_terminated_string(self):
     # if number of bytes not provided, we should read until null character
     self.data += ['\x00', 'd']
     self.fmt += 'cc'  # additional four one-byte characters
     self.binary_data = struct.pack(self.fmt, *self.data)
     self.file_object = cStringIO.StringIO(self.binary_data)
     self.tdf = tdf_file.TDFByteStream(self.file_object)
     self.assertEqual(self.tdf.read_string(), ''.join(self.data[0:3]))
Esempio n. 4
0
    def test_read_integer(self):
        """Test reading four byte integers"""
        data = [1, 2, 3]
        fmt = self.endianness + 'iih'  # two ints and a short
        binary_data = struct.pack(fmt, *data)
        bytestr = tdf_file.TDFByteStream(cStringIO.StringIO(binary_data))
        bitstr = tdf_file.TDFBitStream(bytes=binary_data)

        self.assertEqual(bytestr.read_integer(), bitstr.read_integer())
        # test automatic advancing to the next integer
        self.assertEqual(bytestr.read_integer(), bitstr.read_integer())
Esempio n. 5
0
    def test_read_long(self):
        '''Test reading eight byte integers.

        '''
        data = [1, 2, 3]
        fmt = self.endianness + 'qqh'   # two long ints and a short
        binary_data = struct.pack(fmt, *data)
        bytestr = tdf_file.TDFByteStream(cStringIO.StringIO(binary_data))
        bitstr = tdf_file.TDFBitStream(bytes=binary_data)

        self.assertEqual(bytestr.read_long(), bitstr.read_long())
        # test automatic advancing to the next integer
        self.assertEqual(bytestr.read_long(), bitstr.read_long())
Esempio n. 6
0
    def test_update_offset(self):
        """Test moving around the file."""
        data = [1, 2, 3]
        fmt = self.endianness + 'iii'
        binary_data = struct.pack(fmt, *data)
        bytestr = tdf_file.TDFByteStream(cStringIO.StringIO(binary_data))
        bitstr = tdf_file.TDFBitStream(bytes=binary_data)

        # check if the initial position is set to the beginning of the file
        self.assertEqual(bytestr.update_offset(None),
                         bitstr.update_offset(None))
        # go to random positions in the file
        self.assertEqual(bytestr.update_offset(2), bitstr.update_offset(2))
        self.assertEqual(bytestr.update_offset(1), bitstr.update_offset(1))
        # check if we can seek past the end of the file
        self.assertEqual(bytestr.update_offset(len(data) + 1),
                         bitstr.update_offset(len(data) + 1))
Esempio n. 7
0
 def setUp(self):
     self.data = 'a'
     self.fmt = self.endianness + 'c'  # char - one byte long
     self.binary_data = struct.pack(self.fmt, *self.data)
     self.file_object = cStringIO.StringIO(self.binary_data)
     self.tdf = tdf_file.TDFByteStream(self.file_object)
Esempio n. 8
0
 def setUp(self):
     self.data = [1.1, 2.2, 3.3]
     self.fmt = self.endianness + 'fff'  # three floats
     self.binary_data = struct.pack(self.fmt, *self.data)
     self.file_object = cStringIO.StringIO(self.binary_data)
     self.tdf = tdf_file.TDFByteStream(self.file_object)
Esempio n. 9
0
 def setUp(self):
     self.data = [1, 2, 3]
     self.fmt = self.endianness + 'qqq'  # three longs
     self.binary_data = struct.pack(self.fmt, *self.data)
     self.file_object = cStringIO.StringIO(self.binary_data)
     self.tdf = tdf_file.TDFByteStream(self.file_object)