-
Notifications
You must be signed in to change notification settings - Fork 56
/
test_argutils.py
94 lines (78 loc) · 2.37 KB
/
test_argutils.py
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
94
"""Basic tests for argutils."""
import numpy as np
import pytest
import probnum.utils as pnut
@pytest.mark.parametrize("scalar", [1, 1.0, 1.0 + 2.0j, np.array(1.0)])
def test_as_numpy_scalar_returns_scalar_array(scalar):
"""All sorts of scalars are transformed into a np.generic."""
as_scalar = pnut.as_numpy_scalar(scalar)
assert isinstance(as_scalar, np.ndarray) and as_scalar.shape == ()
np.testing.assert_allclose(as_scalar, scalar, atol=0.0, rtol=1e-12)
@pytest.mark.parametrize("sequence", [[1.0], (1,), np.array([1.0])])
def test_as_numpy_scalar_bad_sequence_is_bad(sequence):
"""Sequence types give rise to ValueErrors in `as_numpy_scalar`."""
with pytest.raises(ValueError):
pnut.as_numpy_scalar(sequence)
@pytest.mark.parametrize("shape_arg", list(range(5)) + [np.int32(8)])
@pytest.mark.parametrize("ndim", [False, True])
def test_as_shape_int(shape_arg, ndim):
if ndim:
shape = pnut.as_shape(shape_arg, ndim=1)
else:
shape = pnut.as_shape(shape_arg)
assert isinstance(shape, tuple)
assert len(shape) == 1
assert all(isinstance(entry, int) for entry in shape)
assert shape[0] == shape_arg
@pytest.mark.parametrize(
"shape_arg",
[
(),
[],
(2,),
[3],
[3, 6, 5],
(1, 1, 1),
(np.int32(7), 2, 4, 8),
],
)
@pytest.mark.parametrize("ndim", [False, True])
def test_as_shape_iterable(shape_arg, ndim):
if ndim:
shape = pnut.as_shape(shape_arg, ndim=len(shape_arg))
else:
shape = pnut.as_shape(shape_arg)
assert isinstance(shape, tuple)
assert len(shape) == len(shape_arg)
assert all(isinstance(entry, int) for entry in shape)
assert all(
entry_shape == entry_shape_arg
for entry_shape, entry_shape_arg in zip(shape_arg, shape)
)
@pytest.mark.parametrize(
"shape_arg",
[
None,
"(1, 2, 3)",
tuple,
],
)
def test_as_shape_wrong_type(shape_arg):
with pytest.raises(TypeError):
pnut.as_shape(shape_arg)
@pytest.mark.parametrize(
"shape_arg, ndim",
[
((), 1),
([], 4),
(3, 3),
((2,), 8),
([3], 5),
([3, 6, 5], 2),
((1, 1, 1), 5),
((np.int32(7), 2, 4, 8), 2),
],
)
def test_as_shape_wrong_ndim(shape_arg, ndim):
with pytest.raises(TypeError):
pnut.as_shape(shape_arg, ndim=ndim)