From 85c2d2a3799680d41dfce9baea7fd37220739ec4 Mon Sep 17 00:00:00 2001 From: Florian Levis Date: Fri, 1 Mar 2024 16:12:45 +0100 Subject: [PATCH 1/2] feat: make Percentage always positive --- src/Magick.NET.Core/Types/Percentage.cs | 24 ++++++++-- src/Magick.NET/MagickImage.cs | 45 +++---------------- src/Magick.NET/Types/MagickGeometry.cs | 14 +----- src/Shared/Throw.cs | 6 +++ .../Types/PercentageTests/TheConstructor.cs | 17 ++++--- .../TheCreateMethod.cs | 16 ------- .../MagickImageTests/TheColorizeMethod.cs | 32 ------------- .../TheContrastStretchMethod.cs | 24 ---------- .../MagickImageTests/TheDeskewMethod.cs | 20 --------- .../TheInterpolativeResizeMethod.cs | 21 --------- .../TheLiquidRescaleMethod.cs | 42 ----------------- .../TheRangeThresholdMethod.cs | 32 ------------- .../MagickImageTests/TheResizeMethod.cs | 29 ------------ .../MagickImageTests/TheSampleMethod.cs | 29 ------------ .../MagickImageTests/TheScaleMethod.cs | 29 ------------ .../MagickImageTests/TheThumbnailMethod.cs | 26 ----------- .../TheLimitMemoryMethod.cs | 6 --- 17 files changed, 43 insertions(+), 369 deletions(-) diff --git a/src/Magick.NET.Core/Types/Percentage.cs b/src/Magick.NET.Core/Types/Percentage.cs index 1d840f7ca4..99051e30f6 100644 --- a/src/Magick.NET.Core/Types/Percentage.cs +++ b/src/Magick.NET.Core/Types/Percentage.cs @@ -18,14 +18,22 @@ namespace ImageMagick; /// /// The value (0% = 0.0, 100% = 100.0). public Percentage(double value) - => _value = value; + { + Throw.IfNegative(nameof(value), value); + + _value = value; + } /// /// Initializes a new instance of the struct. /// /// The value (0% = 0, 100% = 100). public Percentage(int value) - => _value = value; + { + Throw.IfNegative(nameof(value), value); + + _value = value; + } /// /// Converts the specified double to an instance of this type. @@ -170,7 +178,11 @@ public override int GetHashCode() /// The value to use. /// The new value. public double Multiply(double value) - => (value * _value) / 100.0; + { + Throw.IfNegative(nameof(value), value); + + return (value * _value) / 100.0; + } /// /// Multiplies the value by the specified percentage. @@ -178,7 +190,11 @@ public double Multiply(double value) /// The value to use. /// The new value. public int Multiply(int value) - => (int)((value * _value) / 100.0); + { + Throw.IfNegative(nameof(value), value); + + return (int)((value * _value) / 100.0); + } /// /// Returns a double that represents the current percentage. diff --git a/src/Magick.NET/MagickImage.cs b/src/Magick.NET/MagickImage.cs index e873129de6..7fac556835 100644 --- a/src/Magick.NET/MagickImage.cs +++ b/src/Magick.NET/MagickImage.cs @@ -1358,11 +1358,7 @@ public void BlackThreshold(Percentage threshold) /// The channel(s) to make black. /// Thrown when an error is raised by ImageMagick. public void BlackThreshold(Percentage threshold, Channels channels) - { - Throw.IfNegative(nameof(threshold), threshold); - - _nativeInstance.BlackThreshold(threshold.ToString(), channels); - } + => _nativeInstance.BlackThreshold(threshold.ToString(), channels); /// /// Simulate a scene at nighttime in the moonlight. @@ -1736,11 +1732,7 @@ public void ColorDecisionList(string fileName) /// The alpha percentage. /// Thrown when an error is raised by ImageMagick. public void Colorize(IMagickColor color, Percentage alpha) - { - Throw.IfNegative(nameof(alpha), alpha); - - Colorize(color, alpha, alpha, alpha); - } + => Colorize(color, alpha, alpha, alpha); /// /// Colorize image with the specified color, using specified percent alpha for red, green, @@ -1754,9 +1746,6 @@ public void Colorize(IMagickColor color, Percentage alpha) public void Colorize(IMagickColor color, Percentage alphaRed, Percentage alphaGreen, Percentage alphaBlue) { Throw.IfNull(nameof(color), color); - Throw.IfNegative(nameof(alphaRed), alphaRed); - Throw.IfNegative(nameof(alphaGreen), alphaGreen); - Throw.IfNegative(nameof(alphaBlue), alphaBlue); var blend = string.Format(CultureInfo.InvariantCulture, "{0}/{1}/{2}", alphaRed.ToInt32(), alphaGreen.ToInt32(), alphaBlue.ToInt32()); @@ -2282,9 +2271,6 @@ public void ContrastStretch(Percentage blackPoint, Percentage whitePoint) /// Thrown when an error is raised by ImageMagick. public void ContrastStretch(Percentage blackPoint, Percentage whitePoint, Channels channels) { - Throw.IfNegative(nameof(blackPoint), blackPoint); - Throw.IfNegative(nameof(whitePoint), whitePoint); - var contrast = CalculateContrastStretch(blackPoint, whitePoint); _nativeInstance.ContrastStretch(contrast.X, contrast.Y, channels); } @@ -2500,7 +2486,6 @@ public double Deskew(Percentage threshold) public double Deskew(IDeskewSettings settings) { Throw.IfNull(nameof(settings), settings); - Throw.IfNegative(nameof(settings), settings.Threshold); using var temporaryDefines = new TemporaryDefines(this); temporaryDefines.SetArtifact("deskew:auto-crop", settings.AutoCrop); @@ -3849,12 +3834,7 @@ public void LevelColors(IMagickColor blackColor, IMagickColorThe white point. /// Thrown when an error is raised by ImageMagick. public void LinearStretch(Percentage blackPoint, Percentage whitePoint) - { - Throw.IfNegative(nameof(blackPoint), blackPoint); - Throw.IfNegative(nameof(whitePoint), whitePoint); - - _nativeInstance.LinearStretch(PercentageHelper.ToQuantum(blackPoint), PercentageHelper.ToQuantum(whitePoint)); - } + => _nativeInstance.LinearStretch(PercentageHelper.ToQuantum(blackPoint), PercentageHelper.ToQuantum(whitePoint)); /// /// Rescales image with seam carving. @@ -4125,10 +4105,6 @@ public void Modulate(Percentage brightness, Percentage saturation) /// Thrown when an error is raised by ImageMagick. public void Modulate(Percentage brightness, Percentage saturation, Percentage hue) { - Throw.IfNegative(nameof(brightness), brightness); - Throw.IfNegative(nameof(saturation), saturation); - Throw.IfNegative(nameof(hue), hue); - var modulate = string.Format(CultureInfo.InvariantCulture, "{0}/{1}/{2}", brightness.ToDouble(), saturation.ToDouble(), hue.ToDouble()); _nativeInstance.Modulate(modulate); @@ -4698,14 +4674,7 @@ public void RandomThreshold(QuantumType low, QuantumType high, Channels channels /// Defines the maximum black threshold value. /// Thrown when an error is raised by ImageMagick. public void RangeThreshold(Percentage percentageLowBlack, Percentage percentageLowWhite, Percentage percentageHighWhite, Percentage percentageHighBlack) - { - Throw.IfNegative(nameof(percentageLowBlack), percentageLowBlack); - Throw.IfNegative(nameof(percentageLowWhite), percentageLowWhite); - Throw.IfNegative(nameof(percentageHighWhite), percentageHighWhite); - Throw.IfNegative(nameof(percentageHighBlack), percentageHighBlack); - - RangeThreshold(PercentageHelper.ToQuantumType(percentageLowBlack), PercentageHelper.ToQuantumType(percentageLowWhite), PercentageHelper.ToQuantumType(percentageHighWhite), PercentageHelper.ToQuantumType(percentageHighBlack)); - } + => RangeThreshold(PercentageHelper.ToQuantumType(percentageLowBlack), PercentageHelper.ToQuantumType(percentageLowWhite), PercentageHelper.ToQuantumType(percentageHighWhite), PercentageHelper.ToQuantumType(percentageHighBlack)); /// /// Applies soft and hard thresholding. @@ -6908,11 +6877,7 @@ public void WhiteThreshold(Percentage threshold) /// The channel(s) to make black. /// Thrown when an error is raised by ImageMagick. public void WhiteThreshold(Percentage threshold, Channels channels) - { - Throw.IfNegative(nameof(threshold), threshold); - - _nativeInstance.WhiteThreshold(threshold.ToString(), channels); - } + => _nativeInstance.WhiteThreshold(threshold.ToString(), channels); /// /// Writes the image to the specified file. diff --git a/src/Magick.NET/Types/MagickGeometry.cs b/src/Magick.NET/Types/MagickGeometry.cs index 1cbee421c4..e98e02be02 100644 --- a/src/Magick.NET/Types/MagickGeometry.cs +++ b/src/Magick.NET/Types/MagickGeometry.cs @@ -48,12 +48,7 @@ public MagickGeometry(int x, int y, int width, int height) /// The percentage of the width. /// The percentage of the height. public MagickGeometry(Percentage percentageWidth, Percentage percentageHeight) - { - Throw.IfNegative(nameof(percentageWidth), percentageWidth); - Throw.IfNegative(nameof(percentageHeight), percentageHeight); - - InitializeFromPercentage(0, 0, (int)percentageWidth, (int)percentageHeight); - } + => InitializeFromPercentage(0, 0, (int)percentageWidth, (int)percentageHeight); /// /// Initializes a new instance of the class using the specified offsets, width and height. @@ -63,12 +58,7 @@ public MagickGeometry(Percentage percentageWidth, Percentage percentageHeight) /// The percentage of the width. /// The percentage of the height. public MagickGeometry(int x, int y, Percentage percentageWidth, Percentage percentageHeight) - { - Throw.IfNegative(nameof(percentageWidth), percentageWidth); - Throw.IfNegative(nameof(percentageHeight), percentageHeight); - - InitializeFromPercentage(x, y, (int)percentageWidth, (int)percentageHeight); - } + => InitializeFromPercentage(x, y, (int)percentageWidth, (int)percentageHeight); /// /// Initializes a new instance of the class using the specified geometry. diff --git a/src/Shared/Throw.cs b/src/Shared/Throw.cs index e17f187438..2c10094fa6 100644 --- a/src/Shared/Throw.cs +++ b/src/Shared/Throw.cs @@ -73,6 +73,12 @@ public static void IfNegative(string paramName, int value) throw new ArgumentException("Value should not be negative.", paramName); } + public static void IfNegative(string paramName, double value) + { + if (value < 0.0) + throw new ArgumentException("Value should not be negative.", paramName); + } + public static void IfNegative(string paramName, Percentage value) { if ((double)value < 0.0) diff --git a/tests/Magick.NET.Core.Tests/Types/PercentageTests/TheConstructor.cs b/tests/Magick.NET.Core.Tests/Types/PercentageTests/TheConstructor.cs index 9429174ef1..2756d92843 100644 --- a/tests/Magick.NET.Core.Tests/Types/PercentageTests/TheConstructor.cs +++ b/tests/Magick.NET.Core.Tests/Types/PercentageTests/TheConstructor.cs @@ -1,6 +1,7 @@ // Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET. // Licensed under the Apache License, Version 2.0. +using System; using ImageMagick; using Xunit; @@ -10,6 +11,15 @@ public partial class PercentageTests { public class TheConstructor { + [Fact] + public void ShouldThrowExceptionWhenValueIsNegative() + { + Assert.Throws("value", () => new Percentage(-1.0)); + Assert.Throws("value", () => (Percentage)(-1.0)); + Assert.Throws("value", () => new Percentage(-1)); + Assert.Throws("value", () => (Percentage)(-1)); + } + [Fact] public void ShouldDefaultToZero() { @@ -30,12 +40,5 @@ public void ShouldHandleValueAbove100() var percentage = new Percentage(200.0); Assert.Equal("200%", percentage.ToString()); } - - [Fact] - public void ShouldHandleNegativeValue() - { - var percentage = new Percentage(-25); - Assert.Equal("-25%", percentage.ToString()); - } } } diff --git a/tests/Magick.NET.Tests/Factories/MagickGeometryFactoryTests/TheCreateMethod.cs b/tests/Magick.NET.Tests/Factories/MagickGeometryFactoryTests/TheCreateMethod.cs index 08e80fe593..760c39da1d 100644 --- a/tests/Magick.NET.Tests/Factories/MagickGeometryFactoryTests/TheCreateMethod.cs +++ b/tests/Magick.NET.Tests/Factories/MagickGeometryFactoryTests/TheCreateMethod.cs @@ -244,21 +244,5 @@ public void ShouldThrowExceptionWhenHeightIsNegative() Assert.Throws("height", () => factory.Create(5, -1)); Assert.Throws("height", () => factory.Create(0, 0, 5, -1)); } - - [Fact] - public void ShouldThrowExceptionWhenPercentageWidthIsNegative() - { - var factory = new MagickGeometryFactory(); - - Assert.Throws("percentageWidth", () => factory.Create(new Percentage(-1), new Percentage(10))); - } - - [Fact] - public void ShouldThrowExceptionWhenPercentageHeightIsNegative() - { - var factory = new MagickGeometryFactory(); - - Assert.Throws("percentageHeight", () => factory.Create(new Percentage(10), new Percentage(-1))); - } } } diff --git a/tests/Magick.NET.Tests/MagickImageTests/TheColorizeMethod.cs b/tests/Magick.NET.Tests/MagickImageTests/TheColorizeMethod.cs index 563504cb31..8157435f04 100644 --- a/tests/Magick.NET.Tests/MagickImageTests/TheColorizeMethod.cs +++ b/tests/Magick.NET.Tests/MagickImageTests/TheColorizeMethod.cs @@ -21,14 +21,6 @@ public void ShouldThrowExceptionWhenColorIsNull() Assert.Throws("color", () => image.Colorize(null, new Percentage(50))); } - [Fact] - public void ShouldThrowExceptionWhenPercentageIsNegative() - { - using var image = new MagickImage(); - - Assert.Throws("alpha", () => image.Colorize(MagickColors.Purple, new Percentage(-1))); - } - [Fact] public void ShouldColorizeTheImage() { @@ -49,30 +41,6 @@ public void ShouldThrowExceptionWhenColorIsNull() Assert.Throws("color", () => image.Colorize(null, new Percentage(25), new Percentage(50), new Percentage(75))); } - [Fact] - public void ShouldThrowExceptionWhenAlphaRedIsNegative() - { - using var image = new MagickImage(); - - Assert.Throws("alphaRed", () => image.Colorize(MagickColors.Purple, new Percentage(-1), new Percentage(50), new Percentage(75))); - } - - [Fact] - public void ShouldThrowExceptionWhenAlphaGreenIsNegative() - { - using var image = new MagickImage(); - - Assert.Throws("alphaGreen", () => image.Colorize(MagickColors.Purple, new Percentage(25), new Percentage(-1), new Percentage(75))); - } - - [Fact] - public void ShouldThrowExceptionWhenAlphaBlueIsNegative() - { - using var image = new MagickImage(); - - Assert.Throws("alphaBlue", () => image.Colorize(MagickColors.Purple, new Percentage(25), new Percentage(50), new Percentage(-1))); - } - [Fact] public void ShouldColorizeTheImage() { diff --git a/tests/Magick.NET.Tests/MagickImageTests/TheContrastStretchMethod.cs b/tests/Magick.NET.Tests/MagickImageTests/TheContrastStretchMethod.cs index 8c595d9d8c..6c71691cc3 100644 --- a/tests/Magick.NET.Tests/MagickImageTests/TheContrastStretchMethod.cs +++ b/tests/Magick.NET.Tests/MagickImageTests/TheContrastStretchMethod.cs @@ -11,30 +11,6 @@ public partial class MagickImageTests { public class TheContrastStretchMethod { - [Fact] - public void ShouldThrowExceptionWhenPercentageIsNull() - { - using var image = new MagickImage(); - - Assert.Throws("blackPoint", () => image.ContrastStretch(new Percentage(-1))); - } - - [Fact] - public void ShouldThrowExceptionWhenBlackPointIsNull() - { - using var image = new MagickImage(); - - Assert.Throws("blackPoint", () => image.ContrastStretch(new Percentage(-1), new Percentage(50))); - } - - [Fact] - public void ShouldThrowExceptionWhenWhitePointIsNull() - { - using var image = new MagickImage(); - - Assert.Throws("whitePoint", () => image.ContrastStretch(new Percentage(50), new Percentage(-1))); - } - [Fact] public void ShouldImproveTheContrast() { diff --git a/tests/Magick.NET.Tests/MagickImageTests/TheDeskewMethod.cs b/tests/Magick.NET.Tests/MagickImageTests/TheDeskewMethod.cs index a9eafca08e..832f8f7d9e 100644 --- a/tests/Magick.NET.Tests/MagickImageTests/TheDeskewMethod.cs +++ b/tests/Magick.NET.Tests/MagickImageTests/TheDeskewMethod.cs @@ -19,26 +19,6 @@ public void ShouldThrowExceptionWhenSettingsIsNull() Assert.Throws("settings", () => image.Deskew(null)); } - [Fact] - public void ShouldThrowExceptionWhenSettingsThresholdIsNegative() - { - var settings = new DeskewSettings - { - Threshold = new Percentage(-1), - }; - using var image = new MagickImage(); - - Assert.Throws("settings", () => image.Deskew(settings)); - } - - [Fact] - public void ShouldThrowExceptionWhenThresholdIsNegative() - { - using var image = new MagickImage(); - - Assert.Throws("settings", () => image.Deskew(new Percentage(-1))); - } - [Fact] public void ShouldDeskewTheImage() { diff --git a/tests/Magick.NET.Tests/MagickImageTests/TheInterpolativeResizeMethod.cs b/tests/Magick.NET.Tests/MagickImageTests/TheInterpolativeResizeMethod.cs index c6a2391c74..f8dbc908fe 100644 --- a/tests/Magick.NET.Tests/MagickImageTests/TheInterpolativeResizeMethod.cs +++ b/tests/Magick.NET.Tests/MagickImageTests/TheInterpolativeResizeMethod.cs @@ -25,27 +25,6 @@ public void ShouldThrowExceptionWhenHeightIsNegative() Assert.Throws("height", () => image.InterpolativeResize(32, -1, PixelInterpolateMethod.Mesh)); } - [Fact] - public void ShouldThrowExceptionWhenPercentageIsNegative() - { - using var image = new MagickImage(Files.RedPNG); - Assert.Throws("percentageWidth", () => image.InterpolativeResize(new Percentage(-1), PixelInterpolateMethod.Mesh)); - } - - [Fact] - public void ShouldThrowExceptionWhenPercentageWidthIsNegative() - { - using var image = new MagickImage(Files.RedPNG); - Assert.Throws("percentageWidth", () => image.InterpolativeResize(new Percentage(-1), new Percentage(10), PixelInterpolateMethod.Mesh)); - } - - [Fact] - public void ShouldThrowExceptionWhenPercentageHeightIsNegative() - { - using var image = new MagickImage(Files.RedPNG); - Assert.Throws("percentageHeight", () => image.InterpolativeResize(new Percentage(10), new Percentage(-1), PixelInterpolateMethod.Mesh)); - } - [Fact] public void ShouldResizeTheImage() { diff --git a/tests/Magick.NET.Tests/MagickImageTests/TheLiquidRescaleMethod.cs b/tests/Magick.NET.Tests/MagickImageTests/TheLiquidRescaleMethod.cs index daa19999b5..95825c1a76 100644 --- a/tests/Magick.NET.Tests/MagickImageTests/TheLiquidRescaleMethod.cs +++ b/tests/Magick.NET.Tests/MagickImageTests/TheLiquidRescaleMethod.cs @@ -99,32 +99,6 @@ public void ShouldResizeTheImage() public class WithPercentage { - [Fact] - public void ShouldThrowExceptionWhenPercentageIsNegative() - { - var percentage = new Percentage(-1); - using var image = new MagickImage(Files.MagickNETIconPNG); - Assert.Throws("percentageWidth", () => image.LiquidRescale(percentage)); - } - - [Fact] - public void ShouldThrowExceptionWhenPercentageWidthIsNegative() - { - var percentageWidth = new Percentage(-1); - var percentageHeight = new Percentage(1); - using var image = new MagickImage(Files.MagickNETIconPNG); - Assert.Throws("percentageWidth", () => image.LiquidRescale(percentageWidth, percentageHeight)); - } - - [Fact] - public void ShouldThrowExceptionWhenPercentageHeightIsNegative() - { - var percentageWidth = new Percentage(1); - var percentageHeight = new Percentage(-1); - using var image = new MagickImage(Files.MagickNETIconPNG); - Assert.Throws("percentageHeight", () => image.LiquidRescale(percentageWidth, percentageHeight)); - } - [Fact] public void ShouldResizeTheImage() { @@ -148,22 +122,6 @@ public void ShouldIgnoreTheAspectRatio() public class WithPercentageAndRigidity { - [Fact] - public void ShouldThrowExceptionWhenPercentageWidthIsNegative() - { - using var image = new MagickImage(Files.MagickNETIconPNG); - - Assert.Throws("percentageWidth", () => image.LiquidRescale(new Percentage(-1), new Percentage(1), 0.0, 0.0)); - } - - [Fact] - public void ShouldThrowExceptionWhenPercentageHeightIsNegative() - { - using var image = new MagickImage(Files.MagickNETIconPNG); - - Assert.Throws("percentageHeight", () => image.LiquidRescale(new Percentage(1), new Percentage(-1), 0.0, 0.0)); - } - [Fact] public void ShouldApplyTheRigidity() { diff --git a/tests/Magick.NET.Tests/MagickImageTests/TheRangeThresholdMethod.cs b/tests/Magick.NET.Tests/MagickImageTests/TheRangeThresholdMethod.cs index db90ee494c..f01c76d31f 100644 --- a/tests/Magick.NET.Tests/MagickImageTests/TheRangeThresholdMethod.cs +++ b/tests/Magick.NET.Tests/MagickImageTests/TheRangeThresholdMethod.cs @@ -23,38 +23,6 @@ public class TheRangeThresholdMethod { public class WithPercentage { - [Fact] - public void ShouldThrowExceptionWhenLowBlackIsNegative() - { - using var image = new MagickImage(MagickColors.Red, 1, 1); - - Assert.Throws("percentageLowBlack", () => image.RangeThreshold(new Percentage(-1), new Percentage(0), new Percentage(0), new Percentage(0))); - } - - [Fact] - public void ShouldThrowExceptionWhenLowWhiteIsNegative() - { - using var image = new MagickImage(MagickColors.Red, 1, 1); - - Assert.Throws("percentageLowWhite", () => image.RangeThreshold(new Percentage(0), new Percentage(-1), new Percentage(0), new Percentage(0))); - } - - [Fact] - public void ShouldThrowExceptionWhenHighWhiteIsNegative() - { - using var image = new MagickImage(MagickColors.Red, 1, 1); - - Assert.Throws("percentageHighWhite", () => image.RangeThreshold(new Percentage(0), new Percentage(0), new Percentage(-1), new Percentage(0))); - } - - [Fact] - public void ShouldThrowExceptionWhenHighBlackIsNegative() - { - using var image = new MagickImage(MagickColors.Red, 1, 1); - - Assert.Throws("percentageHighBlack", () => image.RangeThreshold(new Percentage(0), new Percentage(0), new Percentage(0), new Percentage(-1))); - } - [Fact] public void ShouldChangeTheImage() { diff --git a/tests/Magick.NET.Tests/MagickImageTests/TheResizeMethod.cs b/tests/Magick.NET.Tests/MagickImageTests/TheResizeMethod.cs index d3bbe3e023..86cfc315bc 100644 --- a/tests/Magick.NET.Tests/MagickImageTests/TheResizeMethod.cs +++ b/tests/Magick.NET.Tests/MagickImageTests/TheResizeMethod.cs @@ -93,35 +93,6 @@ public void ShouldResizeToSmallerArea() public class WithPercentage { - [Fact] - public void ShouldThrowExceptionWhenPercentageIsNegative() - { - var percentage = new Percentage(-0.5); - using var image = new MagickImage(Files.MagickNETIconPNG); - - Assert.Throws("percentageWidth", () => image.Resize(percentage)); - } - - [Fact] - public void ShouldThrowExceptionWhenPercentageWidthIsNegative() - { - var percentageWidth = new Percentage(-0.5); - var percentageHeight = new Percentage(10); - using var image = new MagickImage(Files.MagickNETIconPNG); - - Assert.Throws("percentageWidth", () => image.Resize(percentageWidth, percentageHeight)); - } - - [Fact] - public void ShouldThrowExceptionWhenPercentageHeightIsNegative() - { - var percentageWidth = new Percentage(10); - var percentageHeight = new Percentage(-0.5); - using var image = new MagickImage(Files.MagickNETIconPNG); - - Assert.Throws("percentageHeight", () => image.Resize(percentageWidth, percentageHeight)); - } - [Fact] public void ShouldResizeTheImage() { diff --git a/tests/Magick.NET.Tests/MagickImageTests/TheSampleMethod.cs b/tests/Magick.NET.Tests/MagickImageTests/TheSampleMethod.cs index 511d5138e2..0fdd4b64e6 100644 --- a/tests/Magick.NET.Tests/MagickImageTests/TheSampleMethod.cs +++ b/tests/Magick.NET.Tests/MagickImageTests/TheSampleMethod.cs @@ -61,35 +61,6 @@ public void ShouldResizeTheImage() public class WithPercentage { - [Fact] - public void ShouldThrowExceptionWhenPercentageIsNegative() - { - var percentage = new Percentage(-0.5); - using var image = new MagickImage(Files.Builtin.Logo); - - Assert.Throws("percentageWidth", () => image.Sample(percentage)); - } - - [Fact] - public void ShouldThrowExceptionWhenPercentageWidthIsNegative() - { - var percentageWidth = new Percentage(-0.5); - var percentageHeight = new Percentage(10); - using var image = new MagickImage(Files.Builtin.Logo); - - Assert.Throws("percentageWidth", () => image.Sample(percentageWidth, percentageHeight)); - } - - [Fact] - public void ShouldThrowExceptionWhenPercentageHeightIsNegative() - { - var percentageWidth = new Percentage(10); - var percentageHeight = new Percentage(-0.5); - using var image = new MagickImage(Files.Builtin.Logo); - - Assert.Throws("percentageHeight", () => image.Sample(percentageWidth, percentageHeight)); - } - [Fact] public void ShouldUseTheSpecifiedPercentage() { diff --git a/tests/Magick.NET.Tests/MagickImageTests/TheScaleMethod.cs b/tests/Magick.NET.Tests/MagickImageTests/TheScaleMethod.cs index 5b77ab2f77..e9ff532bb0 100644 --- a/tests/Magick.NET.Tests/MagickImageTests/TheScaleMethod.cs +++ b/tests/Magick.NET.Tests/MagickImageTests/TheScaleMethod.cs @@ -61,35 +61,6 @@ public void ShouldResizeTheImage() public class WithPercentage { - [Fact] - public void ShouldThrowExceptionWhenPercentageIsNegative() - { - var percentage = new Percentage(-0.5); - using var image = new MagickImage(Files.Builtin.Logo); - - Assert.Throws("percentageWidth", () => image.Scale(percentage)); - } - - [Fact] - public void ShouldThrowExceptionWhenPercentageWidthIsNegative() - { - var percentageWidth = new Percentage(-0.5); - var percentageHeight = new Percentage(10); - using var image = new MagickImage(Files.Builtin.Logo); - - Assert.Throws("percentageWidth", () => image.Scale(percentageWidth, percentageHeight)); - } - - [Fact] - public void ShouldThrowExceptionWhenPercentageHeightIsNegative() - { - var percentageWidth = new Percentage(10); - var percentageHeight = new Percentage(-0.5); - using var image = new MagickImage(Files.Builtin.Logo); - - Assert.Throws("percentageHeight", () => image.Scale(percentageWidth, percentageHeight)); - } - [Fact] public void ShouldUseTheSpecifiedPercentage() { diff --git a/tests/Magick.NET.Tests/MagickImageTests/TheThumbnailMethod.cs b/tests/Magick.NET.Tests/MagickImageTests/TheThumbnailMethod.cs index f4ee1dd72c..122a2748fd 100644 --- a/tests/Magick.NET.Tests/MagickImageTests/TheThumbnailMethod.cs +++ b/tests/Magick.NET.Tests/MagickImageTests/TheThumbnailMethod.cs @@ -61,32 +61,6 @@ public void ShouldCreateThumbnailOfTheImage() public class WithPercentage { - [Fact] - public void ShouldThrowExceptionWhenPercentageIsNegative() - { - var percentage = new Percentage(-0.5); - using var image = new MagickImage(); - Assert.Throws("percentageWidth", () => image.Thumbnail(percentage)); - } - - [Fact] - public void ShouldThrowExceptionWhenPercentageWidthIsNegative() - { - var percentageWidth = new Percentage(-0.5); - var percentageHeight = new Percentage(10); - using var image = new MagickImage(); - Assert.Throws("percentageWidth", () => image.Thumbnail(percentageWidth, percentageHeight)); - } - - [Fact] - public void ShouldThrowExceptionWhenPercentageHeightIsNegative() - { - var percentageWidth = new Percentage(10); - var percentageHeight = new Percentage(-0.5); - using var image = new MagickImage(); - Assert.Throws("percentageHeight", () => image.Thumbnail(percentageWidth, percentageHeight)); - } - [Fact] public void ShouldCreateThumbnailOfTheImageWithTheSpecifiedPercentage() { diff --git a/tests/Magick.NET.Tests/ResourceLimitsTests/TheLimitMemoryMethod.cs b/tests/Magick.NET.Tests/ResourceLimitsTests/TheLimitMemoryMethod.cs index 981aedd554..7f08f8f5e7 100644 --- a/tests/Magick.NET.Tests/ResourceLimitsTests/TheLimitMemoryMethod.cs +++ b/tests/Magick.NET.Tests/ResourceLimitsTests/TheLimitMemoryMethod.cs @@ -12,12 +12,6 @@ public partial class ResourceLimitsTests [Collection(nameof(RunTestsSeparately))] public class TheLimitMemoryMethod { - [Fact] - public void ShouldThrowExceptionWhenValueIsNegative() - { - Assert.Throws("percentage", () => ResourceLimits.LimitMemory(new Percentage(-0.99))); - } - [Fact] public void ShouldThrowExceptionWhenValueIsTooHigh() { From 472346a0ff83f6ab10fa1ee506bac1962857961e Mon Sep 17 00:00:00 2001 From: Florian Levis Date: Thu, 7 Mar 2024 19:35:27 +0100 Subject: [PATCH 2/2] chore: update tests, update doc --- src/Magick.NET.Core/Types/Percentage.cs | 8 ++++---- src/Magick.NET/ResourceLimits.cs | 6 +----- .../ResourceLimitsTests/TheLimitMemoryMethod.cs | 6 ------ 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/Magick.NET.Core/Types/Percentage.cs b/src/Magick.NET.Core/Types/Percentage.cs index 99051e30f6..5716580618 100644 --- a/src/Magick.NET.Core/Types/Percentage.cs +++ b/src/Magick.NET.Core/Types/Percentage.cs @@ -16,7 +16,7 @@ namespace ImageMagick; /// /// Initializes a new instance of the struct. /// - /// The value (0% = 0.0, 100% = 100.0). + /// The value (0% = 0.0, 100% = 100.0, 142.42% = 142.42). public Percentage(double value) { Throw.IfNegative(nameof(value), value); @@ -27,7 +27,7 @@ public Percentage(double value) /// /// Initializes a new instance of the struct. /// - /// The value (0% = 0, 100% = 100). + /// The value (0% = 0, 100% = 100, 142% = 142). public Percentage(int value) { Throw.IfNegative(nameof(value), value); @@ -38,14 +38,14 @@ public Percentage(int value) /// /// Converts the specified double to an instance of this type. /// - /// The value (0% = 0, 100% = 100). + /// The value (0% = 0.0, 100% = 100.0, 142.42% = 142.42). public static explicit operator Percentage(double value) => new Percentage(value); /// /// Converts the specified int to an instance of this type. /// - /// The value (0% = 0, 100% = 100). + /// The value (0% = 0, 100% = 100, 142% = 142). public static explicit operator Percentage(int value) => new Percentage(value); diff --git a/src/Magick.NET/ResourceLimits.cs b/src/Magick.NET/ResourceLimits.cs index d15ee57c15..b298316d05 100644 --- a/src/Magick.NET/ResourceLimits.cs +++ b/src/Magick.NET/ResourceLimits.cs @@ -204,11 +204,7 @@ ulong IResourceLimits.Width /// /// The percentage to use. public static void LimitMemory(Percentage percentage) - { - Throw.IfOutOfRange(nameof(percentage), percentage); - - NativeResourceLimits.LimitMemory((double)percentage / 100.0); - } + => NativeResourceLimits.LimitMemory((double)percentage / 100.0); /// /// Set the maximum percentage of memory that can be used for image data. This also changes diff --git a/tests/Magick.NET.Tests/ResourceLimitsTests/TheLimitMemoryMethod.cs b/tests/Magick.NET.Tests/ResourceLimitsTests/TheLimitMemoryMethod.cs index 7f08f8f5e7..1e71695dde 100644 --- a/tests/Magick.NET.Tests/ResourceLimitsTests/TheLimitMemoryMethod.cs +++ b/tests/Magick.NET.Tests/ResourceLimitsTests/TheLimitMemoryMethod.cs @@ -12,12 +12,6 @@ public partial class ResourceLimitsTests [Collection(nameof(RunTestsSeparately))] public class TheLimitMemoryMethod { - [Fact] - public void ShouldThrowExceptionWhenValueIsTooHigh() - { - Assert.Throws("percentage", () => ResourceLimits.LimitMemory(new Percentage(100.1))); - } - [Fact] public void ShouldChangeAreaAndMemory() {