Debian Codegraph @

hypothesis-python/tests/numpy/test_narrow_floats.py

raw
1 # This file is part of Hypothesis, which may be found at
2 # https://github.com/HypothesisWorks/hypothesis/
3 #
4 # Copyright the Hypothesis Authors.
5 # Individual contributors are listed in AUTHORS.rst and the git log.
6 #
7 # This Source Code Form is subject to the terms of the Mozilla Public License,
8 # v. 2.0. If a copy of the MPL was not distributed with this file, You can
9 # obtain one at https://mozilla.org/MPL/2.0/.
10
11 import numpy as np
12 import pytest
13
14 from hypothesis import given
15 from hypothesis.extra.numpy import arrays, from_dtype, integer_dtypes
16 from hypothesis.strategies import data, floats, integers
17
18
19 @pytest.mark.parametrize("dtype", [np.float16, np.float32, np.float64])
20 @pytest.mark.parametrize("low", [-2.0, -1.0, 0.0, 1.0])
21 @given(data())
22 def test_bad_float_exclude_min_in_array(dtype, low, data):
23 elements = floats(
24 low, low + 1, exclude_min=True, width=np.dtype(dtype).itemsize * 8
25 )
26 x = data.draw(arrays(dtype, shape=(1,), elements=elements), label="x")
27 assert np.all(low < x)
28
29
30 @given(floats(width=32))
31 def test_float32_exactly_representable(x):
32 clipped = np.dtype("float32").type(x)
33 if np.isnan(x):
34 assert np.isnan(clipped)
35 else:
36 assert x == float(clipped)
37
38
39 @given(floats(width=16))
40 def test_float16_exactly_representable(x):
41 clipped = np.dtype("float16").type(x)
42 if np.isnan(x):
43 assert np.isnan(clipped)
44 else:
45 assert x == float(clipped)
46
47
48 @given(data=data(), dtype=integer_dtypes())
49 def test_floor_ceil_lossless(data, dtype):
50 # Regression test for issue #1667; ceil converting numpy integers
51 # to float and back to int with loss of exact value.
52 x = data.draw(from_dtype(dtype))
53 assert data.draw(integers(x, x)) == x