Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
test_arraymethod.py85 linesDownload Raw Back to tests
1"""
2This file tests the generic aspects of ArrayMethod.  At the time of writing
3this is private API, but when added, public API may be added here.
4"""
5
6import types
7from typing import Any
8
9import pytest
10
11import numpy as np
12from numpy._core._multiarray_umath import _get_castingimpl as get_castingimpl
13
14
15class TestResolveDescriptors:
16    # Test mainly error paths of the resolve_descriptors function,
17    # note that the `casting_unittests` tests exercise this non-error paths.
18
19    # Casting implementations are the main/only current user:
20    method = get_castingimpl(type(np.dtype("d")), type(np.dtype("f")))
21
22    @pytest.mark.parametrize("args", [
23        (True,),  # Not a tuple.
24        ((None,)),  # Too few elements
25        ((None, None, None),),  # Too many
26        ((None, None),),  # Input dtype is None, which is invalid.
27        ((np.dtype("d"), True),),  # Output dtype is not a dtype
28        ((np.dtype("f"), None),),  # Input dtype does not match method
29    ])
30    def test_invalid_arguments(self, args):
31        with pytest.raises(TypeError):
32            self.method._resolve_descriptors(*args)
33
34
35class TestSimpleStridedCall:
36    # Test mainly error paths of the resolve_descriptors function,
37    # note that the `casting_unittests` tests exercise this non-error paths.
38
39    # Casting implementations are the main/only current user:
40    method = get_castingimpl(type(np.dtype("d")), type(np.dtype("f")))
41
42    @pytest.mark.parametrize(["args", "error"], [
43        ((True,), TypeError),  # Not a tuple
44        (((None,),), TypeError),  # Too few elements
45        ((None, None), TypeError),  # Inputs are not arrays.
46        (((None, None, None),), TypeError),  # Too many
47        (((np.arange(3), np.arange(3)),), TypeError),  # Incorrect dtypes
48        (((np.ones(3, dtype=">d"), np.ones(3, dtype="<f")),),
49         TypeError),  # Does not support byte-swapping
50        (((np.ones((2, 2), dtype="d"), np.ones((2, 2), dtype="f")),),
51         ValueError),  # not 1-D
52        (((np.ones(3, dtype="d"), np.ones(4, dtype="f")),),
53          ValueError),  # different length
54        (((np.frombuffer(b"\0x00" * 3 * 2, dtype="d"),
55           np.frombuffer(b"\0x00" * 3, dtype="f")),),
56         ValueError),  # output not writeable
57    ])
58    def test_invalid_arguments(self, args, error):
59        # This is private API, which may be modified freely
60        with pytest.raises(error):
61            self.method._simple_strided_call(*args)
62
63
64@pytest.mark.parametrize(
65    "cls", [
66        np.ndarray, np.recarray, np.char.chararray, np.matrix, np.memmap
67    ]
68)
69class TestClassGetItem:
70    def test_class_getitem(self, cls: type[np.ndarray]) -> None:
71        """Test `ndarray.__class_getitem__`."""
72        alias = cls[Any, Any]
73        assert isinstance(alias, types.GenericAlias)
74        assert alias.__origin__ is cls
75
76    @pytest.mark.parametrize("arg_len", range(4))
77    def test_subscript_tup(self, cls: type[np.ndarray], arg_len: int) -> None:
78        arg_tup = (Any,) * arg_len
79        if arg_len in (1, 2):
80            assert cls[arg_tup]
81        else:
82            match = f"Too {'few' if arg_len == 0 else 'many'} arguments"
83            with pytest.raises(TypeError, match=match):
84                cls[arg_tup]
85 
codekingpro/portable-devtools · Team Ai