github.com/magodo/terraform@v0.11.12-beta1/dag/graph_test.go (about)

     1  package dag
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestGraph_empty(t *testing.T) {
    10  	var g Graph
    11  	g.Add(1)
    12  	g.Add(2)
    13  	g.Add(3)
    14  
    15  	actual := strings.TrimSpace(g.String())
    16  	expected := strings.TrimSpace(testGraphEmptyStr)
    17  	if actual != expected {
    18  		t.Fatalf("bad: %s", actual)
    19  	}
    20  }
    21  
    22  func TestGraph_basic(t *testing.T) {
    23  	var g Graph
    24  	g.Add(1)
    25  	g.Add(2)
    26  	g.Add(3)
    27  	g.Connect(BasicEdge(1, 3))
    28  
    29  	actual := strings.TrimSpace(g.String())
    30  	expected := strings.TrimSpace(testGraphBasicStr)
    31  	if actual != expected {
    32  		t.Fatalf("bad: %s", actual)
    33  	}
    34  }
    35  
    36  func TestGraph_remove(t *testing.T) {
    37  	var g Graph
    38  	g.Add(1)
    39  	g.Add(2)
    40  	g.Add(3)
    41  	g.Connect(BasicEdge(1, 3))
    42  	g.Remove(3)
    43  
    44  	actual := strings.TrimSpace(g.String())
    45  	expected := strings.TrimSpace(testGraphRemoveStr)
    46  	if actual != expected {
    47  		t.Fatalf("bad: %s", actual)
    48  	}
    49  }
    50  
    51  func TestGraph_replace(t *testing.T) {
    52  	var g Graph
    53  	g.Add(1)
    54  	g.Add(2)
    55  	g.Add(3)
    56  	g.Connect(BasicEdge(1, 2))
    57  	g.Connect(BasicEdge(2, 3))
    58  	g.Replace(2, 42)
    59  
    60  	actual := strings.TrimSpace(g.String())
    61  	expected := strings.TrimSpace(testGraphReplaceStr)
    62  	if actual != expected {
    63  		t.Fatalf("bad: %s", actual)
    64  	}
    65  }
    66  
    67  func TestGraph_replaceSelf(t *testing.T) {
    68  	var g Graph
    69  	g.Add(1)
    70  	g.Add(2)
    71  	g.Add(3)
    72  	g.Connect(BasicEdge(1, 2))
    73  	g.Connect(BasicEdge(2, 3))
    74  	g.Replace(2, 2)
    75  
    76  	actual := strings.TrimSpace(g.String())
    77  	expected := strings.TrimSpace(testGraphReplaceSelfStr)
    78  	if actual != expected {
    79  		t.Fatalf("bad: %s", actual)
    80  	}
    81  }
    82  
    83  // This tests that connecting edges works based on custom Hashcode
    84  // implementations for uniqueness.
    85  func TestGraph_hashcode(t *testing.T) {
    86  	var g Graph
    87  	g.Add(&hashVertex{code: 1})
    88  	g.Add(&hashVertex{code: 2})
    89  	g.Add(&hashVertex{code: 3})
    90  	g.Connect(BasicEdge(
    91  		&hashVertex{code: 1},
    92  		&hashVertex{code: 3}))
    93  
    94  	actual := strings.TrimSpace(g.String())
    95  	expected := strings.TrimSpace(testGraphBasicStr)
    96  	if actual != expected {
    97  		t.Fatalf("bad: %s", actual)
    98  	}
    99  }
   100  
   101  func TestGraphHasVertex(t *testing.T) {
   102  	var g Graph
   103  	g.Add(1)
   104  
   105  	if !g.HasVertex(1) {
   106  		t.Fatal("should have 1")
   107  	}
   108  	if g.HasVertex(2) {
   109  		t.Fatal("should not have 2")
   110  	}
   111  }
   112  
   113  func TestGraphHasEdge(t *testing.T) {
   114  	var g Graph
   115  	g.Add(1)
   116  	g.Add(2)
   117  	g.Connect(BasicEdge(1, 2))
   118  
   119  	if !g.HasEdge(BasicEdge(1, 2)) {
   120  		t.Fatal("should have 1,2")
   121  	}
   122  	if g.HasVertex(BasicEdge(2, 3)) {
   123  		t.Fatal("should not have 2,3")
   124  	}
   125  }
   126  
   127  func TestGraphEdgesFrom(t *testing.T) {
   128  	var g Graph
   129  	g.Add(1)
   130  	g.Add(2)
   131  	g.Add(3)
   132  	g.Connect(BasicEdge(1, 3))
   133  	g.Connect(BasicEdge(2, 3))
   134  
   135  	edges := g.EdgesFrom(1)
   136  
   137  	var expected Set
   138  	expected.Add(BasicEdge(1, 3))
   139  
   140  	var s Set
   141  	for _, e := range edges {
   142  		s.Add(e)
   143  	}
   144  
   145  	if s.Intersection(&expected).Len() != expected.Len() {
   146  		t.Fatalf("bad: %#v", edges)
   147  	}
   148  }
   149  
   150  func TestGraphEdgesTo(t *testing.T) {
   151  	var g Graph
   152  	g.Add(1)
   153  	g.Add(2)
   154  	g.Add(3)
   155  	g.Connect(BasicEdge(1, 3))
   156  	g.Connect(BasicEdge(1, 2))
   157  
   158  	edges := g.EdgesTo(3)
   159  
   160  	var expected Set
   161  	expected.Add(BasicEdge(1, 3))
   162  
   163  	var s Set
   164  	for _, e := range edges {
   165  		s.Add(e)
   166  	}
   167  
   168  	if s.Intersection(&expected).Len() != expected.Len() {
   169  		t.Fatalf("bad: %#v", edges)
   170  	}
   171  }
   172  
   173  type hashVertex struct {
   174  	code interface{}
   175  }
   176  
   177  func (v *hashVertex) Hashcode() interface{} {
   178  	return v.code
   179  }
   180  
   181  func (v *hashVertex) Name() string {
   182  	return fmt.Sprintf("%#v", v.code)
   183  }
   184  
   185  const testGraphBasicStr = `
   186  1
   187    3
   188  2
   189  3
   190  `
   191  
   192  const testGraphEmptyStr = `
   193  1
   194  2
   195  3
   196  `
   197  
   198  const testGraphRemoveStr = `
   199  1
   200  2
   201  `
   202  
   203  const testGraphReplaceStr = `
   204  1
   205    42
   206  3
   207  42
   208    3
   209  `
   210  
   211  const testGraphReplaceSelfStr = `
   212  1
   213    2
   214  2
   215    3
   216  3
   217  `