-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_create.py
108 lines (79 loc) · 2.72 KB
/
test_create.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import numpy
import pytest
from utils import device, dtypeIsInt, mpi_dtypes
import sharpy as sp
@pytest.fixture(params=mpi_dtypes)
def datatype(request):
return request.param
@pytest.fixture(params=[(), (6,), (6, 5), (6, 5, 4)])
def shape(request):
return request.param
@pytest.fixture(
params=[
(sp.ones, 1.0),
(sp.zeros, 0.0),
],
ids=["ones", "zeros"],
)
def creator(request):
return request.param[0], request.param[1]
def test_arange():
n = 10
a = sp.arange(0, n, 1, dtype=sp.int32, device=device)
assert tuple(a.shape) == (n,)
assert numpy.allclose(sp.to_numpy(a), list(range(n)))
def test_arange2():
n = 10
a = sp.arange(0, n, dtype=sp.int32, device=device)
assert tuple(a.shape) == (n,)
assert numpy.allclose(sp.to_numpy(a), list(range(n)))
def test_arange3():
n = 10
a = sp.arange(n, device=device)
assert tuple(a.shape) == (n,)
assert numpy.allclose(sp.to_numpy(a), list(range(n)))
def test_arange_empty():
n = 10
a = sp.arange(n, 0, device=device)
assert tuple(a.shape) == (0,)
assert numpy.allclose(sp.to_numpy(a), list())
def test_arange_empty2():
n = 10
a = sp.arange(0, n, -1, device=device)
assert tuple(a.shape) == (0,)
assert numpy.allclose(sp.to_numpy(a), list())
def test_create_datatypes(creator, datatype):
shape = (6, 4)
func, expected_value = creator
a = func(shape, dtype=datatype, device=device)
assert tuple(a.shape) == shape
assert numpy.allclose(sp.to_numpy(a), [expected_value])
def test_create_shapes(creator, shape):
datatype = sp.int32
func, expected_value = creator
a = func(shape, dtype=datatype, device=device)
assert tuple(a.shape) == shape
assert numpy.allclose(sp.to_numpy(a), [expected_value])
@pytest.mark.parametrize("expected_value", [5.0])
def test_full_shapes(expected_value, shape):
datatype = sp.int32
value = int(expected_value) if dtypeIsInt(datatype) else expected_value
a = sp.full(shape, value, dtype=datatype, device=device)
assert tuple(a.shape) == shape
assert numpy.allclose(sp.to_numpy(a), [expected_value])
def test_create_invalid_shape(creator):
shape = (6, -5)
datatype = sp.int32
func, expected_value = creator
with pytest.raises(ValueError):
func(shape, dtype=datatype, device=device)
def test_full_invalid_shape():
shape = (6, -5)
value = 5
datatype = sp.int32
with pytest.raises(ValueError):
sp.full(shape, value, dtype=datatype, device=device)
@pytest.mark.parametrize("start,end,step", [(0, 99999999999999999999, 1)])
def tests_arange_invalid(start, end, step):
with pytest.raises(TypeError):
sp.arange(start, end, step, dtype=sp.int32, device=device)