Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/JuliaLang/julia
Browse files Browse the repository at this point in the history
  • Loading branch information
mitiemann committed Jan 3, 2024
2 parents 4553581 + ca0a266 commit aef4d9b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 34 deletions.
2 changes: 1 addition & 1 deletion base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion base/ryu/Ryu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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')]"

Expand Down
52 changes: 21 additions & 31 deletions test/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit aef4d9b

Please sign in to comment.