Skip to content

Commit

Permalink
tests for issues #80 (inference stack overflow for recursive resumabl…
Browse files Browse the repository at this point in the history
…e functions) and #86 (repeated variable names)
  • Loading branch information
Krastanov committed Feb 6, 2024
1 parent c09dd32 commit 9ccef15
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ println("Starting tests with $(Threads.nthreads()) threads out of `Sys.CPU_THREA
@doset "main"
@doset "yieldfrom"
@doset "typeparams"
@doset "repeated_variable"
@doset "inference_recursive_calls"
@doset "coverage_preservation"
@doset "performance"
VERSION >= v"1.8" && @doset "doctests"
Expand Down
72 changes: 72 additions & 0 deletions test/test_inference_recursive_calls.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Test
using ResumableFunctions

##

@resumable function rec1(a::Int)::Int
@yield a
if a > 0
for i in rec1(a-1)
@yield i
end
end
end
@test collect(rec1(4)) isa Vector{Int64}
@test collect(rec1(4)) == 4:-1:0

##

@resumable function rec2(a::Int)::Any
@yield a
if a > 0
for i in rec2(a-1)
@yield i
end
end
end
@test collect(rec2(4)) isa Vector{Any}
@test collect(rec2(4)) == 4:-1:0

##

@resumable function rec3(a)
@yield a
if a > 0
for i in rec3(a-1)
@yield i
end
end
end
@test collect(rec3(4)) isa Vector{Any}
@test collect(rec3(4)) == 4:-1:0

##

# From issue #80

@resumable function rf!(a::Vector{Int}, i::Integer, n::Integer)::Vector{Int}
if i > n
@yield a
return
end
a[i] = 0
for _ in rf!(a, i+1, n)
@yield a
end
for k = i+1:n
a[i] = k
a[k] = i
for _ in rf!(a, i+1, k-1)
for _ in rf!(a, k+1, n)
@yield a
end
end
end
end

const n = 3
const a = zeros(Int, n)
collect(rf!(a, 1, n)) == [[3, 0, 1],
[3, 0, 1],
[3, 0, 1],
[3, 0, 1]]
43 changes: 43 additions & 0 deletions test/test_repeated_variable.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Test
using ResumableFunctions

# From issue #86

##

@resumable function singleton()::String
@yield "anything"
end

# 1. must be defined before combined!
@resumable function _empty(x)::String
end

@resumable function combined()::String
for s in singleton()
@yield s # fails "here" because s is determined to be of type Nothing because of the second loop
end
# 2. must reuse of variable
for s in _empty(1) # dummy argument needed to illustrate point 1.
@yield s
end
end

collect(combined())

##

@resumable function singletonF()::String
@yield "anything"
end

@resumable function _emptyF()::String
end

@resumable function combinedF()::String
# @yieldfrom also uses the same variable names for each generated loop
@yieldfrom singletonF()
@yieldfrom _emptyF()
end

collect(combinedF())

0 comments on commit 9ccef15

Please sign in to comment.