Skip to content

Commit

Permalink
Test that sync does something different from zip
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Apr 24, 2016
1 parent bc4e4b5 commit af032b6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
22 changes: 21 additions & 1 deletion test/offsetarrays.jl → test/array_types.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# A type to test unconventional indexing ranges

module OAs # OffsetArrays
module ATs # OffsetArrays

using Base.PermutedDimsArrays: PermutedDimsArray
import ArrayIterationPlayground: inds

immutable OA{T,N,AA<:AbstractArray} <: AbstractArray{T,N}
Expand All @@ -23,4 +24,23 @@ offset{N}(offsets::NTuple{N,Int}, inds::NTuple{N,Int}) = _offset((), offsets, in
_offset(out, ::Tuple{}, ::Tuple{}) = out
@inline _offset(out, offsets, inds) = _offset((out..., inds[1]-offsets[1]), Base.tail(offsets), Base.tail(inds))

# An iterator that deliberately makes PermutedDimsArrays more "dangerous"
# (sync to the rescue!)
immutable PDAIterator
iter::UnitRange{Int}
end
immutable PDAIndex
i::Int
end

Base.parent(A::PermutedDimsArray) = A.parent # move to Base
Base.eachindex{T,N,AA<:Array}(A::PermutedDimsArray{T,N,AA}) = PDAIterator(eachindex(A.parent))

Base.start(iter::PDAIterator) = start(iter.iter)
Base.next(iter::PDAIterator, s) = ((i, s) = next(iter.iter, s); (PDAIndex(i), s))
Base.done(iter::PDAIterator, s) = done(iter.iter, s)

Base.getindex(A::PermutedDimsArray, i::PDAIndex) = parent(A)[i.i]
Base.setindex!(A::PermutedDimsArray, val, i::PDAIndex) = parent(A)[i.i] = val

end
36 changes: 24 additions & 12 deletions test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,38 @@ A = copy(reshape(1:4, 2, 2))
B = Array{Int}(2, 2)
C = PermutedDimsArray(Array{Int}(2, 2), [2,1])

function mycopy!(dest, src)
function badcopy!(dest, src)
for (I, s) in zip(eachindex(dest), src)
dest[I] = s
end
dest
end

@test badcopy!(B, A) == A
badcopy!(C, A)
@test C[2,1] != A[2,1] # oops!
@test C[2,1] == A[1,2]

function goodcopy!(dest, src)
for (I, s) in sync(index(dest), src)
dest[I] = s
end
dest
end

@test mycopy!(B, A) == A
@test mycopy!(C, A) == A
@test C.parent == A'
@test goodcopy!(B, A) == A
@test B[2,1] == A[2,1]
@test B[1,2] == A[1,2]

D = OAs.OA(Array{Int}(2,2), (-1,2))
@test_throws DimensionMismatch mycopy!(D, A)
E = OAs.OA(A, (-1,2))
mycopy!(D, E)
D = ATs.OA(Array{Int}(2,2), (-1,2))
@test_throws DimensionMismatch goodcopy!(D, A)
E = ATs.OA(A, (-1,2))
goodcopy!(D, E)
@test D[0,3] == 1
@test D[1,3] == 2
@test D[0,4] == 3
@test D[1,4] == 4
D = OAs.OA(Array{Int}(2,2), (-2,2))
@test_throws DimensionMismatch mycopy!(D, E)
D = OAs.OA(Array{Int}(2,2), (-1,1))
@test_throws DimensionMismatch mycopy!(D, E)
D = ATs.OA(Array{Int}(2,2), (-2,2))
@test_throws DimensionMismatch goodcopy!(D, E)
D = ATs.OA(Array{Int}(2,2), (-1,1))
@test_throws DimensionMismatch goodcopy!(D, E)
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ using Base.PermutedDimsArrays: PermutedDimsArray

const AIP = ArrayIterationPlayground

include("offsetarrays.jl") # just for testing
include("array_types.jl") # just for testing

A = zeros(2,3)
@test inds(A, 1) == 1:2
@test inds(A, 2) == 1:3
@test inds(A, 3) == 1:1
@test inds(A) == (1:2, 1:3)
B = OAs.OA(Array{Int}(2,2), (-1,2))
B = ATs.OA(Array{Int}(2,2), (-1,2))
@test inds(B) == (0:1, 3:4)

io = IOBuffer()
Expand Down

0 comments on commit af032b6

Please sign in to comment.