Debian Codegraph @

hypothesis-python/tests/nocover/test_compat.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 math
12
13 from hypothesis import given, strategies as st
14 from hypothesis.internal.compat import ceil, floor, int_from_bytes, int_to_bytes
15
16
17 @given(st.binary())
18 def test_convert_back(bs):
19 bs = bytearray(bs)
20 assert int_to_bytes(int_from_bytes(bs), len(bs)) == bs
21
22
23 bytes8 = st.builds(bytearray, st.binary(min_size=8, max_size=8))
24
25
26 @given(bytes8, bytes8)
27 def test_to_int_in_big_endian_order(x, y):
28 x, y = sorted((x, y))
29 assert 0 <= int_from_bytes(x) <= int_from_bytes(y)
30
31
32 ints8 = st.integers(min_value=0, max_value=2**63 - 1)
33
34
35 @given(ints8, ints8)
36 def test_to_bytes_in_big_endian_order(x, y):
37 x, y = sorted((x, y))
38 assert int_to_bytes(x, 8) <= int_to_bytes(y, 8)
39
40
41 @given(st.fractions())
42 def test_ceil(x):
43 assert isinstance(ceil(x), int)
44 assert x <= ceil(x) < x + 1
45 assert ceil(x) == math.ceil(x)
46
47
48 @given(st.fractions())
49 def test_floor(x):
50 assert isinstance(floor(x), int)
51 assert x - 1 < floor(x) <= x
52 assert floor(x) == math.floor(x)