using ImageFiltering, Colors, ColorVectorSpace
using Base.Test

@testset "gradient" begin
    y, x = 1:5, (1:7)'
    for (img, ey, ex) in ((y .* ones(x), 1, 0),
                          (ones(y) .* x, 0, 1),
                          (map(Gray, y.*ones(Float64, x)), Gray(1), Gray(0)),
                          (map(v->RGB(v,0,0), y.*ones(Float64, x)), RGB(1,0,0), RGB(0,0,0)))
        for kernelfunc in (KernelFactors.ando3, KernelFactors.sobel, KernelFactors.prewitt,
                           KernelFactors.ando4, KernelFactors.ando5,
                           Kernel.ando3, Kernel.sobel, Kernel.prewitt,
                           Kernel.ando4, Kernel.ando5)
            gy, gx = @inferred(imgradients(img, kernelfunc, Inner()))
            for val in gy
                @test abs(val - ey) < 1e-4
            end
            for val in gx
                @test abs(val - ex) < 1e-4
            end
            gy, gx = imgradients(img, kernelfunc, Pad{:replicate}())
            @test indices(gy) == indices(gx) == indices(img)
        end
    end
    # 3d
    y, x, z = 1:5, (1:7)', reshape(1:6, 1, 1, 6)
    for (img, ey, ex, ez) in ((y .* ones(x) .* ones(z), 1, 0, 0),
                              (ones(y) .* x .* ones(z), 0, 1, 0),
                              (ones(y) .* ones(x) .* z, 0, 0, 1))
        for kernelfunc in (KernelFactors.ando3, KernelFactors.sobel, KernelFactors.prewitt,
                           Kernel.ando3, Kernel.sobel, Kernel.prewitt)
            gy, gx, gz = @inferred(imgradients(img, kernelfunc, Inner()))
            for val in gy
                @test abs(val - ey) < 1e-4
            end
            for val in gx
                @test abs(val - ex) < 1e-4
            end
            for val in gz
                @test abs(val - ez) < 1e-4
            end
        end
    end
end

nothing
