github.com/jrasell/terraform@v0.6.17-0.20160523115548-2652f5232949/digraph/util_test.go (about)

     1  package digraph
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestDepthFirstWalk(t *testing.T) {
     9  	nodes := ParseBasic(`a -> b
    10  a -> c
    11  a -> d
    12  b -> e
    13  d -> f
    14  e -> a ; cycle`)
    15  	root := nodes["a"]
    16  	expected := []string{
    17  		"a",
    18  		"b",
    19  		"e",
    20  		"c",
    21  		"d",
    22  		"f",
    23  	}
    24  	index := 0
    25  	DepthFirstWalk(root, func(n Node) bool {
    26  		name := n.(*BasicNode).Name
    27  		if expected[index] != name {
    28  			t.Fatalf("expected: %v, got %v", expected[index], name)
    29  		}
    30  		index++
    31  		return true
    32  	})
    33  }
    34  
    35  func TestInDegree(t *testing.T) {
    36  	nodes := ParseBasic(`a -> b
    37  a -> c
    38  a -> d
    39  b -> e
    40  c -> e
    41  d -> f`)
    42  	var nlist []Node
    43  	for _, n := range nodes {
    44  		nlist = append(nlist, n)
    45  	}
    46  
    47  	expected := map[string]int{
    48  		"a": 0,
    49  		"b": 1,
    50  		"c": 1,
    51  		"d": 1,
    52  		"e": 2,
    53  		"f": 1,
    54  	}
    55  	indegree := InDegree(nlist)
    56  	for n, d := range indegree {
    57  		name := n.(*BasicNode).Name
    58  		exp := expected[name]
    59  		if exp != d {
    60  			t.Fatalf("Expected %d for %s, got %d",
    61  				exp, name, d)
    62  		}
    63  	}
    64  }
    65  
    66  func TestOutDegree(t *testing.T) {
    67  	nodes := ParseBasic(`a -> b
    68  a -> c
    69  a -> d
    70  b -> e
    71  c -> e
    72  d -> f`)
    73  	var nlist []Node
    74  	for _, n := range nodes {
    75  		nlist = append(nlist, n)
    76  	}
    77  
    78  	expected := map[string]int{
    79  		"a": 3,
    80  		"b": 1,
    81  		"c": 1,
    82  		"d": 1,
    83  		"e": 0,
    84  		"f": 0,
    85  	}
    86  	outDegree := OutDegree(nlist)
    87  	for n, d := range outDegree {
    88  		name := n.(*BasicNode).Name
    89  		exp := expected[name]
    90  		if exp != d {
    91  			t.Fatalf("Expected %d for %s, got %d",
    92  				exp, name, d)
    93  		}
    94  	}
    95  }
    96  
    97  func TestSinks(t *testing.T) {
    98  	nodes := ParseBasic(`a -> b
    99  a -> c
   100  a -> d
   101  b -> e
   102  c -> e
   103  d -> f`)
   104  	var nlist []Node
   105  	for _, n := range nodes {
   106  		nlist = append(nlist, n)
   107  	}
   108  
   109  	sinks := Sinks(nlist)
   110  
   111  	var haveE, haveF bool
   112  	for _, n := range sinks {
   113  		name := n.(*BasicNode).Name
   114  		switch name {
   115  		case "e":
   116  			haveE = true
   117  		case "f":
   118  			haveF = true
   119  		}
   120  	}
   121  	if !haveE || !haveF {
   122  		t.Fatalf("missing sink")
   123  	}
   124  }
   125  
   126  func TestSources(t *testing.T) {
   127  	nodes := ParseBasic(`a -> b
   128  a -> c
   129  a -> d
   130  b -> e
   131  c -> e
   132  d -> f
   133  x -> y`)
   134  	var nlist []Node
   135  	for _, n := range nodes {
   136  		nlist = append(nlist, n)
   137  	}
   138  
   139  	sources := Sources(nlist)
   140  	if len(sources) != 2 {
   141  		t.Fatalf("bad: %v", sources)
   142  	}
   143  
   144  	var haveA, haveX bool
   145  	for _, n := range sources {
   146  		name := n.(*BasicNode).Name
   147  		switch name {
   148  		case "a":
   149  			haveA = true
   150  		case "x":
   151  			haveX = true
   152  		}
   153  	}
   154  	if !haveA || !haveX {
   155  		t.Fatalf("missing source %v %v", haveA, haveX)
   156  	}
   157  }
   158  
   159  func TestUnreachable(t *testing.T) {
   160  	nodes := ParseBasic(`a -> b
   161  a -> c
   162  a -> d
   163  b -> e
   164  c -> e
   165  d -> f
   166  f -> a
   167  x -> y
   168  y -> z`)
   169  	var nlist []Node
   170  	for _, n := range nodes {
   171  		nlist = append(nlist, n)
   172  	}
   173  
   174  	unreached := Unreachable(nodes["a"], nlist)
   175  	if len(unreached) != 3 {
   176  		t.Fatalf("bad: %v", unreached)
   177  	}
   178  
   179  	var haveX, haveY, haveZ bool
   180  	for _, n := range unreached {
   181  		name := n.(*BasicNode).Name
   182  		switch name {
   183  		case "x":
   184  			haveX = true
   185  		case "y":
   186  			haveY = true
   187  		case "z":
   188  			haveZ = true
   189  		}
   190  	}
   191  	if !haveX || !haveY || !haveZ {
   192  		t.Fatalf("missing %v %v %v", haveX, haveY, haveZ)
   193  	}
   194  }
   195  
   196  func TestUnreachable2(t *testing.T) {
   197  	nodes := ParseBasic(`a -> b
   198  a -> c
   199  a -> d
   200  b -> e
   201  c -> e
   202  d -> f
   203  f -> a
   204  x -> y
   205  y -> z`)
   206  	var nlist []Node
   207  	for _, n := range nodes {
   208  		nlist = append(nlist, n)
   209  	}
   210  
   211  	unreached := Unreachable(nodes["x"], nlist)
   212  	if len(unreached) != 6 {
   213  		t.Fatalf("bad: %v", unreached)
   214  	}
   215  
   216  	expected := map[string]struct{}{
   217  		"a": struct{}{},
   218  		"b": struct{}{},
   219  		"c": struct{}{},
   220  		"d": struct{}{},
   221  		"e": struct{}{},
   222  		"f": struct{}{},
   223  	}
   224  	out := map[string]struct{}{}
   225  	for _, n := range unreached {
   226  		name := n.(*BasicNode).Name
   227  		out[name] = struct{}{}
   228  	}
   229  
   230  	if !reflect.DeepEqual(out, expected) {
   231  		t.Fatalf("bad: %v %v", out, expected)
   232  	}
   233  }