-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathunionfind_test.go
66 lines (48 loc) · 1.18 KB
/
unionfind_test.go
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
package unionfind
import (
"testing"
. "gopkg.in/check.v1"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) {
TestingT(t)
}
type MySuite struct{}
var _ = Suite(&MySuite{})
// Test New
func (s *MySuite) TestNew(c *C) {
uf := New(10)
c.Assert(uf.size, DeepEquals, []int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
c.Assert(uf.root, DeepEquals, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
}
// Test Root when Empty
func (s *MySuite) TestRootEmpty(c *C) {
uf := New(10)
c.Assert(uf.Root(10), Equals, -1)
c.Assert(uf.Root(9), Equals, 9)
}
// Test Root when It has connected components
func (s *MySuite) TestRootConnected(c *C) {
uf := New(10)
uf.Union(1, 2)
uf.Union(2, 3)
uf.Union(5, 6)
uf.Union(2, 8)
c.Assert(uf.Root(8), Equals, 2)
c.Assert(uf.Root(2), Equals, 2)
c.Assert(uf.Root(5), Equals, 6)
}
// Test Union when when we connect the same item
func (s *MySuite) TestUnionSame(c *C) {
uf := New(10)
uf.Union(2, 2)
c.Assert(uf.Root(2), Equals, 2)
}
// Test Union checks size array when it assigns the root
func (s *MySuite) TestUnionChecksSizes(c *C) {
uf := New(5)
uf.Union(0, 1)
uf.Union(1, 2)
uf.Union(2, 3)
c.Assert(uf.size, DeepEquals, []int{1, 4, 1, 1, 1})
}