def test_argument_validation(self): with self.assertRaisesRegexp(TypeError, 'arguments must be BufferWithSegments'): zstd.BufferWithSegmentsCollection(None) with self.assertRaisesRegexp(TypeError, 'arguments must be BufferWithSegments'): zstd.BufferWithSegmentsCollection( zstd.BufferWithSegments(b'foo', ss.pack(0, 3)), None) with self.assertRaisesRegexp(ValueError, 'ZstdBufferWithSegments cannot be empty'): zstd.BufferWithSegmentsCollection(zstd.BufferWithSegments( b'', b''))
def test_buffer_with_segments_collection_input(self): cctx = zstd.ZstdCompressor(write_content_size=True, write_checksum=True) original = [ b'foo1', b'foo2' * 2, b'foo3' * 3, b'foo4' * 4, b'foo5' * 5, ] frames = [cctx.compress(c) for c in original] b = b''.join([original[0], original[1]]) b1 = zstd.BufferWithSegments( b, struct.pack('=QQQQ', 0, len(original[0]), len(original[0]), len(original[1]))) b = b''.join([original[2], original[3], original[4]]) b2 = zstd.BufferWithSegments( b, struct.pack('=QQQQQQ', 0, len(original[2]), len(original[2]), len(original[3]), len(original[2]) + len(original[3]), len(original[4]))) c = zstd.BufferWithSegmentsCollection(b1, b2) result = cctx.multi_compress_to_buffer(c) self.assertEqual(len(result), len(frames)) for i, frame in enumerate(frames): self.assertEqual(result[i].tobytes(), frame)
def test_length(self): b1 = zstd.BufferWithSegments(b'foo', ss.pack(0, 3)) b2 = zstd.BufferWithSegments(b'barbaz', b''.join([ss.pack(0, 3), ss.pack(3, 3)])) c = zstd.BufferWithSegmentsCollection(b1) self.assertEqual(len(c), 1) self.assertEqual(c.size(), 3) c = zstd.BufferWithSegmentsCollection(b2) self.assertEqual(len(c), 2) self.assertEqual(c.size(), 6) c = zstd.BufferWithSegmentsCollection(b1, b2) self.assertEqual(len(c), 3) self.assertEqual(c.size(), 9)
def test_getitem(self): b1 = zstd.BufferWithSegments(b'foo', ss.pack(0, 3)) b2 = zstd.BufferWithSegments(b'barbaz', b''.join([ss.pack(0, 3), ss.pack(3, 3)])) c = zstd.BufferWithSegmentsCollection(b1, b2) with self.assertRaisesRegexp(IndexError, 'offset must be less than 3'): c[3] with self.assertRaisesRegexp(IndexError, 'offset must be less than 3'): c[4] self.assertEqual(c[0].tobytes(), b'foo') self.assertEqual(c[1].tobytes(), b'bar') self.assertEqual(c[2].tobytes(), b'baz')
def test_buffer_with_segments_collection_input(self): cctx = zstd.ZstdCompressor(write_content_size=True) original = [ b'foo0' * 2, b'foo1' * 3, b'foo2' * 4, b'foo3' * 5, b'foo4' * 6, ] frames = cctx.multi_compress_to_buffer(original) # Check round trip. dctx = zstd.ZstdDecompressor() decompressed = dctx.multi_decompress_to_buffer(frames, threads=3) self.assertEqual(len(decompressed), len(original)) for i, data in enumerate(original): self.assertEqual(data, decompressed[i].tobytes()) # And a manual mode. b = b''.join([frames[0].tobytes(), frames[1].tobytes()]) b1 = zstd.BufferWithSegments( b, struct.pack('=QQQQ', 0, len(frames[0]), len(frames[0]), len(frames[1]))) b = b''.join( [frames[2].tobytes(), frames[3].tobytes(), frames[4].tobytes()]) b2 = zstd.BufferWithSegments( b, struct.pack('=QQQQQQ', 0, len(frames[2]), len(frames[2]), len(frames[3]), len(frames[2]) + len(frames[3]), len(frames[4]))) c = zstd.BufferWithSegmentsCollection(b1, b2) dctx = zstd.ZstdDecompressor() decompressed = dctx.multi_decompress_to_buffer(c) self.assertEqual(len(decompressed), 5) for i in range(5): self.assertEqual(decompressed[i].tobytes(), original[i])
def test_empty_constructor(self): with self.assertRaisesRegexp(ValueError, 'must pass at least 1 argument'): zstd.BufferWithSegmentsCollection()