From 86dd7f116a1587bc3634c5289a6b6c5fcf2d15cb Mon Sep 17 00:00:00 2001 From: Max Horn Date: Mon, 11 Sep 2023 12:39:19 +0200 Subject: [PATCH] More WeakKeyIdDict tests --- test/WeakKeyIdDict-test.jl | 76 +++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/test/WeakKeyIdDict-test.jl b/test/WeakKeyIdDict-test.jl index a243db78a4..45d50e70b7 100644 --- a/test/WeakKeyIdDict-test.jl +++ b/test/WeakKeyIdDict-test.jl @@ -13,21 +13,41 @@ @test WeakKeyIdDict(dd) == wkd @test convert(WeakKeyIdDict{Any, Any}, dd) == wkd @test isa(WeakKeyIdDict(dd), WeakKeyIdDict{Any,Any}) + + # test many constructors without type parameters specified @test WeakKeyIdDict(A=>2, B=>3, C=>4) == wkd - @test isa(WeakKeyIdDict(A=>2, B=>3, C=>4), WeakKeyIdDict{Array{Int,1},Int}) + @test isa(WeakKeyIdDict(A=>2, B=>3, C=>4), WeakKeyIdDict{Vector{Int},Int}) @test WeakKeyIdDict(a=>i+1 for (i,a) in enumerate([A,B,C]) ) == wkd @test WeakKeyIdDict([(A,2), (B,3), (C,4)]) == wkd @test WeakKeyIdDict(Pair(A,2), Pair(B,3), Pair(C,4)) == wkd + + # test many constructors with type parameters specified + @test WeakKeyIdDict{Vector{Int},Int}(A=>2, B=>3, C=>4) == wkd + @test isa(WeakKeyIdDict{Vector{Int},Int}(A=>2, B=>3, C=>4), WeakKeyIdDict{Vector{Int},Int}) + @test WeakKeyIdDict{Vector{Int},Int}(a=>i+1 for (i,a) in enumerate([A,B,C]) ) == wkd + @test WeakKeyIdDict{Vector{Int},Int}([(A,2), (B,3), (C,4)]) == wkd + @test WeakKeyIdDict{Vector{Int},Int}(Pair(A,2), Pair(B,3), Pair(C,4)) == wkd + + # test more constructors with mixed types + @test isa(WeakKeyIdDict(A=>2, B=>3, C=>"4"), WeakKeyIdDict{Vector{Int},Any}) + @test isa(WeakKeyIdDict(A=>2, B=>3, "C"=>4), WeakKeyIdDict{Any,Int}) + @test isa(WeakKeyIdDict(A=>2, B=>3, "C"=>"4"), WeakKeyIdDict{Any,Any}) + @test copy(wkd) == wkd @test length(wkd) == 3 @test !isempty(wkd) + @test haskey(wkd, C) + @test getkey(wkd, C, 123) === C res = pop!(wkd, C) @test res == 4 @test C ∉ keys(wkd) @test 4 ∉ values(wkd) + @test !haskey(wkd, C) @test length(wkd) == 2 @test !isempty(wkd) + @test 47 == pop!(wkd, C, 47) + @test getkey(wkd, C, 123) == 123 wkd = filter!( p -> p.first != B, wkd) @test B ∉ keys(wkd) @test 3 ∉ values(wkd) @@ -35,18 +55,48 @@ @test WeakKeyIdDict(Pair(A, 2)) == wkd @test !isempty(wkd) + wkd[A] = 42 + @test wkd[A] == 42 + + wkd = WeakKeyIdDict(A=>2, B=>3, C=>4) + map!(x -> x + 1, values(wkd)) + @test WeakKeyIdDict(A=>3, B=>4, C=>5) == wkd + + wkd = WeakKeyIdDict(A=>2) + @test get(wkd, A, 17) == 2 + @test get!(wkd, A, 17) == 2 + @test get(wkd, B, 17) == 17 + @test length(wkd) == 1 + @test get!(wkd, B, 17) == 17 + @test length(wkd) == 2 + + wkd = WeakKeyIdDict(A=>2) + @test get(() -> 23, wkd, A) == 2 + @test get!(() -> 23, wkd, A) == 2 + @test get(() -> 23, wkd, B) == 23 + @test length(wkd) == 1 + @test get!(() -> 23, wkd, B) == 23 + @test length(wkd) == 2 + wkd = empty!(wkd) @test wkd == empty(wkd) + @test wkd == empty(wkd) @test typeof(wkd) == typeof(empty(wkd)) @test length(wkd) == 0 @test isempty(wkd) @test isa(wkd, WeakKeyIdDict) + @test WeakKeyIdDict() == WeakKeyIdDict(()) @test_throws ArgumentError WeakKeyIdDict([1, 2, 3]) # WeakKeyIdDict does not convert keys @test_throws ArgumentError WeakKeyIdDict{Int,Any}(5.0=>1) + # iterator + wkd = WeakKeyIdDict(A=>2, B=>3, C=>4) + @test Set(collect(wkd)) == Set([A=>2, B=>3, C=>4]) + @test 2+3+4 == sum(v for (k,v) in wkd) + # WeakKeyIdDict hashes with object-id AA = copy(A) GC.@preserve A AA begin @@ -66,4 +116,28 @@ d26939 = WeakKeyIdDict() d26939[big"1.0" + 1.1] = 1 GC.gc() # make sure this doesn't segfault + +end + +@testset "WeakKeyIdDict.gc" begin + # verify that garbage collection takes care of our weak references + A = [1] + wkd = WeakKeyIdDict(A => 1) + let tmp = [ 42 ] + @test length(wkd) == 1 + wkd[tmp] = 2 + @test length(wkd) == 2 + end + # at this point there is no strong reference left to the vector [42] + # previously reachable via tmp + GC.gc(true) + + # FIXME: somehow the following test does not work if it + # is inside a @testset; it works if I copy&paste the content + # into the REPL. Huh? + #= + @test length(wkd) == 1 + @test length(keys(wkd)) == 1 + @test WeakKeyIdDict(A => 1) == wkd + =# end