This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_gg_basic.py
executable file
·197 lines (135 loc) · 4.25 KB
/
test_gg_basic.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
#! /usr/bin/env py.test
import py
import gg
def pytest_funcarg__gr(request):
gr = gg.graph()
for i in range(100):
for j in range(i):
gr.add_link(i, j)
return gr
small_links = set([(1, 2), (3, 4), (5, 1), (5, 4)])
def pytest_funcarg__small(request):
small = gg.graph()
for x in small_links:
small.add_link(*x)
return small
# -- tests
def test_iter(small):
assert set(small) == small_links
def test_iter_gr(gr):
expected = set()
for i in range(100):
for j in range(i):
expected.add((i, j))
assert set(gr) == expected
def test_iter_empty():
assert list(gg.graph()) == []
def test_iter_restart(small):
try:
for i, x in enumerate(small):
print "iterate, link:", x
if i == 1:
raise RuntimeError()
except RuntimeError:
pass
links = set(small)
assert links == small_links
def test_get_links_from(gr):
for i in range(100):
assert set(gr.get_links_from(i)) == set(range(i))
def test_get_links_to(gr):
for i in range(100):
set(gr.get_links_to(i)) == range(1, 100 - i)
def test_graph_len():
g = gg.graph()
assert len(g) == 0
g.add_link(1, 2)
assert len(g) == 1
g.add_link(1, 2)
assert len(g) == 1
g.add_link(2, 1)
assert len(g) == 2
def test_intvector_append():
g = gg.intvector()
for i in range(1000):
g.append(i)
assert list(g) == range(1000)
def test_intvector_empty_init():
g = gg.intvector()
assert list(g) == []
def test_intvector_init_from_list():
g = gg.intvector(range(100))
assert list(g) == range(100)
def test_intvector_init_from_list_with_float():
py.test.raises(TypeError, "gg.intvector([0, 1.5])")
def test_get_reachable_from_empty_graph():
g = gg.graph()
res = g.get_reachable_from(gg.intvector(range(10)))
assert list(res) == range(10)
def test_get_reachable_from():
g = gg.graph()
g.add_link(1, 2)
g.add_link(1, 3)
g.add_link(2, 4)
res = g.get_reachable_from(gg.intvector([1]))
assert set(res) == set([1, 2, 3, 4])
res = g.get_reachable_from(gg.intvector([4]))
assert set(res) == set([4])
def test_get_reachable_from_cyclic():
g = gg.graph()
g.add_link(1, 2)
g.add_link(2, 3)
g.add_link(3, 1)
res = g.get_reachable_from(gg.intvector([1]))
assert set(res) == set([1, 2, 3])
def test_get_reachable_from_multiple_parents():
g = gg.graph()
g.add_link(1, 3)
g.add_link(2, 3)
res = g.get_reachable_from(gg.intvector([1, 2]))
assert list(res) == [1, 2, 3]
def test_maxstartnode():
g = gg.graph()
g.add_link(1, 3)
assert g.maxstartnode() == 1
assert g.maxendnode() == 3
def test_get_links_from_boolvector_return(gr):
allowed = gg.boolvector()
cids = gg.intvector()
cids.append(1)
res = gr.get_links_from(cids, allowed)
assert res is allowed
def test_get_links_from_boolvector_empty(small):
cids = gg.intvector()
allowed = gg.boolvector()
small.get_links_from(cids, allowed)
assert list(allowed) == [False] * 5
allowed[1] = True
small.get_links_from(cids, allowed)
assert list(allowed) == [False, True, False, False, False]
def test_get_links_from_boolvector(small):
cids = gg.intvector([3, 5])
allowed = gg.boolvector()
small.get_links_from(cids, allowed)
assert list(allowed) == [False, True, False, False, True]
assert list(cids) == [3, 5]
allowed = gg.boolvector()
cids.append(1)
small.get_links_from(cids, allowed)
assert list(allowed) == [False, True, True, False, True]
def test_get_links_from_allowed(small):
cids = gg.intvector([3, 5])
result = gg.boolvector()
allowed = gg.boolvector()
small.get_links_from(cids, result, allowed)
assert list(result) == [False, False, False, False, False]
allowed.resize(10)
small.get_links_from(cids, result, allowed)
assert list(result) == [False, False, False, False, False]
allowed[1] = True
small.get_links_from(cids, result, allowed)
assert list(result) == [False, True, False, False, False]
for i in range(10):
allowed[i] = True
small.get_links_from(cids, result, allowed)
assert list(result) == [False, True, False, False, True]