test_eye.py 1.88 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
from itertools import product
rusty1s's avatar
rusty1s committed
2

rusty1s's avatar
rusty1s committed
3
4
import pytest
from torch_sparse.tensor import SparseTensor
rusty1s's avatar
rusty1s committed
5

rusty1s's avatar
rusty1s committed
6
7
8
9
10
11
from .utils import dtypes, devices


@pytest.mark.parametrize('dtype,device', product(dtypes, devices))
def test_eye(dtype, device):
    mat = SparseTensor.eye(3, dtype=dtype, device=device)
rusty1s's avatar
rusty1s committed
12
13
14
    assert mat.storage.row.tolist() == [0, 1, 2]
    assert mat.storage.rowptr.tolist() == [0, 1, 2, 3]
    assert mat.storage.col.tolist() == [0, 1, 2]
rusty1s's avatar
rusty1s committed
15
16
17
    assert mat.storage.value.tolist() == [1, 1, 1]
    assert len(mat.cached_keys()) == 0

rusty1s's avatar
rusty1s committed
18
19
20
21
    mat = SparseTensor.eye(3, dtype=dtype, device=device, has_value=False)
    assert mat.storage.row.tolist() == [0, 1, 2]
    assert mat.storage.rowptr.tolist() == [0, 1, 2, 3]
    assert mat.storage.col.tolist() == [0, 1, 2]
rusty1s's avatar
rusty1s committed
22
23
24
25
    assert mat.storage.value is None
    assert len(mat.cached_keys()) == 0

    mat = SparseTensor.eye(3, 4, dtype=dtype, device=device, fill_cache=True)
rusty1s's avatar
rusty1s committed
26
    assert mat.storage.row.tolist() == [0, 1, 2]
rusty1s's avatar
rusty1s committed
27
    assert mat.storage.rowptr.tolist() == [0, 1, 2, 3]
rusty1s's avatar
rusty1s committed
28
29
30
    assert mat.storage.col.tolist() == [0, 1, 2]
    assert len(mat.cached_keys()) == 5
    assert mat.storage.rowcount.tolist() == [1, 1, 1]
rusty1s's avatar
rusty1s committed
31
    assert mat.storage.colptr.tolist() == [0, 1, 2, 3, 3]
rusty1s's avatar
rusty1s committed
32
    assert mat.storage.colcount.tolist() == [1, 1, 1, 0]
rusty1s's avatar
rusty1s committed
33
34
35
36
    assert mat.storage.csr2csc.tolist() == [0, 1, 2]
    assert mat.storage.csc2csr.tolist() == [0, 1, 2]

    mat = SparseTensor.eye(4, 3, dtype=dtype, device=device, fill_cache=True)
rusty1s's avatar
rusty1s committed
37
    assert mat.storage.row.tolist() == [0, 1, 2]
rusty1s's avatar
rusty1s committed
38
    assert mat.storage.rowptr.tolist() == [0, 1, 2, 3, 3]
rusty1s's avatar
rusty1s committed
39
40
41
    assert mat.storage.col.tolist() == [0, 1, 2]
    assert len(mat.cached_keys()) == 5
    assert mat.storage.rowcount.tolist() == [1, 1, 1, 0]
rusty1s's avatar
rusty1s committed
42
    assert mat.storage.colptr.tolist() == [0, 1, 2, 3]
rusty1s's avatar
rusty1s committed
43
    assert mat.storage.colcount.tolist() == [1, 1, 1]
rusty1s's avatar
rusty1s committed
44
45
    assert mat.storage.csr2csc.tolist() == [0, 1, 2]
    assert mat.storage.csc2csr.tolist() == [0, 1, 2]