Skip to content
Open
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
104 changes: 59 additions & 45 deletions test/testsuite/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,40 @@
@test y == 42
end

@allowscalar @testset "getindex with $T" for T in eltypes
x = rand(T, 32)
src = AT(x)
for (i, xi) in enumerate(x)
@test src[i] == xi
end
@test Array(src[1:3]) == x[1:3]
@test Array(src[3:end]) == x[3:end]
@allowscalar @testset "getindex" begin
@testset "$T" for T in eltypes
x = rand(T, 32)
src = AT(x)
for (i, xi) in enumerate(x)
@test src[i] == xi
end
@test Array(src[1:3]) == x[1:3]
@test Array(src[3:end]) == x[3:end]
end
end

@allowscalar @testset "setindex! with $T" for T in eltypes
x = fill(zero(T), 7)
src = AT(x)
for i = 1:7
src[i] = i
@allowscalar @testset "setindex!" begin
@testset "$T" for T in eltypes
x = fill(zero(T), 7)
src = AT(x)
for i = 1:7
src[i] = i
end
@test Array(src) == T[1:7;]
src[1:3] = T[77, 22, 11]
@test Array(src[1:3]) == T[77, 22, 11]
src[1] = T(0)
end
@test Array(src) == T[1:7;]
src[1:3] = T[77, 22, 11]
@test Array(src[1:3]) == T[77, 22, 11]
src[1] = T(0)
end

@allowscalar @testset "issue #42 with $T" for T in eltypes
Ac = rand(Float32, 2, 2)
A = AT(Ac)
@test A[1] == Ac[1]
@test A[end] == Ac[end]
@test A[1, 1] == Ac[1, 1]
@allowscalar @testset "issue #42" begin
@testset "$T" for T in eltypes
Ac = rand(Float32, 2, 2)
A = AT(Ac)
@test A[1] == Ac[1]
@test A[end] == Ac[end]
@test A[1, 1] == Ac[1, 1]
end
end

@allowscalar @testset "get/setindex!" begin
Expand All @@ -63,18 +69,22 @@
end

@testsuite "indexing multidimensional" (AT, eltypes)->begin
@testset "sliced setindex" for T in eltypes
x = AT(zeros(T, (10, 10, 10, 10)))
y = AT(rand(T, (5, 5, 10, 10)))
x[2:6, 2:6, :, :] = y
@test Array(x[2:6, 2:6, :, :]) == Array(y)
@testset "sliced setindex" begin
@testset "$T" for T in eltypes
x = AT(zeros(T, (10, 10, 10, 10)))
y = AT(rand(T, (5, 5, 10, 10)))
x[2:6, 2:6, :, :] = y
@test Array(x[2:6, 2:6, :, :]) == Array(y)
end
end

@testset "sliced setindex, CPU source" for T in eltypes
x = AT(zeros(T, (2,3,4)))
y = AT(rand(T, (2,3)))
x[:, :, 2] = y
@test Array(x[:, :, 2]) == Array(y)
@testset "sliced setindex, CPU source" begin
@testset "$T" for T in eltypes
x = AT(zeros(T, (2,3,4)))
y = AT(rand(T, (2,3)))
x[:, :, 2] = y
@test Array(x[:, :, 2]) == Array(y)
end
end

@allowscalar @testset "empty array" begin
Expand Down Expand Up @@ -134,11 +144,13 @@ end
end
end

@testset "Broadcasting exceptions" for T in eltypes
x = AT(zeros(T, (10, 10, 10, 10)))
@test_throws ArgumentError x[1, :, :, :] = 0
y = AT(rand(T, (5, 5, 5, 5)))
@test_throws DimensionMismatch x[1:9,1:9,:,:] = y
@testset "Broadcasting exceptions" begin
@testset "$T" for T in eltypes
x = AT(zeros(T, (10, 10, 10, 10)))
@test_throws ArgumentError x[1, :, :, :] = 0
y = AT(rand(T, (5, 5, 5, 5)))
@test_throws DimensionMismatch x[1:9,1:9,:,:] = y
end
end

@testset "mismatching axes/indices" begin
Expand All @@ -156,13 +168,15 @@ end
@test compare(a->Symmetric(a, :U)[a .> 0], AT, a)
end

@testset "setindex! for WrapperGPUArray" for T in eltypes
x = AT(zeros(T, (10, 10)))'
y = AT(rand(T, (5, 5)))
x[2:6, 2:6] = y
@test Array(parent(x)[2:6, 2:6]) == Array(y)'
x[2:6, 2:6] = 1:25
@test Array(parent(x)[2:6, 2:6]) == reshape(1:25, 5, 5)'
@testset "setindex! for WrapperGPUArray" begin
@testset "$T" for T in eltypes
x = AT(zeros(T, (10, 10)))'
y = AT(rand(T, (5, 5)))
x[2:6, 2:6] = y
@test Array(parent(x)[2:6, 2:6]) == Array(y)'
x[2:6, 2:6] = 1:25
@test Array(parent(x)[2:6, 2:6]) == reshape(1:25, 5, 5)'
end
end
end

Expand Down
120 changes: 53 additions & 67 deletions test/testsuite/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -634,13 +634,14 @@ end
@test_throws SingularException ldiv!(D, B)
end

@testset "$f with diagonal $d" for f in (triu, triu!, tril, tril!),
d in -2:2
A = randn(Float32, 10, 10)
@test compare(f, AT, A, d)
@testset "$f" for f in (triu, triu!, tril, tril!)
@testset "with diagonal $d" for d in -2:2
A = randn(Float32, 10, 10)
@test compare(f, AT, A, d)

A_empty = randn(Float32, 0, 0)
@test compare(f, AT, A_empty, d)
A_empty = randn(Float32, 0, 0)
@test compare(f, AT, A_empty, d)
end
end

@testset "rmul!/lmul! with diagonal and number" begin
Expand Down Expand Up @@ -843,74 +844,59 @@ Base.:(*)(x::Number, y::Duo) = Duo(x * y.a, x * y.b)
end

@testsuite "linalg/norm" (AT, eltypes)->begin
@testset "$p-norm($sz x $T)" for sz in [(2,), (2,0), (2,2,2)],
p in Any[0, 0.5, 1, 1.5, 2, Inf, -Inf],
T in eltypes
if T == Int8
continue
end
if !in(float(real(T)), eltypes)
# norm promotes to float, so make sure that type is supported
continue
end
range = real(T) <: Integer ? (T.(1:10)) : T # prevent integer overflow
arr = rand(range, sz)
@test compare(norm, AT, arr, Ref(p))
@test isrealfloattype(typeof(norm(AT(arr), p)))
if !isempty(arr) && real(T) <: AbstractFloat && !iszero(p) && !isinf(p)
# Hit anti-under/overflow rescaling
@allowscalar arr[1] = floatmax(real(T)) / 2
@test compare(norm, AT, arr, Ref(p))
arr .= floatmin(real(T)) * 2
# norm promotes to float, so make sure that type is supported
valid_eltypes = filter(T -> in(float(real(T)), eltypes), eltypes)
valid_eltypes_noint8 = filter(T -> T !== Int8, valid_eltypes)

@testset "$p-norm" for p in Any[0, 0.5, 1, 1.5, 2, Inf, -Inf]
@testset "$sz x $T" for sz in [(2,), (2,0), (2,2,2)],
T in filter(T -> T !== Int8, valid_eltypes)
range = real(T) <: Integer ? (T.(1:10)) : T # prevent integer overflow
arr = rand(range, sz)
@test compare(norm, AT, arr, Ref(p))
@test isrealfloattype(typeof(norm(AT(arr), p)))
if !isempty(arr) && real(T) <: AbstractFloat && !iszero(p) && !isinf(p)
# Hit anti-under/overflow rescaling
@allowscalar arr[1] = floatmax(real(T)) / 2
@test compare(norm, AT, arr, Ref(p))
arr .= floatmin(real(T)) * 2
@test compare(norm, AT, arr, Ref(p))
end
end
end
@testset "$p-opnorm($sz x $T)" for sz in [(2, 0), (2, 3)],
p in Any[1, Inf],
T in eltypes
if T == Int8
continue
end
if !in(float(real(T)), eltypes)
# norm promotes to float, so make sure that type is supported
continue
@testset "$p-opnorm" for p in Any[1, Inf]
@testset "$sz x $T" for sz in [(2, 0), (2, 3)],
T in filter(T -> T !== Int8, valid_eltypes)
range = real(T) <: Integer ? (T.(1:10)) : T # prevent integer overflow
mat = rand(range, sz)
@test compare(opnorm, AT, mat, Ref(p))
@test isrealfloattype(typeof(opnorm(AT(mat), p)))
end
range = real(T) <: Integer ? (T.(1:10)) : T # prevent integer overflow
mat = rand(range, sz)
@test compare(opnorm, AT, mat, Ref(p))
@test isrealfloattype(typeof(opnorm(AT(mat), p)))
end
@testset "normalize($T)" for T in eltypes
if !in(float(real(T)), eltypes)
continue
end
range = real(T) <: Integer ? (T.(1:10)) : T
arr = rand(range, 10)
@test compare(normalize, AT, arr)
@test compare(normalize, AT, arr, Ref(1))
@testset "normalize" begin
@testset "$T" for T in valid_eltypes
range = real(T) <: Integer ? (T.(1:10)) : T
arr = rand(range, 10)
@test compare(normalize, AT, arr)
@test compare(normalize, AT, arr, Ref(1))
end
end
# Wrapped GPU arrays (e.g. SubArray) must also avoid scalar iteration.
@testset "$p-norm(view, $sz x $T)" for sz in [(5,), (5, 5), (4, 4, 4)],
p in Any[0, 1, 2, Inf],
T in eltypes
if T == Int8
continue
end
if !in(float(real(T)), eltypes)
continue
end
range = real(T) <: Integer ? (T.(1:10)) : T
arr = rand(range, sz)
indices = map(d -> 2:d-1, sz)
@test compare(x -> norm(view(x, indices...), p), AT, arr)
end
@testset "normalize(view, $T)" for T in eltypes
if !in(float(real(T)), eltypes)
continue
end
range = real(T) <: Integer ? (T.(1:10)) : T
arr = rand(range, 10)
@test compare(x -> normalize(view(x, 2:9)), AT, arr)
@testset "$p-norm(view)" for p in Any[0, 1, 2, Inf]
@testset "$sz x $T" for sz in [(5,), (5, 5), (4, 4, 4)],
T in filter(T -> T !== Int8, valid_eltypes)
range = real(T) <: Integer ? (T.(1:10)) : T
arr = rand(range, sz)
indices = map(d -> 2:d-1, sz)
@test compare(x -> norm(view(x, indices...), p), AT, arr)
end
end
@testset "normalize(view)" begin
@testset "$T" for T in valid_eltypes
range = real(T) <: Integer ? (T.(1:10)) : T
arr = rand(range, 10)
@test compare(x -> normalize(view(x, 2:9)), AT, arr)
end
end
end

Expand Down
Loading