diff --git a/base/rational.jl b/base/rational.jl index 5288492e72e71..1b060384d95b2 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -95,7 +95,7 @@ end function show(io::IO, x::Rational) show(io, numerator(x)) - if isone(denominator(x)) && get(io, :typeinfo, Any) <: Rational + if isone(denominator(x)) && nonnothing_nonmissing_typeinfo(io) <: Rational return end diff --git a/base/ryu/Ryu.jl b/base/ryu/Ryu.jl index 9b236caeb6ff1..89589aa4ab668 100644 --- a/base/ryu/Ryu.jl +++ b/base/ryu/Ryu.jl @@ -112,7 +112,7 @@ end function Base.show(io::IO, x::T, forceuntyped::Bool=false, fromprint::Bool=false) where {T <: Base.IEEEFloat} compact = get(io, :compact, false)::Bool buf = Base.StringVector(neededdigits(T)) - typed = !forceuntyped && !compact && get(io, :typeinfo, Any) != typeof(x) + typed = !forceuntyped && !compact && Base.nonnothing_nonmissing_typeinfo(io) != typeof(x) pos = writeshortest(buf, 1, x, false, false, true, -1, (x isa Float32 && !fromprint) ? UInt8('f') : UInt8('e'), false, UInt8('.'), typed, compact) write(io, resize!(buf, pos - 1)) diff --git a/base/show.jl b/base/show.jl index eb9f7bcece49d..db0b429f3386f 100644 --- a/base/show.jl +++ b/base/show.jl @@ -1231,8 +1231,9 @@ function show(io::IO, tn::Core.TypeName) print(io, ")") end +nonnothing_nonmissing_typeinfo(io::IO) = nonmissingtype(nonnothingtype(get(io, :typeinfo, Any))) +show(io::IO, b::Bool) = print(io, nonnothing_nonmissing_typeinfo(io) === Bool ? (b ? "1" : "0") : (b ? "true" : "false")) show(io::IO, ::Nothing) = print(io, "nothing") -show(io::IO, b::Bool) = print(io, get(io, :typeinfo, Any) === Bool ? (b ? "1" : "0") : (b ? "true" : "false")) show(io::IO, n::Signed) = (write(io, string(n)); nothing) show(io::IO, n::Unsigned) = print(io, "0x", string(n, pad = sizeof(n)<<1, base = 16)) print(io::IO, n::Unsigned) = print(io, string(n)) diff --git a/test/show.jl b/test/show.jl index 08a29612b245c..b6106c710931c 100644 --- a/test/show.jl +++ b/test/show.jl @@ -2170,6 +2170,20 @@ replstrcolor(x) = sprint((io, x) -> show(IOContext(io, :limit => true, :color => @test_repr "Bool[1, 0]" end +@testset "Unions with Bool (#39590)" begin + @test repr([missing, false]) == "Union{Missing, Bool}[missing, 0]" + @test_repr "Union{Bool, Nothing}[1, 0, nothing]" +end + +# issue #26847 +@test_repr "Union{Missing, Float32}[1.0]" + +# intersection of #45396 and #48822 +@test_repr "Union{Missing, Rational{Int64}}[missing, 1//2, 2]" + +# Don't go too far with #48822 +@test_repr "Union{String, Bool}[true]" + # issue #30505 @test repr(Union{Tuple{Char}, Tuple{Char, Char}}[('a','b')]) == "Union{Tuple{Char}, Tuple{Char, Char}}[('a', 'b')]" diff --git a/test/sorting.jl b/test/sorting.jl index 35635d8588f93..ad821e38efe55 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -1036,40 +1036,30 @@ end @test issorted(sort!(rand(100), Base.Sort.InitialOptimizations(DispatchLoopTestAlg()), Base.Order.Forward)) end -@testset "partialsort tests added for BracketedSort #52006" begin - x = rand(Int, 1000) - @test partialsort(x, 1) == minimum(x) - @test partialsort(x, 1000) == maximum(x) - sx = sort(x) - for i in [1, 2, 4, 10, 11, 425, 500, 845, 991, 997, 999, 1000] - @test partialsort(x, i) == sx[i] - end - for i in [1:1, 1:2, 1:5, 1:8, 1:9, 1:11, 1:108, 135:812, 220:586, 363:368, 450:574, 458:597, 469:638, 487:488, 500:501, 584:594, 1000:1000] - @test partialsort(x, i) == sx[i] +# Pathologize 0 is a noop, pathologize 3 is fully pathological +function pathologize!(x, level) + Base.require_one_based_indexing(x) + k2 = Int(cbrt(length(x))^2) + seed = hash(length(x), Int === Int64 ? 0x85eb830e0216012d : 0xae6c4e15) + for a in 1:level + seed = hash(a, seed) + x[mod.(hash.(1:k2, seed), range.(1:k2,lastindex(x)))] .= a end + x +end - # Semi-pathological input - seed = hash(1000, Int === Int64 ? 0x85eb830e0216012d : 0xae6c4e15) - seed = hash(1, seed) - for i in 1:100 - j = mod(hash(i, seed), i:1000) - x[j] = typemax(Int) +@testset "partialsort tests added for BracketedSort #52006" begin + for x in [pathologize!.(Ref(rand(Int, 1000)), 0:3); pathologize!.(Ref(rand(1000)), 0:3); [pathologize!(rand(Int, 1_000_000), 3)]] + @test partialsort(x, 1) == minimum(x) + @test partialsort(x, lastindex(x)) == maximum(x) + sx = sort(x) + for i in [1, 2, 4, 10, 11, 425, 500, 845, 991, 997, 999, 1000] + @test partialsort(x, i) == sx[i] + end + for i in [1:1, 1:2, 1:5, 1:8, 1:9, 1:11, 1:108, 135:812, 220:586, 363:368, 450:574, 458:597, 469:638, 487:488, 500:501, 584:594, 1000:1000] + @test partialsort(x, i) == sx[i] + end end - @test partialsort(x, 500) == sort(x)[500] - - # Fully pathological input - # it would be too much trouble to actually construct a valid pathological input, so we - # construct an invalid pathological input. - # This test is kind of sketchy because it passes invalid inputs to the function - # Temporarily removed due to flakey test failures. See #52642 for details. - # for i in [1:6, 1:483, 1:957, 77:86, 118:478, 223:227, 231:970, 317:958, 500:501, 500:501, 500:501, 614:620, 632:635, 658:665, 933:940, 937:942, 997:1000, 999:1000] - # x = rand(1:5, 1000) - # @test partialsort(x, i, lt=(<=)) == sort(x)[i] - # end - # for i in [1, 7, 8, 490, 495, 852, 993, 996, 1000] - # x = rand(1:5, 1000) - # @test partialsort(x, i, lt=(<=)) == sort(x)[i] - # end end # This testset is at the end of the file because it is slow.