|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 |
| -import array_api_strict as xp # type: ignore[import-not-found] |
| 3 | +import array_api_strict as xp |
| 4 | +from numpy.testing import assert_array_equal |
4 | 5 |
|
5 | 6 | from array_api_extra import atleast_nd
|
6 | 7 |
|
7 | 8 |
|
8 | 9 | class TestAtLeastND:
|
9 |
| - def test_1d_to_2d(self): |
| 10 | + def test_0D(self): |
| 11 | + x = xp.asarray(1) |
| 12 | + |
| 13 | + y = atleast_nd(x, ndim=0, xp=xp) |
| 14 | + assert_array_equal(y, x) |
| 15 | + |
| 16 | + y = atleast_nd(x, ndim=1, xp=xp) |
| 17 | + assert_array_equal(y, xp.ones((1,))) |
| 18 | + |
| 19 | + y = atleast_nd(x, ndim=5, xp=xp) |
| 20 | + assert_array_equal(y, xp.ones((1, 1, 1, 1, 1))) |
| 21 | + |
| 22 | + def test_1D(self): |
10 | 23 | x = xp.asarray([0, 1])
|
| 24 | + |
| 25 | + y = atleast_nd(x, ndim=0, xp=xp) |
| 26 | + assert_array_equal(y, x) |
| 27 | + |
| 28 | + y = atleast_nd(x, ndim=1, xp=xp) |
| 29 | + assert_array_equal(y, x) |
| 30 | + |
| 31 | + y = atleast_nd(x, ndim=2, xp=xp) |
| 32 | + assert_array_equal(y, xp.asarray([[0, 1]])) |
| 33 | + |
| 34 | + y = atleast_nd(x, ndim=5, xp=xp) |
| 35 | + assert_array_equal(y, xp.reshape(xp.arange(2), (1, 1, 1, 1, 2))) |
| 36 | + |
| 37 | + def test_2D(self): |
| 38 | + x = xp.asarray([[3]]) |
| 39 | + |
| 40 | + y = atleast_nd(x, ndim=0, xp=xp) |
| 41 | + assert_array_equal(y, x) |
| 42 | + |
11 | 43 | y = atleast_nd(x, ndim=2, xp=xp)
|
12 |
| - assert y.ndim == 2 |
| 44 | + assert_array_equal(y, x) |
| 45 | + |
| 46 | + y = atleast_nd(x, ndim=3, xp=xp) |
| 47 | + assert_array_equal(y, 3 * xp.ones((1, 1, 1))) |
| 48 | + |
| 49 | + y = atleast_nd(x, ndim=5, xp=xp) |
| 50 | + assert_array_equal(y, 3 * xp.ones((1, 1, 1, 1, 1))) |
| 51 | + |
| 52 | + def test_5D(self): |
| 53 | + x = xp.ones((1, 1, 1, 1, 1)) |
| 54 | + |
| 55 | + y = atleast_nd(x, ndim=0, xp=xp) |
| 56 | + assert_array_equal(y, x) |
| 57 | + |
| 58 | + y = atleast_nd(x, ndim=4, xp=xp) |
| 59 | + assert_array_equal(y, x) |
| 60 | + |
| 61 | + y = atleast_nd(x, ndim=5, xp=xp) |
| 62 | + assert_array_equal(y, x) |
| 63 | + |
| 64 | + y = atleast_nd(x, ndim=6, xp=xp) |
| 65 | + assert_array_equal(y, xp.ones((1, 1, 1, 1, 1, 1))) |
| 66 | + |
| 67 | + y = atleast_nd(x, ndim=9, xp=xp) |
| 68 | + assert_array_equal(y, xp.ones((1, 1, 1, 1, 1, 1, 1, 1, 1))) |
0 commit comments