comparison test/Grids/grid_test.jl @ 1515:b2eaa1ad19c8 feature/grids/componentview

Move correctness tests of componentview to the tests for ArrayComponentView
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 20 Mar 2024 16:17:53 +0100
parents cfc24f132641
children 8d64f8981bb0
comparison
equal deleted inserted replaced
1509:cfc24f132641 1515:b2eaa1ad19c8
62 f(x,y) = sin(x)*cos(y) 62 f(x,y) = sin(x)*cos(y)
63 @test eval_on(g, f) == map(x̄->f(x̄...), g) 63 @test eval_on(g, f) == map(x̄->f(x̄...), g)
64 end 64 end
65 65
66 @testset "componentview" begin 66 @testset "componentview" begin
67 # REVIEW: I think we can reduce the index ranges.
68 v = [@SMatrix[1 3; 2 4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4] 67 v = [@SMatrix[1 3; 2 4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
69 68
70 @test componentview(v, 1, 1) == [1 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4] 69 @test componentview(v, 1, 1) isa AbstractArray
71 @test componentview(v, 1, 2) == [3 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4] 70 @test componentview(v, 1, :) isa AbstractArray
72 @test componentview(v, 2, 1) == [2 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
73
74 @test componentview(v, 1, :) == [@SVector[1,3] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
75 @test componentview(v, 2, :) == [@SVector[2,4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
76 @test componentview(v, :, 1) == [@SVector[1,2] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
77 @test componentview(v, :, 2) == [@SVector[3,4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
78
79 71
80 A = @SMatrix[ 72 A = @SMatrix[
81 1 4 7; 73 1 4 7;
82 2 5 8; 74 2 5 8;
83 3 6 9; 75 3 6 9;
84 ] 76 ]
85 v = [A .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4] 77 v = [A .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
86 @test componentview(v, 1:2, 1:2) == [@SMatrix[1 4;2 5] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4] 78 @test componentview(v, 2:3, 1:2) isa AbstractArray
87 @test componentview(v, 2:3, 1:2) == [@SMatrix[2 5;3 6] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4] 79
80 # Correctness of the result is tested in ArrayComponentView
88 end 81 end
89 82
90 @testset "ArrayComponentView" begin 83 @testset "ArrayComponentView" begin
91 v = [@SMatrix[1 3; 2 4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4] 84 v = [@SMatrix[1 3; 2 4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
92 85
93 @testset "==" begin 86 @testset "==" begin
94 @test ArrayComponentView(v, (1,1)) == ArrayComponentView(v, (1,1)) 87 @test ArrayComponentView(v, (1,1)) == ArrayComponentView(v, (1,1))
95 @test ArrayComponentView(v, (1,1)) == ArrayComponentView(copy(v), (1,1)) 88 @test ArrayComponentView(v, (1,1)) == ArrayComponentView(copy(v), (1,1))
96 @test ArrayComponentView(v, (1,1)) == [1 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4] 89 @test ArrayComponentView(v, (1,1)) == [1 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
97 @test [1 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4] == ArrayComponentView(v, (1,1)) 90 @test [1 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4] == ArrayComponentView(v, (1,1))
91 end
92
93 @testset "components" begin
94 # REVIEW: I think we can reduce the index ranges.
95 v = [@SMatrix[1 3; 2 4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
96
97 # switch to indexing in test?
98 @test ArrayComponentView(v, (1, 1)) == [1 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
99 @test ArrayComponentView(v, (1, 2)) == [3 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
100 @test ArrayComponentView(v, (2, 1)) == [2 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
101
102 @test ArrayComponentView(v, (1, :)) == [@SVector[1,3] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
103 @test ArrayComponentView(v, (2, :)) == [@SVector[2,4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
104 @test ArrayComponentView(v, (:, 1)) == [@SVector[1,2] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
105 @test ArrayComponentView(v, (:, 2)) == [@SVector[3,4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
106
107
108 A = @SMatrix[
109 1 4 7;
110 2 5 8;
111 3 6 9;
112 ]
113 v = [A .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
114 @test ArrayComponentView(v, (1:2, 1:2)) == [@SMatrix[1 4;2 5] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
115 @test ArrayComponentView(v, (2:3, 1:2)) == [@SMatrix[2 5;3 6] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
98 end 116 end
99 end 117 end
100 118
101 @testset "_ncomponents" begin 119 @testset "_ncomponents" begin
102 @test Grids._ncomponents(Int) == 1 120 @test Grids._ncomponents(Int) == 1