Skip to content

Commit

Permalink
Fixes for OrderedDict tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kmsquire committed Mar 5, 2015
1 parent 4c6e6d0 commit a51199d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
10 changes: 10 additions & 0 deletions src/hashdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
36 changes: 18 additions & 18 deletions test/test_ordereddict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 ]
Expand Down Expand Up @@ -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
Expand All @@ -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


Expand All @@ -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
Expand Down

0 comments on commit a51199d

Please sign in to comment.