Skip to content

Commit

Permalink
Test if edges are retrieved completely, including their attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikbraun committed Mar 1, 2023
1 parent 47a8272 commit 8c01d5d
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 37 deletions.
90 changes: 73 additions & 17 deletions directed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,26 +338,43 @@ func TestDirected_AddEdge(t *testing.T) {
func TestDirected_Edge(t *testing.T) {
tests := map[string]struct {
vertices []int
edges []Edge[int]
edge Edge[int]
args [2]int
expectedError error
}{
"get edge of directed graph": {
"get edge of undirected graph": {
vertices: []int{1, 2, 3},
edges: []Edge[int]{
{Source: 1, Target: 2},
{Source: 2, Target: 3},
},
args: [2]int{2, 3},
edge: Edge[int]{Source: 1, Target: 2},
args: [2]int{1, 2},
},
"get non-existent edge of directed graph": {
vertices: []int{1, 2, 3},
edges: []Edge[int]{
{Source: 1, Target: 2},
},
"get edge of undirected graph with swapped source and target": {
vertices: []int{1, 2, 3},
edge: Edge[int]{Source: 1, Target: 2},
args: [2]int{2, 1},
expectedError: ErrEdgeNotFound,
},
"get non-existent edge of undirected graph": {
vertices: []int{1, 2, 3},
edge: Edge[int]{Source: 1, Target: 2},
args: [2]int{2, 3},
expectedError: ErrEdgeNotFound,
},
"get edge with properties": {
vertices: []int{1, 2, 3},
edge: Edge[int]{
Source: 1,
Target: 2,
Properties: EdgeProperties{
// Attributes can't be tested at the moment, because there is no way to add
// multiple attributes at once (using a functional option like EdgeAttributes).
// ToDo: Add Attributes once an EdgeAttributes functional option exists.
Attributes: map[string]string{},
Weight: 10,
Data: "this is an edge",
},
},
args: [2]int{1, 2},
},
}

for name, test := range tests {
Expand All @@ -367,17 +384,31 @@ func TestDirected_Edge(t *testing.T) {
_ = graph.AddVertex(vertex)
}

for _, edge := range test.edges {
if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil {
t.Fatalf("%s: failed to add edge: %s", name, err.Error())
}
if err := graph.AddEdge(test.edge.Source, test.edge.Target, EdgeWeight(test.edge.Properties.Weight), EdgeData(test.edge.Properties.Data)); err != nil {
t.Fatalf("%s: failed to add edge: %s", name, err.Error())
}

_, err := graph.Edge(test.args[0], test.args[1])
edge, err := graph.Edge(test.args[0], test.args[1])

if !errors.Is(err, test.expectedError) {
t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v", name, test.expectedError, err)
}

if test.expectedError != nil {
continue
}

if edge.Source != test.args[0] {
t.Errorf("%s: source expectancy doesn't match: expected %v, got %v", name, test.args[0], edge.Source)
}

if edge.Target != test.args[1] {
t.Errorf("%s: target expectancy doesn't match: expected %v, got %v", name, test.args[1], edge.Target)
}

if !edgePropertiesAreEqual(edge.Properties, test.edge.Properties) {
t.Errorf("%s: edge property expectancy doesn't match: expected %v, got %v", name, test.edge.Properties, edge.Properties)
}
}
}

Expand Down Expand Up @@ -888,6 +919,31 @@ func slicesAreEqual[T comparable](a, b []T) bool {
return true
}

func edgePropertiesAreEqual(a, b EdgeProperties) bool {
if a.Weight != b.Weight {
return false
}

if a.Data != b.Data {
return false
}

// A length check is required because in the iteration below, a.Attributes could be empty and
// thus circumvent the comparison.
if len(a.Attributes) != len(b.Attributes) {
return false
}

for key, aValue := range a.Attributes {
bValue, ok := b.Attributes[key]
if !ok || aValue != bValue {
return false
}
}

return true
}

func predecessors[K comparable, T any](g *directed[K, T], vertexHash K) ([]K, error) {
var predecessorHashes []K

Expand Down
62 changes: 42 additions & 20 deletions undirected_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,34 +333,42 @@ func TestUndirected_AddEdge(t *testing.T) {
func TestUndirected_Edge(t *testing.T) {
tests := map[string]struct {
vertices []int
edges []Edge[int]
edge Edge[int]
args [2]int
expectedError error
}{
"get edge of undirected graph": {
vertices: []int{1, 2, 3},
edges: []Edge[int]{
{Source: 1, Target: 2},
{Source: 2, Target: 3},
},
args: [2]int{2, 3},
edge: Edge[int]{Source: 1, Target: 2},
args: [2]int{1, 2},
},
"get edge of undirected graph with swapped source and target": {
vertices: []int{1, 2, 3},
edges: []Edge[int]{
{Source: 1, Target: 2},
{Source: 2, Target: 3},
},
args: [2]int{3, 2},
edge: Edge[int]{Source: 1, Target: 2},
args: [2]int{2, 1},
},
"get non-existent edge of undirected graph": {
vertices: []int{1, 2, 3},
edges: []Edge[int]{
{Source: 1, Target: 2},
},
vertices: []int{1, 2, 3},
edge: Edge[int]{Source: 1, Target: 2},
args: [2]int{2, 3},
expectedError: ErrEdgeNotFound,
},
"get edge with properties": {
vertices: []int{1, 2, 3},
edge: Edge[int]{
Source: 1,
Target: 2,
Properties: EdgeProperties{
// Attributes can't be tested at the moment, because there is no way to add
// multiple attributes at once (using a functional option like EdgeAttributes).
// ToDo: Add Attributes once an EdgeAttributes functional option exists.
Attributes: map[string]string{},
Weight: 10,
Data: "this is an edge",
},
},
args: [2]int{1, 2},
},
}

for name, test := range tests {
Expand All @@ -370,17 +378,31 @@ func TestUndirected_Edge(t *testing.T) {
_ = graph.AddVertex(vertex)
}

for _, edge := range test.edges {
if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil {
t.Fatalf("%s: failed to add edge: %s", name, err.Error())
}
if err := graph.AddEdge(test.edge.Source, test.edge.Target, EdgeWeight(test.edge.Properties.Weight), EdgeData(test.edge.Properties.Data)); err != nil {
t.Fatalf("%s: failed to add edge: %s", name, err.Error())
}

_, err := graph.Edge(test.args[0], test.args[1])
edge, err := graph.Edge(test.args[0], test.args[1])

if !errors.Is(err, test.expectedError) {
t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v", name, test.expectedError, err)
}

if test.expectedError != nil {
continue
}

if edge.Source != test.args[0] {
t.Errorf("%s: source expectancy doesn't match: expected %v, got %v", name, test.args[0], edge.Source)
}

if edge.Target != test.args[1] {
t.Errorf("%s: target expectancy doesn't match: expected %v, got %v", name, test.args[1], edge.Target)
}

if !edgePropertiesAreEqual(edge.Properties, test.edge.Properties) {
t.Errorf("%s: edge property expectancy doesn't match: expected %v, got %v", name, test.edge.Properties, edge.Properties)
}
}
}

Expand Down

0 comments on commit 8c01d5d

Please sign in to comment.