diff --git a/Test/Project.toml b/Test/Project.toml new file mode 100644 index 00000000..8a2eb6c2 --- /dev/null +++ b/Test/Project.toml @@ -0,0 +1,2 @@ +[deps] +NamedArrays = "86f7a689-2022-50b4-a561-43c23ac3c673" diff --git a/test/runtests.jl b/test/runtests.jl index a863af41..dfb8173a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,6 +7,7 @@ using StaticArrays using OffsetArrays using Test using Unitful +using NamedArrays ## Test setup @@ -598,6 +599,29 @@ end @test convert(Cholesky{Float32,Matrix{Float32}}, chol).factors isa Matrix{Float32} end + +get_empty(cv::ComponentVector) = ComponentVector(similar(getdata(cv),0), (ComponentArrays.NullAxis(),)) + +@testset "KeepIndices" begin + cv = ComponentVector(a=(a1=100,a2=(a21=210, a22=reshape(1:4,(2,2))),a3=300), b=3, c=4) + # test whether underlying array type is preerved by using a NamedArray + cvn = ComponentVector(NamedArray(getdata(cv),(labels(cv),)), getaxes(cv)) + @test_throws ArgumentError cv[(3,)] # only allow symbols + @test_broken @test == cv[Symbol[]] = get_empty(cv) + @test_broken @test == cvn[Symbol[]] = get_empty(cvn) + @test_broken @test cv[(:b,)] == cv[[:b]] == cv[KeepIndex(:b)] + cv_sub = cv[(:a,:b)] + cv_subt = vcat(cv[KeepIndex(:a)],cv[KeepIndex(:b)]) + @test cv_sub == cv_subt + @test getdata(cvn[(:a,:b)]) isa NamedVector + @test_broken ca2[(:a,:b)] # PartitionedAyis + cv = ComponentArray(a=1, b=[1,2]) # array axis + @test cv[(:a,:b)] == vcat(cv[KeepIndex(:a)],cv[KeepIndex(:b)]) + # + cvn_sub = cvn[KeepIndices(:a,:b, :c)] # 3 comp to test proper reduce rather than vcat + @test getdata(cvn_sub) isa NamedArray +end + @testset "Autodiff" begin include("autodiff_tests.jl") end \ No newline at end of file