-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests for issues #80 (inference stack overflow for recursive resumabl…
…e functions) and #86 (repeated variable names)
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |