comparison test/Grids/tensor_grid_test.jl @ 1421:69c9e6eae686

Merge bugfix/grids/complete_interface_impl
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 25 Aug 2023 08:49:07 +0200
parents 1aa8adbf13d5
children 949fe47d65a4 f11615573e4d e3a80ef08d09
comparison
equal deleted inserted replaced
1409:cb5f30cd0e68 1421:69c9e6eae686
32 @test TensorGrid(g₁, g₄)[3] isa SVector{3,Float64} 32 @test TensorGrid(g₁, g₄)[3] isa SVector{3,Float64}
33 @test TensorGrid(g₁, g₄)[3] == [0.2, 1., 2.] 33 @test TensorGrid(g₁, g₄)[3] == [0.2, 1., 2.]
34 34
35 @test TensorGrid(g₁, g₄, g₂)[3,2] isa SVector{4,Float64} 35 @test TensorGrid(g₁, g₄, g₂)[3,2] isa SVector{4,Float64}
36 @test TensorGrid(g₁, g₄, g₂)[3,2] == [0.2, 1., 2., 2.2] 36 @test TensorGrid(g₁, g₄, g₂)[3,2] == [0.2, 1., 2., 2.2]
37
38 g = TensorGrid(g₁, g₂)
39 @test g[begin, begin] == g[1,1]
40 @test g[begin, end] == g[1,6]
41 @test g[end, end] == g[11,6]
37 end 42 end
38 43
39 @testset "cartesian indexing" begin 44 @testset "cartesian indexing" begin
40 cases = [ 45 cases = [
41 (TensorGrid(g₁, g₂), (1,1) ), 46 (TensorGrid(g₁, g₂), (1,1) ),
57 @test eachindex(TensorGrid(g₁, g₃)) == CartesianIndices((11,10)) 62 @test eachindex(TensorGrid(g₁, g₃)) == CartesianIndices((11,10))
58 @test eachindex(TensorGrid(g₁, g₂, g₃)) == CartesianIndices((11,6,10)) 63 @test eachindex(TensorGrid(g₁, g₂, g₃)) == CartesianIndices((11,6,10))
59 @test eachindex(TensorGrid(g₁, g₄)) == CartesianIndices((11,)) 64 @test eachindex(TensorGrid(g₁, g₄)) == CartesianIndices((11,))
60 @test eachindex(TensorGrid(g₁, g₄, g₂)) == CartesianIndices((11,6)) 65 @test eachindex(TensorGrid(g₁, g₄, g₂)) == CartesianIndices((11,6))
61 end 66 end
67
68 @testset "firstindex" begin
69 @test firstindex(TensorGrid(g₁, g₂, g₃), 1) == 1
70 @test firstindex(TensorGrid(g₁, g₂, g₃), 2) == 1
71 @test firstindex(TensorGrid(g₁, g₂, g₃), 3) == 1
72 end
73
74 @testset "lastindex" begin
75 @test lastindex(TensorGrid(g₁, g₂, g₃), 1) == 11
76 @test lastindex(TensorGrid(g₁, g₂, g₃), 2) == 6
77 @test lastindex(TensorGrid(g₁, g₂, g₃), 3) == 10
78 end
62 end 79 end
63 80
64 @testset "Iterator interface" begin 81 @testset "Iterator interface" begin
65 @test eltype(TensorGrid(g₁, g₂)) == SVector{2,Float64} 82 @test eltype(TensorGrid(g₁, g₂)) == SVector{2,Float64}
66 @test eltype(TensorGrid(g₁, g₃)) == SVector{2,Float64} 83 @test eltype(TensorGrid(g₁, g₃)) == SVector{2,Float64}
89 @test collect(TensorGrid(g₁, g₂)) == [@SVector[x,y] for x ∈ range(0,1,length=11), y ∈ range(2,3,length=6)] 106 @test collect(TensorGrid(g₁, g₂)) == [@SVector[x,y] for x ∈ range(0,1,length=11), y ∈ range(2,3,length=6)]
90 @test collect(TensorGrid(g₁, g₃)) == [@SVector[x,y] for x ∈ range(0,1,length=11), y ∈ 1:10] 107 @test collect(TensorGrid(g₁, g₃)) == [@SVector[x,y] for x ∈ range(0,1,length=11), y ∈ 1:10]
91 @test collect(TensorGrid(g₁, g₂, g₃)) == [@SVector[x,y,z] for x ∈ range(0,1,length=11), y ∈ range(2,3,length=6), z ∈ 1:10] 108 @test collect(TensorGrid(g₁, g₂, g₃)) == [@SVector[x,y,z] for x ∈ range(0,1,length=11), y ∈ range(2,3,length=6), z ∈ 1:10]
92 @test collect(TensorGrid(g₁, g₄)) == [@SVector[x,1,2] for x ∈ range(0,1,length=11)] 109 @test collect(TensorGrid(g₁, g₄)) == [@SVector[x,1,2] for x ∈ range(0,1,length=11)]
93 @test collect(TensorGrid(g₁, g₄, g₂)) == [@SVector[x,1,2,y] for x ∈ range(0,1,length=11), y ∈ range(2,3,length=6)] 110 @test collect(TensorGrid(g₁, g₄, g₂)) == [@SVector[x,1,2,y] for x ∈ range(0,1,length=11), y ∈ range(2,3,length=6)]
111 end
112
113 @testset "Base" begin
114 g₁ = EquidistantGrid(range(0,1,length=11))
115 g₂ = EquidistantGrid(range(2,3,length=6))
116 g = TensorGrid(g₁, g₂)
117
118 @test axes(g, 1) == 1:11
119 @test axes(g, 2) == 1:6
120 @test axes(g) == (1:11,1:6)
94 end 121 end
95 122
96 @testset "refine" begin 123 @testset "refine" begin
97 g1(n) = EquidistantGrid(range(0,1,length=n)) 124 g1(n) = EquidistantGrid(range(0,1,length=n))
98 g2(n) = EquidistantGrid(range(2,3,length=n)) 125 g2(n) = EquidistantGrid(range(2,3,length=n))
142 @test Grids.combine_coordinates(1,2.,3) isa SVector{3, Float64} 169 @test Grids.combine_coordinates(1,2.,3) isa SVector{3, Float64}
143 @test Grids.combine_coordinates(1,2.,3) == [1,2,3] 170 @test Grids.combine_coordinates(1,2.,3) == [1,2,3]
144 @test Grids.combine_coordinates(1,@SVector[2.,3]) isa SVector{3, Float64} 171 @test Grids.combine_coordinates(1,@SVector[2.,3]) isa SVector{3, Float64}
145 @test Grids.combine_coordinates(1,@SVector[2.,3]) == [1,2,3] 172 @test Grids.combine_coordinates(1,@SVector[2.,3]) == [1,2,3]
146 end 173 end
174
175 @testset "grid_and_local_dim_index" begin
176 cases = [
177 ((1,), 1) => (1,1),
178
179 ((1,1), 1) => (1,1),
180 ((1,1), 2) => (2,1),
181
182 ((1,2), 1) => (1,1),
183 ((1,2), 2) => (2,1),
184 ((1,2), 3) => (2,2),
185
186 ((2,1), 1) => (1,1),
187 ((2,1), 2) => (1,2),
188 ((2,1), 3) => (2,1),
189
190 ((2,1,3), 1) => (1,1),
191 ((2,1,3), 2) => (1,2),
192 ((2,1,3), 3) => (2,1),
193 ((2,1,3), 4) => (3,1),
194 ((2,1,3), 5) => (3,2),
195 ((2,1,3), 6) => (3,3),
196 ]
197
198 @testset "grid_and_local_dim_index$args" for (args, expected) ∈ cases
199 @test Grids.grid_and_local_dim_index(args...) == expected
200 end
201 end