using QuasiArrays, ArrayLayouts, LinearAlgebra, Test
import QuasiArrays: apply

@testset "Multiplication" begin
    @testset "Diag * Inclusion" begin
        A = QuasiDiagonal(Inclusion(0:0.1:1))
        @test rowsupport(A,0.1) == colsupport(A,0.1) == 0.1
        b = Inclusion(0:0.1:1)
        Ab = A*b
        @test Ab[0.1] ≈ 0.1^2
        @test_throws DimensionMismatch A*Inclusion(1:2)

        @test A*A isa QuasiDiagonal
        @test (A*A)[0.1,0.1] ≈ A[0.1,0.1]^2
    end
    @testset "Quasi * Quasi" begin
        A = QuasiArray(rand(3,3),(0:0.5:1,0:0.5:1))
        @test *(A) == A
        @test A*A isa QuasiArray
        @test QuasiArray(A*A) == QuasiArray(A.parent*A.parent,A.axes)
    end
    @testset "Quasi * Array" begin
        A = QuasiArray(rand(3,3),(0:0.5:1,Base.OneTo(3)))
        B = rand(3,3)
        @test A*B isa QuasiArray
        @test QuasiArray(A*B) ≈ QuasiArray(A.parent*B,A.axes)
        @test B*A' isa QuasiArray
        @test (A*B)[0.5,1] ≈ (Array(A)*B)[2,1]
    end

    @testset "Triple" begin
        A = QuasiArray(rand(3,3),(0:0.5:1,0:0.5:1))
        @test A isa QuasiArray
        @test Array(A*A) ≈ Array(A)^2
        @test Array(A*A*A) ≈ Array(A)^3
    end

    @testset "absvec" begin
        a = QuasiArray(rand(3),(0:0.5:1,))
        c = QuasiArray(rand(3) .+ im .* randn(3),(0:0.5:1,))
        @test a'a ≈ transpose(a)a ≈ dot(a,a)
        @test c'c ≈ dot(c,c)
        @test transpose(c)c ≈ transpose(c.parent)*c.parent
    end
end