github.com/awesome-flow/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/util/data/ntree_test.go (about)

     1  package data
     2  
     3  //TODO: this test suite fails
     4  
     5  import (
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  type dummy struct {
    11  	ch rune
    12  }
    13  
    14  func TestNTree_buildCompTree(t *testing.T) {
    15  
    16  	a, b, c, d, e, f := &dummy{'a'}, &dummy{'b'}, &dummy{'c'}, &dummy{'d'}, &dummy{'e'}, &dummy{'f'}
    17  
    18  	tree := &NTree{}
    19  	aNode := tree.FindOrInsert(a)
    20  	aNode.FindOrInsert(c)
    21  	aNode.FindOrInsert(d)
    22  
    23  	bNode := aNode.FindOrInsert(b)
    24  	bNode.FindOrInsert(e)
    25  	bNode.FindOrInsert(f)
    26  
    27  	if ptr := tree.Find(a); ptr != nil {
    28  		if ptr.value != a {
    29  			t.Errorf("Unexpected contents in ptr: %+v", ptr)
    30  		}
    31  	} else {
    32  		t.Errorf("Failed to find a")
    33  	}
    34  	nodeA, nodeInsA := tree.Find(a), tree.FindOrInsert(a)
    35  	if nodeA != nodeInsA {
    36  		t.Errorf("Find and findAndInsert returned different values")
    37  	}
    38  	nodeA, parentNodeB := tree.Find(a), tree.FindParent(b)
    39  	if nodeA != parentNodeB {
    40  		t.Errorf("Expected node A to be the parent of node B, %#v, %#v", nodeA, parentNodeB)
    41  	}
    42  	parentNodeC, parentNodeD, nodeA := tree.FindParent(c), tree.FindParent(d), tree.Find(a)
    43  	if nodeA != parentNodeC || nodeA != parentNodeD {
    44  		t.Errorf("Expected node A to be the parent of nodes C and D")
    45  	}
    46  	nodeB, parentNodeE, parentNodeF := tree.Find(b), tree.FindParent(e), tree.FindParent(f)
    47  	if nodeB != parentNodeE || nodeB != parentNodeF {
    48  		t.Errorf("Expected node B to be the parent of nodes E and F")
    49  	}
    50  }
    51  
    52  func TestNTRee_FindCommonParent(t *testing.T) {
    53  	//      R
    54  	//   A      B
    55  	// C  D   E   F
    56  	//G    H        K
    57  	tree := &NTree{}
    58  	nodeA := tree.FindOrInsert("A")
    59  	nodeB := tree.FindOrInsert("B")
    60  	nodeC := nodeA.FindOrInsert("C")
    61  	nodeD := nodeA.FindOrInsert("D")
    62  	nodeF := nodeB.FindOrInsert("F")
    63  	nodeB.FindOrInsert("E")
    64  	nodeC.FindOrInsert("G")
    65  	nodeD.FindOrInsert("H")
    66  	nodeF.FindOrInsert("K")
    67  
    68  	tests := []struct {
    69  		name   string
    70  		lookup []interface{}
    71  		expLCA *NTree
    72  	}{
    73  		{"A and B", []interface{}{"A", "B"}, tree},
    74  		{"C and D", []interface{}{"C", "D"}, nodeA},
    75  		{"E and F", []interface{}{"E", "F"}, nodeB},
    76  		{"G and H", []interface{}{"G", "H"}, nodeA},
    77  		{"E and K", []interface{}{"E", "K"}, nodeB},
    78  		{"G and E", []interface{}{"G", "E"}, tree},
    79  		{"H and E", []interface{}{"H", "E"}, tree},
    80  		{"H and A", []interface{}{"H", "A"}, nodeA},
    81  		{"E and B", []interface{}{"E", "B"}, nodeB},
    82  	}
    83  
    84  	for _, testCase := range tests {
    85  		t.Run(testCase.name, func(t *testing.T) {
    86  			lca := tree.FindCommonParent(testCase.lookup...)
    87  			if lca != testCase.expLCA {
    88  				t.Errorf("Unexpected LCA in %s: expected: %+v, got: %+v",
    89  					testCase.name, testCase.expLCA, lca)
    90  			}
    91  		})
    92  	}
    93  }
    94  
    95  func TestNTree_DetachNonEmpty(t *testing.T) {
    96  	tree := &NTree{}
    97  	nodeA := tree.FindOrInsert("A")
    98  	tree.FindOrInsert("B")
    99  	tree.FindOrInsert("C")
   100  	detA := tree.Detach("A")
   101  	if detA != nodeA {
   102  		t.Errorf("Unexpected value returned by Detach: %+v, expected: %+v", detA, nodeA)
   103  	}
   104  	lookupA := tree.Find("A")
   105  	if lookupA != nil {
   106  		t.Errorf("Did not expect to find node in the tree once detached")
   107  	}
   108  }
   109  
   110  func TestNTree_DetachEmpty(t *testing.T) {
   111  	tree := &NTree{}
   112  	tree.FindOrInsert("A")
   113  	tree.FindOrInsert("B")
   114  	tree.FindOrInsert("C")
   115  	detD := tree.Detach("D")
   116  	if detD != nil {
   117  		t.Errorf("Unexpected value returned by Detach: %+v, expected: nil", detD)
   118  	}
   119  }
   120  
   121  func TestNTree_PostTraversal(t *testing.T) {
   122  	//      R
   123  	//   A      B
   124  	// C  D   E   F
   125  	//G    H        K
   126  	tree := &NTree{}
   127  	nodeA := tree.FindOrInsert("A")
   128  	nodeB := tree.FindOrInsert("B")
   129  	nodeC := nodeA.FindOrInsert("C")
   130  	nodeD := nodeA.FindOrInsert("D")
   131  	nodeB.FindOrInsert("E")
   132  	nodeF := nodeB.FindOrInsert("F")
   133  	nodeC.FindOrInsert("G")
   134  	nodeD.FindOrInsert("H")
   135  	nodeF.FindOrInsert("K")
   136  	res := tree.PostTraversal()
   137  	if !reflect.DeepEqual(res, []interface{}{
   138  		"G", "C", "H", "D", "A", "E", "K", "F", "B",
   139  	}) {
   140  		t.Errorf("Unexpected tree post-traversal result: %+v", res)
   141  	}
   142  }
   143  
   144  func TestNTree_PreTraversal(t *testing.T) {
   145  	//      R
   146  	//   A      B
   147  	// C  D   E   F
   148  	//G    H        K
   149  	tree := &NTree{}
   150  	nodeA := tree.FindOrInsert("A")
   151  	nodeB := tree.FindOrInsert("B")
   152  	nodeC := nodeA.FindOrInsert("C")
   153  	nodeD := nodeA.FindOrInsert("D")
   154  	nodeB.FindOrInsert("E")
   155  	nodeF := nodeB.FindOrInsert("F")
   156  	nodeC.FindOrInsert("G")
   157  	nodeD.FindOrInsert("H")
   158  	nodeF.FindOrInsert("K")
   159  	res := tree.PreTraversal()
   160  	if !reflect.DeepEqual(res, []interface{}{
   161  		"A", "C", "G", "D", "H", "B", "E", "F", "K",
   162  	}) {
   163  		t.Errorf("Unexpected tree pre-traversal result: %+v", res)
   164  	}
   165  }