def test_read(self): """Tests various reaad operations with FilePart.""" filename = 'test_read' contents = bytearray(range(256)) part_length = 23 start_pos = 50 fpath = self.CreateTempFile(file_name=filename, contents=contents) # Read in the whole file. fp = FilePart(fpath, start_pos, part_length) whole_file = fp.read() self.assertEqual(contents[start_pos:(start_pos + part_length)], whole_file) # Read in a piece of the file from the beginning. fp.seek(0) offset = 10 partial_file = fp.read(offset) self.assertEqual(contents[start_pos:(start_pos + offset)], partial_file) # Read in the rest of the file. remaining_file = fp.read(part_length - offset) self.assertEqual(contents[(start_pos + offset):(start_pos + part_length)], remaining_file) self.assertEqual(contents[start_pos:(start_pos + part_length)], partial_file + remaining_file) # Try to read after reaching EOF. empty_file = fp.read(100) self.assertEqual(b'', empty_file) empty_file = fp.read() self.assertEqual(b'', empty_file)
def test_read(self): filename = 'test_read' contents = "" for i in range(1, 256): contents += str(i) part_length = 23 start_pos = 50 fpath = self.CreateTempFile(file_name=filename, contents=contents) # Read in the whole file. fp = FilePart(fpath, start_pos, part_length) whole_file = fp.read() self.assertEqual(contents[start_pos:(start_pos + part_length)], whole_file) # Read in a piece of the file from the beginning. fp.seek(0) offset = 10 partial_file = fp.read(offset) self.assertEqual( contents[start_pos:(start_pos + offset)], partial_file) # Read in the rest of the file. remaining_file = fp.read(part_length - offset) self.assertEqual( contents[(start_pos + offset):(start_pos + part_length)], remaining_file) self.assertEqual( contents[start_pos:(start_pos + part_length)], partial_file + remaining_file) # Try to read after reaching EOF. empty_file = fp.read(100) self.assertEqual('', empty_file) empty_file = fp.read() self.assertEqual('', empty_file)
def test_read(self): filename = 'test_read' contents = "" for i in range(1, 256): contents += str(i) part_length = 23 start_pos = 50 fpath = self.CreateTempFile(file_name=filename, contents=contents) # Read in the whole file. fp = FilePart(fpath, start_pos, part_length) whole_file = fp.read() self.assertEqual(contents[start_pos:(start_pos + part_length)], whole_file) # Read in a piece of the file from the beginning. fp.seek(0) offset = 10 partial_file = fp.read(offset) self.assertEqual(contents[start_pos:(start_pos + offset)], partial_file) # Read in the rest of the file. remaining_file = fp.read(part_length - offset) self.assertEqual( contents[(start_pos + offset):(start_pos + part_length)], remaining_file) self.assertEqual(contents[start_pos:(start_pos + part_length)], partial_file + remaining_file) # Try to read after reaching EOF. empty_file = fp.read(100) self.assertEqual('', empty_file) empty_file = fp.read() self.assertEqual('', empty_file)