Skip to content

Commit

Permalink
Check for repeated keys in construction, fixes #63
Browse files Browse the repository at this point in the history
  • Loading branch information
davidavdav committed Jun 22, 2018
1 parent 3feb4ad commit b86e95b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ letter(i) = string(Char((64+i) % 256))

## helpers for constructing names dictionaries
defaultnames(dim::Integer) = map(string, 1:dim)
defaultnamesdict(names::Vector) = OrderedDict(zip(names, 1:length(names)))
function defaultnamesdict(names::Vector)
dict = OrderedDict(zip(names, 1:length(names)))
length(dict) == length(names) || error("Cannot have duplicated names for indices")
return dict
end
defaultnamesdict(dim::Integer) = defaultnamesdict(defaultnames(dim))
defaultnamesdict(dims::Tuple) = map(defaultnamesdict, dims) # ::NTuple{length(dims), OrderedDict}

Expand Down
3 changes: 3 additions & 0 deletions test/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ for i in 1:5
n1 = @inferred NamedArray(Int, dims...)
n2 = @inferred NamedArray{Int}(dims...)
end

## repeated indices #63
@test_throws ErrorException @inferred n[["one", "one"], :]
10 changes: 5 additions & 5 deletions test/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ print("conversions, ")
print("changing names, ")
## changingnames
for f = (:sum, :prod, :maximum, :minimum, :mean, :std, :var)
for dim=1:2
for dim in 1:2
@eval @test ($f)(n.array, $dim) == ($f)(n, $dim).array
@eval @inferred ($f)(n, $dim)
end
end

for f in (:cumprod, :cumsum, :cumsum_kbn)
for dim=1:2
for dim in 1:2
@eval @test ($f)(n.array, $dim) == ($f)(n, $dim).array
@eval @inferred ($f)(n, $dim)
end
Expand All @@ -64,7 +64,7 @@ end
print("multi-dimensional, ")
#multidimensional
m = NamedArray(rand(2,3,4,3,2,3))
for i1=1:2, i2=1:3, i3=1:4, i4=1:3, i5=1:2, i6=1:3
for i1 in 1:2, i2 in 1:3, i3 in 1:4, i4 in 1:3, i5 in 1:2, i6 in 1:3
@test m[i1,i2,i3,i4,i5,i6] == m.array[i1,i2,i3,i4,i5,i6]
@test m[string(i1), string(i2), string(i3), string(i4), string(i5), string(i6)] == m.array[i1,i2,i3,i4,i5,i6]
end
Expand Down Expand Up @@ -120,7 +120,7 @@ for f in (:sin, :cos, :tan, :sinpi, :cospi, :sinh, :cosh, :tanh, :asin, :acos,
end
end
#39
v = n[1,:]
v = n[1, :]
@test sin.(v).array == sin.(v.array)
@test namesanddim(sin.(v)) == namesanddim(v)

Expand All @@ -139,7 +139,7 @@ include("show.jl")
include("speed.jl")

# julia issue #17328
a = NamedArray([1.0,2.0,3.0,4.0])
a = NamedArray([1.0, 2.0, 3.0, 4.0])
@test sum(abs, a, 1)[1] == 10

println("done!")

0 comments on commit b86e95b

Please sign in to comment.