Mercurial > public > mercurial-scm > hg-stable
view contrib/python-zstandard/tests/test_buffer_util.py @ 37495:b1fb341d8a61
zstandard: vendor python-zstandard 0.9.0
This was just released. It features a number of goodies. More info at
https://gregoryszorc.com/blog/2018/04/09/release-of-python-zstandard-0.9/.
The clang-format ignore list was updated to reflect the new source
of files.
The project contains a vendored copy of zstandard 1.3.4. The old
version was 1.1.3. One of the changes between those versions is that
zstandard is now dual licensed BSD + GPLv2 and the patent rights grant
has been removed. Good riddance.
The API should be backwards compatible. So no changes in core
should be needed. However, there were a number of changes in the
library that we'll want to adapt to. Those will be addressed in
subsequent commits.
Differential Revision: https://phab.mercurial-scm.org/D3198
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Mon, 09 Apr 2018 10:13:29 -0700 |
parents | e0dc40530c5a |
children | 675775c33ab6 |
line wrap: on
line source
import struct import unittest import zstandard as zstd ss = struct.Struct('=QQ') class TestBufferWithSegments(unittest.TestCase): def test_arguments(self): with self.assertRaises(TypeError): zstd.BufferWithSegments() with self.assertRaises(TypeError): zstd.BufferWithSegments(b'foo') # Segments data should be a multiple of 16. with self.assertRaisesRegexp(ValueError, 'segments array size is not a multiple of 16'): zstd.BufferWithSegments(b'foo', b'\x00\x00') def test_invalid_offset(self): with self.assertRaisesRegexp(ValueError, 'offset within segments array references memory'): zstd.BufferWithSegments(b'foo', ss.pack(0, 4)) def test_invalid_getitem(self): b = zstd.BufferWithSegments(b'foo', ss.pack(0, 3)) with self.assertRaisesRegexp(IndexError, 'offset must be non-negative'): test = b[-10] with self.assertRaisesRegexp(IndexError, 'offset must be less than 1'): test = b[1] with self.assertRaisesRegexp(IndexError, 'offset must be less than 1'): test = b[2] def test_single(self): b = zstd.BufferWithSegments(b'foo', ss.pack(0, 3)) self.assertEqual(len(b), 1) self.assertEqual(b.size, 3) self.assertEqual(b.tobytes(), b'foo') self.assertEqual(len(b[0]), 3) self.assertEqual(b[0].offset, 0) self.assertEqual(b[0].tobytes(), b'foo') def test_multiple(self): b = zstd.BufferWithSegments(b'foofooxfooxy', b''.join([ss.pack(0, 3), ss.pack(3, 4), ss.pack(7, 5)])) self.assertEqual(len(b), 3) self.assertEqual(b.size, 12) self.assertEqual(b.tobytes(), b'foofooxfooxy') self.assertEqual(b[0].tobytes(), b'foo') self.assertEqual(b[1].tobytes(), b'foox') self.assertEqual(b[2].tobytes(), b'fooxy') class TestBufferWithSegmentsCollection(unittest.TestCase): def test_empty_constructor(self): with self.assertRaisesRegexp(ValueError, 'must pass at least 1 argument'): zstd.BufferWithSegmentsCollection() 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_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')