From a51199d4a4131ee596f205f18ae3b2ec228336db Mon Sep 17 00:00:00 2001 From: Kevin Squire Date: Wed, 4 Mar 2015 19:45:45 -0800 Subject: [PATCH] Fixes for OrderedDict tests --- src/hashdict.jl | 10 ++++++++++ test/test_ordereddict.jl | 36 ++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/hashdict.jl b/src/hashdict.jl index 4e1e1f0ec..d47130718 100644 --- a/src/hashdict.jl +++ b/src/hashdict.jl @@ -530,3 +530,13 @@ next(v::ValueIterator{HashDict}, i) = (v.dict.vals[i], skip_deleted(v.dict,i+1)) next{K,V}(v::KeyIterator{HashDict{K,V,Ordered}}, i) = (v.dict.keys[v.dict.order[i]], skip_deleted(v.dict,i+1)) next{K,V}(v::ValueIterator{HashDict{K,V,Ordered}}, i) = (v.dict.vals[v.dict.order[i]], skip_deleted(v.dict,i+1)) + +if VERSION >= v"0.4.0-dev+980" + push!(t::HashDict, p::Pair) = setindex!(t, p.second, p.first) + push!(t::HashDict, p::Pair, q::Pair) = push!(push!(t, p), q) + push!(t::HashDict, p::Pair, q::Pair, r::Pair...) = push!(push!(push!(t, p), q), r...) +end + +push!(d::HashDict, p) = setindex!(d, p[2], p[1]) +push!(d::HashDict, p, q) = push!(push!(d, p), q) +push!(d::HashDict, p, q, r...) = push!(push!(push!(d, p), q), r...) diff --git a/test/test_ordereddict.jl b/test/test_ordereddict.jl index 9bcb60f97..7eff5f0f3 100644 --- a/test/test_ordereddict.jl +++ b/test/test_ordereddict.jl @@ -7,9 +7,9 @@ using Base.Test @test typeof(OrderedDict([(1,2.0)])) == OrderedDict{Int,Float64} @test typeof(OrderedDict([("a",1),("b",2)])) == OrderedDict{ASCIIString,Int} if VERSION >= v"0.4.0-dev+980" - @test typeof(OrderedDict(1 => 1.0)) == OrderedDict{Int,Float64} - @test typeof(OrderedDict(1 => 1.0, 2 => 2.0)) == OrderedDict{Int,Float64} - @test typeof(OrderedDict(1 => 1.0, 2 => 2.0, 3 => 3.0)) == OrderedDict{Int,Float64} + @test typeof(OrderedDict(Pair(1, 1.0))) == OrderedDict{Int,Float64} + @test typeof(OrderedDict(Pair(1, 1.0), Pair(2, 2.0))) == OrderedDict{Int,Float64} + @test typeof(OrderedDict(Pair(1, 1.0), Pair(2, 2.0), Pair(3, 3.0))) == OrderedDict{Int,Float64} end # empty dictionary @@ -108,7 +108,7 @@ for i=10000:20000 @test h[i]==i+1 end -h = OrderedDict{Any,Any}("a" => 3) +h = OrderedDict{Any,Any}([("a", 3)]) @test h["a"] == 3 h["a","b"] = 4 @test h["a","b"] == h[("a","b")] == 4 @@ -126,7 +126,7 @@ let @test get_KeyError end -_d = OrderedDict("a"=>0) +_d = OrderedDict([("a", 0)]) @test isa([k for k in filter(x->length(x)==1, collect(keys(_d)))], Vector{Any}) let @@ -174,7 +174,7 @@ let end @test_throws ArgumentError first(OrderedDict()) -@test first(OrderedDict(:f=>2)) == (:f,2) +@test first(OrderedDict([(:f, 2)])) == (:f,2) # issue #1821 let @@ -190,16 +190,16 @@ let bestkey(d, key) = key bestkey{K<:AbstractString,V}(d::Associative{K,V}, key) = string(key) bar(x) = bestkey(x, :y) - @test bar(OrderedDict(:x => [1,2,5])) == :y - @test bar(OrderedDict("x" => [1,2,5])) == "y" + @test bar(OrderedDict([(:x, [1,2,5])])) == :y + @test bar(OrderedDict([("x", [1,2,5])])) == "y" end @test isequal(OrderedDict(), OrderedDict()) -@test isequal(OrderedDict(1 => 1), OrderedDict(1 => 1)) -@test !isequal(OrderedDict(1 => 1), OrderedDict()) -@test !isequal(OrderedDict(1 => 1), OrderedDict(1 => 2)) -@test !isequal(OrderedDict(1 => 1), OrderedDict(2 => 1)) +@test isequal(OrderedDict([(1, 1)]), OrderedDict([(1, 1)])) +@test !isequal(OrderedDict([(1, 1)]), OrderedDict()) +@test !isequal(OrderedDict([(1, 1)]), OrderedDict([(1, 2)])) +@test !isequal(OrderedDict([(1, 1)]), OrderedDict([(2, 1)])) # Generate some data to populate dicts to be compared data_in = [ (rand(1:1000), randstring(2)) for _ in 1:1001 ] @@ -240,13 +240,13 @@ d4[1001] = randstring(3) # Here is what currently happens when dictionaries of different types # are compared. This is not necessarily desirable. These tests are # descriptive rather than proscriptive. -@test !isequal(OrderedDict(1 => 2), OrderedDict("dog" => "bone")) +@test !isequal(OrderedDict([(1, 2)]), OrderedDict([("dog", "bone")])) @test isequal(OrderedDict{Int,Int}(), OrderedDict{AbstractString,AbstractString}()) # get! (get with default values assigned to the given location) ## TODO: get! not implemented for OrderedDict -# let f(x) = x^2, d = OrderedDict(8=>19) +# let f(x) = x^2, d = OrderedDict((8, 19)) # @test get!(d, 8, 5) == 19 # @test get!(d, 19, 2) == 2 @@ -263,7 +263,7 @@ d4[1001] = randstring(3) # f(4) # end == 16 -# @test d == OrderedDict(8=>19, 19=>2, 42=>4) +# @test d == OrderedDict((8, 19), (19, 2), (42, 4)) # end @@ -288,12 +288,12 @@ end # issue 9295 let d = OrderedDict() - @test is(push!(d, 'a' => 1), d) + @test is(push!(d, ('a', 1)), d) @test d['a'] == 1 - @test is(push!(d, 'b' => 2, 'c' => 3), d) + @test is(push!(d, ('b', 2), ('c', 3)), d) @test d['b'] == 2 @test d['c'] == 3 - @test is(push!(d, 'd' => 4, 'e' => 5, 'f' => 6), d) + @test is(push!(d, ('d', 4), ('e', 5), ('f', 6)), d) @test d['d'] == 4 @test d['e'] == 5 @test d['f'] == 6