Revision 5006db1399ed521f00f56708181879627ad39d5b authored by Alexander Plavin on 30 September 2023, 15:17:14 UTC, committed by GitHub on 30 September 2023, 15:17:14 UTC
No behavior change, just a performance improvement: searchsorted(range, lt=<) now performs exactly as fast as searchsorted(range). Passing lt=< allows searchsorted to find floating point values such as -0.0, so it's useful to make it fast - see https://github.com/JuliaLang/julia/issues/44102#issuecomment-1418152295.
1 parent d988f8f
scopedvalues.jl
# This file is a part of Julia. License is MIT: https://julialang.org/license
import Base: ScopedValues
@testset "errors" begin
@test ScopedValue{Float64}(1)[] == 1.0
@test_throws InexactError ScopedValue{Int}(1.5)
val = ScopedValue(1)
@test_throws MethodError val[] = 2
with() do
@test_throws MethodError val[] = 2
end
val = ScopedValue{Int}()
@test_throws KeyError val[]
@test_throws MethodError ScopedValue()
end
const sval = ScopedValue(1)
@testset "inheritance" begin
@test sval[] == 1
with() do
@test sval[] == 1
with() do
@test sval[] == 1
end
with(sval => 2) do
@test sval[] == 2
end
@test sval[] == 1
end
@test sval[] == 1
end
const sval_float = ScopedValue(1.0)
@testset "multiple scoped values" begin
with(sval => 2, sval_float => 2.0) do
@test sval[] == 2
@test sval_float[] == 2.0
end
with(sval => 2, sval => 3) do
@test sval[] == 3
end
end
emptyf() = nothing
@testset "conversion" begin
with(emptyf, sval_float=>2)
@test_throws MethodError with(emptyf, sval_float=>"hello")
end
import Base.Threads: @spawn
@testset "tasks" begin
@test fetch(@spawn begin
sval[]
end) == 1
with(sval => 2) do
@test fetch(@spawn begin
sval[]
end) == 2
end
end
@testset "show" begin
@test sprint(show, ScopedValue{Int}()) == "ScopedValue{$Int}(undefined)"
@test sprint(show, sval) == "ScopedValue{$Int}(1)"
@test sprint(show, ScopedValues.current_scope()) == "nothing"
with(sval => 2.0) do
@test sprint(show, sval) == "ScopedValue{$Int}(2)"
objid = sprint(show, Base.objectid(sval))
@test sprint(show, ScopedValues.current_scope()) == "Base.ScopedValues.Scope(ScopedValue{$Int}@$objid => 2)"
end
end
const depth = ScopedValue(0)
function nth_with(f, n)
if n <= 0
f()
else
with(depth => n) do
nth_with(f, n-1)
end
end
end
@testset "nested with" begin
@testset for depth in 1:16
nth_with(depth) do
@test sval_float[] == 1.0
end
with(sval_float=>2.0) do
nth_with(depth) do
@test sval_float[] == 2.0
end
end
nth_with(depth) do
with(sval_float=>2.0) do
@test sval_float[] == 2.0
end
end
end
with(sval_float=>2.0) do
nth_with(15) do
@test sval_float[] == 2.0
with(sval_float => 3.0) do
@test sval_float[] == 3.0
end
end
end
end
@testset "macro" begin
@with sval=>2 sval_float=>2.0 begin
@test sval[] == 2
@test sval_float[] == 2.0
end
# Doesn't do much...
@with begin
@test sval[] == 1
@test sval_float[] == 1.0
end
end
![swh spinner](/static/img/swh-spinner.gif)
Computing file changes ...