comparison test/Grids/tensor_grid_test.jl @ 1266:a4ddae8b5d49 refactor/grids

Add tests for TensorGrid and make them pass
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 24 Feb 2023 21:42:28 +0100
parents c150eabaf656
children dbddd0f61bde
comparison
equal deleted inserted replaced
1265:9c9ea2900250 1266:a4ddae8b5d49
1 using Test
1 using Sbplib.Grids 2 using Sbplib.Grids
2 using Test 3 using StaticArrays
4 using Sbplib.RegionIndices
3 5
4 @testset "TensorGrid" begin 6 @testset "TensorGrid" begin
5 @test_broken false 7 g₁ = EquidistantGrid(range(0,1,length=11))
8 g₂ = EquidistantGrid(range(2,3,length=6))
9 g₃ = EquidistantGrid(1:10)
10 g₄ = ZeroDimGrid(@SVector[1,2])
6 11
12 @test TensorGrid(g₁, g₂) isa TensorGrid
13 @test TensorGrid(g₁, g₂) isa Grid{SVector{2,Float64}, 2}
14 @test TensorGrid(g₃, g₃) isa Grid{SVector{2,Int}, 2}
15 @test TensorGrid(g₁, g₂, g₃) isa Grid{SVector{3,Float64}, 3}
16 @test TensorGrid(g₁, g₄) isa Grid{SVector{3,Float64}, 1}
17 @test TensorGrid(g₁, g₄, g₂) isa Grid{SVector{4,Float64}, 2}
7 18
8 @testset "restrict" begin 19 @testset "Indexing Interface" begin
9 @test_broken restrict(g, 1:2) == nothing 20 @test TensorGrid(g₁, g₂)[1,1] isa SVector{2,Float64}
10 @test_broken restrict(g, 2:3) == nothing 21 @test TensorGrid(g₁, g₂)[1,1] == [0.0,2.0]
11 @test_broken restrict(g, [1,3]) == nothing 22 @test TensorGrid(g₁, g₂)[3,5] == [0.2,2.8]
12 @test_broken restrict(g, [2,1]) == nothing 23 @test TensorGrid(g₁, g₂)[10,6] == [0.9,3.0]
24
25 @test TensorGrid(g₁, g₃)[1,1] isa SVector{2,Float64}
26 @test TensorGrid(g₁, g₃)[1,1] == [0.0,1.0]
27
28 @test TensorGrid(g₁, g₂, g₃)[3,4,5] isa SVector{3,Float64}
29 @test TensorGrid(g₁, g₂, g₃)[3,4,5] == [0.2, 2.6, 5.0]
30
31 @test TensorGrid(g₁, g₄)[3] isa SVector{3,Float64}
32 @test TensorGrid(g₁, g₄)[3] == [0.2, 1., 2.]
33
34 @test TensorGrid(g₁, g₄, g₂)[3,2] isa SVector{4,Float64}
35 @test TensorGrid(g₁, g₄, g₂)[3,2] == [0.2, 1., 2., 2.2]
36
37 @test eachindex(TensorGrid(g₁, g₂)) == CartesianIndices((11,6))
38 @test eachindex(TensorGrid(g₁, g₃)) == CartesianIndices((11,10))
39 @test eachindex(TensorGrid(g₁, g₂, g₃)) == CartesianIndices((11,6,10))
40 @test eachindex(TensorGrid(g₁, g₄)) == CartesianIndices((11,))
41 @test eachindex(TensorGrid(g₁, g₄, g₂)) == CartesianIndices((11,6))
42 end
43
44 @testset "Iterator interface" begin
45 @test eltype(TensorGrid(g₁, g₂)) == SVector{2,Float64}
46 @test eltype(TensorGrid(g₁, g₃)) == SVector{2,Float64}
47 @test eltype(TensorGrid(g₁, g₂, g₃)) == SVector{3,Float64}
48 @test eltype(TensorGrid(g₁, g₄)) == SVector{3,Float64}
49 @test eltype(TensorGrid(g₁, g₄, g₂)) == SVector{4,Float64}
50
51 @test size(TensorGrid(g₁, g₂)) == (11,6)
52 @test size(TensorGrid(g₁, g₃)) == (11,10)
53 @test size(TensorGrid(g₁, g₂, g₃)) == (11,6,10)
54 @test size(TensorGrid(g₁, g₄)) == (11,)
55 @test size(TensorGrid(g₁, g₄, g₂)) == (11,6)
56
57 @test Base.IteratorSize(TensorGrid(g₁, g₂)) == Base.HasShape{2}()
58 @test Base.IteratorSize(TensorGrid(g₁, g₃)) == Base.HasShape{2}()
59 @test Base.IteratorSize(TensorGrid(g₁, g₂, g₃)) == Base.HasShape{3}()
60 @test Base.IteratorSize(TensorGrid(g₁, g₄)) == Base.HasShape{1}()
61 @test Base.IteratorSize(TensorGrid(g₁, g₄, g₂)) == Base.HasShape{2}()
62
63 @test iterate(TensorGrid(g₁, g₂))[1] isa SVector{2,Float64}
64 @test iterate(TensorGrid(g₁, g₃))[1] isa SVector{2,Float64}
65 @test iterate(TensorGrid(g₁, g₂, g₃))[1] isa SVector{3,Float64}
66 @test iterate(TensorGrid(g₁, g₄))[1] isa SVector{3,Float64}
67 @test iterate(TensorGrid(g₁, g₄, g₂))[1] isa SVector{4,Float64}
68
69 @test collect(TensorGrid(g₁, g₂)) == [@SVector[x,y] for x ∈ range(0,1,length=11), y ∈ range(2,3,length=6)]
70 @test collect(TensorGrid(g₁, g₃)) == [@SVector[x,y] for x ∈ range(0,1,length=11), y ∈ 1:10]
71 @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]
72 @test collect(TensorGrid(g₁, g₄)) == [@SVector[x,1,2] for x ∈ range(0,1,length=11)]
73 @test collect(TensorGrid(g₁, g₄, g₂)) == [@SVector[x,1,2,y] for x ∈ range(0,1,length=11), y ∈ range(2,3,length=6)]
74 end
75
76 @testset "refine" begin
77 g1(n) = EquidistantGrid(range(0,1,length=n))
78 g2(n) = EquidistantGrid(range(2,3,length=n))
79
80 @test refine(TensorGrid(g1(11), g2(6)),1) == TensorGrid(g1(11), g2(6))
81 @test refine(TensorGrid(g1(11), g2(6)),2) == TensorGrid(g1(21), g2(11))
82 @test refine(TensorGrid(g1(11), g2(6)),3) == TensorGrid(g1(31), g2(16))
83 @test_broken refine(TensorGrid(g1(11), g₄), 1) == TensorGrid(g1(11), g₄)
84 @test_broken refine(TensorGrid(g1(11), g₄), 2) == TensorGrid(g1(21), g₄)
85 end
86
87 @testset "coarsen" begin
88 g1(n) = EquidistantGrid(range(0,1,length=n))
89 g2(n) = EquidistantGrid(range(2,3,length=n))
90
91 @test coarsen(TensorGrid(g1(11), g2(6)),1) == TensorGrid(g1(11), g2(6))
92 @test coarsen(TensorGrid(g1(21), g2(11)),2) == TensorGrid(g1(11), g2(6))
93 @test coarsen(TensorGrid(g1(31), g2(16)),3) == TensorGrid(g1(11), g2(6))
94 @test_broken coarsen(TensorGrid(g1(11), g₄), 1) == TensorGrid(g1(11), g₄)
95 @test_broken coarsen(TensorGrid(g1(21), g₄), 2) == TensorGrid(g1(11), g₄)
96 end
97
98 @testset "boundary_identifiers" begin
99 @test boundary_identifiers(TensorGrid(g₁, g₂)) == map((n,id)->TensorGridBoundary{n,id}(), (1,1,2,2), (Lower,Upper,Lower,Upper))
100 @test_broken boundary_identifiers(TensorGrid(g₁, g₄)) == (TensorGridBoundary{1,Lower}(),TensorGridBoundary{1,Upper}())
101 end
102
103 @testset "boundary_grid" begin
104 @test boundary_grid(TensorGrid(g₁, g₂), TensorGridBoundary{1, Upper}()) == TensorGrid(ZeroDimGrid(g₁[end]), g₂)
105 @test boundary_grid(TensorGrid(g₁, g₄), TensorGridBoundary{1, Upper}()) == TensorGrid(ZeroDimGrid(g₁[end]), g₄)
13 end 106 end
14 end 107 end
108
109 @testset "combined_coordinate_vector_type" begin
110 @test Grids.combined_coordinate_vector_type(Float64) == Float64
111 @test Grids.combined_coordinate_vector_type(Float64, Int) == SVector{2,Float64}
112 @test Grids.combined_coordinate_vector_type(Float32, Int16, Int32) == SVector{3,Float32}
113
114 @test Grids.combined_coordinate_vector_type(SVector{2,Float64}) == SVector{2,Float64}
115 @test Grids.combined_coordinate_vector_type(SVector{2,Float64}, SVector{1,Float64}) == SVector{3,Float64}
116 @test Grids.combined_coordinate_vector_type(SVector{2,Float64}, SVector{1,Int}, SVector{3, Float32}) == SVector{6,Float64}
117 end
118
119 @testset "combine_coordinates" begin
120 @test Grids.combine_coordinates(1,2,3) isa SVector{3, Int}
121 @test Grids.combine_coordinates(1,2,3) == [1,2,3]
122 @test Grids.combine_coordinates(1,2.,3) isa SVector{3, Float64}
123 @test Grids.combine_coordinates(1,2.,3) == [1,2,3]
124 @test Grids.combine_coordinates(1,@SVector[2.,3]) isa SVector{3, Float64}
125 @test Grids.combine_coordinates(1,@SVector[2.,3]) == [1,2,3]
126 end