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

using Dates, Random

# Compare precision in a manner sensitive to subnormals, which lose
# precision compared to widening.
function cmp_sn(w, hi, lo, slopbits=0)
    if !isfinite(hi)
        if abs(w) > realmax(typeof(hi))
            return isinf(hi) && sign(w) == sign(hi)
        end
        if isnan(w) && isnan(hi)
            return true
        end
        return w == hi
    end
    if abs(w) < subnormalmin(typeof(hi))
        return (hi == zero(hi) || abs(w - widen(hi)) < abs(w)) && lo == zero(hi)
    end
    # Compare w == hi + lo unless `lo` issubnormal
    z = widen(hi) + widen(lo)
    if !issubnormal(lo) && lo != 0
        if slopbits == 0
            return z == w
        end
        wr, zr = roundshift(w, slopbits), roundshift(z, slopbits)
        return max(wr-1, zero(wr)) <= zr <= wr+1
    end
    # round w to the same number of bits as z
    zu = asbits(z)
    wu = asbits(w)
    lastbit = false
    while zu > 0 && !isodd(zu)
        lastbit = isodd(wu)
        zu = zu >> 1
        wu = wu >> 1
    end
    return wu <= zu <= wu + lastbit
end

asbits(x) = reinterpret(Base.uinttype(typeof(x)), x)

function roundshift(x, n)
    xu = asbits(x)
    lastbit = false
    for i = 1:n
        lastbit = isodd(xu)
        xu = xu >> 1
    end
    xu + lastbit
end

subnormalmin(::Type{T}) where T = reinterpret(T, Base.uinttype(T)(1))

function highprec_pair(x, y)
    slopbits = (Base.Math.significand_bits(typeof(widen(x))) + 1) -
        2*(Base.Math.significand_bits(typeof(x)) + 1)
    hi, lo = Base.add12(x, y)
    @test cmp_sn(widen(x) + widen(y), hi, lo)
    hi, lo = Base.mul12(x, y)
    @test cmp_sn(widen(x) * widen(y), hi, lo)
    y == 0 && return nothing
    hi, lo = Base.div12(x, y)
    @test cmp_sn(widen(x) / widen(y), hi, lo, slopbits)
    nothing
end
@testset "high precision" begin
    # Because ranges rely on high precision arithmetic, test those utilities first
    for (I, T) in ((Int16, Float16), (Int32, Float32), (Int64, Float64)), i = 1:10^3
        i = rand(I) >> 1  # test large values below
        hi, lo = Base.splitprec(T, i)
        @test widen(hi) + widen(lo) == i
        @test endswith(bitstring(hi), repeat('0', Base.Math.significand_bits(T) ÷ 2))
    end
    for (I, T) in ((Int16, Float16), (Int32, Float32), (Int64, Float64))
        x = T(typemax(I))
        Δi = ceil(I, eps(x))
        for i = typemax(I)-2Δi:typemax(I)-Δi
            hi, lo = Base.splitprec(T, i)
            @test widen(hi) + widen(lo) == i
            @test endswith(bitstring(hi), repeat('0', Base.Math.significand_bits(T) ÷ 2))
        end
        for i = typemin(I):typemin(I)+Δi
            hi, lo = Base.splitprec(T, i)
            @test widen(hi) + widen(lo) == i
            @test endswith(bitstring(hi), repeat('0', Base.Math.significand_bits(T) ÷ 2))
        end
    end

    # # This tests every possible pair of Float16s. It takes too long for
    # # ordinary use, which is why it's commented out.
    # function pair16()
    #     for yu in 0x0000:0xffff
    #         for xu in 0x0000:0xffff
    #             x, y = reinterpret(Float16, xu), reinterpret(Float16, yu)
    #             highprec_pair(x, y)
    #         end
    #     end
    # end

    for T in (Float16, Float32) # skip Float64 (bit representation of BigFloat is not available)
        for i = 1:10^5
            x, y = rand(T), rand(T)
            highprec_pair(x, y)
            highprec_pair(-x, y)
            highprec_pair(x, -y)
            highprec_pair(-x, -y)
        end
        # Make sure we test dynamic range too
        for i = 1:10^5
            x, y = rand(T), rand(T)
            x == 0 || y == 0 && continue
            x, y = log(x), log(y)
            highprec_pair(x, y)
        end
    end
end
asww(x) = widen(widen(x.hi)) + widen(widen(x.lo))
astuple(x) = (x.hi, x.lo)

function cmp_sn2(w, hi, lo, slopbits=0)
    if !isfinite(hi)
        if abs(w) > realmax(typeof(hi))
            return isinf(hi) && sign(w) == sign(hi)
        end
        if isnan(w) && isnan(hi)
            return true
        end
        return w == hi
    end
    if abs(w) < subnormalmin(typeof(hi))
        return (hi == zero(hi) || abs(w - widen(hi)) < abs(w)) && lo == zero(hi)
    end
    z = widen(hi) + widen(lo)
    w == z && return true
    zu, wu = asbits(z), asbits(w)
    while zu > 0 && !isodd(zu)
        zu = zu >> 1
        wu = wu >> 1
    end
    zu = zu >> slopbits
    wu = wu >> slopbits
    return wu - 1 <= zu <= wu + 1
end
@testset "TwicePrecision" begin
    # TwicePrecision test. These routines lose accuracy if you form
    # intermediate subnormals; with Float16, this happens so frequently,
    # let's only test Float32.
    let T = Float32
        Tw = widen(T)
        slopbits = (Base.Math.significand_bits(Tw) + 1) -
            2*(Base.Math.significand_bits(T) + 1)
        for i = 1:10^5
            x = Base.TwicePrecision{T}(rand())
            y = Base.TwicePrecision{T}(rand())
            xw, yw = asww(x), asww(y)
            @test cmp_sn2(Tw(xw+yw), astuple(x+y)..., slopbits)
            @test cmp_sn2(Tw(xw-yw), astuple(x-y)..., slopbits)
            @test cmp_sn2(Tw(xw*yw), astuple(x*y)..., slopbits)
            @test cmp_sn2(Tw(xw/yw), astuple(x/y)..., slopbits)
            y = rand(T)
            yw = widen(widen(y))
            @test cmp_sn2(Tw(xw+yw), astuple(x+y)..., slopbits)
            @test cmp_sn2(Tw(xw-yw), astuple(x-y)..., slopbits)
            @test cmp_sn2(Tw(xw*yw), astuple(x*y)..., slopbits)
            @test cmp_sn2(Tw(xw/yw), astuple(x/y)..., slopbits)
        end
    end

    x1 = Base.TwicePrecision{Float64}(1)
    x0 = Base.TwicePrecision{Float64}(0)
    xinf = Base.TwicePrecision{Float64}(Inf)
    @test Float64(x1+x0)  == 1
    @test Float64(x1+0)   == 1
    @test Float64(x1+0.0) == 1
    @test Float64(x1*x0)  == 0
    @test Float64(x1*0)   == 0
    @test Float64(x1*0.0) == 0
    @test Float64(x1/x0)  == Inf
    @test Float64(x1/0)   == Inf
    @test Float64(xinf*x1) == Inf
    @test isnan(Float64(xinf*x0))
    @test isnan(Float64(xinf*0))
    @test isnan(Float64(xinf*0.0))
    @test isnan(Float64(x0/x0))
    @test isnan(Float64(x0/0))
    @test isnan(Float64(x0/0.0))
end
@testset "ranges" begin
    @test size(10:1:0) == (0,)
    @testset "colon" begin
        @inferred(colon(10, 1, 0))
        @inferred(colon(1, .2, 2))
        @inferred(colon(1., .2, 2.))
        @inferred(colon(2, -.2, 1))
        @inferred(colon(1, 0))
        @inferred(colon(0.0, -0.5))
    end

    @testset "indexing" begin
        L32 = @inferred(linspace(Int32(1), Int32(4), 4))
        L64 = @inferred(linspace(Int64(1), Int64(4), 4))
        @test @inferred(L32[1]) === 1.0 && @inferred(L64[1]) === 1.0
        @test L32[2] == 2 && L64[2] == 2
        @test L32[3] == 3 && L64[3] == 3
        @test L32[4] == 4 && L64[4] == 4
        @test @inferred(linspace(1.0, 2.0, 2.0f0)) === linspace(1.0, 2.0, 2)
        @test @inferred(linspace(1.0, 2.0, 2))[1] === 1.0
        @test @inferred(linspace(1.0f0, 2.0f0, 2))[1] === 1.0f0
        @test @inferred(linspace(Float16(1.0), Float16(2.0), 2))[1] === Float16(1.0)

        let r = 5:-1:1
            @test r[1]==5
            @test r[2]==4
            @test r[3]==3
            @test r[4]==2
            @test r[5]==1
        end
        @test @inferred((0.1:0.1:0.3)[2]) === 0.2
        @test @inferred((0.1f0:0.1f0:0.3f0)[2]) === 0.2f0

        @test @inferred((1:5)[1:4]) === 1:4
        @test @inferred((1.0:5)[1:4]) === 1.0:4
        @test (2:6)[1:4] == 2:5
        @test (1:6)[2:5] === 2:5
        @test (1:6)[2:2:5] === 2:2:4
        @test (1:2:13)[2:6] === 3:2:11
        @test (1:2:13)[2:3:7] === 3:6:13

        @test isempty((1:4)[5:4])
        @test_throws BoundsError (1:10)[8:-1:-2]

        let r = typemax(Int)-5:typemax(Int)-1
            @test_throws BoundsError r[7]
        end
    end
    @testset "length" begin
        @test length(.1:.1:.3) == 3
        @test length(1.1:1.1:3.3) == 3
        @test length(1.1:1.3:3) == 2
        @test length(1:1:1.8) == 1
        @test length(1:.2:2) == 6
        @test length(1.:.2:2.) == 6
        @test length(2:-.2:1) == 6
        @test length(2.:-.2:1.) == 6
        @test length(2:.2:1) == 0
        @test length(2.:.2:1.) == 0

        @test length(1:0) == 0
        @test length(0.0:-0.5) == 0
        @test length(1:2:0) == 0
    end
    @testset "findall(::OccursIn, ::Array)" begin
        @test findall(occursin(3:20), [5.2, 3.3]) == findall(occursin(Vector(3:20)), [5.2, 3.3])

        let span = 5:20,
            r = -7:3:42
            @test findall(occursin(span), r) == 5:10
            r = 15:-2:-38
            @test findall(occursin(span), r) == 1:6
        end
    end
    @testset "reverse" begin
        @test reverse(reverse(1:10)) == 1:10
        @test reverse(reverse(typemin(Int):typemax(Int))) == typemin(Int):typemax(Int)
        @test reverse(reverse(typemin(Int):2:typemax(Int))) == typemin(Int):2:typemax(Int)
    end
    @testset "intersect" begin
        @test intersect(1:5, 2:3) == 2:3
        @test intersect(-3:5, 2:8) == 2:5
        @test intersect(-8:-3, -8:-3) == -8:-3
        @test intersect(1:5, 5:13) == 5:5
        @test isempty(intersect(-8:-3, -2:2))
        @test isempty(intersect(-3:7, 2:1))
        @test intersect(1:11, -2:3:15) == 1:3:10
        @test intersect(1:11, -2:2:15) == 2:2:10
        @test intersect(1:11, -2:1:15) == 1:11
        @test intersect(1:11, 15:-1:-2) == 1:11
        @test intersect(1:11, 15:-4:-2) == 3:4:11
        @test intersect(-20:-5, -10:3:-2) == -10:3:-7
        @test isempty(intersect(-5:5, -6:13:20))
        @test isempty(intersect(1:11, 15:4:-2))
        @test isempty(intersect(11:1, 15:-4:-2))
        #@test intersect(-5:5, 1+0*(1:3)) == 1:1
        #@test isempty(intersect(-5:5, 6+0*(1:3)))
        @test intersect(-15:4:7, -10:-2) == -7:4:-3
        @test intersect(13:-2:1, -2:8) == 7:-2:1
        @test isempty(intersect(13:2:1, -2:8))
        @test isempty(intersect(13:-2:1, 8:-2))
        #@test intersect(5+0*(1:4), 2:8) == 5+0*(1:4)
        #@test isempty(intersect(5+0*(1:4), -7:3))
        @test intersect(0:3:24, 0:4:24) == 0:12:24
        @test intersect(0:4:24, 0:3:24) == 0:12:24
        @test intersect(0:3:24, 24:-4:0) == 0:12:24
        @test intersect(24:-3:0, 0:4:24) == 24:-12:0
        @test intersect(24:-3:0, 24:-4:0) == 24:-12:0
        @test intersect(1:3:24, 0:4:24) == 4:12:16
        @test intersect(0:6:24, 0:4:24) == 0:12:24
        @test isempty(intersect(1:6:2400, 0:4:2400))
        @test intersect(-51:5:100, -33:7:125) == -26:35:79
        @test intersect(-51:5:100, -32:7:125) == -11:35:94
        #@test intersect(0:6:24, 6+0*(0:4:24)) == 6:6:6
        #@test intersect(12+0*(0:6:24), 0:4:24) == AbstractRange(12, 0, 5)
        #@test isempty(intersect(6+0*(0:6:24), 0:4:24))
        @test intersect(-10:3:24, -10:3:24) == -10:3:23
        @test isempty(intersect(-11:3:24, -10:3:24))
        @test intersect(typemin(Int):2:typemax(Int),1:10) == 2:2:10
        @test intersect(1:10,typemin(Int):2:typemax(Int)) == 2:2:10

        @test intersect(reverse(typemin(Int):2:typemax(Int)),typemin(Int):2:typemax(Int)) == reverse(typemin(Int):2:typemax(Int))
        @test intersect(typemin(Int):2:typemax(Int),reverse(typemin(Int):2:typemax(Int))) == typemin(Int):2:typemax(Int)

        @test intersect(UnitRange(1,2),3) == UnitRange(3,2)
        @test intersect(UnitRange(1,2), UnitRange(1,5), UnitRange(3,7), UnitRange(4,6)) == UnitRange(4,3)

        @test intersect(1:3, 2) === intersect(2, 1:3) === 2:2
        @test intersect(1.0:3.0, 2) == intersect(2, 1.0:3.0) == [2.0]
    end
    @testset "sort/sort!/partialsort" begin
        @test sort(UnitRange(1,2)) == UnitRange(1,2)
        @test sort!(UnitRange(1,2)) == UnitRange(1,2)
        @test sort(1:10, rev=true) == 10:-1:1
        @test sort(-3:3, by=abs) == [0,-1,1,-2,2,-3,3]
        @test partialsort(1:10, 4) == 4
    end
    @testset "in" begin
        @test 0 in UInt(0):100:typemax(UInt)
        @test last(UInt(0):100:typemax(UInt)) in UInt(0):100:typemax(UInt)
        @test -9223372036854775790 in -9223372036854775790:100:9223372036854775710
        @test -9223372036854775690 in -9223372036854775790:100:9223372036854775710
        @test -90 in -9223372036854775790:100:9223372036854775710
        @test 10 in -9223372036854775790:100:9223372036854775710
        @test 110 in -9223372036854775790:100:9223372036854775710
        @test 9223372036854775610 in -9223372036854775790:100:9223372036854775710
        @test 9223372036854775710 in -9223372036854775790:100:9223372036854775710

        @test !(3.5 in 1:5)
        @test (3 in 1:5)
        @test (3 in 5:-1:1)
        #@test (3 in 3+0*(1:5))
        #@test !(4 in 3+0*(1:5))

        let r = 0.0:0.01:1.0
            @test (r[30] in r)
        end
        let r = (-4*Int64(maxintfloat(Int === Int32 ? Float32 : Float64))):5
            @test (3 in r)
            @test (3.0 in r)
        end

        @test !(1 in 1:0)
        @test !(1.0 in 1.0:0.0)
    end
    @testset "in() works across types, including non-numeric types (#21728)" begin
        @test 1//1 in 1:3
        @test 1//1 in 1.0:3.0
        @test !(5//1 in 1:3)
        @test !(5//1 in 1.0:3.0)
        @test Complex(1, 0) in 1:3
        @test Complex(1, 0) in 1.0:3.0
        @test Complex(1.0, 0.0) in 1:3
        @test Complex(1.0, 0.0) in 1.0:3.0
        @test !(Complex(1, 1) in 1:3)
        @test !(Complex(1, 1) in 1.0:3.0)
        @test !(Complex(1.0, 1.0) in 1:3)
        @test !(Complex(1.0, 1.0) in 1.0:3.0)
        @test !(π in 1:3)
        @test !(π in 1.0:3.0)
        @test !("a" in 1:3)
        @test !("a" in 1.0:3.0)
        @test !(1 in Date(2017, 01, 01):Dates.Day(1):Date(2017, 01, 05))
        @test !(Complex(1, 0) in Date(2017, 01, 01):Dates.Day(1):Date(2017, 01, 05))
        @test !(π in Date(2017, 01, 01):Dates.Day(1):Date(2017, 01, 05))
        @test !("a" in Date(2017, 01, 01):Dates.Day(1):Date(2017, 01, 05))
    end
end
@testset "indexing range with empty range (#4309)" begin
    @test (3:6)[5:4] == 7:6
    @test_throws BoundsError (3:6)[5:5]
    @test_throws BoundsError (3:6)[5]
    @test (0:2:10)[7:6] == 12:2:10
    @test_throws BoundsError (0:2:10)[7:7]
end
# indexing with negative ranges (#8351)
for a=AbstractRange[3:6, 0:2:10], b=AbstractRange[0:1, 2:-1:0]
    @test_throws BoundsError a[b]
end

# avoiding intermediate overflow (#5065)
@test length(1:4:typemax(Int)) == div(typemax(Int),4) + 1

@testset "overflow in length" begin
    @test_throws OverflowError length(0:typemax(Int))
    @test_throws OverflowError length(typemin(Int):typemax(Int))
    @test_throws OverflowError length(-1:typemax(Int)-1)
end
@testset "loops involving typemin/typemax" begin
    n = 0
    s = 0
    # loops ending at typemax(Int)
    for i = (typemax(Int)-1):typemax(Int)
        s += 1
        @test s <= 2
    end
    @test s == 2

    s = 0
    for i = (typemax(Int)-2):(typemax(Int)-1)
        s += 1
        @test s <= 2
    end
    @test s == 2

    s = 0
    for i = typemin(Int):(typemin(Int)+1)
        s += 1
        @test s <= 2
    end
    @test s == 2

    # loops covering the full range of smaller integer types
    s = 0
    for i = typemin(UInt8):typemax(UInt8)
        s += 1
    end
    @test s == 256

    s = 0
    for i = typemin(UInt8):one(UInt8):typemax(UInt8)
        s += 1
    end
    @test s == 256

    # loops past typemax(Int)
    n = 0
    s = Int128(0)
    for i = typemax(UInt64)-2:typemax(UInt64)
        n += 1
        s += i
    end
    @test n == 3
    @test s == 3*Int128(typemax(UInt64)) - 3

    # loops over empty ranges
    s = 0
    for i = 0xff:0x00
        s += 1
    end
    @test s == 0

    s = 0
    for i = Int128(typemax(Int128)):Int128(typemin(Int128))
        s += 1
    end
    @test s == 0
end

@testset "sums of ranges" begin
    @test sum(1:100) == 5050
    @test sum(0:100) == 5050
    @test sum(-100:100) == 0
    @test sum(0:2:100) == 2550
end
@testset "overflowing sums (see #5798)" begin
    if Sys.WORD_SIZE == 64
        @test sum(Int128(1):10^18) == div(10^18 * (Int128(10^18)+1), 2)
        @test sum(Int128(1):10^18-1) == div(10^18 * (Int128(10^18)-1), 2)
    else
        @test sum(Int64(1):10^9) == div(10^9 * (Int64(10^9)+1), 2)
        @test sum(Int64(1):10^9-1) == div(10^9 * (Int64(10^9)-1), 2)
    end
end
@testset "Tricky sums of StepRangeLen #8272" begin
    @test sum(10000.:-0.0001:0) == 5.00000005e11
    @test sum(0:0.001:1) == 500.5
    @test sum(0:0.000001:1) == 500000.5
    @test sum(0:0.1:10) == 505.
end
@testset "broadcasted operations with scalars" begin
    @test broadcast(-, 1:3, 2) == -1:1
    @test broadcast(-, 1:3, 0.25) == 1-0.25:3-0.25
    @test broadcast(+, 1:3, 2) == 3:5
    @test broadcast(+, 1:3, 0.25) == 1+0.25:3+0.25
    @test broadcast(+, 1:2:6, 1) == 2:2:6
    @test broadcast(+, 1:2:6, 0.3) == 1+0.3:2:5+0.3
    @test broadcast(-, 1:2:6, 1) == 0:2:4
    @test broadcast(-, 1:2:6, 0.3) == 1-0.3:2:5-0.3
    @test broadcast(-, 2, 1:3) == 1:-1:-1
end
@testset "operations between ranges and arrays" begin
    @test all(([1:5;] + (5:-1:1)) .== 6)
    @test all(((5:-1:1) + [1:5;]) .== 6)
    @test all(([1:5;] - (1:5)) .== 0)
    @test all(((1:5) - [1:5;]) .== 0)
end
@testset "tricky floating-point ranges" begin
    for (start, step, stop, len) in ((1, 1, 3, 3), (0, 1, 3, 4),
                                    (3, -1, -1, 5), (1, -1, -3, 5),
                                    (0, 1, 10, 11), (0, 7, 21, 4),
                                    (0, 11, 33, 4), (1, 11, 34, 4),
                                    (0, 13, 39, 4), (1, 13, 40, 4),
                                    (11, 11, 33, 3), (3, 1, 11, 9),
                                    (0, 10, 55, 0), (0, -1, 5, 0), (0, 10, 5, 0),
                                    (0, 1, 5, 0), (0, -10, 5, 0), (0, -10, 0, 1),
                                    (0, -1, 1, 0), (0, 1, -1, 0), (0, -1, -10, 11))
        r = start/10:step/10:stop/10
        a = Vector(start:step:stop)./10
        ra = Vector(r)

        @test r == a
        @test isequal(r, a)

        @test r == ra
        @test isequal(r, ra)

        @test hash(r) == hash(a)
        @test hash(r) == hash(ra)

        if len > 0
            l = linspace(start/10, stop/10, len)
            la = Vector(l)

            @test a == l
            @test r == l
            @test isequal(a, l)
            @test isequal(r, l)

            @test l == la
            @test isequal(l, la)

            @test hash(l) == hash(a)
            @test hash(l) == hash(la)
        end
    end

    @test 1.0:1/49:27.0 == linspace(1.0,27.0,1275) == [49:1323;]./49
    @test isequal(1.0:1/49:27.0, linspace(1.0,27.0,1275))
    @test isequal(1.0:1/49:27.0, Vector(49:1323)./49)
    @test hash(1.0:1/49:27.0) == hash(linspace(1.0,27.0,1275)) == hash(Vector(49:1323)./49)

    @test [prevfloat(0.1):0.1:0.3;] == [prevfloat(0.1), 0.2, 0.3]
    @test [nextfloat(0.1):0.1:0.3;] == [nextfloat(0.1), 0.2]
    @test [prevfloat(0.0):0.1:0.3;] == [prevfloat(0.0), 0.1, 0.2]
    @test [nextfloat(0.0):0.1:0.3;] == [nextfloat(0.0), 0.1, 0.2]
    @test [0.1:0.1:prevfloat(0.3);] == [0.1, 0.2]
    @test [0.1:0.1:nextfloat(0.3);] == [0.1, 0.2, nextfloat(0.3)]
    @test [0.0:0.1:prevfloat(0.3);] == [0.0, 0.1, 0.2]
    @test [0.0:0.1:nextfloat(0.3);] == [0.0, 0.1, 0.2, nextfloat(0.3)]
    @test [0.1:prevfloat(0.1):0.3;] == [0.1, 0.2, 0.3]
    @test [0.1:nextfloat(0.1):0.3;] == [0.1, 0.2]
    @test [0.0:prevfloat(0.1):0.3;] == [0.0, prevfloat(0.1), prevfloat(0.2), 0.3]
    @test [0.0:nextfloat(0.1):0.3;] == [0.0, nextfloat(0.1), nextfloat(0.2)]
end
@testset "issue #7420 for type $T" for T = (Float32, Float64,), # BigFloat),
    a = -5:25,
    s = [-5:-1; 1:25; ],
    d = 1:25,
    n = -1:15

    denom = convert(T, d)
    strt = convert(T, a)/denom
    Δ     = convert(T, s)/denom
    stop  = convert(T, (a + (n - 1) * s)) / denom
    vals  = T[a:s:(a + (n - 1) * s); ] ./ denom
    r = strt:Δ:stop
    @test [r;] == vals
    @test [linspace(strt, stop, length(r));] == vals
    n = length(r)
    @test [r[1:n];] == [r;]
    @test [r[2:n];] == [r;][2:end]
    @test [r[1:3:n];] == [r;][1:3:n]
    @test [r[2:2:n];] == [r;][2:2:n]
    @test [r[n:-1:2];] == [r;][n:-1:2]
    @test [r[n:-2:1];] == [r;][n:-2:1]
end

@testset "issue #20373 (unliftable ranges with exact end points)" begin
    @test [3*0.05:0.05:0.2;]    == [linspace(3*0.05,0.2,2);]   == [3*0.05,0.2]
    @test [0.2:-0.05:3*0.05;]   == [linspace(0.2,3*0.05,2);]   == [0.2,3*0.05]
    @test [-3*0.05:-0.05:-0.2;] == [linspace(-3*0.05,-0.2,2);] == [-3*0.05,-0.2]
    @test [-0.2:0.05:-3*0.05;]  == [linspace(-0.2,-3*0.05,2);] == [-0.2,-3*0.05]
end

function range_fuzztests(::Type{T}, niter, nrange) where {T}
    for i = 1:niter, n in nrange
        strt, Δ = randn(T), randn(T)
        Δ == 0 && continue
        stop = strt + (n-1)*Δ
        # `n` is not necessarily unique s.t. `strt + (n-1)*Δ == stop`
        # so test that `length(strt:Δ:stop)` satisfies this identity
        # and is the closest value to `(stop-strt)/Δ` to do so
        lo = hi = n
        while strt + (lo-1)*Δ == stop; lo -= 1; end
        while strt + (hi-1)*Δ == stop; hi += 1; end
        m = clamp(round(Int, (stop-strt)/Δ) + 1, lo+1, hi-1)
        r = strt:Δ:stop
        @test m == length(r)
        @test strt == first(r)
        @test Δ == step(r)
        @test_skip stop == last(r)
        l = linspace(strt,stop,n)
        @test n == length(l)
        @test strt == first(l)
        @test stop  == last(l)
    end
end
@testset "range fuzztests for $T" for T = (Float32, Float64,)
    range_fuzztests(T, 2^15, 1:5)
end

@testset "Inexact errors on 32 bit architectures. #22613" begin
    @test first(linspace(log(0.2), log(10.0), 10)) == log(0.2)
    @test last(linspace(log(0.2), log(10.0), 10)) == log(10.0)
    @test length(Base.floatrange(-3e9, 1.0, 1, 1.0)) == 1
end

@testset "linspace & ranges with very small endpoints for type $T" for T = (Float32, Float64)
    z = zero(T)
    u = eps(z)
    @test first(linspace(u,u,0)) == u
    @test last(linspace(u,u,0)) == u
    @test first(linspace(-u,u,0)) == -u
    @test last(linspace(-u,u,0)) == u
    @test [linspace(-u,u,0);] == []
    @test [linspace(-u,-u,1);] == [-u]
    @test [linspace(-u,u,2);] == [-u,u]
    @test [linspace(-u,u,3);] == [-u,0,u]
    @test first(linspace(-u,-u,0)) == -u
    @test last(linspace(-u,-u,0)) == -u
    @test first(linspace(u,-u,0)) == u
    @test last(linspace(u,-u,0)) == -u
    @test [linspace(u,-u,0);] == []
    @test [linspace(u,u,1);] == [u]
    @test [linspace(u,-u,2);] == [u,-u]
    @test [linspace(u,-u,3);] == [u,0,-u]
    v = linspace(-u,u,12)
    @test length(v) == 12
    @test [-3u:u:3u;] == [linspace(-3u,3u,7);] == [-3:3;].*u
    @test [3u:-u:-3u;] == [linspace(3u,-3u,7);] == [3:-1:-3;].*u
end

@testset "linspace with very large endpoints for type $T" for T = (Float32, Float64)
    largeint = Int(min(maxintfloat(T), typemax(Int)))
    a = realmax()
    for i = 1:5
        @test [linspace(a,a,1);] == [a]
        @test [linspace(-a,-a,1);] == [-a]
        b = realmax()
        for j = 1:5
            @test [linspace(-a,b,0);] == []
            @test [linspace(-a,b,2);] == [-a,b]
            @test [linspace(-a,b,3);] == [-a,(b-a)/2,b]
            @test [linspace(a,-b,0);] == []
            @test [linspace(a,-b,2);] == [a,-b]
            @test [linspace(a,-b,3);] == [a,(a-b)/2,-b]
            for c = largeint-3:largeint
                s = linspace(-a,b,c)
                @test first(s) == -a
                @test last(s) == b
                @test length(s) == c
                s = linspace(a,-b,c)
                @test first(s) == a
                @test last(s) == -b
                @test length(s) == c
            end
            b = prevfloat(b)
        end
        a = prevfloat(a)
    end
end

# issue #20380
let r = LinSpace(1,4,4)
    @test isa(r[1:4], LinSpace)
end

@testset "linspace with 1 or 0 elements (whose step length is NaN)" begin
    @test issorted(linspace(1,1,0))
    @test issorted(linspace(1,1,1))
end
# near-equal ranges
@test 0.0:0.1:1.0 != 0.0f0:0.1f0:1.0f0

# comparing and hashing ranges
@testset "comparing and hashing ranges" begin
    Rs = AbstractRange[1:1, 1:1:1, 1:2, 1:1:2,
                       map(Int32,1:3:17), map(Int64,1:3:17), 1:0, 1:-1:0, 17:-3:0,
                       0.0:0.1:1.0, map(Float32,0.0:0.1:1.0),
                       1.0:eps():1.0 .+ 10eps(), 9007199254740990.:1.0:9007199254740994,
                       linspace(0, 1, 20), map(Float32, linspace(0, 1, 20))]
    for r in Rs
        local r
        ar = Vector(r)
        @test r == ar
        @test isequal(r,ar)
        @test hash(r) == hash(ar)
        for s in Rs
            as = Vector(s)
            @test isequal(r,s) == (hash(r)==hash(s))
            @test (r==s) == (ar==as)
        end
    end
end

@testset "comparing UnitRanges and OneTo" begin
    @test 1:2:10 == 1:2:10 != 1:3:10 != 1:3:13 != 2:3:13 == 2:3:11 != 2:11
    @test 1:1:10 == 1:10 == 1:10 == Base.OneTo(10) == Base.OneTo(10)
    @test 1:10 != 2:10 != 2:11 != Base.OneTo(11)
    @test Base.OneTo(10) != Base.OneTo(11) != 1:10
end
# issue #2959
@test 1.0:1.5 == 1.0:1.0:1.5 == 1.0:1.0
#@test 1.0:(.3-.1)/.1 == 1.0:2.0

@testset "length with typemin/typemax" begin
    let r = typemin(Int64):2:typemax(Int64), s = typemax(Int64):-2:typemin(Int64)
        @test first(r) == typemin(Int64)
        @test last(r) == (typemax(Int64)-1)
        @test_throws OverflowError length(r)

        @test first(s) == typemax(Int64)
        @test last(s) == (typemin(Int64)+1)
        @test_throws OverflowError length(s)
    end

    @test length(typemin(Int64):3:typemax(Int64)) == 6148914691236517206
    @test length(typemax(Int64):-3:typemin(Int64)) == 6148914691236517206

    for s in 3:100
        @test length(typemin(Int):s:typemax(Int)) == length(big(typemin(Int)):big(s):big(typemax(Int)))
        @test length(typemax(Int):-s:typemin(Int)) == length(big(typemax(Int)):big(-s):big(typemin(Int)))
    end

    @test length(UInt(1):UInt(1):UInt(0)) == 0
    @test length(typemax(UInt):UInt(1):(typemax(UInt)-1)) == 0
    @test length(typemax(UInt):UInt(2):(typemax(UInt)-1)) == 0
    @test length((typemin(Int)+3):5:(typemin(Int)+1)) == 0
end
# issue #6364
@test length((1:64)*(pi/5)) == 64

@testset "issue #6973" begin
    r1 = 1.0:0.1:2.0
    r2 = 1.0f0:0.2f0:3.0f0
    r3 = 1:2:21
    @test r1 + r1 == 2*r1
    @test r1 + r2 == 2.0:0.3:5.0
    @test (r1 + r2) - r2 == r1
    @test r1 + r3 == convert(StepRangeLen{Float64}, r3) + r1
    @test r3 + r3 == 2 * r3
end

@testset "issue #7114" begin
    let r = -0.004532318104333742:1.2597349521122731e-5:0.008065031416788989
        @test length(r[1:end-1]) == length(r) - 1
        @test isa(r[1:2:end],AbstractRange) && length(r[1:2:end]) == div(length(r)+1, 2)
        @test r[3:5][2] ≈ r[4]
        @test r[5:-2:1][2] ≈ r[3]
        @test_throws BoundsError r[0:10]
        @test_throws BoundsError r[1:10000]
    end

    let r = linspace(1/3, 5/7, 6)
        @test length(r) == 6
        @test r[1] == 1/3
        @test abs(r[end] - 5/7) <= eps(5/7)
    end

    let r = linspace(0.25, 0.25, 1)
        @test length(r) == 1
        @test_throws ArgumentError linspace(0.25,0.5,1)
    end
end

# issue #7426
@test [typemax(Int):1:typemax(Int);] == [typemax(Int)]

#issue #7484
let r7484 = 0.1:0.1:1
    @test [reverse(r7484);] == reverse([r7484;])
end

@testset "issue #7387" begin
    for r in (0:1, 0.0:1.0)
        local r
        @test [r .+ im;] == [r;] .+ im
        @test [r .- im;] == [r;] .- im
        @test [r * im;] == [r;] * im
        @test [r / im;] == [r;] / im
    end
end
# Preservation of high precision upon addition
let r = (-0.1:0.1:0.3) + broadcast(+, -0.3:0.1:0.1, 1e-12)
    @test r[3] == 1e-12
end

@testset "issue #7709" begin
    @test length(map(identity, 0x01:0x05)) == 5
    @test length(map(identity, 0x0001:0x0005)) == 5
    @test length(map(identity, UInt64(1):UInt64(5))) == 5
    @test length(map(identity, UInt128(1):UInt128(5))) == 5
end
@testset "mean/median" begin
    for f in (mean, median)
        for n = 2:5
            @test f(2:n) == f([2:n;])
            @test f(2:0.1:n) ≈ f([2:0.1:n;])
        end
    end
end
@testset "issue #8531" begin
    smallint = (Int === Int64 ?
                (Int8,UInt8,Int16,UInt16,Int32,UInt32) :
                (Int8,UInt8,Int16,UInt16))
    for T in smallint
        @test length(typemin(T):typemax(T)) == 2^(8*sizeof(T))
    end
end

# issue #8584
@test (0:1//2:2)[1:2:3] == 0:1//1:1

# issue #12278
@test length(1:UInt(0)) == 0

@testset "zip" begin
    i = 0
    x = 1:2:8
    y = 2:2:8
    xy = 1:8
    for (thisx, thisy) in zip(x, y)
        @test thisx == xy[i+=1]
        @test thisy == xy[i+=1]
    end
end

@testset "issue #9962" begin
    @test eltype(0:1//3:10) <: Rational
    @test (0:1//3:10)[1] == 0
    @test (0:1//3:10)[2] == 1//3
end
@testset "converting ranges (issue #10965)" begin
    @test promote(0:1, UInt8(2):UInt8(5)) === (0:1, 2:5)
    @test convert(UnitRange{Int}, 0:5) === 0:5
    @test convert(UnitRange{Int128}, 0:5) === Int128(0):Int128(5)

    @test promote(0:1:1, UInt8(2):UInt8(1):UInt8(5)) === (0:1:1, 2:1:5)
    @test convert(StepRange{Int,Int}, 0:1:1) === 0:1:1
    @test convert(StepRange{Int128,Int128}, 0:1:1) === Int128(0):Int128(1):Int128(1)

    @test promote(0:1:1, 2:5) === (0:1:1, 2:1:5)
    @test convert(StepRange{Int128,Int128}, 0:5) === Int128(0):Int128(1):Int128(5)
    @test convert(StepRange, 0:5) === 0:1:5
    @test convert(StepRange{Int128,Int128}, 0.:5) === Int128(0):Int128(1):Int128(5)

    @test_throws ArgumentError StepRange(1.1,1,5.1)

    @test promote(0f0:inv(3f0):1f0, 0.:2.:5.) === (0:1/3:1, 0.:2.:5.)

    @test convert(StepRangeLen{Float64}, 0:1/3:1) === 0:1/3:1
    @test convert(StepRangeLen{Float64}, 0f0:inv(3f0):1f0) === 0:1/3:1

    @test promote(0:1/3:1, 0:5) === (0:1/3:1, 0.:1.:5.)
    @test convert(StepRangeLen{Float64}, 0:5) === 0.:1.:5.
    @test convert(StepRangeLen{Float64}, 0:1:5) === 0.:1.:5.
    @test convert(StepRangeLen, 0:5) == 0:5
    @test convert(StepRangeLen, 0:1:5) == 0:1:5

    @test convert(LinSpace{Float64}, 0.0:0.1:0.3) === LinSpace{Float64}(0.0, 0.3, 4)
    @test convert(LinSpace, 0.0:0.1:0.3) === LinSpace{Float64}(0.0, 0.3, 4)
    @test convert(LinSpace, 0:3) === LinSpace{Int}(0, 3, 4)

    @test promote('a':'z', 1:2) === ('a':'z', 1:1:2)
    @test eltype(['a':'z', 1:2]) == (StepRange{T,Int} where T)
end

@testset "LinSpace ops" begin
    @test start(LinSpace(0,3,4)) == 1
    @test 2*LinSpace(0,3,4) == LinSpace(0,6,4)
    @test LinSpace(0,3,4)*2 == LinSpace(0,6,4)
    @test LinSpace(0,3,4)/3 == LinSpace(0,1,4)
    @test broadcast(-, 2, LinSpace(0,3,4)) == LinSpace(2,-1,4)
    @test broadcast(+, 2, LinSpace(0,3,4)) == LinSpace(2,5,4)
    @test -LinSpace(0,3,4) == LinSpace(0,-3,4)
    @test reverse(LinSpace(0,3,4)) == LinSpace(3,0,4)
end
@testset "Issue #11245" begin
    io = IOBuffer()
    show(io, linspace(1, 2, 3))
    str = String(take!(io))
#    @test str == "linspace(1.0,2.0,3)"
    @test str == "1.0:0.5:2.0"
end

@testset "issue 10950" begin
    r = 1//2:3
    @test length(r) == 3
    i = 1
    for x in r
        @test x == i//2
        i += 2
    end
    @test i == 7
end

@testset "stringmime/repr" begin
    # stringmime/show should display the range or linspace nicely
    # to test print_range in range.jl
    replstrmime(x) = sprint((io,x) -> show(IOContext(io, :limit => true, :displaysize => (24, 80)), MIME("text/plain"), x), x)
    @test replstrmime(1:4) == "1:4"
    @test stringmime("text/plain", 1:4) == "1:4"
    @test stringmime("text/plain", linspace(1,5,7)) == "1.0:0.6666666666666666:5.0"
    @test stringmime("text/plain", LinSpace{Float64}(1,5,7)) == "7-element LinSpace{Float64}:\n 1.0,1.66667,2.33333,3.0,3.66667,4.33333,5.0"
    @test repr(linspace(1,5,7)) == "1.0:0.6666666666666666:5.0"
    @test repr(LinSpace{Float64}(1,5,7)) == "linspace(1.0,5.0,7)"
    @test replstrmime(0:100.) == "0.0:1.0:100.0"
    # next is to test a very large range, which should be fast because print_range
    # only examines spacing of the left and right edges of the range, sufficient
    # to cover the designated screen size.
    @test replstrmime(linspace(0,100, 10000)) == "0.0:0.010001000100010001:100.0"
    @test replstrmime(LinSpace{Float64}(0,100, 10000)) == "10000-element LinSpace{Float64}:\n 0.0,0.010001,0.020002,0.030003,0.040004,…,99.95,99.96,99.97,99.98,99.99,100.0"

    @test sprint(show, UnitRange(1, 2)) == "1:2"
    @test sprint(show, StepRange(1, 2, 5)) == "1:2:5"
end

@testset "Issue 11049 and related" begin
    @test promote(linspace(0f0, 1f0, 3), linspace(0., 5., 2)) ===
        (linspace(0., 1., 3), linspace(0., 5., 2))
    @test convert(LinSpace{Float64}, linspace(0., 1., 3)) === LinSpace(0., 1., 3)
    @test convert(LinSpace{Float64}, linspace(0f0, 1f0, 3)) === LinSpace(0., 1., 3)

    @test promote(linspace(0., 1., 3), 0:5) === (linspace(0., 1., 3),
                                                 linspace(0., 5., 6))
    @test convert(LinSpace{Float64}, 0:5) === LinSpace(0., 5., 6)
    @test convert(LinSpace{Float64}, 0:1:5) === LinSpace(0., 5., 6)
    @test convert(LinSpace, 0:5) === LinSpace{Int}(0, 5, 6)
    @test convert(LinSpace, 0:1:5) === LinSpace{Int}(0, 5, 6)

    function test_range_index(r, s)
        @test typeof(r[s]) == typeof(r)
        @test [r;][s] == [r[s];]
    end
    test_range_index(linspace(0.1, 0.3, 3), 1:2)
    test_range_index(linspace(0.1, 0.3, 3), 1:0)
    test_range_index(linspace(1.0, 1.0, 1), 1:1)
    test_range_index(linspace(1.0, 1.0, 1), 1:0)
    test_range_index(linspace(1.0, 2.0, 0), 1:0)

    function test_linspace_identity(r::AbstractRange{T}, mr) where T
        @test -r == mr
        @test -Vector(r) == Vector(mr)
        @test isa(-r, typeof(r))

        @test broadcast(+, broadcast(+, 1, r), -1) == r
        @test 1 .+ Vector(r) == Vector(1 .+ r) == Vector(r .+ 1)
        @test isa(broadcast(+, broadcast(+, 1, r), -1), typeof(r))
        @test broadcast(-, broadcast(-, 1, r), 1) == mr
        @test 1 .- Vector(r) == Vector(1 .- r) == Vector(1 .+ mr)
        @test Vector(r) .- 1 == Vector(r .- 1) == -Vector(mr .+ 1)
        @test isa(broadcast(-, broadcast(-, 1, r), 1), typeof(r))

        @test 1 * r * 1 == r
        @test 2 * r * T(0.5) == r
        @test isa(1 * r * 1, typeof(r))
        @test r / 1 == r
        @test r / 2 * 2 == r
        @test r / T(0.5) * T(0.5) == r
        @test isa(r / 1, typeof(r))

        @test (2 * Vector(r) == Vector(r * 2) == Vector(2 * r) ==
               Vector(r * T(2.0)) == Vector(T(2.0) * r) ==
               Vector(r / T(0.5)) == -Vector(mr * T(2.0)))
    end

    test_linspace_identity(linspace(1.0, 27.0, 10), linspace(-1.0, -27.0, 10))
    test_linspace_identity(linspace(1f0, 27f0, 10), linspace(-1f0, -27f0, 10))

    test_linspace_identity(linspace(1.0, 27.0, 0), linspace(-1.0, -27.0, 0))
    test_linspace_identity(linspace(1f0, 27f0, 0), linspace(-1f0, -27f0, 0))

    test_linspace_identity(linspace(1.0, 1.0, 1), linspace(-1.0, -1.0, 1))
    test_linspace_identity(linspace(1f0, 1f0, 1), linspace(-1f0, -1f0, 1))

    @test reverse(linspace(1.0, 27.0, 1275)) == linspace(27.0, 1.0, 1275)
    @test [reverse(linspace(1.0, 27.0, 1275));] ==
        reverse([linspace(1.0, 27.0, 1275);])
end
@testset "PR 12200 and related" begin
    for _r in (1:2:100, 1:100, 1f0:2f0:100f0, 1.0:2.0:100.0,
               linspace(1, 100, 10), linspace(1f0, 100f0, 10))
        float_r = float(_r)
        big_r = big.(_r)
        @test typeof(big_r).name === typeof(_r).name
        if eltype(_r) <: AbstractFloat
            @test isa(float_r, typeof(_r))
            @test eltype(big_r) === BigFloat
        else
            @test isa(float_r, AbstractRange)
            @test eltype(float_r) <: AbstractFloat
            @test eltype(big_r) === BigInt
        end
    end

    @test_throws DimensionMismatch linspace(1.,5.,5) + linspace(1.,5.,6)
    @test_throws DimensionMismatch linspace(1.,5.,5) - linspace(1.,5.,6)
    @test_throws DimensionMismatch linspace(1.,5.,5) .* linspace(1.,5.,6)
    @test_throws DimensionMismatch linspace(1.,5.,5) ./ linspace(1.,5.,6)

    @test_throws DimensionMismatch (1:5) + (1:6)
    @test_throws DimensionMismatch (1:5) - (1:6)
    @test_throws DimensionMismatch (1:5) .* (1:6)
    @test_throws DimensionMismatch (1:5) ./ (1:6)

    @test_throws DimensionMismatch (1.:5.) + (1.:6.)
    @test_throws DimensionMismatch (1.:5.) - (1.:6.)
    @test_throws DimensionMismatch (1.:5.) .* (1.:6.)
    @test_throws DimensionMismatch (1.:5.) ./ (1.:6.)

    function test_range_sum_diff(r1, r2, r_sum, r_diff)
        @test r1 + r2 == r_sum
        @test r2 + r1 == r_sum
        @test r1 - r2 == r_diff
        @test r2 - r1 == -r_diff

        @test Vector(r1) + Vector(r2) == Vector(r_sum)
        @test Vector(r2) + Vector(r1) == Vector(r_sum)
        @test Vector(r1) - Vector(r2) == Vector(r_diff)
        @test Vector(r2) - Vector(r1) == Vector(-r_diff)
    end

    test_range_sum_diff(1:5, 0:2:8, 1:3:13, 1:-1:-3)
    test_range_sum_diff(1.:5., 0.:2.:8., 1.:3.:13., 1.:-1.:-3.)
    test_range_sum_diff(linspace(1.,5.,5), linspace(0.,-4.,5),
                        linspace(1.,1.,5), linspace(1.,9.,5))

    test_range_sum_diff(1:5, 0.:2.:8., 1.:3.:13., 1.:-1.:-3.)
    test_range_sum_diff(1:5, linspace(0, 8, 5),
                        linspace(1, 13, 5), linspace(1, -3, 5))
    test_range_sum_diff(1.:5., linspace(0, 8, 5),
                        linspace(1, 13, 5), linspace(1, -3, 5))
end
# Issue #12388
let r = 0x02:0x05
    @test r[2:3] == 0x03:0x04
end

@testset "Issue #13738" begin
    for r in (big(1):big(2), UInt128(1):UInt128(2), 0x1:0x2)
        local r
        rr = r[r]
        @test typeof(rr) == typeof(r)
        @test r[r] == r
        # these calls to similar must not throw:
        @test size(similar(r, size(r))) == size(similar(r, length(r)))
    end
end
@testset "sign, conj, ~ (Issue #16067)" begin
    A = -1:1
    B = -1.0:1.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) === B

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

@testset "conversion to Array" begin
    r = 1:3
    a = [1,2,3]
    @test convert(Array, r) == a
    @test convert(Array{Int}, r) == a
    @test convert(Array{Float64}, r) == a
    @test convert(Array{Int,1}, r) == a
    @test convert(Array{Float64,1}, r) == a
end

@testset "OneTo" begin
    let r = Base.OneTo(-5)
        @test isempty(r)
        @test length(r) == 0
        @test size(r) == (0,)
    end
    let r = Base.OneTo(3)
        @test !isempty(r)
        @test length(r) == 3
        @test size(r) == (3,)
        @test step(r) == 1
        @test first(r) == 1
        @test last(r) == 3
        @test minimum(r) == 1
        @test maximum(r) == 3
        @test r[2] == 2
        @test r[2:3] === 2:3
        @test_throws BoundsError r[4]
        @test_throws BoundsError r[0]
        @test broadcast(+, r, 1) === 2:4
        @test 2*r === 2:2:6
        @test r + r === 2:2:6
        k = 0
        for i in r
            @test i == (k += 1)
        end
        @test intersect(r, Base.OneTo(2)) == Base.OneTo(2)
        @test intersect(r, 0:5) == 1:3
        @test intersect(r, 2) === intersect(2, r) === 2:2
        @test findall(occursin(r), r) === findall(occursin(1:length(r)), r) ===
              findall(occursin(r), 1:length(r)) === 1:length(r)
        io = IOBuffer()
        show(io, r)
        str = String(take!(io))
        @test str == "Base.OneTo(3)"
    end
    let r = Base.OneTo(7)
        @test findall(occursin(2:(length(r) - 1)), r) === 2:(length(r) - 1)
        @test findall(occursin(r), 2:(length(r) - 1)) === 1:(length(r) - 2)
    end
end

@testset "linspace of other types" begin
    let r = linspace(0, 3//10, 4)
        @test eltype(r) == Rational{Int}
        @test r[2] === 1//10
    end

    let a = 1.0,
        b = nextfloat(1.0),
        ba = BigFloat(a),
        bb = BigFloat(b),
        r = linspace(ba, bb, 3)
        @test eltype(r) == BigFloat
        @test r[1] == a && r[3] == b
        @test r[2] == (ba+bb)/2
    end

    let (a, b) = (rand(10), rand(10)),
        r = linspace(a, b, 5)
        @test r[1] == a && r[5] == b
        for i = 2:4
            x = ((5 - i) // 4) * a + ((i - 1) // 4) * b
            @test r[i] == x
        end
    end
end
@testset "issue #23178" begin
    r = linspace(Float16(0.1094), Float16(0.9697), 300)
    @test r[1] == Float16(0.1094)
    @test r[end] == Float16(0.9697)
end

# issue #20382
let r = @inferred(colon(big(1.0),big(2.0),big(5.0)))
    @test eltype(r) == BigFloat
end

@testset "issue #14420" begin
    for r in (linspace(0.10000000000000045, 1, 50), 0.10000000000000045:(1-0.10000000000000045)/49:1)
        local r
        @test r[1] === 0.10000000000000045
        @test r[end] === 1.0
    end
end
@testset "issue #20381" begin
    r = linspace(-big(1.0),big(1.0),4)
    @test isa(@inferred(r[2]), BigFloat)
    @test r[2] ≈ big(-1.0)/3
end

@testset "issue #20520" begin
    r = linspace(1.3173739f0, 1.3173739f0, 3)
    @test length(r) == 3
    @test first(r) === 1.3173739f0
    @test last(r)  === 1.3173739f0
    @test r[2]     === 1.3173739f0
    r = linspace(1.0, 3+im, 4)
    @test r[1] === 1.0+0.0im
    @test r[2] ≈ (5/3)+(1/3)im
    @test r[3] ≈ (7/3)+(2/3)im
    @test r[4] === 3.0+im
end

# ambiguity between colon methods (#20988)
struct NotReal; val; end
Base.:+(x, y::NotReal) = x + y.val
Base.zero(y::NotReal) = zero(y.val)
Base.rem(x, y::NotReal) = rem(x, y.val)
Base.isless(x, y::NotReal) = isless(x, y.val)
@test colon(1, NotReal(1), 5) isa StepRange{Int,NotReal}

isdefined(Main, :TestHelpers) || @eval Main include("TestHelpers.jl")
using .Main.TestHelpers: Furlong
@testset "dimensional correctness" begin
    @test length(Vector(Furlong(2):Furlong(10))) == 9
    @test length(range(Furlong(2), 9)) == 9
    @test Vector(Furlong(2):Furlong(1):Furlong(10)) == Vector(range(Furlong(2),Furlong(1),9)) == Furlong.(2:10)
    @test Vector(Furlong(1.0):Furlong(0.5):Furlong(10.0)) ==
          Vector(Furlong(1):Furlong(0.5):Furlong(10)) == Furlong.(1:0.5:10)
end

@testset "issue #22270" begin
    linsp = linspace(1.0, 2.0, 10)
    @test typeof(linsp.ref) == Base.TwicePrecision{Float64}
    @test Float32(linsp.ref) === convert(Float32, linsp.ref)
    @test Float32(linsp.ref) ≈ linsp.ref.hi + linsp.ref.lo
end

@testset "logspace" begin
    n = 10; a = 2; b = 4
    # test default values; base = 10
    @test logspace(a, b, 50) == 10 .^ linspace(a, b, 50)
    @test logspace(a, b, n) == 10 .^ linspace(a, b, n)
    for base in (10, 2, ℯ)
        @test logspace(a, b, 50, base=base) == base.^linspace(a, b, 50)
        @test logspace(a, b, n, base=base) == base.^linspace(a, b, n)
    end
end

@testset "issue #23300" begin
    x = -5:big(1.0):5
    @test map(Float64, x) === -5.0:1.0:5.0
    @test map(Float32, x) === -5.0f0:1.0f0:5.0f0
    @test map(Float16, x) === Float16(-5.0):Float16(1.0):Float16(5.0)
    @test map(BigFloat, x) === x
end

max_stars_repo_name: nreigl/julia

max_issues_repo_name: BenChung/Julia-BTI

max_forks_repo_name: BenChung/Julia-BTI


text:
# ------------ Choice sites ------------ #

@inline function (ctx::GenerateContext)(call::typeof(rand),
                                        addr::T,
                                        d::Distribution{K}) where {T <: Address, K}
    visit!(ctx, addr)
    if has_top(ctx.select, addr)
        s = get_top(ctx.select, addr)
        score = logpdf(d, s)
        add_choice!(ctx, addr, ChoiceSite(score, s))
        increment!(ctx, score)
    else
        s = rand(d)
        add_choice!(ctx, addr, ChoiceSite(logpdf(d, s), s))
    end
    return s
end

# ------------ Learnable ------------ #

@inline function (ctx::GenerateContext)(fn::typeof(learnable), addr::Address)
    visit!(ctx, addr)
    has_top(ctx.params, addr) && return get_top(ctx.params, addr)
    error("Parameter not provided at address $addr.")
end

# ------------ Black box call sites ------------ #

@inline function (ctx::GenerateContext)(c::typeof(rand),
                                        addr::T,
                                        call::Function,
                                        args...) where T <: Address
    visit!(ctx, addr)
    ps = get_subparameters(ctx, addr)
    ss = get_subselection(ctx, addr)
    ret, cl, w = generate(ss, ps, call, args...)
    add_call!(ctx, addr, cl)
    increment!(ctx, w)
    return ret
end

ext: jl


text:
# ------------------------------------------------------------------
# Licensed under the ISC License. See LICENSE in the project root.
# ------------------------------------------------------------------

"""
    DensityRatioWeighter(tdata, [vars]; [options])

Density ratio weights based on empirical distribution of
variables in target data `tdata`. Default to all variables.

## Optional parameters

* `estimator` - Density ratio estimator (default to `LSIF()`)
* `optlib`    - Optimization library (default to `default_optlib(estimator)`)

### Notes

Estimators from `DensityRatioEstimation.jl` are supported.
"""
struct DensityRatioWeighter <: AbstractWeighter
  tdata
  vars
  dre
  optlib
end

function DensityRatioWeighter(tdata, vars=nothing; estimator=LSIF(),
                              optlib=default_optlib(estimator))
  validvars = collect(name.(variables(tdata)))
  wvars = isnothing(vars) ? validvars : vars
  @assert wvars ⊆ validvars "invalid variables ($wvars) for spatial data"
  DensityRatioWeighter(tdata, wvars, estimator, optlib)
end

function weight(sdata, weighter::DensityRatioWeighter)
  # retrieve method parameters
  tdata  = weighter.tdata
  vars   = weighter.vars
  dre    = weighter.dre
  optlib = weighter.optlib

  @assert vars ⊆ name.(variables(sdata)) "invalid variables ($vars) for spatial data"

  # numerator and denominator samples
  Ωnu = view(tdata, vars)
  Ωde = view(sdata, vars)
  xnu = collect(Tables.rows(values(Ωnu)))
  xde = collect(Tables.rows(values(Ωde)))

  # perform denstiy ratio estimation
  ratios = densratio(xnu, xde, dre, optlib=optlib)

  SpatialWeights(domain(sdata), ratios)
end


text:
# function worker_create(n::Integer=1)
#         @info("Creating worker process.")
#         pids = addprocs(1, exeflags = ["-t $(length(CUDA.devices()))", "--project=$(Base.active_project())"])
#         # _load_me_on_workers(GPUInspector; pids=pids)
#         return pids
# end

# function _load_me_on_workers(m::Module=@__MODULE__; pids = workers())
#     @show m
#     # pkgid = Base.PkgId(m)
#     # @everywhere pids Base.require($pkgid)
#     @everywhere pids Base.require(Main, :GPUInspector)
# end

"""
    @worker_create n -> pids
Create `n` workers (i.e. separate Julia processes) and
execute `using GPUInspector, CUDA` on all of them.
Returns the `pids` of the created workers.
"""
macro worker_create(n)
    quote
        nt = length(CUDA.devices())
        @info("Creating $(Int($n)) worker process(es) with $nt Julia threads each.")
        pids = addprocs(
            Int($n); exeflags=["-t $(nt)", "--project=$(Base.active_project())"]
        )
        @everywhere pids using GPUInspector, CUDA
        return pids
    end
end

"Kills all Julia workers."
macro worker_killall()
    quote
        @info("Killing all worker process(es).")
        rmprocs(workers())
        @info("Done.")
        nothing
    end
end

"""
    @worker ex
Creates a worker process, spawns the given command on it,
and kills the worker process once the command has finished execution.

**Implementation:** a Julia thread (we use `@spawn`) will be used to `wait` on the task and kill the worker.

**Examples:**
```julia
@worker GPUInspector.functional()
@worker stresstest(CUDA.devices(); duration=10, verbose=false)
```
"""
macro worker(ex)
    quote
        @info("Creating worker process.")
        pid, = addprocs(
            1;
            exeflags=["-t $(length(CUDA.devices()))", "--project=$(Base.active_project())"],
        )
        @everywhere pid using GPUInspector, CUDA

        $:(; t=Distributed.@spawnat pid $(esc(ex)))

        Threads.@spawn begin
            wait(t)
            # @info("Killing worker process.")
            rmprocs(pid)
        end
        t
    end
end

"""
    @worker pid ex
Spawns the given command on the given worker process.

**Examples:**
```julia
@worker 3 GPUInspector.functional()
@worker 3 stresstest(CUDA.devices(); duration=10, verbose=false)
```
"""
macro worker(pid, ex)
    quote
        @info("Spawning on worker process $(Int($pid)).")
        $:(; t=Distributed.@spawnat $pid $(esc(ex)))
    end
end


text:
# Link the Cities

function plotPath(city)

        n = size(city)[1]

        path = zeros(n + 1, 2)
        path[1: n, :] = city
        path[n + 1, :] = city[1, :]

        plot!(path[:, 1], path[:, 2])

end

max_stars_repo_name: mizu-bai/Try-Julia

max_issues_repo_name: mizu-bai/Data-Science-Demos

max_forks_repo_name: mizu-bai/Data-Science-Demos


text:
using Documenter
using Gridap

makedocs(
  sitename = "Gridap.jl",
  format = Documenter.HTML(),
  modules = [Gridap],
  pages = [
    "Home" => "index.md",
    "Getting Started" => "pages/getting-started.md",
    "Manual" => "pages/manual.md"]
 )

deploydocs(repo = "github.com/gridap/Gridap.jl.git")


text:
# MIT License
#
# Copyright (c) 2018 Martin Biel
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
    AbstractSampler

Abstract supertype for sampler objects.
"""
abstract type AbstractSampler{S <: AbstractScenario} end
"""
    sample(sampler::AbstractSampler{S})

Sample a scenario of type `S` using `sampler`.
"""
function sample(sampler::AbstractSampler)
    return sampler()
end
function Base.show(io::IO, sampler::AbstractSampler{S}) where S <: AbstractScenario
    print(io, "$(S.name.name) sampler")
    return io
end
function Base.show(io::IO, sampler::AbstractSampler{S}) where S <: Scenario
    print(io, "Scenario sampler")
    return io
end
"""
    sample(sampler::AbstractSampler{S}, π::AbstractSampler)

Sample a scenario of type `S` using `sampler` and set the probability of the sampled scenario to `π`.
"""
function sample(sampler::AbstractSampler, π::AbstractFloat)
    scenario = sampler()
    set_probability!(scenario, π)
    return scenario
end
"""
    Sampler

General purpose sampler object that samples Scenario.

See also: [`Scenario`](@ref), [`@scenario`](@ref), [`@sampler`](@ref)
"""
struct Sampler <: AbstractSampler{Scenario}
    sampler::Function
end

function (sampler::Sampler)()
    return sampler.sampler()
end

function sample!(scenarios::Scenarios{S}, sampler::AbstractSampler{S}, n::Integer) where S <: AbstractScenario
    _sample!(scenarios, sampler, n, length(scenarios), 1/n)
    return nothing
end
function sample!(scenarios::Scenarios{S}, sampler::AbstractSampler{Scenario}, n::Integer) where S <: Scenario
    _sample!(scenarios, sampler, n, length(scenarios), 1/n)
    return nothing
end

function _sample!(scenarios::Scenarios, sampler::AbstractSampler, n::Integer, m::Integer, π::AbstractFloat)
    if m > 0
        # Rescale probabilities of existing scenarios
        for scenario in scenarios
            p = probability(scenario) * m / (m+n)
            set_probability!(scenario, p)
        end
        π *= n/(m+n)
    end
    for i = 1:n
        push!(scenarios, sample(sampler, π))
    end
    return nothing
end


text:
using Interact, Mux, WebIO
using InteractBase
#######################
using JuMP
using Juniper
using Ipopt
using Plotly

#ENV["PLOTS_DEFAULT_BACKEND"] = "PlotlyJS"

        function optimization1(Amt1,Days1,xmin1,xmax1,type1)
                optimizer = Juniper.Optimizer
                nl_solver = optimizer_with_attributes(Ipopt.Optimizer, "print_level"=>0)
                #usi5ng LinearAlgebra # for the dot product
                model = Model(optimizer_with_attributes(optimizer, "nl_solver"=>nl_solver))
                #= initialize
                Food,Medicine,Digital,Travel,misc
                =#
                Amount=Amt1#convert(AbstractFloat,10000)
                Days=convert(Int,Days1)
                xmin=xmin1/100#(Float64)[40, 10, 10, 5, 5]/100
                xmax=xmax1/100#(Float64)[60 ,20 ,20, 10, 10]/100

                #xmin=0.2.*rand(5)
                #xmax=xmin .+ (0.99 .-xmin) .*rand(5)

                println(type1)
	            println("hello")
				println(xmin)
                println(xmax)
				println(Amount)

                # Assign Maximum and minimum PerDay Expense
                minXd=(Amount/Days1)*(.90)  # min Assign 90% PerDay
                maxXd=(Amount/Days1)*(1.10)          # max Assign 110% PerDay

				println("hi")
				  println(Days)
                  println(minXd)

                minXdA=minXd.*ones(1,Days)
               println("how")
               println(type1)
                if(type1==1)
                  maxXdA=maxXd.*ones(1,Days)  #Fixed
                else
                  maxXdA=minXdA .+ minXdA .*rand(Days)# rand
                end

                nVar=size(xmin)[1]

                println(nVar)
                W=zeros(nVar,Days)
                Xd=zeros(1,Days)

                # Assign Variable
                W1=repeat(xmin;outer=[1 Days ]) .*1.0
                W2=repeat(xmax;outer=[1 Days ]) .*1.0
                @variable(model, w[i=1:nVar,j=1:Days],lower_bound=W1[i,j],upper_bound=W2[i,j])
                @variable(model, xd[i=1:Days],lower_bound=minXdA[i],upper_bound=maxXdA[i])
                # Assign Minmium and Maximum
                # Assign Constarint
                b=1.0 .*ones(1,Days)

				println(b)
                @constraint(model, con, sum(w,dims=1) .== b)
                b1=Amount
                @constraint(model, con1, sum(xd) .== b1)
                # Run Optimizer
                @objective(model, Min, (sum(w*xd)-Amount))
                #@NLconstraint(m, sum(w[i]*x[i]^2 for i=1:5) <= 45)
                optimize!(model)
                # retrieve the objective value, corresponding x values and the status
                println(JuMP.value.(w))
                println(JuMP.value.(xd))
                println(JuMP.objective_value(model))
                println(JuMP.termination_status(model))
                println((JuMP.value.(w[:,1:4])))

                #y1=Vector{Any}(undef,Days)
                y1 = Array{Float16}(undef, nVar, Days)
                for i=1:Days
                        y1[:,i]=JuMP.value.(w[:,i]) .*JuMP.value.(xd[i])
                end

                # Extract Plot and results

                #ENV["PLOTS_DEFAULT_BACKEND"] = "PlotlyJS"
                ##
                x1 = Vector{String}(undef, Days);
                        for i = 1:Days
                                x1[i]="Day-$i"
                        end
                        println(x1)
                return JuMP,y1,x1;

end

#########################

    d = OrderedDict(:label1 => Widgets.input("Expense Scheduler",style = Dict("font-size"=>"2.8em", "color"=>"#000678","display"=>"flex", "justify-content"=>"center" );type="button"),
        :txt1 => Widgets.textbox(hint="Enter a Number",style = Dict("font-size"=>"1.5em","width"=>"10em", "position"=> "static", "margin-left"=> "2.7em","color"=>"#00f678","text-align"=> "center" ),value="21";label=HTML(string("
Days
"))),
        :txt2 => Widgets.textbox(hint="Enter a Number",style = Dict("font-size"=>"1.5em","width"=>"10em", "position"=> "static", "margin-left"=> "2.7em","color"=>"#00f678","text-align"=> "center" ),value="10000";label=HTML(string("
Total Amount
"))),
        :label2=> Widgets.input("Cost Min %",style = Dict("font-size"=>"1.5em", "color"=>"#000678","display"=>"flex", "justify-content"=>"center" );type="button"),
        :label3=> Widgets.input("Cost Max %",style = Dict("font-size"=>"1.5em", "color"=>"#000678","display"=>"flex", "justify-content"=>"center" );type="button"),
        :txt3 => Widgets.textbox(hint="Enter a Number",style = Dict("font-size"=>"1.5em","width"=>"10em","position"=> "static", "margin-left"=> "2.7em", "border"=>"3px solid #73AD21", "color"=>"#000678","text-align"=> "center" ),value="40";label=HTML(string("
Food
")),),
        :txt4 => Widgets.textbox(hint="Enter a Number",style = Dict("font-size"=>"1.5em","width"=>"10em","position"=> "static", "margin-left"=> "2.6em", "border"=>"3px solid #73AD21", "color"=>"#000678","text-align"=> "center" ),value="60";label=label=HTML(string("
Food
"))),
        :txt5 => Widgets.textbox(hint="Enter a Number",style = Dict("font-size"=>"1.5em","width"=>"10em","position"=> "static", "margin-left"=> ".7em", "border"=>"3px solid #73AD21", "color"=>"#000678","text-align"=> "center" ),value="10";label=HTML(string("
Medicine
"))),
        :txt6 => Widgets.textbox(hint="Enter a Number",style = Dict("font-size"=>"1.5em","width"=>"10em", "position"=> "static", "margin-left"=> ".7em", "border"=>"3px solid #73AD21","color"=>"#000678","text-align"=> "center" ),value="20";label=HTML(string("
Medicine
"))),
        :txt7 => Widgets.textbox(hint="Enter a Number",style = Dict("font-size"=>"1.5em","width"=>"10em","position"=> "static", "margin-left"=> "1.9em", "border"=>"3px solid #73AD21", "color"=>"#000678","text-align"=> "center" ),value="10";label=HTML(string("
Digital
"))),
        :txt8 => Widgets.textbox(hint="Enter a Number",style = Dict("font-size"=>"1.5em","width"=>"10em","position"=> "static", "margin-left"=> "1.9em", "border"=>"3px solid #73AD21", "color"=>"#000678","text-align"=> "center" ),value="20";label=HTML(string("
Digital
"))),
        :txt9 => Widgets.textbox(hint="Enter a Number",style = Dict("font-size"=>"1.5em","width"=>"10em","position"=> "static", "margin-left"=> "2.1em", "border"=>"3px solid #73AD21", "color"=>"#000678","text-align"=> "center" ),value="5";label=HTML(string("
Travel
"))),
        :txt10 => Widgets.textbox(hint="Enter a Number",style = Dict("font-size"=>"1.5em","width"=>"10em","position"=> "static", "margin-left"=> "2.1em", "border"=>"3px solid #73AD21", "color"=>"#000678","text-align"=> "center" ),value="10";label=HTML(string("
Travel
"))),
        :txt11 => Widgets.textbox(hint="Enter a Number",style = Dict("font-size"=>"1.5em","width"=>"10em","position"=> "static", "margin-left"=> "2.7em", "border"=>"3px solid #73AD21", "color"=>"#000678","text-align"=> "center" ),value="5";label=HTML(string("
Misc
"))),
        :txt12 => Widgets.textbox(hint="Enter a Number",style = Dict("font-size"=>"1.5em","width"=>"10em","position"=> "static", "margin-left"=> "2.7em", "border"=>"3px solid #73AD21", "color"=>"#000678","text-align"=> "center" ),value="10";label=HTML(string("
Misc
"))),
        :toggle1 => Widgets.togglebuttons(Dict("Fixed" => 1, "Random" => 2), label=HTML(string("
PerDay Cost Selection
"))),
        :button1    =>Widgets.button("Schedule",style = Dict("position"=> "static", "margin-left"=> "2.7em","width"=>"10em","background-color"=>"#000222")),
		:txtlink => HTML(string("
Julia Code
")),
        #:lat =>  InteractBase.latex(\\sum_{i=1}^{\\infty} e^i")
        )

    plt = Observable{Any}(Plotly.plot())

    #println(type1)

    ## #

    #plt = Interact.@map plot(sin, color = &output)
    ## #

    t = (Interact.Widget{:mywidget}(d))
    #t = Interact.Widget{:mywidget}(d, output = output)
    #ui=dom"div"(ui,button3,py1)
    Interact.@layout! t vbox(:label1, CSSUtil.vskip(1em),
                           hbox(pad(1em, :txt1), CSSUtil.hskip(2em), :txt2,:txtlink),
                             hbox(CSSUtil.hskip(9em),:label2, CSSUtil.hskip(20em), :label3),
                             hbox(:txt3, CSSUtil.hskip(2em), :txt4),
                             hbox(:txt5, CSSUtil.hskip(2em), :txt6),
                             hbox(:txt7, CSSUtil.hskip(2em), :txt8),
                             hbox(:txt9, CSSUtil.hskip(2em), :txt10),
                             hbox(:txt11, CSSUtil.hskip(2em), :txt12),hbox(:toggle1,CSSUtil.hskip(40em),:button1),CSSUtil.vskip(1em),

    );
    ui = dom"div"(t, plt)
    h = on(d[:button1]) do val
           println("Got an update: ", val)
           days1=parse.(Float64,d[:txt1][])
           amt1=parse.(Float64,d[:txt2][])
           xmin1=parse.(Float64,[d[:txt3][],d[:txt5][],d[:txt7][],d[:txt9][],d[:txt11][]])
           xmax1=parse.(Float64,[d[:txt4][],d[:txt6][],d[:txt8][],d[:txt10][],d[:txt12][]])
           type1=Float64(d[:toggle1][])

           JuMP,y1,x1=optimization1(amt1,days1,xmin1,xmax1,type1)

           trace1 = bar(;x=x1,
                 y=y1[1,:],
           name="Food")
           trace2 = bar(x=x1,
           y=y1[2,:],
          name="Medicine")
          trace3 = bar(x=x1,
                 y=y1[3,:],
           name="Digital")
           trace4 = bar(x=x1,
           y=y1[4,:],
           name="Travel")
                 trace5 = bar(x=x1,
                 y=y1[5,:],
                 name="Misc")
                 data = [trace1, trace2,trace3, trace4,trace5]
                 layout = Layout(;barmode="stack")
                 plt1=Plotly.plot(data, layout)
                 map!(t->plt1,plt,d[:button1])
                 #display(plt)
                 #ui = dom"div"(t, plt)
       end

	using Mux
    port=80
    try
      WebIO.webio_serve(page("/", req -> ui), port)
    catch e
      if isa(e, IOError)
        # sleep and then try again
        sleep(0.1)
        WebIO.webio_serve(page("/", req -> ui), port)
      else
        throw(e)
      end
    end

    #using Mux
    #WebIO.webio_serve(page("/", req -> ui), 80)

max_stars_repo_name: amburosesekar/ExpenseScheduler

max_issues_repo_name: amburosesekar/ExpenseScheduler

max_forks_repo_name: amburosesekar/ExpenseScheduler


text:
# Julia wrapper for header: /usr/include/scip/cons_benders.h
# Automatically generated using Clang.jl wrap_c

function SCIPincludeConshdlrBenders(scip)
    ccall((:SCIPincludeConshdlrBenders, libscip), SCIP_RETCODE, (Ptr{SCIP_},), scip)
end

function SCIPconsBendersEnforceSolution(scip, sol, conshdlr, result, type, checkint)
    ccall((:SCIPconsBendersEnforceSolution, libscip), SCIP_RETCODE, (Ptr{SCIP_}, Ptr{SCIP_SOL}, Ptr{SCIP_CONSHDLR}, Ptr{SCIP_RESULT}, SCIP_BENDERSENFOTYPE, UInt32), scip, sol, conshdlr, result, type, checkint)
end


text:
export HelloEvent,
       OutgoingPingEvent,
       RTMError,
       ErrorEvent

@slackevent(HelloEvent, "hello", begin end)

@slackoutevent(OutgoingPingEvent, "ping", begin end)

immutable RTMError
    code::Int64
    msg::UTF8String
end

@slackevent(ErrorEvent, "error", begin
        error::RTMError
    end)

@slackevent(PresenceChangeEvent, "presence_change", begin
        presence::UTF8String
        user::UserId
    end)

@slackevent(UserTypingEvent, "user_typing", begin
        channel::ChannelId
        user::UserId
    end)

@slackevent(ReconnectUrlEvent, "reconnect_url", begin
        url::UTF8String
    end)


text:
@testset "Grid Sampling" begin
    for T in (Float32, Float64)
        x = ones(T, (2, 2, 1, 1))
        grid = Array{T}(undef, 2, 2, 2, 1)
        grid[:, 1, 1, 1] .= (-1, -1)
        grid[:, 2, 1, 1] .= (1, -1)
        grid[:, 1, 2, 1] .= (-1, 1)
        grid[:, 2, 2, 1] .= (1, 1)

        ∇grid_true = Array{T}(undef, size(grid))
        ∇grid_true[:, :, 1, 1] = [[0.0, 0.0] [-0.5, 0.0]]
        ∇grid_true[:, :, 2, 1] = [[0.0, -0.5] [-0.5, -0.5]]

        x_gpu, grid_gpu = CuArray(x), CuArray(grid)

        padding_mode = :zeros
        y_gpu = grid_sample(x_gpu, grid_gpu; padding_mode=padding_mode)
        @test x == collect(y_gpu)
        @test eltype(y_gpu) == T

        external_grad = CUDA.ones(T, size(y_gpu))
        ∇input, ∇grid = ∇grid_sample(external_grad, x_gpu, grid_gpu; padding_mode=padding_mode)
        @test x == collect(∇input)
        @test ∇grid_true == collect(∇grid)
        @test eltype(∇input) == T
        @test eltype(∇grid) == T

        padding_mode = :border
        fill!(∇grid_true, 0.0)
        sampled = grid_sample(x_gpu, grid_gpu; padding_mode=padding_mode)
        @test x == collect(sampled)
        @test eltype(sampled) == T

        ∇input, ∇grid = ∇grid_sample(external_grad, x_gpu, grid_gpu; padding_mode=padding_mode)
        @test x == collect(∇input)
        @test ∇grid_true == collect(∇grid)
        @test eltype(∇input) == T
        @test eltype(∇grid) == T
    end
end

@testset "Compare grid sampling with NNlib" begin
    w, h, c, n = 16, 16, 2, 4
    input = rand(Float64, w, h, c, n)
    grid = zeros(Float64, 2, w, h, n)
    @inbounds for xi in 1:w, yi in 1:h, ni in 1:n
        grid[1, xi, yi, ni] = (xi / w) * 2.0 - 1.0 + 0.01
        grid[2, xi, yi, ni] = (yi / h) * 2.0 - 1.0
    end
    for padding_mode in (:zeros, :border)
        gputest(grid_sample, input, grid; atol=1e-6, padding_mode=padding_mode)
    end
end


text:
module DynaSEM

export beam01, chirp_lin, chirp_log, modalmethod, state1

include("beam01.jl")
include("chirp.jl")
include("modalmethod.jl")
include("state1.jl")

end # module


text:
# Export datatypes
export ELFDynamicLinks, ELFDynamicLink, ELFRPath

# Export methods
export DynamicLinks, DynamicLink, StrTab, RPath

import Base: getindex, show

"""
    ELFDynamicLinks

ELF dynamic linkage table, contains the list dynamic loader commands, see the
`ELFDynEntry` type for an opaque and unhelpful detailing of these commands.
"""
struct ELFDynamicLinks{H <: ELFHandle} <: DynamicLinks{H}
    section_ref::SectionRef{H}
    links::Vector
end

"""
    DynamicLinks(oh::ELFHandle)

Return the `ELFDynamicLinks` object that is contained within the first
`.dynamic` section within the ELF object.
"""
function DynamicLinks(oh::ELFHandle)
    sections = Sections(oh)
    dyn_section = findfirst(sections, ".dynamic")

    # Create half-initialized `DynamicLinks` object, so we have it around to
    # pass to the `DynamicLink()` constructor below
    dls = ELFDynamicLinks(dyn_section, DynamicLink[])

    # Find all our DT_NEEDED DynEntries
    des = ELFDynEntries(oh, [DT_NEEDED])

    # Now push those onto our half-initialized DynamicLinks object, linking
    # them back to it
    for idx in 1:length(des)
        push!(dls.links, DynamicLink(dls, des[idx]))
    end

    # Return our DynamicLinks object
    return dls
end
Section(dls::ELFDynamicLinks) = dls.section_ref
handle(dls::ELFDynamicLinks) = handle(Section(dls))
lastindex(dls::ELFDynamicLinks) = lastindex(dls.links)
getindex(dls::ELFDynamicLinks, idx) = getindex(dls.links, idx)

"""
    StrTab(dls::ELFDynamicLinks)

Given an ELF `DynamicLinks` container, construct an `ELFStrTab` that refers to
the associated dynamic string table.
"""
function StrTab(dls::ELFDynamicLinks{H}) where {H <: ELFHandle}
    # The `sh_link` field in a ".dynamic" section always points to the
    # dynamic string table (usually called ".dynstr")
    section_idx = deref(Section(dls)).sh_link+1
    strtab_section = Sections(handle(dls))[section_idx]
    return ELFStrTab{H}(strtab_section)
end

struct ELFDynamicLink{H <: ELFHandle} <: DynamicLink{H}
    dls::ELFDynamicLinks{H}
    dyn_entry::ELFDynEntryRef{H}
end

function DynamicLink(dls::ELFDynamicLinks{H},
                     dyn_entry::ELFDynEntryRef{H}) where {H <: ELFHandle}
    return ELFDynamicLink(dls, dyn_entry)
end

DynamicLinks(dl::ELFDynamicLink) = dl.dls
handle(dl::ELFDynamicLink) = handle(DynamicLinks(dl))

function path(dl::ELFDynamicLink)
    strtab = StrTab(DynamicLinks(dl))
    return strtab_lookup(dl.dyn_entry)
end

"""
    ELFRPath

Stores the RPath entries from an ELF object
"""
struct ELFRPath{H <: ELFHandle} <: RPath{H}
    section_ref::SectionRef{H}
    rpath::Vector
end

"""
    RPath(oh::ELFHandle)

Return the `ELFRPath` object that is contained within the first `.dynamic`
section within the ELF object.
"""
function RPath(oh::ELFHandle)
    sections = Sections(oh)
    dyn_section = findfirst(sections, ".dynamic")

    # Get all dyn entries that are DT_RUNPATH or DT_RPATH
    des = ELFDynEntries(oh, [DT_RPATH, DT_RUNPATH])

    # Lookup each and every one of those strings, split them out into paths
    colon_paths = [strtab_lookup(d) for d in des]
    paths = vcat([split(p, ":") for p in colon_paths]...)

    # Return our RPath object
    return ELFRPath(dyn_section, paths)
end

Section(rpath::ELFRPath) = rpath.section_ref
handle(rpath::ELFRPath) = handle(Section(rpath))
rpaths(rpath::ELFRPath) = rpath.rpath


text:
module JuliaJSBridge

using WebIO, Observables, Blink

export JuliaJS

function HTMLFromObj(obj)
    io = IOBuffer()
	Base.show(io, MIME"text/html"(), obj)
    res = String(take!(io))

    return res
end

mutable struct JuliaJS
    _win::Blink.Window
    _scope::WebIO.Scope
    _obs::Observable
    _update_func::Union{Nothing, Function}
    _update_func_tag::Any       # an additional parameter to be sent to the function
end

win(jjs::JuliaJS) = jjs._win
scope(jjs::JuliaJS) = jjs._scope
obs(jjs::JuliaJS) = jjs._obs
update_func(jjs::JuliaJS) = jjs._update_func
update_func_tag(jjs::JuliaJS) = jjs._update_func_tag

function JuliaJS(win::Blink.Window; update_func::Union{Nothing, Function}=nothing, update_func_tag::Any=nothing)
    scope = Scope()

    update_obs = Observable(scope, "OnUpdateJulia", Dict{Any,Any}( "ran"=>"gal"))

    res = JuliaJS(win, scope, update_obs, update_func, update_func_tag)

    on(update_obs) do val
        onJuliaJSUpdate(res, val)
    end

    return res
end

function onJuliaJSUpdate(jjs::JuliaJS, args)
    # @show p1
    # @show args

    if (update_func(jjs) !== nothing)
        update_func(jjs)(args, update_func_tag(jjs))
    end

    # if (p2["meta_data"]["OP"] == "HTMLRequest")
    #     guid = p2["meta_data"]["GUID"]

    #     @show p2["meta_data"]["From"]

    #     html = "?"

    #     # if (p2["meta_data"]["From"] == 1)
    #     #     button_collapse_all1 = button("Button 1")
    #     #     on(button_collapse_all1) do val
    #     #         @info "Button 1"
    #     #     end
    #     #     html = HTMLFromObj(button_collapse_all1);
    #     # elseif (p2["meta_data"]["From"] == 2)
    #     #     button_collapse_all2 = button("Button 2")
    #     #     on(button_collapse_all2) do val
    #     #         @info "Button 2"
    #     #     end
    #     #     html = HTMLFromObj(button_collapse_all2);
    #     # elseif (p2["meta_data"]["From"] == 3)
    #     #     text_filter2 = textbox("Filter:")
    #     #     on(text_filter2) do val
    #     #         @info "text_filter" val
    #     #     end
    #     #     html = HTMLFromObj(text_filter2);
    #     # else
    #     #     temp = Node(:span,
    #     #         Node(:p, "This is sentence 1"),
    #     #         Node(:p, "This is sentence 22222"),
    #     #         Node(:p, "Source: $(p2["meta_data"]["From"])"),
    #     #     )

    #     #     html = HTMLFromObj(temp);
    #     # end

    #     # @info "Running The Code"
    #     # @info jjs
    #     # @js_ win(jjs) begin
    #     #     console.log("1234")
    #     #     JuliaJS.SetHTMLFromJulia($(guid), $(html))
    #     #     console.log("4321")
    #     # end
    #     # @info "After Running The Code"

    # end
end

function readLocalJSFile()
    fn = splitext(@__FILE__)[1] * ".js"
    data = read(fn)
    data = String(UInt8.(data))   # conver to a string from vector of bytes
    return data
end

function node(jj::JuliaJS)

    # @info jj._obs
    # @info typeof(jj._obs)
    # @info "OBS ID = [$(Observables.obsid(jj._obs))]"
    # @info "SCOPE ID = [$(WebIO.scopeid(jj._scope))]"
    scope_id = WebIO.scopeid(jj._scope);
    obs_id = Observables.obsid(jj._obs)

    js = readLocalJSFile()
    js = js * """
    /* console.log("Hi from JuliaJSCommunicator"); */

    JuliaJS["ToJulia"] = function (meta_data, data) {
        WebIO.setval(
            {
                "name":"OnUpdateJulia",
                "scope": "$(scope_id)",
                "id": "$(obs_id)",
                "type":"observable"
            },
            {
                "meta_data": meta_data,
                "data": data,
            }
        );
    }

    """

    res = Node(:dom,
        jj._scope,
        Node(:script, js),
    )

    return res;
end

"""
    JS_loader_node(filenames...)

Return a `WebIO.Node` object that contains all the supplied files as children nodes. Recognized extentions files such
as '.js' and '.css' are contains in a :script and :style nodes respectivly.
"""
function JS_loader_node(filenames...)
    nodes = []
    for filename in filenames
        ext = lowercase(splitext(filename)[2])
        data = read(filename)
        data = String(UInt8.(data))   # conver to a string from vector of bytes
        if (ext == ".js")
            push!(nodes, Node(:script, data))
        elseif (ext == ".css")
            push!(nodes, Node(:style, data))
        else
            push!(nodes, Node(:dom, data))
        end
    end

    return Node(:dom, nodes...)
end

"""
Fixing the path in the styles file for the MaterialIcons font for Blink to load locally from the dist directory
"""
function fixMaterialIConsStyles(styles_fn::String)
    f = open(styles_fn, "r")
    data = read(f, String)
    close(f)

    prefix = replace(abspath(dirname(styles_fn)), '\\' => '/')
    fixed_data = replace(data, "url(MaterialIcons-Regular" => "url(file:$(prefix)/MaterialIcons-Regular")

    # debug
    # open("C:/T/Styles.txt", "w") do f
    #     write(f, fixed_s)
    # end

    return Node(:style, fixed_data)
end

function application_node(jjs::JuliaJS, app_dir::String)

    dist_dir = joinpath(app_dir, "dist", basename(app_dir))

    # if ther is no dist folder, assume the files are in the given folder
    if (!isdir(dist_dir))
        dist_dir = app_dir
    end

    ls = readdir(dist_dir, join=true)

    # special treatment of the Material Design Icons
    # additional_css = nothing
    # if (length([fn for fn in ls if startswith(basename(fn), "MaterialIcons-Regular")]) > 0)

    #     all_files = [fn for fn in ls if startswith(basename(fn), "MaterialIcons-Regular")]

    #     eot_fn = replace([fn for fn in all_files if endswith(basename(fn), ".eot")][1], '\\'=>'/')
    #     ttf_fn = replace([fn for fn in all_files if endswith(basename(fn), ".ttf")][1], '\\'=>'/')
    #     woff_fn = replace([fn for fn in all_files if endswith(basename(fn), ".woff")][1], '\\'=>'/')
    #     woff2_fn = replace([fn for fn in all_files if endswith(basename(fn), ".woff2")][1], '\\'=>'/')
    #     # @info eot_fn
    #     # @info ttf_fn
    #     # @info woff_fn
    #     # @info woff2_fn

    #     # html, body { height: 100%; }
    #     # body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }
    #     additional_css = """

    #     @font-face {
    #         font-family: 'Material Icons';
    #         font-style: normal;
    #         font-weight: 400;
    #         src: url($eot_fn); /* For IE6-8 */
    #         src: local('Material Icons'),
    #              local('MaterialIcons-Regular'),
    #              url($woff2_fn) format('woff2'),
    #              url($woff_fn) format('woff'),
    #              url($ttf_fn) format('truetype');
    #       }
    #     """
    # end

    node = Node(:dom,
        JuliaJSBridge.node(jjs),
        JS_loader_node(
            joinpath(dirname(@__FILE__), "localjs.js"),
            joinpath(dirname(@__FILE__), "localjs.css"),
        ),
        fixMaterialIConsStyles([fn for fn in ls if startswith(basename(fn), "styles")][1]),
        JS_loader_node(
            # [fn for fn in ls if startswith(basename(fn), "styles")][1],
            [fn for fn in ls if startswith(basename(fn), "main")][1],
            [fn for fn in ls if startswith(basename(fn), "polyfills")][1],
            [fn for fn in ls if startswith(basename(fn), "runtime")][1],
        ),
        # (additional_css!==nothing) ? Node(:style, additional_css) : Node(:p, ""),
    )

    return node
end

end # module JuliaJSBridge()