From af032b67c24f0e958b3cc0cd98d090e782a5d23c Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Sun, 24 Apr 2016 11:46:48 -0500 Subject: [PATCH] Test that sync does something different from zip --- test/{offsetarrays.jl => array_types.jl} | 22 ++++++++++++++- test/dense.jl | 36 ++++++++++++++++-------- test/runtests.jl | 4 +-- 3 files changed, 47 insertions(+), 15 deletions(-) rename test/{offsetarrays.jl => array_types.jl} (53%) diff --git a/test/offsetarrays.jl b/test/array_types.jl similarity index 53% rename from test/offsetarrays.jl rename to test/array_types.jl index 4008a2d..be07041 100644 --- a/test/offsetarrays.jl +++ b/test/array_types.jl @@ -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} @@ -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 diff --git a/test/dense.jl b/test/dense.jl index 0ef71dc..c5c0f71 100644 --- a/test/dense.jl +++ b/test/dense.jl @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index d62e668..6912443 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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()