Debian Codegraph @

rpython/tool/algo/test/test_unionfind.py

raw symbols SCIP
1 from rpython.tool.algo.unionfind import UnionFind
2
3
4 def test_cleanup():
5 state = []
6 class ReferencedByExternalState(object):
7 def __init__(self, obj):
8 state.append(self)
9 self.obj = obj
10
11 def absorb(self, other):
12 state.remove(other)
13
14 uf = UnionFind(ReferencedByExternalState)
15 uf.find(1)
16 for i in xrange(1, 10, 2):
17 uf.union(i, 1)
18 uf.find(2)
19 for i in xrange(2, 20, 2):
20 uf.union(i, 2)
21 assert len(state) == 2 # we have exactly 2 partitions
22
23 def test_asymmetric_absorb():
24 class Info(object):
25 def __init__(self, obj):
26 self.values = [obj]
27
28 def absorb(self, other):
29 self.values += other.values
30
31 uf = UnionFind(Info)
32 uf.union(2, 3)
33 uf.union(1, 2)
34 assert uf[1].values == uf[2].values == uf[3].values == [1, 2, 3]