Skip to content

Commit

Permalink
TST: create_diagonal: add test from SciPy
Browse files Browse the repository at this point in the history
Co-authored-by: Jake Bowhay <jb9.bowhay@gmail.com>
  • Loading branch information
lucascolley and j-bowhay committed Nov 18, 2024
1 parent 0b28fc3 commit f4a3bad
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions tests/test_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# array-api-strict#6
import array_api_strict as xp # type: ignore[import-untyped]
import numpy as np
import pytest
from numpy.testing import assert_allclose, assert_array_equal, assert_equal

Expand Down Expand Up @@ -114,6 +115,7 @@ def test_combination(self):

class TestCreateDiagonal:
def test_1d(self):
# from np.diag tests
vals = 100 * xp.arange(5, dtype=xp.float64)
b = xp.zeros((5, 5))
for k in range(5):
Expand All @@ -127,13 +129,24 @@ def test_1d(self):
assert_array_equal(create_diagonal(vals, offset=2, xp=xp), b)
assert_array_equal(create_diagonal(vals, offset=-2, xp=xp), c)

@pytest.mark.parametrize("n", range(1, 10))
@pytest.mark.parametrize("offset", range(1, 10))
def test_create_diagonal(self, n, offset):
# from scipy._lib tests
rng = np.random.default_rng(2347823)
one = xp.asarray(1.0)
x = rng.random(n)
A = create_diagonal(xp.asarray(x, dtype=one.dtype), offset=offset, xp=xp)
B = xp.asarray(np.diag(x, offset), dtype=one.dtype)
assert_array_equal(A, B)

def test_0d(self):
with pytest.raises(ValueError, match="1-dimensional"):
print(create_diagonal(xp.asarray(1), xp=xp))
create_diagonal(xp.asarray(1), xp=xp)

def test_2d(self):
with pytest.raises(ValueError, match="1-dimensional"):
print(create_diagonal(xp.asarray([[1]]), xp=xp))
create_diagonal(xp.asarray([[1]]), xp=xp)


class TestKron:
Expand Down

0 comments on commit f4a3bad

Please sign in to comment.