|
| 1 | +import pytest |
| 2 | + |
| 3 | +from arrayfire.array_api import Array, float32, int16 |
| 4 | +from arrayfire.array_api._dtypes import supported_dtypes |
| 5 | + |
| 6 | + |
| 7 | +def test_empty_array() -> None: |
| 8 | + array = Array() |
| 9 | + |
| 10 | + assert array.dtype == float32 |
| 11 | + assert array.ndim == 0 |
| 12 | + assert array.size == 0 |
| 13 | + assert array.shape == () |
| 14 | + assert len(array) == 0 |
| 15 | + |
| 16 | + |
| 17 | +def test_empty_array_with_nonempty_dtype() -> None: |
| 18 | + array = Array(dtype=int16) |
| 19 | + |
| 20 | + assert array.dtype == int16 |
| 21 | + assert array.ndim == 0 |
| 22 | + assert array.size == 0 |
| 23 | + assert array.shape == () |
| 24 | + assert len(array) == 0 |
| 25 | + |
| 26 | + |
| 27 | +def test_empty_array_with_nonempty_shape() -> None: |
| 28 | + array = Array(shape=(2, 3)) |
| 29 | + |
| 30 | + assert array.dtype == float32 |
| 31 | + assert array.ndim == 2 |
| 32 | + assert array.size == 6 |
| 33 | + assert array.shape == (2, 3) |
| 34 | + assert len(array) == 2 |
| 35 | + |
| 36 | + |
| 37 | +def test_array_from_1d_list() -> None: |
| 38 | + array = Array([1, 2, 3]) |
| 39 | + |
| 40 | + assert array.dtype == float32 |
| 41 | + assert array.ndim == 1 |
| 42 | + assert array.size == 3 |
| 43 | + assert array.shape == (3,) |
| 44 | + assert len(array) == 3 |
| 45 | + |
| 46 | + |
| 47 | +def test_array_from_2d_list() -> None: |
| 48 | + with pytest.raises(TypeError): |
| 49 | + Array([[1, 2, 3], [1, 2, 3]]) |
| 50 | + |
| 51 | + |
| 52 | +def test_array_from_list_with_unsupported_dtype() -> None: |
| 53 | + for dtype in supported_dtypes: |
| 54 | + if dtype == float32: |
| 55 | + continue |
| 56 | + with pytest.raises(TypeError): |
| 57 | + Array([1, 2, 3], dtype=dtype) |
| 58 | + |
| 59 | + |
| 60 | +def test_array_from_af_array() -> None: |
| 61 | + array1 = Array([1]) |
| 62 | + array2 = Array(array1) |
| 63 | + |
| 64 | + assert array1.dtype == array2.dtype == float32 |
| 65 | + assert array1.ndim == array2.ndim == 1 |
| 66 | + assert array1.size == array2.size == 1 |
| 67 | + assert array1.shape == array2.shape == (1,) |
| 68 | + assert len(array1) == len(array2) == 1 |
| 69 | + |
| 70 | + |
| 71 | +def test_array_from_int_without_shape() -> None: |
| 72 | + with pytest.raises(TypeError): |
| 73 | + Array(1) |
| 74 | + |
| 75 | + |
| 76 | +def test_array_from_int_without_dtype() -> None: |
| 77 | + with pytest.raises(TypeError): |
| 78 | + Array(1, shape=(1,)) |
| 79 | + |
| 80 | +# def test_array_from_int_with_parameters() -> None: # BUG seg fault |
| 81 | +# array = Array(1, shape=(1,), dtype=float32) |
| 82 | + |
| 83 | +# assert array.dtype == float32 |
| 84 | +# assert array.ndim == 1 |
| 85 | +# assert array.size == 1 |
| 86 | +# assert array.shape == (1,) |
| 87 | +# assert len(array) == 1 |
| 88 | + |
| 89 | + |
| 90 | +def test_array_from_unsupported_type() -> None: |
| 91 | + with pytest.raises(TypeError): |
| 92 | + Array((5, 5)) # type: ignore[arg-type] |
| 93 | + |
| 94 | + with pytest.raises(TypeError): |
| 95 | + Array({1: 2, 3: 4}) # type: ignore[arg-type] |
0 commit comments