Debian Codegraph @

hypothesis-python/tests/cover/test_intervalset.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 pytest
12
13 from hypothesis import HealthCheck, assume, example, given, settings, strategies as st
14 from hypothesis.internal.conjecture.provider_conformance import (
15 interval_lists,
16 intervals,
17 )
18 from hypothesis.internal.intervalsets import IntervalSet
19
20 # various tests in this file impose a max_codepoint restriction on intervals,
21 # for performance. There may be possibilities for performance improvements in
22 # IntervalSet itself as well.
23
24
25 @given(intervals(max_codepoint=200))
26 @settings(deadline=None)
27 def test_intervals_are_equivalent_to_their_lists(intervals):
28 ls = list(intervals)
29 assert len(ls) == len(intervals)
30 for i in range(len(ls)):
31 assert ls[i] == intervals[i]
32 for i in range(1, len(ls) - 1):
33 assert ls[-i] == intervals[-i]
34
35
36 @given(intervals(max_codepoint=200))
37 def test_intervals_match_indexes(intervals):
38 ls = list(intervals)
39 for v in ls:
40 assert ls.index(v) == intervals.index(v)
41
42
43 @example(intervals=IntervalSet(((1, 1),)), v=0)
44 @example(intervals=IntervalSet(()), v=0)
45 @given(intervals(), st.integers(0, 0x10FFFF))
46 def test_error_for_index_of_not_present_value(intervals, v):
47 assume(v not in intervals)
48 with pytest.raises(ValueError):
49 intervals.index(v)
50
51
52 def test_validates_index():
53 with pytest.raises(IndexError):
54 IntervalSet([])[1]
55
56 with pytest.raises(IndexError):
57 IntervalSet([[1, 10]])[11]
58
59 with pytest.raises(IndexError):
60 IntervalSet([[1, 10]])[-11]
61
62
63 def test_index_above_is_index_if_present():
64 assert IntervalSet([[1, 10]]).index_above(1) == 0
65 assert IntervalSet([[1, 10]]).index_above(2) == 1
66
67
68 def test_index_above_is_length_if_higher():
69 assert IntervalSet([[1, 10]]).index_above(100) == 10
70
71
72 def intervals_to_set(ints):
73 return set(IntervalSet(ints))
74
75
76 @settings(suppress_health_check=[HealthCheck.filter_too_much, HealthCheck.too_slow])
77 @example(x=[(0, 1), (3, 3)], y=[(1, 3)])
78 @example(x=[(0, 1)], y=[(0, 0), (1, 1)])
79 @example(x=[(0, 1)], y=[(1, 1)])
80 @given(interval_lists(max_codepoint=200), interval_lists(max_codepoint=200))
81 def test_subtraction_of_intervals(x, y):
82 xs = intervals_to_set(x)
83 ys = intervals_to_set(y)
84 assume(not xs.isdisjoint(ys))
85 z = IntervalSet(x).difference(IntervalSet(y)).intervals
86 assert z == tuple(sorted(z))
87 for a, b in z:
88 assert a <= b
89 assert intervals_to_set(z) == intervals_to_set(x) - intervals_to_set(y)
90
91
92 @given(intervals(max_codepoint=200), intervals(max_codepoint=200))
93 def test_interval_intersection(x, y):
94 assert set(x & y) == set(x) & set(y)
95 assert set(x.intersection(y)) == set(x).intersection(y)
96
97
98 def test_char_in_shrink_order():
99 xs = IntervalSet([(0, 256)])
100 assert xs[xs._idx_of_zero] == ord("0")
101 assert xs[xs._idx_of_Z] == ord("Z")
102 rewritten = [ord(xs.char_in_shrink_order(i)) for i in range(256)]
103 assert rewritten != list(range(256))
104 assert sorted(rewritten) == sorted(range(256))
105
106
107 def test_index_from_char_in_shrink_order():
108 xs = IntervalSet([(0, 256)])
109 for i in xs:
110 assert xs.index_from_char_in_shrink_order(xs.char_in_shrink_order(i)) == i
111
112
113 def test_intervalset_equal():
114 xs1 = IntervalSet([(0, 256)])
115 xs2 = IntervalSet([(0, 256)])
116 assert xs1 == xs2
117
118 xs3 = IntervalSet([(0, 255)])
119 assert xs2 != xs3