Skip to content

Revise Unit Tests #153

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

Merged
merged 6 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ a = copy(ttask)

Notes:

- The [Turing](https://github.com/TuringLang/Turing.jl) probabilistic
programming language uses this task copying feature in an efficient
implementation of the [particle filtering](https://en.wikipedia.org/wiki/Particle_filter)
sampling algorithm for arbitrary order [Markov processes](https://en.wikipedia.org/wiki/Markov_model#Hidden_Markov_model).
- The [Turing](https://github.com/TuringLang/Turing.jl) probabilistic
programming language uses this task copying feature in an efficient
implementation of the [particle
filtering](https://en.wikipedia.org/wiki/Particle_filter) sampling
algorithm for arbitrary order [Markov
processes](https://en.wikipedia.org/wiki/Markov_model#Hidden_Markov_model).

- From v0.6.0, Libtask is implemented by recording all the computing
to a tape and copying that tape. Before that version, it is based on
Expand Down
10 changes: 8 additions & 2 deletions src/tapedtask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,14 @@ function TArray(T::Type, dim)
Base.depwarn("`TArray` is deprecated, please use `Array` instead.", :TArray)
Array{T}(undef, dim)
end
tzeros, tfill = zeros, fill

function tzeros(args...)
Base.depwarn("`tzeros` is deprecated, please use `zeros` instead.", :tzeros)
zeros(args...)
end
function tfill(args...)
Base.depwarn("`tfill` is deprecated, please use `fill` instead.", :tzeros)
fill(args...)
end
function TRef(x)
Base.depwarn("`TRef` is deprecated, please use `Ref` instead.", :TArray)
Ref(x)
Expand Down
51 changes: 51 additions & 0 deletions test/issues.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
@testset "Issues" begin
@testset "Issue: PR-86 (DynamicPPL.jl/pull/261)" begin
function f()
t = Array{Int}(undef, 1)
t[1] = 0
for _ in 1:4000
produce(t[1])
t[1]
t[1] = 1 + t[1]
end
end

ttask = TapedTask(f)

ex = try
for _ in 1:999
consume(ttask)
consume(ttask)
a = copy(ttask)
consume(a)
consume(a)
end
catch ex
ex
end
@test ex === nothing
end

@testset "Issue-140, copy unstarted task" begin
function f(x)
for i in 1:3
produce(i + x)
end
end

ttask = TapedTask(f, 3)
ttask2 = copy(ttask)
@test consume(ttask2) == 4
ttask3 = copy(ttask; args=(4,))
@test consume(ttask3) == 5
end

@testset "Issue-148, unused argument" begin
function f(x)
produce(1)
end

ttask = TapedTask(f, 2)
@test consume(ttask) == 1
end
end
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ using Test

include("tf.jl")
include("tapedtask.jl")
include("tarray.jl")
include("tref.jl")
include("tape_copy.jl")
include("issues.jl")

if haskey(ENV, "BENCHMARK")
include("benchmarks.jl")
Expand Down
174 changes: 174 additions & 0 deletions test/tape_copy.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
@testset "tape copy" begin
# Test case 1: stack allocated objects are deep copied.
@testset "stack allocated objects shallow copy" begin
function f()
t = 0
while true
produce(t)
t = 1 + t
end
end

ttask = TapedTask(f)
@test consume(ttask) == 0
@test consume(ttask) == 1
a = copy(ttask)
@test consume(a) == 2
@test consume(a) == 3
@test consume(ttask) == 2
@test consume(ttask) == 3

@inferred Libtask.TapedFunction(f)
end

# Test case 2: Array objects are deeply copied.
@testset "Array objects deep copy" begin
function f()
t = [0 1 2]
while true
produce(t[1])
t[1] = 1 + t[1]
end
end

ttask = TapedTask(f)
@test consume(ttask) == 0
@test consume(ttask) == 1
a = copy(ttask)
@test consume(a) == 2
@test consume(a) == 3
@test consume(ttask) == 2
@test consume(ttask) == 3
@test consume(ttask) == 4
@test consume(ttask) == 5
end

# Test case 3: Dict objects are shallowly copied.
@testset "Dict objects shallow copy" begin
function f()
t = Dict(1=>10, 2=>20)
while true
produce(t[1])
t[1] = 1 + t[1]
end
end

ttask = TapedTask(f)

@test consume(ttask) == 10
@test consume(ttask) == 11

a = copy(ttask)
@test consume(a) == 12
@test consume(a) == 13

@test consume(ttask) == 14
@test consume(ttask) == 15
end

@testset "Array deep copy 2" begin
function f()
t = Array{Int}(undef, 1)
t[1] = 0
while true
produce(t[1])
t[1]
t[1] = 1 + t[1]
end
end

ttask = TapedTask(f)

consume(ttask)
consume(ttask)
a = copy(ttask)
consume(a)
consume(a)

@test consume(ttask) == 2
@test consume(a) == 4

DATA = Dict{Task, Array}()
function g()
ta = zeros(UInt64, 4)
for i in 1:4
ta[i] = hash(current_task())
DATA[current_task()] = ta
produce(ta[i])
end
end

ttask = TapedTask(g)
@test consume(ttask) == hash(ttask.task) # index = 1
@test consume(ttask) == hash(ttask.task) # index = 2

a = copy(ttask)
@test consume(a) == hash(a.task) # index = 3
@test consume(a) == hash(a.task) # index = 4

@test consume(ttask) == hash(ttask.task) # index = 3

@test DATA[ttask.task] == [hash(ttask.task), hash(ttask.task), hash(ttask.task), 0]
@test DATA[a.task] == [hash(ttask.task), hash(ttask.task), hash(a.task), hash(a.task)]
end

# Test atomic values.
@testset "ref atomic" begin
function f()
t = Ref(1)
t[] = 0
for _ in 1:6
produce(t[])
t[]
t[] += 1
end
end

ctask = TapedTask(f)

consume(ctask)
consume(ctask)

a = copy(ctask)
consume(a)
consume(a)

@test consume(ctask) == 2
@test consume(a) == 4
end

@testset "ref of dictionary deep copy" begin
function f()
t = Ref(Dict("A" => 1, 5 => "B"))
t[]["A"] = 0
for _ in 1:6
produce(t[]["A"])
t[]["A"] += 1
end
end

ctask = TapedTask(f)

consume(ctask)
consume(ctask)

a = copy(ctask)
consume(a)
consume(a)

@test consume(ctask) == 2
@test consume(a) == 4
end

@testset "ref of array deep copy" begin
# Create a TRef storing a matrix.
x = TRef([1 2 3; 4 5 6])
x[][1, 3] = 900
@test x[][1,3] == 900

# TRef holding an array.
y = TRef([1,2,3])
y[][2] = 19
@test y[][2] == 19
end
end
92 changes: 0 additions & 92 deletions test/tapedtask.jl
Original file line number Diff line number Diff line change
@@ -1,71 +1,4 @@
@testset "tapedtask" begin
# Test case 1: stack allocated objects are deep copied.
@testset "stack allocated objects" begin
function f()
t = 0
while true
produce(t)
t = 1 + t
end
end

ttask = TapedTask(f)
@test consume(ttask) == 0
@test consume(ttask) == 1
a = copy(ttask)
@test consume(a) == 2
@test consume(a) == 3
@test consume(ttask) == 2
@test consume(ttask) == 3

@inferred Libtask.TapedFunction(f)
end

# Test case 2: Array objects are deeply copied.
@testset "Array objects" begin
function f()
t = [0 1 2]
while true
produce(t[1])
t[1] = 1 + t[1]
end
end

ttask = TapedTask(f)
@test consume(ttask) == 0
@test consume(ttask) == 1
a = copy(ttask)
@test consume(a) == 2
@test consume(a) == 3
@test consume(ttask) == 2
@test consume(ttask) == 3
@test consume(ttask) == 4
@test consume(ttask) == 5
end

# Test case 3: Dict objects are shallowly copied.
@testset "Dict objects" begin
function f()
t = Dict(1=>10, 2=>20)
while true
produce(t[1])
t[1] = 1 + t[1]
end
end

ttask = TapedTask(f)

@test consume(ttask) == 10
@test consume(ttask) == 11

a = copy(ttask)
@test consume(a) == 12
@test consume(a) == 13

@test consume(ttask) == 14
@test consume(ttask) == 15
end

@testset "iteration" begin
function f()
t = 1
Expand Down Expand Up @@ -207,29 +140,4 @@
end
end
end

@testset "Issues" begin
@testset "Issue-140, copy unstarted task" begin
function f(x)
for i in 1:3
produce(i + x)
end
end

ttask = TapedTask(f, 3)
ttask2 = copy(ttask)
@test consume(ttask2) == 4
ttask3 = copy(ttask; args=(4,))
@test consume(ttask3) == 5
end

@testset "Issue-148, unused argument" begin
function f(x)
produce(1)
end

ttask = TapedTask(f, 2)
@test consume(ttask) == 1
end
end
end
Loading