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))
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())
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())
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))