Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
def test_generate_indices_with_and_without_ellipsis():
    """Strategy can generate indices with and without Ellipsis."""
    strat = (xps.array_shapes(min_dims=1, max_dims=32).flatmap(
        xps.indices).map(lambda idx: idx
                         if isinstance(idx, tuple) else (idx, )))
    find_any(strat, lambda ix: Ellipsis in ix)
    find_any(strat, lambda ix: Ellipsis not in ix)
Ejemplo n.º 3
0
def test_minimize_arrays_with_0d_shape_strategy():
    """Strategy with shape strategy that can generate empty tuples minimizes to
    0d arrays."""
    smallest = minimal(xps.arrays(xp.int8, xps.array_shapes(min_dims=0)))
    assert smallest.shape == ()
Ejemplo n.º 4
0
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)


@given(xps.array_shapes(), st.data())
def test_draw_arrays_from_shapes(shape, data):
    """Draw arrays from shapes."""
    x = data.draw(xps.arrays(xp.int8, shape))
    assert x.ndim == len(shape)
    assert x.shape == shape
    assert_array_namespace(x)


@given(st.integers(0, 10), st.data())
def test_draw_arrays_from_int_shapes(size, data):
    """Draw arrays from integers as shapes."""
    x = data.draw(xps.arrays(xp.int8, size))
    assert x.shape == (size,)
    assert_array_namespace(x)
Ejemplo n.º 5
0
    )
)
def test_indices_replaces_whole_axis_slices_with_ellipsis(idx):
    # `slice(None)` (aka `:`) is the only valid index for an axis of size
    # zero, so if all dimensions are 0 then a `...` will replace all the
    # slices because we generate `...` for entire contiguous runs of `:`
    assert slice(None) not in idx


@given(xps.indices((3, 3, 3, 3, 3)))
def test_efficiently_generate_indexers(_):
    """Generation is not too slow."""


@given(
    shape=xps.array_shapes(min_dims=1, max_side=4)
    | xps.array_shapes(min_dims=1, min_side=0, max_side=10),
    allow_ellipsis=st.booleans(),
    data=st.data(),
)
def test_generate_valid_indices(shape, allow_ellipsis, data):
    """Strategy generates valid indices."""
    min_dims = data.draw(st.integers(0, len(shape)), label="min_dims")
    max_dims = data.draw(
        st.none() | st.integers(min_dims, len(shape)), label="max_dims"
    )
    indexer = data.draw(
        xps.indices(
            shape,
            min_dims=min_dims,
            max_dims=max_dims,
Ejemplo n.º 6
0
    func = getattr(xps, name)
    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."""