Skip to content

Commit

Permalink
Fix bug in empty!, add tests for WeakKeyDict
Browse files Browse the repository at this point in the history
  • Loading branch information
kshyatt committed Dec 3, 2016
1 parent 36a582e commit 50f0848
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/weakkeydict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ get!{K}(default::Callable, wkh::WeakKeyDict{K}, key) = lock(() -> get!(default,
pop!{K}(wkh::WeakKeyDict{K}, key) = lock(() -> pop!(wkh.ht, key), wkh)
pop!{K}(wkh::WeakKeyDict{K}, key, default) = lock(() -> pop!(wkh.ht, key, default), wkh)
delete!{K}(wkh::WeakKeyDict{K}, key) = lock(() -> delete!(wkh.ht, key), wkh)
empty!(wkh::WeakKeyDict) = (lock(() -> empty!(wkh.ht)); wkh)
empty!(wkh::WeakKeyDict) = (lock(() -> empty!(wkh.ht), wkh); wkh)
haskey{K}(wkh::WeakKeyDict{K}, key) = lock(() -> haskey(wkh.ht, key), wkh)
getindex{K}(wkh::WeakKeyDict{K}, key) = lock(() -> getindex(wkh.ht, key), wkh)
isempty(wkh::WeakKeyDict) = isempty(wkh.ht)
Expand Down
34 changes: 34 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,37 @@ end

# #18213
Dict(1 => rand(2,3), 'c' => "asdf") # just make sure this does not trigger a deprecation

@testset "WeakKeyDict" begin
A = [1]
B = [2]
C = [3]
local x = 0
local y = 0
local z = 0
finalizer(A, a->(x+=1))
finalizer(B, b->(y+=1))
finalizer(C, c->(z+=1))
wkd = WeakKeyDict()
wkd[A] = 2
wkd[B] = 3
wkd[C] = 4
@test length(wkd) == 3
@test !isempty(wkd)
res = pop!(wkd, C)
@test res == 4
@test C keys(wkd)
@test 4 values(wkd)
@test length(wkd) == 2
@test !isempty(wkd)
wkd = filter!( (k,v) -> k != B, wkd)
@test B keys(wkd)
@test 3 values(wkd)
@test length(wkd) == 1
@test !isempty(wkd)

wkd = empty!(wkd)
@test length(wkd) == 0
@test isempty(wkd)
@test isa(wkd, WeakKeyDict)
end

0 comments on commit 50f0848

Please sign in to comment.