Skip to content

Commit

Permalink
Split the tests into suites using some metaprogramming.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed May 19, 2020
1 parent b142b8e commit 734f39d
Show file tree
Hide file tree
Showing 14 changed files with 580 additions and 601 deletions.
36 changes: 22 additions & 14 deletions test/testsuite.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# test suite that can be used for all packages inheriting from GPUArrays
#
# use this by either calling `test`, or inspecting the dictionary `tests`

module TestSuite

Expand Down Expand Up @@ -28,12 +30,25 @@ function supported_eltypes()
(Float32, Float64, Int32, Int64, ComplexF32, ComplexF64)
end

# list of tests
const tests = Dict()
macro testsuite(name, ex)
safe_name = lowercase(replace(name, " "=>"_"))
fn = Symbol("test_$(safe_name)")
quote
$fn(AT) = $(esc(ex))(AT)

@assert !haskey(tests, $name)
tests[$name] = $fn
end
end

include("testsuite/construction.jl")
include("testsuite/gpuinterface.jl")
include("testsuite/indexing.jl")
include("testsuite/io.jl")
include("testsuite/base.jl")
include("testsuite/vector.jl")
#include("testsuite/vector.jl")
include("testsuite/mapreduce.jl")
include("testsuite/broadcasting.jl")
include("testsuite/linalg.jl")
Expand All @@ -42,23 +57,16 @@ include("testsuite/fft.jl")
include("testsuite/random.jl")
include("testsuite/uniformscaling.jl")


"""
Runs the entire GPUArrays test suite on array type `AT`
"""
function test(AT::Type{<:AbstractGPUArray})
TestSuite.test_construction(AT)
TestSuite.test_gpuinterface(AT)
TestSuite.test_indexing(AT)
TestSuite.test_io(AT)
TestSuite.test_base(AT)
TestSuite.test_mapreduce(AT)
TestSuite.test_broadcasting(AT)
TestSuite.test_linalg(AT)
TestSuite.test_math(AT)
TestSuite.test_fft(AT)
TestSuite.test_random(AT)
TestSuite.test_uniformscaling(AT)
for (name, fun) in tests
code = quote
$fun($AT)
end
@eval @testset $name $code
end
end

end
272 changes: 135 additions & 137 deletions test/testsuite/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,142 +25,140 @@ function ntuple_closure(ctx, result, ::Val{N}, testval) where N
return
end

function test_base(AT)
@testset "base functionality" begin
@testset "copyto!" begin
x = fill(0f0, (10, 10))
y = rand(Float32, (20, 10))
a = AT(x)
b = AT(y)
r1 = CartesianIndices((1:7, 3:8))
r2 = CartesianIndices((4:10, 3:8))
copyto!(x, r1, y, r2)
copyto!(a, r1, b, r2)
@test x == Array(a)
r2 = CartesianIndices((4:11, 3:8))
@test_throws DimensionMismatch copyto!(a, r1, b, r2)

r2 = CartesianIndices((4:10, 3:8))
x2 = fill(0f0, (10, 10))
copyto!(x2, r1, b, r2)
@test x2 == x

fill!(a, 0f0)
copyto!(a, r1, y, r2)
@test Array(a) == x

x = fill(0f0, (10,))
y = rand(Float32, (20,))
a = AT(x)
b = AT(y)
r1 = CartesianIndices((1:7,))
r2 = CartesianIndices((4:10,))
copyto!(x, r1, y, r2)
copyto!(a, r1, b, r2)
@test x == Array(a)
r2 = CartesianIndices((4:11,))
@test_throws ArgumentError copyto!(a, r1, b, r2)

x = fill(0f0, (10,))
y = rand(Float32, (20,))
a = AT(x)
b = AT(y)
r1 = (1:7,)
r2 = (4:10,)
copyto!(x, r1[1], y, r2[1])
copyto!(a, r1, b, r2)
@test x == Array(a)

x = fill(0., (10,))
y = fill(1, (10,))
a = AT(x)
b = AT(y)
copyto!(a, b)
@test Float64.(y) == Array(a)
end

@testset "vcat + hcat" begin
@test compare(vcat, AT, fill(0f0, (10, 10)), rand(Float32, 20, 10))
@test compare(hcat, AT, fill(0f0, (10, 10)), rand(Float32, 10, 10))

@test compare(hcat, AT, rand(Float32, 3, 3), rand(Float32, 3, 3))
@test compare(vcat, AT, rand(Float32, 3, 3), rand(Float32, 3, 3))
@test compare((a,b) -> cat(a, b; dims=4), AT, rand(Float32, 3, 4), rand(Float32, 3, 4))
end

@testset "reshape" begin
@test compare(reshape, AT, rand(10), Ref((10,)))
@test compare(reshape, AT, rand(10), Ref((10,1)))
@test compare(reshape, AT, rand(10), Ref((1,10)))

@test reshape(AT(rand(10)), (10,1)) isa AT
@test_throws Exception reshape(AT(rand(10)), (10,2))
end

@testset "reinterpret" begin
a = rand(ComplexF32, 22)
A = AT(a)
af0 = reinterpret(Float32, a)
Af0 = reinterpret(Float32, A)
@test Array(Af0) == af0
a = rand(ComplexF32, 4, 4)
A = AT(a)
@test_throws ArgumentError reinterpret(Int8, A)

a = rand(ComplexF32, 10 * 10)
A = AT(a)
af0 = reshape(reinterpret(Float32, vec(a)), (20, 10))
Af0 = reshape(reinterpret(Float32, vec(A)), (20, 10))
@test Array(Af0) == af0
end

@testset "ntuple test" begin
result = AT(Vector{NTuple{3, Float32}}(undef, 1))
gpu_call(ntuple_test, result, Val(3))
@test Array(result)[1] == (77, 2*77, 3*77)
x = 88f0
gpu_call(ntuple_closure, result, Val(3), x)
@test Array(result)[1] == (x, 2*x, 3*x)
end

@testset "cartesian iteration" begin
Ac = rand(Float32, 32, 32)
A = AT(Ac)
result = fill!(copy(A), 0.0)
gpu_call(cartesian_iter, result, A, size(A))
Array(result) == Ac
end

@testset "Custom kernel from Julia function" begin
x = AT(rand(Float32, 100))
y = AT(rand(Float32, 100))
gpu_call(clmap!, -, x, y; target=x)
jy = Array(y)
@test map!(-, jy, jy) Array(x)
end

@testset "map" begin
@test compare((a, b)-> map(+, a, b), AT, rand(Float32, 10), rand(Float32, 10))
@test compare((a, b)-> map!(-, a, b), AT, rand(Float32, 10), rand(Float32, 10))
@test compare((a, b, c, d)-> map!(*, a, b, c, d), AT, rand(Float32, 10), rand(Float32, 10), rand(Float32, 10), rand(Float32, 10))
end

@testset "repeat" begin
@test compare(a-> repeat(a, 5, 6), AT, rand(Float32, 10))
@test compare(a-> repeat(a, 5), AT, rand(Float32, 10))
@test compare(a-> repeat(a, 5), AT, rand(Float32, 5, 4))
@test compare(a-> repeat(a, 4, 3), AT, rand(Float32, 10, 15))
end

@testset "permutedims" begin
@test compare(x->permutedims(x, [1, 2]), AT, rand(4, 4))

inds = rand(1:100, 150, 150)
@test compare(x->permutedims(view(x, inds, :), (3, 2, 1)), AT, rand(100, 100))
end

@testset "circshift" begin
@test compare(x->circshift(x, (0,1)), AT, reshape(Vector(1:16), (4,4)))
end
@testsuite "base" AT->begin
@testset "copyto!" begin
x = fill(0f0, (10, 10))
y = rand(Float32, (20, 10))
a = AT(x)
b = AT(y)
r1 = CartesianIndices((1:7, 3:8))
r2 = CartesianIndices((4:10, 3:8))
copyto!(x, r1, y, r2)
copyto!(a, r1, b, r2)
@test x == Array(a)
r2 = CartesianIndices((4:11, 3:8))
@test_throws DimensionMismatch copyto!(a, r1, b, r2)

r2 = CartesianIndices((4:10, 3:8))
x2 = fill(0f0, (10, 10))
copyto!(x2, r1, b, r2)
@test x2 == x

fill!(a, 0f0)
copyto!(a, r1, y, r2)
@test Array(a) == x

x = fill(0f0, (10,))
y = rand(Float32, (20,))
a = AT(x)
b = AT(y)
r1 = CartesianIndices((1:7,))
r2 = CartesianIndices((4:10,))
copyto!(x, r1, y, r2)
copyto!(a, r1, b, r2)
@test x == Array(a)
r2 = CartesianIndices((4:11,))
@test_throws ArgumentError copyto!(a, r1, b, r2)

x = fill(0f0, (10,))
y = rand(Float32, (20,))
a = AT(x)
b = AT(y)
r1 = (1:7,)
r2 = (4:10,)
copyto!(x, r1[1], y, r2[1])
copyto!(a, r1, b, r2)
@test x == Array(a)

x = fill(0., (10,))
y = fill(1, (10,))
a = AT(x)
b = AT(y)
copyto!(a, b)
@test Float64.(y) == Array(a)
end

@testset "vcat + hcat" begin
@test compare(vcat, AT, fill(0f0, (10, 10)), rand(Float32, 20, 10))
@test compare(hcat, AT, fill(0f0, (10, 10)), rand(Float32, 10, 10))

@test compare(hcat, AT, rand(Float32, 3, 3), rand(Float32, 3, 3))
@test compare(vcat, AT, rand(Float32, 3, 3), rand(Float32, 3, 3))
@test compare((a,b) -> cat(a, b; dims=4), AT, rand(Float32, 3, 4), rand(Float32, 3, 4))
end

@testset "reshape" begin
@test compare(reshape, AT, rand(10), Ref((10,)))
@test compare(reshape, AT, rand(10), Ref((10,1)))
@test compare(reshape, AT, rand(10), Ref((1,10)))

@test reshape(AT(rand(10)), (10,1)) isa AT
@test_throws Exception reshape(AT(rand(10)), (10,2))
end

@testset "reinterpret" begin
a = rand(ComplexF32, 22)
A = AT(a)
af0 = reinterpret(Float32, a)
Af0 = reinterpret(Float32, A)
@test Array(Af0) == af0
a = rand(ComplexF32, 4, 4)
A = AT(a)
@test_throws ArgumentError reinterpret(Int8, A)

a = rand(ComplexF32, 10 * 10)
A = AT(a)
af0 = reshape(reinterpret(Float32, vec(a)), (20, 10))
Af0 = reshape(reinterpret(Float32, vec(A)), (20, 10))
@test Array(Af0) == af0
end

@testset "ntuple test" begin
result = AT(Vector{NTuple{3, Float32}}(undef, 1))
gpu_call(ntuple_test, result, Val(3))
@test Array(result)[1] == (77, 2*77, 3*77)
x = 88f0
gpu_call(ntuple_closure, result, Val(3), x)
@test Array(result)[1] == (x, 2*x, 3*x)
end

@testset "cartesian iteration" begin
Ac = rand(Float32, 32, 32)
A = AT(Ac)
result = fill!(copy(A), 0.0)
gpu_call(cartesian_iter, result, A, size(A))
Array(result) == Ac
end

@testset "Custom kernel from Julia function" begin
x = AT(rand(Float32, 100))
y = AT(rand(Float32, 100))
gpu_call(clmap!, -, x, y; target=x)
jy = Array(y)
@test map!(-, jy, jy) Array(x)
end

@testset "map" begin
@test compare((a, b)-> map(+, a, b), AT, rand(Float32, 10), rand(Float32, 10))
@test compare((a, b)-> map!(-, a, b), AT, rand(Float32, 10), rand(Float32, 10))
@test compare((a, b, c, d)-> map!(*, a, b, c, d), AT, rand(Float32, 10), rand(Float32, 10), rand(Float32, 10), rand(Float32, 10))
end

@testset "repeat" begin
@test compare(a-> repeat(a, 5, 6), AT, rand(Float32, 10))
@test compare(a-> repeat(a, 5), AT, rand(Float32, 10))
@test compare(a-> repeat(a, 5), AT, rand(Float32, 5, 4))
@test compare(a-> repeat(a, 4, 3), AT, rand(Float32, 10, 15))
end

@testset "permutedims" begin
@test compare(x->permutedims(x, [1, 2]), AT, rand(4, 4))

inds = rand(1:100, 150, 150)
@test compare(x->permutedims(view(x, inds, :), (3, 2, 1)), AT, rand(100, 100))
end

@testset "circshift" begin
@test compare(x->circshift(x, (0,1)), AT, reshape(Vector(1:16), (4,4)))
end
end
8 changes: 3 additions & 5 deletions test/testsuite/broadcasting.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
function test_broadcasting(AT)
@testset "broadcast" begin
broadcasting(AT)
vec3(AT)
end
@testsuite "broadcasting" AT->begin
broadcasting(AT)
vec3(AT)
end

test_idx(idx, A::AbstractArray{T}) where T = A[idx] * T(2)
Expand Down
Loading

0 comments on commit 734f39d

Please sign in to comment.