From 725da49ab7cb97c3f8a671da797327ce8f120e5e Mon Sep 17 00:00:00 2001 From: Jack Hu Date: Sat, 13 Nov 2021 17:46:53 -0500 Subject: [PATCH] test: Add test for vertices and edge count in pgraph --- pgraph/pgraph_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/pgraph/pgraph_test.go b/pgraph/pgraph_test.go index 709b753bf8..03b188c785 100644 --- a/pgraph/pgraph_test.go +++ b/pgraph/pgraph_test.go @@ -15,12 +15,14 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +//go:build !root // +build !root package pgraph import ( "reflect" + "strconv" "testing" ) @@ -49,6 +51,49 @@ func TestCount1(t *testing.T) { } } +func TestCountNodesAndEdges(t *testing.T) { + + G := &Graph{Name: "MyG"} + + if i := G.NumVertices(); i != 0 { + t.Errorf("should have 0 vertices instead of: %d", i) + } + + if i := G.NumEdges(); i != 0 { + t.Errorf("should have 0 edges instead of: %d", i) + } + + // add vertices + var vertices [6]Vertex + for i := 0; i < len(vertices); i++ { + var name string = "v" + strconv.Itoa(i+1) + vertices[i] = NV(name) + } + + // add edges + var edges [5]Edge + for x := 0; x < len(edges); x++ { + var name string = "e" + strconv.Itoa(x+1) + edges[x] = NE(name) + } + + // add edges and vertices to graph + for a := 0; a < len(vertices); a++ { + var b int = a + 1 + if a <= len(vertices)-1 { + G.AddVertex(vertices[a], vertices[b], edges[a]) + } + } + + if y := G.NumVertices(); y != 6 { + t.Errorf("should have 6 vertices instead of: %d", y) + } + + if w := G.NumEdges(); w != 5 { + t.Errorf("should have 5 edges instead of: %d", w) + } +} + func TestAddVertex1(t *testing.T) { G := &Graph{Name: "g2"} v1 := NV("v1")