|
6 | 6 |
|
7 | 7 | class TestSparseVector: |
8 | 8 | def test_from_dense(self): |
9 | | - assert SparseVector.from_dense([1, 0, 2, 0, 3, 0]).to_list() == [1, 0, 2, 0, 3, 0] |
10 | | - assert SparseVector.from_dense([1, 0, 2, 0, 3, 0]).to_numpy().tolist() == [1, 0, 2, 0, 3, 0] |
11 | | - assert SparseVector.from_dense(np.array([1, 0, 2, 0, 3, 0])).to_list() == [1, 0, 2, 0, 3, 0] |
| 9 | + assert SparseVector([1, 0, 2, 0, 3, 0]).to_list() == [1, 0, 2, 0, 3, 0] |
| 10 | + assert SparseVector([1, 0, 2, 0, 3, 0]).to_numpy().tolist() == [1, 0, 2, 0, 3, 0] |
| 11 | + assert SparseVector(np.array([1, 0, 2, 0, 3, 0])).to_list() == [1, 0, 2, 0, 3, 0] |
| 12 | + |
| 13 | + def test_from_dense_dimensions(self): |
| 14 | + with pytest.raises(ValueError) as error: |
| 15 | + SparseVector([1, 0, 2, 0, 3, 0], 6) |
| 16 | + assert str(error.value) == 'dimensions not allowed' |
12 | 17 |
|
13 | 18 | def test_from_dict(self): |
14 | | - assert SparseVector.from_dict({0: 1, 2: 2, 4: 3}, 6).to_list() == [1, 0, 2, 0, 3, 0] |
| 19 | + assert SparseVector({0: 1, 2: 2, 4: 3}, 6).to_list() == [1, 0, 2, 0, 3, 0] |
| 20 | + |
| 21 | + def test_from_dict_no_dimensions(self): |
| 22 | + with pytest.raises(ValueError) as error: |
| 23 | + SparseVector({0: 1, 2: 2, 4: 3}) |
| 24 | + assert str(error.value) == 'dimensions required' |
15 | 25 |
|
16 | 26 | def test_from_sparse(self): |
17 | 27 | arr = coo_array(np.array([1, 0, 2, 0, 3, 0])) |
18 | | - assert SparseVector.from_sparse(arr).to_list() == [1, 0, 2, 0, 3, 0] |
19 | | - assert SparseVector.from_sparse(arr.todok()).to_list() == [1, 0, 2, 0, 3, 0] |
| 28 | + assert SparseVector(arr).to_list() == [1, 0, 2, 0, 3, 0] |
| 29 | + assert SparseVector(arr.todok()).to_list() == [1, 0, 2, 0, 3, 0] |
| 30 | + |
| 31 | + def test_from_sparse_dimensions(self): |
| 32 | + with pytest.raises(ValueError) as error: |
| 33 | + SparseVector(coo_array(np.array([1, 0, 2, 0, 3, 0])), 6) |
| 34 | + assert str(error.value) == 'dimensions not allowed' |
20 | 35 |
|
21 | 36 | def test_repr(self): |
22 | | - assert repr(SparseVector.from_dense([1, 0, 2, 0, 3, 0])) == 'SparseVector(6, [0, 2, 4], [1.0, 2.0, 3.0])' |
23 | | - assert str(SparseVector.from_dense([1, 0, 2, 0, 3, 0])) == 'SparseVector(6, [0, 2, 4], [1.0, 2.0, 3.0])' |
| 37 | + assert repr(SparseVector([1, 0, 2, 0, 3, 0])) == 'SparseVector({0: 1.0, 2: 2.0, 4: 3.0}, 6)' |
| 38 | + assert str(SparseVector([1, 0, 2, 0, 3, 0])) == 'SparseVector({0: 1.0, 2: 2.0, 4: 3.0}, 6)' |
24 | 39 |
|
25 | 40 | def test_dim(self): |
26 | | - assert SparseVector.from_dense([1, 0, 2, 0, 3, 0]).dim() == 6 |
| 41 | + assert SparseVector([1, 0, 2, 0, 3, 0]).dim() == 6 |
27 | 42 |
|
28 | 43 | def test_to_dict(self): |
29 | | - assert SparseVector.from_dense([1, 0, 2, 0, 3, 0]).to_dict() == {0: 1, 2: 2, 4: 3} |
| 44 | + assert SparseVector([1, 0, 2, 0, 3, 0]).to_dict() == {0: 1, 2: 2, 4: 3} |
30 | 45 |
|
31 | 46 | def test_to_coo(self): |
32 | | - assert SparseVector.from_dense([1, 0, 2, 0, 3, 0]).to_coo().toarray().tolist() == [[1, 0, 2, 0, 3, 0]] |
| 47 | + assert SparseVector([1, 0, 2, 0, 3, 0]).to_coo().toarray().tolist() == [[1, 0, 2, 0, 3, 0]] |
0 commit comments