-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathtest_vlen_array.py
More file actions
93 lines (73 loc) · 2.3 KB
/
Copy pathtest_vlen_array.py
File metadata and controls
93 lines (73 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import numpy as np
import pytest
from tests.common import (
assert_array_items_equal,
check_backwards_compatibility,
check_config,
check_encode_decode_array,
check_repr,
)
VLenArray = pytest.importorskip("numcodecs.vlen").VLenArray
arrays = [
np.array([np.array([1, 2, 3]), np.array([4]), np.array([5, 6])] * 300, dtype=object),
np.array([np.array([1, 2, 3]), np.array([4]), np.array([5, 6])] * 300, dtype=object).reshape(
90, 10
),
]
codecs = [
VLenArray('<i1'),
VLenArray('<i2'),
VLenArray('<i4'),
VLenArray('<i8'),
VLenArray('<u1'),
VLenArray('<u2'),
VLenArray('<u4'),
VLenArray('<u8'),
]
def test_encode_decode():
for arr in arrays:
for codec in codecs:
check_encode_decode_array(arr, codec)
def test_config():
codec = VLenArray('<i8')
check_config(codec)
def test_repr():
check_repr("VLenArray(dtype='<i8')")
def test_backwards_compatibility():
check_backwards_compatibility(VLenArray.codec_id, arrays, codecs)
def test_encode_errors():
codec = VLenArray('<i8')
with pytest.raises(ValueError):
codec.encode('foo')
with pytest.raises(ValueError):
codec.encode(['foo', 'bar'])
def test_decode_errors():
codec = VLenArray('<i8')
with pytest.raises(TypeError):
codec.decode(1234)
# these should look like corrupt data
with pytest.raises(ValueError):
codec.decode(b'foo')
with pytest.raises(ValueError):
codec.decode(np.arange(2, 3, dtype='i4'))
with pytest.raises(ValueError):
codec.decode(np.arange(10, 20, dtype='i4'))
with pytest.raises(TypeError):
codec.decode('foo')
# test out parameter
enc = codec.encode(arrays[0])
with pytest.raises(TypeError):
codec.decode(enc, out=b'foo')
with pytest.raises(TypeError):
codec.decode(enc, out='foo')
with pytest.raises(TypeError):
codec.decode(enc, out=123)
with pytest.raises(ValueError):
codec.decode(enc, out=np.zeros(10, dtype='i4'))
def test_encode_none():
a = np.array([[1, 3], None, [4, 7]], dtype=object)
codec = VLenArray(int)
enc = codec.encode(a)
dec = codec.decode(enc)
expect = np.array([np.array([1, 3]), np.array([]), np.array([4, 7])], dtype=object)
assert_array_items_equal(expect, dec)