Skip to content

Commit

Permalink
Add getindex(::Pair) + tests for Pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Feb 10, 2015
1 parent 19066b2 commit 8b814f7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ isequal(p::Pair, q::Pair) = isequal(p.first,q.first) & isequal(p.second,q.second

isless(p::Pair, q::Pair) = ifelse(!isequal(p.first,q.first), isless(p.first,q.first),
isless(p.second,q.second))
getindex(p::Pair,i::Int) = getfield(p,i)
getindex(p::Pair,i::Real) = getfield(p, convert(Int, i))

# some operators not defined yet
global //, .>>, .<<, >:, <|, |>, hcat, hvcat, , ×, , , , , , , , , , ,
Expand Down
27 changes: 27 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
# Pair
p = Pair(1,2)
@test p == (1=>2)
@test isequal(p,1=>2)
@test start(p) == 1
@test next(p, 1) == (1,2)
@test !done(p, 1)
@test !done(p,2)
@test done(p,3)
@test !done(p,0)
@test Base.indexed_next(p, 1, (1,2)) == (1,2)
@test Base.indexed_next(p, 2, (1,2)) == (2,3)
@test (1=>2) < (2=>3)
@test (2=>2) < (2=>3)
@test !((2=>3) < (2=>3))
@test (2=>3) < (4=>3)
@test (1=>100) < (4=>1)
@test p[1] == 1
@test p[2] == 2
@test_throws BoundsError p[3]
@test_throws BoundsError p[false]
@test p[true] == 1
@test p[2.0] == 2
@test p[0x01] == 1
@test_throws InexactError p[2.3]

# Dict
h = Dict()
for i=1:10000
h[i] = i+1
Expand Down

0 comments on commit 8b814f7

Please sign in to comment.