topWalk tests

This commit is contained in:
Josh Deprez 2021-09-27 11:04:45 +10:00
parent 239d5c88fd
commit 86e435e5f8

View file

@ -81,3 +81,108 @@ func TestDAGAddVertex(t *testing.T) {
t.Errorf("diff after adding vertex u a second time:\n%s", diff)
}
}
func TestDAGRemoveVertex(t *testing.T) {
u := fakeDrawBoxer("u")
v := fakeDrawBoxer("v")
w := fakeDrawBoxer("w")
d := dag{
u: edges{
in: drawerSet{},
out: drawerSet{v: {}},
},
v: edges{
in: drawerSet{u: {}},
out: drawerSet{w: {}},
},
w: edges{
in: drawerSet{v: {}},
out: drawerSet{},
},
}
d.removeVertex(u)
want := dag{
v: edges{
in: drawerSet{},
out: drawerSet{w: {}},
},
w: edges{
in: drawerSet{v: {}},
out: drawerSet{},
},
}
if diff := cmp.Diff(d, want, cmp.AllowUnexported(edges{})); diff != "" {
t.Errorf("diff after removing vertex u:\n%s", diff)
}
d.removeVertex(u)
if diff := cmp.Diff(d, want, cmp.AllowUnexported(edges{})); diff != "" {
t.Errorf("diff after removing vertex u a second time:\n%s", diff)
}
d.removeVertex(w)
want = dag{
v: edges{
in: drawerSet{},
out: drawerSet{},
},
}
if diff := cmp.Diff(d, want, cmp.AllowUnexported(edges{})); diff != "" {
t.Errorf("diff after removing vertex v:\n%s", diff)
}
}
func TestTopWalk(t *testing.T) {
u := fakeDrawBoxer("u")
v := fakeDrawBoxer("v")
w := fakeDrawBoxer("w")
d := dag{
u: edges{
in: drawerSet{},
out: drawerSet{v: {}},
},
v: edges{
in: drawerSet{u: {}},
out: drawerSet{w: {}},
},
w: edges{
in: drawerSet{v: {}},
out: drawerSet{},
},
}
var got []Drawer
d.topWalk(func(x Drawer) {
got = append(got, x)
})
want := []Drawer{u, v, w}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("topWalk visited in wrong order - diff:\n%s", diff)
}
}
func TestTopWalkToleratesCycle(t *testing.T) {
u := fakeDrawBoxer("u")
v := fakeDrawBoxer("v")
w := fakeDrawBoxer("w")
d := dag{
u: edges{
in: drawerSet{w: {}},
out: drawerSet{v: {}},
},
v: edges{
in: drawerSet{u: {}},
out: drawerSet{w: {}},
},
w: edges{
in: drawerSet{v: {}},
out: drawerSet{u: {}},
},
}
got := make(map[Drawer]int)
d.topWalk(func(x Drawer) {
got[x]++
})
want := map[Drawer]int{u: 1, v: 1, w: 1}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("topWalk visited vertices wrong number of times - diff:\n%s", diff)
}
}