Skip to content

Commit

Permalink
add tests, basic outline
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanbza committed Jan 20, 2020
1 parent b8b6e71 commit 3a05161
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 2 deletions.
79 changes: 77 additions & 2 deletions graph.go
Expand Up @@ -11,8 +11,6 @@ import (
type Vertex struct {
Label string
SizeBytes int64
// All vertices recursively dominated by this vertex.
dominatees []*Vertex
}

type edge struct {
Expand Down Expand Up @@ -266,3 +264,80 @@ func negativeComplementVertices(a, b []*Vertex) []string {
}
return out
}

type treeNode struct {
v *Vertex
children []*treeNode
}

// TODO https://www.cs.au.dk/~gerth/advising/thesis/henrik-knakkegaard-christensen.pdf
func (g *graph) buildDominatorTree() (root *treeNode) {

return nil
}

// Used for tests.
func (a *treeNode) Equal(b *treeNode) bool {
if a == nil && b == nil {
return true
}

if (a == nil && b != nil) || (a != nil && b == nil) {
return false
}

if (a.v == nil && b.v != nil) || (a.v != nil && b.v == nil) {
return false
}

if a.v == nil && b.v == nil {
return true
}

if a.v.Label != b.v.Label {
return false
}

acs := make(map[string]struct{})
for _, c := range a.children {
acs[c.v.Label] = struct{}{}
}

bcs := make(map[string]*treeNode)
for _, c := range b.children {
bcs[c.v.Label] = c
}

if len(acs) != len(bcs) {
return false
}

for c := range acs {
if _, ok := bcs[c]; !ok {
return false
}

if !a.Equal(bcs[c]) {
return false
}
}

return true
}

// Used for tests.
func (tn *treeNode) String() string {
if len(tn.children) == 0 {
return tn.v.Label
}

out := fmt.Sprintf("%s { ", tn.v.Label)
for i, c := range tn.children {
if i > 0 {
out += " "
}
out += c.String()
}
out += " }"
return out
}
106 changes: 106 additions & 0 deletions graph_test.go
@@ -0,0 +1,106 @@
package main

import "testing"

import "bytes"

func TestBuildDominatorTree(t *testing.T) {
for _, tc := range []struct {
name string
in string
want *treeNode
}{
{
name: "One child",
in: "A B",
want: &treeNode{v: &Vertex{Label: "A"}, children: []*treeNode{
{v: &Vertex{Label: "B"}},
}},
},
{
name: "Two children",
in: "A B\nA C",
want: &treeNode{v: &Vertex{Label: "A"}, children: []*treeNode{
{v: &Vertex{Label: "B"}},
{v: &Vertex{Label: "C"}},
}},
},
{
name: "Chain",
in: "A B\nB C",
want: &treeNode{v: &Vertex{Label: "A"}, children: []*treeNode{
{v: &Vertex{Label: "B"}, children: []*treeNode{
{v: &Vertex{Label: "C"}},
}},
}},
},
{
name: "Chain two",
in: "A B\nB C\nB D",
want: &treeNode{v: &Vertex{Label: "A"}, children: []*treeNode{
{v: &Vertex{Label: "B"}, children: []*treeNode{
{v: &Vertex{Label: "C"}},
{v: &Vertex{Label: "D"}},
}},
}},
},
{
name: "Recursion",
in: "A B\nB A",
want: &treeNode{v: &Vertex{Label: "A"}, children: []*treeNode{
{v: &Vertex{Label: "B"}},
}},
},
{
name: "Dominate",
in: "A B\nA C\nB C",
want: &treeNode{v: &Vertex{Label: "A"}, children: []*treeNode{
{v: &Vertex{Label: "B"}},
{v: &Vertex{Label: "C"}},
}},
},
{
name: "Dominate one out",
in: "A B\nB C\nB D\nC D",
want: &treeNode{v: &Vertex{Label: "A"}, children: []*treeNode{
{v: &Vertex{Label: "B"}, children: []*treeNode{
{v: &Vertex{Label: "C"}},
{v: &Vertex{Label: "D"}},
}},
}},
},
// https://www.cs.au.dk/~gerth/advising/thesis/henrik-knakkegaard-christensen.pdf
{
name: "Complex",
in: "C D\nC G\nD H\nD A\nA B\nA F\nG E\nG I\nH J\nJ I\nJ K\nI E\nK C",
want: &treeNode{v: &Vertex{Label: "C"}, children: []*treeNode{
{v: &Vertex{Label: "D"}, children: []*treeNode{
{v: &Vertex{Label: "H"}},
{v: &Vertex{Label: "A"}, children: []*treeNode{
{v: &Vertex{Label: "B"}},
{v: &Vertex{Label: "F"}},
}},
}},
{v: &Vertex{Label: "G"}, children: []*treeNode{
{v: &Vertex{Label: "E"}},
{v: &Vertex{Label: "I"}},
}},
{v: &Vertex{Label: "J"}, children: []*treeNode{
{v: &Vertex{Label: "K"}},
}},
}},
},
} {
t.Run(tc.name, func(t *testing.T) {
b := bytes.NewBufferString(tc.in)
g, err := newGraph(b)
if err != nil {
t.Fatal(err)
}
got := g.buildDominatorTree()
if !got.Equal(tc.want) {
t.Fatalf("got %s, want %s", got, tc.want)
}
})
}
}

0 comments on commit 3a05161

Please sign in to comment.