From ab36f90feb89dfa53cbde45839b77fd0d44d608c Mon Sep 17 00:00:00 2001 From: SERAG AHMED Date: Fri, 13 Nov 2026 13:08:43 +0000 Subject: [PATCH] Allow a rounding residual in the Grid span distribution assert (#22701) * Add a failing test for the Grid span distribution assert In Debug builds, measuring star rows with RowSpacing and a spanned child fails the sanity check in EnsureMinSizeInDefinitionRange on a rounding residual (#22677). * Allow a rounding residual in the Grid span distribution assert After distributing a spanned child's size into its definitions, EnsureMinSizeInDefinitionRange asserted that nothing was left with MathUtilities.IsZero, whose fixed threshold is about 2.2e-15. At layout sizes in the hundreds, the floating-point residual can be larger (about 2.8e-14 in the reported case), so Debug builds failed the assert. The sanity check now compares the distributed size with the requested size using MathUtilities.AreClose, whose tolerance scales with the values and is never stricter than IsZero. Release builds are unaffected. Fixes #22677 --- src/Avalonia.Controls/Grid.cs | 10 ++++--- .../Avalonia.Controls.UnitTests/GridTests.cs | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Avalonia.Controls/Grid.cs b/src/Avalonia.Controls/Grid.cs index 2e15b07463..a93a2324b2 100644 --- a/src/Avalonia.Controls/Grid.cs +++ b/src/Avalonia.Controls/Grid.cs @@ -1335,8 +1335,9 @@ namespace Avalonia.Controls sizeToDistribute -= newMinSize; } - // sanity check: requested size must all be distributed - Debug.Assert(MathUtilities.IsZero(sizeToDistribute)); + // sanity check: requested size must all be distributed, up to a rounding residual + // relative to the requested size + Debug.Assert(MathUtilities.AreClose(requestedSize - sizeToDistribute, requestedSize)); } else if (requestedSize <= rangeMaxSize) { @@ -1379,8 +1380,9 @@ namespace Avalonia.Controls sizeToDistribute -= (tempDefinition.MinSize - preferredSize); } - // sanity check: requested size must all be distributed - Debug.Assert(MathUtilities.IsZero(sizeToDistribute)); + // sanity check: requested size must all be distributed, up to a rounding residual + // relative to the requested size + Debug.Assert(MathUtilities.AreClose(requestedSize - sizeToDistribute, requestedSize)); } else { diff --git a/tests/Avalonia.Controls.UnitTests/GridTests.cs b/tests/Avalonia.Controls.UnitTests/GridTests.cs index 3a9a8cdaee..ea685aa34c 100644 --- a/tests/Avalonia.Controls.UnitTests/GridTests.cs +++ b/tests/Avalonia.Controls.UnitTests/GridTests.cs @@ -2121,6 +2121,32 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(new Rect(0, 0, 60, 100), target.Children[0].Bounds); } + [Fact] + public void Grid_Controls_With_Spacing_With_Span_Allows_A_Rounding_Residual() + { + // Distributing the spanned height into the three star rows leaves a residual of + // about 2.8e-14, which failed the Debug sanity check in EnsureMinSizeInDefinitionRange. + var target = new Grid + { + RowSpacing = 12, + RowDefinitions = RowDefinitions.Parse("*,*,*,*,*,*,*"), + Children = + { + new Border + { + Height = 540.8571428571429, + [Grid.RowProperty] = 4, + [Grid.RowSpanProperty] = 3 + } + }, + }; + + target.Measure(new Size(1000, 1278)); + + // The child's height plus the four spacings outside its span, layout rounded + Assert.Equal(589, target.DesiredSize.Height); + } + [Fact] public void Grid_Controls_With_Spacing_With_Span_And_SharedSize() {