Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_length(self):
if not hasattr(zstd, "BufferWithSegmentsCollection"):
self.skipTest("BufferWithSegmentsCollection not available")
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_list_input(self):
cctx = zstd.ZstdCompressor(write_checksum=True)
if not hasattr(cctx, "multi_compress_to_buffer"):
self.skipTest("multi_compress_to_buffer not available")
original = [b"foo" * 12, b"bar" * 6]
frames = [cctx.compress(c) for c in original]
b = cctx.multi_compress_to_buffer(original)
self.assertIsInstance(b, zstd.BufferWithSegmentsCollection)
self.assertEqual(len(b), 2)
self.assertEqual(b.size(), 44)
self.assertEqual(b[0].tobytes(), frames[0])
self.assertEqual(b[1].tobytes(), frames[1])
def test_empty_constructor(self):
if not hasattr(zstd, "BufferWithSegmentsCollection"):
self.skipTest("BufferWithSegmentsCollection not available")
with self.assertRaisesRegex(ValueError, "must pass at least 1 argument"):
zstd.BufferWithSegmentsCollection()
def test_argument_validation(self):
if not hasattr(zstd, "BufferWithSegmentsCollection"):
self.skipTest("BufferWithSegmentsCollection not available")
with self.assertRaisesRegex(TypeError, "arguments must be BufferWithSegments"):
zstd.BufferWithSegmentsCollection(None)
with self.assertRaisesRegex(TypeError, "arguments must be BufferWithSegments"):
zstd.BufferWithSegmentsCollection(
zstd.BufferWithSegments(b"foo", ss.pack(0, 3)), None
)
with self.assertRaisesRegex(
ValueError, "ZstdBufferWithSegments cannot be empty"
):
zstd.BufferWithSegmentsCollection(zstd.BufferWithSegments(b"", b""))
self.skipTest("BufferWithSegmentsCollection not available")
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)
)
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)
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_argument_validation(self):
if not hasattr(zstd, "BufferWithSegmentsCollection"):
self.skipTest("BufferWithSegmentsCollection not available")
with self.assertRaisesRegex(TypeError, "arguments must be BufferWithSegments"):
zstd.BufferWithSegmentsCollection(None)
with self.assertRaisesRegex(TypeError, "arguments must be BufferWithSegments"):
zstd.BufferWithSegmentsCollection(
zstd.BufferWithSegments(b"foo", ss.pack(0, 3)), None
)
with self.assertRaisesRegex(
ValueError, "ZstdBufferWithSegments cannot be empty"
):
zstd.BufferWithSegmentsCollection(zstd.BufferWithSegments(b"", b""))