Skip to content

Commit eadbe9b

Browse files
committed
Add tests. Minor fixes. Update CI
1 parent b14aa91 commit eadbe9b

File tree

5 files changed

+110
-39
lines changed

5 files changed

+110
-39
lines changed

.github/workflows/build.yaml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ jobs:
99
steps:
1010
- uses: actions/checkout@v1
1111

12-
- name: Set up Python 3.9
12+
- name: Set up Python 3.10
1313
uses: actions/setup-python@v1
1414
with:
15-
python-version: 3.9
15+
python-version: 3.10
1616

1717
- name: Install dependencies
1818
run: |
1919
python -m pip install --upgrade pip
2020
pip install -r requirements.txt
21+
2122
- name: Test with pytest
22-
run: |
23-
pytest
23+
run: pytest
24+
25+
- name: Test array_api
26+
run: python -m pytest arrayfire/array_api

arrayfire/array_api/_array_object.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from ._dtypes import CShape, Dtype, c_dim_t, float32, supported_dtypes
1111
from ._utils import Device, PointerSource, to_str
1212

13-
ShapeType = tuple[None | int, ...]
13+
ShapeType = tuple[int, ...]
1414

1515

1616
@dataclass
@@ -30,9 +30,10 @@ class Array:
3030
arr = ctypes.c_void_p(0)
3131

3232
def __init__(
33-
self, x: None | Array | py_array.array | list = None, dtype: None | Dtype = None,
34-
pointer_source: PointerSource = PointerSource.host, shape: None | tuple[int] = None,
35-
offset: None | ctypes._SimpleCData[int] = None, strides: None | tuple[int, ...] = None) -> None:
33+
self, x: None | Array | py_array.array | int | ctypes.c_void_p | list = None, dtype: None | Dtype = None,
34+
pointer_source: PointerSource = PointerSource.host, shape: None | ShapeType = None,
35+
offset: None | ctypes._SimpleCData[int] = None, strides: None | ShapeType = None) -> None:
36+
_no_initial_dtype = False # HACK, FIXME
3637

3738
if isinstance(dtype, str):
3839
dtype = _str_to_dtype(dtype)
@@ -69,7 +70,7 @@ def __init__(
6970
_array_buffer = _ArrayBuffer(x if not isinstance(x, ctypes.c_void_p) else x.value)
7071

7172
if not shape:
72-
raise RuntimeError("Expected to receive the initial shape due to the x being a data pointer.")
73+
raise TypeError("Expected to receive the initial shape due to the x being a data pointer.")
7374

7475
if _no_initial_dtype:
7576
raise TypeError("Expected to receive the initial dtype due to the x being a data pointer.")
@@ -206,7 +207,7 @@ def _metadata_string(dtype: Dtype, dims: None | ShapeType = None) -> str:
206207
f"Dims: {str(dims) if dims else ''}")
207208

208209

209-
def _get_cshape(shape: None | tuple[int], buffer_length: int) -> CShape:
210+
def _get_cshape(shape: None | ShapeType, buffer_length: int) -> CShape:
210211
if shape:
211212
return CShape(*shape)
212213

arrayfire/array_api/pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[pytest]
2-
addopts = --cache-clear --cov=./arrayfire/array_api --flake8 --mypy --isort ./arrayfire/array_api
2+
addopts = --cache-clear --cov=./arrayfire/array_api --flake8 --isort ./arrayfire/array_api
33
console_output_style = classic
44
markers = mypy

arrayfire/array_api/tests/test_array.py

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)