comparison contrib/python-zstandard/tests/test_buffer_util.py @ 43994:de7838053207

zstandard: vendor python-zstandard 0.13.0 Version 0.13.0 of the package was just released. It contains an upgraded zstd C library which can result in some performance wins, official support for Python 3.8, and a blackened code base. There were no meaningful code or functionality changes in this release of python-zstandard: just reformatting and an upgraded zstd library version. So the diff seems much larger than what it is. Files were added without modifications. The clang-format-ignorelist file was updated to reflect a new header file in the zstd distribution. # no-check-commit because 3rd party code has different style guidelines Differential Revision: https://phab.mercurial-scm.org/D7770
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 28 Dec 2019 09:55:45 -0800
parents 675775c33ab6
children 5e84a96d865b
comparison
equal deleted inserted replaced
43993:873d0fecb9a3 43994:de7838053207
1 import struct 1 import struct
2 import unittest 2 import unittest
3 3
4 import zstandard as zstd 4 import zstandard as zstd
5 5
6 ss = struct.Struct('=QQ') 6 from .common import TestCase
7
8 ss = struct.Struct("=QQ")
7 9
8 10
9 class TestBufferWithSegments(unittest.TestCase): 11 class TestBufferWithSegments(TestCase):
10 def test_arguments(self): 12 def test_arguments(self):
11 if not hasattr(zstd, 'BufferWithSegments'): 13 if not hasattr(zstd, "BufferWithSegments"):
12 self.skipTest('BufferWithSegments not available') 14 self.skipTest("BufferWithSegments not available")
13 15
14 with self.assertRaises(TypeError): 16 with self.assertRaises(TypeError):
15 zstd.BufferWithSegments() 17 zstd.BufferWithSegments()
16 18
17 with self.assertRaises(TypeError): 19 with self.assertRaises(TypeError):
18 zstd.BufferWithSegments(b'foo') 20 zstd.BufferWithSegments(b"foo")
19 21
20 # Segments data should be a multiple of 16. 22 # Segments data should be a multiple of 16.
21 with self.assertRaisesRegexp(ValueError, 'segments array size is not a multiple of 16'): 23 with self.assertRaisesRegex(
22 zstd.BufferWithSegments(b'foo', b'\x00\x00') 24 ValueError, "segments array size is not a multiple of 16"
25 ):
26 zstd.BufferWithSegments(b"foo", b"\x00\x00")
23 27
24 def test_invalid_offset(self): 28 def test_invalid_offset(self):
25 if not hasattr(zstd, 'BufferWithSegments'): 29 if not hasattr(zstd, "BufferWithSegments"):
26 self.skipTest('BufferWithSegments not available') 30 self.skipTest("BufferWithSegments not available")
27 31
28 with self.assertRaisesRegexp(ValueError, 'offset within segments array references memory'): 32 with self.assertRaisesRegex(
29 zstd.BufferWithSegments(b'foo', ss.pack(0, 4)) 33 ValueError, "offset within segments array references memory"
34 ):
35 zstd.BufferWithSegments(b"foo", ss.pack(0, 4))
30 36
31 def test_invalid_getitem(self): 37 def test_invalid_getitem(self):
32 if not hasattr(zstd, 'BufferWithSegments'): 38 if not hasattr(zstd, "BufferWithSegments"):
33 self.skipTest('BufferWithSegments not available') 39 self.skipTest("BufferWithSegments not available")
34 40
35 b = zstd.BufferWithSegments(b'foo', ss.pack(0, 3)) 41 b = zstd.BufferWithSegments(b"foo", ss.pack(0, 3))
36 42
37 with self.assertRaisesRegexp(IndexError, 'offset must be non-negative'): 43 with self.assertRaisesRegex(IndexError, "offset must be non-negative"):
38 test = b[-10] 44 test = b[-10]
39 45
40 with self.assertRaisesRegexp(IndexError, 'offset must be less than 1'): 46 with self.assertRaisesRegex(IndexError, "offset must be less than 1"):
41 test = b[1] 47 test = b[1]
42 48
43 with self.assertRaisesRegexp(IndexError, 'offset must be less than 1'): 49 with self.assertRaisesRegex(IndexError, "offset must be less than 1"):
44 test = b[2] 50 test = b[2]
45 51
46 def test_single(self): 52 def test_single(self):
47 if not hasattr(zstd, 'BufferWithSegments'): 53 if not hasattr(zstd, "BufferWithSegments"):
48 self.skipTest('BufferWithSegments not available') 54 self.skipTest("BufferWithSegments not available")
49 55
50 b = zstd.BufferWithSegments(b'foo', ss.pack(0, 3)) 56 b = zstd.BufferWithSegments(b"foo", ss.pack(0, 3))
51 self.assertEqual(len(b), 1) 57 self.assertEqual(len(b), 1)
52 self.assertEqual(b.size, 3) 58 self.assertEqual(b.size, 3)
53 self.assertEqual(b.tobytes(), b'foo') 59 self.assertEqual(b.tobytes(), b"foo")
54 60
55 self.assertEqual(len(b[0]), 3) 61 self.assertEqual(len(b[0]), 3)
56 self.assertEqual(b[0].offset, 0) 62 self.assertEqual(b[0].offset, 0)
57 self.assertEqual(b[0].tobytes(), b'foo') 63 self.assertEqual(b[0].tobytes(), b"foo")
58 64
59 def test_multiple(self): 65 def test_multiple(self):
60 if not hasattr(zstd, 'BufferWithSegments'): 66 if not hasattr(zstd, "BufferWithSegments"):
61 self.skipTest('BufferWithSegments not available') 67 self.skipTest("BufferWithSegments not available")
62 68
63 b = zstd.BufferWithSegments(b'foofooxfooxy', b''.join([ss.pack(0, 3), 69 b = zstd.BufferWithSegments(
64 ss.pack(3, 4), 70 b"foofooxfooxy", b"".join([ss.pack(0, 3), ss.pack(3, 4), ss.pack(7, 5)])
65 ss.pack(7, 5)])) 71 )
66 self.assertEqual(len(b), 3) 72 self.assertEqual(len(b), 3)
67 self.assertEqual(b.size, 12) 73 self.assertEqual(b.size, 12)
68 self.assertEqual(b.tobytes(), b'foofooxfooxy') 74 self.assertEqual(b.tobytes(), b"foofooxfooxy")
69 75
70 self.assertEqual(b[0].tobytes(), b'foo') 76 self.assertEqual(b[0].tobytes(), b"foo")
71 self.assertEqual(b[1].tobytes(), b'foox') 77 self.assertEqual(b[1].tobytes(), b"foox")
72 self.assertEqual(b[2].tobytes(), b'fooxy') 78 self.assertEqual(b[2].tobytes(), b"fooxy")
73 79
74 80
75 class TestBufferWithSegmentsCollection(unittest.TestCase): 81 class TestBufferWithSegmentsCollection(TestCase):
76 def test_empty_constructor(self): 82 def test_empty_constructor(self):
77 if not hasattr(zstd, 'BufferWithSegmentsCollection'): 83 if not hasattr(zstd, "BufferWithSegmentsCollection"):
78 self.skipTest('BufferWithSegmentsCollection not available') 84 self.skipTest("BufferWithSegmentsCollection not available")
79 85
80 with self.assertRaisesRegexp(ValueError, 'must pass at least 1 argument'): 86 with self.assertRaisesRegex(ValueError, "must pass at least 1 argument"):
81 zstd.BufferWithSegmentsCollection() 87 zstd.BufferWithSegmentsCollection()
82 88
83 def test_argument_validation(self): 89 def test_argument_validation(self):
84 if not hasattr(zstd, 'BufferWithSegmentsCollection'): 90 if not hasattr(zstd, "BufferWithSegmentsCollection"):
85 self.skipTest('BufferWithSegmentsCollection not available') 91 self.skipTest("BufferWithSegmentsCollection not available")
86 92
87 with self.assertRaisesRegexp(TypeError, 'arguments must be BufferWithSegments'): 93 with self.assertRaisesRegex(TypeError, "arguments must be BufferWithSegments"):
88 zstd.BufferWithSegmentsCollection(None) 94 zstd.BufferWithSegmentsCollection(None)
89 95
90 with self.assertRaisesRegexp(TypeError, 'arguments must be BufferWithSegments'): 96 with self.assertRaisesRegex(TypeError, "arguments must be BufferWithSegments"):
91 zstd.BufferWithSegmentsCollection(zstd.BufferWithSegments(b'foo', ss.pack(0, 3)), 97 zstd.BufferWithSegmentsCollection(
92 None) 98 zstd.BufferWithSegments(b"foo", ss.pack(0, 3)), None
99 )
93 100
94 with self.assertRaisesRegexp(ValueError, 'ZstdBufferWithSegments cannot be empty'): 101 with self.assertRaisesRegex(
95 zstd.BufferWithSegmentsCollection(zstd.BufferWithSegments(b'', b'')) 102 ValueError, "ZstdBufferWithSegments cannot be empty"
103 ):
104 zstd.BufferWithSegmentsCollection(zstd.BufferWithSegments(b"", b""))
96 105
97 def test_length(self): 106 def test_length(self):
98 if not hasattr(zstd, 'BufferWithSegmentsCollection'): 107 if not hasattr(zstd, "BufferWithSegmentsCollection"):
99 self.skipTest('BufferWithSegmentsCollection not available') 108 self.skipTest("BufferWithSegmentsCollection not available")
100 109
101 b1 = zstd.BufferWithSegments(b'foo', ss.pack(0, 3)) 110 b1 = zstd.BufferWithSegments(b"foo", ss.pack(0, 3))
102 b2 = zstd.BufferWithSegments(b'barbaz', b''.join([ss.pack(0, 3), 111 b2 = zstd.BufferWithSegments(
103 ss.pack(3, 3)])) 112 b"barbaz", b"".join([ss.pack(0, 3), ss.pack(3, 3)])
113 )
104 114
105 c = zstd.BufferWithSegmentsCollection(b1) 115 c = zstd.BufferWithSegmentsCollection(b1)
106 self.assertEqual(len(c), 1) 116 self.assertEqual(len(c), 1)
107 self.assertEqual(c.size(), 3) 117 self.assertEqual(c.size(), 3)
108 118
113 c = zstd.BufferWithSegmentsCollection(b1, b2) 123 c = zstd.BufferWithSegmentsCollection(b1, b2)
114 self.assertEqual(len(c), 3) 124 self.assertEqual(len(c), 3)
115 self.assertEqual(c.size(), 9) 125 self.assertEqual(c.size(), 9)
116 126
117 def test_getitem(self): 127 def test_getitem(self):
118 if not hasattr(zstd, 'BufferWithSegmentsCollection'): 128 if not hasattr(zstd, "BufferWithSegmentsCollection"):
119 self.skipTest('BufferWithSegmentsCollection not available') 129 self.skipTest("BufferWithSegmentsCollection not available")
120 130
121 b1 = zstd.BufferWithSegments(b'foo', ss.pack(0, 3)) 131 b1 = zstd.BufferWithSegments(b"foo", ss.pack(0, 3))
122 b2 = zstd.BufferWithSegments(b'barbaz', b''.join([ss.pack(0, 3), 132 b2 = zstd.BufferWithSegments(
123 ss.pack(3, 3)])) 133 b"barbaz", b"".join([ss.pack(0, 3), ss.pack(3, 3)])
134 )
124 135
125 c = zstd.BufferWithSegmentsCollection(b1, b2) 136 c = zstd.BufferWithSegmentsCollection(b1, b2)
126 137
127 with self.assertRaisesRegexp(IndexError, 'offset must be less than 3'): 138 with self.assertRaisesRegex(IndexError, "offset must be less than 3"):
128 c[3] 139 c[3]
129 140
130 with self.assertRaisesRegexp(IndexError, 'offset must be less than 3'): 141 with self.assertRaisesRegex(IndexError, "offset must be less than 3"):
131 c[4] 142 c[4]
132 143
133 self.assertEqual(c[0].tobytes(), b'foo') 144 self.assertEqual(c[0].tobytes(), b"foo")
134 self.assertEqual(c[1].tobytes(), b'bar') 145 self.assertEqual(c[1].tobytes(), b"bar")
135 self.assertEqual(c[2].tobytes(), b'baz') 146 self.assertEqual(c[2].tobytes(), b"baz")