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

Code before:
        m = self.dia_container([[1, 2], [3, 4]]).tocoo()
        flat_inds = np.ravel_multi_index((m.row, m.col), m.shape)
        inds_are_sorted = np.all(np.diff(flat_inds) > 0)
        assert m.has_canonical_format == inds_are_sorted

    def test_tocoo_tocsr_tocsc_gh19245(self):
        # test index_dtype with tocoo, tocsr, tocsc
        data = np.array([[1, 2, 3, 4]]).repeat(3, axis=0)
        offsets = np.array([0, -1, 2], dtype=np.int32)
        dia = sparse.dia_array((data, offsets), shape=(4, 4))

        coo = dia.tocoo()
        assert coo.col.dtype == np.int32
        csr = dia.tocsr()
        assert csr.indices.dtype == np.int32
        csc = dia.tocsc()
        assert csc.indices.dtype == np.int32

    def test_tocsr(self):
        # test bound checks (other pathological cases are tested by
        # TestConstructUtils::test_spdiags, and normal operation is ensured by
        # many other tests here using .toarray())
        d1 = [[1]]        # diagonal shorter than width
        d3 = [[1, 2, 3]]  # diagonal longer than width

        cases = [(d1, [-1], 1, [[0, 0], [1, 0]]),  # within
                 (d1, [1],  0, [[0, 0], [0, 0]]),  # above (but within if full)
                 (d1, [3],  0, [[0, 0], [0, 0]]),  # all above
                 (d1, [-3], 0, [[0, 0], [0, 0]]),  # all below
                 (d3, [-1], 1, [[0, 0], [1, 0]]),  # within (only head)
                 (d3, [1],  1, [[0, 2], [0, 0]]),  # within (only tail)
                 (d3, [3],  0, [[0, 0], [0, 0]]),  # all above
                 (d3, [-3], 0, [[0, 0], [0, 0]])]  # all below

        for d, o, n, r in cases:
            A = self.dia_container((d, o), shape=(2, 2)).tocsr()
            assert A.nnz == n
            B = np.array(r)
            assert_array_equal(A.toarray(), B)

    def test_add_sparse(self):
        # test format and cases not covered by common add tests
        A = diag([1, 2])
        B = A + diag([3], 1)
        Asp = dia_matrix(A)
        Bsp = dia_matrix(B)

        Csp = Asp + Bsp
        assert isinstance(Csp, dia_matrix)
        assert_array_equal(Csp.toarray(), A + B)


Review comment (bug):
Let's convert each of these comments into a string, and include that in the assert message (i.e., as the third argument to `assert_array_equal`). This will make it a lot easier to figure out which case failed in the event of a test failure.

Code after:
    assert result.__args__ == (np.int8, tuple[int])

    result = coo_array[np.int8]
    assert isinstance(result, GenericAlias)
    assert result.__origin__ is coo_array
    assert result.__args__ == (np.int8,)


class TestDIA(sparse_test_class(getset=False, slicing=False, slicing_assign=False,
                                fancy_indexing=False, fancy_assign=False,
                                minmax=False, nnz_axis=False)):
    spcreator = dia_array
    math_dtypes = [np.int_, np.float64, np.complex128]

    def test_constructor1(self):
        D = array([[1, 0, 3, 0],
                   [1, 2, 0, 4],
                   [0, 2, 3, 0],
                   [0, 0, 3, 4]])
        data = np.array([[1,2,3,4]]).repeat(3,axis=0)
        offsets = np.array([0,-1,2])
        assert_equal(self.dia_container((data, offsets), shape=(4, 4)).toarray(), D)

    @pytest.mark.xfail(run=False, reason='DIA does not have a __getitem__')
    def test_iterator(self):
        pass

    @with_64bit_maxval_limit(3)
    def test_setdiag_dtype(self):
        m = self.dia_container(np.eye(3))
        assert_equal(m.offsets.dtype, np.int32)
        m.setdiag((3,), k=2)
        assert_equal(m.offsets.dtype, np.int32)

        m = self.dia_container(np.eye(4))
        assert_equal(m.offsets.dtype, np.int64)
        m.setdiag((3,), k=3)
        assert_equal(m.offsets.dtype, np.int64)

    def ill_cases(self):
        # Ill-formed inputs and reference 2 x 2 outputs for testing _getnnz()
        # and tocsr(): list of tuples
        # (data, offsets, nnz, dense array, case description)

        d1 = [[1]]        # diagonal shorter than width
        d3 = [[1, 2, 3]]  # diagonal longer than width

        return [(d1, [-1], 1, [[0, 0], [1, 0]],
                 'shorter diagonal within'),
                (d1, [1],  0, [[0, 0], [0, 0]],
                 'shorter diagonal above (but within if full)'),