Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d48c9f8

Browse files
committedSep 16, 2021
Rudimentary test_full_like()
1 parent 5aa7d90 commit d48c9f8

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed
 

‎array_api_tests/hypothesis_helpers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
floats, just, composite, one_of, none,
88
booleans)
99
from hypothesis.extra.numpy import mutually_broadcastable_shapes
10+
from hypothesis.extra.array_api import make_strategies_namespace
1011
from hypothesis import assume
1112

1213
from .pytest_helpers import nargs
@@ -16,10 +17,14 @@
1617
integer_or_boolean_dtype_objects, dtype_objects)
1718
from ._array_module import (ones, full, float32, float64, bool as bool_dtype, _UndefinedStub)
1819
from . import _array_module
20+
from ._array_module import mod as xp
1921

2022
from .function_stubs import elementwise_functions
2123

2224

25+
xps = make_strategies_namespace(xp)
26+
27+
2328
# Set this to True to not fail tests just because a dtype isn't implemented.
2429
# If no compatible dtype is implemented for a given test, the test will fail
2530
# with a hypothesis health check error. Note that this functionality will not

‎array_api_tests/test_creation_functions.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
from ._array_module import (asarray, arange, ceil, empty, eye, full,
2-
equal, all, linspace, ones, zeros, isnan)
1+
from ._array_module import (asarray, arange, ceil, empty, eye, full, full_like,
2+
equal, all, linspace, ones, zeros, isnan)
33
from .array_helpers import (is_integer_dtype, dtype_ranges,
44
assert_exactly_equal, isintegral, is_float_dtype)
55
from .hypothesis_helpers import (numeric_dtypes, dtypes, MAX_ARRAY_SIZE,
66
shapes, sizes, sqrt_sizes, shared_dtypes,
7-
scalars)
7+
scalars, xps)
88

99
from hypothesis import assume, given
10-
from hypothesis.strategies import integers, floats, one_of, none, booleans, just
10+
from hypothesis.strategies import integers, floats, one_of, none, booleans, just, shared
1111

1212
int_range = integers(-MAX_ARRAY_SIZE, MAX_ARRAY_SIZE)
1313
float_range = floats(-MAX_ARRAY_SIZE, MAX_ARRAY_SIZE,
@@ -131,8 +131,32 @@ def test_full(shape, fill_value, dtype):
131131
assert all(equal(a, asarray(fill_value, **kwargs))), "full() array did not equal the fill value"
132132

133133
# TODO: implement full_like (requires hypothesis arrays support)
134-
def test_full_like():
135-
pass
134+
@given(
135+
a=xps.arrays(
136+
dtype=shared(xps.scalar_dtypes(), key='dtypes'),
137+
shape=xps.array_shapes(),
138+
),
139+
fill_value=shared(xps.scalar_dtypes(), key='dtypes').flatmap(xps.from_dtype),
140+
kwargs=one_of(
141+
just({}),
142+
shared(xps.scalar_dtypes(), key='dtypes').map(lambda d: {'dtype': d}),
143+
),
144+
)
145+
def test_full_like(a, fill_value, kwargs):
146+
a_like = full_like(a, fill_value, **kwargs)
147+
148+
if kwargs is None:
149+
pass # TODO: Should it actually match the fill_value?
150+
else:
151+
assert a_like.dtype == a.dtype
152+
153+
assert a_like.shape == a.shape, "full_like() produced an array with incorrect shape"
154+
155+
if is_float_dtype(a_like.dtype) and isnan(asarray(fill_value)):
156+
assert all(isnan(a_like)), "full_like() array did not equal the fill value"
157+
else:
158+
assert all(equal(a_like, asarray(fill_value, dtype=a.dtype))), "full_like() array did not equal the fill value"
159+
136160

137161
@given(scalars(shared_dtypes, finite=True),
138162
scalars(shared_dtypes, finite=True),

0 commit comments

Comments
 (0)
Please sign in to comment.