-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_test.go
54 lines (43 loc) · 1.12 KB
/
graph_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package datastruct
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewGraph(t *testing.T) {
verties := []*Vertex{
{Name: "A", Value: 1},
{Name: "B", Value: 2},
}
edges := []*Edge{
{Cost: 1.0, From: "A", To: "B"},
}
graph, err := NewGraph(verties, edges)
assert.NoError(t, err)
assert.Equal(t, 2, len(graph.verties))
assert.Equal(t, 1, len(graph.edges))
}
func TestAddVertext(t *testing.T) {
graph, _ := NewGraph([]*Vertex{}, []*Edge{})
vertex := &Vertex{Name: "A", Value: 1}
err := graph.AddVertext(vertex)
assert.NoError(t, err)
assert.Equal(t, 1, len(graph.verties))
// Test adding duplicate vertex
err = graph.AddVertext(vertex)
assert.Error(t, err)
}
func TestAddEdge(t *testing.T) {
verties := []*Vertex{
{Name: "A", Value: 1},
{Name: "B", Value: 2},
}
graph, _ := NewGraph(verties, []*Edge{})
edge := &Edge{Cost: 1.0, From: "A", To: "B"}
err := graph.AddEdge(edge)
assert.NoError(t, err)
assert.Equal(t, 1, len(graph.edges))
// Test adding edge with invalid vertex
invalidEdge := &Edge{Cost: 1.0, From: "A", To: "C"}
err = graph.AddEdge(invalidEdge)
assert.Error(t, err)
}