comparison test/Grids/equidistant_grid_test.jl @ 1858:4a9be96f2569 feature/documenter_logo

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Sun, 12 Jan 2025 21:18:44 +0100
parents 471a948cd2b2
children 03894fd7b132 871f3f1decea
comparison
equal deleted inserted replaced
1857:ffde7dad9da5 1858:4a9be96f2569
1 using Diffinitive.Grids
2 using Test
3 using Diffinitive.LazyTensors
4
5
6 @testset "EquidistantGrid" begin
7 @test EquidistantGrid(0:0.1:10) isa EquidistantGrid
8 @test EquidistantGrid(range(0,1,length=10)) isa EquidistantGrid
9 @test EquidistantGrid(LinRange(0,1,11)) isa EquidistantGrid
10
11 @testset "Indexing Interface" begin
12 g = EquidistantGrid(0:0.1:10)
13 @test g[1] == 0.0
14 @test g[5] == 0.4
15 @test g[101] == 10.0
16
17 @test g[begin] == 0.0
18 @test g[end] == 10.0
19
20 @test all(eachindex(g) .== 1:101)
21
22 @test firstindex(g) == 1
23 @test lastindex(g) == 101
24 end
25
26 @testset "Iterator interface" begin
27 @test eltype(EquidistantGrid(0:10)) == Int
28 @test eltype(EquidistantGrid(0:2:10)) == Int
29 @test eltype(EquidistantGrid(0:0.1:10)) == Float64
30 @test size(EquidistantGrid(0:10)) == (11,)
31 @test size(EquidistantGrid(0:0.1:10)) == (101,)
32
33 @test size(EquidistantGrid(0:0.1:10),1) == 101
34
35 @test collect(EquidistantGrid(0:0.1:0.5)) == [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]
36
37 @test Base.IteratorSize(EquidistantGrid{Float64, StepRange{Float64}}) == Base.HasShape{1}()
38 end
39
40 @testset "Base" begin
41 @test ndims(EquidistantGrid(0:10)) == 1
42
43 g = EquidistantGrid(0:0.1:10)
44 @test axes(g,1) == 1:101
45 @test axes(g) == (1:101,)
46 end
47
48 @testset "spacing" begin
49 @test spacing(EquidistantGrid(0:10)) == 1
50 @test spacing(EquidistantGrid(0:0.1:10)) == 0.1
51 end
52
53 @testset "inverse_spacing" begin
54 @test inverse_spacing(EquidistantGrid(0:10)) == 1
55 @test inverse_spacing(EquidistantGrid(0:0.1:10)) == 10
56 end
57
58 @testset "min_spacing" begin
59 @test min_spacing(EquidistantGrid(0:10)) == 1
60 @test min_spacing(EquidistantGrid(0:0.1:10)) == 0.1
61 end
62
63 @testset "boundary_identifiers" begin
64 g = EquidistantGrid(0:0.1:10)
65 @test boundary_identifiers(g) == (LowerBoundary(), UpperBoundary())
66 @inferred boundary_identifiers(g)
67 end
68
69 @testset "boundary_grid" begin
70 g = EquidistantGrid(0:0.1:1)
71 @test boundary_grid(g, LowerBoundary()) == ZeroDimGrid(0.0)
72 @test boundary_grid(g, UpperBoundary()) == ZeroDimGrid(1.0)
73 end
74
75 @testset "boundary_indices" begin
76 g = EquidistantGrid(0:0.1:1)
77 @test boundary_indices(g, LowerBoundary()) == (1,)
78 @test boundary_indices(g, UpperBoundary()) == (11,)
79
80 g = EquidistantGrid(2:0.1:10)
81 @test boundary_indices(g, LowerBoundary()) == (1,)
82 @test boundary_indices(g, UpperBoundary()) == (81,)
83
84 end
85
86 @testset "refine" begin
87 g = EquidistantGrid(0:0.1:1)
88 @test refine(g, 1) == g
89 @test refine(g, 2) == EquidistantGrid(0:0.05:1)
90 @test refine(g, 3) == EquidistantGrid(0:(0.1/3):1)
91 end
92
93 @testset "coarsen" begin
94 g = EquidistantGrid(0:1:10)
95 @test coarsen(g, 1) == g
96 @test coarsen(g, 2) == EquidistantGrid(0:2:10)
97
98 g = EquidistantGrid(0:0.1:1)
99 @test coarsen(g, 1) == g
100 @test coarsen(g, 2) == EquidistantGrid(0:0.2:1)
101
102 g = EquidistantGrid(0:10)
103 @test coarsen(g, 1) == EquidistantGrid(0:1:10)
104 @test coarsen(g, 2) == EquidistantGrid(0:2:10)
105
106 @test_throws DomainError(3, "Size minus 1 must be divisible by the ratio.") coarsen(g, 3)
107 end
108 end
109
110
111 @testset "equidistant_grid" begin
112 @test equidistant_grid(0.0,1.0, 4) isa EquidistantGrid
113 @test equidistant_grid((0.0,0.0),(8.0,5.0), 4, 3) isa TensorGrid
114 @test equidistant_grid((0.0,),(8.0,), 4) isa TensorGrid
115
116 # constuctor
117 @test_throws DomainError equidistant_grid(0.0, 1.0, 0)
118 @test_throws DomainError equidistant_grid(1.0, 1.0, 1)
119 @test_throws DomainError equidistant_grid(1.0, -1.0, 1)
120
121 @test_throws DomainError equidistant_grid((0.0,0.0),(1.0,1.0), 0, 0)
122 @test_throws DomainError equidistant_grid((1.0,1.0),(1.0,1.0), 1, 1)
123 @test_throws DomainError equidistant_grid((1.0,1.0),(-1.0,-1.0), 1, 1)
124
125 @test_throws ArgumentError equidistant_grid((0.0,),(8.0,5.0), 4, 3, 4)
126
127 @testset "Base" begin
128 @test eltype(equidistant_grid(0.0, 1.0, 4)) == Float64
129 @test eltype(equidistant_grid((0,0),(1,3), 4, 3)) <: AbstractVector{Float64}
130
131 @test size(equidistant_grid(0.0, 1.0, 4)) == (4,)
132 @test size(equidistant_grid((0.0,0.0), (2.0,1.0), 5, 3)) == (5,3)
133
134 @test size(equidistant_grid((0.0,0.0), (2.0,1.0), 5, 3), 1) == 5
135 @test size(equidistant_grid((0.0,0.0), (2.0,1.0), 5, 3), 2) == 3
136
137 @test ndims(equidistant_grid(0.0, 1.0, 4)) == 1
138 @test ndims(equidistant_grid((0.0,0.0), (2.0,1.0), 5, 3)) == 2
139 end
140
141 @testset "getindex" begin
142 g = equidistant_grid((-1.0,0.0), (0.0,7.11), 5, 3)
143 gp = collect(g);
144 p = [(-1.,0.) (-1.,7.11/2) (-1.,7.11);
145 (-0.75,0.) (-0.75,7.11/2) (-0.75,7.11);
146 (-0.5,0.) (-0.5,7.11/2) (-0.5,7.11);
147 (-0.25,0.) (-0.25,7.11/2) (-0.25,7.11);
148 (0.,0.) (0.,7.11/2) (0.,7.11)]
149 for i ∈ eachindex(gp)
150 @test [gp[i]...] ≈ [p[i]...] atol=5e-13
151 end
152 end
153 end
154
155
156 @testset "change_length" begin
157 @test Grids.change_length(0:20, 21) == 0:20
158 @test Grids.change_length(0:20, 11) == 0:2:20
159 @test Grids.change_length(0:2:20, 21) == 0:20
160
161 @test Grids.change_length(range(0,1,length=10), 10) == range(0,1,length=10)
162 @test Grids.change_length(range(0,1,length=10), 5) == range(0,1,length=5)
163 @test Grids.change_length(range(0,1,length=10), 20) == range(0,1,length=20)
164
165 @test Grids.change_length(LinRange(1,2,10),10) == LinRange(1,2,10)
166 @test Grids.change_length(LinRange(1,2,10),15) == LinRange(1,2,15)
167 end