|
2 | 2 |
|
3 | 3 | isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl")
|
4 | 4 | using .Main.OffsetArrays
|
5 |
| -import .Main.OffsetArrays: IdOffsetRange |
| 5 | +import .Main.OffsetArrays: IdOffsetRange, no_offset_view |
6 | 6 | using Random
|
7 | 7 | using LinearAlgebra
|
8 | 8 | using Base: IdentityUnitRange
|
9 | 9 | using Test
|
10 | 10 |
|
| 11 | +no_offset_axes(x, d) = no_offset_view(axes(x, d)) |
| 12 | +no_offset_axes(x) = map(no_offset_view, axes(x)) |
| 13 | + |
| 14 | +function same_value(r1, r2) |
| 15 | + length(r1) == length(r2) || return false |
| 16 | + for (v1, v2) in zip(r1, r2) |
| 17 | + v1 == v2 || return false |
| 18 | + end |
| 19 | + return true |
| 20 | +end |
| 21 | + |
11 | 22 | if !isdefined(@__MODULE__, :T24Linear)
|
12 | 23 | include("testhelpers/arrayindexingtypes.jl")
|
13 | 24 | end
|
|
846 | 857 | a = OffsetArray(4:5, 5:6)
|
847 | 858 | @test reshape(a, :) === a
|
848 | 859 | @test reshape(a, (:,)) === a
|
| 860 | + |
| 861 | + A0 = [1 3; 2 4] |
| 862 | + A = OffsetArray(A0, (-1,2)) |
| 863 | + |
| 864 | + B = reshape(A0, -10:-9, 9:10) |
| 865 | + @test isa(B, OffsetArray{Int,2}) |
| 866 | + @test parent(B) == A0 |
| 867 | + @test axes(B) == IdentityUnitRange.((-10:-9, 9:10)) |
| 868 | + B = reshape(A, -10:-9, 9:10) |
| 869 | + @test isa(B, OffsetArray{Int,2}) |
| 870 | + @test axes(B) == IdentityUnitRange.((-10:-9, 9:10)) |
| 871 | + b = reshape(A, -7:-4) |
| 872 | + @test axes(b) == (IdentityUnitRange(-7:-4),) |
| 873 | + @test isa(parent(b), Vector{Int}) |
| 874 | + @test parent(b) == A0[:] |
| 875 | + a = OffsetArray(rand(3,3,3), -1:1, 0:2, 3:5) |
| 876 | + # Offset axes are required for reshape(::OffsetArray, ::Val) support |
| 877 | + b = reshape(a, Val(2)) |
| 878 | + @test isa(b, OffsetArray{Float64,2}) |
| 879 | + @test axes(b) == IdentityUnitRange.((-1:1, 1:9)) |
| 880 | + b = reshape(a, Val(4)) |
| 881 | + @test isa(b, OffsetArray{Float64,4}) |
| 882 | + @test axes(b) == (axes(a)..., IdentityUnitRange(1:1)) |
| 883 | + |
| 884 | + @test reshape(OffsetArray(-1:0, -1:0), :, 1) == reshape(-1:0, 2, 1) |
| 885 | + @test reshape(OffsetArray(-1:2, -1:2), -2:-1, :) == reshape(-1:2, -2:-1, 2) |
| 886 | + |
| 887 | + @test reshape(OffsetArray(-1:0, -1:0), :) == OffsetArray(-1:0, -1:0) |
| 888 | + @test reshape(A, :) == reshape(A0, :) |
| 889 | + |
| 890 | + # reshape with one Colon for AbstractArrays |
| 891 | + B = reshape(A0, -10:-9, :) |
| 892 | + @test B isa OffsetArray{Int,2} |
| 893 | + @test parent(B) == A0 |
| 894 | + @test no_offset_axes(B, 1) == -10:-9 |
| 895 | + @test axes(B, 2) == axes(A0, 2) |
| 896 | + |
| 897 | + B = reshape(A0, -10:-9, 3:3, :) |
| 898 | + @test B isa OffsetArray{Int,3} |
| 899 | + @test same_value(A0, B) |
| 900 | + @test no_offset_axes(B, 1) == -10:-9 |
| 901 | + @test no_offset_axes(B, 2) == 3:3 |
| 902 | + @test axes(B, 3) == 1:2 |
| 903 | + |
| 904 | + B = reshape(A0, -10:-9, 3:4, :) |
| 905 | + @test B isa OffsetArray{Int,3} |
| 906 | + @test same_value(A0, B) |
| 907 | + @test no_offset_axes(B, 1) == -10:-9 |
| 908 | + @test no_offset_axes(B, 2) == 3:4 |
| 909 | + @test axes(B, 3) == 1:1 |
| 910 | + |
| 911 | + # pop the parent |
| 912 | + B = reshape(A, size(A)) |
| 913 | + @test B == A0 |
| 914 | + B = reshape(A, (Base.OneTo(2), 2)) |
| 915 | + @test B == A0 |
| 916 | + B = reshape(A, (2,:)) |
| 917 | + @test B == A0 |
| 918 | + |
| 919 | + # julialang/julia #33614 |
| 920 | + A = OffsetArray(-1:0, (-2,)) |
| 921 | + @test reshape(A, :) == A |
| 922 | + Arsc = reshape(A, :, 1) |
| 923 | + Arss = reshape(A, 2, 1) |
| 924 | + @test Arsc[1,1] == Arss[1,1] == -1 |
| 925 | + @test Arsc[2,1] == Arss[2,1] == 0 |
| 926 | + @test_throws BoundsError Arsc[0,1] |
| 927 | + @test_throws BoundsError Arss[0,1] |
| 928 | + A = OffsetArray([-1,0], (-2,)) |
| 929 | + Arsc = reshape(A, :, 1) |
| 930 | + Arsc[1,1] = 5 |
| 931 | + @test first(A) == 5 |
| 932 | + |
| 933 | + Vec64 = zeros(6) |
| 934 | + ind_a_64 = 3 |
| 935 | + ind_a_32 =Int32.(ind_a_64) |
| 936 | + @test reshape(Vec64, ind_a_32, :) == reshape(Vec64, ind_a_64, :) |
| 937 | + |
849 | 938 | R = reshape(zeros(6), 2, :)
|
850 | 939 | @test R isa Matrix
|
851 | 940 | @test axes(R) == (1:2, 1:3)
|
|
856 | 945 | @test axes(R) == (2:3, 1:3)
|
857 | 946 | R = reshape(zeros(6,1), 2:3, :)
|
858 | 947 | @test axes(R) == (2:3, 1:3)
|
| 948 | + |
| 949 | + R = reshape(zeros(6), 1:2, :) |
| 950 | + @test axes(R) == (1:2, 1:3) |
| 951 | + R = reshape(zeros(6,1), 1:2, :) |
| 952 | + @test axes(R) == (1:2, 1:3) |
| 953 | + |
| 954 | + # reshape works even if the parent doesn't have 1-based indices |
| 955 | + # this works even if the parent doesn't support the reshape |
| 956 | + r = OffsetArray(IdentityUnitRange(0:1), -1) |
| 957 | + @test reshape(r, 2) == 0:1 |
| 958 | + @test reshape(r, (2,)) == 0:1 |
| 959 | + @test reshape(r, :) == OffsetArray(0:1, -1:0) |
| 960 | + @test reshape(r, (:,)) == OffsetArray(0:1, -1:0) |
| 961 | + |
| 962 | + @test reshape(ones(2:3, 4:5), (2, :)) == ones(2,2) |
| 963 | + |
| 964 | + # more than one colon is not allowed |
| 965 | + @test_throws Exception reshape(ones(3:4, 4:5, 1:2), :, :, 2) |
| 966 | + @test_throws Exception reshape(ones(3:4, 4:5, 1:2), :, 2, :) |
| 967 | + |
| 968 | + A = OffsetArray(rand(4, 4), -1, -1); |
| 969 | + B = reshape(A, (2, :)) |
| 970 | + @test axes(B, 1) == 1:2 |
| 971 | + @test axes(B, 2) == 1:8 |
| 972 | + |
| 973 | + # reshape with one single colon becomes a `vec` |
| 974 | + A = OffsetArray(rand(4, 4), -1, -1) |
| 975 | + @test reshape(A, (:, )) == vec(A) |
| 976 | + @test reshape(A, :) == vec(A) |
| 977 | + |
| 978 | + A0 = [1 3; 2 4] |
| 979 | + A = reshape(A0, 2:3, 4:5) |
| 980 | + @test axes(A) == Base.IdentityUnitRange.((2:3, 4:5)) |
| 981 | + @test reshape(A, axes(parent(A))..., :) == reshape(A0, axes(A0)..., :) |
| 982 | + |
| 983 | + B = reshape(A0, -10:-9, 9:10) |
| 984 | + @test isa(B, OffsetArray{Int,2}) |
| 985 | + @test parent(B) == A0 |
| 986 | + @test axes(B) == Base.IdentityUnitRange.((-10:-9, 9:10)) |
| 987 | + |
| 988 | + @testset "BigInt reshape" begin |
| 989 | + struct MyBigFill{T,N} <: AbstractArray{T,N} |
| 990 | + val :: T |
| 991 | + axes :: NTuple{N,Base.OneTo{BigInt}} |
| 992 | + end |
| 993 | + MyBigFill(val, sz::Tuple{}) = MyBigFill{typeof(val),0}(val, sz) |
| 994 | + MyBigFill(val, sz::Tuple{Vararg{Integer}}) = MyBigFill(val, map(Base.OneTo{BigInt}, sz)) |
| 995 | + Base.size(M::MyBigFill) = map(length, M.axes) |
| 996 | + Base.axes(M::MyBigFill) = M.axes |
| 997 | + function Base.getindex(M::MyBigFill{<:Any,N}, ind::Vararg{Integer,N}) where {N} |
| 998 | + checkbounds(M, ind...) |
| 999 | + M.val |
| 1000 | + end |
| 1001 | + function Base.isassigned(M::MyBigFill{<:Any,N}, ind::Vararg{BigInt,N}) where {N} |
| 1002 | + checkbounds(M, ind...) |
| 1003 | + true |
| 1004 | + end |
| 1005 | + function Base.reshape(M::MyBigFill, ind::NTuple{N,BigInt}) where {N} |
| 1006 | + length(M) == prod(ind) || throw(ArgumentError("length mismatch in reshape")) |
| 1007 | + MyBigFill(M.val, ind) |
| 1008 | + end |
| 1009 | + Base.reshape(M::MyBigFill, ind::Tuple{}) = MyBigFill(M.val, ind) |
| 1010 | + |
| 1011 | + M = MyBigFill(4, (2, 3)) |
| 1012 | + O = OffsetArray(M) |
| 1013 | + @test vec(O) isa MyBigFill |
| 1014 | + @test vec(O) == vec(M) |
| 1015 | + @test reshape(O, :, big(2)) == reshape(M, :, big(2)) |
| 1016 | + @test reshape(O, :, big(2)) isa MyBigFill |
| 1017 | + @test reshape(O, axes(M)) isa MyBigFill |
| 1018 | + @test reshape(O, axes(M)) == reshape(M, axes(M)) |
| 1019 | + @test reshape(O, axes(M,1), :) isa MyBigFill |
| 1020 | + @test reshape(O, axes(M,1), :) == reshape(M, axes(M,1), :) |
| 1021 | + @test reshape(O, axes(M,1), big(1), :) isa MyBigFill |
| 1022 | + @test reshape(O, axes(M,1), big(1), :) == reshape(M, axes(M,1), big(1), :) |
| 1023 | + |
| 1024 | + M = MyBigFill(4, (1,1)) |
| 1025 | + O = OffsetArray(M) |
| 1026 | + @test reshape(O) isa MyBigFill |
| 1027 | + @test reshape(O) == reshape(M) |
| 1028 | + end |
859 | 1029 | end
|
860 | 1030 |
|
861 | 1031 | @testset "stack" begin
|
@@ -924,14 +1094,3 @@ end
|
924 | 1094 | b = sum(a, dims=1)
|
925 | 1095 | @test b[begin] == sum(r)
|
926 | 1096 | end
|
927 |
| - |
928 |
| -@testset "reshape" begin |
929 |
| - A0 = [1 3; 2 4] |
930 |
| - A = reshape(A0, 2:3, 4:5) |
931 |
| - @test axes(A) == Base.IdentityUnitRange.((2:3, 4:5)) |
932 |
| - |
933 |
| - B = reshape(A0, -10:-9, 9:10) |
934 |
| - @test isa(B, OffsetArray{Int,2}) |
935 |
| - @test parent(B) == A0 |
936 |
| - @test axes(B) == Base.IdentityUnitRange.((-10:-9, 9:10)) |
937 |
| -end |
|
0 commit comments