Skip to content

Commit 5f82fa7

Browse files
authored
Fix floating point equality comparison in layouts (#4972)
* Fix floating point equality comparison in layouts * Add tests for grid layout widths/heights * Add myself to .zenodo.json
1 parent 8b9bcf2 commit 5f82fa7

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

.zenodo.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,11 @@
770770
"name": "Syver Døving Agdestein",
771771
"orcid": "0000-0002-1589-2916",
772772
"type": "Other"
773+
},
774+
{
775+
"affiliation": "The Alan Turing Institute",
776+
"name": "Penelope Yong",
777+
"type": "Other"
773778
}
774779
],
775780
"upload_type": "software"

src/layouts.jl

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -216,23 +216,18 @@ function GridLayout(
216216
kw...,
217217
)
218218
# Check the values for heights and widths if values are provided
219+
all_between_one(xs) = all(x -> 0 < x < 1, xs)
219220
if heights !== nothing
220-
if sum(heights) != 1
221-
error("The sum of heights must be 1!")
222-
end
223-
if all(x -> 0 < x < 1, heights) == false
224-
error("Values for heights must be in the range (0, 1)!")
225-
end
221+
sum(heights) 1 || error("The heights provided ($(heights)) must sum to 1.")
222+
all_between_one(heights) ||
223+
error("The heights provided ($(heights)) must be in the range (0, 1).")
226224
else
227225
heights = zeros(dims[1])
228226
end
229227
if widths !== nothing
230-
if sum(widths) != 1
231-
error("The sum of widths must be 1!")
232-
end
233-
if all(x -> 0 < x < 1, widths) == false
234-
error("Values for widths must be in the range (0, 1)!")
235-
end
228+
sum(widths) 1 || error("The widths provided ($(widths)) must sum to 1.")
229+
all_between_one(widths) ||
230+
error("The widths provided ($(widths)) must be in the range (0, 1).")
236231
else
237232
widths = zeros(dims[2])
238233
end

test/test_layouts.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ end
6565
@test_throws ErrorException plot(map(_ -> plot(1:2), 1:5)...; layout = grid(2, 2))
6666
end
6767

68+
@testset "Allowed grid widths/heights" begin
69+
@test_nowarn grid(2, 1, heights = [0.5, 0.5])
70+
@test_nowarn grid(4, 1, heights = [0.3, 0.3, 0.3, 0.1])
71+
@test_nowarn grid(1, 2, widths = [0.5, 0.5])
72+
@test_nowarn grid(1, 4, widths = [0.3, 0.3, 0.3, 0.1])
73+
@test_throws ErrorException grid(2, 1, heights = [0.5, 0.4])
74+
@test_throws ErrorException grid(4, 1, heights = [1.5, -0.5])
75+
@test_throws ErrorException grid(1, 2, widths = [0.5, 0.4])
76+
@test_throws ErrorException grid(1, 4, widths = [1.5, -0.5])
77+
end
78+
6879
@testset "Invalid viewport" begin
6980
# github.com/JuliaPlots/Plots.jl/issues/2804
7081
pl = plot(1, layout = (10, 2))

0 commit comments

Comments
 (0)