From 2db33926ac39dafd95f2c62825e74514541caeb8 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Sun, 7 Jun 2020 08:03:22 +0930 Subject: [PATCH] graph/iterator: extend iteration tests and fix counting error --- graph/iterator/nodes.go | 5 +---- graph/iterator/nodes_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/graph/iterator/nodes.go b/graph/iterator/nodes.go index beec898f..a8ae68d0 100644 --- a/graph/iterator/nodes.go +++ b/graph/iterator/nodes.go @@ -24,10 +24,7 @@ func (n *OrderedNodes) Len() int { if n.idx >= len(n.nodes) { return 0 } - if n.idx <= 0 { - return len(n.nodes) - } - return len(n.nodes[n.idx:]) + return len(n.nodes[n.idx+1:]) } // Next returns whether the next call of Node will return a valid node. diff --git a/graph/iterator/nodes_test.go b/graph/iterator/nodes_test.go index 89e4f317..864acdbe 100644 --- a/graph/iterator/nodes_test.go +++ b/graph/iterator/nodes_test.go @@ -34,6 +34,9 @@ func TestOrderedNodesIterate(t *testing.T) { var got []graph.Node for it.Next() { got = append(got, it.Node()) + if len(got)+it.Len() != len(test.nodes) { + t.Errorf("unexpected iterator length during iteration for round %d: got:%d want:%d", i, it.Len(), len(test.nodes)-len(got)) + } } want := test.nodes if !reflect.DeepEqual(got, want) { @@ -91,6 +94,9 @@ func TestImplicitNodesIterate(t *testing.T) { var got []graph.Node for it.Next() { got = append(got, it.Node()) + if len(got)+it.Len() != test.end-test.beg { + t.Errorf("unexpected iterator length during iteration for round %d: got:%d want:%d", i, it.Len(), (test.end-test.beg)-len(got)) + } } if !reflect.DeepEqual(got, test.want) { t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, test.want)