Skip to content

Commit d390da5

Browse files
committed
Updated unit tests due to changes in ImnageMagick and added extra unit test for the compare metrics.
1 parent 4838476 commit d390da5

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

tests/Magick.NET.Tests/MagickImageTests/TheCompareMethod.cs

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,34 @@ public void ShouldUseTheColorFuzz()
155155
ColorAssert.Equal(new MagickColor("#fd2ff729f28b"), diff, 0, 0);
156156
}
157157

158+
[Theory]
159+
[InlineData(ErrorMetric.Undefined, 0.3653)]
160+
[InlineData(ErrorMetric.Absolute, 0.2905)]
161+
[InlineData(ErrorMetric.DotProductCorrelation, 0.4668)]
162+
[InlineData(ErrorMetric.Fuzz, 0.4662)]
163+
[InlineData(ErrorMetric.MeanAbsolute, 0.1747)]
164+
[InlineData(ErrorMetric.MeanErrorPerPixel, 0.1747)]
165+
[InlineData(ErrorMetric.MeanSquared, 0.1334)]
166+
[InlineData(ErrorMetric.NormalizedCrossCorrelation, 0.4668)]
167+
[InlineData(ErrorMetric.PeakAbsolute, 1)]
168+
[InlineData(ErrorMetric.PeakSignalToNoiseRatio, 0.0728)]
169+
[InlineData(ErrorMetric.PerceptualHash, 0)]
170+
[InlineData(ErrorMetric.RootMeanSquared, 0.3653)]
171+
[InlineData(ErrorMetric.StructuralSimilarity, 0.1546)]
172+
[InlineData(ErrorMetric.StructuralDissimilarity, 0.4226)]
173+
public void ShouldReturnTheCorrectValueForEachErrorMetric(ErrorMetric errorMetric, double expectedResult)
174+
{
175+
using var image = new MagickImage(Files.Builtin.Logo);
176+
using var other = image.CloneAndMutate(image => image.Rotate(180));
177+
178+
var result = image.Compare(other, errorMetric);
179+
Assert.InRange(result, expectedResult, expectedResult + 0.0001);
180+
}
181+
158182
[Theory]
159183
[InlineData(ErrorMetric.Undefined, 0.4726)]
160184
[InlineData(ErrorMetric.Absolute, 0.3944, 0.3945)]
185+
[InlineData(ErrorMetric.DotProductCorrelation, 0.4748)]
161186
[InlineData(ErrorMetric.Fuzz, 0.5677, 0.5676)]
162187
[InlineData(ErrorMetric.MeanAbsolute, 0.2714)]
163188
[InlineData(ErrorMetric.MeanErrorPerPixel, 0.2714)]
@@ -167,31 +192,40 @@ public void ShouldUseTheColorFuzz()
167192
[InlineData(ErrorMetric.PeakSignalToNoiseRatio, 0.0542)]
168193
[InlineData(ErrorMetric.PerceptualHash, 0)]
169194
[InlineData(ErrorMetric.RootMeanSquared, 0.4726)]
170-
[InlineData(ErrorMetric.StructuralSimilarity, 0.4220)]
171-
[InlineData(ErrorMetric.StructuralDissimilarity, 0.2889)]
172-
public void ShouldReturnTheCorrectValueForEachErrorMetric(ErrorMetric errorMetric, double expectedResult, double? expectedArm64Result = null)
195+
[InlineData(ErrorMetric.StructuralSimilarity, 0.2889)]
196+
[InlineData(ErrorMetric.StructuralDissimilarity, 0.3555)]
197+
public void ShouldReturnTheCorrectValueForEachErrorMetricForImageWithAlphaChannel(ErrorMetric errorMetric, double expectedResult, double? expectedArm64Result = null)
173198
{
174199
using var image = new MagickImage(Files.MagickNETIconPNG);
175200
using var other = image.CloneAndMutate(image => image.Rotate(180));
176201

177202
var result = image.Compare(other, errorMetric);
178-
if (expectedArm64Result != null && (TestRuntime.IsLinuxArm64 || TestRuntime.IsMacOSArm64))
203+
if (expectedArm64Result != null && TestRuntime.IsArm64)
179204
Assert.InRange(result, expectedArm64Result.Value, expectedArm64Result.Value + 0.0001);
180205
else
181206
Assert.InRange(result, expectedResult, expectedResult + 0.0001);
182207
}
183208

184-
[Theory]
185-
[InlineData(ErrorMetric.PhaseCorrelation)]
186-
[InlineData(ErrorMetric.DotProductCorrelation)]
187-
public void ShouldThrowExceptionWhenErrorMetricIsNotSupported(ErrorMetric errorMetric)
209+
[Fact]
210+
public void ShouldReturnTheCorrectValueForPhaseCorrelationErrorMetric()
188211
{
189212
using var image = new MagickImage(Files.Builtin.Logo);
213+
image.Resize(64, 64);
190214
using var other = image.CloneAndMutate(image => image.Rotate(180));
191215

192-
var exception = Assert.Throws<MagickImageErrorException>(() => image.Compare(other, errorMetric));
216+
var result = image.Compare(other, ErrorMetric.PhaseCorrelation);
217+
Assert.InRange(result, 0.1871, 0.1872);
218+
}
219+
220+
[Fact]
221+
public void ShouldReturnTheCorrectValueForPhaseCorrelationErrorMetricWithAlphaChannel()
222+
{
223+
using var image = new MagickImage(Files.MagickNETIconPNG);
224+
image.Resize(64, 64);
225+
using var other = image.CloneAndMutate(image => image.Rotate(180));
193226

194-
Assert.Contains("metric not supported", exception.Message);
227+
var result = image.Compare(other, ErrorMetric.PhaseCorrelation);
228+
Assert.InRange(result, 0.0085, 0.0086);
195229
}
196230
}
197231
}

tests/Magick.NET.Tests/TestHelpers/TestRuntime.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ static TestRuntime()
1313
var isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
1414
var isMacOS = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
1515

16-
IsLinuxArm64 = isLinux && Runtime.Architecture == Architecture.Arm64;
17-
IsMacOSArm64 = isMacOS && Runtime.Architecture == Architecture.Arm64;
16+
IsArm64 = Runtime.Architecture == Architecture.Arm64;
17+
IsLinuxArm64 = isLinux && IsArm64;
18+
IsMacOSArm64 = isMacOS && IsArm64;
1819
}
1920

21+
public static bool IsArm64 { get; }
22+
2023
public static bool IsLinuxArm64 { get; }
2124

2225
public static bool IsMacOSArm64 { get; }

0 commit comments

Comments
 (0)