From 862ad07195014fd60ab40731b967f42b77ead392 Mon Sep 17 00:00:00 2001 From: cossio Date: Sun, 15 Dec 2024 18:48:37 +0100 Subject: [PATCH 1/5] dict docs --- base/abstractdict.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/base/abstractdict.jl b/base/abstractdict.jl index 85a726b4cdbf4..3be930151d4d4 100644 --- a/base/abstractdict.jl +++ b/base/abstractdict.jl @@ -88,8 +88,8 @@ Return an iterator over all keys in a dictionary. When the keys are stored internally in a hash table, as is the case for `Dict`, the order in which they are returned may vary. -But `keys(a)` and `values(a)` both iterate `a` and -return the elements in the same order. +But `keys(a)`, `values(a)` and `pairs(a)` all iterate `a` +and return the elements in the same order. # Examples ```jldoctest @@ -114,8 +114,8 @@ Return an iterator over all values in a collection. When the values are stored internally in a hash table, as is the case for `Dict`, the order in which they are returned may vary. -But `keys(a)` and `values(a)` both iterate `a` and -return the elements in the same order. +But `keys(a)`, `values(a)` and `pairs(a)` all iterate `a` +and return the elements in the same order. # Examples ```jldoctest @@ -138,6 +138,10 @@ values(a::AbstractDict) = ValueIterator(a) Return an iterator over `key => value` pairs for any collection that maps a set of keys to a set of values. This includes arrays, where the keys are the array indices. +When the entries are stored internally in a hash table, +as is the case for `Dict`, the order in which they are returned may vary. +But `keys(a)`, `values(a)` and `pairs(a)` all iterate `a` +and return the elements in the same order. # Examples ```jldoctest From 8c6dc4af6755f30da12abe416b003849d979b11a Mon Sep 17 00:00:00 2001 From: cossio Date: Mon, 16 Dec 2024 10:22:08 +0100 Subject: [PATCH 2/5] add tests --- test/dict.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/dict.jl b/test/dict.jl index 909afb3607907..a5fe7fa80b81f 100644 --- a/test/dict.jl +++ b/test/dict.jl @@ -787,6 +787,20 @@ end [v for (k, v) in d] == [d[x[1]] for (i, x) in enumerate(d)] end +@testset "consistency of iteration order" begin + d = Dict(randn() => randn() for _ = 1:100) + @test [k for (k,v) = d] == + [k for k = keys(d)] == + [k for (k,v) = pairs(d)] == + collect(keys(d)) + @test [v for (k,v) = d] == + [v for v = values(d)] == + [v for (k,v) = pairs(d)] == + [d[k] for k = keys(d)] == + collect(values(d)) + @test [k => v for (k,v) = d] == collect(d) == collect(pairs(d)) +end + @testset "generators, similar" begin d = Dict(:a=>"a") # TODO: restore when 0.7 deprecation is removed From 22b9d123b778408134c8a39be883a694727cf87d Mon Sep 17 00:00:00 2001 From: cossio Date: Mon, 16 Dec 2024 10:24:20 +0100 Subject: [PATCH 3/5] test name --- test/dict.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dict.jl b/test/dict.jl index a5fe7fa80b81f..cbebcedad8a18 100644 --- a/test/dict.jl +++ b/test/dict.jl @@ -787,7 +787,7 @@ end [v for (k, v) in d] == [d[x[1]] for (i, x) in enumerate(d)] end -@testset "consistency of iteration order" begin +@testset "consistency of dict iteration order (issue #56841)" begin d = Dict(randn() => randn() for _ = 1:100) @test [k for (k,v) = d] == [k for k = keys(d)] == From e2bfff5060b82f553a028df009a2ac552bcb01c6 Mon Sep 17 00:00:00 2001 From: cossio Date: Tue, 17 Dec 2024 00:03:09 +0100 Subject: [PATCH 4/5] improve tests --- test/dict.jl | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/test/dict.jl b/test/dict.jl index cbebcedad8a18..730d2c44de570 100644 --- a/test/dict.jl +++ b/test/dict.jl @@ -788,17 +788,12 @@ end end @testset "consistency of dict iteration order (issue #56841)" begin - d = Dict(randn() => randn() for _ = 1:100) - @test [k for (k,v) = d] == - [k for k = keys(d)] == - [k for (k,v) = pairs(d)] == - collect(keys(d)) - @test [v for (k,v) = d] == - [v for v = values(d)] == - [v for (k,v) = pairs(d)] == - [d[k] for k = keys(d)] == - collect(values(d)) - @test [k => v for (k,v) = d] == collect(d) == collect(pairs(d)) + dict = Dict(randn() => randn() for _ = 1:100) + foreach(zip(dict, keys(dict), values(dict), pairs(dict))) do (d, k, v, p) + @test d == p + @test first(d) == first(p) == k + @test last(d) == last(p) == v + end end @testset "generators, similar" begin From 0092eb0a0419a55890e14290648fb7caf4dfca0f Mon Sep 17 00:00:00 2001 From: cossio Date: Tue, 17 Dec 2024 14:53:06 +0100 Subject: [PATCH 5/5] test --- test/dict.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/dict.jl b/test/dict.jl index 730d2c44de570..83d35ae18bb85 100644 --- a/test/dict.jl +++ b/test/dict.jl @@ -789,10 +789,8 @@ end @testset "consistency of dict iteration order (issue #56841)" begin dict = Dict(randn() => randn() for _ = 1:100) - foreach(zip(dict, keys(dict), values(dict), pairs(dict))) do (d, k, v, p) - @test d == p - @test first(d) == first(p) == k - @test last(d) == last(p) == v + @test all(zip(dict, keys(dict), values(dict), pairs(dict))) do (d, k, v, p) + d == p && first(d) == first(p) == k && last(d) == last(p) == v end end