forked from HypothesisWorks/hypothesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_from_dtype.py
107 lines (87 loc) · 3.88 KB
/
test_from_dtype.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
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
import math
import pytest
from hypothesis.extra.array_api import DTYPE_NAMES, find_castable_builtin_for_dtype
from hypothesis.internal.floats import width_smallest_normals
from tests.array_api.common import flushes_to_zero
from tests.common.debug import (
assert_all_examples,
assert_no_examples,
find_any,
minimal,
)
@pytest.mark.parametrize("dtype_name", DTYPE_NAMES)
def test_strategies_have_reusable_values(xp, xps, dtype_name):
"""Inferred strategies have reusable values."""
strat = xps.from_dtype(dtype_name)
assert strat.has_reusable_values
@pytest.mark.parametrize("dtype_name", DTYPE_NAMES)
def test_produces_castable_instances_from_dtype(xp, xps, dtype_name):
"""Strategies inferred by dtype generate values of a builtin type castable
to the dtype."""
dtype = getattr(xp, dtype_name)
builtin = find_castable_builtin_for_dtype(xp, dtype)
assert_all_examples(xps.from_dtype(dtype), lambda v: isinstance(v, builtin))
@pytest.mark.parametrize("dtype_name", DTYPE_NAMES)
def test_produces_castable_instances_from_name(xp, xps, dtype_name):
"""Strategies inferred by dtype name generate values of a builtin type
castable to the dtype."""
dtype = getattr(xp, dtype_name)
builtin = find_castable_builtin_for_dtype(xp, dtype)
assert_all_examples(xps.from_dtype(dtype_name), lambda v: isinstance(v, builtin))
@pytest.mark.parametrize("dtype_name", DTYPE_NAMES)
def test_passing_inferred_strategies_in_arrays(xp, xps, dtype_name):
"""Inferred strategies usable in arrays strategy."""
elements = xps.from_dtype(dtype_name)
find_any(xps.arrays(dtype_name, 10, elements=elements))
@pytest.mark.parametrize(
"dtype, kwargs, predicate",
[
# Floating point: bounds, exclusive bounds, and excluding nonfinites
("float32", {"min_value": 1, "max_value": 2}, lambda x: 1 <= x <= 2),
(
"float32",
{"min_value": 1, "max_value": 2, "exclude_min": True, "exclude_max": True},
lambda x: 1 < x < 2,
),
("float32", {"allow_nan": False}, lambda x: not math.isnan(x)),
("float32", {"allow_infinity": False}, lambda x: not math.isinf(x)),
("float32", {"allow_nan": False, "allow_infinity": False}, math.isfinite),
# Integer bounds, limited to the representable range
("int8", {"min_value": -1, "max_value": 1}, lambda x: -1 <= x <= 1),
("uint8", {"min_value": 1, "max_value": 2}, lambda x: 1 <= x <= 2),
],
)
def test_from_dtype_with_kwargs(xp, xps, dtype, kwargs, predicate):
"""Strategies inferred with kwargs generate values in bounds."""
strat = xps.from_dtype(dtype, **kwargs)
assert_all_examples(strat, predicate)
def test_can_minimize_floats(xp, xps):
"""Inferred float strategy minimizes to a good example."""
smallest = minimal(xps.from_dtype(xp.float32), lambda n: n >= 1.0)
assert smallest == 1
smallest_normal = width_smallest_normals[32]
@pytest.mark.parametrize(
"kwargs",
[
{},
{"min_value": -1},
{"max_value": 1},
{"min_value": -1, "max_value": 1},
],
)
def test_subnormal_generation(xp, xps, kwargs):
"""Generation of subnormals is dependent on FTZ behaviour of array module."""
strat = xps.from_dtype(xp.float32, **kwargs).filter(lambda n: n != 0)
if flushes_to_zero(xp, width=32):
assert_no_examples(strat, lambda n: -smallest_normal < n < smallest_normal)
else:
find_any(strat, lambda n: -smallest_normal < n < smallest_normal)