Skip to content

Commit b265fea

Browse files
authored
Move testhelpers over from julia repo (#1371)
This makes it easier to keep the test helpers in sync with the code changes in this repo, and also makes it easier to add tests.
1 parent 9a429fe commit b265fea

24 files changed

+1446
-56
lines changed

test/adjtrans.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ isdefined(Main, :pruned_old_LA) || @eval Main include("prune_old_LA.jl")
66

77
using Test, LinearAlgebra
88

9-
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
9+
const BASE_TEST_PATH = joinpath(dirname(pathof(LinearAlgebra)), "..", "test")
10+
const TESTHELPERS = joinpath(BASE_TEST_PATH, "testhelpers")
1011

11-
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "OffsetArrays.jl"))
12+
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($TESTHELPERS, "OffsetArrays.jl"))
1213
using .Main.OffsetArrays
1314

14-
isdefined(Main, :ImmutableArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "ImmutableArrays.jl"))
15+
isdefined(Main, :ImmutableArrays) || @eval Main include(joinpath($TESTHELPERS, "ImmutableArrays.jl"))
1516
using .Main.ImmutableArrays
1617

1718
@testset "Adjoint and Transpose inner constructor basics" begin

test/bidiag.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@ isdefined(Main, :pruned_old_LA) || @eval Main include("prune_old_LA.jl")
77
using Test, LinearAlgebra, Random
88
using LinearAlgebra: BlasReal, BlasFloat
99

10-
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
10+
const BASE_TEST_PATH = joinpath(dirname(pathof(LinearAlgebra)), "..", "test")
11+
const TESTHELPERS = joinpath(BASE_TEST_PATH, "testhelpers")
1112

12-
isdefined(Main, :Quaternions) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "Quaternions.jl"))
13+
isdefined(Main, :Quaternions) || @eval Main include(joinpath($TESTHELPERS, "Quaternions.jl"))
1314
using .Main.Quaternions
1415

15-
isdefined(Main, :InfiniteArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "InfiniteArrays.jl"))
16+
isdefined(Main, :InfiniteArrays) || @eval Main include(joinpath($TESTHELPERS, "InfiniteArrays.jl"))
1617
using .Main.InfiniteArrays
1718

18-
isdefined(Main, :FillArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "FillArrays.jl"))
19+
isdefined(Main, :FillArrays) || @eval Main include(joinpath($TESTHELPERS, "FillArrays.jl"))
1920
using .Main.FillArrays
2021

21-
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "OffsetArrays.jl"))
22+
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($TESTHELPERS, "OffsetArrays.jl"))
2223
using .Main.OffsetArrays
2324

24-
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "SizedArrays.jl"))
25+
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($TESTHELPERS, "SizedArrays.jl"))
2526
using .Main.SizedArrays
2627

27-
isdefined(Main, :ImmutableArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "ImmutableArrays.jl"))
28+
isdefined(Main, :ImmutableArrays) || @eval Main include(joinpath($TESTHELPERS, "ImmutableArrays.jl"))
2829
using .Main.ImmutableArrays
2930

3031
include("testutils.jl") # test_approx_eq_modphase

test/cholesky.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ using Test, LinearAlgebra, Random
88
using LinearAlgebra: BlasComplex, BlasFloat, BlasReal, QRPivoted,
99
PosDefException, RankDeficientException, chkfullrank
1010

11-
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
11+
const BASE_TEST_PATH = joinpath(dirname(pathof(LinearAlgebra)), "..", "test")
12+
const TESTHELPERS = joinpath(BASE_TEST_PATH, "testhelpers")
1213

13-
isdefined(Main, :Quaternions) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "Quaternions.jl"))
14+
isdefined(Main, :Quaternions) || @eval Main include(joinpath($TESTHELPERS, "Quaternions.jl"))
1415
using .Main.Quaternions
1516

1617
function unary_ops_tests(a, ca, tol; n=size(a, 1))

test/dense.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ using Test, LinearAlgebra, Random
88
using LinearAlgebra: BlasComplex, BlasFloat, BlasReal
99
using Test: GenericArray
1010

11-
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
12-
isdefined(Main, :FillArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "FillArrays.jl"))
11+
const BASE_TEST_PATH = joinpath(dirname(pathof(LinearAlgebra)), "..", "test")
12+
const TESTHELPERS = joinpath(BASE_TEST_PATH, "testhelpers")
13+
14+
isdefined(Main, :FillArrays) || @eval Main include(joinpath($TESTHELPERS, "FillArrays.jl"))
1315
import Main.FillArrays
1416

15-
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "SizedArrays.jl"))
17+
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($TESTHELPERS, "SizedArrays.jl"))
1618
using Main.SizedArrays
1719

1820
@testset "Check that non-floats are correctly promoted" begin

test/diagonal.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ isdefined(Main, :pruned_old_LA) || @eval Main include("prune_old_LA.jl")
77
using Test, LinearAlgebra, Random
88
using LinearAlgebra: BlasFloat, BlasComplex
99

10-
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
10+
const BASE_TEST_PATH = joinpath(dirname(pathof(LinearAlgebra)), "..", "test")
11+
const TESTHELPERS = joinpath(BASE_TEST_PATH, "testhelpers")
1112

12-
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "OffsetArrays.jl"))
13+
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($TESTHELPERS, "OffsetArrays.jl"))
1314
using .Main.OffsetArrays
1415

15-
isdefined(Main, :InfiniteArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "InfiniteArrays.jl"))
16+
isdefined(Main, :InfiniteArrays) || @eval Main include(joinpath($TESTHELPERS, "InfiniteArrays.jl"))
1617
using .Main.InfiniteArrays
1718

18-
isdefined(Main, :FillArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "FillArrays.jl"))
19+
isdefined(Main, :FillArrays) || @eval Main include(joinpath($TESTHELPERS, "FillArrays.jl"))
1920
using .Main.FillArrays
2021

21-
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "SizedArrays.jl"))
22+
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($TESTHELPERS, "SizedArrays.jl"))
2223
using .Main.SizedArrays
2324

24-
isdefined(Main, :ImmutableArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "ImmutableArrays.jl"))
25+
isdefined(Main, :ImmutableArrays) || @eval Main include(joinpath($TESTHELPERS, "ImmutableArrays.jl"))
2526
using .Main.ImmutableArrays
2627

2728
const n=12 # Size of matrix problem to test

test/generic.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@ using Test, LinearAlgebra, Random
88
using Test: GenericArray
99
using LinearAlgebra: isbanded
1010

11-
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
11+
const BASE_TEST_PATH = joinpath(dirname(pathof(LinearAlgebra)), "..", "test")
12+
const TESTHELPERS = joinpath(BASE_TEST_PATH, "testhelpers")
1213

13-
isdefined(Main, :Quaternions) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "Quaternions.jl"))
14+
isdefined(Main, :Quaternions) || @eval Main include(joinpath($TESTHELPERS, "Quaternions.jl"))
1415
using .Main.Quaternions
1516

16-
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "OffsetArrays.jl"))
17+
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($TESTHELPERS, "OffsetArrays.jl"))
1718
using .Main.OffsetArrays
1819

19-
isdefined(Main, :DualNumbers) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "DualNumbers.jl"))
20+
isdefined(Main, :DualNumbers) || @eval Main include(joinpath($TESTHELPERS, "DualNumbers.jl"))
2021
using .Main.DualNumbers
2122

22-
isdefined(Main, :FillArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "FillArrays.jl"))
23+
isdefined(Main, :FillArrays) || @eval Main include(joinpath($TESTHELPERS, "FillArrays.jl"))
2324
using .Main.FillArrays
2425

25-
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "SizedArrays.jl"))
26+
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($TESTHELPERS, "SizedArrays.jl"))
2627
using .Main.SizedArrays
2728

2829
Random.seed!(123)

test/hessenberg.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ isdefined(Main, :pruned_old_LA) || @eval Main include("prune_old_LA.jl")
66

77
using Test, LinearAlgebra, Random
88

9-
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
10-
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "SizedArrays.jl"))
9+
const BASE_TEST_PATH = joinpath(dirname(pathof(LinearAlgebra)), "..", "test")
10+
const TESTHELPERS = joinpath(BASE_TEST_PATH, "testhelpers")
11+
12+
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($TESTHELPERS, "SizedArrays.jl"))
1113
using .Main.SizedArrays
1214

13-
isdefined(Main, :ImmutableArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "ImmutableArrays.jl"))
15+
isdefined(Main, :ImmutableArrays) || @eval Main include(joinpath($TESTHELPERS, "ImmutableArrays.jl"))
1416
using .Main.ImmutableArrays
1517

1618
# for tuple tests below

test/matmul.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ using Base: rtoldefault
88
using Test, LinearAlgebra, Random
99
using LinearAlgebra: mul!, Symmetric, Hermitian
1010

11-
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
11+
const BASE_TEST_PATH = joinpath(dirname(pathof(LinearAlgebra)), "..", "test")
12+
const TESTHELPERS = joinpath(BASE_TEST_PATH, "testhelpers")
1213

13-
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "SizedArrays.jl"))
14+
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($TESTHELPERS, "SizedArrays.jl"))
1415
using .Main.SizedArrays
1516

1617
## Test Julia fallbacks to BLAS routines

test/special.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ isdefined(Main, :pruned_old_LA) || @eval Main include("prune_old_LA.jl")
77
using Test, LinearAlgebra, Random
88
using LinearAlgebra: rmul!, BandIndex
99

10-
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
11-
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "SizedArrays.jl"))
10+
const BASE_TEST_PATH = joinpath(dirname(pathof(LinearAlgebra)), "..", "test")
11+
const TESTHELPERS = joinpath(BASE_TEST_PATH, "testhelpers")
12+
13+
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($TESTHELPERS, "SizedArrays.jl"))
1214
using .Main.SizedArrays
1315

1416
n= 10 #Size of matrix to test

test/structuredbroadcast.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ isdefined(Main, :pruned_old_LA) || @eval Main include("prune_old_LA.jl")
66

77
using Test, LinearAlgebra
88

9-
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
10-
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "SizedArrays.jl"))
9+
const BASE_TEST_PATH = joinpath(dirname(pathof(LinearAlgebra)), "..", "test")
10+
const TESTHELPERS = joinpath(BASE_TEST_PATH, "testhelpers")
11+
12+
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($TESTHELPERS, "SizedArrays.jl"))
1113
using .Main.SizedArrays
1214

1315
@testset "broadcast[!] over combinations of scalars, structured matrices, and dense vectors/matrices" begin

test/symmetric.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ isdefined(Main, :pruned_old_LA) || @eval Main include("prune_old_LA.jl")
66

77
using Test, LinearAlgebra, Random
88

9-
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
9+
const BASE_TEST_PATH = joinpath(dirname(pathof(LinearAlgebra)), "..", "test")
10+
const TESTHELPERS = joinpath(BASE_TEST_PATH, "testhelpers")
1011

11-
isdefined(Main, :Quaternions) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "Quaternions.jl"))
12+
isdefined(Main, :Quaternions) || @eval Main include(joinpath($TESTHELPERS, "Quaternions.jl"))
1213
using .Main.Quaternions
1314

14-
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "SizedArrays.jl"))
15+
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($TESTHELPERS, "SizedArrays.jl"))
1516
using .Main.SizedArrays
1617

17-
isdefined(Main, :ImmutableArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "ImmutableArrays.jl"))
18+
isdefined(Main, :ImmutableArrays) || @eval Main include(joinpath($TESTHELPERS, "ImmutableArrays.jl"))
1819
using .Main.ImmutableArrays
1920

2021
Random.seed!(1010)

test/testhelpers/DualNumbers.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
module DualNumbers
4+
5+
export Dual
6+
7+
# Dual numbers type with minimal interface
8+
# example of a (real) number type that subtypes Number, but not Real.
9+
# Can be used to test generic linear algebra functions.
10+
11+
struct Dual{T<:Real} <: Number
12+
val::T
13+
eps::T
14+
end
15+
Base.:+(x::Dual, y::Dual) = Dual(x.val + y.val, x.eps + y.eps)
16+
Base.:-(x::Dual, y::Dual) = Dual(x.val - y.val, x.eps - y.eps)
17+
Base.:*(x::Dual, y::Dual) = Dual(x.val * y.val, x.eps * y.val + y.eps * x.val)
18+
Base.:*(x::Number, y::Dual) = Dual(x*y.val, x*y.eps)
19+
Base.:*(x::Dual, y::Number) = Dual(x.val*y, x.eps*y)
20+
Base.:/(x::Dual, y::Dual) = Dual(x.val / y.val, (x.eps*y.val - x.val*y.eps)/(y.val*y.val))
21+
22+
Base.:(==)(x::Dual, y::Dual) = x.val == y.val && x.eps == y.eps
23+
24+
Base.promote_rule(::Type{Dual{T}}, ::Type{T}) where {T} = Dual{T}
25+
Base.promote_rule(::Type{Dual{T}}, ::Type{S}) where {T,S<:Real} = Dual{promote_type(T, S)}
26+
Base.promote_rule(::Type{Dual{T}}, ::Type{Dual{S}}) where {T,S} = Dual{promote_type(T, S)}
27+
28+
Base.convert(::Type{Dual{T}}, x::Dual{T}) where {T} = x
29+
Base.convert(::Type{Dual{T}}, x::Dual) where {T} = Dual(convert(T, x.val), convert(T, x.eps))
30+
Base.convert(::Type{Dual{T}}, x::Real) where {T} = Dual(convert(T, x), zero(T))
31+
32+
Base.float(x::Dual) = Dual(float(x.val), float(x.eps))
33+
# the following two methods are needed for normalize (to check for potential overflow)
34+
Base.typemax(x::Dual) = Dual(typemax(x.val), zero(x.eps))
35+
Base.prevfloat(x::Dual{<:AbstractFloat}) = prevfloat(x.val)
36+
37+
Base.abs2(x::Dual) = x*x
38+
Base.abs(x::Dual) = sqrt(abs2(x))
39+
Base.sqrt(x::Dual) = Dual(sqrt(x.val), x.eps/(2sqrt(x.val)))
40+
41+
Base.isless(x::Dual, y::Dual) = x.val < y.val
42+
Base.isless(x::Real, y::Dual) = x < y.val
43+
Base.isinf(x::Dual) = isinf(x.val) & isfinite(x.eps)
44+
Base.real(x::Dual) = x # since we currently only consider Dual{<:Real}
45+
46+
end # module

test/testhelpers/FillArrays.jl

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
module FillArrays
2+
3+
struct Fill{T, N, S<:NTuple{N,Integer}} <: AbstractArray{T,N}
4+
value::T
5+
size::S
6+
end
7+
8+
Fill(v, size::Vararg{Integer}) = Fill(v, size)
9+
10+
Base.size(F::Fill) = F.size
11+
12+
Base.copy(F::Fill) = F
13+
14+
Base.AbstractArray{T,N}(F::Fill{<:Any,N}) where {T,N} = Fill(T(F.value), F.size)
15+
16+
@inline getindex_value(F::Fill) = F.value
17+
18+
@inline function Base.getindex(F::Fill{<:Any,N}, i::Vararg{Int,N}) where {N}
19+
@boundscheck checkbounds(F, i...)
20+
getindex_value(F)
21+
end
22+
23+
@inline function Base.setindex!(F::Fill, v, k::Integer)
24+
@boundscheck checkbounds(F, k)
25+
v == getindex_value(F) || throw(ArgumentError("Cannot setindex! to $v for a Fill with value $(getindex_value(F))."))
26+
F
27+
end
28+
29+
@inline function Base.fill!(F::Fill, v)
30+
v == getindex_value(F) || throw(ArgumentError("Cannot fill! with $v a Fill with value $(getindex_value(F))."))
31+
F
32+
end
33+
34+
Base.zero(F::Fill) = Fill(zero(F.value), size(F))
35+
36+
Base.show(io::IO, F::Fill) = print(io, "Fill($(F.value), $(F.size))")
37+
Base.show(io::IO, ::MIME"text/plain", F::Fill) = show(io, F)
38+
39+
_first_or_one(t::Tuple) = t[1]
40+
_first_or_one(t::Tuple{}) = 1
41+
42+
_match_size(sz::Tuple{}, inner::Tuple{}, outer::Tuple{}) = ()
43+
function _match_size(sz::Tuple, inner::Tuple, outer::Tuple)
44+
t1 = (_first_or_one(sz), _first_or_one(inner), _first_or_one(outer))
45+
t2 = _match_size(sz[2:end], inner[2:end], outer[2:end])
46+
(t1, t2...)
47+
end
48+
49+
function _repeat_size(sz::Tuple, inner::Tuple, outer::Tuple)
50+
t = _match_size(sz, inner, outer)
51+
map(*, getindex.(t, 1), getindex.(t, 2), getindex.(t, 3))
52+
end
53+
54+
function Base.repeat(A::Fill; inner=ntuple(x->1, ndims(A)), outer=ntuple(x->1, ndims(A)))
55+
Base.require_one_based_indexing(A)
56+
length(inner) >= ndims(A) ||
57+
throw(ArgumentError("number of inner repetitions $(length(inner)) cannot be "*
58+
"less than number of dimensions of input array $(ndims(A))"))
59+
length(outer) >= ndims(A) ||
60+
throw(ArgumentError("number of outer repetitions $(length(outer)) cannot be "*
61+
"less than number of dimensions of input array $(ndims(A))"))
62+
sz = _repeat_size(size(A), Tuple(inner), Tuple(outer))
63+
Fill(getindex_value(A), sz)
64+
end
65+
66+
end

0 commit comments

Comments
 (0)