def test_read(self): sio = StringIO.StringIO("ab\x00\x00cdef\x00gh") f = ZeroRangesAdapter(sio, block_size = 1) self.assertEqual(f.zero_ranges(), [{'start': 2, 'end': 4}, {'start': 8, 'end': 9}]) self.assertEqual(f.read(5), "abcde") self.assertEqual(f.read(2), "fg") self.assertEqual(f.read(2), "h") self.assertEqual(f.read(2), "")
def test_different_block_size(self): sio = StringIO.StringIO("a" * 1024 + "\x00"*1024 + "c" * 1024 + "\x00"*1024 + "d" *1024 + "\x00"*1023) f = ZeroRangesAdapter(sio, block_size = 1024) self.assertEqual(f.zero_ranges(), [{'end': 2048, 'start': 1024}, {'end': 4096, 'start': 3072}]) f.seek(0) self.assertEqual(f.read(), "a"*1024 + "c" * 1024 + "d" * 1024 + "\x00" * 1023) f.seek(1024*3+1022) self.assertEqual(f.read(), "\x00")
def test_seek(self): sio = StringIO.StringIO("ab\x00\x00cdef\x00gh") f = ZeroRangesAdapter(sio, block_size = 1) self.assertEqual(f.zero_ranges(), [{'start': 2, 'end': 4}, {'start': 8, 'end': 9}]) f.seek(5) self.assertEqual(f.read(2), "fg") self.assertEqual(f.read(2), "h") self.assertEqual(f.read(2), "") f.seek(3, os.SEEK_END) self.assertEqual(f.read(), "fgh") f.seek(1000, os.SEEK_END) self.assertEqual(f.read(), "abcdefgh")
def test_random_read(self): data_len = 1024*1023 pad_block_size = 1024 data = "".join([chr(random.randint(ord('a'), ord('z'))) for i in range(data_len)]) pad_indexes = [random.randint(0, len(data)) for i in range(100)] pad_indexes.sort() last_index = 0 zero_padded_data = "" for index in pad_indexes: zero_padded_data += data[last_index:index] zero_padded_data += "\x00" * random.randint(0,1024) last_index = index zero_padded_data += data[last_index:] f = StringIO.StringIO(data) fz = ZeroRangesAdapter(StringIO.StringIO(zero_padded_data), block_size = 1) pprint.pprint(fz.zero_ranges()) for i in range(100): offset = random.randint(0, len(data)) size = random.randint(0, len(data)) print "READ offset=%ld, size=%ld" % (offset, size) f.seek(offset) fz.seek(offset) self.assertEqual(f.read(size), fz.read(size), "failed to read the same data from adapter")