text:
# This file is a part of Julia. License is MIT: http://julialang.org/license

# Array test

@testset "basics" begin
    @test length([1, 2, 3]) == 3
    @test countnz([1, 2, 3]) == 3

    let a = ones(4), b = a+a, c = a-a
        @test b[1] === 2. && b[2] === 2. && b[3] === 2. && b[4] === 2.
        @test c[1] === 0. && c[2] === 0. && c[3] === 0. && c[4] === 0.
    end

    @test length((1,)) == 1
    @test length((1,2)) == 2

    @test isequal(1.+[1,2,3], [2,3,4])
    @test isequal([1,2,3].+1, [2,3,4])
    @test isequal(1.-[1,2,3], [0,-1,-2])
    @test isequal([1,2,3].-1, [0,1,2])

    @test isequal(5*[1,2,3], [5,10,15])
    @test isequal([1,2,3]*5, [5,10,15])
    @test isequal(1./[1,2,5], [1.0,0.5,0.2])
    @test isequal([1,2,3]/5, [0.2,0.4,0.6])

    @test isequal(2.%[1,2,3], [0,0,2])
    @test isequal([1,2,3].%2, [1,0,1])
    @test isequal(2.÷[1,2,3], [2,1,0])
    @test isequal([1,2,3].÷2, [0,1,1])
    @test isequal(-2.%[1,2,3], [0,0,-2])
    @test isequal([-1,-2,-3].%2, [-1,0,-1])
    @test isequal(-2.÷[1,2,3], [-2,-1,0])
    @test isequal([-1,-2,-3].÷2, [0,-1,-1])

    @test isequal(1.<<[1,2,5], [2,4,32])
    @test isequal(128.>>[1,2,5], [64,32,4])
    @test isequal(2.>>1, 1)
    @test isequal(1.<<1, 2)
    @test isequal([1,2,5].<<[1,2,5], [2,8,160])
    @test isequal([10,20,50].>>[1,2,5], [5,5,1])

    a = ones(2,2)
    a[1,1] = 1
    a[1,2] = 2
    a[2,1] = 3
    a[2,2] = 4
    b = a'
    @test a[1,1] == 1. && a[1,2] == 2. && a[2,1] == 3. && a[2,2] == 4.
    @test b[1,1] == 1. && b[2,1] == 2. && b[1,2] == 3. && b[2,2] == 4.
    a[[1 2 3 4]] = 0
    @test a == zeros(2,2)
    a[[1 2], [1 2]] = 1
    @test a == ones(2,2)
    a[[1 2], 1] = 0
    @test a[1,1] == 0. && a[1,2] == 1. && a[2,1] == 0. && a[2,2] == 1.
    a[:, [1 2]] = 2
    @test a == 2ones(2,2)

    a = Array{Float64}(2, 2, 2, 2, 2)
    a[1,1,1,1,1] = 10
    a[1,2,1,1,2] = 20
    a[1,1,2,2,1] = 30

    @test a[1,1,1,1,1] == 10
    @test a[1,2,1,1,2] == 20
    @test a[1,1,2,2,1] == 30

    @test_throws ArgumentError reinterpret(Int8, a)

    b = reshape(a, (32,))
    @test b[1]  == 10
    @test b[19] == 20
    @test b[13] == 30
    @test_throws DimensionMismatch reshape(b,(5,7))
    @test_throws DimensionMismatch reshape(b,(35,))
    @test_throws DimensionMismatch reinterpret(Int, b, (35,))
    @test_throws ArgumentError reinterpret(Any, b, (32,))
    @test_throws DimensionMismatch reinterpret(Complex128, b, (32,))
    c = ["hello", "world"]
    @test_throws ArgumentError reinterpret(Float32, c, (2,))
    a = Vector(ones(5))
    @test_throws ArgumentError resize!(a, -2)

    b = rand(32)
    a = reshape(b, (2, 2, 2, 2, 2))
    @test ndims(a) == 5
    @test a[2,1,2,2,1] == b[14]
    @test a[2,2,2,2,2] == b[end]
end
@testset "reshaping SubArrays" begin
    a = collect(reshape(1:5, 1, 5))
    @testset "linearfast" begin
        s = view(a, :, 2:4)
        r = reshape(s, (length(s),))
        @test length(r) == 3
        @test r[1] == 2
        @test r[3,1] == 4
        @test r[Base.ReshapedIndex(CartesianIndex((1,2)))] == 3
        @test parent(reshape(r, (1,3))) === r.parent === s
        @test parentindexes(r) == (1:1, 1:3)
        @test reshape(r, (3,)) === r
        @test convert(Array{Int,1}, r) == [2,3,4]
        @test_throws MethodError convert(Array{Int,2}, r)
        @test convert(Array{Int}, r) == [2,3,4]
        @test Base.unsafe_convert(Ptr{Int}, r) == Base.unsafe_convert(Ptr{Int}, s)
    end
    @testset "linearslow" begin
        s = view(a, :, [2,3,5])
        r = reshape(s, length(s))
        @test length(r) == 3
        @test r[1] == 2
        @test r[3,1] == 5
        @test r[Base.ReshapedIndex(CartesianIndex((1,2)))] == 3
        @test parent(reshape(r, (1,3))) === r.parent === s
        @test parentindexes(r) == (1:1, 1:3)
        @test reshape(r, (3,)) === r
        @test convert(Array{Int,1}, r) == [2,3,5]
        @test_throws MethodError convert(Array{Int,2}, r)
        @test convert(Array{Int}, r) == [2,3,5]
        @test_throws ErrorException Base.unsafe_convert(Ptr{Int}, r)
        r[2] = -1
        @test a[3] == -1
        a = zeros(0, 5)  # an empty linearslow array
        s = view(a, :, [2,3,5])
        @test length(reshape(s, length(s))) == 0
    end
end
@testset "reshape(a, Val{N})" begin
    a = ones(Int,3,3)
    s = view(a, 1:2, 1:2)
    for N in (1,3)
        @test isa(reshape(a, Val{N}), Array{Int,N})
        @test isa(reshape(s, Val{N}), Base.ReshapedArray{Int,N})
    end
end
@testset "reshape with colon" begin
    # Reshape with an omitted dimension
    let A = linspace(1, 60, 60)
        @test size(reshape(A, :))         == (60,)
        @test size(reshape(A, :, 1))      == (60, 1)
        @test size(reshape(A, (:, 2)))    == (30, 2)
        @test size(reshape(A, 3, :))      == (3, 20)
        @test size(reshape(A, 2, 3, :))   == (2, 3, 10)
        @test size(reshape(A, (2, :, 5))) == (2, 6, 5)
        @test_throws DimensionMismatch reshape(A, 7, :)
        @test_throws DimensionMismatch reshape(A, :, 2, 3, 4)
        @test_throws DimensionMismatch reshape(A, (:, :))

        B = rand(2,2,2,2)
        @test size(reshape(B, :)) == (16,)
        @test size(reshape(B, :, 4)) == (4, 4)
        @test size(reshape(B, (2, 1, :))) == (2, 1, 8)
        @test_throws DimensionMismatch reshape(B, 3, :)
        @test_throws DimensionMismatch reshape(B, :, :, 2, 2)
    end
end

@test reshape(1:5, (5,)) === 1:5
@test reshape(1:5, 5) === 1:5

@testset "setindex! on a reshaped range" begin
    a = reshape(1:20, 5, 4)
    for idx in ((3,), (2,2), (Base.ReshapedIndex(1),))
        try
            a[idx...] = 7
            error("wrong error")
        catch err
            @test err.msg == "indexed assignment fails for a reshaped range; consider calling collect"
        end
    end
end

@testset "conversion from ReshapedArray to Array (#18262)" begin
    a = Base.ReshapedArray(1:3, (3, 1), ())
    @test convert(Array, a) == a
    @test convert(Array{Int}, a) == a
    @test convert(Array{Float64}, a) == a
    @test convert(Matrix, a) == a
    @test convert(Matrix{Int}, a) == a
    @test convert(Matrix{Float64}, a) == a
    b = Base.ReshapedArray(1:3, (3,), ())
    @test convert(Array, b) == b
    @test convert(Array{Int}, b) == b
    @test convert(Array{Float64}, b) == b
    @test convert(Vector, b) == b
    @test convert(Vector{Int}, b) == b
    @test convert(Vector{Float64}, b) == b
end

@testset "operations with IndexLinear ReshapedArray" begin
    b = collect(1:12)
    a = Base.ReshapedArray(b, (4,3), ())
    @test a[3,2] == 7
    @test a[6] == 6
    a[3,2] = -2
    a[6] = -3
    a[Base.ReshapedIndex(5)] = -4
    @test b[5] == -4
    @test b[6] == -3
    @test b[7] == -2
    b = reinterpret(Int, a, (3,4))
    b[1] = -1
    @test vec(b) == vec(a)

    a = rand(1, 1, 8, 8, 1)
    @test @inferred(squeeze(a, 1)) == @inferred(squeeze(a, (1,))) == reshape(a, (1, 8, 8, 1))
    @test @inferred(squeeze(a, (1, 5))) == squeeze(a, (5, 1)) == reshape(a, (1, 8, 8))
    @test @inferred(squeeze(a, (1, 2, 5))) == squeeze(a, (5, 2, 1)) == reshape(a, (8, 8))
    @test_throws ArgumentError squeeze(a, 0)
    @test_throws ArgumentError squeeze(a, (1, 1))
    @test_throws ArgumentError squeeze(a, (1, 2, 1))
    @test_throws ArgumentError squeeze(a, (1, 1, 2))
    @test_throws ArgumentError squeeze(a, 3)
    @test_throws ArgumentError squeeze(a, 4)
    @test_throws ArgumentError squeeze(a, 6)

    sz = (5,8,7)
    A = reshape(1:prod(sz),sz...)
    @test A[2:6] == [2:6;]
    @test A[1:3,2,2:4] == cat(2,46:48,86:88,126:128)
    @test A[:,7:-3:1,5] == [191 176 161; 192 177 162; 193 178 163; 194 179 164; 195 180 165]
    @test reshape(A, Val{2})[:,3:9] == reshape(11:45,5,7)
    rng = (2,2:3,2:2:5)
    tmp = zeros(Int,map(maximum,rng)...)
    tmp[rng...] = A[rng...]
    @test  tmp == cat(3,zeros(Int,2,3),[0 0 0; 0 47 52],zeros(Int,2,3),[0 0 0; 0 127 132])

    @test cat([1,2],1,2,3.,4.,5.) == diagm([1,2,3.,4.,5.])
    blk = [1 2;3 4]
    tmp = cat([1,3],blk,blk)
    @test tmp[1:2,1:2,1] == blk
    @test tmp[1:2,1:2,2] == zero(blk)
    @test tmp[3:4,1:2,1] == zero(blk)
    @test tmp[3:4,1:2,2] == blk

    x = rand(2,2)
    b = x[1,:]
    @test isequal(size(b), (2,))
    b = x[:,1]
    @test isequal(size(b), (2,))

    x = rand(5,5)
    b = x[2:3,2]
    @test b[1] == x[2,2] && b[2] == x[3,2]

    B = zeros(4,5)
    B[:,3] = 1:4
    @test B == [0 0 1 0 0; 0 0 2 0 0; 0 0 3 0 0; 0 0 4 0 0]
    B[2,:] = 11:15
    @test B == [0 0 1 0 0; 11 12 13 14 15; 0 0 3 0 0; 0 0 4 0 0]
    B[[3,1],[2,4]] = [21 22; 23 24]
    @test B == [0 23 1 24 0; 11 12 13 14 15; 0 21 3 22 0; 0 0 4 0 0]
    B[4,[2,3]] = 7
    @test B == [0 23 1 24 0; 11 12 13 14 15; 0 21 3 22 0; 0 7 7 0 0]

    @test isequal(reshape(reshape(1:27, 3, 3, 3), Val{2})[1,:], [1,  4,  7,  10,  13,  16,  19,  22,  25])

    a = [3, 5, -7, 6]
    b = [4, 6, 2, -7, 1]
    ind = findin(a, b)
    @test ind == [3,4]

    rt = Base.return_types(setindex!, Tuple{Array{Int32, 3}, UInt8, Vector{Int}, Int16, UnitRange{Int}})
    @test length(rt) == 1 && rt[1] == Array{Int32, 3}
end
@testset "construction" begin
    @test typeof(Vector{Int}(3)) == Vector{Int}
    @test typeof(Vector{Int}()) == Vector{Int}
    @test typeof(Vector(3)) == Vector{Any}
    @test typeof(Vector()) == Vector{Any}
    @test typeof(Matrix{Int}(2,3)) == Matrix{Int}
    @test typeof(Matrix(2,3)) == Matrix{Any}

    @test size(Vector{Int}(3)) == (3,)
    @test size(Vector{Int}()) == (0,)
    @test size(Vector(3)) == (3,)
    @test size(Vector()) == (0,)
    @test size(Matrix{Int}(2,3)) == (2,3)
    @test size(Matrix(2,3)) == (2,3)

    # TODO: will throw MethodError after 0.6 deprecations are deleted
    dw = Base.JLOptions().depwarn
    if dw == 2
        @test_throws ErrorException Matrix{Int}()
        @test_throws ErrorException Matrix()
    elseif dw == 1
        @test_warn "deprecated" Matrix{Int}()
        @test_warn "deprecated" Matrix()
    elseif dw == 0
        @test size(Matrix{Int}()) == (0,0)
        @test size(Matrix()) == (0,0)
    else
        error("unexpected depwarn value")
    end
    @test_throws MethodError Array{Int,3}()
end
@testset "get" begin
    A = reshape(1:24, 3, 8)
    x = get(A, 32, -12)
    @test x == -12
    x = get(A, 14, -12)
    @test x == 14
    x = get(A, (2,4), -12)
    @test x == 11
    x = get(A, (4,4), -12)
    @test x == -12
    X = get(A, -5:5, NaN32)
    @test eltype(X) == Float32
    @test Base.elsize(X) == sizeof(Float32)
    @test !all(isinteger, X)
    @test isnan.(X) == [trues(6);falses(5)]
    @test X[7:11] == [1:5;]
    X = get(A, (2:4, 9:-2:-13), 0)
    Xv = zeros(Int, 3, 12)
    Xv[1:2, 2:5] = A[2:3, 7:-2:1]
    @test X == Xv
    X2 = get(A, Vector{Int}[[2:4;], [9:-2:-13;]], 0)
    @test X == X2
end
@testset "arrays as dequeues" begin
    l = Any[1]
    push!(l,2,3,8)
    @test l[1]==1 && l[2]==2 && l[3]==3 && l[4]==8
    v = pop!(l)
    @test v == 8
    v = pop!(l)
    @test v == 3
    @test length(l)==2
    m = Any[]
    @test_throws ArgumentError pop!(m)
    @test_throws ArgumentError shift!(m)
    unshift!(l,4,7,5)
    @test l[1]==4 && l[2]==7 && l[3]==5 && l[4]==1 && l[5]==2
    v = shift!(l)
    @test v == 4
    @test length(l)==4

    v = [3, 7, 6]
    @test_throws BoundsError insert!(v, 0, 5)
    for i = 1:4
        vc = copy(v)
        @test insert!(vc, i, 5) === vc
        @test vc == [v[1:(i-1)]; 5; v[i:end]]
    end
    @test_throws BoundsError insert!(v, 5, 5)
end
@testset "concatenation" begin
    @test isequal([ones(2,2)  2*ones(2,1)], [1. 1 2; 1 1 2])
    @test isequal([ones(2,2); 2*ones(1,2)], [1. 1; 1 1; 2 2])
end

@testset "typed array literals" begin
    X = Float64[1 2 3]
    Y = [1. 2. 3.]
    @test size(X) == size(Y)
    for i = 1:3 @test X[i] === Y[i] end
    X = Float64[1;2;3]
    Y = [1.,2.,3.]
    @test size(X) == size(Y)
    for i = 1:3 @test X[i] === Y[i] end
    X = Float64[1 2 3; 4 5 6]
    Y = [1. 2. 3.; 4. 5. 6.]
    @test size(X) == size(Y)
    for i = 1:length(X) @test X[i] === Y[i] end

    _array_equiv(a,b) = eltype(a) == eltype(b) && a == b
    @test _array_equiv(UInt8[1:3;4], [0x1,0x2,0x3,0x4])
    @test_throws MethodError UInt8[1:3]
    @test_throws MethodError UInt8[1:3,]
    @test_throws MethodError UInt8[1:3,4:6]
    a = Array{UnitRange{Int}}(1); a[1] = 1:3
    @test _array_equiv([1:3,], a)
    a = Array{UnitRange{Int}}(2); a[1] = 1:3; a[2] = 4:6
    @test _array_equiv([1:3,4:6], a)
end

@testset "typed hvcat" begin
    X = Float64[1 2 3; 4 5 6]
    X32 = Float32[X X; X X]
    @test eltype(X32) <: Float32
    for i=[1,3], j=[1,4]
        @test X32[i:(i+1), j:(j+2)] == X
    end
end
@testset "end" begin
    X = [ i+2j for i=1:5, j=1:5 ]
    @test X[end,end] == 15
    @test X[end]     == 15  # linear index
    @test X[2,  end] == 12
    @test X[end,  2] == 9
    @test X[end-1,2] == 8
    Y = [2, 1, 4, 3]
    @test X[Y[end],1] == 5
    @test X[end,Y[end]] == 11
end
@testset "find, findfirst, findnext, findlast, findprev" begin
    a = [0,1,2,3,0,1,2,3]
    @test find(a) == [2,3,4,6,7,8]
    @test find(a.==2) == [3,7]
    @test find(isodd,a) == [2,4,6,8]
    @test findfirst(a) == 2
    @test findfirst(a.==0) == 1
    @test findfirst(a.==5) == 0
    @test findfirst([1,2,4,1,2,3,4], 3) == 6
    @test findfirst(isodd, [2,4,6,3,9,2,0]) == 4
    @test findfirst(isodd, [2,4,6,2,0]) == 0
    @test findnext(a,4) == 4
    @test findnext(a,5) == 6
    @test findnext(a,1) == 2
    @test findnext(a,1,4) == 6
    @test findnext(a,5,4) == 0
    @test findlast(a) == 8
    @test findlast(a.==0) == 5
    @test findlast(a.==5) == 0
    @test findlast([1,2,4,1,2,3,4], 3) == 6
    @test findlast(isodd, [2,4,6,3,9,2,0]) == 5
    @test findlast(isodd, [2,4,6,2,0]) == 0
    @test findprev(a,4) == 4
    @test findprev(a,5) == 4
    @test findprev(a,1) == 0
    @test findprev(a,1,4) == 2
    @test findprev(a,1,8) == 6
    @test findprev(isodd, [2,4,5,3,9,2,0], 7) == 5
    @test findprev(isodd, [2,4,5,3,9,2,0], 2) == 0
end
@testset "find with general iterables" begin
    s = "julia"
    # FIXME once 16269 is resolved
    # @test find(s) == [1,2,3,4,5]
    @test find(c -> c == 'l', s) == [3]
    g = graphemes("日本語")
    @test find(g) == [1,2,3]
    @test find(isascii, g) == Int[]
end
@testset "findn" begin
    b = findn(ones(2,2,2,2))
    @test (length(b[1]) == 16)
    @test (length(b[2]) == 16)
    @test (length(b[3]) == 16)
    @test (length(b[4]) == 16)

    #hand made case
    a = ([2,1,2],[1,2,2],[2,2,2])
    z = zeros(2,2,2)
    for i = 1:3
        z[a[1][i],a[2][i],a[3][i]] = 10
    end
    @test isequal(a,findn(z))
end

@testset "findmin findmax indmin indmax" begin
    @test indmax([10,12,9,11]) == 2
    @test indmin([10,12,9,11]) == 3
    @test findmin([NaN,3.2,1.8]) == (1.8,3)
    @test findmax([NaN,3.2,1.8]) == (3.2,2)
    @test findmin([NaN,3.2,1.8,NaN]) == (1.8,3)
    @test findmax([NaN,3.2,1.8,NaN]) == (3.2,2)
    @test findmin([3.2,1.8,NaN,2.0]) == (1.8,2)
    @test findmax([3.2,1.8,NaN,2.0]) == (3.2,1)

    #14085
    @test findmax(4:9) == (9,6)
    @test indmax(4:9) == 6
    @test findmin(4:9) == (4,1)
    @test indmin(4:9) == 1
    @test findmax(5:-2:1) == (5,1)
    @test indmax(5:-2:1) == 1
    @test findmin(5:-2:1) == (1,3)
    @test indmin(5:-2:1) == 3
end

@testset "permutedims" begin
    # keeps the num of dim
    p = randperm(5)
    q = randperm(5)
    a = rand(p...)
    b = permutedims(a,q)
    @test isequal(size(b), tuple(p[q]...))

    # hand made case
    y = zeros(1,2,3)
    for i = 1:6
        y[i]=i
    end

    z = zeros(3,1,2)
    for i = 1:3
        z[i] = i*2-1
        z[i+3] = i*2
    end

    # permutes correctly
    @test isequal(z,permutedims(y,[3,1,2]))
    @test isequal(z,permutedims(y,(3,1,2)))

    # of a subarray
    a = rand(5,5)
    s = view(a,2:3,2:3)
    p = permutedims(s, [2,1])
    @test p[1,1]==a[2,2] && p[1,2]==a[3,2]
    @test p[2,1]==a[2,3] && p[2,2]==a[3,3]

    # of a non-strided subarray
    a = reshape(1:60, 3, 4, 5)
    s = view(a,:,[1,2,4],[1,5])
    c = convert(Array, s)
    for p in ([1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1])
        @test permutedims(s, p) == permutedims(c, p)
        @test PermutedDimsArray(s, p) == permutedims(c, p)
    end
    @test_throws ArgumentError permutedims(a, (1,1,1))
    @test_throws ArgumentError permutedims(s, (1,1,1))
    @test_throws ArgumentError PermutedDimsArray(a, (1,1,1))
    @test_throws ArgumentError PermutedDimsArray(s, (1,1,1))
    cp = PermutedDimsArray(c, (3,2,1))
    @test pointer(cp) == pointer(c)
    @test_throws ArgumentError pointer(cp, 2)
    @test strides(cp) == (9,3,1)
    ap = PermutedDimsArray(collect(a), (2,1,3))
    @test strides(ap) == (3,1,12)

    for A in [rand(1,2,3,4),rand(2,2,2,2),rand(5,6,5,6),rand(1,1,1,1)]
        perm = randperm(4)
        @test isequal(A,permutedims(permutedims(A,perm),invperm(perm)))
        @test isequal(A,permutedims(permutedims(A,invperm(perm)),perm))
    end
end

@testset "circshift" begin
    @test circshift(1:5, -1) == circshift(1:5, 4) == circshift(1:5, -6) == [2,3,4,5,1]
    @test circshift(1:5, 1) == circshift(1:5, -4) == circshift(1:5, 6)  == [5,1,2,3,4]
    a = [1:5;]
    @test_throws ArgumentError Base.circshift!(a, a, 1)
    b = copy(a)
    @test Base.circshift!(b, a, 1) == [5,1,2,3,4]
end

# unique across dim

# All rows and columns unique
A = ones(10, 10)
A[diagind(A)] = shuffle!([1:10;])
@test unique(A, 1) == A
@test unique(A, 2) == A

# 10 repeats of each row
B = A[shuffle!(repmat(1:10, 10)), :]
C = unique(B, 1)
@test sortrows(C) == sortrows(A)
@test unique(B, 2) == B
@test unique(B.', 2).' == C

# Along third dimension
D = cat(3, B, B)
@test unique(D, 1) == cat(3, C, C)
@test unique(D, 3) == cat(3, B)

# With hash collisions
struct HashCollision
    x::Float64
end
Base.hash(::HashCollision, h::UInt) = h
@test map(x->x.x, unique(map(HashCollision, B), 1)) == C

@testset "large matrices transpose" begin
    for i = 1 : 3
        a = rand(200, 300)
        @test isequal(a', permutedims(a, [2, 1]))
    end
end

@testset "repmat and repeat" begin
    local A, A1, A2, A3, v, v2, cv, cv2, c, R, T
    A = ones(Int,2,3,4)
    A1 = reshape(repmat([1,2],1,12),2,3,4)
    A2 = reshape(repmat([1 2 3],2,4),2,3,4)
    A3 = reshape(repmat([1 2 3 4],6,1),2,3,4)
    @test isequal(cumsum(A),A1)
    @test isequal(cumsum(A,1),A1)
    @test isequal(cumsum(A,2),A2)
    @test isequal(cumsum(A,3),A3)

    # issue 20112
    A3 = reshape(repmat([1 2 3 4],UInt32(6),UInt32(1)),2,3,4)
    @test isequal(cumsum(A,3),A3)
    @test repmat([1,2,3,4], UInt32(1)) == [1,2,3,4]
    @test repmat([1 2], UInt32(2)) == repmat([1 2], UInt32(2), UInt32(1))

    # issue 20564
    @test_throws MethodError repmat(1, 2, 3)
    @test_throws MethodError repmat([1, 2], 1, 2, 3)

    R = repeat([1, 2])
    @test R == [1, 2]
    R = repeat([1, 2], inner=1)
    @test R == [1, 2]
    R = repeat([1, 2], outer=1)
    @test R == [1, 2]
    R = repeat([1, 2], inner=(1,))
    @test R == [1, 2]
    R = repeat([1, 2], outer=(1,))
    @test R == [1, 2]
    R = repeat([1, 2], inner=[1])
    @test R == [1, 2]
    R = repeat([1, 2], outer=[1])
    @test R == [1, 2]
    R = repeat([1, 2], inner=1, outer=1)
    @test R == [1, 2]
    R = repeat([1, 2], inner=(1,), outer=(1,))
    @test R == [1, 2]
    R = repeat([1, 2], inner=[1], outer=[1])
    @test R == [1, 2]

    R = repeat([1, 2], inner=2)
    @test R == [1, 1, 2, 2]
    R = repeat([1, 2], outer=2)
    @test R == [1, 2, 1, 2]
    R = repeat([1, 2], inner=(2,))
    @test R == [1, 1, 2, 2]
    R = repeat([1, 2], outer=(2,))
    @test R == [1, 2, 1, 2]
    R = repeat([1, 2], inner=[2])
    @test R == [1, 1, 2, 2]
    R = repeat([1, 2], outer=[2])
    @test R == [1, 2, 1, 2]

    R = repeat([1, 2], inner=2, outer=2)
    @test R == [1, 1, 2, 2, 1, 1, 2, 2]
    R = repeat([1, 2], inner=(2,), outer=(2,))
    @test R == [1, 1, 2, 2, 1, 1, 2, 2]
    R = repeat([1, 2], inner=[2], outer=[2])
    @test R == [1, 1, 2, 2, 1, 1, 2, 2]

    R = repeat([1, 2], inner = (1, 1), outer = (1, 1))
    @test R == reshape([1, 2], (2,1))
    R = repeat([1, 2], inner = (2, 1), outer = (1, 1))
    @test R == reshape([1, 1, 2, 2], (4,1))
    R = repeat([1, 2], inner = (1, 2), outer = (1, 1))
    @test R == [1 1; 2 2]
    R = repeat([1, 2], inner = (1, 1), outer = (2, 1))
    @test R == reshape([1, 2, 1, 2], (4,1))
    R = repeat([1, 2], inner = (1, 1), outer = (1, 2))
    @test R == [1 1; 2 2]

    R = repeat([1 2;
                3 4], inner = (1, 1), outer = (1, 1))
    @test R == [1 2;
                    3 4]
    R = repeat([1 2;
                3 4], inner = (1, 1), outer = (2, 1))
    @test R == [1 2;
                    3 4;
                    1 2;
                    3 4]
    R = repeat([1 2;
                3 4], inner = (1, 1), outer = (1, 2))
    @test R == [1 2 1 2;
                    3 4 3 4]
    R = repeat([1 2;
                3 4], inner = (1, 1), outer = (2, 2))
    @test R == [1 2 1 2;
                    3 4 3 4;
                    1 2 1 2;
                    3 4 3 4]
    R = repeat([1 2;
                3 4], inner = (2, 1), outer = (1, 1))
    @test R == [1 2;
                    1 2;
                    3 4;
                    3 4]
    R = repeat([1 2;
                3 4], inner = (2, 1), outer = (2, 1))
    @test R == [1 2;
                    1 2;
                    3 4;
                    3 4;
                    1 2;
                    1 2;
                    3 4;
                    3 4]
    R = repeat([1 2;
                3 4], inner = (2, 1), outer = (1, 2))
    @test R == [1 2 1 2;
                    1 2 1 2;
                    3 4 3 4;
                    3 4 3 4;]
    R = repeat([1 2;
                3 4], inner = (2, 1), outer = (2, 2))
    @test R == [1 2 1 2;
                    1 2 1 2;
                    3 4 3 4;
                    3 4 3 4;
                    1 2 1 2;
                    1 2 1 2;
                    3 4 3 4;
                    3 4 3 4]
    R = repeat([1 2;
                3 4], inner = (1, 2), outer = (1, 1))
    @test R == [1 1 2 2;
                    3 3 4 4]
    R = repeat([1 2;
                3 4], inner = (1, 2), outer = (2, 1))
    @test R == [1 1 2 2;
                    3 3 4 4;
                    1 1 2 2;
                    3 3 4 4]
    R = repeat([1 2;
                3 4], inner = (1, 2), outer = (1, 2))
    @test R == [1 1 2 2 1 1 2 2;
                    3 3 4 4 3 3 4 4]
    R = repeat([1 2;
                3 4], inner = (1, 2), outer = (2, 2))
    @test R == [1 1 2 2 1 1 2 2;
                    3 3 4 4 3 3 4 4;
                    1 1 2 2 1 1 2 2;
                    3 3 4 4 3 3 4 4]
    R = repeat([1 2;
                3 4], inner = (2, 2), outer = [1, 1])
    @test R == [1 1 2 2;
                    1 1 2 2;
                    3 3 4 4;
                    3 3 4 4]
    R = repeat([1 2;
                3 4], inner = (2, 2), outer = (2, 1))
    @test R == [1 1 2 2;
                    1 1 2 2;
                    3 3 4 4;
                    3 3 4 4;
                    1 1 2 2;
                    1 1 2 2;
                    3 3 4 4;
                    3 3 4 4]
    R = repeat([1 2;
                3 4], inner = (2, 2), outer = (1, 2))
    @test R == [1 1 2 2 1 1 2 2;
                    1 1 2 2 1 1 2 2;
                    3 3 4 4 3 3 4 4;
                    3 3 4 4 3 3 4 4]
    R = repeat([1 2;
                3 4], inner = (2, 2), outer = (2, 2))
    @test R == [1 1 2 2 1 1 2 2;
                    1 1 2 2 1 1 2 2;
                    3 3 4 4 3 3 4 4;
                    3 3 4 4 3 3 4 4;
                    1 1 2 2 1 1 2 2;
                    1 1 2 2 1 1 2 2;
                    3 3 4 4 3 3 4 4;
                    3 3 4 4 3 3 4 4]
    @test_throws ArgumentError repeat([1 2;
                                        3 4], inner=2, outer=(2, 2))
    @test_throws ArgumentError repeat([1 2;
                                        3 4], inner=(2, 2), outer=2)
    @test_throws ArgumentError repeat([1 2;
                                        3 4], inner=(2,), outer=(2, 2))
    @test_throws ArgumentError repeat([1 2;
                                        3 4], inner=(2, 2), outer=(2,))

    A = reshape(1:8, 2, 2, 2)
    R = repeat(A, inner = (1, 1, 2), outer = (1, 1, 1))
    T = reshape([1:4; 1:4; 5:8; 5:8], 2, 2, 4)
    @test R == T
    A = Array{Int}(2, 2, 2)
    A[:, :, 1] = [1 2;
                    3 4]
    A[:, :, 2] = [5 6;
                    7 8]
    R = repeat(A, inner = (2, 2, 2), outer = (2, 2, 2))
    @test R[1, 1, 1] == 1
    @test R[2, 2, 2] == 1
    @test R[3, 3, 3] == 8
    @test R[4, 4, 4] == 8
    @test R[5, 5, 5] == 1
    @test R[6, 6, 6] == 1
    @test R[7, 7, 7] == 8
    @test R[8, 8, 8] == 8

    R = repeat(1:2)
    @test R == [1, 2]
    R = repeat(1:2, inner=1)
    @test R == [1, 2]
    R = repeat(1:2, inner=2)
    @test R == [1, 1, 2, 2]
    R = repeat(1:2, outer=1)
    @test R == [1, 2]
    R = repeat(1:2, outer=2)
    @test R == [1, 2, 1, 2]
    R = repeat(1:2, inner=(3,), outer=(2,))
    @test R == [1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2]

    A = rand(4,4)
    for s in Any[A[1:2:4, 1:2:4], view(A, 1:2:4, 1:2:4)]
        c = cumsum(s, 1)
        @test c[1,1] == A[1,1]
        @test c[2,1] == A[1,1]+A[3,1]
        @test c[1,2] == A[1,3]
        @test c[2,2] == A[1,3]+A[3,3]

        c = cumsum(s, 2)
        @test c[1,1] == A[1,1]
        @test c[2,1] == A[3,1]
        @test c[1,2] == A[1,1]+A[1,3]
        @test c[2,2] == A[3,1]+A[3,3]
    end

    v   = [1,1e100,1,-1e100]*1000
    v2  = [1,-1e100,1,1e100]*1000

    cv  = [1,1e100,1e100,2]*1000
    cv2 = [1,-1e100,-1e100,2]*1000

    @test isequal(cumsum_kbn(v), cv)
    @test isequal(cumsum_kbn(v2), cv2)

    A = [v reverse(v) v2 reverse(v2)]

    c = cumsum_kbn(A, 1)

    @test isequal(c[:,1], cv)
    @test isequal(c[:,3], cv2)
    @test isequal(c[4,:], [2.0, 2.0, 2.0, 2.0]*1000)

    c = cumsum_kbn(A, 2)

    @test isequal(c[1,:], cv2)
    @test isequal(c[3,:], cv)
    @test isequal(c[:,4], [2.0,2.0,2.0,2.0]*1000)
end

@testset "indexing with bools" begin
    @test (1:5)[[true,false,true,false,true]] == [1,3,5]
    @test [1:5;][[true,false,true,false,true]] == [1,3,5]
    @test_throws BoundsError (1:5)[[true,false,true,false]]
    @test_throws BoundsError (1:5)[[true,false,true,false,true,false]]
    @test_throws BoundsError [1:5;][[true,false,true,false]]
    @test_throws BoundsError [1:5;][[true,false,true,false,true,false]]
    a = [1:5;]
    a[[true,false,true,false,true]] = 6
    @test a == [6,2,6,4,6]
    a[[true,false,true,false,true]] = [7,8,9]
    @test a == [7,2,8,4,9]
    @test_throws DimensionMismatch (a[[true,false,true,false,true]] = [7,8,9,10])
    A = reshape(1:15, 3, 5)
    @test A[[true, false, true], [false, false, true, true, false]] == [7 10; 9 12]
    @test_throws BoundsError A[[true, false], [false, false, true, true, false]]
    @test_throws BoundsError A[[true, false, true], [false, true, true, false]]
    @test_throws BoundsError A[[true, false, true, true], [false, false, true, true, false]]
    @test_throws BoundsError A[[true, false, true], [false, false, true, true, false, true]]
    A = ones(Int, 3, 5)
    @test_throws DimensionMismatch A[2,[true, false, true, true, false]] = 2:5
    A[2,[true, false, true, true, false]] = 2:4
    @test A == [1 1 1 1 1; 2 1 3 4 1; 1 1 1 1 1]
    @test_throws DimensionMismatch A[[true,false,true], 5] = [19]
    @test_throws DimensionMismatch A[[true,false,true], 5] = 19:21
    A[[true,false,true], 5] = 7
    @test A == [1 1 1 1 7; 2 1 3 4 1; 1 1 1 1 7]

    B = cat(3, 1, 2, 3)
    @test B[:,:,[true, false, true]] == reshape([1,3], 1, 1, 2)  # issue #5454
end

# issue #2342
@test isequal(cumsum([1 2 3]), [1 2 3])

@testset "set-like operations" begin
    @test isequal(union([1,2,3], [4,3,4]), [1,2,3,4])
    @test isequal(union(['e','c','a'], ['b','a','d']), ['e','c','a','b','d'])
    @test isequal(union([1,2,3], [4,3], [5]), [1,2,3,4,5])
    @test isequal(union([1,2,3]), [1,2,3])
    @test isequal(union([1,2,3], Int64[]), Int64[1,2,3])
    @test isequal(union([1,2,3], Float64[]), Float64[1.0,2,3])
    @test isequal(union(Int64[], [1,2,3]), Int64[1,2,3])
    @test isequal(union(Int64[]), Int64[])
    @test isequal(intersect([1,2,3], [4,3,4]), [3])
    @test isequal(intersect(['e','c','a'], ['b','a','d']), ['a'])
    @test isequal(intersect([1,2,3], [3,1], [2,1,3]), [1,3])
    @test isequal(intersect([1,2,3]), [1,2,3])
    @test isequal(intersect([1,2,3], Int64[]), Int64[])
    @test isequal(intersect([1,2,3], Float64[]), Float64[])
    @test isequal(intersect(Int64[], [1,2,3]), Int64[])
    @test isequal(intersect(Int64[]), Int64[])
    @test isequal(setdiff([1,2,3,4], [2,5,4]), [1,3])
    @test isequal(setdiff([1,2,3,4], [7,8,9]), [1,2,3,4])
    @test isequal(setdiff([1,2,3,4], Int64[]), Int64[1,2,3,4])
    @test isequal(setdiff([1,2,3,4], [1,2,3,4,5]), Int64[])
    @test isequal(symdiff([1,2,3], [4,3,4]), [1,2,4])
    @test isequal(symdiff(['e','c','a'], ['b','a','d']), ['e','c','b','d'])
    @test isequal(symdiff([1,2,3], [4,3], [5]), [1,2,4,5])
    @test isequal(symdiff([1,2,3,4,5], [1,2,3], [3,4]), [3,5])
    @test isequal(symdiff([1,2,3]), [1,2,3])
    @test isequal(symdiff([1,2,3], Int64[]), Int64[1,2,3])
    @test isequal(symdiff([1,2,3], Float64[]), Float64[1.0,2,3])
    @test isequal(symdiff(Int64[], [1,2,3]), Int64[1,2,3])
    @test isequal(symdiff(Int64[]), Int64[])
end

@testset "mapslices" begin
    local a,h,i
    a = rand(5,5)
    s = mapslices(sort, a, [1])
    S = mapslices(sort, a, [2])
    for i = 1:5
        @test s[:,i] == sort(a[:,i])
        @test vec(S[i,:]) == sort(vec(a[i,:]))
    end

    # issue #3613
    b = mapslices(sum, ones(2,3,4), [1,2])
    @test size(b) === (1,1,4)
    @test all(b.==6)

    # issue #5141
    ## Update Removed the version that removes the dimensions when dims==1:ndims(A)
    c1 = mapslices(x-> maximum(-x), a, [])
    @test c1 == -a

    # other types than Number
    @test mapslices(prod,["1" "2"; "3" "4"],1) == ["13" "24"]
    @test mapslices(prod,["1"],1) == ["1"]

    # issue #5177

    c = ones(2,3,4)
    m1 = mapslices(x-> ones(2,3), c, [1,2])
    m2 = mapslices(x-> ones(2,4), c, [1,3])
    m3 = mapslices(x-> ones(3,4), c, [2,3])
    @test size(m1) == size(m2) == size(m3) == size(c)

    n1 = mapslices(x-> ones(6), c, [1,2])
    n2 = mapslices(x-> ones(6), c, [1,3])
    n3 = mapslices(x-> ones(6), c, [2,3])
    n1a = mapslices(x-> ones(1,6), c, [1,2])
    n2a = mapslices(x-> ones(1,6), c, [1,3])
    n3a = mapslices(x-> ones(1,6), c, [2,3])
    @test size(n1a) == (1,6,4) && size(n2a) == (1,3,6)  && size(n3a) == (2,1,6)
    @test size(n1) == (6,1,4) && size(n2) == (6,3,1)  && size(n3) == (2,6,1)

    # mutating functions
    o = ones(3, 4)
    m = mapslices(x->fill!(x, 0), o, 2)
    @test m == zeros(3, 4)
    @test o == ones(3, 4)
end

@testset "single multidimensional index" begin
    a = rand(6,6)
    I = [1 4 5; 4 2 6; 5 6 3]
    a2 = a[I]
    @test size(a2) == size(I)
    for i = 1:length(a2)
        @test a2[i] == a[I[i]]
    end
    a = [1,3,5]
    b = [1 3]
    a[b] = 8
    @test a == [8,3,8]
end

@testset "assigning an array into itself" begin
    a = [1,3,5]
    b = [3,1,2]
    a[b] = a
    @test a == [3,5,1]
    a = [3,2,1]
    a[a] = [4,5,6]
    @test a == [6,5,4]
end

@testset "lexicographic comparison" begin
    @test lexcmp([1.0], [1]) == 0
    @test lexcmp([1], [1.0]) == 0
    @test lexcmp([1, 1], [1, 1]) == 0
    @test lexcmp([1, 1], [2, 1]) == -1
    @test lexcmp([2, 1], [1, 1]) == 1
    @test lexcmp([1, 1], [1, 2]) == -1
    @test lexcmp([1, 2], [1, 1]) == 1
    @test lexcmp([1], [1, 1]) == -1
    @test lexcmp([1, 1], [1]) == 1
end

@testset "sort on arrays" begin
    local a = rand(3,3)

    asr = sortrows(a)
    @test lexless(asr[1,:],asr[2,:])
    @test lexless(asr[2,:],asr[3,:])

    asc = sortcols(a)
    @test lexless(asc[:,1],asc[:,2])
    @test lexless(asc[:,2],asc[:,3])

    # mutating functions
    o = ones(3, 4)
    m = mapslices(x->fill!(x, 0), o, 2)
    @test m == zeros(3, 4)
    @test o == ones(3, 4)

    # issue #18524
    m = mapslices(x->tuple(x), [1 2; 3 4], 1)
    @test m[1,1] == ([1,3],)
    @test m[1,2] == ([2,4],)

    asr = sortrows(a, rev=true)
    @test lexless(asr[2,:],asr[1,:])
    @test lexless(asr[3,:],asr[2,:])

    asc = sortcols(a, rev=true)
    @test lexless(asc[:,2],asc[:,1])
    @test lexless(asc[:,3],asc[:,2])

    as = sort(a, 1)
    @test issorted(as[:,1])
    @test issorted(as[:,2])
    @test issorted(as[:,3])

    as = sort(a, 2)
    @test issorted(as[1,:])
    @test issorted(as[2,:])
    @test issorted(as[3,:])

    local b = rand(21,21,2)

    bs = sort(b, 1)
    for i in 1:21
        @test issorted(bs[:,i,1])
        @test issorted(bs[:,i,2])
    end

    bs = sort(b, 2)
    for i in 1:21
        @test issorted(bs[i,:,1])
        @test issorted(bs[i,:,2])
    end

    bs = sort(b, 3)
    @test all(bs[:,:,1] .<= bs[:,:,2])
end

@testset "fill" begin
    @test fill!(Array{Float64}(1),-0.0)[1] === -0.0
    A = ones(3,3)
    S = view(A, 2, 1:3)
    fill!(S, 2)
    S = view(A, 1:2, 3)
    fill!(S, 3)
    @test A == [1 1 3; 2 2 3; 1 1 1]
    rt = Base.return_types(fill!, Tuple{Array{Int32, 3}, UInt8})
    @test length(rt) == 1 && rt[1] == Array{Int32, 3}
    A = Array{Union{UInt8,Int8}}(3)
    fill!(A, UInt8(3))
    @test A == [0x03, 0x03, 0x03]
    # Issue #9964
    A = Array{Vector{Float64}}(2)
    fill!(A, [1, 2])
    @test A[1] == [1, 2]
    @test A[1] === A[2]
end

@testset "splice!" begin
    for idx in Any[1, 2, 5, 9, 10, 1:0, 2:1, 1:1, 2:2, 1:2, 2:4, 9:8, 10:9, 9:9, 10:10,
                   8:9, 9:10, 6:9, 7:10]
        for repl in Any[[], [11], [11,22], [11,22,33,44,55]]
            a = [1:10;]; acopy = copy(a)
            @test splice!(a, idx, repl) == acopy[idx]
            @test a == [acopy[1:(first(idx)-1)]; repl; acopy[(last(idx)+1):end]]
        end
    end
end

@testset "deleteat!" begin
    for idx in Any[1, 2, 5, 9, 10, 1:0, 2:1, 1:1, 2:2, 1:2, 2:4, 9:8, 10:9, 9:9, 10:10,
                   8:9, 9:10, 6:9, 7:10]
        # integer indexing with AbstractArray
        a = [1:10;]; acopy = copy(a)
        @test deleteat!(a, idx) == [acopy[1:(first(idx)-1)]; acopy[(last(idx)+1):end]]

        # integer indexing with non-AbstractArray iterable
        a = [1:10;]; acopy = copy(a)
        @test deleteat!(a, (i for i in idx)) == [acopy[1:(first(idx)-1)]; acopy[(last(idx)+1):end]]

        # logical indexing
        a = [1:10;]; acopy = copy(a)
        @test deleteat!(a, map(i -> i in idx, 1:length(a))) == [acopy[1:(first(idx)-1)]; acopy[(last(idx)+1):end]]
    end
    a = [1:10;]
    @test deleteat!(a, 11:10) == [1:10;]
    @test deleteat!(a, [1,3,5,7:10...]) == [2,4,6]
    @test_throws BoundsError deleteat!(a, 13)
    @test_throws BoundsError deleteat!(a, [1,13])
    @test_throws ArgumentError deleteat!(a, [5,3])
    @test_throws BoundsError deleteat!(a, 5:20)
    @test_throws BoundsError deleteat!(a, Bool[])
    @test_throws BoundsError deleteat!(a, [true])
    @test_throws BoundsError deleteat!(a, falses(11))
end

@testset "comprehensions" begin
    X = [ i+2j for i=1:5, j=1:5 ]
    @test X[2,3] == 8
    @test X[4,5] == 14
    @test isequal(ones(2,3) * ones(2,3)', [3. 3.; 3. 3.])
    # @test isequal([ [1,2] for i=1:2, : ], [1 2; 1 2])
    # where element type is a Union. try to confuse type inference.
    foo32_64(x) = (x<2) ? Int32(x) : Int64(x)
    boo32_64() = [ foo32_64(i) for i=1:2 ]
    let a36 = boo32_64()
        @test a36[1]==1 && a36[2]==2
    end
    @test isequal([1,2,3], [a for (a,b) in enumerate(2:4)])
    @test isequal([2,3,4], [b for (a,b) in enumerate(2:4)])

    @testset "comprehension in let-bound function" begin
        let x⊙y = sum([x[i]*y[i] for i=1:length(x)])
            @test [1,2] ⊙ [3,4] == 11
        end

        @test_throws DomainError (10.^[-1])[1] == 0.1
        @test (10.^[-1.])[1] == 0.1
    end
end

@testset "eachindexvalue" begin
    A14 = [11 13; 12 14]
    R = CartesianRange(indices(A14))
    @test [a for (a,b) in enumerate(IndexLinear(),    A14)] == [1,2,3,4]
    @test [a for (a,b) in enumerate(IndexCartesian(), A14)] == vec(collect(R))
    @test [b for (a,b) in enumerate(IndexLinear(),    A14)] == [11,12,13,14]
    @test [b for (a,b) in enumerate(IndexCartesian(), A14)] == [11,12,13,14]
end

@testset "reverse" begin
    @test reverse([2,3,1]) == [1,3,2]
    @test reverse([1:10;],1,4) == [4,3,2,1,5,6,7,8,9,10]
    @test reverse([1:10;],3,6) == [1,2,6,5,4,3,7,8,9,10]
    @test reverse([1:10;],6,10) == [1,2,3,4,5,10,9,8,7,6]
    @test reverse(1:10,1,4) == [4,3,2,1,5,6,7,8,9,10]
    @test reverse(1:10,3,6) == [1,2,6,5,4,3,7,8,9,10]
    @test reverse(1:10,6,10) == [1,2,3,4,5,10,9,8,7,6]
    @test reverse!([1:10;]) == [10,9,8,7,6,5,4,3,2,1]
    @test reverse!([1:10;],1,4) == [4,3,2,1,5,6,7,8,9,10]
    @test reverse!([1:10;],3,6) == [1,2,6,5,4,3,7,8,9,10]
    @test reverse!([1:10;],6,10) == [1,2,3,4,5,10,9,8,7,6]
    @test reverse!([1:10;], 11) == [1:10;]
    @test_throws BoundsError reverse!([1:10;], 1, 11)
    @test reverse!(Any[]) == Any[]
end

@testset "flipdim" begin
    @test isequal(flipdim([2,3,1], 1), [1,3,2])
    @test_throws ArgumentError flipdim([2,3,1], 2)
    @test isequal(flipdim([2 3 1], 1), [2 3 1])
    @test isequal(flipdim([2 3 1], 2), [1 3 2])
    @test_throws ArgumentError flipdim([2,3,1], -1)
    @test isequal(flipdim(1:10, 1), 10:-1:1)
    @test_throws ArgumentError flipdim(1:10, 2)
    @test_throws ArgumentError flipdim(1:10, -1)
    @test isequal(flipdim(Array{Int}(0,0),1), Array{Int}(0,0))  # issue #5872

    a = rand(5,3)
    @test flipdim(flipdim(a,2),2) == a
    @test_throws ArgumentError flipdim(a,3)
end

@testset "isdiag, istril, istriu" begin
    @test isdiag(3)
    @test istril(4)
    @test istriu(5)
    @test !isdiag([1 2; 3 4])
    @test !istril([1 2; 3 4])
    @test !istriu([1 2; 3 4])
    @test isdiag([1 0; 0 4])
    @test istril([1 0; 3 4])
    @test istriu([1 2; 0 4])
end

# issue 4228
A = [[i i; i i] for i=1:2]
@test cumsum(A) == Any[[1 1; 1 1], [3 3; 3 3]]
@test cumprod(A) == Any[[1 1; 1 1], [4 4; 4 4]]

isdefined(Main, :TestHelpers) || @eval Main include("TestHelpers.jl")
using TestHelpers.OAs

@testset "prepend/append" begin
    # PR #4627
    A = [1,2]
    @test append!(A, A) == [1,2,1,2]
    @test prepend!(A, A) == [1,2,1,2,1,2,1,2]

    # iterators with length:
    @test append!([1,2], (9,8)) == [1,2,9,8] == push!([1,2], (9,8)...)
    @test prepend!([1,2], (9,8)) == [9,8,1,2] == unshift!([1,2], (9,8)...)
    @test append!([1,2], ()) == [1,2] == prepend!([1,2], ())
    # iterators without length:
    g = (i for i = 1:10 if iseven(i))
    @test append!([1,2], g) == [1,2,2,4,6,8,10] == push!([1,2], g...)
    @test prepend!([1,2], g) == [2,4,6,8,10,1,2] == unshift!([1,2], g...)
    g = (i for i = 1:2:10 if iseven(i)) # isempty(g) == true
    @test append!([1,2], g) == [1,2] == push!([1,2], g...)
    @test prepend!([1,2], g) == [1,2] == unshift!([1,2], g...)

    # offset array
    @test append!([1,2], OffsetArray([9,8], (-3,))) == [1,2,9,8]
    @test prepend!([1,2], OffsetArray([9,8], (-3,))) == [9,8,1,2]
end

A = [1,2]
s = Set([1,2,3])
@test sort(append!(A, s)) == [1,1,2,2,3]

@testset "cases where shared arrays can/can't be grown" begin
    A = [1 3;2 4]
    B = reshape(A, 4)
    @test push!(B,5) == [1,2,3,4,5]
    @test pop!(B) == 5
    C = reshape(B, 1, 4)
    @test_throws MethodError push!(C, 5)

    A = [NaN]; B = [NaN]
    @test !(A==A)
    @test isequal(A,A)
    @test A===A
    @test !(A==B)
    @test isequal(A,B)
    @test A!==B
end
# complete testsuite for reducedim

# Inferred types
Nmax = 3 # TODO: go up to CARTESIAN_DIMS+2 (currently this exposes problems)
for N = 1:Nmax
    #indexing with (UnitRange, UnitRange, UnitRange)
    args = ntuple(d->UnitRange{Int}, N)
    @test Base.return_types(getindex, Tuple{Array{Float32, N}, args...}) == [Array{Float32, N}]
    @test Base.return_types(getindex, Tuple{BitArray{N}, args...}) == Any[BitArray{N}]
    @test Base.return_types(setindex!, Tuple{Array{Float32, N}, Array{Int, 1}, args...}) == [Array{Float32, N}]
    # Indexing with (UnitRange, UnitRange, Int)
    args = ntuple(d->d<N ? UnitRange{Int} : Int, N)
    N > 1 && @test Base.return_types(getindex, Tuple{Array{Float32, N}, args...}) == [Array{Float32, N-1}]
    N > 1 && @test Base.return_types(getindex, Tuple{BitArray{N}, args...}) == [BitArray{N-1}]
    N > 1 && @test Base.return_types(setindex!, Tuple{Array{Float32, N}, Array{Int, 1}, args...}) == [Array{Float32, N}]
end

# issue #6645 (32-bit)
let x = Float64[]
    for i=1:5; push!(x, 1.0); end
    @test dot(zeros(5),x) == 0.0
end

# issue #6977
@test size([]') == (1,0)

# issue #6996
@test Any[ 1 2; 3 4 ]' == Any[ 1 2; 3 4 ].'

# map with promotion (issue #6541)
@test map(join, ["z", "я"]) == ["z", "я"]

# Handle block matrices
A = [randn(2,2) for i = 1:2, j = 1:2]
@test issymmetric(A.'A)
A = [complex.(randn(2,2), randn(2,2)) for i = 1:2, j = 1:2]
@test ishermitian(A'A)

# issue #7197
function i7197()
    S = [1 2 3; 4 5 6; 7 8 9]
    ind2sub(size(S), 5)
end
@test i7197() == (2,2)

# PR #8622 and general indexin test
function pr8622()
    x=[1,3,5,7]
    y=[5,4,3]
    return indexin(x,y)
end
@test pr8622() == [0,3,1,0]

#6828 - size of specific dimensions
let a = Array{Float64}(10)
    @test size(a) == (10,)
    @test size(a, 1) == 10
    @test size(a,2,1) == (1,10)
    aa = Array{Float64}(2,3)
    @test size(aa) == (2,3)
    @test size(aa,4,3,2,1) == (1,1,3,2)
    @test size(aa,1,2) == (2,3)
    aaa = Array{Float64}(9,8,7,6,5,4,3,2,1)
    @test size(aaa,1,1) == (9,9)
    @test size(aaa,4) == 6
    @test size(aaa,9,8,7,6,5,4,3,2,19,8,7,6,5,4,3,2,1) == (1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,9)

    #18459 Test Array{T, N} constructor
    b = Array{Float64, 1}(10)
    @test size(a) == size(b)
    bb = Array{Float64, 2}(2,3)
    @test size(aa) == size(bb)
    bbb = Array{Float64, 9}(9,8,7,6,5,4,3,2,1)
    @test size(aaa) == size(bbb)
end

# Cartesian
function cartesian_foo()
    Base.@nexprs 2 d->(a_d_d = d)
    a_2_2
end
@test cartesian_foo() == 2

@testset "Multidimensional iterators" begin
    for a in ([1:5;], reshape([2]))
        counter = 0
        for I in eachindex(a)
            counter += 1
        end
        @test counter == length(a)
        counter = 0
        for aa in a
            counter += 1
        end
        @test counter == length(a)
    end
end

function mdsum(A)
    s = 0.0
    for a in A
        s += a
    end
    s
end

function mdsum2(A)
    s = 0.0
    @inbounds for I in eachindex(A)
        s += A[I]
    end
    s
end

@testset "linear indexing" begin
    a = [1:5;]
    @test isa(Base.IndexStyle(a), Base.IndexLinear)
    b = view(a, :)
    @test isa(Base.IndexStyle(b), Base.IndexLinear)
    @test isa(Base.IndexStyle(trues(2)), Base.IndexLinear)
    @test isa(Base.IndexStyle(BitArray{2}), Base.IndexLinear)
    aa = fill(99, 10)
    aa[1:2:9] = a
    shp = [5]
    for i = 1:10
        A = reshape(a, tuple(shp...))
        @test mdsum(A) == 15
        @test mdsum2(A) == 15
        AA = reshape(aa, tuple(2, shp...))
        B = view(AA, 1:1, ntuple(i->Colon(), i)...)
        @test isa(Base.IndexStyle(B), Base.IteratorsMD.IndexCartesian)
        @test mdsum(B) == 15
        @test mdsum2(B) == 15
        unshift!(shp, 1)
    end

    a = [1:10;]
    shp = [2,5]
    for i = 2:10
        A = reshape(a, tuple(shp...))
        @test mdsum(A) == 55
        @test mdsum2(A) == 55
        B = view(A, ntuple(i->Colon(), i)...)
        @test mdsum(B) == 55
        @test mdsum2(B) == 55
        insert!(shp, 2, 1)
    end

    a = reshape([2])
    @test mdsum(a) == 2
    @test mdsum2(a) == 2

    a = ones(0,5)
    b = view(a, :, :)
    @test mdsum(b) == 0
    a = ones(5,0)
    b = view(a, :, :)
    @test mdsum(b) == 0
end
@testset "CartesianIndex" begin
    for a in (copy(reshape(1:60, 3, 4, 5)),
              view(copy(reshape(1:60, 3, 4, 5)), 1:3, :, :),
              view(copy(reshape(1:60, 3, 4, 5)), CartesianIndex.(1:3, (1:4)'), :),
              view(copy(reshape(1:60, 3, 4, 5)), :, CartesianIndex.(1:4, (1:5)')))
        @test a[CartesianIndex{3}(2,3,4)] == 44
        a[CartesianIndex{3}(2,3,3)] = -1
        @test a[CartesianIndex{3}(2,3,3)] == -1
        @test a[2,CartesianIndex{2}(3,4)] == 44
        a[1,CartesianIndex{2}(3,4)] = -2
        @test a[1,CartesianIndex{2}(3,4)] == -2
        @test a[CartesianIndex{1}(2),3,CartesianIndex{1}(4)] == 44
        a[CartesianIndex{1}(2),3,CartesianIndex{1}(3)] = -3
        @test a[CartesianIndex{1}(2),3,CartesianIndex{1}(3)] == -3

        @test a[:, :, CartesianIndex((1,))] == (@view a[:, :, CartesianIndex((1,))]) == a[:,:,1]
        @test a[CartesianIndex((1,)), [1,2], :] == (@view a[CartesianIndex((1,)), [1,2], :]) == a[1,[1,2],:]
        @test a[CartesianIndex((2,)), 3:4, :] == (@view a[CartesianIndex((2,)), 3:4, :]) == a[2,3:4,:]
        @test a[[CartesianIndex(1,3),CartesianIndex(2,4)],3:3] ==
              (@view a[[CartesianIndex(1,3),CartesianIndex(2,4)],3:3]) == reshape([a[1,3,3]; a[2,4,3]], 2, 1)

        @test a[[CartesianIndex()], :, :, :] == (@view a[[CartesianIndex()], :, :, :]) == reshape(a, 1, 3, 4, 5)
        @test a[:, [CartesianIndex()], :, :] == (@view a[:, [CartesianIndex()], :, :]) == reshape(a, 3, 1, 4, 5)
        @test a[:, :, [CartesianIndex()], :] == (@view a[:, :, [CartesianIndex()], :]) == reshape(a, 3, 4, 1, 5)
        @test a[:, :, :, [CartesianIndex()]] == (@view a[:, :, :, [CartesianIndex()]]) == reshape(a, 3, 4, 5, 1)
        a2 = reshape(a, Val{2})
        @test a2[[CartesianIndex()], :, :]   == (@view a2[[CartesianIndex()], :, :])   == reshape(a, 1, 3, 20)
        @test a2[:, [CartesianIndex()], :]   == (@view a2[:, [CartesianIndex()], :])   == reshape(a, 3, 1, 20)
        @test a2[:, :, [CartesianIndex()]]   == (@view a2[:, :, [CartesianIndex()]])   == reshape(a, 3, 20, 1)
        a1 = reshape(a, Val{1})
        @test a1[[CartesianIndex()], :]      == (@view a1[[CartesianIndex()], :])      == reshape(a, 1, 60)
        @test a1[:, [CartesianIndex()]]      == (@view a1[:, [CartesianIndex()]])      == reshape(a, 60, 1)

        @test_throws BoundsError a[[CartesianIndex(1,5),CartesianIndex(2,4)],3:3]
        @test_throws BoundsError a[1:4, [CartesianIndex(1,3),CartesianIndex(2,4)]]
        @test_throws BoundsError @view a[[CartesianIndex(1,5),CartesianIndex(2,4)],3:3]
        @test_throws BoundsError @view a[1:4, [CartesianIndex(1,3),CartesianIndex(2,4)]]
    end

    for a in (view(zeros(3, 4, 5), :, :, :),
              view(zeros(3, 4, 5), 1:3, :, :))
        a[CartesianIndex{3}(2,3,3)] = -1
        @test a[CartesianIndex{3}(2,3,3)] == -1
        a[1,CartesianIndex{2}(3,4)] = -2
        @test a[1,CartesianIndex{2}(3,4)] == -2
        a[CartesianIndex{1}(2),3,CartesianIndex{1}(3)] = -3
        @test a[CartesianIndex{1}(2),3,CartesianIndex{1}(3)] == -3
        a[[CartesianIndex(1,3),CartesianIndex(2,4)],3:3] = -4
        @test a[1,3,3] == -4
        @test a[2,4,3] == -4
    end

    I1 = CartesianIndex((2,3,0))
    I2 = CartesianIndex((-1,5,2))
    @test -I1 == CartesianIndex((-2,-3,0))
    @test I1 + I2 == CartesianIndex((1,8,2))
    @test I2 + I1 == CartesianIndex((1,8,2))
    @test I1 - I2 == CartesianIndex((3,-2,-2))
    @test I2 - I1 == CartesianIndex((-3,2,2))
    @test I1 + 1 == CartesianIndex((3,4,1))
    @test I1 - 2 == CartesianIndex((0,1,-2))

    @test zero(CartesianIndex{2}) == CartesianIndex((0,0))
    @test zero(CartesianIndex((2,3))) == CartesianIndex((0,0))
    @test one(CartesianIndex{2}) == CartesianIndex((1,1))
    @test one(CartesianIndex((2,3))) == CartesianIndex((1,1))

    @test min(CartesianIndex((2,3)), CartesianIndex((5,2))) == CartesianIndex((2,2))
    @test max(CartesianIndex((2,3)), CartesianIndex((5,2))) == CartesianIndex((5,3))

    # CartesianIndex allows construction at a particular dimensionality
    @test length(CartesianIndex{3}()) == 3
    @test length(CartesianIndex{3}(1,2)) == 3
    @test length(CartesianIndex{3}((1,2))) == 3
    @test length(CartesianIndex{3}(1,2,3)) == 3
    @test length(CartesianIndex{3}((1,2,3))) == 3
    @test_throws ArgumentError CartesianIndex{3}(1,2,3,4)
    @test_throws ArgumentError CartesianIndex{3}((1,2,3,4))

    @test length(I1) == 3

    @test isless(CartesianIndex((1,1)), CartesianIndex((2,1)))
    @test isless(CartesianIndex((1,1)), CartesianIndex((1,2)))
    @test isless(CartesianIndex((2,1)), CartesianIndex((1,2)))
    @test !isless(CartesianIndex((1,2)), CartesianIndex((2,1)))

    a = spzeros(2,3)
    @test CartesianRange(size(a)) == eachindex(a)
    a[CartesianIndex{2}(2,3)] = 5
    @test a[2,3] == 5
    b = view(a, 1:2, 2:3)
    b[CartesianIndex{2}(1,1)] = 7
    @test a[1,2] == 7
    @test 2*CartesianIndex{3}(1,2,3) == CartesianIndex{3}(2,4,6)

    R = CartesianRange(CartesianIndex{2}(2,3),CartesianIndex{2}(5,5))
    @test eltype(R) <: CartesianIndex{2}
    @test eltype(typeof(R)) <: CartesianIndex{2}
    indexes = collect(R)
    @test indexes[1] == CartesianIndex{2}(2,3)
    @test indexes[2] == CartesianIndex{2}(3,3)
    @test indexes[4] == CartesianIndex{2}(5,3)
    @test indexes[5] == CartesianIndex{2}(2,4)
    @test indexes[12] == CartesianIndex{2}(5,5)
    @test length(indexes) == 12
    @test length(R) == 12
    @test ndims(R) == 2
    @test in(CartesianIndex((2,3)), R)
    @test in(CartesianIndex((3,3)), R)
    @test in(CartesianIndex((3,5)), R)
    @test in(CartesianIndex((5,5)), R)
    @test !in(CartesianIndex((1,3)), R)
    @test !in(CartesianIndex((3,2)), R)
    @test !in(CartesianIndex((3,6)), R)
    @test !in(CartesianIndex((6,5)), R)

    @test @inferred(convert(NTuple{2,UnitRange}, R)) === (2:5, 3:5)
    @test @inferred(convert(Tuple{Vararg{UnitRange}}, R)) === (2:5, 3:5)

    @test CartesianRange((3:5,-7:7)) == CartesianRange(CartesianIndex{2}(3,-7),CartesianIndex{2}(5,7))
    @test CartesianRange((3,-7:7)) == CartesianRange(CartesianIndex{2}(3,-7),CartesianIndex{2}(3,7))
end

@testset "itr, start, done, next" begin
    r = 2:3
    itr = eachindex(r)
    state = start(itr)
    @test !done(itr, state)
    _, state = next(itr, state)
    @test !done(itr, state)
    val, state = next(itr, state)
    @test done(itr, state)
    @test r[val] == 3
    r = sparse(collect(2:3:8))
    itr = eachindex(r)
    state = start(itr)
    @test !done(itr, state)
    _, state = next(itr, state)
    _, state = next(itr, state)
    @test !done(itr, state)
    val, state = next(itr, state)
    @test r[val] == 8
    @test done(itr, state)
end

R = CartesianRange((1,3))
@test done(R, start(R)) == false
R = CartesianRange((0,3))
@test done(R, start(R)) == true
R = CartesianRange((3,0))
@test done(R, start(R)) == true

@test @inferred(eachindex(Base.IndexCartesian(),zeros(3),zeros(2,2),zeros(2,2,2),zeros(2,2))) == CartesianRange((3,2,2))
@test @inferred(eachindex(Base.IndexLinear(),zeros(3),zeros(2,2),zeros(2,2,2),zeros(2,2))) == 1:8
@test @inferred(eachindex(zeros(3),view(zeros(3,3),1:2,1:2),zeros(2,2,2),zeros(2,2))) == CartesianRange((3,2,2))
@test @inferred(eachindex(zeros(3),zeros(2,2),zeros(2,2,2),zeros(2,2))) == 1:8

@testset "rotates" begin
    a = [1 0 0; 0 0 0]
    @test rotr90(a,1) == [0 1; 0 0; 0 0]
    @test rotr90(a,2) == rot180(a,1)
    @test rotr90(a,3) == rotl90(a,1)
    @test rotl90(a,3) == rotr90(a,1)
    @test rotl90(a,1) == rotr90(a,3)
    @test rotl90(a,4) == a
    @test rotr90(a,4) == a
    @test rot180(a,2) == a
end

# issue #9648
let x = fill(1.5f0, 10^7)
    @test abs(1.5f7 - cumsum(x)[end]) < 3*eps(1.5f7)
    @test cumsum(x) == cumsum!(similar(x), x)
end

# PR #10164
@test eltype(Array{Int}) == Int
@test eltype(Array{Int,1}) == Int

# PR #11080
let x = fill(0.9, 1000)
    @test prod(x) ≈ cumprod(x)[end]
end

@testset "binary ops on bool arrays" begin
    A = Array(trues(5))
    @test A + true == [2,2,2,2,2]
    A = Array(trues(5))
    @test A + false == [1,1,1,1,1]
    A = Array(trues(5))
    @test true + A == [2,2,2,2,2]
    A = Array(trues(5))
    @test false + A == [1,1,1,1,1]
    A = Array(trues(5))
    @test A - true == [0,0,0,0,0]
    A = Array(trues(5))
    @test A - false == [1,1,1,1,1]
    A = Array(trues(5))
    @test true - A == [0,0,0,0,0]
    A = Array(trues(5))
    @test false - A == [-1,-1,-1,-1,-1]
end

@testset "simple transposes" begin
    a = ones(Complex,1,5)
    b = zeros(Complex,5)
    c = ones(Complex,2,5)
    d = ones(Complex,6)
    @test_throws DimensionMismatch transpose!(a,d)
    @test_throws DimensionMismatch transpose!(d,a)
    @test_throws DimensionMismatch ctranspose!(a,d)
    @test_throws DimensionMismatch ctranspose!(d,a)
    @test_throws DimensionMismatch transpose!(b,c)
    @test_throws DimensionMismatch ctranspose!(b,c)
    @test_throws DimensionMismatch transpose!(c,b)
    @test_throws DimensionMismatch ctranspose!(c,b)
    transpose!(b,a)
    @test b == ones(Complex,5)
    b = ones(Complex,5)
    a = zeros(Complex,1,5)
    transpose!(a,b)
    @test a == ones(Complex,1,5)
    b = zeros(Complex,5)
    ctranspose!(b,a)
    @test b == ones(Complex,5)
    a = zeros(Complex,1,5)
    ctranspose!(a,b)
    @test a == ones(Complex,1,5)
end

@testset "bounds checking for copy!" begin
    a = rand(5,3)
    b = rand(6,7)
    @test_throws BoundsError copy!(a,b)
    @test_throws ArgumentError copy!(a,2:3,1:3,b,1:5,2:7)
    @test_throws ArgumentError Base.copy_transpose!(a,2:3,1:3,b,1:5,2:7)
end

module RetTypeDecl
    using Base.Test
    import Base: +, *, broadcast, convert

    struct MeterUnits{T,P} <: Number
        val::T
    end
    MeterUnits{T}(val::T, pow::Int) = MeterUnits{T,pow}(val)

    m  = MeterUnits(1.0, 1)   # 1.0 meter, i.e. units of length
    m2 = MeterUnits(1.0, 2)   # 1.0 meter^2, i.e. units of area

    (+){T,pow}(x::MeterUnits{T,pow}, y::MeterUnits{T,pow}) = MeterUnits{T,pow}(x.val+y.val)
    (*){T,pow}(x::Int, y::MeterUnits{T,pow}) = MeterUnits{typeof(x*one(T)),pow}(x*y.val)
    (*){T}(x::MeterUnits{T,1}, y::MeterUnits{T,1}) = MeterUnits{T,2}(x.val*y.val)
    broadcast{T}(::typeof(*), x::MeterUnits{T,1}, y::MeterUnits{T,1}) = MeterUnits{T,2}(x.val*y.val)
    convert{T,pow}(::Type{MeterUnits{T,pow}}, y::Real) = MeterUnits{T,pow}(convert(T,y))

    @test @inferred(m+[m,m]) == [m+m,m+m]
    @test @inferred([m,m]+m) == [m+m,m+m]
    @test @inferred(broadcast(*,m,[m,m])) == [m2,m2]
    @test @inferred(broadcast(*,[m,m],m)) == [m2,m2]
    @test @inferred([m 2m; m m]*[m,m]) == [3m2,2m2]
    @test @inferred(broadcast(*,[m m],[m,m])) == [m2 m2; m2 m2]
end

# range, range ops
A = 1:5
B = 1.5:5.5
@test A + B == 2.5:2.0:10.5

@testset "slicedim" begin
    for A in (reshape(collect(1:20), 4, 5),
              reshape(1:20, 4, 5))
        @test slicedim(A, 1, 2) == collect(2:4:20)
        @test slicedim(A, 2, 2) == collect(5:8)
        @test_throws ArgumentError slicedim(A,0,1)
        @test slicedim(A, 3, 1) == A
        @test_throws BoundsError slicedim(A, 3, 2)
        @test @inferred(slicedim(A, 1, 2:2)) == collect(2:4:20)'
    end
end

###
### IndexCartesian workout
###
struct LinSlowMatrix{T} <: DenseArray{T,2}
    data::Matrix{T}
end

# This is the default, but just to be sure
Base.IndexStyle{A<:LinSlowMatrix}(::Type{A}) = Base.IndexCartesian()

Base.size(A::LinSlowMatrix) = size(A.data)

Base.getindex(A::LinSlowMatrix, i::Integer) = error("Not defined")
Base.getindex(A::LinSlowMatrix, i::Integer, j::Integer) = A.data[i,j]

Base.setindex!(A::LinSlowMatrix, v, i::Integer) = error("Not defined")
Base.setindex!(A::LinSlowMatrix, v, i::Integer, j::Integer) = A.data[i,j] = v

A = rand(3,5)
B = LinSlowMatrix(A)
S = view(A, :, :)

@test A == B
@test B == A
@test isequal(A, B)
@test isequal(B, A)

for (a,b) in zip(A, B)
    @test a == b
end
for (a,s) in zip(A, S)
    @test a == s
end

C = copy(B)
@test A == C
@test B == C

@test vec(A) == vec(B) == vec(S)
@test minimum(A) == minimum(B) == minimum(S)
@test maximum(A) == maximum(B) == maximum(S)

a, ai = findmin(A)
b, bi = findmin(B)
s, si = findmin(S)
@test a == b == s
@test ai == bi == si

a, ai = findmax(A)
b, bi = findmax(B)
s, si = findmax(S)
@test a == b == s
@test ai == bi == si

fill!(B, 2)
@test all(x->x==2, B)

iall = (1:size(A,1)).*ones(Int,size(A,2))'
jall = ones(Int,size(A,1)).*(1:size(A,2))'
i,j = findn(B)
@test vec(i) == vec(iall)
@test vec(j) == vec(jall)
fill!(S, 2)
i,j = findn(S)
@test vec(i) == vec(iall)
@test vec(j) == vec(jall)

copy!(B, A)
copy!(S, A)

@test cat(1, A, B, S) == cat(1, A, A, A)
@test cat(2, A, B, S) == cat(2, A, A, A)

@test cumsum(A, 1) == cumsum(B, 1) == cumsum(S, 1)
@test cumsum(A, 2) == cumsum(B, 2) == cumsum(S, 2)

@test mapslices(sort, A, 1) == mapslices(sort, B, 1) == mapslices(sort, S, 1)
@test mapslices(sort, A, 2) == mapslices(sort, B, 2) == mapslices(sort, S, 2)

@test flipdim(A, 1) == flipdim(B, 1) == flipdim(S, 2)
@test flipdim(A, 2) == flipdim(B, 2) == flipdim(S, 2)

@test A + 1 == B + 1 == S + 1
@test 2*A == 2*B == 2*S
@test A/3 == B/3 == S/3

# issue #13250
x13250 = zeros(3)
x13250[UInt(1):UInt(2)] = 1.0
@test x13250[1] == 1.0
@test x13250[2] == 1.0
@test x13250[3] == 0.0

struct SquaresVector <: AbstractArray{Int, 1}
    count::Int
end
Base.size(S::SquaresVector) = (S.count,)
Base.IndexStyle(::Type{SquaresVector}) = Base.IndexLinear()
Base.getindex(S::SquaresVector, i::Int) = i*i
foo_squares = SquaresVector(5)
@test convert(Array{Int}, foo_squares) == [1,4,9,16,25]
@test convert(Array{Int, 1}, foo_squares) == [1,4,9,16,25]

# issue #13254
let A = zeros(Int, 2, 2), B = zeros(Float64, 2, 2)
    f1() = [1]
    f2() = [1;]
    f3() = [1;2]
    f4() = [1;2.0]
    f5() = [1 2]
    f6() = [1 2.0]
    f7() = Int[1]
    f8() = Float64[1]
    f9() = Int[1;]
    f10() = Float64[1;]
    f11() = Int[1;2]
    f12() = Float64[1;2]
    f13() = Int[1;2.0]
    f14() = Int[1 2]
    f15() = Float64[1 2]
    f16() = Int[1 2.0]
    f17() = [1:2;]
    f18() = Int[1:2;]
    f19() = Float64[1:2;]
    f20() = [1:2;1:2]
    f21() = Int[1:2;1:2]
    f22() = Float64[1:2;1:2]
    f23() = [1:2;1.0:2.0]
    f24() = Int[1:2;1.0:2.0]
    f25() = [1:2 1:2]
    f26() = Int[1:2 1:2]
    f27() = Float64[1:2 1:2]
    f28() = [1:2 1.0:2.0]
    f29() = Int[1:2 1.0:2.0]
    f30() = [A;]
    f31() = Int[A;]
    f32() = Float64[A;]
    f33() = [A;A]
    f34() = Int[A;A]
    f35() = Float64[A;A]
    f36() = [A;B]
    f37() = Int[A;B]
    f38() = [A A]
    f39() = Int[A A]
    f40() = Float64[A A]
    f41() = [A B]
    f42() = Int[A B]

    for f in [f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16,
              f17, f18, f19, f20, f21, f22, f23, f24, f25, f26, f27, f28, f29, f30,
              f31, f32, f33, f34, f35, f36, f37, f38, f39, f40, f41, f42]
        @test isleaftype(Base.return_types(f, ())[1])
    end
end

# issue #14482
@inferred map(Int8, Int[0])

# make sure @inbounds isn't used too much
mutable struct OOB_Functor{T}; a::T; end
(f::OOB_Functor)(i::Int) = f.a[i]
let f = OOB_Functor([1,2])
    @test_throws BoundsError map(f, [1,2,3,4,5])
end

# issue 15654
@test cumprod([5], 2) == [5]
@test cumprod([1 2; 3 4], 3) == [1 2; 3 4]
@test cumprod([1 2; 3 4], 1) == [1 2; 3 8]
@test cumprod([1 2; 3 4], 2) == [1 2; 3 12]

@test cumsum([5], 2) == [5]
@test cumsum([1 2; 3 4], 1) == [1 2; 4 6]
@test cumsum([1 2; 3 4], 2) == [1 3; 3 7]
@test cumsum([1 2; 3 4], 3) == [1 2; 3 4]

# issue #18363
@test_throws DimensionMismatch cumsum!([0,0], 1:4)
@test cumsum(Any[])::Vector{Any} == Any[]
@test cumsum(Any[1, 2.3])::Vector{Any} == [1, 3.3] == cumsum(Real[1, 2.3])::Vector{Real}
@test cumsum([true,true,true]) == [1,2,3]
@test cumsum(0x00:0xff)[end] === 0x80 # overflow
@test cumsum([[true], [true], [false]])::Vector{Vector{Int}} == [[1], [2], [2]]

#issue #18336
@test cumsum([-0.0, -0.0])[1] === cumsum([-0.0, -0.0])[2] === -0.0
@test cumprod(-0.0im + (0:0))[1] === Complex(0.0, -0.0)

module TestNLoops15895

using Base.Cartesian
using Base.Test

# issue 15894
function f15894(d)
    s = zero(eltype(d))
    @nloops 1 i d begin
        s += @nref 1 d i
    end
    s
end
@test f15894(ones(Int, 100)) == 100
end

@testset "sign, conj, ~" begin
    A = [-10,0,3]
    B = [-10.0,0.0,3.0]
    C = [1,im,0]

    @test sign.(A) == [-1,0,1]
    @test sign.(B) == [-1,0,1]
    @test typeof(sign.(A)) == Vector{Int}
    @test typeof(sign.(B)) == Vector{Float64}

    @test conj(A) == A
    @test conj(B) == A
    @test conj(C) == [1,-im,0]
    @test typeof(conj(A)) == Vector{Int}
    @test typeof(conj(B)) == Vector{Float64}
    @test typeof(conj(C)) == Vector{Complex{Int}}

    @test .~A == [9,-1,-4]
    @test typeof(.~A) == Vector{Int}
end

@testset "issue #16247" begin
    A = zeros(3,3)
    @test size(A[:,0x1:0x2]) == (3, 2)
    @test size(A[:,UInt(1):UInt(2)]) == (3,2)
    @test size(similar(A, UInt(3), 0x3)) == size(similar(A, (UInt(3), 0x3))) == (3,3)
end

# issue 17254
module AutoRetType

using Base.Test

struct Foo end
for op in (:+, :*, :÷, :%, :<<, :>>, :-, :/, :\, ://, :^)
    @eval import Base.$(op)
    @eval $(op)(::Foo, ::Foo) = Foo()
end
A = fill(Foo(), 10, 10)
@test typeof(A+A) == Matrix{Foo}
@test typeof(A-A) == Matrix{Foo}
for op in (:.+, :.*, :.÷, :.%, :.<<, :.>>, :.-, :./, :.\, :.//, :.^)
    @eval @test typeof($(op)(A,A)) == Matrix{Foo}
end

end # module AutoRetType

@testset "concatenations of dense matrices/vectors yield dense matrices/vectors" begin
    N = 4
    densevec = ones(N)
    densemat = diagm(ones(N))
    # Test that concatenations of homogeneous pairs of either dense matrices or dense vectors
    # (i.e., Matrix-Matrix concatenations, and Vector-Vector concatenations) yield dense arrays
    for densearray in (densevec, densemat)
        @test isa(vcat(densearray, densearray), Array)
        @test isa(hcat(densearray, densearray), Array)
        @test isa(hvcat((2,), densearray, densearray), Array)
        @test isa(cat((1,2), densearray, densearray), Array)
    end
    # Test that concatenations of heterogeneous Matrix-Vector pairs yield dense matrices
    @test isa(hcat(densemat, densevec), Array)
    @test isa(hcat(densevec, densemat), Array)
    @test isa(hvcat((2,), densemat, densevec), Array)
    @test isa(hvcat((2,), densevec, densemat), Array)
    @test isa(cat((1,2), densemat, densevec), Array)
    @test isa(cat((1,2), densevec, densemat), Array)
end

@testset "type constructor Array{T, N}(d...) works (especially for N>3)" begin
    a = Array{Float64}(10)
    b = Array{Float64, 1}(10)
    @test size(a) == (10,)
    @test size(a, 1) == 10
    @test size(a,2,1) == (1,10)
    @test size(a) == size(b)
    a = Array{Float64}(2,3)
    b = Array{Float64, 2}(2,3)
    @test size(a) == (2,3)
    @test size(a,4,3,2,1) == (1,1,3,2)
    @test size(a,1,2) == (2,3)
    @test size(a) == size(b)
    a = Array{Float64}(9,8,7,6,5,4,3,2,1)
    b = Array{Float64, 9}(9,8,7,6,5,4,3,2,1)
    @test size(a,1,1) == (9,9)
    @test size(a,4) == 6
    @test size(a,9,8,7,6,5,4,3,2,19,8,7,6,5,4,3,2,1) == (1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,9)
    @test size(a) == size(b)
end

@testset "accumulate, accumulate!" begin
    @test accumulate(+, [1,2,3]) == [1, 3, 6]
    @test accumulate(min, [1 2; 3 4], 1) == [1 2; 1 2]
    @test accumulate(max, [1 2; 3 0], 2) == [1 2; 3 3]
    @test accumulate(+, Bool[]) == Int[]
    @test accumulate(*, Bool[]) == Bool[]
    @test accumulate(+, Float64[]) == Float64[]

    @test accumulate(min, [1, 2, 5, -1, 3, -2]) == [1, 1, 1, -1, -1, -2]
    @test accumulate(max, [1, 2, 5, -1, 3, -2]) == [1, 2, 5, 5, 5, 5]

    @test accumulate(max, [1 0; 0 1], 1) == [1 0; 1 1]
    @test accumulate(max, [1 0; 0 1], 2) == [1 1; 0 1]
    @test accumulate(min, [1 0; 0 1], 1) == [1 0; 0 0]
    @test accumulate(min, [1 0; 0 1], 2) == [1 0; 0 0]

    @test isa(accumulate(+,     Int[]) , Vector{Int})
    @test isa(accumulate(+, 1., Int[]) , Vector{Float64})
    @test accumulate(+, 1, [1,2]) == [2, 4]
    arr = randn(4)
    @test accumulate(*, 1, arr) ≈ accumulate(*, arr)

    N = 5
    for arr in [rand(Float64, N), rand(Bool, N), rand(-2:2, N)]
        for (op, cumop) in [(+, cumsum), (*, cumprod)]
            @inferred accumulate(op, arr)
            accumulate_arr = accumulate(op, arr)
            @test accumulate_arr ≈ cumop(arr)
            @test accumulate_arr[end] ≈ reduce(op, arr)
            @test accumulate_arr[1] ≈ arr[1]
            @test accumulate(op, arr, 10) ≈ arr

            if eltype(arr) in [Int, Float64] # eltype of out easy
                out = similar(arr)
                @test accumulate!(op, out, arr) ≈ accumulate_arr
                @test out ≈ accumulate_arr
            end
        end
    end

    # exotic indexing
    arr = randn(4)
    oarr = OffsetArray(arr, (-3,))
    @test accumulate(+, oarr).parent == accumulate(+, arr)

    @inferred accumulate(+, randn(3))
    @inferred accumulate(+, 1, randn(3))

    # asymmetric operation
    op(x,y) = 2x+y
    @test accumulate(op, [10,20, 30]) == [10, op(10, 20), op(op(10, 20), 30)] == [10, 40, 110]
    @test accumulate(op, [10 20 30], 2) == [10 op(10, 20) op(op(10, 20), 30)] == [10 40 110]
end

@testset "zeros and ones" begin
    @test ones([1,2], Float64, (2,3)) == ones(2,3)
    @test ones(2) == ones(Int, 2) == ones([2,3], Float32, 2) ==  [1,1]
    @test isa(ones(2), Vector{Float64})
    @test isa(ones(Int, 2), Vector{Int})
    @test isa(ones([2,3], Float32, 2), Vector{Float32})

    function test_zeros(arr, T, s)
        @test all(arr .== 0)
        @test isa(arr, T)
        @test size(arr) == s
    end
    test_zeros(zeros(),      Array{Float64, 0}, ())
    test_zeros(zeros(2),     Vector{Float64},   (2,))
    test_zeros(zeros(2,3),   Matrix{Float64},   (2,3))
    test_zeros(zeros((2,3)), Matrix{Float64},   (2,3))

    test_zeros(zeros(Int, 6),      Vector{Int}, (6,))
    test_zeros(zeros(Int, 2, 3),   Matrix{Int}, (2,3))
    test_zeros(zeros(Int, (2, 3)), Matrix{Int}, (2,3))

    test_zeros(zeros([1 2; 3 4]), Matrix{Int}, (2, 2))
    test_zeros(zeros([1 2; 3 4], Float64), Matrix{Float64}, (2, 2))

    zs = zeros(SparseMatrixCSC([1 2; 3 4]), Complex{Float64}, (2,3))
    test_zeros(zs, SparseMatrixCSC{Complex{Float64}}, (2, 3))

    @testset "#19265" begin
        @test_throws MethodError zeros(Float64, [1.])
        x = [1.]
        test_zeros(zeros(x, Float64), Vector{Float64}, (1,))
        @test x == [1.]
    end

    # exotic indexing
    oarr = zeros(randn(3), UInt16, 1:3, -1:0)
    @test indices(oarr) == (1:3, -1:0)
    test_zeros(oarr.parent, Matrix{UInt16}, (3, 2))
end

# issue #11053
mutable struct T11053
    a::Float64
end
Base.:*(a::T11053, b::Real) = T11053(a.a*b)
Base.:(==)(a::T11053, b::T11053) = a.a == b.a
@test [T11053(1)] * 5 == [T11053(1)] .* 5 == [T11053(5.0)]

max_stars_repo_name: felixrehren/julia

max_issues_repo_name: felixrehren/julia

max_forks_repo_name: felixrehren/julia


text:
# a simple LP example
#
#   maximize x + y
#
#   s.t. 50 x + 24 y <= 2400
#        30 x + 33 y <= 2100
#        x >= 45, y >= 5
#
#   solution: x = 45, y = 6.25, objv = 51.25

using Gurobi, Test

@testset "LP 01a" begin
    env = Gurobi.Env()
    setparam!(env, "OutputFlag", 0)

    method = getparam(env, "Method")

    model = Gurobi.Model(env, "lp_01", :maximize)

    # add variables
    add_cvar!(model, 1.0, 45., Inf)  # x
    add_cvar!(model, 1.0,  5., Inf)  # y
    update_model!(model)

    # add constraints
    add_constr!(model, [50., 24.], '<', 2400.)
    add_constr!(model, [30., 33.], '<', 2100.)
    update_model!(model)

    # perform optimization
    optimize(model)

    # show results
    info = get_optiminfo(model)

    @test get_solution(model) == [45, 6.25]
    @test get_objval(model) == 51.25

    GC.gc()  # test finalizers
end

ext: jl


text:
using IJulia
notebook(dir=dirname(@__FILE__))


text:
_make_left_env(ψ::CuMPS, k::Int) = CUDA.ones(eltype(ψ), 1, 1, k)
_make_left_env_new(ψ::CuMPS, k::Int) = CUDA.ones(eltype(ψ), 1, k)
_make_LL(ψ::CuMPS, b::Int, k::Int, d::Int) = CUDA.zeros(eltype(ψ), b, b, k, d)
_make_LL_new(ψ::CuMPS, b::Int, k::Int, d::Int) = CUDA.zeros(eltype(ψ), b, k, d)

function CuMPS(ig::MetaGraph, control::MPSControl)

    Dcut = control.max_bond
    tol = control.var_ϵ
    max_sweeps = control.max_sweeps
    schedule = control.β
    @info "Set control parameters for MPS" Dcut tol max_sweeps

    rank = get_prop(ig, :rank)

    @info "Preparing Hadamard state as MPS"
    ρ = CuMPS(HadamardMPS(Float32, rank))
    is_right = true
    @info "Sweeping through β and σ" schedule
    for dβ ∈ schedule
        ρ = _apply_layer_of_gates(ig, ρ, control, dβ)
    end
    ρ
end

function CuMPS(ig::MetaGraph, control::MPSControl, type::Symbol)
    L = nv(ig)
    Dcut = control.max_bond
    tol = control.var_ϵ
    max_sweeps = control.max_sweeps
    dβ = control.dβ
    β = control.β
    @info "Set control parameters for MPS" Dcut tol max_sweeps
    rank = get_prop(ig, :rank)

    @info "Preparing Hadamard state as MPS"
    ρ = CuMPS(HadamardMPS(Float32, rank))
    is_right = true
    @info "Sweeping through β and σ" dβ

    if type == :log
        k = ceil(log2(β/dβ))
        dβmax = β/(2^k)
        ρ = _apply_layer_of_gates(ig, ρ, control, dβmax)
        for j ∈ 1:k
            ρ = multiply_purifications(ρ, ρ, L)
            if bond_dimension(ρ) > Dcut
                @info "Compresing MPS" bond_dimension(ρ), Dcut
                ρ = compress(ρ, Dcut, tol, max_sweeps)
                is_right = true
            end
        end
        ρ
    elseif type == :lin
        k = β/dβ
        dβmax = β/k
        ρ = _apply_layer_of_gates(ig, ρ, control, dβmax)
        ρ0 = copy(ρ)
        for j ∈ 1:k
            ρ = multiply_purifications(ρ, ρ0, L)
            if bond_dimension(ρ) > Dcut
                @info "Compresing MPS" bond_dimension(ρ), Dcut
                ρ = compress(ρ, Dcut, tol, max_sweeps)
                is_right = true
            end
        end
    end
    ρ

end


text:
using YAML
using Unitful

function convertchemkin2yml(chemkinpath;spcdictpath="",output="chem.rms")
    if spcdictpath != ""
        spcs,rxns = chemkin.load_chemkin_file(chemkinpath,dictionary_path=spcdictpath)
    else
        spcs,rxns = chemkin.load_chemkin_file(chemkinpath)
    end
    D = getmechdictfromchemkin(spcs,rxns)
    writeyml(D;path=output)
end

function writeyml(D;path="chem.rms")
    YAML.write_file(path,D)
end

function getmechdictfromchemkin(spcs,rxns)
    names = [x.label for x in spcs]
    for (i,name) in enumerate(names)
        if count(names.==name) > 1
            names[i] = string(name,"-",count(names.==name))
        end
    end
    D = Dict([])
    D["Units"] = Dict([])
    D["Reactions"] = []
    D["Phases"] = [Dict([])]
    D["Phases"][1]["name"] = "phase"
    D["Phases"][1]["Species"] = [obj2dict(x,spcs,names) for x in spcs]
    D["Reactions"] = [obj2dict(x,spcs,names) for x in rxns]
    return D
end

function convertcanterayml2rmsyml(path;output="chem.rms")
    canterayml = YAML.load_file(path)
    D = getmechdictfromcantera(canterayml)
    writeyml(D;path=output)
end

function getmechdictfromcantera(canterayml)
    unsupportedphases = [
        "binary-solution-tabulated",
        "compound-lattice",
        "constant-density",
        "Debye-Huckel",
        "edge",
        "electron-cloud",
        "fixed-stoichiometry",
        "HMW-electrolyte",
        # "ideal-gas",
        "ideal-molal-solution",
        "ideal-condensed",
        "ideal-solution-VPSS",
        # "ideal-surface",
        "ions-from-neutral-molecule",
        "lattice",
        "liquid-water-IAPWS95",
        "Margules",
        "Maskell-solid-solution",
        "Peng-Robinson",
        "plasma",
        "pure-fluid",
        "Redlich-Kister",
        "Redlich-Kwong",
    ]

    spcs = canterayml["species"]
    rxns = canterayml["reactions"]
    phases = canterayml["phases"]
    units = canterayml["units"] #user specified units
    units = Dict(key=>uparse(value) for (key,value) in units) #convert to Unitful units

    #prevent duplicate names
    spc_names = [spc["name"] for spc in spcs]
    for (i,name) in enumerate(spc_names)
        if count(spc_names.==name) > 1
            k = 2
            new_name = name
            while new_name in spc_names
                new_name = string(name,"-",k)
            end
            spc_names[i] = new_name
        end
    end

    D = Dict([])
    D["Units"] = Dict([]) #will be converting all values to SI
    D["Phases"] = []
    for phase in phases
        if phase["thermo"] in unsupportedphases
            @error "Currently not supporting $(phase["thermo"])"
        end
        phasedict = Dict()
        phasedict["name"] = phase["name"]
        phasedict["Species"] = [canteradict2rmsdict(spc,spcs,spc_names,units,:species) for spc in spcs if spc["name"] in phase["species"]]
        push!(D["Phases"],phasedict)
    end
    D["Reactions"] = [canteradict2rmsdict(rxn,spcs,spc_names,units,:reaction) for rxn in rxns]
    return D
end

function getradicals(obj::T) where {T}
    sm = obj.molecule[1].to_smiles()
    if sm == "[O][O]"
        return 0
    else
        return obj.molecule[1].multiplicity-1
    end
end

function obj2dict(obj,spcs,names;label="solvent")
    D = Dict([])
    if pybuiltin("isinstance")(obj,species.Species)
        D["name"] = names[findall(x->x==obj,spcs)[1]]
        D["type"] = "Species"
        if length(obj.molecule) == 0
            println(obj)
            println(obj.label)
        end
        D["smiles"] = obj.molecule[1].to_smiles()
        D["thermo"] = obj2dict(obj.thermo,spcs,names)
        if D["smiles"] != "[O][O]"
            D["radicalelectrons"] = obj.molecule[1].multiplicity-1
        else
            D["radicalelectrons"] = 0
        end
    elseif pybuiltin("isinstance")(obj,nasa.NASA)
        D["polys"] = [obj2dict(k,spcs,names) for k in obj.polynomials]
        D["type"] = "NASA"
    elseif pybuiltin("isinstance")(obj,nasa.NASAPolynomial)
        D["type"] = "NASApolynomial"
        D["coefs"] = PyVector(obj.coeffs)
        D["Tmax"] = obj.Tmax.value_si
        D["Tmin"] = obj.Tmin.value_si
    elseif pybuiltin("isinstance")(obj,reaction.Reaction)
        D["reactants"] = [names[findall(y->y==x,spcs)[1]]  for x in obj.reactants]
        D["products"] = [names[findall(y->y==x,spcs)[1]] for x in obj.products]
        D["kinetics"] = obj2dict(obj.kinetics,spcs,names)
        D["type"] = "ElementaryReaction"
        D["radicalchange"] = sum([getradicals(x) for x in obj.products])-sum([getradicals(x) for x in obj.reactants])
        D["reversible"] = obj.reversible
    elseif pybuiltin("isinstance")(obj,arrhenius.Arrhenius)
        D["type"] = "Arrhenius"
        D["A"] = obj.A.value_si
        D["Ea"] = obj.Ea.value_si
        D["n"] = obj.n.value_si
    elseif pybuiltin("isinstance")(obj,arrhenius.PDepArrhenius)
        D["type"] = "PdepArrhenius"
        D["Ps"] = PyVector(obj.pressures.value_si)
        D["arrs"] = [obj2dict(x,spcs,names) for x in obj.arrhenius]
    elseif pybuiltin("isinstance")(obj,arrhenius.MultiArrhenius)
        D["type"] = "MultiArrhenius"
        D["arrs"] = [obj2dict(x,spcs,names) for x in obj.arrhenius]
    elseif pybuiltin("isinstance")(obj,arrhenius.MultiPDepArrhenius)
        D["type"] = "MultiPdepArrhenius"
        D["parrs"] = [obj2dict(x,spcs,names) for x in obj.arrhenius]
    elseif pybuiltin("isinstance")(obj,falloff.ThirdBody)
        D["type"] = "ThirdBody"
        D["arr"] = obj2dict(obj.arrheniusLow,spcs,names)
        D["efficiencies"] = Dict([spcs[i].label=>float(val) for (i,val) in enumerate(obj.get_effective_collider_efficiencies(spcs)) if val != 1])
    elseif pybuiltin("isinstance")(obj,falloff.Lindemann)
        D["type"] = "Lindemann"
        D["arrhigh"] = obj2dict(obj.arrheniusHigh,spcs,names)
        D["arrlow"] = obj2dict(obj.arrheniusLow,spcs,names)
        D["efficiencies"] = Dict([spcs[i].label=>float(val) for (i,val) in enumerate(obj.get_effective_collider_efficiencies(spcs)) if val != 1])
    elseif pybuiltin("isinstance")(obj,falloff.Troe)
        D["type"] = "Troe"
        D["arrhigh"] = obj2dict(obj.arrheniusHigh,spcs,names)
        D["arrlow"] = obj2dict(obj.arrheniusLow,spcs,names)
        D["efficiencies"] = Dict([spcs[i].label=>float(val) for (i,val) in enumerate(obj.get_effective_collider_efficiencies(spcs)) if val != 1])
        D["a"] = obj.alpha
        D["T1"] = obj.T1.value_si
        if !isa(obj.T2,Nothing)
            D["T2"] = obj.T2.value_si
        else
            D["T2"] = 0.0
        end
        D["T3"] = obj.T3.value_si
    elseif pybuiltin("isinstance")(obj,chebyshev.Chebyshev)
        D["type"] = "Chebyshev"
        n,m = size(obj.coeffs.value_si)
        D["coefs"] = PyVector([PyVector(obj.coeffs.value_si[i,:]) for i in 1:n])
        D["Tmin"] = obj.Tmin.value_si
        D["Tmax"] = obj.Tmax.value_si
        D["Pmin"] = obj.Pmin.value_si
        D["Pmax"] = obj.Pmax.value_si
    elseif pybuiltin("isinstance")(obj,wilhoit.Wilhoit)
        D["type"] = "Wilhoit"
        D["coefs"] = [obj.a0,obj.a1,obj.a2,obj.a3]
        D["Cp0"] = obj.Cp0.value_si
        D["Cpinf"] = obj.CpInf.value_si
        D["H0"] = obj.H0.value_si
        D["S0"] = obj.S0.value_si
        D["B"] = obj.B.value_si
    elseif pybuiltin("isinstance")(obj,solvation.SolventData)
        D["type"] = "Solvent"
        D["name"] = label
        dsub = dict()
        dsub["type"] = "RiedelViscosity"
        dsub["A"] = obj.A.value_si
        dsub["B"] = obj.B.value_si
        dsub["C"] = obj.C.value_si
        dsub["D"] = obj.D.value_si
        dsub["E"] = obj.E.value_si
        D["mu"] = dsub
    elseif pybuiltin("is")(obj,pybuiltin("None"))
        return pybuiltin("None")
    else
        throw(error("object $obj not understood"))
    end
    return D
end

function canteradict2rmsdict(canteradict,spcs,names,units,dict_type;numreactants=nothing,polyindex=nothing)
    D = Dict([])
    if dict_type == :species #species
        D["name"] = names[findall(x->x==canteradict,spcs)[1]]
        D["type"] = "Species"
        D["smiles"] = ""
        D["thermo"] = canteradict2rmsdict(canteradict["thermo"],spcs,names,units,:thermo)
        if haskey(canteradict,"equation-of-state")
            model = canteradict["equation-of-state"]["model"]
            @error "Currently not supporting $(model) thermo model"
        end
    elseif dict_type == :thermo
        if canteradict["model"] == "NASA7"
            D["type"] = "NASA"
            D["polys"] = [canteradict2rmsdict(canteradict,spcs,names,units,:NASApolynomial;polyindex=i) for i in 1:(length(canteradict["temperature-ranges"])-1)]
        else
             @error "Currently only support NASA7 thermo model from Cantera"
        end
    elseif dict_type == :NASApolynomial
        D["type"] = "NASApolynomial"
        D["Tmax"] = canteradict["temperature-ranges"][polyindex+1]
        D["Tmin"] = canteradict["temperature-ranges"][polyindex]
        D["coefs"] = canteradict["data"][polyindex,:][1]
    elseif dict_type == :reaction #reaction
        equation = canteradict["equation"]
        reversible = true
        if occursin(" <=> ",equation) #reversible
            reactants, products = split(canteradict["equation"]," <=> ")
        elseif occursin(" => ",equation) #irreversible
            reactants, products = split(canteradict["equation"]," => ")
            reversible = false
        end
        reactants = _interpretstoichstring(reactants,names)
        products = _interpretstoichstring(products,names)
        kinetics = canteradict2rmsdict(canteradict,spcs,names,units,:kinetics,numreactants=length(reactants))
        D["reactants"] = reactants
        D["products"] = products
        D["kinetics"] = kinetics
        D["reversible"] = reversible
        D["type"] = "ElementaryReaction"
    elseif dict_type == :kinetics
        if haskey(canteradict,"type")
            kinetics_type = canteradict["type"]
            if kinetics_type == "three-body"
                D["type"] = "ThirdBody"
                D["arr"] = canteradict2rmsdict(canteradict["rate-constant"],spcs,names,units,:arrhenius,numreactants=numreactants+1)
                D["efficiencies"] = get(canteradict,"efficiencies",Dict([]))
            elseif kinetics_type == "falloff"
                if haskey(canteradict,"Troe")
                    D["type"] = "Troe"
                    D["arrhigh"] = canteradict2rmsdict(canteradict["high-P-rate-constant"],spcs,names,units,:arrhenius,numreactants=numreactants)
                    D["arrlow"] = canteradict2rmsdict(canteradict["low-P-rate-constant"],spcs,names,units,:arrhenius,numreactants=numreactants+1)
                    D["efficiencies"] = get(canteradict,"efficiencies",Dict([]))
                    D["a"] = canteradict["Troe"]["A"]
                    D["T1"] = canteradict["Troe"]["T1"]
                    if haskey(canteradict["Troe"],"T2")
                        D["T2"] = canteradict["Troe"]["T2"]
                    else
                        D["T2"] = 0.0
                    end
                    D["T3"] = canteradict["Troe"]["T3"]
                else #Lindemann
                    D["type"] = "Lindemann"
                    D["arrhigh"] = canteradict2rmsdict(canteradict["high-P-rate-constant"],spcs,names,units,:arrhenius,numreactants=numreactants)
                    D["arrlow"] = canteradict2rmsdict(canteradict["low-P-rate-constant"],spcs,names,units,:arrhenius,numreactants=numreactants+1)
                    D["efficiencies"] = get(canteradict,"efficiencies",Dict([]))
                end
            elseif kinetics_type == "pressure-dependent-Arrhenius"
                D["type"] = "PdepArrhenius"
                D["Ps"] = [kinetics["P"] for kinetics in canteradict["rate-constants"]]
                D["arrs"] = [canteradict2rmsdict(kinetics,spcs,names,units,:arrhenius,numreactants=numreactants) for kinetics in canteradict["rate-constants"]]
            elseif kinetics_type == "Chebyshev"
                D["type"] = "Chebyshev"
                D["coefs"] = [canteradict["data"][:,i] for i in 1:size(canteradict["data"],2)]
                D["Tmin"] = tosivalue(canteradict["temperature-range"][1],units=units,value_type=:temperature)
                D["Tmax"] = tosivalue(canteradict["temperature-range"][end],units=units,value_type=:temperature)
                D["Pmin"] = tosivalue(canteradict["pressure-range"][1],units=units,value_type=:pressure)
                D["Pmax"] = tosivalue(canteradict["pressure-range"][end],units=units,value_type=:pressure)
            else
                @error "Currently not supporting $(kinetics_type)"
            end
        elseif haskey(canteradict,"sticking-coefficient")
            D = canteradict2rmsdict(canteradict["sticking-coefficient"],spcs,names,units,:arrhenius,numreactants=0)
            D["type"] = "StickingCoefficient"
        elseif haskey(canteradict,"rate-constant") #arrhenius
            D = canteradict2rmsdict(canteradict["rate-constant"],spcs,names,units,:arrhenius,numreactants=numreactants)
        else
            @error "Currently not supporting $(canteradict) type kinetics"
        end
        if haskey(canteradict,"coverage-dependencies")
            @error "Currently not supporting coverage dependencies"
        end
    elseif dict_type == :arrhenius
        D["type"] = "Arrhenius"
        D["A"] = tosivalue(canteradict["A"],units=units,value_type=:Afactor,numreactants=numreactants)
        D["Ea"] = tosivalue(canteradict["Ea"],units=units,value_type=:activationenergy)
        D["n"] = canteradict["b"]
    end
    return D
end

function _interpretstoichstring(spcs,names)
    spc_names = Vector{String}()

    spcs = split(spcs," ")
    for (ind,item) in enumerate(spcs)
        stoich = tryparse(Int64,item)
        if stoich != nothing
            spc = spcs[ind+1]
            append!(spc_names,[spc for i in 1:(stoich-1)])
        else
            if item in names
                spc = item
                push!(spc_names,spc)
            end
        end
    end
    return spc_names
end

function tosivalue(value;units=nothing,value_type=nothing,numreactants=nothing)

    if value isa String #unit specified for this individual value
        value,unit = split(value," ")
        value = parse(Float64,value)
        unit = uparse(unit)
    else
        if value_type == :temperature
            unit = get(units,"temperature",Unitful.K)
        elseif value_type == :pressure
            unit = get(units,"pressure",Unitful.Pa)
        elseif value_type == :activationenergy
            unit = get(units,"activation-energy",nothing)
            if unit == nothing
                quantity_unit = get(units,"quantity",Unitful.mol)
                energy_unit = get(units,"energy",Unitful.J)
                unit = (energy_unit)/(quantity_unit)
            end
        elseif value_type == :Afactor
            time_unit = get(units,"time",Unitful.s)
            length_unit = get(units,"length",Unitful.m)
            quantity_unit = get(units,"quantity",Unitful.mol)
            if numreactants == 1
                unit = time_unit^(-1)
            elseif numreactants == 2
                unit = (length_unit)^3/(quantity_unit*time_unit)
            elseif numreactants == 3
                unit = (length_unit)^6/((quantity_unit)^2*(time_unit))
            elseif numreactants == 0
                return value
            end
        end
    end
    return upreferred(value*unit).val
end


text:
function JSmacro(ex::Expr)
    mc = ex.args[1]
    if     mc == Symbol("@async")
        return JSasync(ex)
    elseif mc == Symbol("@reg")
        return JSreg(ex)
    elseif mc == Symbol("@posedge")
        return JSedgereg(ex)
    elseif mc == Symbol("@negedge")
        return JSedgereg(ex)
    elseif mc == Symbol("@delay")
        return JSdelay(ex)
    elseif mc == Symbol("@block")
        return JSmacroblock(ex)
    elseif mc == Symbol("@verilog")
        return JSverilog(ex)
    elseif mc == Symbol("@julia")
        return JSjulia(ex)
    else
        error("Hit an unexplored macrocall: $(mc)")
    end
end

function JSasync(ex::Expr)
    if !isa(ex.args[2], Expr)
        error("@async block is not a complete expr:\n$(ex)")
    end

    if !isa(ex.args[2].args[1], Expr)
        error("First argument in @async block is not an expr:\n$(ex)")
    end

    # Throw an error if a :(:=) is used
    JScolonequals(ex.args[2].args[1], ex.args[2].args[1])

    # Throw an error if a :(<=) is used
    JSnonblock(ex.args[2].args[1], ex.args[2].args[1])

    # Descend into the async and recursively do stuff
    ex.args[2].args[1] = JSasynchelper(ex.args[2].args[1])
end

function JSreg(ex::Expr)
    # Throw an error if the basic syntax is not met
    if !isa(ex.args[2], Expr)
        error("@reg block is not a complete expr:\n$(ex)")
    end
    if !isa(ex.args[2].args[1], Expr)
        error("First argument in @reg block is not an expr:\n$(ex)")
    end
    if length(ex.args[2].args[1].args) != 1
        error("Multiple Exprs found in @reg block:\n$(ex)")
    end

    # Throw an error if the first expr is not an if
    if ex.args[2].args[1].head != :if
        error("First argument in @reg block is not an if statement:\n$(ex)")
    end

    # Throws an error if there is more than the first if-else
    if length(ex.args[2].args) != 1
        error("@reg block contains more than 1 nested if-else expr:\n$(ex)")
    end

    # Throw an error if the bottom else is used
    if length(ex.args[2].args[1].args) != 2
        if JSregbottomelse(ex.args[2].args[1].args[3])
            error("The bottom-most else is used in an @reg block:\n$(ex)")
        end
    end

    # Throw an error if a :(:=) is used
    JScolonequals(ex.args[2].args[1])

    # Turn :(<=) into :(=)
    ex.args[2].args[1] = JSregequals(ex.args[2].args[1])

    # Descend into the reg and recursively do stuff
    ex.args[2].args[1] = JSreghelper(ex.args[2].args[1])

    # Turn :(=) into :(<=)
    ex.args[2].args[1] = JSregnonblocking(ex.args[2].args[1])
end

function JSedgereg(ex::Expr)
    # Throw an error if the basic syntax is not met
    if     isa(ex.args[2], Symbol)
        if !haskey(arrowDict , ex.args[2])
            error("Discovered uninstantiated symbol: $(rhs) in expr:\n$(ex)")
        end
        if arrowDict[ex.args[2]].BitCount != 1
            error("Pos or Neg edge register defined by a wire of bit count not 1:\n$(ex)")
        end
    elseif isa(ex.args[2], Expr)
        # MOON matrix stuff
        name = ex.args[2].args[1]
        if !haskey(arrowDict , name)
            error("Discovered uninstantiated symbol: $(name) in expr:\n$(ex)")
        end
        if isa(ex.args[2].args[2] , Expr)
            # Is a ref
            # check for single bit
            bl = ex.args[2].args[2].args[1]
            br = (length(ex.args[2].args) == 3) ?
                ex.args[2].args[2].args[3] : ex.args[2].args[2].args[2]
            bmax = max(bl, br)
            bmin = min(bl, br)
            arrowbl = arrowDict[name].BitLeft
            arrowbr = arrowDict[name].BitRight
            if bmax != bmin
                error("Wire for edge reg is referencing more than 1 bit:\n$(ex)")
            end
            if arrowDict[name].Endian == 1 && (bmax > arrowbl)
                error("Referencing bit $(bmax) which is outside wire $(name):\n$(ex)")
            end
            if arrowDict[name].Endian == 1 && (bmin < arrowbr)
                error("Referencing bit $(bmin) which is outside wire $(name):\n$(ex)")
            end
            if arrowDict[name].Endian == 2 && (bmax > arrowbr)
                error("Referencing bit $(bmax) which is outside wire $(name):\n$(ex)")
            end
            if arrowDict[name].Endian == 2 && (bmin < arrowbl)
                error("Referencing bit $(bmin) which is outside wire $(name):\n$(ex)")
            end
            # check for single bit outside range
        else
            # Must be a single bit
            b = ex.args[2].args[2]
            bl = arrowDict[name].BitLeft
            br = arrowDict[name].BitRight
            if arrowDict[name].Endian == 1 && (b > bl || b < br)
                error("Referencing bit $(b) which is outside wire $(name):\n$(ex)")
            end
            if arrowDict[name].Endian == 2 && (b < bl || b > br)
                error("Referencing bit $(b) which is outside wire $(name):\n$(ex)")
            end
        end
    end

    if !isa(ex.args[3], Expr)
        error("@posedge or @negedge block is not a complete Expr:\n$(ex)")
    end

    # Throw an error if a :(:=) is used
    JScolonequals(ex.args[2].args[1])

    # Turn :(<=) into :(=)
    ex.args[2].args[1] = JSregequals(ex.args[2].args[1])

    # Descend into the reg and recursively do stuff
    ex.args[2].args[1] = JSreghelper(ex.args[2].args[1])

    # Turn :(=) into :(<=)
    ex.args[2].args[1] = JSregnonblocking(ex.args[2].args[1])
end

# TODO this stuff
# Throw an error if all wires are not written to
# Create wires if needed, change to regs as needed
# Accomodate for ifs when they show up,
# for ifs turn condition into JIR
function JSasynchelper(ex::Expr)

end

function JSreghelper(ex::Expr)

end

function JSregbottomelse(ex::Expr)
    # Passed an else statement block
    l = length(ex.args)
    iferror = true
    for i = 1:l
        arg = ex.args[i]
        if isa(arg, Expr) && arg.head == :if
            iferror = false
            if length(arg.args) == 3
                iferror = iferror | JSregbottomelse(arg.args[3])
            end
        end
    end
    return iferror
end

# Throw an error if a :(:=) is recursively found
function JScolonequals(ex::Expr)
    JScolonequalshelper(ex, ex)
end
function JScolonequals(top::Expr, ex::Expr)
    l = length(ex.args)
    for i = 1:l
        arg = ex.args[i]
        if isa(arg, Expr)
            if arg.head == :(:=)
                error("ColonEquals assignment used in $(ex) in @reg block:\n$(top)")
            end
            JScolonequalshelper(top, arg)
        end
    end
end

# Replace :(=) in regs with :(<=)
function JSregnonblocking(ex::Expr)
    l = length(ex.args)
    for i = 1:l
        arg = ex.args[i]
        if isa(arg, Expr)
            if arg.head == :(=)
                ex.args[i] = Expr(:call, :(<=), arg.args[1], arg.args[2])
            else
                ex.args[i] = JSregnonblocking(arg)
            end
        end
    end
    return ex
end

function JSregequals(ex::Expr)
    l = length(ex.args)
    for i = 1:l
        arg = ex.args[i]
        if isa(arg, Expr)
            if arg.head == :call && arg.args[1] == :(<=)
                ex.args[i] = Expr(:(=), arg.args[2], arg.args[3])
            else
                ex.args[i] = JSregnonblocking(arg)
            end
        end
    end
    return ex
end

# Throw an error if a :(<=) is recursively found
function JSnonblock(top::Expr, ex::Expr)
    l = length(ex.args)
    for i = 1:l
        arg = ex.args[i]
        if isa(arg, Expr)
            if arg.head == :call && arg.args[1] == :(<=)
                error("Nonblocking operator used in $(ex) in @async block:\n$(top)")
            end
            JSnonblock(top, arg)
        end
    end
end

function JSdelay(ex::Expr)
    l = length(ex.args)
    if l != 3
        error("Inappopriate number of arguments in @delay block, is $(l-1) should be 2:\n$(ex)")
    end
    if !isa(ex.args[2], Float64)
        error("Time delay does not represent a Float64 number:\n$(ex)")
    end
    if !isa(ex.args[3], Expr)
        error("Final argument of @delay block is not a expr:\n$(ex)")
    end
    return ex
end

function JSmacroblock(ex::Expr)
    if length(ex.args) != 4
        error("@block macro has incorrect number of arguments, should be 3:\n$(ex)")
    end
    if !isa(ex.args[2], Symbol)
        error("@block macro does not have a Symbol as it's first argument:\n$(ex)")
    end
    if !isa(ex.args[3], String)
        error("@block macro does not have a String as it's second argument:\n$(ex)")
    end
    if !isa(ex.args[3], Expr)
        error("@block macro does not have an Expr as it's third argument:\n$(ex)")
    end
    if ex.args[3].head != Tuple
        error("@block macro does not have a Tuple as it's third argument:\n$(ex)")
    end

    return ex
end

function JSverilog(ex::Expr)
    if length(ex.args) != 2
        error("@verilog macro has incorrect number of arguments, should be only 1:\n$(ex)")
    end
    if !isa(ex.args[2], Expr)
        error("@verilog macro does not have an expr as it's argument:\n$(ex)")
    end
    if ex.args[2].head != :block
        error("@verilog macro does not have a block as it's argument:\n$(ex)")
    end
    if length(ex.args[2].args) != 1
        error("@verilog macro expr has incorrect number of exprs, should be only 1:\n$(ex)")
    end
    if !isa(ex.args[2].args[1], String)
        error("@verilog macro does not have contain a String inside it's block argument:\n$(ex)")
    end

    return ex
end

function JSjulia(ex::Expr)
    if length(ex.args) != 2
        error("@julia macro has incorrect number of arguments, should be only 1:\n$(ex)")
    end
    if !isa(ex.args[2], Expr)
        error("@julia macro does not have an expr as it's argument:\n$(ex)")
    end
    if ex.args[2].head != :block
        error("@julia macro does not have a block as it's argument:\n$(ex)")
    end
    if length(ex.args[2].args) != 1
        error("@julia macro expr has incorrect number of exprs, should be only 1:\n$(ex)")
    end
    if !isa(ex.args[2].args[1], String)
        error("@julia macro does not have contain a String inside it's block argument:\n$(ex)")
    end

    return ex
end

max_stars_repo_name: trevortheblack/Juliog

max_issues_repo_name: trevortheblack/Juliog

max_forks_repo_name: trevortheblack/Juliog


text:
###############################################################################
#
# Integral functions
#
###############################################################################
"""
Function `numerical∫` calculates the integral numerically. The supported methods are

$(METHODLIST)

"""
function numerical∫ end

"""
When two arrays (one for f(x) and one for Δx) are provided, `numerical∫` computes the sum of the mutiplication of the two using matrix mutiplication
    `fxs' * Δxs`.

    numerical∫(f::Array{FT,1}, Δx::Array{FT,1}) where {FT<:AbstractFloat}

Intergal of given
- `f` f(x) for each x
- `Δx` Δx for each x

Note that f and Δx may have different dimensions, and if so a warning will display.

---
Examples
```julia
FT = Float32;
f_sum = numerical∫(FT[1,2,3,4], FT[0.1,0.1,0.2,0.3]);
```
"""
numerical∫(f::Array{FT,1}, Δx::Array{FT,1}) where {FT<:AbstractFloat} =
(
    if length(Δx) == length(f)
        return f' * Δx
    else
        @warn twarn("Dimensions not matching, use the matching parts only...");
        N = min(length(f), length(Δx));
        return adjoint(view(f,1:N)) * view(Δx,1:N)
    end
)

"""
The above methods is useful for both evenly and non-evenly distributed `Δx`. However, for many cases `Δx` is evenly distributed and provided as a number
    rather than an array. In this scenario, a special method is given:

    numerical∫(f::Array{FT,1}, Δx::FT) where {FT<:AbstractFloat}

Intergal of given
- `f` f(x) for each x
- `Δx` Δx for x

---
Examples
```julia
FT = Float32;
f_sum = numerical∫(FT[1,2,3,4], FT(0.1));
```
"""
numerical∫(f::Array{FT,1}, Δx::FT) where {FT<:AbstractFloat} =
(
    return sum(f) * abs(Δx)
)

"""
We also provide a function to manually solve for the integral of a given function for a given range of x. The method is

    numerical∫(f::Function,
               x_min::FT,
               x_max::FT,
               n::Int
    ) where {FT<:AbstractFloat}

Intergal of given
- `f` A function
- `x_min` Minimum limit of x
- `x_max` Maximum limit of x
- `n` Number of points in the x range (evenly stepped)

---
Examples
```julia
FT = Float32;
func(x) = x^2;
f_sum = numerical∫(func, FT(0), FT(2), 20);
```
"""
numerical∫(f::Function,
           x_min::FT,
           x_max::FT,
           n::Int
) where {FT<:AbstractFloat} =
(
    _sum = 0;
    _dx  = (x_max - x_min) / n;
    for _i in 1:n
        _x = x_min + FT(_i-0.5) * _dx;
        _sum += f(_x);
    end;

    return _sum * _dx;
)

"""
This method automatically computes the integral of a function for an `x` within a range:

    numerical∫(f::Function,
               x_min::FT,
               x_max::FT,
               x_tol::FT = sqrt(eps(FT)),
               y_tol::FT = sqrt(eps(FT))
    ) where {FT<:AbstractFloat}

Intergal of given
- `f` A function
- `x_min` Minimum limit of x
- `x_max` Maximum limit of x
- `x_tol` Tolerance of Δx (x/N)
- `y_tol` Tolerance of the integral solution

---
Example
```julia
FT = Float32;
func(x) = x^2;
f_sum = numerical∫(func, FT(0), FT(2));
f_sum = numerical∫(func, FT(0), FT(2), FT(1e-3), FT(1e-3));
```
"""
numerical∫(f::Function,
           x_min::FT,
           x_max::FT,
           x_tol::FT = sqrt(eps(FT)),
           y_tol::FT = sqrt(eps(FT))
) where {FT<:AbstractFloat} =
(
    @assert y_tol > 0;

    # _sum_0: sum before halfing steps (_N), _sum_N: sum after halfing steps
    _sum_0::FT = (f(x_min) + f(x_max)) / 2;
    _sum_N::FT = 0;
    _N = 1;

    # continue the steps till the tolerances are reached
    while true
        _sum_N = (numerical∫(f, x_min, x_max, _N) + _sum_0) / 2;
        if abs(_sum_N - _sum_0) < y_tol || 1/_N < x_tol
            break;
        end;
        _sum_0 = _sum_N;
        _N *= 2;
    end;

    return _sum_N
)


text:
using DashBootstrapComponents

checklist = dbc_checklist(
    id="checklist-selected-style",
    options=[
        Dict("label" => "Option 1", "value" => 1),
        Dict("label" => "Option 2", "value" => 2),
        Dict("label" => "Option 3", "value" => 3),
    ],
    labelCheckedStyle=Dict("color" => "red"),
);

max_stars_repo_name: harisbal/dash-bootstrap-components

max_issues_repo_name: harisbal/dash-bootstrap-components

max_forks_repo_name: harisbal/dash-bootstrap-components