123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package toposort
- import "testing"
- func index(s []string, v string) int {
- for i, s := range s {
- if s == v {
- return i
- }
- }
- return -1
- }
- type Edge struct {
- From string
- To string
- }
- func TestDuplicatedNode(t *testing.T) {
- graph := NewGraph()
- graph.AddNode("a")
- if graph.AddNode("a") {
- t.Error("not raising duplicated node error")
- }
- }
- func TestRemoveNotExistEdge(t *testing.T) {
- graph := NewGraph()
- if graph.RemoveEdge("a", "b") {
- t.Error("not raising not exist edge error")
- }
- }
- func TestWikipedia(t *testing.T) {
- graph := NewGraph()
- graph.AddNodes("2", "3", "5", "7", "8", "9", "10", "11")
- edges := []Edge{
- {"7", "8"},
- {"7", "11"},
- {"5", "11"},
- {"3", "8"},
- {"3", "10"},
- {"11", "2"},
- {"11", "9"},
- {"11", "10"},
- {"8", "9"},
- }
- for _, e := range edges {
- graph.AddEdge(e.From, e.To)
- }
- result, ok := graph.Toposort()
- if !ok {
- t.Error("closed path detected in no closed pathed graph")
- }
- for _, e := range edges {
- if i, j := index(result, e.From), index(result, e.To); i > j {
- t.Errorf("dependency failed: not satisfy %v(%v) > %v(%v)", e.From, i, e.To, j)
- }
- }
- }
- func TestCycle(t *testing.T) {
- graph := NewGraph()
- graph.AddNodes("1", "2", "3")
- graph.AddEdge("1", "2")
- graph.AddEdge("2", "3")
- graph.AddEdge("3", "1")
- _, ok := graph.Toposort()
- if ok {
- t.Error("closed path not detected in closed pathed graph")
- }
- }
|