From af7eaddf3de8fdcc2f52a66d97c0aba65e4c3702 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 27 Oct 2022 20:06:52 +1000 Subject: [PATCH] Update PixelSamplingStrategyTests.cs --- .../PixelSamplingStrategyTests.cs | 76 ++++++++++++++----- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/tests/ImageSharp.Tests/Quantization/PixelSamplingStrategyTests.cs b/tests/ImageSharp.Tests/Quantization/PixelSamplingStrategyTests.cs index f42726412..17c60d4d7 100644 --- a/tests/ImageSharp.Tests/Quantization/PixelSamplingStrategyTests.cs +++ b/tests/ImageSharp.Tests/Quantization/PixelSamplingStrategyTests.cs @@ -10,24 +10,36 @@ namespace SixLabors.ImageSharp.Tests.Quantization; public class PixelSamplingStrategyTests { - public static readonly TheoryData DefaultPixelSamplingStrategy_Data = new TheoryData() - { - { 100, 100, 1, 10000 }, - { 100, 100, 1, 5000 }, - { 100, 100, 10, 50000 }, - { 99, 100, 11, 30000 }, - { 97, 99, 11, 80000 }, - { 99, 100, 11, 20000 }, - { 99, 501, 20, 100000 }, - { 97, 500, 20, 10000 }, - { 103, 501, 20, 1000 }, - }; + public static readonly TheoryData DefaultPixelSamplingStrategy_MultiFrame_Data = new() + { + { 100, 100, 1, 10000 }, + { 100, 100, 1, 5000 }, + { 100, 100, 10, 50000 }, + { 99, 100, 11, 30000 }, + { 97, 99, 11, 80000 }, + { 99, 100, 11, 20000 }, + { 99, 501, 20, 100000 }, + { 97, 500, 20, 10000 }, + { 103, 501, 20, 1000 }, + }; + + public static readonly TheoryData DefaultPixelSamplingStrategy_Data = new() + { + { 100, 100, 9900 }, + { 100, 100, 5000 }, + { 99, 100, 30000 }, + { 97, 99, 80000 }, + { 99, 100, 20000 }, + { 99, 501, 100000 }, + { 97, 500, 10000 }, + { 103, 501, 1000 }, + }; [Fact] public void ExtensivePixelSamplingStrategy_EnumeratesAll() { using Image image = CreateTestImage(100, 100, 100); - var strategy = new ExtensivePixelSamplingStrategy(); + ExtensivePixelSamplingStrategy strategy = new(); foreach (Buffer2DRegion region in strategy.EnumeratePixelRegions(image)) { @@ -40,12 +52,12 @@ public class PixelSamplingStrategyTests } [Theory] - [WithBlankImages(nameof(DefaultPixelSamplingStrategy_Data), 1, 1, PixelTypes.L8)] - public void DefaultPixelSamplingStrategy_IsFair(TestImageProvider dummyProvider, int width, int height, int noOfFrames, int maximumNumberOfPixels) + [WithBlankImages(nameof(DefaultPixelSamplingStrategy_MultiFrame_Data), 1, 1, PixelTypes.L8)] + public void DefaultPixelSamplingStrategy_IsFair_MultiFrame(TestImageProvider dummyProvider, int width, int height, int noOfFrames, int maximumNumberOfPixels) { using Image image = CreateTestImage(width, height, noOfFrames); - var strategy = new DefaultPixelSamplingStrategy(maximumNumberOfPixels, 0.1); + DefaultPixelSamplingStrategy strategy = new(maximumNumberOfPixels, 0.1); long visitedPixels = 0; foreach (Buffer2DRegion region in strategy.EnumeratePixelRegions(image)) @@ -67,9 +79,37 @@ public class PixelSamplingStrategyTests Assert.True(visitRatio <= 1.1, $"{visitedPixels}>{maximumPixels}"); } + [Theory] + [WithBlankImages(nameof(DefaultPixelSamplingStrategy_Data), 1, 1, PixelTypes.L8)] + public void DefaultPixelSamplingStrategy_IsFair(TestImageProvider dummyProvider, int width, int height, int maximumNumberOfPixels) + { + using Image image = CreateTestImage(width, height, 1); + + DefaultPixelSamplingStrategy strategy = new(maximumNumberOfPixels, 0.1); + + long visitedPixels = 0; + foreach (Buffer2DRegion region in strategy.EnumeratePixelRegions(image)) + { + PaintWhite(region); + visitedPixels += region.Width * region.Height; + } + + image.DebugSave( + dummyProvider, + $"W{width}_H{height}_maximumNumberOfPixels_{maximumNumberOfPixels}", + appendPixelTypeToFileName: false); + + int maximumPixels = image.Width * image.Height * image.Frames.Count / 10; + maximumPixels = Math.Max(maximumPixels, (int)strategy.MaximumPixels); + + // allow some inaccuracy: + double visitRatio = visitedPixels / (double)maximumPixels; + Assert.True(visitRatio <= 1.1, $"{visitedPixels}>{maximumPixels}"); + } + private static void PaintWhite(Buffer2DRegion region) { - var white = new L8(255); + L8 white = new(255); for (int y = 0; y < region.Height; y++) { region.DangerousGetRowSpan(y).Fill(white); @@ -79,7 +119,7 @@ public class PixelSamplingStrategyTests private static Image CreateTestImage(int width, int height, int noOfFrames, bool paintWhite = false) { L8 bg = paintWhite ? new L8(255) : default; - var image = new Image(width, height, bg); + Image image = new(width, height, bg); for (int i = 1; i < noOfFrames; i++) {