From 5f82fa7766f7960c664aebd2d921ce0e3a5ab16e Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Fri, 23 Aug 2024 11:26:53 +0100 Subject: [PATCH] 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 --- .zenodo.json | 5 +++++ src/layouts.jl | 19 +++++++------------ test/test_layouts.jl | 11 +++++++++++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/.zenodo.json b/.zenodo.json index 539036d03..42fbc2c8a 100644 --- a/.zenodo.json +++ b/.zenodo.json @@ -770,6 +770,11 @@ "name": "Syver Døving Agdestein", "orcid": "0000-0002-1589-2916", "type": "Other" + }, + { + "affiliation": "The Alan Turing Institute", + "name": "Penelope Yong", + "type": "Other" } ], "upload_type": "software" diff --git a/src/layouts.jl b/src/layouts.jl index 2eed33558..8c98ee165 100644 --- a/src/layouts.jl +++ b/src/layouts.jl @@ -216,23 +216,18 @@ function GridLayout( kw..., ) # Check the values for heights and widths if values are provided + all_between_one(xs) = all(x -> 0 < x < 1, xs) if heights !== nothing - if sum(heights) != 1 - error("The sum of heights must be 1!") - end - if all(x -> 0 < x < 1, heights) == false - error("Values for heights must be in the range (0, 1)!") - end + sum(heights) ≈ 1 || error("The heights provided ($(heights)) must sum to 1.") + all_between_one(heights) || + error("The heights provided ($(heights)) must be in the range (0, 1).") else heights = zeros(dims[1]) end if widths !== nothing - if sum(widths) != 1 - error("The sum of widths must be 1!") - end - if all(x -> 0 < x < 1, widths) == false - error("Values for widths must be in the range (0, 1)!") - end + sum(widths) ≈ 1 || error("The widths provided ($(widths)) must sum to 1.") + all_between_one(widths) || + error("The widths provided ($(widths)) must be in the range (0, 1).") else widths = zeros(dims[2]) end diff --git a/test/test_layouts.jl b/test/test_layouts.jl index 182720e92..952da4f06 100644 --- a/test/test_layouts.jl +++ b/test/test_layouts.jl @@ -65,6 +65,17 @@ end @test_throws ErrorException plot(map(_ -> plot(1:2), 1:5)...; layout = grid(2, 2)) end +@testset "Allowed grid widths/heights" begin + @test_nowarn grid(2, 1, heights = [0.5, 0.5]) + @test_nowarn grid(4, 1, heights = [0.3, 0.3, 0.3, 0.1]) + @test_nowarn grid(1, 2, widths = [0.5, 0.5]) + @test_nowarn grid(1, 4, widths = [0.3, 0.3, 0.3, 0.1]) + @test_throws ErrorException grid(2, 1, heights = [0.5, 0.4]) + @test_throws ErrorException grid(4, 1, heights = [1.5, -0.5]) + @test_throws ErrorException grid(1, 2, widths = [0.5, 0.4]) + @test_throws ErrorException grid(1, 4, widths = [1.5, -0.5]) +end + @testset "Invalid viewport" begin # github.com/JuliaPlots/Plots.jl/issues/2804 pl = plot(1, layout = (10, 2))