comparison test/Grids/equidistant_grid_test.jl @ 1854:654a2b7e6824 tooling/benchmarks

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Sat, 11 Jan 2025 10:19:47 +0100
parents 471a948cd2b2
children 03894fd7b132 871f3f1decea
comparison
equal deleted inserted replaced
1378:2b5480e2d4bf 1854:654a2b7e6824
1 using Sbplib.Grids 1 using Diffinitive.Grids
2 using Test 2 using Test
3 using Sbplib.RegionIndices 3 using Diffinitive.LazyTensors
4 using Sbplib.LazyTensors
5 4
6 5
7 @testset "EquidistantGrid" begin 6 @testset "EquidistantGrid" begin
8 @test EquidistantGrid(0:0.1:10) isa EquidistantGrid 7 @test EquidistantGrid(0:0.1:10) isa EquidistantGrid
9 @test EquidistantGrid(range(0,1,length=10)) isa EquidistantGrid 8 @test EquidistantGrid(range(0,1,length=10)) isa EquidistantGrid
17 16
18 @test g[begin] == 0.0 17 @test g[begin] == 0.0
19 @test g[end] == 10.0 18 @test g[end] == 10.0
20 19
21 @test all(eachindex(g) .== 1:101) 20 @test all(eachindex(g) .== 1:101)
21
22 @test firstindex(g) == 1
23 @test lastindex(g) == 101
22 end 24 end
23 25
24 @testset "Iterator interface" begin 26 @testset "Iterator interface" begin
25 @test eltype(EquidistantGrid(0:10)) == Int 27 @test eltype(EquidistantGrid(0:10)) == Int
26 @test eltype(EquidistantGrid(0:2:10)) == Int 28 @test eltype(EquidistantGrid(0:2:10)) == Int
27 @test eltype(EquidistantGrid(0:0.1:10)) == Float64 29 @test eltype(EquidistantGrid(0:0.1:10)) == Float64
28 @test size(EquidistantGrid(0:10)) == (11,) 30 @test size(EquidistantGrid(0:10)) == (11,)
29 @test size(EquidistantGrid(0:0.1:10)) == (101,) 31 @test size(EquidistantGrid(0:0.1:10)) == (101,)
30 32
33 @test size(EquidistantGrid(0:0.1:10),1) == 101
34
31 @test collect(EquidistantGrid(0:0.1:0.5)) == [0.0, 0.1, 0.2, 0.3, 0.4, 0.5] 35 @test collect(EquidistantGrid(0:0.1:0.5)) == [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]
32 36
33 @test Base.IteratorSize(EquidistantGrid{Float64, StepRange{Float64}}) == Base.HasShape{1}() 37 @test Base.IteratorSize(EquidistantGrid{Float64, StepRange{Float64}}) == Base.HasShape{1}()
34 end 38 end
35 39
36 @testset "Base" begin 40 @testset "Base" begin
37 @test ndims(EquidistantGrid(0:10)) == 1 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,)
38 end 46 end
39 47
40 @testset "spacing" begin 48 @testset "spacing" begin
41 @test spacing(EquidistantGrid(0:10)) == 1 49 @test spacing(EquidistantGrid(0:10)) == 1
42 @test spacing(EquidistantGrid(0:0.1:10)) == 0.1 50 @test spacing(EquidistantGrid(0:0.1:10)) == 0.1
45 @testset "inverse_spacing" begin 53 @testset "inverse_spacing" begin
46 @test inverse_spacing(EquidistantGrid(0:10)) == 1 54 @test inverse_spacing(EquidistantGrid(0:10)) == 1
47 @test inverse_spacing(EquidistantGrid(0:0.1:10)) == 10 55 @test inverse_spacing(EquidistantGrid(0:0.1:10)) == 10
48 end 56 end
49 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
50 @testset "boundary_identifiers" begin 63 @testset "boundary_identifiers" begin
51 g = EquidistantGrid(0:0.1:10) 64 g = EquidistantGrid(0:0.1:10)
52 @test boundary_identifiers(g) == (Lower(), Upper()) 65 @test boundary_identifiers(g) == (LowerBoundary(), UpperBoundary())
53 @inferred boundary_identifiers(g) 66 @inferred boundary_identifiers(g)
54 end 67 end
55 68
56 @testset "boundary_grid" begin 69 @testset "boundary_grid" begin
57 g = EquidistantGrid(0:0.1:1) 70 g = EquidistantGrid(0:0.1:1)
58 @test boundary_grid(g, Lower()) == ZeroDimGrid(0.0) 71 @test boundary_grid(g, LowerBoundary()) == ZeroDimGrid(0.0)
59 @test boundary_grid(g, Upper()) == ZeroDimGrid(1.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
60 end 84 end
61 85
62 @testset "refine" begin 86 @testset "refine" begin
63 g = EquidistantGrid(0:0.1:1) 87 g = EquidistantGrid(0:0.1:1)
64 @test refine(g, 1) == g 88 @test refine(g, 1) == g
83 end 107 end
84 end 108 end
85 109
86 110
87 @testset "equidistant_grid" begin 111 @testset "equidistant_grid" begin
88 @test equidistant_grid(4,0.0,1.0) isa EquidistantGrid 112 @test equidistant_grid(0.0,1.0, 4) isa EquidistantGrid
89 @test equidistant_grid((4,3),(0.0,0.0),(8.0,5.0)) isa TensorGrid 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
90 115
91 # constuctor 116 # constuctor
92 @test_throws DomainError equidistant_grid(0,0.0,1.0) 117 @test_throws DomainError equidistant_grid(0.0, 1.0, 0)
93 @test_throws DomainError equidistant_grid(1,1.0,1.0) 118 @test_throws DomainError equidistant_grid(1.0, 1.0, 1)
94 @test_throws DomainError equidistant_grid(1,1.0,-1.0) 119 @test_throws DomainError equidistant_grid(1.0, -1.0, 1)
95 120
96 @test_throws DomainError equidistant_grid((0,0),(0.0,0.0),(1.0,1.0)) 121 @test_throws DomainError equidistant_grid((0.0,0.0),(1.0,1.0), 0, 0)
97 @test_throws DomainError equidistant_grid((1,1),(1.0,1.0),(1.0,1.0)) 122 @test_throws DomainError equidistant_grid((1.0,1.0),(1.0,1.0), 1, 1)
98 @test_throws DomainError equidistant_grid((1,1),(1.0,1.0),(-1.0,-1.0)) 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)
99 126
100 @testset "Base" begin 127 @testset "Base" begin
101 @test eltype(equidistant_grid(4,0.0,1.0)) == Float64 128 @test eltype(equidistant_grid(0.0, 1.0, 4)) == Float64
102 @test eltype(equidistant_grid((4,3),(0,0),(1,3))) <: AbstractVector{Float64} 129 @test eltype(equidistant_grid((0,0),(1,3), 4, 3)) <: AbstractVector{Float64}
103 @test size(equidistant_grid(4,0.0,1.0)) == (4,) 130
104 @test size(equidistant_grid((5,3), (0.0,0.0), (2.0,1.0))) == (5,3) 131 @test size(equidistant_grid(0.0, 1.0, 4)) == (4,)
105 @test ndims(equidistant_grid(4,0.0,1.0)) == 1 132 @test size(equidistant_grid((0.0,0.0), (2.0,1.0), 5, 3)) == (5,3)
106 @test ndims(equidistant_grid((5,3), (0.0,0.0), (2.0,1.0))) == 2 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
107 end 139 end
108 140
109 @testset "getindex" begin 141 @testset "getindex" begin
110 g = equidistant_grid((5,3), (-1.0,0.0), (0.0,7.11)) 142 g = equidistant_grid((-1.0,0.0), (0.0,7.11), 5, 3)
111 gp = collect(g); 143 gp = collect(g);
112 p = [(-1.,0.) (-1.,7.11/2) (-1.,7.11); 144 p = [(-1.,0.) (-1.,7.11/2) (-1.,7.11);
113 (-0.75,0.) (-0.75,7.11/2) (-0.75,7.11); 145 (-0.75,0.) (-0.75,7.11/2) (-0.75,7.11);
114 (-0.5,0.) (-0.5,7.11/2) (-0.5,7.11); 146 (-0.5,0.) (-0.5,7.11/2) (-0.5,7.11);
115 (-0.25,0.) (-0.25,7.11/2) (-0.25,7.11); 147 (-0.25,0.) (-0.25,7.11/2) (-0.25,7.11);