Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add adler tests with and without intrinsics
  • Loading branch information
brianpopow committed Feb 21, 2022
commit cbf46759194b228660535b8d21122420337e88ed
36 changes: 28 additions & 8 deletions tests/ImageSharp.Tests/Formats/Png/Adler32Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using SixLabors.ImageSharp.Compression.Zlib;
using SixLabors.ImageSharp.Tests.TestUtilities;
using Xunit;
using SharpAdler32 = ICSharpCode.SharpZipLib.Checksum.Adler32;

Expand All @@ -15,10 +16,7 @@ public class Adler32Tests
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
public void ReturnsCorrectWhenEmpty(uint input)
{
Assert.Equal(input, Adler32.Calculate(input, default));
}
public void CalculateAdler_ReturnsCorrectWhenEmpty(uint input) => Assert.Equal(input, Adler32.Calculate(input, default));

[Theory]
[InlineData(0)]
Expand All @@ -28,24 +26,46 @@ public void ReturnsCorrectWhenEmpty(uint input)
[InlineData(1024 + 15)]
[InlineData(2034)]
[InlineData(4096)]
public void MatchesReference(int length)
public void CalculateAdler_MatchesReference(int length) => CalculateAdlerAndCompareToReference(length);

private static void CalculateAdlerAndCompareToReference(int length)
{
var data = GetBuffer(length);
// arrange
byte[] data = GetBuffer(length);
var adler = new SharpAdler32();
adler.Update(data);

long expected = adler.Value;

// act
long actual = Adler32.Calculate(data);

// assert
Assert.Equal(expected, actual);
}

private static byte[] GetBuffer(int length)
{
var data = new byte[length];
byte[] data = new byte[length];
new Random(1).NextBytes(data);

return data;
}

#if SUPPORTS_RUNTIME_INTRINSICS
[Fact]
public void RunCalculateAdlerTest_WithHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunCalculateAdlerTest, HwIntrinsics.AllowAll);

[Fact]
public void RunCalculateAdlerTest_WithoutHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunCalculateAdlerTest, HwIntrinsics.DisableHWIntrinsic);

private static void RunCalculateAdlerTest()
{
int[] testData = { 0, 8, 215, 1024, 1024 + 15, 2034, 4096 };
for (int i = 0; i < testData.Length; i++)
{
CalculateAdlerAndCompareToReference(testData[i]);
}
}
#endif
}
}