Skip to content

Commit

Permalink
Added and fixed some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Ferris committed Oct 29, 2017
1 parent 69bf980 commit 996304c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
4 changes: 2 additions & 2 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,8 @@ of the columns of `A`.
would create an array of `Int`, initialized to zero, matching the
indices of `A`.
"""
similar(f, shape::Tuple) = f(to_shape(shape))
similar(f, dims::DimOrInd...) = similar(f, dims)
similar(f, shape::Tuple) = f(to_shape(shape)) # doesn't share well with Associative
similar(f, dims::DimOrInd...) = similar(f, dims) # doesn't share well with Associative

## from general iterable to any array

Expand Down
40 changes: 35 additions & 5 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ end
a[1] = a
a[a] = 2

sa = similar(a)
sa = empty(a)
@test isempty(sa)
@test isa(sa, ObjectIdDict)

Expand Down Expand Up @@ -430,6 +430,36 @@ end
[v for (k, v) in d] == [d[x[1]] for (i, x) in enumerate(d)]
end

@testset "similar" begin
d = Dict(:a=>2, :b=>3)

d2 = similar(d)
@test d2 isa typeof(d)
@test length(keys(d2)) == 2
@test :a keys(d2)
@test :b keys(d2)

d3 = similar(d, Float64)
@test d3 isa Dict{Symbol, Float64}
@test length(keys(d3)) == 2
@test :a keys(d3)
@test :b keys(d3)

d4 = similar(d, [10, 20, 30])
@test d4 isa Dict{Int, Int}
@test length(keys(d4)) == 3
@test 10 keys(d4)
@test 20 keys(d4)
@test 30 keys(d4)

d5 = similar(d, Float64, [10, 20, 30])
@test d5 isa Dict{Int, Float64}
@test length(keys(d5)) == 3
@test 10 keys(d5)
@test 20 keys(d5)
@test 30 keys(d5)
end

@testset "generators, similar" begin
d = Dict(:a=>"a")
# TODO: restore when 0.7 deprecation is removed
Expand Down Expand Up @@ -507,8 +537,8 @@ import Base.ImmutableDict
@test get(d, k1, :default) === :default
@test d1["key1"] === v1
@test d4["key1"] === v2
@test similar(d3) === d
@test similar(d) === d
@test empty(d3) === d
@test empty(d) === d

@test_throws KeyError d[k1]
@test_throws KeyError d1["key2"]
Expand Down Expand Up @@ -649,8 +679,8 @@ Dict(1 => rand(2,3), 'c' => "asdf") # just make sure this does not trigger a dep
@test !isempty(wkd)

wkd = empty!(wkd)
@test wkd == similar(wkd)
@test typeof(wkd) == typeof(similar(wkd))
@test wkd == empty(wkd)
@test typeof(wkd) == typeof(empty(wkd))
@test length(wkd) == 0
@test isempty(wkd)
@test isa(wkd, WeakKeyDict)
Expand Down

0 comments on commit 996304c

Please sign in to comment.