MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / TestDirectedGraph

Function TestDirectedGraph

graph/graph_test.go:58–102  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

56}
57
58func TestDirectedGraph(t *testing.T) {
59
60 // Testing self-loops separately only for directed graphs.
61 // For undirected graphs each edge already creates a self-loop.
62 directedGraphTestCases := append(graphTestCases, struct {
63 name string
64 edges [][]int
65 vertices int
66 }{
67 "self-loops",
68 [][]int{
69 {0, 1, 1},
70 {1, 2, 2},
71 {2, 1, 3},
72 },
73 3,
74 })
75
76 for _, test := range directedGraphTestCases {
77 t.Run(fmt.Sprint(test.name), func(t *testing.T) {
78 // Initializing graph, adding edges
79 graph := New(test.vertices)
80 graph.Directed = true
81 for _, edge := range test.edges {
82 graph.AddWeightedEdge(edge[0], edge[1], edge[2])
83 }
84
85 if graph.vertices != test.vertices {
86 t.Errorf("Number of vertices, Expected: %d, Computed: %d", test.vertices, graph.vertices)
87 }
88 edgeCount := 0
89 for _, e := range graph.edges {
90 edgeCount += len(e)
91 }
92 if edgeCount != len(test.edges) {
93 t.Errorf("Number of edges, Expected: %d, Computed: %d", len(test.edges), edgeCount)
94 }
95 for _, edge := range test.edges {
96 if val, found := graph.edges[edge[0]][edge[1]]; !found || val != edge[2] {
97 t.Errorf("Edge {%d->%d (%d)} not found", edge[0], edge[1], edge[2])
98 }
99 }
100 })
101 }
102}
103
104func TestUndirectedGraph(t *testing.T) {
105

Callers

nothing calls this directly

Calls 2

AddWeightedEdgeMethod · 0.80
NewFunction · 0.70

Tested by

no test coverage detected