MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / TestUnionFind

Function TestUnionFind

graph/unionfind_test.go:7–41  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

5)
6
7func TestUnionFind(t *testing.T) {
8 u := NewUnionFind(10) // Creating a Union-Find data structure with 10 elements
9
10 //union operations
11 u.Union(0, 1)
12 u.Union(2, 3)
13 u.Union(4, 5)
14 u.Union(6, 7)
15
16 // Testing the parent of specific elements
17 t.Run("Test Find", func(t *testing.T) {
18 if u.Find(0) != u.Find(1) || u.Find(2) != u.Find(3) || u.Find(4) != u.Find(5) || u.Find(6) != u.Find(7) {
19 t.Error("Union operation not functioning correctly")
20 }
21 })
22
23 u.Union(1, 5) // Additional union operation
24 u.Union(3, 7) // Additional union operation
25
26 // Testing the parent of specific elements after more union operations
27 t.Run("Test Find after Union", func(t *testing.T) {
28 if u.Find(0) != u.Find(5) || u.Find(1) != u.Find(4) || u.Find(2) != u.Find(7) || u.Find(3) != u.Find(6) {
29 t.Error("Union operation not functioning correctly")
30 }
31 })
32
33 u.Union(3, 7) // Repeated union operation
34
35 // Testing that repeated union operations are idempotent
36 t.Run("Test Find after repeated Union", func(t *testing.T) {
37 if u.Find(2) != u.Find(6) || u.Find(2) != u.Find(7) || u.Find(3) != u.Find(6) || u.Find(3) != u.Find(7) {
38 t.Error("Union operation not functioning correctly")
39 }
40 })
41}

Callers

nothing calls this directly

Calls 3

UnionMethod · 0.95
FindMethod · 0.95
NewUnionFindFunction · 0.85

Tested by

no test coverage detected