Beispiel #1
0
def test_efficiently_generate_unique_arrays_using_all_elements(x):
    """Unique strategy with elements strategy range equivalent to its size
    generates arrays with all possible values. Generation is not too slow.

    Avoids the birthday paradox with UniqueSampledListStrategy.
    """
    assert xp.unique_values(x).size == x.size
Beispiel #2
0
def test_generate_unique_arrays_without_fill(x):
    """Generate arrays from unique strategy with no fill.

    Covers the collision-related branches for fully dense unique arrays.
    Choosing 25 of 256 possible values means we're almost certain to see
    colisions thanks to the birthday paradox, but finding unique values should
    still be easy.
    """
    assume(xp.unique_values(x).size == x.size)
Beispiel #3
0
def test_may_reuse_distinct_integers_if_asked():
    """Strategy with shared elements and fill strategies of distinct integers
    may generate arrays with non-distinct values."""
    find_any(
        xps.arrays(xp.uint64,
                   10,
                   elements=distinct_integers(),
                   fill=distinct_integers()),
        lambda x: xp.unique_values(x).size < x.size,
    )
Beispiel #4
0
def test_array_element_rewriting(data, start, size):
    """Unique strategy generates arrays with expected elements."""
    x = data.draw(
        xps.arrays(
            dtype=xp.int64,
            shape=size,
            elements=st.integers(start, start + size - 1),
            unique=True,
        ))
    x_set_expect = xp.linspace(start, start + size - 1, size, dtype=xp.int64)
    x_set = xp.sort(xp.unique_values(x))
    assert xp.all(x_set == x_set_expect)
Beispiel #5
0
def test_does_not_reuse_distinct_integers(x):
    """Strategy with distinct integer elements strategy generates arrays with
    distinct values."""
    assert xp.unique_values(x).size == x.size
Beispiel #6
0
def test_unique_array_with_fill_can_use_all_elements(x):
    """Unique strategy with elements range equivalent to its size and NaN fill
    can generate arrays with all possible values."""
    assume(xp.unique_values(x).size == x.size)
Beispiel #7
0
def test_is_still_unique_with_nan_fill(x):
    """Unique strategy with NaN fill generates unique arrays."""
    assert xp.unique_values(x).size == x.size
Beispiel #8
0
def test_generate_unique_arrays_using_all_elements(x):
    """Unique strategy with elements strategy range equal to its size will only
    generate arrays with one of each possible element."""
    assert xp.unique_values(x).size == x.size
Beispiel #9
0
def test_generate_unique_arrays(x):
    """Generates unique arrays."""
    assert xp.unique_values(x).size == x.size
Beispiel #10
0
from hypothesis.internal.floats import width_smallest_normals

from tests.array_api.common import COMPLIANT_XP, WIDTHS_FTZ, xp, xps
from tests.common.debug import find_any, minimal
from tests.common.utils import fails_with, flaky

needs_xp_unique_values = pytest.mark.skipif(not hasattr(xp, "unique_values"),
                                            reason="optional API")

# xp.unique_value() should return distinct NaNs - if not, tests that (rightly)
# assume such behaviour will likely fail. This mark namely addresses mocking the
# array module with NumPy 1.21, which treats NaNs as not distinct.
# See https://mail.python.org/pipermail/numpy-discussion/2021-August/081995.html
two_nans = xp.asarray([float("nan"), float("nan")])
assumes_distinct_nans = pytest.mark.xfail(
    hasattr(xp, "unique_values") and xp.unique_values(two_nans).size != 2,
    reason="NaNs not distinct",
)


def assert_array_namespace(x):
    """Check array has __array_namespace__() and it returns the correct module.

    This check is skipped if a mock array module is being used.
    """
    if COMPLIANT_XP:
        assert x.__array_namespace__() is xp


@given(xps.scalar_dtypes(), st.data())
def test_draw_arrays_from_dtype(dtype, data):