Skip to content

Commit 996304c

Browse files
author
Andy Ferris
committed
Added and fixed some tests
1 parent 69bf980 commit 996304c

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

base/abstractarray.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ of the columns of `A`.
563563
would create an array of `Int`, initialized to zero, matching the
564564
indices of `A`.
565565
"""
566-
similar(f, shape::Tuple) = f(to_shape(shape))
567-
similar(f, dims::DimOrInd...) = similar(f, dims)
566+
similar(f, shape::Tuple) = f(to_shape(shape)) # doesn't share well with Associative
567+
similar(f, dims::DimOrInd...) = similar(f, dims) # doesn't share well with Associative
568568

569569
## from general iterable to any array
570570

test/dict.jl

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ end
381381
a[1] = a
382382
a[a] = 2
383383

384-
sa = similar(a)
384+
sa = empty(a)
385385
@test isempty(sa)
386386
@test isa(sa, ObjectIdDict)
387387

@@ -430,6 +430,36 @@ end
430430
[v for (k, v) in d] == [d[x[1]] for (i, x) in enumerate(d)]
431431
end
432432

433+
@testset "similar" begin
434+
d = Dict(:a=>2, :b=>3)
435+
436+
d2 = similar(d)
437+
@test d2 isa typeof(d)
438+
@test length(keys(d2)) == 2
439+
@test :a keys(d2)
440+
@test :b keys(d2)
441+
442+
d3 = similar(d, Float64)
443+
@test d3 isa Dict{Symbol, Float64}
444+
@test length(keys(d3)) == 2
445+
@test :a keys(d3)
446+
@test :b keys(d3)
447+
448+
d4 = similar(d, [10, 20, 30])
449+
@test d4 isa Dict{Int, Int}
450+
@test length(keys(d4)) == 3
451+
@test 10 keys(d4)
452+
@test 20 keys(d4)
453+
@test 30 keys(d4)
454+
455+
d5 = similar(d, Float64, [10, 20, 30])
456+
@test d5 isa Dict{Int, Float64}
457+
@test length(keys(d5)) == 3
458+
@test 10 keys(d5)
459+
@test 20 keys(d5)
460+
@test 30 keys(d5)
461+
end
462+
433463
@testset "generators, similar" begin
434464
d = Dict(:a=>"a")
435465
# TODO: restore when 0.7 deprecation is removed
@@ -507,8 +537,8 @@ import Base.ImmutableDict
507537
@test get(d, k1, :default) === :default
508538
@test d1["key1"] === v1
509539
@test d4["key1"] === v2
510-
@test similar(d3) === d
511-
@test similar(d) === d
540+
@test empty(d3) === d
541+
@test empty(d) === d
512542

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

651681
wkd = empty!(wkd)
652-
@test wkd == similar(wkd)
653-
@test typeof(wkd) == typeof(similar(wkd))
682+
@test wkd == empty(wkd)
683+
@test typeof(wkd) == typeof(empty(wkd))
654684
@test length(wkd) == 0
655685
@test isempty(wkd)
656686
@test isa(wkd, WeakKeyDict)

0 commit comments

Comments
 (0)