github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  type hashVertex struct {
   128  	code interface{}
   129  }
   130  
   131  func (v *hashVertex) Hashcode() interface{} {
   132  	return v.code
   133  }
   134  
   135  func (v *hashVertex) Name() string {
   136  	return fmt.Sprintf("%#v", v.code)
   137  }
   138  
   139  const testGraphBasicStr = `
   140  1
   141    3
   142  2
   143  3
   144  `
   145  
   146  const testGraphEmptyStr = `
   147  1
   148  2
   149  3
   150  `
   151  
   152  const testGraphRemoveStr = `
   153  1
   154  2
   155  `
   156  
   157  const testGraphReplaceStr = `
   158  1
   159    42
   160  3
   161  42
   162    3
   163  `
   164  
   165  const testGraphReplaceSelfStr = `
   166  1
   167    2
   168  2
   169    3
   170  3
   171  `