From 31560d87f17f8f0b84ba5b8be7f3080b8399b66f Mon Sep 17 00:00:00 2001
From: oca-agent <277152249+oca-agent@users.noreply.github.com>
Date: Thu, 28 May 2026 22:23:43 -0400
Subject: [PATCH] test: resolve non-deterministic assertion in regional
contrast saliency tests on Windows
---
.../Algorithms/Saliency/SaliencyTests.cs | 65 +++++++++++++++++--
1 file changed, 60 insertions(+), 5 deletions(-)
diff --git a/Leiter.Tests/Algorithms/Saliency/SaliencyTests.cs b/Leiter.Tests/Algorithms/Saliency/SaliencyTests.cs
index 6eeeec7..b6f8adf 100644
--- a/Leiter.Tests/Algorithms/Saliency/SaliencyTests.cs
+++ b/Leiter.Tests/Algorithms/Saliency/SaliencyTests.cs
@@ -1,4 +1,3 @@
-
using Xunit;
using Leiter.Core;
using Leiter.Pixels;
@@ -16,7 +15,7 @@ namespace Leiter.Tests.Algorithms.Saliency;
public class SaliencyTests
{
///
- /// Verifies that the global contrast saliency should compute correctly behaves correctly.
+ /// Verifies that the global contrast saliency should compute correctly.
///
[Fact]
public void GlobalContrastSaliency_ShouldComputeCorrectly()
@@ -54,7 +53,33 @@ public void GlobalContrastSaliency_ShouldComputeCorrectly()
}
///
- /// Verifies that the regional contrast saliency should compute correctly behaves correctly.
+ /// Verifies that global contrast saliency of a contrasting object is correctly computed.
+ ///
+ [Fact]
+ public void GlobalContrastSaliency_ContrastingObject_ShouldBeMostSalient()
+ {
+ // 10x10 image with a 4x4 red square in the center (16 pixels, 16%) and black background
+ var img = new SequentialMatrix(10, 10);
+ img.SetAll(new Rgb8(0, 0, 0));
+ for (int y = 3; y <= 6; y++)
+ {
+ for (int x = 3; x <= 6; x++)
+ {
+ img[x, y] = new Rgb8(255, 0, 0);
+ }
+ }
+
+ var saliency = GlobalContrastSaliency.ComputeSaliency(img, enableColorSpaceSmoothing: false);
+
+ // The contrasting red region should have highest saliency (1.0)
+ Assert.Equal(1.0, saliency[5, 5].Value, 4);
+
+ // The black background should have the lowest saliency (0.0)
+ Assert.Equal(0.0, saliency[0, 0].Value, 4);
+ }
+
+ ///
+ /// Verifies that the regional contrast saliency should compute correctly.
///
[Fact]
public void RegionalContrastSaliency_ShouldComputeCorrectly()
@@ -87,12 +112,42 @@ public void RegionalContrastSaliency_ShouldComputeCorrectly()
Assert.Equal(0.0, minS, 4);
Assert.Equal(1.0, maxS, 4);
- // Verify regional saliency is not all flat zeros/ones and varies by region
- Assert.True(saliency[0, 0].Value < saliency[10, 10].Value);
+ // Verify regional saliency contains various levels (i.e. not all flat zeros/ones)
+ Assert.True(saliency.Any(p => p.Value > 0.0 && p.Value < 1.0));
// Turn off smoothing and border calculation to test the alternate branches
var saliencyRaw = RegionalContrastSaliency.ComputeSaliency(img, segmentation, enableSmoothing: false, computeBorderRegions: false);
Assert.Equal(20, saliencyRaw.Width);
Assert.True(saliencyRaw.Min(p => p.Value) >= 0.0);
}
+
+ ///
+ /// Verifies that regional contrast saliency of a contrasting object is correctly computed.
+ ///
+ [Fact]
+ public void RegionalContrastSaliency_ContrastingObject_ShouldBeMostSalient()
+ {
+ // 12x12 image with a 4x4 red square in the center (16 pixels) and black background
+ var img = new SequentialMatrix(12, 12);
+ img.SetAll(new Rgb8(0, 0, 0));
+ for (int y = 4; y <= 7; y++)
+ {
+ for (int x = 4; x <= 7; x++)
+ {
+ img[x, y] = new Rgb8(255, 0, 0);
+ }
+ }
+
+ // Segment the image. With exact colors, the segment boundaries are completely clear.
+ var segmentation = EgbiSegmentation.Segment(img, kFactor: 1.0, epsilon: 1.0, minSegmentSize: 4);
+
+ // Compute regional saliency
+ var saliency = RegionalContrastSaliency.ComputeSaliency(img, segmentation, enableSmoothing: false, computeBorderRegions: false);
+
+ // The contrasting center red region should have highest saliency (1.0)
+ Assert.Equal(1.0, saliency[5, 5].Value, 4);
+
+ // The background black region should have the lowest saliency (0.0)
+ Assert.Equal(0.0, saliency[0, 0].Value, 4);
+ }
}