Mercurial > repos > public > sbplib_julia
comparison test/Grids/tensor_grid_test.jl @ 1360:f59228534d3a tooling/benchmarks
Merge default
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Sat, 20 May 2023 15:15:22 +0200 |
parents | 42ecd4b3e215 |
children | 3d6425c36d32 |
comparison
equal
deleted
inserted
replaced
1321:42738616422e | 1360:f59228534d3a |
---|---|
1 using Test | |
2 using Sbplib.Grids | |
3 using StaticArrays | |
4 using Sbplib.RegionIndices | |
5 | |
6 @testset "TensorGrid" begin | |
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]) | |
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} | |
18 | |
19 @testset "Indexing Interface" begin | |
20 @testset "regular indexing" begin | |
21 @test TensorGrid(g₁, g₂)[1,1] isa SVector{2,Float64} | |
22 @test TensorGrid(g₁, g₂)[1,1] == [0.0,2.0] | |
23 @test TensorGrid(g₁, g₂)[3,5] == [0.2,2.8] | |
24 @test TensorGrid(g₁, g₂)[10,6] == [0.9,3.0] | |
25 | |
26 @test TensorGrid(g₁, g₃)[1,1] isa SVector{2,Float64} | |
27 @test TensorGrid(g₁, g₃)[1,1] == [0.0,1.0] | |
28 | |
29 @test TensorGrid(g₁, g₂, g₃)[3,4,5] isa SVector{3,Float64} | |
30 @test TensorGrid(g₁, g₂, g₃)[3,4,5] == [0.2, 2.6, 5.0] | |
31 | |
32 @test TensorGrid(g₁, g₄)[3] isa SVector{3,Float64} | |
33 @test TensorGrid(g₁, g₄)[3] == [0.2, 1., 2.] | |
34 | |
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] | |
37 end | |
38 | |
39 @testset "cartesian indexing" begin | |
40 cases = [ | |
41 (TensorGrid(g₁, g₂), (1,1) ), | |
42 (TensorGrid(g₁, g₂), (3,5) ), | |
43 (TensorGrid(g₁, g₂), (10,6) ), | |
44 (TensorGrid(g₁, g₃), (1,1) ), | |
45 (TensorGrid(g₁, g₂, g₃), (3,4,5)), | |
46 (TensorGrid(g₁, g₄), (3) ), | |
47 (TensorGrid(g₁, g₄, g₂), (3,2) ), | |
48 ] | |
49 | |
50 @testset "i = $is" for (g, is) ∈ cases | |
51 @test g[CartesianIndex(is...)] == g[is...] | |
52 end | |
53 end | |
54 | |
55 @testset "eachindex" begin | |
56 @test eachindex(TensorGrid(g₁, g₂)) == CartesianIndices((11,6)) | |
57 @test eachindex(TensorGrid(g₁, g₃)) == CartesianIndices((11,10)) | |
58 @test eachindex(TensorGrid(g₁, g₂, g₃)) == CartesianIndices((11,6,10)) | |
59 @test eachindex(TensorGrid(g₁, g₄)) == CartesianIndices((11,)) | |
60 @test eachindex(TensorGrid(g₁, g₄, g₂)) == CartesianIndices((11,6)) | |
61 end | |
62 end | |
63 | |
64 @testset "Iterator interface" begin | |
65 @test eltype(TensorGrid(g₁, g₂)) == SVector{2,Float64} | |
66 @test eltype(TensorGrid(g₁, g₃)) == SVector{2,Float64} | |
67 @test eltype(TensorGrid(g₁, g₂, g₃)) == SVector{3,Float64} | |
68 @test eltype(TensorGrid(g₁, g₄)) == SVector{3,Float64} | |
69 @test eltype(TensorGrid(g₁, g₄, g₂)) == SVector{4,Float64} | |
70 | |
71 @test size(TensorGrid(g₁, g₂)) == (11,6) | |
72 @test size(TensorGrid(g₁, g₃)) == (11,10) | |
73 @test size(TensorGrid(g₁, g₂, g₃)) == (11,6,10) | |
74 @test size(TensorGrid(g₁, g₄)) == (11,) | |
75 @test size(TensorGrid(g₁, g₄, g₂)) == (11,6) | |
76 | |
77 @test Base.IteratorSize(TensorGrid(g₁, g₂)) == Base.HasShape{2}() | |
78 @test Base.IteratorSize(TensorGrid(g₁, g₃)) == Base.HasShape{2}() | |
79 @test Base.IteratorSize(TensorGrid(g₁, g₂, g₃)) == Base.HasShape{3}() | |
80 @test Base.IteratorSize(TensorGrid(g₁, g₄)) == Base.HasShape{1}() | |
81 @test Base.IteratorSize(TensorGrid(g₁, g₄, g₂)) == Base.HasShape{2}() | |
82 | |
83 @test iterate(TensorGrid(g₁, g₂))[1] isa SVector{2,Float64} | |
84 @test iterate(TensorGrid(g₁, g₃))[1] isa SVector{2,Float64} | |
85 @test iterate(TensorGrid(g₁, g₂, g₃))[1] isa SVector{3,Float64} | |
86 @test iterate(TensorGrid(g₁, g₄))[1] isa SVector{3,Float64} | |
87 @test iterate(TensorGrid(g₁, g₄, g₂))[1] isa SVector{4,Float64} | |
88 | |
89 @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] | |
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] | |
92 @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)] | |
94 end | |
95 | |
96 @testset "refine" begin | |
97 g1(n) = EquidistantGrid(range(0,1,length=n)) | |
98 g2(n) = EquidistantGrid(range(2,3,length=n)) | |
99 | |
100 @test refine(TensorGrid(g1(11), g2(6)),1) == TensorGrid(g1(11), g2(6)) | |
101 @test refine(TensorGrid(g1(11), g2(6)),2) == TensorGrid(g1(21), g2(11)) | |
102 @test refine(TensorGrid(g1(11), g2(6)),3) == TensorGrid(g1(31), g2(16)) | |
103 @test refine(TensorGrid(g1(11), g₄), 1) == TensorGrid(g1(11), g₄) | |
104 @test refine(TensorGrid(g1(11), g₄), 2) == TensorGrid(g1(21), g₄) | |
105 end | |
106 | |
107 @testset "coarsen" begin | |
108 g1(n) = EquidistantGrid(range(0,1,length=n)) | |
109 g2(n) = EquidistantGrid(range(2,3,length=n)) | |
110 | |
111 @test coarsen(TensorGrid(g1(11), g2(6)),1) == TensorGrid(g1(11), g2(6)) | |
112 @test coarsen(TensorGrid(g1(21), g2(11)),2) == TensorGrid(g1(11), g2(6)) | |
113 @test coarsen(TensorGrid(g1(31), g2(16)),3) == TensorGrid(g1(11), g2(6)) | |
114 @test coarsen(TensorGrid(g1(11), g₄), 1) == TensorGrid(g1(11), g₄) | |
115 @test coarsen(TensorGrid(g1(21), g₄), 2) == TensorGrid(g1(11), g₄) | |
116 end | |
117 | |
118 @testset "boundary_identifiers" begin | |
119 @test boundary_identifiers(TensorGrid(g₁, g₂)) == map((n,id)->TensorGridBoundary{n,id}(), (1,1,2,2), (Lower,Upper,Lower,Upper)) | |
120 @test boundary_identifiers(TensorGrid(g₁, g₄)) == (TensorGridBoundary{1,Lower}(),TensorGridBoundary{1,Upper}()) | |
121 end | |
122 | |
123 @testset "boundary_grid" begin | |
124 @test boundary_grid(TensorGrid(g₁, g₂), TensorGridBoundary{1, Upper}()) == TensorGrid(ZeroDimGrid(g₁[end]), g₂) | |
125 @test boundary_grid(TensorGrid(g₁, g₄), TensorGridBoundary{1, Upper}()) == TensorGrid(ZeroDimGrid(g₁[end]), g₄) | |
126 end | |
127 end | |
128 | |
129 @testset "combined_coordinate_vector_type" begin | |
130 @test Grids.combined_coordinate_vector_type(Float64) == Float64 | |
131 @test Grids.combined_coordinate_vector_type(Float64, Int) == SVector{2,Float64} | |
132 @test Grids.combined_coordinate_vector_type(Float32, Int16, Int32) == SVector{3,Float32} | |
133 | |
134 @test Grids.combined_coordinate_vector_type(SVector{2,Float64}) == SVector{2,Float64} | |
135 @test Grids.combined_coordinate_vector_type(SVector{2,Float64}, SVector{1,Float64}) == SVector{3,Float64} | |
136 @test Grids.combined_coordinate_vector_type(SVector{2,Float64}, SVector{1,Int}, SVector{3, Float32}) == SVector{6,Float64} | |
137 end | |
138 | |
139 @testset "combine_coordinates" begin | |
140 @test Grids.combine_coordinates(1,2,3) isa SVector{3, Int} | |
141 @test Grids.combine_coordinates(1,2,3) == [1,2,3] | |
142 @test Grids.combine_coordinates(1,2.,3) isa SVector{3, Float64} | |
143 @test Grids.combine_coordinates(1,2.,3) == [1,2,3] | |
144 @test Grids.combine_coordinates(1,@SVector[2.,3]) isa SVector{3, Float64} | |
145 @test Grids.combine_coordinates(1,@SVector[2.,3]) == [1,2,3] | |
146 end |