1
1
2
2
@testset " dimensions" begin
3
3
4
+ @testset " to_dims" begin
5
+ @testset " small case" begin
6
+ @test ArrayInterface. to_dims ((:x , :y ), :x ) == 1
7
+ @test ArrayInterface. to_dims ((:x , :y ), :y ) == 2
8
+ @test_throws ArgumentError ArrayInterface. to_dims ((:x , :y ), :z ) # not found
9
+ end
10
+
11
+ @testset " large case" begin
12
+ @test ArrayInterface. to_dims ((:x , :y , :a , :b , :c , :d ), :x ) == 1
13
+ @test ArrayInterface. to_dims ((:x , :y , :a , :b , :c , :d ), :a ) == 3
14
+ @test ArrayInterface. to_dims ((:x , :y , :a , :b , :c , :d ), :d ) == 6
15
+ @test_throws ArgumentError ArrayInterface. to_dims ((:x , :y , :a , :b , :c , :d ), :z ) # not found
16
+ end
17
+ end
18
+
4
19
struct NamedDimsWrapper{L,T,N,P<: AbstractArray{T,N} } <: AbstractArray{T,N}
5
20
parent:: P
6
21
NamedDimsWrapper {L} (p) where {L} = new {L,eltype(p),ndims(p),typeof(p)} (p)
@@ -10,8 +25,11 @@ ArrayInterface.has_dimnames(::Type{T}) where {T<:NamedDimsWrapper} = true
10
25
ArrayInterface. dimnames (:: Type{T} ) where {L,T<: NamedDimsWrapper{L} } = L
11
26
Base. parent (x:: NamedDimsWrapper ) = x. parent
12
27
Base. size (x:: NamedDimsWrapper ) = size (parent (x))
28
+ Base. size (x:: NamedDimsWrapper , d) = ArrayInterface. size (x, d)
13
29
Base. axes (x:: NamedDimsWrapper ) = axes (parent (x))
30
+ Base. axes (x:: NamedDimsWrapper , d) = ArrayInterface. axes (x, d)
14
31
Base. strides (x:: NamedDimsWrapper ) = Base. strides (parent (x))
32
+ Base. strides (x:: NamedDimsWrapper , d) = ArrayInterface. strides (x, d)
15
33
16
34
Base. getindex (x:: NamedDimsWrapper ; kwargs... ) = ArrayInterface. getindex (x; kwargs... )
17
35
Base. getindex (x:: NamedDimsWrapper , args... ) = ArrayInterface. getindex (x, args... )
@@ -46,9 +64,9 @@ dnums = ntuple(+, length(d))
46
64
@test @inferred (ArrayInterface. to_dims (x, reverse (d))) === reverse (dnums)
47
65
@test_throws ArgumentError ArrayInterface. to_dims (x, :z )
48
66
49
- @test @inferred (ArrayInterface . size (x, :x )) == size (parent (x), 1 )
50
- @test @inferred (ArrayInterface . axes (x, :x )) == axes (parent (x), 1 )
51
- @test ArrayInterface . strides (x, :x ) == ArrayInterface. strides (parent (x))[1 ]
67
+ @test @inferred (size (x, :x )) == size (parent (x), 1 )
68
+ @test @inferred (axes (x, :x )) == axes (parent (x), 1 )
69
+ @test strides (x, :x ) == ArrayInterface. strides (parent (x))[1 ]
52
70
53
71
x[x = 1 ] = [2 , 3 ]
54
72
@test @inferred (getindex (x, x = 1 )) == [2 , 3 ]
0 commit comments