Skip to content

Commit 548654e

Browse files
author
Jack Dunham
committed
Fix bug with AbstractNetworkIterator types of length 1 not executing their single compute! call
1 parent afd15c3 commit 548654e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/solvers/iterators.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ abstract type AbstractNetworkIterator end
1212
islaststep(iterator::AbstractNetworkIterator) = state(iterator) >= length(iterator)
1313

1414
function Base.iterate(iterator::AbstractNetworkIterator, init = true)
15-
islaststep(iterator) && return nothing
15+
# The assumption is that first "increment!" is implicit, therefore we must skip the
16+
# the termination check for the first iteration, i.e. `AbstractNetworkIterator` is not
17+
# defined when length < 1,
18+
init || islaststep(iterator) && return nothing
1619
# We seperate increment! from step! and demand that any AbstractNetworkIterator *must*
1720
# define a method for increment! This way we avoid cases where one may wish to nest
1821
# calls to different step! methods accidentaly incrementing multiple times.

test/solvers/test_iterators.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ end
4949
import .TestIteratorUtils
5050

5151
@testset "`AbstractNetworkIterator` Interface" begin
52+
53+
@testset "Edge cases" begin
54+
TI = TestIteratorUtils.TestIterator(1, 1, [])
55+
cb = []
56+
@test islaststep(TI)
57+
for _ in TI
58+
@test islaststep(TI)
59+
push!(cb, state(TI))
60+
end
61+
@test length(cb) == 1
62+
@test length(TI.output) == 1
63+
@test only(cb) == 1
64+
end
5265
TI = TestIteratorUtils.TestIterator(1, 4, [])
5366

5467
@test !islaststep((TI))

0 commit comments

Comments
 (0)