Skip to content

Commit a903ee5

Browse files
committed
Add better tests from numcodecs.js
1 parent 1096df7 commit a903ee5

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

numcodecs/tests/test_zstd.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
np.random.randint(0, 2**25, size=1000, dtype='u8').view('m8[m]'),
4848
np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('M8[ns]'),
4949
np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('m8[ns]'),
50-
np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('M8[m]'),
51-
np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('m8[m]'),
50+
np.random.randint(-(2**63), -(2**63) + 20, dtype='i8').view('M8[m]'),
51+
np.random.randint(-(2**63), -(2**63) + 20, dtype='i8').view('m8[m]'),
5252
]
5353

5454

@@ -93,15 +93,23 @@ def test_native_functions():
9393

9494

9595
def test_streaming_decompression():
96+
# Test input frames with unknown frame content size
9697
codec = Zstd()
97-
# Bytes from streaming compression
98+
99+
# Encode bytes directly that were the result of streaming compression
98100
bytes_val = bytes(bytearray([
99101
40, 181, 47, 253, 0, 88, 97, 0, 0, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33,
100102
]))
101103
dec = codec.decode(bytes_val)
102104
assert dec == b'Hello World!'
103105

104-
bytes2 = bytes(bytearray([
106+
# Two consecutive frames given as input
107+
bytes2 = bytes(bytearray(bytes_val * 2))
108+
dec2 = codec.decode(bytes2)
109+
assert dec2 == b'Hello World!Hello World!'
110+
111+
# Single long frame that decompresses to a large output
112+
bytes3 = bytes(bytearray([
105113
40, 181, 47, 253, 0, 88, 36, 2, 0, 164, 3, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
106114
83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
107115
109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 1, 0, 58, 252, 223, 115, 5, 5, 76, 0, 0, 8,
@@ -112,5 +120,10 @@ def test_streaming_decompression():
112120
255, 57, 16, 2, 76, 0, 0, 8, 85, 1, 0, 252, 255, 57, 16, 2, 76, 0, 0, 8, 77, 1, 0, 252, 255, 57, 16, 2, 77, 0, 0, 8,
113121
69, 1, 0, 252, 127, 29, 8, 1,
114122
]))
115-
dec2 = codec.decode(bytes2)
116-
assert dec2 == b'ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz' * 1024 * 32
123+
dec3 = codec.decode(bytes3)
124+
assert dec3 == b'ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz' * 1024 * 32
125+
126+
# Garbage input results in an error
127+
bytes4 = bytes(bytearray([0, 0, 0, 0, 0, 0, 0, 0]))
128+
with pytest.raises(RuntimeError, match='Zstd decompression error: invalid input data'):
129+
codec.decode(bytes4)

0 commit comments

Comments
 (0)