github.com/yimialmonte/fabric@v2.1.1+incompatible/common/graph/perm_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package graph
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestPermute(t *testing.T) {
    17  	vR := NewTreeVertex("r", nil)
    18  	vR.Threshold = 2
    19  
    20  	vD := vR.AddDescendant(NewTreeVertex("D", nil))
    21  	vD.Threshold = 2
    22  	for _, id := range []string{"A", "B", "C"} {
    23  		vD.AddDescendant(NewTreeVertex(id, nil))
    24  	}
    25  
    26  	vE := vR.AddDescendant(NewTreeVertex("E", nil))
    27  	vE.Threshold = 2
    28  	for _, id := range []string{"a", "b", "c"} {
    29  		vE.AddDescendant(NewTreeVertex(id, nil))
    30  	}
    31  
    32  	vF := vR.AddDescendant(NewTreeVertex("F", nil))
    33  	vF.Threshold = 2
    34  	for _, id := range []string{"1", "2", "3"} {
    35  		vF.AddDescendant(NewTreeVertex(id, nil))
    36  	}
    37  
    38  	permutations := vR.ToTree().Permute(1000)
    39  	// For a sub-tree with r-(D,E) we have 9 combinations (3 combinations of each sub-tree where D and E are the roots)
    40  	// For a sub-tree with r-(D,F) we have 9 combinations from the same logic
    41  	// For a sub-tree with r-(E,F) we have 9 combinations too
    42  	// Total 27 combinations
    43  	assert.Equal(t, 27, len(permutations))
    44  
    45  	listCombination := func(i Iterator) []string {
    46  		var traversal []string
    47  		for {
    48  			v := i.Next()
    49  			if v == nil {
    50  				break
    51  			}
    52  			traversal = append(traversal, v.Id)
    53  		}
    54  		return traversal
    55  	}
    56  
    57  	// First combination is a left most traversal on the combination graph
    58  	expectedScan := []string{"r", "D", "E", "A", "B", "a", "b"}
    59  	assert.Equal(t, expectedScan, listCombination(permutations[0].BFS()))
    60  
    61  	// Last combination is a right most traversal on the combination graph
    62  	expectedScan = []string{"r", "E", "F", "b", "c", "2", "3"}
    63  	assert.Equal(t, expectedScan, listCombination(permutations[26].BFS()))
    64  }
    65  
    66  func TestPermuteTooManyCombinations(t *testing.T) {
    67  	root := NewTreeVertex("r", nil)
    68  	root.Threshold = 500
    69  	for i := 0; i < 1000; i++ {
    70  		root.AddDescendant(NewTreeVertex(fmt.Sprintf("%d", i), nil))
    71  	}
    72  	permutations := root.ToTree().Permute(501)
    73  	assert.Len(t, permutations, 501)
    74  }