Repository: scipy/scipy
File: scipy/sparse/tests/test_base.py (Python)

Code before:


class _TestGetSet:
    def test_getelement(self):
        def check(dtype):
            D = array([[1,0,0],
                       [4,3,0],
                       [0,2,0],
                       [0,0,0]], dtype=dtype)
            A = self.spcreator(D)

            M,N = D.shape

            for i in range(-M, M):
                for j in range(-N, N):
                    assert_equal(A[i,j], D[i,j])

            assert_equal(type(A[1,1]), dtype)

            for ij in [(0,3),(-1,3),(4,0),(4,3),(4,-1), (1, 2, 3)]:
                assert_raises((IndexError, TypeError), A.__getitem__, ij)

        for dtype in supported_dtypes:
            check(np.dtype(dtype))

    def test_setelement(self, ):
        scalar_containers = [lambda x: csr_array(np.array([[x]])), np.array, lambda x: x]
        for scalar_container in scalar_containers:
            def check(dtype, scalar_container):
                A = self.spcreator((3,4), dtype=dtype)
                with warnings.catch_warnings():
                    warnings.filterwarnings("ignore", WMSG, SparseEfficiencyWarning)
                    A[0, 0] = scalar_container(dtype.type(0))  # bug 870
                    A[1, 2] = scalar_container(dtype.type(4.0))
                    A[0, 1] = scalar_container(dtype.type(3))
                    A[2, 0] = scalar_container(dtype.type(2.0))
                    A[0,-1] = scalar_container(dtype.type(8))
                    A[-1,-2] = scalar_container(dtype.type(7))
                    A[0, 1] = scalar_container(dtype.type(5))

                if dtype != np.bool_:
                    assert_array_equal(
                        A.toarray(),
                        [
                            [0, 5, 0, 8],
                            [0, 0, 4, 0],
                            [2, 0, 7, 0]
                        ]
                    )

                for ij in [(0,4),(-1,4),(3,0),(3,4),(3,-1)]:

Review comment (suggestion):
```suggestion
    def test_setelement(self):
```

Code after:


class _TestGetSet:
    def test_getelement(self):
        def check(dtype):
            D = array([[1,0,0],
                       [4,3,0],
                       [0,2,0],
                       [0,0,0]], dtype=dtype)
            A = self.spcreator(D)

            M,N = D.shape

            for i in range(-M, M):
                for j in range(-N, N):
                    assert_equal(A[i,j], D[i,j])

            assert_equal(type(A[1,1]), dtype)

            for ij in [(0,3),(-1,3),(4,0),(4,3),(4,-1), (1, 2, 3)]:
                assert_raises((IndexError, TypeError), A.__getitem__, ij)

        for dtype in supported_dtypes:
            check(np.dtype(dtype))

    def test_setelement(self):
        def check(dtype, scalar_container):
            A = self.spcreator((3, 4), dtype=dtype)
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", WMSG, SparseEfficiencyWarning)
                A[0, 0] = scalar_container(dtype.type(0))  # bug 870
                A[1, 2] = scalar_container(dtype.type(4.0))
                A[0, 1] = scalar_container(dtype.type(3))
                A[2, 0] = scalar_container(dtype.type(2.0))
                A[0, -1] = scalar_container(dtype.type(8))
                A[-1, -2] = scalar_container(dtype.type(7))
                A[0, 1] = scalar_container(dtype.type(5))

            if dtype != np.bool_:
                assert_array_equal(
                    A.toarray(), [[0, 5, 0, 8], [0, 0, 4, 0], [2, 0, 7, 0]]
                )

            for ij in [(0, 4), (-1, 4), (3, 0), (3, 4), (3, -1)]:
                assert_raises(IndexError, A.__setitem__, ij, 123.0)

            for v in [[1, 2, 3], array([1, 2, 3])]:
                assert_raises(ValueError, A.__setitem__, (0, 0), v)

            if not np.issubdtype(dtype, np.complexfloating) and dtype != np.bool_:
                for v in [3j]: