-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tuple.py
147 lines (123 loc) · 5.08 KB
/
test_tuple.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
import random
import pytest
import tuple
from hypothesis import given, assume
import hypothesis.strategies as st
@st.composite
def generate_tuple_params(draw):
""" use hypothesis to generate parameter sets """
result = {}
result['minval'] = draw(st.integers(min_value=0, max_value=1000))
result['maxval'] = draw(st.integers(
min_value=result['minval'], max_value=100000))
assume(result['maxval'] >= result['minval'])
result['maxrange'] = draw(st.integers(min_value=1, max_value=1000))
result['num'] = draw(st.integers(min_value=1, max_value=1000))
return result
def generate_tuples_test_primitive():
### deterministic edge case tests ###
tuple.generate_tuples(0,0,1,1)
with pytest.raises(tuple.TupleException):
tuple.generate_tuples(0,0,0,0)
with pytest.raises(tuple.TupleException):
tuple.generate_tuples(2,1,1,1)
@given(generate_tuple_params())
def generate_tuples_test_composite(params):
### use hypothesis composite to generate tuples from random input ###
# redundant with generate_tuples_test_data. Just to check out hypothesis
tuple.generate_tuples(**params)
@given(st.data())
def generate_tuples_test_data(data):
### draw from hypothesis data to generate tuples from random input ###
# redundant with generate_tuples_test_composite. Just to check out hypothesis
x = data.draw(st.integers(min_value=0, max_value=1000))
y = data.draw(st.integers(min_value=x, max_value=100000))
r = data.draw(st.integers(min_value=1, max_value=1000))
n = data.draw(st.integers(min_value=1, max_value=1000))
tuples = tuple.generate_tuples(x, y, r, n)
def generate_tuples_test_probability():
### test value distribution in generated tuples ###
tuples = tuple.generate_tuples(0, 100, 10, 100000)
lval_list = [x for (x, _) in tuples]
rval_list = [y for (_, y) in tuples]
# check edges
assert 0 in lval_list
assert 100 in rval_list
# check middle part
for i in range(1, 100):
assert i in lval_list
assert i in rval_list
# playing around with pytest really ;)
@pytest.mark.parametrize("tuple1, tuple2, output", [
((1, 1), (1, 1), (1, 1)),
((0, 1), (1, 2), (0, 2)),
((3, 5), (2, 5), (2, 5)),
((3, 5), (2, 4), (2, 5)),
((3, 5), (4, 7), (3, 7)),
])
def merge_test(tuple1, tuple2, output):
### test correctness of merge logic ###
assert tuple._merge(tuple1, tuple2) == output
def merge_tuples_test():
### verify dynamic invocation ###
assert tuple.merge_tuples('naive',[(0,0), (0,0)]) == [(0,0)]
with pytest.raises(tuple.TupleException):
assert tuple.merge_tuples('nonexistent',[(0,0), (0,0)])
def verify_tuples_test():
### test correctness of verify logic (must raise on overlap) ###
assert tuple.verify_tuples([(1, 1), (2, 3)])
assert tuple.verify_tuples([(10, 20), (2, 3)])
with pytest.raises(tuple.TupleException):
tuple.verify_tuples([(1, 2), (2, 3)])
with pytest.raises(tuple.TupleException):
tuple.verify_tuples([(5, 10), (2, 3), (8, 12)])
def sort_tuples_test():
assert tuple.sort_tuples([(1, 1), (2, 3)]) == [(1, 1), (2, 3)]
assert tuple.sort_tuples([(2, 3), (1, 1)]) == [(1, 1), (2, 3)]
@given(generate_tuple_params())
def count_tuples_test(params):
# simple tests
assert tuple.count_tuples([]) == 0
assert tuple.count_tuples([(1, 1)]) == 1
# run some random tests
for _ in range(0, 10):
tuples = tuple.generate_tuples(**params)
assert tuple.count_tuples(tuples) == params['num']
def overlap_test():
### test correctness of overlap logic ###
assert tuple._overlaps((1, 1), (1, 1)) == True
assert tuple._overlaps((3, 5), (1, 3)) == True
assert tuple._overlaps((3, 5), (1, 4)) == True
assert tuple._overlaps((3, 5), (3, 5)) == True
assert tuple._overlaps((3, 5), (2, 4)) == True
assert tuple._overlaps((3, 5), (3, 6)) == True
assert tuple._overlaps((2, 5), (5, 1)) == True
assert tuple._overlaps((2, 7), (5, 1)) == True
assert tuple._overlaps((2, 10), (5, 10)) == True
assert tuple._overlaps((5, 10), (5, 10)) == True
assert tuple._overlaps((5, 11), (5, 10)) == True
assert tuple._overlaps((6, 10), (5, 10)) == True
assert tuple._overlaps((1, 2), (3, 4)) == False
assert tuple._overlaps((5, 6), (3, 4)) == False
@pytest.mark.slow
@given(generate_tuple_params())
def naive_strategy_test(params):
for _ in range(0, 10):
tuples = tuple.generate_tuples(**params)
result = tuple.naive_strategy(tuples)
try:
tuple.verify_tuples(result)
except tuple.TupleException as e:
raise tuple.TupleException(
f'Validation failed for input data {tuples} with: {e}')
@pytest.mark.slow
@given(generate_tuple_params())
def sort_strategy_test(params):
for _ in range(0, 10):
tuples = tuple.generate_tuples(**params)
result = tuple.sort_strategy(tuples)
try:
tuple.verify_tuples(result)
except tuple.TupleException as e:
raise tuple.TupleException(
f'Validation failed for input data {tuples} with: {e}')