Debian Codegraph @

contrib/python-zstandard/tests/test_buffer_util.py

raw
1 import struct
2 import unittest
3
4 import zstandard as zstd
5
6 ss = struct.Struct("=QQ")
7
8
9 @unittest.skipUnless(
10 "buffer_types" in zstd.backend_features, "buffer types not available"
11 )
12 class TestBufferWithSegments(unittest.TestCase):
13 def test_arguments(self):
14 with self.assertRaises(TypeError):
15 zstd.BufferWithSegments()
16
17 with self.assertRaises(TypeError):
18 zstd.BufferWithSegments(b"foo")
19
20 # Segments data should be a multiple of 16.
21 with self.assertRaisesRegex(
22 ValueError, "segments array size is not a multiple of 16"
23 ):
24 zstd.BufferWithSegments(b"foo", b"\x00\x00")
25
26 def test_invalid_offset(self):
27 with self.assertRaisesRegex(
28 ValueError, "offset within segments array references memory"
29 ):
30 zstd.BufferWithSegments(b"foo", ss.pack(0, 4))
31
32 def test_invalid_getitem(self):
33 b = zstd.BufferWithSegments(b"foo", ss.pack(0, 3))
34
35 with self.assertRaisesRegex(IndexError, "offset must be non-negative"):
36 test = b[-10]
37
38 with self.assertRaisesRegex(IndexError, "offset must be less than 1"):
39 test = b[1]
40
41 with self.assertRaisesRegex(IndexError, "offset must be less than 1"):
42 test = b[2]
43
44 def test_single(self):
45 b = zstd.BufferWithSegments(b"foo", ss.pack(0, 3))
46 self.assertEqual(len(b), 1)
47 self.assertEqual(b.size, 3)
48 self.assertEqual(b.tobytes(), b"foo")
49
50 self.assertEqual(len(b[0]), 3)
51 self.assertEqual(b[0].offset, 0)
52 self.assertEqual(b[0].tobytes(), b"foo")
53
54 def test_multiple(self):
55 b = zstd.BufferWithSegments(
56 b"foofooxfooxy",
57 b"".join([ss.pack(0, 3), ss.pack(3, 4), ss.pack(7, 5)]),
58 )
59 self.assertEqual(len(b), 3)
60 self.assertEqual(b.size, 12)
61 self.assertEqual(b.tobytes(), b"foofooxfooxy")
62
63 self.assertEqual(b[0].tobytes(), b"foo")
64 self.assertEqual(b[1].tobytes(), b"foox")
65 self.assertEqual(b[2].tobytes(), b"fooxy")
66
67
68 @unittest.skipUnless(
69 "buffer_types" in zstd.backend_features, "buffer types not available"
70 )
71 class TestBufferWithSegmentsCollection(unittest.TestCase):
72 def test_empty_constructor(self):
73 with self.assertRaisesRegex(
74 ValueError, "must pass at least 1 argument"
75 ):
76 zstd.BufferWithSegmentsCollection()
77
78 def test_argument_validation(self):
79 with self.assertRaisesRegex(
80 TypeError, "arguments must be BufferWithSegments"
81 ):
82 zstd.BufferWithSegmentsCollection(None)
83
84 with self.assertRaisesRegex(
85 TypeError, "arguments must be BufferWithSegments"
86 ):
87 zstd.BufferWithSegmentsCollection(
88 zstd.BufferWithSegments(b"foo", ss.pack(0, 3)), None
89 )
90
91 with self.assertRaisesRegex(
92 ValueError, "ZstdBufferWithSegments cannot be empty"
93 ):
94 zstd.BufferWithSegmentsCollection(zstd.BufferWithSegments(b"", b""))
95
96 def test_length(self):
97 b1 = zstd.BufferWithSegments(b"foo", ss.pack(0, 3))
98 b2 = zstd.BufferWithSegments(
99 b"barbaz", b"".join([ss.pack(0, 3), ss.pack(3, 3)])
100 )
101
102 c = zstd.BufferWithSegmentsCollection(b1)
103 self.assertEqual(len(c), 1)
104 self.assertEqual(c.size(), 3)
105
106 c = zstd.BufferWithSegmentsCollection(b2)
107 self.assertEqual(len(c), 2)
108 self.assertEqual(c.size(), 6)
109
110 c = zstd.BufferWithSegmentsCollection(b1, b2)
111 self.assertEqual(len(c), 3)
112 self.assertEqual(c.size(), 9)
113
114 def test_getitem(self):
115 b1 = zstd.BufferWithSegments(b"foo", ss.pack(0, 3))
116 b2 = zstd.BufferWithSegments(
117 b"barbaz", b"".join([ss.pack(0, 3), ss.pack(3, 3)])
118 )
119
120 c = zstd.BufferWithSegmentsCollection(b1, b2)
121
122 with self.assertRaisesRegex(IndexError, "offset must be less than 3"):
123 c[3]
124
125 with self.assertRaisesRegex(IndexError, "offset must be less than 3"):
126 c[4]
127
128 self.assertEqual(c[0].tobytes(), b"foo")
129 self.assertEqual(c[1].tobytes(), b"bar")
130 self.assertEqual(c[2].tobytes(), b"baz")