Skip to content

Commit

Permalink
Do not allow short arrays, they're just hard to handle
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed May 16, 2022
1 parent a51db4e commit da82de1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/StarWarsArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ struct StarWarsArray{T,N,P<:AbstractArray,O<:StarWarsOrder} <: AbstractArray{T,N
parent::P
end
function StarWarsArray(p::P, order::Type{<:StarWarsOrder}=OriginalOrder) where {T,N,P<:AbstractArray{T,N}}
StarWarsArray{T,N,P,order}(p)
if order === OriginalOrder
minimum(size(p)) < 6 &&
throw(ArgumentError("A StarWarsArray with OriginalOrder requires at least 6 elements in each dimension. Input array has size $(size(p))"))
end
return StarWarsArray{T,N,P,order}(p)
end

machete_view_index(i) = range(1, stop=i)
function StarWarsArray(p::P, order::Type{MacheteOrder}) where {T,N,P<:AbstractArray{T,N}}
StarWarsArray{T,N,P,order}(view(p, machete_view_index.(size(p) .- 1)...))
minimum(size(p)) < 5 &&
throw(ArgumentError("A StarWarsArray with MacheteOrder requires at least 5 elements in each dimension. Input array has size $(size(p))"))
return StarWarsArray{T,N,P,order}(view(p, machete_view_index.(size(p) .- 1)...))
end

order(::StarWarsArray{T,N,P,O}) where {T,N,P,O} = O
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ import StarWarsArrays: StarWarsError, order
@test axes(m) == ([4, 5, 6, 1, 2, 3, 7, 8, 9], [4, 5, 6, 1, 2, 3, 7, 8, 9])
@test length(m) == 81
@test size(m) == (9, 9)

@test_throws ArgumentError StarWarsArray(rand(2), OriginalOrder)
@test_throws ArgumentError StarWarsArray(rand(8, 2), OriginalOrder)
end

@testset "MacheteOrder" begin
Expand Down Expand Up @@ -79,6 +82,9 @@ end
@test length(m) == 64
@test size(m) == (8, 8)

@test_throws ArgumentError StarWarsArray(rand(4), MacheteOrder)
@test_throws ArgumentError StarWarsArray(rand(6, 2, 9), MacheteOrder)

@test sprint(showerror, StarWarsArrays.StarWarsError(1, MacheteOrder)) ==
"StarWarsError: there is no episode 1 in MacheteOrder"
end

0 comments on commit da82de1

Please sign in to comment.