Skip to content

Commit 2f048f7

Browse files
author
Andy Ferris
committed
More comprehensive tests, squashed some bugs
1 parent 651c9b6 commit 2f048f7

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

src/getindices.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ function getindices(container, indices)
3434
return map(i -> container[i], indices)
3535
end
3636
getindices(container, ::Colon) = getindices(container, keys(container))
37+
getindices(t::Tuple, ::Colon) = t
38+
@static if VERSION > v"0.7-"
39+
getindices(nt::NamedTuple, ::Colon) = nt
40+
end
3741

3842
function getindices(container, indices::AbstractDict)
3943
out = empty(indices, keytype(indices), _valtype(container))
@@ -42,12 +46,17 @@ function getindices(container, indices::AbstractDict)
4246
end
4347
return out
4448
end
49+
function getindices(d::AbstractDict, ::Colon)
50+
return copy(d)
51+
end
4552

4653
# Make a 0-D array instead of scalar
4754
Base.@propagate_inbounds function getindices(a::AbstractArray, is::Int...)
4855
out = similar(a, eltype(a), ())
4956
out[] = a[is...]
5057
return out
5158
end
59+
60+
# Ambiguities
5261
Base.@propagate_inbounds getindices(a::AbstractArray, is::Union{Int,Colon,AbstractArray}...) = a[is...]
53-
getindices(a::AbstractVector, ::Colon) = a[:] # Ambiguity
62+
getindices(a::AbstractVector, ::Colon) = a[:]

src/setindices.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ function setindices!(container, value, indices::AbstractDict)
1818
end
1919
return container
2020
end
21+
22+
if VERSION < v"0.7-"
23+
setindices!(a::AbstractArray, value, ::Colon) = setindices!(a, value, eachindex(a))
24+
end

src/view.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,4 @@ ViewDict{Symbol,Int64,Array{Int64,1},Dict{Symbol,Int64}} with 2 entries:
177177
"""
178178
view(d, inds::AbstractDict) = ViewDict(d, inds)
179179
view(d::AbstractArray, inds::AbstractDict) = ViewDict(d, inds) # disambiguation
180-
parent(d::ViewDict) = v.parent
180+
parent(d::ViewDict) = d.parent

test/runtests.jl

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ end
1313
@static if VERSION > v"0.7-"
1414
@test getindices(d, (aa = :a, cc = :c)) == (aa = "Alice", cc = "Charlie")
1515
end
16+
@test getindices(d, :) == d
1617

1718
v = [11, 12, 13]
1819
@test (getindices(v, 2)::Array{Int, 0})[] == 12
@@ -22,6 +23,7 @@ end
2223
@static if VERSION > v"0.7-"
2324
@test getindices(v, (a = 1, c = 3)) === (a = 11, c = 13)
2425
end
26+
@test getindices(v, :) == v
2527

2628
t = (11, 12, 13)
2729
@test getindices(t, [1, 3]) == [11, 13]
@@ -30,13 +32,15 @@ end
3032
@static if VERSION > v"0.7-"
3133
@test getindices(t, (a = 1, c = 3)) === (a = 11, c = 13)
3234
end
35+
@test getindices(t, :) == t
3336

3437
@static if VERSION > v"0.7-"
3538
nt = (a = 1, b = 2.0, c = "three")
3639
@test getindices(nt, [:a, :c]) == [1, "three"]
3740
@test getindices(nt, Dict(:aa => :a, :cc => :c)) == Dict(:aa => 1, :cc => "three")
3841
@test getindices(nt, (:a, :c)) == (1, "three")
3942
@test getindices(nt, (aa = :a, cc = :c)) == (aa = 1, cc = "three")
43+
@test getindices(nt, :) == nt
4044
end
4145
end
4246

@@ -60,6 +64,10 @@ end
6064
@test d5 == Dict(:a => "Someone", :b => "Bob", :c => "Someone")
6165
end
6266

67+
d6 = copy(d)
68+
setindices!(d6, "Someone", :)
69+
@test d6 == Dict(:a => "Someone", :b => "Someone", :c => "Someone")
70+
6371
v = [11, 12, 13]
6472
v2 = copy(v)
6573
setindices!(v2, 20, [1, 3])
@@ -78,27 +86,49 @@ end
7886
setindices!(v5, 20, (a = 1, c = 3))
7987
@test v5 == [20, 12, 20]
8088
end
89+
90+
v6 = copy(v)
91+
setindices!(v6, 20, :)
92+
@test v6 == [20, 20, 20]
8193
end
8294

8395
@testset "view" begin
8496
d = Dict(:a => "Alice", :b => "Bob", :c => "Charlie")
97+
d2 = copy(d)
8598

8699
@test view(d, [:a, :c])::ViewArray == ["Alice", "Charlie"]
87100
@test view(d, Dict(:aa => :a, :cc => :c))::ViewDict == Dict(:aa => "Alice", :cc => "Charlie")
88101

89102
av = view(d, [:a, :c])
103+
@test parent(av) === d
104+
@test av[1] == "Alice"
105+
@test Indexing.axes(av) === (Base.OneTo(2),)
90106
av[1] = "Someone"
91107
@test d == Dict(:a => "Someone", :b => "Bob", :c => "Charlie")
92108

93-
dv = view(d, Dict(:aa => :a, :cc => :c))
109+
dv = view(d2, Dict(:aa => :a, :cc => :c))
110+
@test parent(dv) === d2
111+
@test dv[:aa] == "Alice"
112+
@test keys(dv) [:aa, :cc] # Probably will want to change this
113+
@test length(keys(dv)) == 2
114+
@test haskey(dv, :aa)
94115
dv[:aa] = "No-one"
95-
@test d == Dict(:a => "No-one", :b => "Bob", :c => "Charlie")
116+
@test d2 == Dict(:a => "No-one", :b => "Bob", :c => "Charlie")
96117

97118
v = [11, 12, 13]
98119

99120
@test view(v, Dict(:a =>1 , :c => 3))::ViewDict == Dict(:a => 11, :c => 13)
100121

101122
dv2 = view(v, Dict(:a =>1 , :c => 3))
123+
@test parent(dv2) === v
124+
@test dv2[:a] == 11
125+
@test keys(dv2) [:a, :c]
126+
@test length(keys(dv2)) == 2
127+
@test haskey(dv2, :a)
102128
dv2[:a] = 21
103129
@test v == [21, 12, 13]
130+
131+
@test ViewArray(d, [:b, :c])::ViewArray == ["Bob", "Charlie"]
132+
@test ViewVector(d, [:b, :c])::ViewVector == ["Bob", "Charlie"]
133+
@test ViewMatrix(d, [:b :c; :c :b])::ViewMatrix == ["Bob" "Charlie"; "Charlie" "Bob"]
104134
end

0 commit comments

Comments
 (0)