Skip to content

Commit

Permalink
Re-organize tests a little.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Sep 7, 2021
1 parent e73f062 commit e1a4b3d
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 275 deletions.
1 change: 0 additions & 1 deletion test/testsuite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ 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/reductions.jl")
Expand Down
34 changes: 34 additions & 0 deletions test/testsuite/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,38 @@ end
@test Array(b) == [0]
@test Array(a) == [1]
end

@testset "input output" begin
# compact=false to avoid type aliases
replstr(x, kv::Pair...) = sprint((io,x) -> show(IOContext(io, :compact => false, :limit => true, :displaysize => (24, 80), kv...), MIME("text/plain"), x), x)
showstr(x, kv::Pair...) = sprint((io,x) -> show(IOContext(io, :limit => true, :displaysize => (24, 80), kv...), x), x)

@testset "showing" begin
# vectors and non-vector arrays showing
# are handled differently in base/arrayshow.jl
A = AT(Int64[1])
B = AT(Int64[1 2;3 4])

msg = replstr(A)
@test occursin(Regex("^1-element $AT{Int64,\\s?1.*}:\n 1\$"), msg)

# # result of e.g. `print` differs on 32bit and 64bit machines
# due to different definition of `Int` type
# print([1]) shows as [1] on 64bit but Int64[1] on 32bit
msg = showstr(A)
@test msg == "[1]" || msg == "Int64[1]"

msg = replstr(B)
@test occursin(Regex("^2×2 $AT{Int64,\\s?2.*}:\n 1 2\n 3 4\$"), msg)

msg = showstr(B)
@test msg == "[1 2; 3 4]" || msg == "Int64[1 2; 3 4]"

# the printing of Adjoint depends on global state
msg = replstr(A')
@test occursin(Regex("^1×1 Adjoint{Int64,\\s?$AT{Int64,\\s?1.*}}:\n 1\$"), msg) ||
occursin(Regex("^1×1 LinearAlgebra.Adjoint{Int64,\\s?$AT{Int64,\\s?1.*}}:\n 1\$"), msg) ||
occursin(Regex("^1×1 adjoint\\(::$AT{Int64,\\s?1.*}\\) with eltype Int64:\n 1\$"), msg)
end
end
end
330 changes: 166 additions & 164 deletions test/testsuite/construction.jl
Original file line number Diff line number Diff line change
@@ -1,180 +1,182 @@
@testsuite "construct/direct" (AT, eltypes)->begin
for T in eltypes
B = AT{T}(undef, 10)
@test B isa AT{T,1}
@test size(B) == (10,)
@test eltype(B) == T

B = AT{T}(undef, 10, 10)
@test B isa AT{T,2}
@test size(B) == (10, 10)
@test eltype(B) == T

B = AT{T}(undef, (10, 10))
@test B isa AT{T,2}
@test size(B) == (10, 10)
@test eltype(B) == T
end

# compare against Array
for typs in [(), (Int,), (Int,1), (Int,2), (Float32,), (Float32,1), (Float32,2)],
args in [(), (1,), (1,2), ((1,),), ((1,2),),
(undef,), (undef, 1,), (undef, 1,2), (undef, (1,),), (undef, (1,2),),
(Int,), (Int, 1,), (Int, 1,2), (Int, (1,),), (Int, (1,2),),
([1,2],), ([1 2],)]
cpu = try
Array{typs...}(args...)
catch ex
isa(ex, MethodError) || rethrow()
nothing
end

gpu = try
AT{typs...}(args...)
catch ex
isa(ex, MethodError) || rethrow()
cpu == nothing || rethrow()
nothing
@testsuite "constructors" (AT, eltypes)->begin
@testset "direct" begin
for T in eltypes
B = AT{T}(undef, 10)
@test B isa AT{T,1}
@test size(B) == (10,)
@test eltype(B) == T

B = AT{T}(undef, 10, 10)
@test B isa AT{T,2}
@test size(B) == (10, 10)
@test eltype(B) == T

B = AT{T}(undef, (10, 10))
@test B isa AT{T,2}
@test size(B) == (10, 10)
@test eltype(B) == T
end

if cpu == nothing
@test gpu == nothing
else
@test typeof(cpu) == typeof(convert(Array, gpu))
# compare against Array
for typs in [(), (Int,), (Int,1), (Int,2), (Float32,), (Float32,1), (Float32,2)],
args in [(), (1,), (1,2), ((1,),), ((1,2),),
(undef,), (undef, 1,), (undef, 1,2), (undef, (1,),), (undef, (1,2),),
(Int,), (Int, 1,), (Int, 1,2), (Int, (1,),), (Int, (1,2),),
([1,2],), ([1 2],)]
cpu = try
Array{typs...}(args...)
catch ex
isa(ex, MethodError) || rethrow()
nothing
end

gpu = try
AT{typs...}(args...)
catch ex
isa(ex, MethodError) || rethrow()
cpu == nothing || rethrow()
nothing
end

if cpu == nothing
@test gpu == nothing
else
@test typeof(cpu) == typeof(convert(Array, gpu))
end
end
end
end

@testsuite "construct/similar" (AT, eltypes)->begin
for T in eltypes
B = AT{T}(undef, 10)

B = similar(B, Int32, 11, 15)
@test B isa AT{Int32,2}
@test size(B) == (11, 15)
@test eltype(B) == Int32

B = similar(B, T)
@test B isa AT{T,2}
@test size(B) == (11, 15)
@test eltype(B) == T

B = similar(B, (5,))
@test B isa AT{T,1}
@test size(B) == (5,)
@test eltype(B) == T

B = similar(B, 7)
@test B isa AT{T,1}
@test size(B) == (7,)
@test eltype(B) == T

B = similar(AT{Int32}, (11, 15))
@test B isa AT{Int32,2}
@test size(B) == (11, 15)
@test eltype(B) == Int32

B = similar(AT{T}, (5,))
@test B isa AT{T,1}
@test size(B) == (5,)
@test eltype(B) == T

B = similar(AT{T}, 7)
@test B isa AT{T,1}
@test size(B) == (7,)
@test eltype(B) == T

B = similar(Broadcast.Broadcasted(*, (B, B)), T)
@test B isa AT{T,1}
@test size(B) == (7,)
@test eltype(B) == T

B = similar(Broadcast.Broadcasted(*, (B, B)), Int32, (11, 15))
@test B isa AT{Int32,2}
@test size(B) == (11, 15)
@test eltype(B) == Int32
@testset "similar" begin
for T in eltypes
B = AT{T}(undef, 10)

B = similar(B, Int32, 11, 15)
@test B isa AT{Int32,2}
@test size(B) == (11, 15)
@test eltype(B) == Int32

B = similar(B, T)
@test B isa AT{T,2}
@test size(B) == (11, 15)
@test eltype(B) == T

B = similar(B, (5,))
@test B isa AT{T,1}
@test size(B) == (5,)
@test eltype(B) == T

B = similar(B, 7)
@test B isa AT{T,1}
@test size(B) == (7,)
@test eltype(B) == T

B = similar(AT{Int32}, (11, 15))
@test B isa AT{Int32,2}
@test size(B) == (11, 15)
@test eltype(B) == Int32

B = similar(AT{T}, (5,))
@test B isa AT{T,1}
@test size(B) == (5,)
@test eltype(B) == T

B = similar(AT{T}, 7)
@test B isa AT{T,1}
@test size(B) == (7,)
@test eltype(B) == T

B = similar(Broadcast.Broadcasted(*, (B, B)), T)
@test B isa AT{T,1}
@test size(B) == (7,)
@test eltype(B) == T

B = similar(Broadcast.Broadcasted(*, (B, B)), Int32, (11, 15))
@test B isa AT{Int32,2}
@test size(B) == (11, 15)
@test eltype(B) == Int32
end
end
end

@testsuite "construct/convenience" (AT, eltypes)->begin
for T in eltypes
A = AT(rand(T, 3))
b = rand(T)
fill!(A, b)
@test A isa AT{T,1}
@test Array(A) == fill(b, 3)

A = zero(AT(rand(T, 2)))
@test A isa AT{T,1}
@test Array(A) == zero(rand(T, 2))

A = zero(AT(rand(T, 2, 2)))
@test A isa AT{T,2}
@test Array(A) == zero(rand(T, 2, 2))

A = zero(AT(rand(T, 2, 2, 2)))
@test A isa AT{T,3}
@test Array(A) == zero(rand(T, 2, 2, 2))

A = one(AT(rand(T, 2, 2)))
@test A isa AT{T,2}
@test Array(A) == one(rand(T, 2, 2))

A = oneunit(AT(rand(T, 2, 2)))
@test A isa AT{T,2}
@test Array(A) == oneunit(rand(T, 2, 2))
@testset "convenience" begin
for T in eltypes
A = AT(rand(T, 3))
b = rand(T)
fill!(A, b)
@test A isa AT{T,1}
@test Array(A) == fill(b, 3)

A = zero(AT(rand(T, 2)))
@test A isa AT{T,1}
@test Array(A) == zero(rand(T, 2))

A = zero(AT(rand(T, 2, 2)))
@test A isa AT{T,2}
@test Array(A) == zero(rand(T, 2, 2))

A = zero(AT(rand(T, 2, 2, 2)))
@test A isa AT{T,3}
@test Array(A) == zero(rand(T, 2, 2, 2))

A = one(AT(rand(T, 2, 2)))
@test A isa AT{T,2}
@test Array(A) == one(rand(T, 2, 2))

A = oneunit(AT(rand(T, 2, 2)))
@test A isa AT{T,2}
@test Array(A) == oneunit(rand(T, 2, 2))
end
end
end

@testsuite "construct/conversions" (AT, eltypes)->begin
for T in eltypes
Bc = round.(rand(10, 10) .* 10.0)
B = AT{T}(Bc)
@test size(B) == (10, 10)
@test eltype(B) == T
@test Array(B) Bc

Bc = rand(T, 10)
B = AT(Bc)
@test size(B) == (10,)
@test eltype(B) == T
@test Array(B) Bc

Bc = rand(T, 10, 10)
B = AT{T, 2}(Bc)
@test size(B) == (10, 10)
@test eltype(B) == T
@test Array(B) Bc

intervals = Dict(
Float16 => -2^11:2^11,
Float32 => -2^24:2^24,
Float64 => -2^53:2^53,
)

Bc = rand(Int8, 3, 3, 3)
B = convert(AT{T, 3}, Bc)
@test size(B) == (3, 3, 3)
@test eltype(B) == T
@test Array(B) Bc
@testset "conversions" begin
for T in eltypes
Bc = round.(rand(10, 10) .* 10.0)
B = AT{T}(Bc)
@test size(B) == (10, 10)
@test eltype(B) == T
@test Array(B) Bc

Bc = rand(T, 10)
B = AT(Bc)
@test size(B) == (10,)
@test eltype(B) == T
@test Array(B) Bc

Bc = rand(T, 10, 10)
B = AT{T, 2}(Bc)
@test size(B) == (10, 10)
@test eltype(B) == T
@test Array(B) Bc

intervals = Dict(
Float16 => -2^11:2^11,
Float32 => -2^24:2^24,
Float64 => -2^53:2^53,
)

Bc = rand(Int8, 3, 3, 3)
B = convert(AT{T, 3}, Bc)
@test size(B) == (3, 3, 3)
@test eltype(B) == T
@test Array(B) Bc
end
end
end

@testsuite "construct/uniformscaling" (AT, eltypes)->begin
for T in eltypes
x = Matrix{T}(I, 4, 2)
@testset "uniformscaling" begin
for T in eltypes
x = Matrix{T}(I, 4, 2)

x1 = AT{T, 2}(I, 4, 2)
x2 = AT{T}(I, (4, 2))
x3 = AT{T, 2}(I, (4, 2))
x1 = AT{T, 2}(I, 4, 2)
x2 = AT{T}(I, (4, 2))
x3 = AT{T, 2}(I, (4, 2))

@test Array(x1) x
@test Array(x2) x
@test Array(x3) x
@test Array(x1) x
@test Array(x2) x
@test Array(x3) x

x = Matrix(T(3) * I, 2, 4)
x1 = AT(T(3) * I, 2, 4)
@test eltype(x1) == T
@test Array(x1) x
x = Matrix(T(3) * I, 2, 4)
x1 = AT(T(3) * I, 2, 4)
@test eltype(x1) == T
@test Array(x1) x
end
end
end
Loading

0 comments on commit e1a4b3d

Please sign in to comment.