Skip to content

Commit 0d70435

Browse files
committed
Added more tests
1 parent 19695f2 commit 0d70435

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

dsu.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ type Dsu struct {
1212
// The element values are not stored explicitly in the data structure,
1313
// and the user of the data structure is responsible for maintaining
1414
// a correspondence between the element values and their indices.
15-
func NewDsu(n uint) *Dsu {
16-
dsu := &Dsu{
15+
func NewDsu(n uint) Dsu {
16+
dsu := Dsu{
1717
parent: make([]uint, n),
1818
size: make([]uint, n),
1919
}

dsu_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,21 @@ func TestSimpleUnion(t *testing.T) {
1616
t.Error("2 should not be in the same set as 0 and 1")
1717
}
1818
}
19+
20+
func TestDoubleUnion(t *testing.T) {
21+
d := dsu.NewDsu(3)
22+
d.Union(0, 1)
23+
d.Union(1, 2)
24+
if d.Find(0) != d.Find(1) || d.Find(1) != d.Find(2) || d.Find(0) != d.Find(2) {
25+
t.Error("0, 1, and 2 should be in the same set")
26+
}
27+
}
28+
29+
func TestUnionOfSameSet(t *testing.T) {
30+
d := dsu.NewDsu(2)
31+
d.Union(0, 1)
32+
d.Union(0, 1)
33+
if d.Find(0) != d.Find(1) {
34+
t.Error("0 and 1 should be in the same set")
35+
}
36+
}

0 commit comments

Comments
 (0)