gonum.org/v1/gonum@v0.14.0/graph/path/disjoint_test.go (about)

     1  // Copyright ©2014 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package path
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func TestDisjointSetMakeSet(t *testing.T) {
    12  	t.Parallel()
    13  
    14  	ds := make(djSet)
    15  	ds.add(3)
    16  	if len(ds) != 1 {
    17  		t.Error("Disjoint set master map of wrong size")
    18  	}
    19  
    20  	node, ok := ds[3]
    21  	if !ok {
    22  		t.Error("Make set did not successfully add element")
    23  	} else {
    24  		if node == nil {
    25  			t.Fatal("Disjoint set node from add is nil")
    26  		}
    27  
    28  		if node.rank != 0 {
    29  			t.Error("Node rank set incorrectly")
    30  		}
    31  
    32  		if node.parent != nil {
    33  			t.Error("Node parent set incorrectly")
    34  		}
    35  	}
    36  }
    37  
    38  func TestDisjointSetFind(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	ds := make(djSet)
    42  	ds.add(3)
    43  	ds.add(4)
    44  	ds.add(5)
    45  	ds.union(ds.find(3), ds.find(4))
    46  
    47  	if ds.find(3) == ds.find(5) {
    48  		t.Error("Disjoint sets incorrectly found to be the same")
    49  	}
    50  }
    51  
    52  func TestUnion(t *testing.T) {
    53  	t.Parallel()
    54  
    55  	ds := make(djSet)
    56  	ds.add(3)
    57  	ds.add(4)
    58  	ds.add(5)
    59  	ds.union(ds.find(3), ds.find(4))
    60  	ds.union(ds.find(4), ds.find(5))
    61  
    62  	if ds.find(3) != ds.find(5) {
    63  		t.Error("Sets found to be disjoint after union")
    64  	}
    65  }