コード例 #1
0
ファイル: test_arrays.py プロジェクト: vlulla/hypothesis
def test_minimize_arrays_with_default_dtype_shape_strategies():
    """Strategy with default scalar_dtypes and array_shapes strategies minimize
    to a boolean 1-dimensional array of size 1."""
    smallest = minimal(xps.arrays(xps.scalar_dtypes(), xps.array_shapes()))
    assert smallest.shape == (1,)
    assert smallest.dtype == xp.bool
    assert not xp.any(smallest)
コード例 #2
0
ファイル: test_arrays.py プロジェクト: vlulla/hypothesis
from tests.common.debug import find_any, minimal
from tests.common.utils import fails_with, flaky

needs_xp_unique = pytest.mark.skipif(not hasattr(xp, "unique"), reason="optional API")


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):
    """Draw arrays from dtypes."""
    x = data.draw(xps.arrays(dtype, ()))
    assert x.dtype == dtype
    assert_array_namespace(x)


@given(st.sampled_from(DTYPE_NAMES), st.data())
def test_draw_arrays_from_scalar_names(name, data):
    """Draw arrays from dtype names."""
    x = data.draw(xps.arrays(name, ()))
    assert x.dtype == getattr(xp, name)
    assert_array_namespace(x)

コード例 #3
0
    assert func.__name__ == name
    assert func.__doc__ is not None
    # The (private) top-level strategy methods may expose a xp argument in their
    # function signatures. make_strategies_namespace() exists to wrap these
    # top-level methods by binding the passed xp argument, and so the namespace
    # it returns should not expose xp in any of its function signatures.
    assert "xp" not in signature(func).parameters.keys()


@pytest.mark.parametrize(
    "name, strat",
    [
        ("from_dtype", xps.from_dtype(xp.int8)),
        ("arrays", xps.arrays(xp.int8, 5)),
        ("array_shapes", xps.array_shapes()),
        ("scalar_dtypes", xps.scalar_dtypes()),
        ("boolean_dtypes", xps.boolean_dtypes()),
        ("numeric_dtypes", xps.numeric_dtypes()),
        ("integer_dtypes", xps.integer_dtypes()),
        ("unsigned_integer_dtypes", xps.unsigned_integer_dtypes()),
        ("floating_dtypes", xps.floating_dtypes()),
        ("valid_tuple_axes", xps.valid_tuple_axes(0)),
        ("broadcastable_shapes", xps.broadcastable_shapes(())),
        ("mutually_broadcastable_shapes",
         xps.mutually_broadcastable_shapes(3)),
        ("indices", xps.indices((5, ))),
    ],
)
def test_namespaced_strategies_repr(name, strat):
    """Namespaced strategies have good repr."""
    assert repr(strat).startswith(name +
コード例 #4
0
ファイル: test_from_dtype.py プロジェクト: vlulla/hypothesis
# obtain one at https://mozilla.org/MPL/2.0/.
#
# END HEADER

import math

import pytest

from hypothesis import given, strategies as st
from hypothesis.extra.array_api import DTYPE_NAMES, find_castable_builtin_for_dtype

from tests.array_api.common import xp, xps
from tests.common.debug import minimal


@given(xps.scalar_dtypes())
def test_strategies_have_reusable_values(dtype):
    """Inferred strategies have reusable values."""
    strat = xps.from_dtype(dtype)
    assert strat.has_reusable_values


DTYPES = [getattr(xp, name) for name in DTYPE_NAMES]


@pytest.mark.parametrize("dtype", DTYPES)
def test_produces_castable_instances_from_dtype(dtype):
    """Strategies inferred by dtype generate values of a builtin type castable
    to the dtype."""
    builtin = find_castable_builtin_for_dtype(xp, dtype)
コード例 #5
0
def test_minimise_scalar_dtypes():
    assert minimal(xps.scalar_dtypes()) == xp.bool