-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathsorting_test.py
293 lines (238 loc) · 6.81 KB
/
sorting_test.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"""
intervaltree: A mutable, self-balancing interval tree for Python.
Queries may be by point, by range overlap, or by range envelopment.
Test module: Intervals, sorting methods
Copyright 2013-2018 Chaim Leib Halbert
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from intervaltree import Interval
import pytest
def test_interval_overlaps_point():
iv = Interval(0, 10)
assert not iv.overlaps(-5)
assert iv.overlaps(0)
assert iv.overlaps(5)
assert not iv.overlaps(10)
assert not iv.overlaps(15)
def test_interval_overlaps_range():
iv0 = Interval(0, 10)
iv1 = (-10, -5)
iv2 = (-10, 0)
iv3 = (-10, 5)
iv4 = (-10, 10)
iv5 = (-10, 20)
iv6 = (0, 20)
iv7 = (5, 20)
iv8 = (10, 20)
iv9 = (15, 20)
assert iv0.overlaps(*iv0[0:1])
assert not iv0.overlaps(*iv1)
assert not iv0.overlaps(*iv2)
assert iv0.overlaps(*iv3)
assert iv0.overlaps(*iv4)
assert iv0.overlaps(*iv5)
assert iv0.overlaps(*iv6)
assert iv0.overlaps(*iv7)
assert not iv0.overlaps(*iv8)
assert not iv0.overlaps(*iv9)
assert iv0.overlaps(Interval(*iv0))
assert not iv0.overlaps(Interval(*iv1))
assert not iv0.overlaps(Interval(*iv2))
assert iv0.overlaps(Interval(*iv3))
assert iv0.overlaps(Interval(*iv4))
assert iv0.overlaps(Interval(*iv5))
assert iv0.overlaps(Interval(*iv6))
assert iv0.overlaps(Interval(*iv7))
assert not iv0.overlaps(Interval(*iv8))
assert not iv0.overlaps(Interval(*iv9))
def test_interval_int_comparison_operators():
"""
Test comparisons with integers using < and >
"""
iv = Interval(0, 10)
assert (iv > -5)
assert (-5 < iv)
assert not (iv < -5)
assert not (-5 > iv)
assert (iv > 0) # special for sorting
assert (0 < iv) # special for sorting
assert not (iv < 0)
assert not (0 > iv)
assert not (iv > 5)
assert not (5 < iv)
assert (iv < 5) # special for sorting
assert (5 > iv) # special for sorting
assert not (iv > 10)
assert not (10 < iv)
assert (iv < 10)
assert (10 > iv)
assert not (iv > 15)
assert not (15 < iv)
assert (iv < 15)
assert (15 > iv)
def test_interval_int_comparison_methods():
"""
Test comparisons with integers using gt(), ge(), lt() and le()
"""
iv = Interval(0, 10)
assert iv.gt(-5)
assert iv.ge(-5)
assert not iv.lt(-5)
assert not iv.le(-5)
assert not iv.gt(0)
assert iv.ge(0)
assert not iv.lt(0)
assert not iv.le(0)
assert not iv.gt(5)
assert not iv.ge(5)
assert not iv.lt(5)
assert not iv.le(5)
assert not iv.gt(10)
assert not iv.ge(10)
assert iv.lt(10)
assert iv.le(10)
assert not iv.gt(15)
assert not iv.ge(15)
assert iv.lt(15)
assert iv.le(15)
def test_interval_interval_comparison_methods():
"""
Test comparisons with other Intervals using gt(), ge(), lt() and
le()
"""
iv0 = Interval(0, 10)
iv1 = Interval(-10, -5)
iv2 = Interval(-10, 0)
iv3 = Interval(-10, 5)
iv4 = Interval(-10, 10)
iv5 = Interval(-10, 20)
iv6 = Interval(0, 20)
iv7 = Interval(5, 20)
iv8 = Interval(10, 20)
iv9 = Interval(15, 20)
assert not iv0.gt(iv0)
assert iv0.gt(iv1)
assert iv0.gt(iv2)
assert not iv0.gt(iv3)
assert not iv0.gt(iv4)
assert not iv0.gt(iv5)
assert not iv0.gt(iv6)
assert not iv0.gt(iv7)
assert not iv0.gt(iv8)
assert not iv0.gt(iv9)
assert iv0.ge(iv0)
assert iv0.ge(iv1)
assert iv0.ge(iv2)
assert iv0.ge(iv3)
assert iv0.ge(iv4)
assert iv0.ge(iv5)
assert iv0.ge(iv6)
assert not iv0.ge(iv7)
assert not iv0.ge(iv8)
assert not iv0.ge(iv9)
assert not iv0.lt(iv0)
assert not iv0.lt(iv1)
assert not iv0.lt(iv2)
assert not iv0.lt(iv3)
assert not iv0.lt(iv4)
assert not iv0.lt(iv5)
assert not iv0.lt(iv6)
assert not iv0.lt(iv7)
assert iv0.lt(iv8)
assert iv0.lt(iv9)
assert iv0.le(iv0)
assert not iv0.le(iv1)
assert not iv0.le(iv2)
assert not iv0.le(iv3)
assert iv0.le(iv4)
assert iv0.le(iv5)
assert iv0.le(iv6)
assert iv0.le(iv7)
assert iv0.le(iv8)
assert iv0.le(iv9)
def test_interval_null_interval_comparison_methods():
"""
Test comparisons with other Intervals using gt(), ge(), lt() and
le()
"""
iv0 = Interval(0, 10)
ivn = Interval(0, 0)
with pytest.raises(ValueError):
iv0.gt(ivn)
with pytest.raises(ValueError):
ivn.gt(iv0)
with pytest.raises(ValueError):
iv0.ge(ivn)
with pytest.raises(ValueError):
ivn.ge(iv0)
with pytest.raises(ValueError):
iv0.lt(ivn)
with pytest.raises(ValueError):
ivn.lt(iv0)
with pytest.raises(ValueError):
iv0.le(ivn)
with pytest.raises(ValueError):
ivn.le(iv0)
def test_interval_interval_cmp():
"""
Test comparisons with other Intervals using __cmp__()
"""
iv0 = Interval(0, 10)
iv1 = Interval(-10, -5)
iv2 = Interval(-10, 0)
iv3 = Interval(-10, 5)
iv4 = Interval(-10, 10)
iv5 = Interval(-10, 20)
iv6 = Interval(0, 20)
iv7 = Interval(5, 20)
iv8 = Interval(10, 20)
iv9 = Interval(15, 20)
assert iv0.__cmp__(iv0) == 0
assert iv0.__cmp__(iv1) == 1
assert iv0.__cmp__(iv2) == 1
assert iv0.__cmp__(iv3) == 1
assert iv0.__cmp__(iv4) == 1
assert iv0.__cmp__(iv5) == 1
assert iv0.__cmp__(iv6) == -1
assert iv0.__cmp__(iv7) == -1
assert iv0.__cmp__(iv8) == -1
assert iv0.__cmp__(iv9) == -1
def test_interval_int_cmp():
"""
Test comparisons with ints using __cmp__()
"""
iv = Interval(0, 10)
assert iv.__cmp__(-5) == 1
assert iv.__cmp__(0) == 1
assert iv.__cmp__(5) == -1
assert iv.__cmp__(10) == -1
assert iv.__cmp__(15) == -1
def test_interval_sort_interval():
base = Interval(0, 10)
ivs = [
Interval(-10, -5),
Interval(-10, 0),
Interval(-10, 5),
Interval(-10, 10),
Interval(-10, 20),
Interval(0, 20),
Interval(5, 20),
Interval(10, 20),
Interval(15, 20),
]
for iv in ivs:
sort = sorted([base, iv])
assert sort[0].__cmp__(sort[1]) in (-1, 0)
sort = sorted([iv, base])
assert sort[0].__cmp__(sort[1]) in (-1, 0)
if __name__ == "__main__":
import pytest
pytest.main([__file__, '-v'])