|
4 | 4 |
|
5 | 5 | pytestmark = pytest.requires_numpy |
6 | 6 |
|
| 7 | +SCALAR_TYPES = {} |
| 8 | + |
7 | 9 | with pytest.suppress(ImportError): |
8 | 10 | import numpy as np |
9 | 11 |
|
| 12 | + SCALAR_TYPES = dict([ |
| 13 | + (np.bool_, False), |
| 14 | + (np.int8, -7), |
| 15 | + (np.int16, -15), |
| 16 | + (np.int32, -31), |
| 17 | + (np.int64, -63), |
| 18 | + (np.uint8, 9), |
| 19 | + (np.uint16, 17), |
| 20 | + (np.uint32, 33), |
| 21 | + (np.uint64, 65), |
| 22 | + (np.single, 1.125), |
| 23 | + (np.double, 1.25), |
| 24 | + (np.longdouble, 1.5), |
| 25 | + (np.csingle, 1 - 0.125j), |
| 26 | + (np.cdouble, 1 - 0.25j), |
| 27 | + (np.clongdouble, 1 - 0.5j), |
| 28 | + ]) |
10 | 29 |
|
11 | | -@pytest.fixture(scope='module') |
12 | | -def scalar_types(): |
13 | | - return [ |
14 | | - np.bool_, |
15 | | - np.int8, |
16 | | - np.int16, |
17 | | - np.int32, |
18 | | - np.int64, |
19 | | - np.uint8, |
20 | | - np.uint16, |
21 | | - np.uint32, |
22 | | - np.uint64, |
23 | | - np.float32, |
24 | | - np.float64, |
25 | | - ] |
| 30 | +ALL_TYPES = [int, bool, float, bytes, str, type(None)] + list(SCALAR_TYPES) |
26 | 31 |
|
27 | 32 |
|
28 | | -@pytest.fixture(scope='module') |
29 | | -def other_types(): |
30 | | - return [int, bool, float, bytes, str, np.complex64, type(None)] |
| 33 | +def type_name(tp): |
| 34 | + try: |
| 35 | + if tp is np.longdouble: |
| 36 | + return 'longdouble' |
| 37 | + elif issubclass(tp, np.floating): |
| 38 | + return 'float' + str(8 * tp().itemsize) |
| 39 | + elif tp is np.clongdouble: |
| 40 | + return 'longcomplex' |
| 41 | + elif issubclass(tp, np.complexfloating): |
| 42 | + return 'complex' + str(8 * tp().itemsize) |
| 43 | + return tp.__name__.rstrip('_') |
| 44 | + except BaseException: |
| 45 | + # no numpy |
| 46 | + return str(tp) |
31 | 47 |
|
32 | 48 |
|
33 | | -def signature(tp): |
34 | | - name = tp.__name__.rstrip('_') |
35 | | - return 'test_numpy_scalars(x: {tp}) -> Tuple[str, {tp}]'.format(tp=name) |
| 49 | +@pytest.fixture(scope='module', params=list(SCALAR_TYPES), ids=type_name) |
| 50 | +def scalar_type(request): |
| 51 | + return request.param |
36 | 52 |
|
37 | 53 |
|
38 | | -def test_numpy_scalars_single(scalar_types, other_types): |
39 | | - s_tp = 'str' if sys.version_info[0] >= 3 else 'unicode' |
40 | | - for scalar_type in scalar_types: |
41 | | - name = scalar_type.__name__.rstrip('_') |
42 | | - func = getattr(m, 'test_' + name) |
43 | | - sig = 'test_{tp}(x: {tp}) -> Tuple[{s_tp}, {tp}]\n'.format(tp=name, s_tp=s_tp) |
44 | | - assert func.__doc__ == sig |
45 | | - for tp in (scalar_types + other_types): |
46 | | - value = None if isinstance(None, tp) else tp() |
47 | | - if tp is scalar_type: |
48 | | - result = func(value) |
49 | | - assert result[0] == name |
50 | | - assert isinstance(result[1], tp) |
51 | | - assert result[1] == tp(1) |
52 | | - else: |
53 | | - with pytest.raises(TypeError): |
54 | | - func(value) |
| 54 | +def expected_signature(tp): |
| 55 | + s = 'str' if sys.version_info[0] >= 3 else 'unicode' |
| 56 | + t = type_name(tp) |
| 57 | + return 'test_{t}(x: {t}) -> Tuple[{s}, {t}]\n'.format(s=s, t=t) |
55 | 58 |
|
56 | 59 |
|
57 | | -def test_numpy_scalars_overload(scalar_types, other_types): |
58 | | - func = m.test_numpy_scalars |
59 | | - for tp in (scalar_types + other_types): |
60 | | - value = None if isinstance(None, tp) else tp() |
61 | | - if tp in scalar_types: |
62 | | - name = tp.__name__.rstrip('_') |
| 60 | +def test_numpy_scalars_single(scalar_type): |
| 61 | + expected = SCALAR_TYPES[scalar_type] |
| 62 | + name = type_name(scalar_type) |
| 63 | + func = getattr(m, 'test_' + name) |
| 64 | + assert func.__doc__ == expected_signature(scalar_type) |
| 65 | + for tp in ALL_TYPES: |
| 66 | + value = None if isinstance(None, tp) else tp(1) |
| 67 | + if tp is scalar_type: |
63 | 68 | result = func(value) |
64 | 69 | assert result[0] == name |
65 | 70 | assert isinstance(result[1], tp) |
66 | | - assert result[1] == tp(1) |
| 71 | + assert result[1] == tp(expected) |
| 72 | + else: |
| 73 | + with pytest.raises(TypeError): |
| 74 | + func(value) |
| 75 | + |
| 76 | + |
| 77 | +def test_numpy_scalars_overload(): |
| 78 | + func = m.test_numpy_scalars |
| 79 | + for tp in ALL_TYPES: |
| 80 | + value = None if isinstance(None, tp) else tp(1) |
| 81 | + if tp in SCALAR_TYPES: |
| 82 | + result = func(value) |
| 83 | + assert result[0] == type_name(tp) |
| 84 | + assert isinstance(result[1], tp) |
| 85 | + assert result[1] == tp(SCALAR_TYPES[tp]) |
67 | 86 | else: |
68 | 87 | with pytest.raises(TypeError): |
69 | 88 | func(value) |
0 commit comments