Browse Source

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
pull/22816/head
SERAG AHMED 1 week ago
committed by GitHub
parent
commit
ab36f90feb
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 10
      src/Avalonia.Controls/Grid.cs
  2. 26
      tests/Avalonia.Controls.UnitTests/GridTests.cs

10
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
{

26
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()
{

Loading…
Cancel
Save