Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add test for vertices and edge count in pgraph #682

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions pgraph/pgraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

//go:build !root
// +build !root

package pgraph

import (
"reflect"
"strconv"
"testing"
)

Expand Down Expand Up @@ -49,6 +51,54 @@ 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)
}

// populate vertices
var vertices [6]Vertex
for i := 0; i < len(vertices); i++ {
var name string = "v" + strconv.Itoa(i+1)
vertices[i] = NV(name)
}

// populate edges
var edges [5]Edge
for x := 0; x < len(edges); x++ {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can re-use i as a counter variable here and below...

var name string = "e" + strconv.Itoa(x+1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name := ...

edges[x] = NE(name)
}

// add all the vertices
for a := 0; a < len(vertices); a++ {
G.AddVertex(vertices[a])
}

// add edges to the vertices
for a := 0; a < len(vertices); a++ {
var b int = a + 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b := a + 1

if a <= len(vertices)-2 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subtle: -2's don't usually appear in code as much as -1's. Since we're a zero-based language, it's expected to see a -1 here and there... But you're using the -2 off of the first of the two elements... So it raises an eyebrow... So probably best to use -1 and compare it to b instead.

G.AddEdge(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")
Expand Down