-
Notifications
You must be signed in to change notification settings - Fork 0
/
union_find_test.py
72 lines (48 loc) · 1.71 KB
/
union_find_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
# -*- Coding:utf-8 -*-
"""
"""
from nose.tools import eq_, assert_true, assert_false
from union_find import UnionFind, _isiterable
import pickle
def test_pickle():
union_find1 = UnionFind('root')
eq_(pickle.loads(pickle.dumps(union_find1)), union_find1)
union_find1.create_sub_union_find('sub1')
union_find1.create_sub_union_find('sub2')
union_find1.parent = UnionFind('root2')
eq_(pickle.loads(pickle.dumps(union_find1)), union_find1)
def test_empty_pickle():
union_find1 = UnionFind()
roundtrip = pickle.loads(pickle.dumps(union_find1))
eq_(roundtrip, union_find1)
def test_same():
union_find1 = UnionFind('root', subs=[])
sub1 = union_find1.create_sub_union_find('sub1')
union_find1.parent = UnionFind('root2', subs=[])
sub2 = union_find1.create_sub_union_find('sub2')
assert_true(sub1.same(sub2))
assert_true(sub2.same(sub1))
assert_true(sub1.same([sub1, sub2]))
union_find2 = UnionFind('root2', subs=[])
assert_false(union_find1.same(union_find2))
union_find2.data = 'root'
assert_false(union_find1.same(union_find2))
def test_union():
root1 = UnionFind('root')
sub1 = root1.create_sub_union_find('sub1')
roo2 = UnionFind('root2')
sub2 = roo2.create_sub_union_find('sub2')
sub2.unite(sub1)
assert_true(sub1.same([sub1, sub2, root1, roo2]))
def test_copy():
root1 = UnionFind('root')
root1.create_sub_union_find('sub1')
root1_copy = root1.copy()
eq_(root1, root1_copy)
assert_true(root1 is not root1_copy)
def test___str__():
root1 = UnionFind('root')
root1.create_sub_union_find('sub1')
isinstance(root1.__str__(), str)
def test__isiterable():
assert_false(_isiterable('aaa'))