|
1 | 1 | using OffsetArrays |
2 | 2 | using Test |
3 | 3 | using DelimitedFiles |
4 | | -using OffsetArrays: IdentityUnitRange |
| 4 | +using OffsetArrays: IdentityUnitRange, no_offset_view |
| 5 | +using CatIndices: BidirectionalVector |
5 | 6 |
|
6 | 7 | @test isempty(detect_ambiguities(OffsetArrays, Base, Core)) |
7 | 8 |
|
|
402 | 403 | @test typeof(OffsetVector{Float64}(undef, -2:2)) == typeof(OffsetArray{Float64}(undef, -2:2)) |
403 | 404 | end |
404 | 405 |
|
| 406 | +#### |
| 407 | +#### type defined for testing no_offset_view |
| 408 | +#### |
| 409 | + |
| 410 | +struct NegativeArray{T,N,S <: AbstractArray{T,N}} <: AbstractArray{T,N} |
| 411 | + parent::S |
| 412 | +end |
| 413 | + |
| 414 | +Base.axes(A::NegativeArray) = map(n -> (-n):(-1), size(A.parent)) |
| 415 | + |
| 416 | +Base.size(A::NegativeArray) = size(A.parent) |
| 417 | + |
| 418 | +function Base.getindex(A::NegativeArray{T,N}, I::Vararg{Int,N}) where {T,N} |
| 419 | + getindex(A.parent, (I .+ size(A.parent) .+ 1)...) |
| 420 | +end |
| 421 | + |
| 422 | +@testset "no offset view" begin |
| 423 | + # OffsetArray fallback |
| 424 | + A = randn(3, 3) |
| 425 | + O1 = OffsetArray(A, -1:1, 0:2) |
| 426 | + O2 = OffsetArray(O1, -2:0, -3:(-1)) |
| 427 | + @test no_offset_view(O2) ≡ A |
| 428 | + |
| 429 | + # generic fallback |
| 430 | + A = collect(reshape(1:12, 3, 4)) |
| 431 | + N = NegativeArray(A) |
| 432 | + @test N[-3, -4] == 1 |
| 433 | + V = no_offset_view(N) |
| 434 | + @test collect(V) == A |
| 435 | + |
| 436 | + # bidirectional |
| 437 | + B = BidirectionalVector([1, 2, 3]) |
| 438 | + pushfirst!(B, 0) |
| 439 | + OB = OffsetArrays.no_offset_view(B) |
| 440 | + @test axes(OB, 1) == 1:4 |
| 441 | + @test collect(OB) == 0:3 |
| 442 | +end |
| 443 | + |
405 | 444 | @testset "no nesting" begin |
406 | 445 | A = randn(2, 3) |
407 | 446 | x = A[2, 2] |
|
0 commit comments