Finished go/pov

This commit is contained in:
2017-08-24 12:10:01 -05:00
parent 862e37f5fb
commit 169f72ca40
11 changed files with 1117 additions and 22 deletions

View File

@@ -2,12 +2,7 @@ package pov
const testVersion = 2
// A Graph is just a collection of Nodes
type Node struct {
label string
connections []Node
}
// A Graph just holds all nodes in the graph
type Graph struct {
nodes []Node
}
@@ -16,6 +11,12 @@ func New() *Graph {
return new(Graph)
}
// A Node is a label and a slice of references to other nodes
type Node struct {
label string
connections []Node
}
func (g *Graph) AddNode(lbl string) {
if g.getNode(lbl) == nil {
g.nodes = append(g.nodes, Node{label: lbl})
@@ -72,6 +73,7 @@ func (g *Graph) getPath(fr, to string) []string {
}
tst := g.getPath(st.connections[i].label, to)
if len(tst) > 0 {
tst = append([]string{fr}, tst...)
valPaths = append(valPaths, tst)
}
}
@@ -101,15 +103,14 @@ func (g *Graph) ChangeRoot(oldRoot, newRoot string) *Graph {
if n == nil {
return g
}
n = n.copy()
p := g.getNode(pth[0])
if p == nil {
return g
}
p = p.copy()
p.removeConnection(n.label)
n.connections = append(n.connections, *p)
oldRoot = n.label
pth = g.getPath(oldRoot, newRoot)
}
}
return g
@@ -124,13 +125,3 @@ func (n *Node) removeConnection(to string) {
}
}
}
// Node.copy() returns a new node that is a copy of this node
func (n *Node) copy() *Node {
ret := Node{label: n.label}
for _, k := range n.connections {
cp := k.copy()
ret.connections = append(ret.connections, *cp)
}
return &ret
}

View File

@@ -1,7 +1,6 @@
package pov
import (
"fmt"
"reflect"
"sort"
"testing"
@@ -267,13 +266,10 @@ func TestTestVersion(t *testing.T) {
}
func TestConstruction(t *testing.T) {
fmt.Println("TestConstruction")
for _, tc := range testCases {
fmt.Println("Starting test:", tc.description)
got := tc.graph().ArcList()
want := tc.arcStrings
tc.testResult(got, want, "incorrect graph construction", t)
fmt.Println(" Test Passed:", tc.description)
}
}