ginger/graph/graph_test.go
Brian Picciano c4dd673bf4 Refactor Graph internals to deduplicate OpenEdges
This change required OpenEdges to be passed around as pointers, which in
turn required me to audit how value copying is being done everywhere and
simplify it in a few places. I think I covered all the bases.

The new internals of Graph allow the graph's actual structure to be
reflected within the graph itself. For example, if the OpenEdges of two
ValueIns are equivalent then the same OpenEdge pointer is shared for the
two internally. This applies recursively, so if two OpenEdge tuples
share an inner OpenEdge, they will share the same pointer.

This change is a preliminary requirement for creating a graph mapping
operation. Without OpenEdge deduplication the map operation would end up
operating on the same OpenEdge multiple times, which would be incorrect.
2021-12-29 13:57:14 -07:00

116 lines
2.7 KiB
Go

package graph
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
type S string
func (s S) Equal(s2 Value) bool { return s == s2.(S) }
func (s S) String() string { return string(s) }
func TestEqual(t *testing.T) {
var (
zeroValue S
zeroGraph = new(Graph[S])
)
tests := []struct {
a, b *Graph[S]
exp bool
}{
{
a: zeroGraph,
b: zeroGraph,
exp: true,
},
{
a: zeroGraph,
b: zeroGraph.AddValueIn(ValueOut[S]("in", "incr"), "out"),
exp: false,
},
{
a: zeroGraph.AddValueIn(ValueOut[S]("in", "incr"), "out"),
b: zeroGraph.AddValueIn(ValueOut[S]("in", "incr"), "out"),
exp: true,
},
{
a: zeroGraph.AddValueIn(ValueOut[S]("in", "incr"), "out"),
b: zeroGraph.AddValueIn(TupleOut[S]([]*OpenEdge[S]{
ValueOut[S]("in", "ident"),
ValueOut[S]("1", "ident"),
}, "add"), "out"),
exp: false,
},
{
// tuples are different order
a: zeroGraph.AddValueIn(TupleOut[S]([]*OpenEdge[S]{
ValueOut[S]("1", "ident"),
ValueOut[S]("in", "ident"),
}, "add"), "out"),
b: zeroGraph.AddValueIn(TupleOut[S]([]*OpenEdge[S]{
ValueOut[S]("in", "ident"),
ValueOut[S]("1", "ident"),
}, "add"), "out"),
exp: false,
},
{
// tuple with no edge value and just a single input edge should be
// equivalent to just that edge.
a: zeroGraph.AddValueIn(TupleOut[S]([]*OpenEdge[S]{
ValueOut[S]("1", "ident"),
}, zeroValue), "out"),
b: zeroGraph.AddValueIn(ValueOut[S]("1", "ident"), "out"),
exp: true,
},
{
// tuple with an edge value and just a single input edge that has no
// edgeVal should be equivalent to just that edge with the tuple's
// edge value.
a: zeroGraph.AddValueIn(TupleOut[S]([]*OpenEdge[S]{
ValueOut[S]("1", zeroValue),
}, "ident"), "out"),
b: zeroGraph.AddValueIn(ValueOut[S]("1", "ident"), "out"),
exp: true,
},
{
a: zeroGraph.
AddValueIn(ValueOut[S]("in", "incr"), "out").
AddValueIn(ValueOut[S]("in2", "incr2"), "out2"),
b: zeroGraph.
AddValueIn(ValueOut[S]("in", "incr"), "out"),
exp: false,
},
{
a: zeroGraph.
AddValueIn(ValueOut[S]("in", "incr"), "out").
AddValueIn(ValueOut[S]("in2", "incr2"), "out2"),
b: zeroGraph.
AddValueIn(ValueOut[S]("in", "incr"), "out").
AddValueIn(ValueOut[S]("in2", "incr2"), "out2"),
exp: true,
},
{
// order of value ins shouldn't matter
a: zeroGraph.
AddValueIn(ValueOut[S]("in", "incr"), "out").
AddValueIn(ValueOut[S]("in2", "incr2"), "out2"),
b: zeroGraph.
AddValueIn(ValueOut[S]("in2", "incr2"), "out2").
AddValueIn(ValueOut[S]("in", "incr"), "out"),
exp: true,
},
}
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
assert.Equal(t, test.exp, test.a.Equal(test.b))
})
}
}