diff --git a/test/runtests.jl b/test/runtests.jl index 9a77b0e2..b94a4b0a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1136,6 +1136,7 @@ Base.BroadcastStyle(::Broadcast.ArrayStyle{MyArray1}, ::Broadcast.ArrayStyle{MyA Base.BroadcastStyle(::Broadcast.ArrayStyle{MyArray2}, S::Broadcast.DefaultArrayStyle) = S @testset "broadcast" begin + dotaddsub((a, b, c),) = a .+ b .- c s = StructArray{ComplexF64}((rand(2,2), rand(2,2))) @test isa(@inferred(s .+ s), StructArray) @test (s .+ s).re == 2*s.re @@ -1159,18 +1160,21 @@ Base.BroadcastStyle(::Broadcast.ArrayStyle{MyArray2}, S::Broadcast.DefaultArrayS s2 = StructArray{ComplexF64}((MyArray2(rand(2)), MyArray2(rand(2)))) s3 = StructArray{ComplexF64}((MyArray3(rand(2)), MyArray3(rand(2)))) s4 = StructArray{ComplexF64}((rand(2), rand(2))) - - function _test_similar(a, b, c) - try - d = StructArray{ComplexF64}((a.re .+ b.re .- c.re, a.im .+ b.im .- c.im)) - @test typeof(a .+ b .- c) == typeof(d) - catch - @test_throws MethodError a .+ b .- c + test_set = Any[s1, s2, s3, s4] + for ia in 1:4, ib in 1:4, ic in 1:4 + as = test_set[ia], test_set[ib], test_set[ic] + is = ia, ib, ic + ns = count(==(3), is), count(==(4), is) + if count(==(2), is) == 0 && !(ia<=ib<=ic) + # Skip some order independent test (MyArray1 > MyArray3 > Array) + continue + elseif ns in ((1,2), (2,1)) || (ic == 3 && ia + ib == 6) + @test_throws MethodError dotaddsub(as) + else + d = StructArray{ComplexF64}((dotaddsub(map(a->a.re, as)), dotaddsub(map(a->a.im, as)))) + @test @inferred(dotaddsub(as))::typeof(d) == d end end - for s in (s1,s2,s3,s4), s′ in (s1,s2,s3,s4), s″ in (s1,s2,s3,s4) - _test_similar(s, s′, s″) - end # test for dimensionality track s = s1 @@ -1197,21 +1201,20 @@ Base.BroadcastStyle(::Broadcast.ArrayStyle{MyArray2}, S::Broadcast.DefaultArrayS @test (x -> x.x.x.a).(StructArray(x=StructArray(x=StructArray(a=1:3)))) == [1, 2, 3] @testset "ambiguity check" begin - function _test(a, b, c) - if a isa StructArray || b isa StructArray || c isa StructArray - d = @inferred a .+ b .- c - @test d == collect(a) .+ collect(b) .- collect(c) - @test d isa StructArray - end - end testset = Any[StructArray([1;2+im]), 1:2, (1,2), StructArray(@SArray [1 1+2im]), (@SArray [1 2]) ] - for aa in testset, bb in testset, cc in testset - _test(aa, bb, cc) + # The 3 styles in testset has clear priorities: + # Style{Tuple} < StaticArrayStyle < DefaultArrayStyle + # Thus is OK to skip some test as the result is order independent + for ia in 1:5, ib in ia:5, ic in ib:5 + as = a, b, c = testset[ia], testset[ib], testset[ic] + if a isa StructArray || b isa StructArray || c isa StructArray + @test @inferred(dotaddsub(as))::StructArray == dotaddsub(map(collect, as)) + end end end