Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some tests from Base #4

Merged
merged 5 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/WeakKeyIdDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,27 @@ function Base.setindex!(wkh::WeakKeyIdDict{K}, v, key) where {K}
end
return wkh
end
function Base.get!(wkh::WeakKeyIdDict{K}, key, default) where {K}
function Base.get!(wkh::WeakKeyIdDict{K, V}, key, default) where {K, V}
ericphanson marked this conversation as resolved.
Show resolved Hide resolved
v = lock(wkh) do
k = WeakRefForWeakDict(key)
if key !== nothing && haskey(wkh.ht, k)
wkh.ht[k]
else
wkh[key] = default
wkh[key] = convert(V, default)
end
end
return v
return v::V
end
function Base.get!(default::Base.Callable, wkh::WeakKeyIdDict{K}, key) where {K}
function Base.get!(default::Base.Callable, wkh::WeakKeyIdDict{K, V}, key) where {K, V}
ericphanson marked this conversation as resolved.
Show resolved Hide resolved
v = lock(wkh) do
k = WeakRefForWeakDict(key)
if key !== nothing && haskey(wkh.ht, k)
wkh.ht[k]
else
wkh[key] = default()
wkh[key] = convert(V, default())
end
end
return v
return v::V
end

function Base.getkey(wkh::WeakKeyIdDict{K}, kk, default) where {K}
Expand Down
62 changes: 62 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,68 @@ include("set_up_tests.jl")
Aqua.test_all(WeakKeyIdDicts; ambiguities=false)
end

# NOTE: the first two testsets are copied from
# https://github.com/JuliaLang/julia/blob/d7dc9a8cc8f2aebf04d5cecc8625be250169644b/test/dict.jl#L565-L626
# an modified for WeakKeyIdDict. WeakKeyIdDict doesn't support integeres as keys so some
# of the tests no longer make sense, and/or have to be modified

# https://github.com/JuliaLang/julia/pull/10657
mutable struct T10647{T}
x::T
end
@testset "issue julia#10647" begin
a = WeakKeyIdDict()
a["1"] = a
a[a] = 2
a["3"] = T10647(a)
@test isequal(a, a)
show(IOBuffer(), a)
Base.show(Base.IOContext(IOBuffer(), :limit => true), a)
Base.show(IOBuffer(), a)
Base.show(Base.IOContext(IOBuffer(), :limit => true), a)
end

@testset "WeakKeyIdDict{Any,Any} and partial inference" begin
a = WeakKeyIdDict{Any,Any}()
a["1"] = a
a[a] = 2

sa = empty(a)
@test isempty(sa)
@test isa(sa, WeakKeyIdDict{Any,Any})

@test length(a) == 2
@test "1" in keys(a)
@test a in keys(a)
@test a["1"] === a
@test a[a] === 2

ca = copy(a)
@test length(ca) == length(a)
@test isequal(ca, a)
@test ca !== a # make sure they are different objects

ca = empty!(ca)
@test length(ca) == 0
@test length(a) == 2

d = Dict('a' => 1, 'b' => 1, 'c' => 3)
@test a != d
@test !isequal(a, d)

d = @inferred WeakKeyIdDict{Any,Any}(Pair("1", 1), Pair("2", 2), Pair("3", 3))
@test isa(d, WeakKeyIdDict{Any,Any})
@test d == WeakKeyIdDict{Any,Any}("1" => 1, "2" => 2, "3" => 3)
@test eltype(d) == Pair{Any,Any}

d = WeakKeyIdDict{Any,Int32}(:hi => 7)
let c = Ref{Any}(1.5)
f() = c[]
@test @inferred(get!(f, d, :hi)) === Int32(7)
@test_throws InexactError(:Int32, Int32, 1.5) get!(f, d, :hello)
end
end

@testset "WeakKeyIdDict" begin
A = [1]
B = [2]
Expand Down
Loading