Skip to content

Commit e1ba371

Browse files
Add checkbounds tests for empty fields
1 parent 632eef3 commit e1ba371

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

test/Fields/field.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,46 @@ end
9494
@test axes(point_field) isa Spaces.PointSpace
9595
end
9696

97+
# Requires `--check-bounds=yes`
98+
@testset "Constructing & broadcasting over empty fields" begin
99+
FT = Float32
100+
for space in TU.all_spaces(FT)
101+
f = TU.FieldFromNamedTuple(space, (;))
102+
@. f += f
103+
end
104+
105+
function test_broken_throws(f)
106+
try
107+
@. f += 1
108+
# we want to throw exception, test is broken
109+
@test_broken false
110+
catch
111+
# we want to throw exception, unexpected pass
112+
@test_broken true
113+
end
114+
end
115+
empty_field(space) = TU.FieldFromNamedTuple(space, (;))
116+
117+
# Broadcasting over the wrong size should error
118+
test_broken_throws(empty_field(TU.PointSpace(FT)))
119+
test_broken_throws(empty_field(TU.SpectralElementSpace1D(FT)))
120+
test_broken_throws(empty_field(TU.SpectralElementSpace2D(FT)))
121+
test_broken_throws(empty_field(TU.ColumnCenterFiniteDifferenceSpace(FT)))
122+
test_broken_throws(empty_field(TU.ColumnFaceFiniteDifferenceSpace(FT)))
123+
test_broken_throws(empty_field(TU.SphereSpectralElementSpace(FT)))
124+
test_broken_throws(empty_field(TU.CenterExtrudedFiniteDifferenceSpace(FT)))
125+
test_broken_throws(empty_field(TU.FaceExtrudedFiniteDifferenceSpace(FT)))
126+
127+
# TODO: performance optimization: shouldn't we do
128+
# nothing when broadcasting over empty fields?
129+
# This is otherwise a performance penalty if
130+
# users regularly rely on empty fields. In particular:
131+
# - does iterating over empty fields load data?
132+
# - what is the overhead in iterating over empty fields?
133+
# - what is the use case of anything useful that can be
134+
# done by iterating over empty fields?
135+
end
136+
97137
@testset "Broadcasting interception for tuple-valued fields" begin
98138
n1 = n2 = 1
99139
Nij = 4

0 commit comments

Comments
 (0)